read.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import { describe, expect, test } from "bun:test"
  2. import path from "path"
  3. import { ReadTool } from "../../src/tool/read"
  4. import { Instance } from "../../src/project/instance"
  5. import { tmpdir } from "../fixture/fixture"
  6. import { PermissionNext } from "../../src/permission/next"
  7. import { Agent } from "../../src/agent/agent"
  8. const FIXTURES_DIR = path.join(import.meta.dir, "fixtures")
  9. const ctx = {
  10. sessionID: "test",
  11. messageID: "",
  12. callID: "",
  13. agent: "build",
  14. abort: AbortSignal.any([]),
  15. messages: [],
  16. metadata: () => {},
  17. ask: async () => {},
  18. }
  19. describe("tool.read external_directory permission", () => {
  20. test("allows reading absolute path inside project directory", async () => {
  21. await using tmp = await tmpdir({
  22. init: async (dir) => {
  23. await Bun.write(path.join(dir, "test.txt"), "hello world")
  24. },
  25. })
  26. await Instance.provide({
  27. directory: tmp.path,
  28. fn: async () => {
  29. const read = await ReadTool.init()
  30. const result = await read.execute({ filePath: path.join(tmp.path, "test.txt") }, ctx)
  31. expect(result.output).toContain("hello world")
  32. },
  33. })
  34. })
  35. test("allows reading file in subdirectory inside project directory", async () => {
  36. await using tmp = await tmpdir({
  37. init: async (dir) => {
  38. await Bun.write(path.join(dir, "subdir", "test.txt"), "nested content")
  39. },
  40. })
  41. await Instance.provide({
  42. directory: tmp.path,
  43. fn: async () => {
  44. const read = await ReadTool.init()
  45. const result = await read.execute({ filePath: path.join(tmp.path, "subdir", "test.txt") }, ctx)
  46. expect(result.output).toContain("nested content")
  47. },
  48. })
  49. })
  50. test("asks for external_directory permission when reading absolute path outside project", async () => {
  51. await using outerTmp = await tmpdir({
  52. init: async (dir) => {
  53. await Bun.write(path.join(dir, "secret.txt"), "secret data")
  54. },
  55. })
  56. await using tmp = await tmpdir({ git: true })
  57. await Instance.provide({
  58. directory: tmp.path,
  59. fn: async () => {
  60. const read = await ReadTool.init()
  61. const requests: Array<Omit<PermissionNext.Request, "id" | "sessionID" | "tool">> = []
  62. const testCtx = {
  63. ...ctx,
  64. ask: async (req: Omit<PermissionNext.Request, "id" | "sessionID" | "tool">) => {
  65. requests.push(req)
  66. },
  67. }
  68. await read.execute({ filePath: path.join(outerTmp.path, "secret.txt") }, testCtx)
  69. const extDirReq = requests.find((r) => r.permission === "external_directory")
  70. expect(extDirReq).toBeDefined()
  71. expect(extDirReq!.patterns.some((p) => p.includes(outerTmp.path))).toBe(true)
  72. },
  73. })
  74. })
  75. test("asks for external_directory permission when reading relative path outside project", async () => {
  76. await using tmp = await tmpdir({ git: true })
  77. await Instance.provide({
  78. directory: tmp.path,
  79. fn: async () => {
  80. const read = await ReadTool.init()
  81. const requests: Array<Omit<PermissionNext.Request, "id" | "sessionID" | "tool">> = []
  82. const testCtx = {
  83. ...ctx,
  84. ask: async (req: Omit<PermissionNext.Request, "id" | "sessionID" | "tool">) => {
  85. requests.push(req)
  86. },
  87. }
  88. // This will fail because file doesn't exist, but we can check if permission was asked
  89. await read.execute({ filePath: "../outside.txt" }, testCtx).catch(() => {})
  90. const extDirReq = requests.find((r) => r.permission === "external_directory")
  91. expect(extDirReq).toBeDefined()
  92. },
  93. })
  94. })
  95. test("does not ask for external_directory permission when reading inside project", async () => {
  96. await using tmp = await tmpdir({
  97. git: true,
  98. init: async (dir) => {
  99. await Bun.write(path.join(dir, "internal.txt"), "internal content")
  100. },
  101. })
  102. await Instance.provide({
  103. directory: tmp.path,
  104. fn: async () => {
  105. const read = await ReadTool.init()
  106. const requests: Array<Omit<PermissionNext.Request, "id" | "sessionID" | "tool">> = []
  107. const testCtx = {
  108. ...ctx,
  109. ask: async (req: Omit<PermissionNext.Request, "id" | "sessionID" | "tool">) => {
  110. requests.push(req)
  111. },
  112. }
  113. await read.execute({ filePath: path.join(tmp.path, "internal.txt") }, testCtx)
  114. const extDirReq = requests.find((r) => r.permission === "external_directory")
  115. expect(extDirReq).toBeUndefined()
  116. },
  117. })
  118. })
  119. })
  120. describe("tool.read env file permissions", () => {
  121. const cases: [string, boolean][] = [
  122. [".env", true],
  123. [".env.local", true],
  124. [".env.production", true],
  125. [".env.development.local", true],
  126. [".env.example", false],
  127. [".envrc", false],
  128. ["environment.ts", false],
  129. ]
  130. describe.each(["build", "plan"])("agent=%s", (agentName) => {
  131. test.each(cases)("%s asks=%s", async (filename, shouldAsk) => {
  132. await using tmp = await tmpdir({
  133. init: (dir) => Bun.write(path.join(dir, filename), "content"),
  134. })
  135. await Instance.provide({
  136. directory: tmp.path,
  137. fn: async () => {
  138. const agent = await Agent.get(agentName)
  139. let askedForEnv = false
  140. const ctxWithPermissions = {
  141. ...ctx,
  142. ask: async (req: Omit<PermissionNext.Request, "id" | "sessionID" | "tool">) => {
  143. for (const pattern of req.patterns) {
  144. const rule = PermissionNext.evaluate(req.permission, pattern, agent.permission)
  145. if (rule.action === "ask" && req.permission === "read") {
  146. askedForEnv = true
  147. }
  148. if (rule.action === "deny") {
  149. throw new PermissionNext.DeniedError(agent.permission)
  150. }
  151. }
  152. },
  153. }
  154. const read = await ReadTool.init()
  155. await read.execute({ filePath: path.join(tmp.path, filename) }, ctxWithPermissions)
  156. expect(askedForEnv).toBe(shouldAsk)
  157. },
  158. })
  159. })
  160. })
  161. })
  162. describe("tool.read truncation", () => {
  163. test("truncates large file by bytes and sets truncated metadata", async () => {
  164. await using tmp = await tmpdir({
  165. init: async (dir) => {
  166. const base = await Bun.file(path.join(FIXTURES_DIR, "models-api.json")).text()
  167. const target = 60 * 1024
  168. const content = base.length >= target ? base : base.repeat(Math.ceil(target / base.length))
  169. await Bun.write(path.join(dir, "large.json"), content)
  170. },
  171. })
  172. await Instance.provide({
  173. directory: tmp.path,
  174. fn: async () => {
  175. const read = await ReadTool.init()
  176. const result = await read.execute({ filePath: path.join(tmp.path, "large.json") }, ctx)
  177. expect(result.metadata.truncated).toBe(true)
  178. expect(result.output).toContain("Output truncated at")
  179. expect(result.output).toContain("bytes")
  180. },
  181. })
  182. })
  183. test("truncates by line count when limit is specified", async () => {
  184. await using tmp = await tmpdir({
  185. init: async (dir) => {
  186. const lines = Array.from({ length: 100 }, (_, i) => `line${i}`).join("\n")
  187. await Bun.write(path.join(dir, "many-lines.txt"), lines)
  188. },
  189. })
  190. await Instance.provide({
  191. directory: tmp.path,
  192. fn: async () => {
  193. const read = await ReadTool.init()
  194. const result = await read.execute({ filePath: path.join(tmp.path, "many-lines.txt"), limit: 10 }, ctx)
  195. expect(result.metadata.truncated).toBe(true)
  196. expect(result.output).toContain("File has more lines")
  197. expect(result.output).toContain("line0")
  198. expect(result.output).toContain("line9")
  199. expect(result.output).not.toContain("line10")
  200. },
  201. })
  202. })
  203. test("does not truncate small file", async () => {
  204. await using tmp = await tmpdir({
  205. init: async (dir) => {
  206. await Bun.write(path.join(dir, "small.txt"), "hello world")
  207. },
  208. })
  209. await Instance.provide({
  210. directory: tmp.path,
  211. fn: async () => {
  212. const read = await ReadTool.init()
  213. const result = await read.execute({ filePath: path.join(tmp.path, "small.txt") }, ctx)
  214. expect(result.metadata.truncated).toBe(false)
  215. expect(result.output).toContain("End of file")
  216. },
  217. })
  218. })
  219. test("respects offset parameter", async () => {
  220. await using tmp = await tmpdir({
  221. init: async (dir) => {
  222. const lines = Array.from({ length: 20 }, (_, i) => `line${i}`).join("\n")
  223. await Bun.write(path.join(dir, "offset.txt"), lines)
  224. },
  225. })
  226. await Instance.provide({
  227. directory: tmp.path,
  228. fn: async () => {
  229. const read = await ReadTool.init()
  230. const result = await read.execute({ filePath: path.join(tmp.path, "offset.txt"), offset: 10, limit: 5 }, ctx)
  231. expect(result.output).toContain("line10")
  232. expect(result.output).toContain("line14")
  233. expect(result.output).not.toContain("line0")
  234. expect(result.output).not.toContain("line15")
  235. },
  236. })
  237. })
  238. test("truncates long lines", async () => {
  239. await using tmp = await tmpdir({
  240. init: async (dir) => {
  241. const longLine = "x".repeat(3000)
  242. await Bun.write(path.join(dir, "long-line.txt"), longLine)
  243. },
  244. })
  245. await Instance.provide({
  246. directory: tmp.path,
  247. fn: async () => {
  248. const read = await ReadTool.init()
  249. const result = await read.execute({ filePath: path.join(tmp.path, "long-line.txt") }, ctx)
  250. expect(result.output).toContain("...")
  251. expect(result.output.length).toBeLessThan(3000)
  252. },
  253. })
  254. })
  255. test("image files set truncated to false", async () => {
  256. await using tmp = await tmpdir({
  257. init: async (dir) => {
  258. // 1x1 red PNG
  259. const png = Buffer.from(
  260. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==",
  261. "base64",
  262. )
  263. await Bun.write(path.join(dir, "image.png"), png)
  264. },
  265. })
  266. await Instance.provide({
  267. directory: tmp.path,
  268. fn: async () => {
  269. const read = await ReadTool.init()
  270. const result = await read.execute({ filePath: path.join(tmp.path, "image.png") }, ctx)
  271. expect(result.metadata.truncated).toBe(false)
  272. expect(result.attachments).toBeDefined()
  273. expect(result.attachments?.length).toBe(1)
  274. },
  275. })
  276. })
  277. test("large image files are properly attached without error", async () => {
  278. await Instance.provide({
  279. directory: FIXTURES_DIR,
  280. fn: async () => {
  281. const read = await ReadTool.init()
  282. const result = await read.execute({ filePath: path.join(FIXTURES_DIR, "large-image.png") }, ctx)
  283. expect(result.metadata.truncated).toBe(false)
  284. expect(result.attachments).toBeDefined()
  285. expect(result.attachments?.length).toBe(1)
  286. expect(result.attachments?.[0].type).toBe("file")
  287. },
  288. })
  289. })
  290. test(".fbs files (FlatBuffers schema) are read as text, not images", async () => {
  291. await using tmp = await tmpdir({
  292. init: async (dir) => {
  293. // FlatBuffers schema content
  294. const fbsContent = `namespace MyGame;
  295. table Monster {
  296. pos:Vec3;
  297. name:string;
  298. inventory:[ubyte];
  299. }
  300. root_type Monster;`
  301. await Bun.write(path.join(dir, "schema.fbs"), fbsContent)
  302. },
  303. })
  304. await Instance.provide({
  305. directory: tmp.path,
  306. fn: async () => {
  307. const read = await ReadTool.init()
  308. const result = await read.execute({ filePath: path.join(tmp.path, "schema.fbs") }, ctx)
  309. // Should be read as text, not as image
  310. expect(result.attachments).toBeUndefined()
  311. expect(result.output).toContain("namespace MyGame")
  312. expect(result.output).toContain("table Monster")
  313. },
  314. })
  315. })
  316. })
  317. describe("tool.read loaded instructions", () => {
  318. test("loads AGENTS.md from parent directory and includes in metadata", async () => {
  319. await using tmp = await tmpdir({
  320. init: async (dir) => {
  321. await Bun.write(path.join(dir, "subdir", "AGENTS.md"), "# Test Instructions\nDo something special.")
  322. await Bun.write(path.join(dir, "subdir", "nested", "test.txt"), "test content")
  323. },
  324. })
  325. await Instance.provide({
  326. directory: tmp.path,
  327. fn: async () => {
  328. const read = await ReadTool.init()
  329. const result = await read.execute({ filePath: path.join(tmp.path, "subdir", "nested", "test.txt") }, ctx)
  330. expect(result.output).toContain("test content")
  331. expect(result.output).toContain("system-reminder")
  332. expect(result.output).toContain("Test Instructions")
  333. expect(result.metadata.loaded).toBeDefined()
  334. expect(result.metadata.loaded).toContain(path.join(tmp.path, "subdir", "AGENTS.md"))
  335. },
  336. })
  337. })
  338. })