apply_patch.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. import { describe, expect, test } from "bun:test"
  2. import path from "path"
  3. import * as fs from "fs/promises"
  4. import { ApplyPatchTool } from "../../src/tool/apply_patch"
  5. import { Instance } from "../../src/project/instance"
  6. import { tmpdir } from "../fixture/fixture"
  7. const baseCtx = {
  8. sessionID: "test",
  9. messageID: "",
  10. callID: "",
  11. agent: "build",
  12. abort: AbortSignal.any([]),
  13. messages: [],
  14. metadata: () => {},
  15. }
  16. type AskInput = {
  17. permission: string
  18. patterns: string[]
  19. always: string[]
  20. metadata: {
  21. diff: string
  22. filepath: string
  23. files: Array<{
  24. filePath: string
  25. relativePath: string
  26. type: "add" | "update" | "delete" | "move"
  27. diff: string
  28. before: string
  29. after: string
  30. additions: number
  31. deletions: number
  32. movePath?: string
  33. }>
  34. }
  35. }
  36. type ToolCtx = typeof baseCtx & {
  37. ask: (input: AskInput) => Promise<void>
  38. }
  39. const execute = async (params: { patchText: string }, ctx: ToolCtx) => {
  40. const tool = await ApplyPatchTool.init()
  41. return tool.execute(params, ctx)
  42. }
  43. const makeCtx = () => {
  44. const calls: AskInput[] = []
  45. const ctx: ToolCtx = {
  46. ...baseCtx,
  47. ask: async (input) => {
  48. calls.push(input)
  49. },
  50. }
  51. return { ctx, calls }
  52. }
  53. describe("tool.apply_patch freeform", () => {
  54. test("requires patchText", async () => {
  55. const { ctx } = makeCtx()
  56. await expect(execute({ patchText: "" }, ctx)).rejects.toThrow("patchText is required")
  57. })
  58. test("rejects invalid patch format", async () => {
  59. const { ctx } = makeCtx()
  60. await expect(execute({ patchText: "invalid patch" }, ctx)).rejects.toThrow("apply_patch verification failed")
  61. })
  62. test("rejects empty patch", async () => {
  63. const { ctx } = makeCtx()
  64. const emptyPatch = "*** Begin Patch\n*** End Patch"
  65. await expect(execute({ patchText: emptyPatch }, ctx)).rejects.toThrow("patch rejected: empty patch")
  66. })
  67. test("applies add/update/delete in one patch", async () => {
  68. await using fixture = await tmpdir({ git: true })
  69. const { ctx, calls } = makeCtx()
  70. await Instance.provide({
  71. directory: fixture.path,
  72. fn: async () => {
  73. const modifyPath = path.join(fixture.path, "modify.txt")
  74. const deletePath = path.join(fixture.path, "delete.txt")
  75. await fs.writeFile(modifyPath, "line1\nline2\n", "utf-8")
  76. await fs.writeFile(deletePath, "obsolete\n", "utf-8")
  77. const patchText =
  78. "*** Begin Patch\n*** Add File: nested/new.txt\n+created\n*** Delete File: delete.txt\n*** Update File: modify.txt\n@@\n-line2\n+changed\n*** End Patch"
  79. const result = await execute({ patchText }, ctx)
  80. expect(result.title).toContain("Success. Updated the following files")
  81. expect(result.output).toContain("Success. Updated the following files")
  82. // Strict formatting assertions for slashes
  83. expect(result.output).toMatch(/A nested\/new\.txt/)
  84. expect(result.output).toMatch(/D delete\.txt/)
  85. expect(result.output).toMatch(/M modify\.txt/)
  86. if (process.platform === "win32") {
  87. expect(result.output).not.toContain("\\")
  88. }
  89. expect(result.metadata.diff).toContain("Index:")
  90. expect(calls.length).toBe(1)
  91. // Verify permission metadata includes files array for UI rendering
  92. const permissionCall = calls[0]
  93. expect(permissionCall.metadata.files).toHaveLength(3)
  94. expect(permissionCall.metadata.files.map((f) => f.type).sort()).toEqual(["add", "delete", "update"])
  95. const addFile = permissionCall.metadata.files.find((f) => f.type === "add")
  96. expect(addFile).toBeDefined()
  97. expect(addFile!.relativePath).toBe("nested/new.txt")
  98. expect(addFile!.after).toBe("created\n")
  99. const updateFile = permissionCall.metadata.files.find((f) => f.type === "update")
  100. expect(updateFile).toBeDefined()
  101. expect(updateFile!.before).toContain("line2")
  102. expect(updateFile!.after).toContain("changed")
  103. const added = await fs.readFile(path.join(fixture.path, "nested", "new.txt"), "utf-8")
  104. expect(added).toBe("created\n")
  105. expect(await fs.readFile(modifyPath, "utf-8")).toBe("line1\nchanged\n")
  106. await expect(fs.readFile(deletePath, "utf-8")).rejects.toThrow()
  107. },
  108. })
  109. })
  110. test("permission metadata includes move file info", async () => {
  111. await using fixture = await tmpdir({ git: true })
  112. const { ctx, calls } = makeCtx()
  113. await Instance.provide({
  114. directory: fixture.path,
  115. fn: async () => {
  116. const original = path.join(fixture.path, "old", "name.txt")
  117. await fs.mkdir(path.dirname(original), { recursive: true })
  118. await fs.writeFile(original, "old content\n", "utf-8")
  119. const patchText =
  120. "*** Begin Patch\n*** Update File: old/name.txt\n*** Move to: renamed/dir/name.txt\n@@\n-old content\n+new content\n*** End Patch"
  121. await execute({ patchText }, ctx)
  122. expect(calls.length).toBe(1)
  123. const permissionCall = calls[0]
  124. expect(permissionCall.metadata.files).toHaveLength(1)
  125. const moveFile = permissionCall.metadata.files[0]
  126. expect(moveFile.type).toBe("move")
  127. expect(moveFile.relativePath).toBe("renamed/dir/name.txt")
  128. expect(moveFile.movePath).toBe(path.join(fixture.path, "renamed/dir/name.txt"))
  129. expect(moveFile.before).toBe("old content\n")
  130. expect(moveFile.after).toBe("new content\n")
  131. },
  132. })
  133. })
  134. test("applies multiple hunks to one file", async () => {
  135. await using fixture = await tmpdir()
  136. const { ctx } = makeCtx()
  137. await Instance.provide({
  138. directory: fixture.path,
  139. fn: async () => {
  140. const target = path.join(fixture.path, "multi.txt")
  141. await fs.writeFile(target, "line1\nline2\nline3\nline4\n", "utf-8")
  142. const patchText =
  143. "*** Begin Patch\n*** Update File: multi.txt\n@@\n-line2\n+changed2\n@@\n-line4\n+changed4\n*** End Patch"
  144. await execute({ patchText }, ctx)
  145. expect(await fs.readFile(target, "utf-8")).toBe("line1\nchanged2\nline3\nchanged4\n")
  146. },
  147. })
  148. })
  149. test("inserts lines with insert-only hunk", async () => {
  150. await using fixture = await tmpdir()
  151. const { ctx } = makeCtx()
  152. await Instance.provide({
  153. directory: fixture.path,
  154. fn: async () => {
  155. const target = path.join(fixture.path, "insert_only.txt")
  156. await fs.writeFile(target, "alpha\nomega\n", "utf-8")
  157. const patchText = "*** Begin Patch\n*** Update File: insert_only.txt\n@@\n alpha\n+beta\n omega\n*** End Patch"
  158. await execute({ patchText }, ctx)
  159. expect(await fs.readFile(target, "utf-8")).toBe("alpha\nbeta\nomega\n")
  160. },
  161. })
  162. })
  163. test("appends trailing newline on update", async () => {
  164. await using fixture = await tmpdir()
  165. const { ctx } = makeCtx()
  166. await Instance.provide({
  167. directory: fixture.path,
  168. fn: async () => {
  169. const target = path.join(fixture.path, "no_newline.txt")
  170. await fs.writeFile(target, "no newline at end", "utf-8")
  171. const patchText =
  172. "*** Begin Patch\n*** Update File: no_newline.txt\n@@\n-no newline at end\n+first line\n+second line\n*** End Patch"
  173. await execute({ patchText }, ctx)
  174. const contents = await fs.readFile(target, "utf-8")
  175. expect(contents.endsWith("\n")).toBe(true)
  176. expect(contents).toBe("first line\nsecond line\n")
  177. },
  178. })
  179. })
  180. test("moves file to a new directory", async () => {
  181. await using fixture = await tmpdir()
  182. const { ctx } = makeCtx()
  183. await Instance.provide({
  184. directory: fixture.path,
  185. fn: async () => {
  186. const original = path.join(fixture.path, "old", "name.txt")
  187. await fs.mkdir(path.dirname(original), { recursive: true })
  188. await fs.writeFile(original, "old content\n", "utf-8")
  189. const patchText =
  190. "*** Begin Patch\n*** Update File: old/name.txt\n*** Move to: renamed/dir/name.txt\n@@\n-old content\n+new content\n*** End Patch"
  191. await execute({ patchText }, ctx)
  192. const moved = path.join(fixture.path, "renamed", "dir", "name.txt")
  193. await expect(fs.readFile(original, "utf-8")).rejects.toThrow()
  194. expect(await fs.readFile(moved, "utf-8")).toBe("new content\n")
  195. },
  196. })
  197. })
  198. test("moves file overwriting existing destination", async () => {
  199. await using fixture = await tmpdir()
  200. const { ctx } = makeCtx()
  201. await Instance.provide({
  202. directory: fixture.path,
  203. fn: async () => {
  204. const original = path.join(fixture.path, "old", "name.txt")
  205. const destination = path.join(fixture.path, "renamed", "dir", "name.txt")
  206. await fs.mkdir(path.dirname(original), { recursive: true })
  207. await fs.mkdir(path.dirname(destination), { recursive: true })
  208. await fs.writeFile(original, "from\n", "utf-8")
  209. await fs.writeFile(destination, "existing\n", "utf-8")
  210. const patchText =
  211. "*** Begin Patch\n*** Update File: old/name.txt\n*** Move to: renamed/dir/name.txt\n@@\n-from\n+new\n*** End Patch"
  212. await execute({ patchText }, ctx)
  213. await expect(fs.readFile(original, "utf-8")).rejects.toThrow()
  214. expect(await fs.readFile(destination, "utf-8")).toBe("new\n")
  215. },
  216. })
  217. })
  218. test("adds file overwriting existing file", async () => {
  219. await using fixture = await tmpdir()
  220. const { ctx } = makeCtx()
  221. await Instance.provide({
  222. directory: fixture.path,
  223. fn: async () => {
  224. const target = path.join(fixture.path, "duplicate.txt")
  225. await fs.writeFile(target, "old content\n", "utf-8")
  226. const patchText = "*** Begin Patch\n*** Add File: duplicate.txt\n+new content\n*** End Patch"
  227. await execute({ patchText }, ctx)
  228. expect(await fs.readFile(target, "utf-8")).toBe("new content\n")
  229. },
  230. })
  231. })
  232. test("rejects update when target file is missing", async () => {
  233. await using fixture = await tmpdir()
  234. const { ctx } = makeCtx()
  235. await Instance.provide({
  236. directory: fixture.path,
  237. fn: async () => {
  238. const patchText = "*** Begin Patch\n*** Update File: missing.txt\n@@\n-nope\n+better\n*** End Patch"
  239. await expect(execute({ patchText }, ctx)).rejects.toThrow(
  240. "apply_patch verification failed: Failed to read file to update",
  241. )
  242. },
  243. })
  244. })
  245. test("rejects delete when file is missing", async () => {
  246. await using fixture = await tmpdir()
  247. const { ctx } = makeCtx()
  248. await Instance.provide({
  249. directory: fixture.path,
  250. fn: async () => {
  251. const patchText = "*** Begin Patch\n*** Delete File: missing.txt\n*** End Patch"
  252. await expect(execute({ patchText }, ctx)).rejects.toThrow()
  253. },
  254. })
  255. })
  256. test("rejects delete when target is a directory", async () => {
  257. await using fixture = await tmpdir()
  258. const { ctx } = makeCtx()
  259. await Instance.provide({
  260. directory: fixture.path,
  261. fn: async () => {
  262. const dirPath = path.join(fixture.path, "dir")
  263. await fs.mkdir(dirPath)
  264. const patchText = "*** Begin Patch\n*** Delete File: dir\n*** End Patch"
  265. await expect(execute({ patchText }, ctx)).rejects.toThrow()
  266. },
  267. })
  268. })
  269. test("rejects invalid hunk header", async () => {
  270. await using fixture = await tmpdir()
  271. const { ctx } = makeCtx()
  272. await Instance.provide({
  273. directory: fixture.path,
  274. fn: async () => {
  275. const patchText = "*** Begin Patch\n*** Frobnicate File: foo\n*** End Patch"
  276. await expect(execute({ patchText }, ctx)).rejects.toThrow("apply_patch verification failed")
  277. },
  278. })
  279. })
  280. test("rejects update with missing context", async () => {
  281. await using fixture = await tmpdir()
  282. const { ctx } = makeCtx()
  283. await Instance.provide({
  284. directory: fixture.path,
  285. fn: async () => {
  286. const target = path.join(fixture.path, "modify.txt")
  287. await fs.writeFile(target, "line1\nline2\n", "utf-8")
  288. const patchText = "*** Begin Patch\n*** Update File: modify.txt\n@@\n-missing\n+changed\n*** End Patch"
  289. await expect(execute({ patchText }, ctx)).rejects.toThrow("apply_patch verification failed")
  290. expect(await fs.readFile(target, "utf-8")).toBe("line1\nline2\n")
  291. },
  292. })
  293. })
  294. test("verification failure leaves no side effects", async () => {
  295. await using fixture = await tmpdir()
  296. const { ctx } = makeCtx()
  297. await Instance.provide({
  298. directory: fixture.path,
  299. fn: async () => {
  300. const patchText =
  301. "*** Begin Patch\n*** Add File: created.txt\n+hello\n*** Update File: missing.txt\n@@\n-old\n+new\n*** End Patch"
  302. await expect(execute({ patchText }, ctx)).rejects.toThrow()
  303. const createdPath = path.join(fixture.path, "created.txt")
  304. await expect(fs.readFile(createdPath, "utf-8")).rejects.toThrow()
  305. },
  306. })
  307. })
  308. test("supports end of file anchor", async () => {
  309. await using fixture = await tmpdir()
  310. const { ctx } = makeCtx()
  311. await Instance.provide({
  312. directory: fixture.path,
  313. fn: async () => {
  314. const target = path.join(fixture.path, "tail.txt")
  315. await fs.writeFile(target, "alpha\nlast\n", "utf-8")
  316. const patchText = "*** Begin Patch\n*** Update File: tail.txt\n@@\n-last\n+end\n*** End of File\n*** End Patch"
  317. await execute({ patchText }, ctx)
  318. expect(await fs.readFile(target, "utf-8")).toBe("alpha\nend\n")
  319. },
  320. })
  321. })
  322. test("rejects missing second chunk context", async () => {
  323. await using fixture = await tmpdir()
  324. const { ctx } = makeCtx()
  325. await Instance.provide({
  326. directory: fixture.path,
  327. fn: async () => {
  328. const target = path.join(fixture.path, "two_chunks.txt")
  329. await fs.writeFile(target, "a\nb\nc\nd\n", "utf-8")
  330. const patchText = "*** Begin Patch\n*** Update File: two_chunks.txt\n@@\n-b\n+B\n\n-d\n+D\n*** End Patch"
  331. await expect(execute({ patchText }, ctx)).rejects.toThrow()
  332. expect(await fs.readFile(target, "utf-8")).toBe("a\nb\nc\nd\n")
  333. },
  334. })
  335. })
  336. test("disambiguates change context with @@ header", async () => {
  337. await using fixture = await tmpdir()
  338. const { ctx } = makeCtx()
  339. await Instance.provide({
  340. directory: fixture.path,
  341. fn: async () => {
  342. const target = path.join(fixture.path, "multi_ctx.txt")
  343. await fs.writeFile(target, "fn a\nx=10\ny=2\nfn b\nx=10\ny=20\n", "utf-8")
  344. const patchText = "*** Begin Patch\n*** Update File: multi_ctx.txt\n@@ fn b\n-x=10\n+x=11\n*** End Patch"
  345. await execute({ patchText }, ctx)
  346. expect(await fs.readFile(target, "utf-8")).toBe("fn a\nx=10\ny=2\nfn b\nx=11\ny=20\n")
  347. },
  348. })
  349. })
  350. test("EOF anchor matches from end of file first", async () => {
  351. await using fixture = await tmpdir()
  352. const { ctx } = makeCtx()
  353. await Instance.provide({
  354. directory: fixture.path,
  355. fn: async () => {
  356. const target = path.join(fixture.path, "eof_anchor.txt")
  357. // File has duplicate "marker" lines - one in middle, one at end
  358. await fs.writeFile(target, "start\nmarker\nmiddle\nmarker\nend\n", "utf-8")
  359. // With EOF anchor, should match the LAST "marker" line, not the first
  360. const patchText =
  361. "*** Begin Patch\n*** Update File: eof_anchor.txt\n@@\n-marker\n-end\n+marker-changed\n+end\n*** End of File\n*** End Patch"
  362. await execute({ patchText }, ctx)
  363. // First marker unchanged, second marker changed
  364. expect(await fs.readFile(target, "utf-8")).toBe("start\nmarker\nmiddle\nmarker-changed\nend\n")
  365. },
  366. })
  367. })
  368. test("parses heredoc-wrapped patch", async () => {
  369. await using fixture = await tmpdir()
  370. const { ctx } = makeCtx()
  371. await Instance.provide({
  372. directory: fixture.path,
  373. fn: async () => {
  374. const patchText = `cat <<'EOF'
  375. *** Begin Patch
  376. *** Add File: heredoc_test.txt
  377. +heredoc content
  378. *** End Patch
  379. EOF`
  380. await execute({ patchText }, ctx)
  381. const content = await fs.readFile(path.join(fixture.path, "heredoc_test.txt"), "utf-8")
  382. expect(content).toBe("heredoc content\n")
  383. },
  384. })
  385. })
  386. test("parses heredoc-wrapped patch without cat", async () => {
  387. await using fixture = await tmpdir()
  388. const { ctx } = makeCtx()
  389. await Instance.provide({
  390. directory: fixture.path,
  391. fn: async () => {
  392. const patchText = `<<EOF
  393. *** Begin Patch
  394. *** Add File: heredoc_no_cat.txt
  395. +no cat prefix
  396. *** End Patch
  397. EOF`
  398. await execute({ patchText }, ctx)
  399. const content = await fs.readFile(path.join(fixture.path, "heredoc_no_cat.txt"), "utf-8")
  400. expect(content).toBe("no cat prefix\n")
  401. },
  402. })
  403. })
  404. test("matches with trailing whitespace differences", async () => {
  405. await using fixture = await tmpdir()
  406. const { ctx } = makeCtx()
  407. await Instance.provide({
  408. directory: fixture.path,
  409. fn: async () => {
  410. const target = path.join(fixture.path, "trailing_ws.txt")
  411. // File has trailing spaces on some lines
  412. await fs.writeFile(target, "line1 \nline2\nline3 \n", "utf-8")
  413. // Patch doesn't have trailing spaces - should still match via rstrip pass
  414. const patchText = "*** Begin Patch\n*** Update File: trailing_ws.txt\n@@\n-line2\n+changed\n*** End Patch"
  415. await execute({ patchText }, ctx)
  416. expect(await fs.readFile(target, "utf-8")).toBe("line1 \nchanged\nline3 \n")
  417. },
  418. })
  419. })
  420. test("matches with leading whitespace differences", async () => {
  421. await using fixture = await tmpdir()
  422. const { ctx } = makeCtx()
  423. await Instance.provide({
  424. directory: fixture.path,
  425. fn: async () => {
  426. const target = path.join(fixture.path, "leading_ws.txt")
  427. // File has leading spaces
  428. await fs.writeFile(target, " line1\nline2\n line3\n", "utf-8")
  429. // Patch without leading spaces - should match via trim pass
  430. const patchText = "*** Begin Patch\n*** Update File: leading_ws.txt\n@@\n-line2\n+changed\n*** End Patch"
  431. await execute({ patchText }, ctx)
  432. expect(await fs.readFile(target, "utf-8")).toBe(" line1\nchanged\n line3\n")
  433. },
  434. })
  435. })
  436. test("matches with Unicode punctuation differences", async () => {
  437. await using fixture = await tmpdir()
  438. const { ctx } = makeCtx()
  439. await Instance.provide({
  440. directory: fixture.path,
  441. fn: async () => {
  442. const target = path.join(fixture.path, "unicode.txt")
  443. // File has fancy Unicode quotes (U+201C, U+201D) and em-dash (U+2014)
  444. const leftQuote = "\u201C"
  445. const rightQuote = "\u201D"
  446. const emDash = "\u2014"
  447. await fs.writeFile(target, `He said ${leftQuote}hello${rightQuote}\nsome${emDash}dash\nend\n`, "utf-8")
  448. // Patch uses ASCII equivalents - should match via normalized pass
  449. // The replacement uses ASCII quotes from the patch (not preserving Unicode)
  450. const patchText =
  451. '*** Begin Patch\n*** Update File: unicode.txt\n@@\n-He said "hello"\n+He said "hi"\n*** End Patch'
  452. await execute({ patchText }, ctx)
  453. // Result has ASCII quotes because that's what the patch specifies
  454. expect(await fs.readFile(target, "utf-8")).toBe(`He said "hi"\nsome${emDash}dash\nend\n`)
  455. },
  456. })
  457. })
  458. })