apply_patch.test.ts 20 KB

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