read.test.ts 18 KB

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