read.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 directory-scoped external_directory permission when reading external directory", async () => {
  76. await using outerTmp = await tmpdir({
  77. init: async (dir) => {
  78. await Bun.write(path.join(dir, "external", "a.txt"), "a")
  79. },
  80. })
  81. await using tmp = await tmpdir({ git: true })
  82. await Instance.provide({
  83. directory: tmp.path,
  84. fn: async () => {
  85. const read = await ReadTool.init()
  86. const requests: Array<Omit<PermissionNext.Request, "id" | "sessionID" | "tool">> = []
  87. const testCtx = {
  88. ...ctx,
  89. ask: async (req: Omit<PermissionNext.Request, "id" | "sessionID" | "tool">) => {
  90. requests.push(req)
  91. },
  92. }
  93. await read.execute({ filePath: path.join(outerTmp.path, "external") }, testCtx)
  94. const extDirReq = requests.find((r) => r.permission === "external_directory")
  95. expect(extDirReq).toBeDefined()
  96. expect(extDirReq!.patterns).toContain(path.join(outerTmp.path, "external", "*"))
  97. },
  98. })
  99. })
  100. test("asks for external_directory permission when reading relative path outside project", async () => {
  101. await using tmp = await tmpdir({ git: true })
  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. // This will fail because file doesn't exist, but we can check if permission was asked
  114. await read.execute({ filePath: "../outside.txt" }, testCtx).catch(() => {})
  115. const extDirReq = requests.find((r) => r.permission === "external_directory")
  116. expect(extDirReq).toBeDefined()
  117. },
  118. })
  119. })
  120. test("does not ask for external_directory permission when reading inside project", async () => {
  121. await using tmp = await tmpdir({
  122. git: true,
  123. init: async (dir) => {
  124. await Bun.write(path.join(dir, "internal.txt"), "internal content")
  125. },
  126. })
  127. await Instance.provide({
  128. directory: tmp.path,
  129. fn: async () => {
  130. const read = await ReadTool.init()
  131. const requests: Array<Omit<PermissionNext.Request, "id" | "sessionID" | "tool">> = []
  132. const testCtx = {
  133. ...ctx,
  134. ask: async (req: Omit<PermissionNext.Request, "id" | "sessionID" | "tool">) => {
  135. requests.push(req)
  136. },
  137. }
  138. await read.execute({ filePath: path.join(tmp.path, "internal.txt") }, testCtx)
  139. const extDirReq = requests.find((r) => r.permission === "external_directory")
  140. expect(extDirReq).toBeUndefined()
  141. },
  142. })
  143. })
  144. })
  145. describe("tool.read env file permissions", () => {
  146. const cases: [string, boolean][] = [
  147. [".env", true],
  148. [".env.local", true],
  149. [".env.production", true],
  150. [".env.development.local", true],
  151. [".env.example", false],
  152. [".envrc", false],
  153. ["environment.ts", false],
  154. ]
  155. describe.each(["build", "plan"])("agent=%s", (agentName) => {
  156. test.each(cases)("%s asks=%s", async (filename, shouldAsk) => {
  157. await using tmp = await tmpdir({
  158. init: (dir) => Bun.write(path.join(dir, filename), "content"),
  159. })
  160. await Instance.provide({
  161. directory: tmp.path,
  162. fn: async () => {
  163. const agent = await Agent.get(agentName)
  164. let askedForEnv = false
  165. const ctxWithPermissions = {
  166. ...ctx,
  167. ask: async (req: Omit<PermissionNext.Request, "id" | "sessionID" | "tool">) => {
  168. for (const pattern of req.patterns) {
  169. const rule = PermissionNext.evaluate(req.permission, pattern, agent.permission)
  170. if (rule.action === "ask" && req.permission === "read") {
  171. askedForEnv = true
  172. }
  173. if (rule.action === "deny") {
  174. throw new PermissionNext.DeniedError(agent.permission)
  175. }
  176. }
  177. },
  178. }
  179. const read = await ReadTool.init()
  180. await read.execute({ filePath: path.join(tmp.path, filename) }, ctxWithPermissions)
  181. expect(askedForEnv).toBe(shouldAsk)
  182. },
  183. })
  184. })
  185. })
  186. })
  187. describe("tool.read truncation", () => {
  188. test("truncates large file by bytes and sets truncated metadata", async () => {
  189. await using tmp = await tmpdir({
  190. init: async (dir) => {
  191. const base = await Bun.file(path.join(FIXTURES_DIR, "models-api.json")).text()
  192. const target = 60 * 1024
  193. const content = base.length >= target ? base : base.repeat(Math.ceil(target / base.length))
  194. await Bun.write(path.join(dir, "large.json"), content)
  195. },
  196. })
  197. await Instance.provide({
  198. directory: tmp.path,
  199. fn: async () => {
  200. const read = await ReadTool.init()
  201. const result = await read.execute({ filePath: path.join(tmp.path, "large.json") }, ctx)
  202. expect(result.metadata.truncated).toBe(true)
  203. expect(result.output).toContain("Output capped at")
  204. expect(result.output).toContain("Use offset=")
  205. },
  206. })
  207. })
  208. test("truncates by line count when limit is specified", async () => {
  209. await using tmp = await tmpdir({
  210. init: async (dir) => {
  211. const lines = Array.from({ length: 100 }, (_, i) => `line${i}`).join("\n")
  212. await Bun.write(path.join(dir, "many-lines.txt"), lines)
  213. },
  214. })
  215. await Instance.provide({
  216. directory: tmp.path,
  217. fn: async () => {
  218. const read = await ReadTool.init()
  219. const result = await read.execute({ filePath: path.join(tmp.path, "many-lines.txt"), limit: 10 }, ctx)
  220. expect(result.metadata.truncated).toBe(true)
  221. expect(result.output).toContain("Showing lines 1-10 of 100")
  222. expect(result.output).toContain("Use offset=11")
  223. expect(result.output).toContain("line0")
  224. expect(result.output).toContain("line9")
  225. expect(result.output).not.toContain("line10")
  226. },
  227. })
  228. })
  229. test("does not truncate small file", async () => {
  230. await using tmp = await tmpdir({
  231. init: async (dir) => {
  232. await Bun.write(path.join(dir, "small.txt"), "hello world")
  233. },
  234. })
  235. await Instance.provide({
  236. directory: tmp.path,
  237. fn: async () => {
  238. const read = await ReadTool.init()
  239. const result = await read.execute({ filePath: path.join(tmp.path, "small.txt") }, ctx)
  240. expect(result.metadata.truncated).toBe(false)
  241. expect(result.output).toContain("End of file")
  242. },
  243. })
  244. })
  245. test("respects offset parameter", async () => {
  246. await using tmp = await tmpdir({
  247. init: async (dir) => {
  248. const lines = Array.from({ length: 20 }, (_, i) => `line${i + 1}`).join("\n")
  249. await Bun.write(path.join(dir, "offset.txt"), lines)
  250. },
  251. })
  252. await Instance.provide({
  253. directory: tmp.path,
  254. fn: async () => {
  255. const read = await ReadTool.init()
  256. const result = await read.execute({ filePath: path.join(tmp.path, "offset.txt"), offset: 10, limit: 5 }, ctx)
  257. expect(result.output).toContain("10: line10")
  258. expect(result.output).toContain("14: line14")
  259. expect(result.output).not.toContain("9: line10")
  260. expect(result.output).not.toContain("15: line15")
  261. expect(result.output).toContain("line10")
  262. expect(result.output).toContain("line14")
  263. expect(result.output).not.toContain("line0")
  264. expect(result.output).not.toContain("line15")
  265. },
  266. })
  267. })
  268. test("throws when offset is beyond end of file", async () => {
  269. await using tmp = await tmpdir({
  270. init: async (dir) => {
  271. const lines = Array.from({ length: 3 }, (_, i) => `line${i + 1}`).join("\n")
  272. await Bun.write(path.join(dir, "short.txt"), lines)
  273. },
  274. })
  275. await Instance.provide({
  276. directory: tmp.path,
  277. fn: async () => {
  278. const read = await ReadTool.init()
  279. await expect(
  280. read.execute({ filePath: path.join(tmp.path, "short.txt"), offset: 4, limit: 5 }, ctx),
  281. ).rejects.toThrow("Offset 4 is out of range for this file (3 lines)")
  282. },
  283. })
  284. })
  285. test("allows reading empty file at default offset", async () => {
  286. await using tmp = await tmpdir({
  287. init: async (dir) => {
  288. await Bun.write(path.join(dir, "empty.txt"), "")
  289. },
  290. })
  291. await Instance.provide({
  292. directory: tmp.path,
  293. fn: async () => {
  294. const read = await ReadTool.init()
  295. const result = await read.execute({ filePath: path.join(tmp.path, "empty.txt") }, ctx)
  296. expect(result.metadata.truncated).toBe(false)
  297. expect(result.output).toContain("End of file - total 0 lines")
  298. },
  299. })
  300. })
  301. test("throws when offset > 1 for empty file", async () => {
  302. await using tmp = await tmpdir({
  303. init: async (dir) => {
  304. await Bun.write(path.join(dir, "empty.txt"), "")
  305. },
  306. })
  307. await Instance.provide({
  308. directory: tmp.path,
  309. fn: async () => {
  310. const read = await ReadTool.init()
  311. await expect(read.execute({ filePath: path.join(tmp.path, "empty.txt"), offset: 2 }, ctx)).rejects.toThrow(
  312. "Offset 2 is out of range for this file (0 lines)",
  313. )
  314. },
  315. })
  316. })
  317. test("does not mark final directory page as truncated", async () => {
  318. await using tmp = await tmpdir({
  319. init: async (dir) => {
  320. await Promise.all(
  321. Array.from({ length: 10 }, (_, i) => Bun.write(path.join(dir, "dir", `file-${i + 1}.txt`), `line${i}`)),
  322. )
  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, "dir"), offset: 6, limit: 5 }, ctx)
  330. expect(result.metadata.truncated).toBe(false)
  331. expect(result.output).not.toContain("Showing 5 of 10 entries")
  332. },
  333. })
  334. })
  335. test("truncates long lines", async () => {
  336. await using tmp = await tmpdir({
  337. init: async (dir) => {
  338. const longLine = "x".repeat(3000)
  339. await Bun.write(path.join(dir, "long-line.txt"), longLine)
  340. },
  341. })
  342. await Instance.provide({
  343. directory: tmp.path,
  344. fn: async () => {
  345. const read = await ReadTool.init()
  346. const result = await read.execute({ filePath: path.join(tmp.path, "long-line.txt") }, ctx)
  347. expect(result.output).toContain("(line truncated to 2000 chars)")
  348. expect(result.output.length).toBeLessThan(3000)
  349. },
  350. })
  351. })
  352. test("image files set truncated to false", async () => {
  353. await using tmp = await tmpdir({
  354. init: async (dir) => {
  355. // 1x1 red PNG
  356. const png = Buffer.from(
  357. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==",
  358. "base64",
  359. )
  360. await Bun.write(path.join(dir, "image.png"), png)
  361. },
  362. })
  363. await Instance.provide({
  364. directory: tmp.path,
  365. fn: async () => {
  366. const read = await ReadTool.init()
  367. const result = await read.execute({ filePath: path.join(tmp.path, "image.png") }, ctx)
  368. expect(result.metadata.truncated).toBe(false)
  369. expect(result.attachments).toBeDefined()
  370. expect(result.attachments?.length).toBe(1)
  371. expect(result.attachments?.[0]).not.toHaveProperty("id")
  372. expect(result.attachments?.[0]).not.toHaveProperty("sessionID")
  373. expect(result.attachments?.[0]).not.toHaveProperty("messageID")
  374. },
  375. })
  376. })
  377. test("large image files are properly attached without error", async () => {
  378. await Instance.provide({
  379. directory: FIXTURES_DIR,
  380. fn: async () => {
  381. const read = await ReadTool.init()
  382. const result = await read.execute({ filePath: path.join(FIXTURES_DIR, "large-image.png") }, ctx)
  383. expect(result.metadata.truncated).toBe(false)
  384. expect(result.attachments).toBeDefined()
  385. expect(result.attachments?.length).toBe(1)
  386. expect(result.attachments?.[0].type).toBe("file")
  387. expect(result.attachments?.[0]).not.toHaveProperty("id")
  388. expect(result.attachments?.[0]).not.toHaveProperty("sessionID")
  389. expect(result.attachments?.[0]).not.toHaveProperty("messageID")
  390. },
  391. })
  392. })
  393. test(".fbs files (FlatBuffers schema) are read as text, not images", async () => {
  394. await using tmp = await tmpdir({
  395. init: async (dir) => {
  396. // FlatBuffers schema content
  397. const fbsContent = `namespace MyGame;
  398. table Monster {
  399. pos:Vec3;
  400. name:string;
  401. inventory:[ubyte];
  402. }
  403. root_type Monster;`
  404. await Bun.write(path.join(dir, "schema.fbs"), fbsContent)
  405. },
  406. })
  407. await Instance.provide({
  408. directory: tmp.path,
  409. fn: async () => {
  410. const read = await ReadTool.init()
  411. const result = await read.execute({ filePath: path.join(tmp.path, "schema.fbs") }, ctx)
  412. // Should be read as text, not as image
  413. expect(result.attachments).toBeUndefined()
  414. expect(result.output).toContain("namespace MyGame")
  415. expect(result.output).toContain("table Monster")
  416. },
  417. })
  418. })
  419. })
  420. describe("tool.read loaded instructions", () => {
  421. test("loads AGENTS.md from parent directory and includes in metadata", async () => {
  422. await using tmp = await tmpdir({
  423. init: async (dir) => {
  424. await Bun.write(path.join(dir, "subdir", "AGENTS.md"), "# Test Instructions\nDo something special.")
  425. await Bun.write(path.join(dir, "subdir", "nested", "test.txt"), "test content")
  426. },
  427. })
  428. await Instance.provide({
  429. directory: tmp.path,
  430. fn: async () => {
  431. const read = await ReadTool.init()
  432. const result = await read.execute({ filePath: path.join(tmp.path, "subdir", "nested", "test.txt") }, ctx)
  433. expect(result.output).toContain("test content")
  434. expect(result.output).toContain("system-reminder")
  435. expect(result.output).toContain("Test Instructions")
  436. expect(result.metadata.loaded).toBeDefined()
  437. expect(result.metadata.loaded).toContain(path.join(tmp.path, "subdir", "AGENTS.md"))
  438. },
  439. })
  440. })
  441. })
  442. describe("tool.read binary detection", () => {
  443. test("rejects text extension files with null bytes", async () => {
  444. await using tmp = await tmpdir({
  445. init: async (dir) => {
  446. const bytes = Buffer.from([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x77, 0x6f, 0x72, 0x6c, 0x64])
  447. await Bun.write(path.join(dir, "null-byte.txt"), bytes)
  448. },
  449. })
  450. await Instance.provide({
  451. directory: tmp.path,
  452. fn: async () => {
  453. const read = await ReadTool.init()
  454. await expect(read.execute({ filePath: path.join(tmp.path, "null-byte.txt") }, ctx)).rejects.toThrow(
  455. "Cannot read binary file",
  456. )
  457. },
  458. })
  459. })
  460. test("rejects known binary extensions", async () => {
  461. await using tmp = await tmpdir({
  462. init: async (dir) => {
  463. await Bun.write(path.join(dir, "module.wasm"), "not really wasm")
  464. },
  465. })
  466. await Instance.provide({
  467. directory: tmp.path,
  468. fn: async () => {
  469. const read = await ReadTool.init()
  470. await expect(read.execute({ filePath: path.join(tmp.path, "module.wasm") }, ctx)).rejects.toThrow(
  471. "Cannot read binary file",
  472. )
  473. },
  474. })
  475. })
  476. })