read.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 content = await Bun.file(path.join(FIXTURES_DIR, "models-api.json")).text()
  167. await Bun.write(path.join(dir, "large.json"), content)
  168. },
  169. })
  170. await Instance.provide({
  171. directory: tmp.path,
  172. fn: async () => {
  173. const read = await ReadTool.init()
  174. const result = await read.execute({ filePath: path.join(tmp.path, "large.json") }, ctx)
  175. expect(result.metadata.truncated).toBe(true)
  176. expect(result.output).toContain("Output truncated at")
  177. expect(result.output).toContain("bytes")
  178. },
  179. })
  180. })
  181. test("truncates by line count when limit is specified", async () => {
  182. await using tmp = await tmpdir({
  183. init: async (dir) => {
  184. const lines = Array.from({ length: 100 }, (_, i) => `line${i}`).join("\n")
  185. await Bun.write(path.join(dir, "many-lines.txt"), lines)
  186. },
  187. })
  188. await Instance.provide({
  189. directory: tmp.path,
  190. fn: async () => {
  191. const read = await ReadTool.init()
  192. const result = await read.execute({ filePath: path.join(tmp.path, "many-lines.txt"), limit: 10 }, ctx)
  193. expect(result.metadata.truncated).toBe(true)
  194. expect(result.output).toContain("File has more lines")
  195. expect(result.output).toContain("line0")
  196. expect(result.output).toContain("line9")
  197. expect(result.output).not.toContain("line10")
  198. },
  199. })
  200. })
  201. test("does not truncate small file", async () => {
  202. await using tmp = await tmpdir({
  203. init: async (dir) => {
  204. await Bun.write(path.join(dir, "small.txt"), "hello world")
  205. },
  206. })
  207. await Instance.provide({
  208. directory: tmp.path,
  209. fn: async () => {
  210. const read = await ReadTool.init()
  211. const result = await read.execute({ filePath: path.join(tmp.path, "small.txt") }, ctx)
  212. expect(result.metadata.truncated).toBe(false)
  213. expect(result.output).toContain("End of file")
  214. },
  215. })
  216. })
  217. test("respects offset parameter", async () => {
  218. await using tmp = await tmpdir({
  219. init: async (dir) => {
  220. const lines = Array.from({ length: 20 }, (_, i) => `line${i}`).join("\n")
  221. await Bun.write(path.join(dir, "offset.txt"), lines)
  222. },
  223. })
  224. await Instance.provide({
  225. directory: tmp.path,
  226. fn: async () => {
  227. const read = await ReadTool.init()
  228. const result = await read.execute({ filePath: path.join(tmp.path, "offset.txt"), offset: 10, limit: 5 }, ctx)
  229. expect(result.output).toContain("line10")
  230. expect(result.output).toContain("line14")
  231. expect(result.output).not.toContain("line0")
  232. expect(result.output).not.toContain("line15")
  233. },
  234. })
  235. })
  236. test("truncates long lines", async () => {
  237. await using tmp = await tmpdir({
  238. init: async (dir) => {
  239. const longLine = "x".repeat(3000)
  240. await Bun.write(path.join(dir, "long-line.txt"), longLine)
  241. },
  242. })
  243. await Instance.provide({
  244. directory: tmp.path,
  245. fn: async () => {
  246. const read = await ReadTool.init()
  247. const result = await read.execute({ filePath: path.join(tmp.path, "long-line.txt") }, ctx)
  248. expect(result.output).toContain("...")
  249. expect(result.output.length).toBeLessThan(3000)
  250. },
  251. })
  252. })
  253. test("image files set truncated to false", async () => {
  254. await using tmp = await tmpdir({
  255. init: async (dir) => {
  256. // 1x1 red PNG
  257. const png = Buffer.from(
  258. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==",
  259. "base64",
  260. )
  261. await Bun.write(path.join(dir, "image.png"), png)
  262. },
  263. })
  264. await Instance.provide({
  265. directory: tmp.path,
  266. fn: async () => {
  267. const read = await ReadTool.init()
  268. const result = await read.execute({ filePath: path.join(tmp.path, "image.png") }, ctx)
  269. expect(result.metadata.truncated).toBe(false)
  270. expect(result.attachments).toBeDefined()
  271. expect(result.attachments?.length).toBe(1)
  272. },
  273. })
  274. })
  275. test("large image files are properly attached without error", async () => {
  276. await Instance.provide({
  277. directory: FIXTURES_DIR,
  278. fn: async () => {
  279. const read = await ReadTool.init()
  280. const result = await read.execute({ filePath: path.join(FIXTURES_DIR, "large-image.png") }, ctx)
  281. expect(result.metadata.truncated).toBe(false)
  282. expect(result.attachments).toBeDefined()
  283. expect(result.attachments?.length).toBe(1)
  284. expect(result.attachments?.[0].type).toBe("file")
  285. },
  286. })
  287. })
  288. test(".fbs files (FlatBuffers schema) are read as text, not images", async () => {
  289. await using tmp = await tmpdir({
  290. init: async (dir) => {
  291. // FlatBuffers schema content
  292. const fbsContent = `namespace MyGame;
  293. table Monster {
  294. pos:Vec3;
  295. name:string;
  296. inventory:[ubyte];
  297. }
  298. root_type Monster;`
  299. await Bun.write(path.join(dir, "schema.fbs"), fbsContent)
  300. },
  301. })
  302. await Instance.provide({
  303. directory: tmp.path,
  304. fn: async () => {
  305. const read = await ReadTool.init()
  306. const result = await read.execute({ filePath: path.join(tmp.path, "schema.fbs") }, ctx)
  307. // Should be read as text, not as image
  308. expect(result.attachments).toBeUndefined()
  309. expect(result.output).toContain("namespace MyGame")
  310. expect(result.output).toContain("table Monster")
  311. },
  312. })
  313. })
  314. })
  315. describe("tool.read loaded instructions", () => {
  316. test("loads AGENTS.md from parent directory and includes in metadata", async () => {
  317. await using tmp = await tmpdir({
  318. init: async (dir) => {
  319. await Bun.write(path.join(dir, "subdir", "AGENTS.md"), "# Test Instructions\nDo something special.")
  320. await Bun.write(path.join(dir, "subdir", "nested", "test.txt"), "test content")
  321. },
  322. })
  323. await Instance.provide({
  324. directory: tmp.path,
  325. fn: async () => {
  326. const read = await ReadTool.init()
  327. const result = await read.execute({ filePath: path.join(tmp.path, "subdir", "nested", "test.txt") }, ctx)
  328. expect(result.output).toContain("test content")
  329. expect(result.output).toContain("system-reminder")
  330. expect(result.output).toContain("Test Instructions")
  331. expect(result.metadata.loaded).toBeDefined()
  332. expect(result.metadata.loaded).toContain(path.join(tmp.path, "subdir", "AGENTS.md"))
  333. },
  334. })
  335. })
  336. })