tool-apply-patch.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect } from "bun:test"
  4. import { Deferred, Effect, Exit, Fiber, Layer } from "effect"
  5. import { FileMutation } from "@opencode-ai/core/file-mutation"
  6. import { FSUtil } from "@opencode-ai/core/fs-util"
  7. import { Location } from "@opencode-ai/core/location"
  8. import { LocationMutation } from "@opencode-ai/core/location-mutation"
  9. import { PermissionV2 } from "@opencode-ai/core/permission"
  10. import { AbsolutePath } from "@opencode-ai/core/schema"
  11. import { SessionV2 } from "@opencode-ai/core/session"
  12. import { ToolRegistry } from "@opencode-ai/core/tool/registry"
  13. import { ApplyPatchTool } from "@opencode-ai/core/tool/apply-patch"
  14. import { location } from "./fixture/location"
  15. import { tmpdir } from "./fixture/tmpdir"
  16. import { testEffect } from "./lib/effect"
  17. import { toolIdentity, executeTool, settleTool, toolDefinitions } from "./lib/tool"
  18. const sessionID = SessionV2.ID.make("ses_apply_patch_tool_test")
  19. const assertions: PermissionV2.AssertInput[] = []
  20. let denyAction: string | undefined
  21. let failRemoveTarget: string | undefined
  22. let readsBeforeEditApproval = 0
  23. let editApproved = false
  24. let blockRemoveTarget: string | undefined
  25. let removeStarted: Deferred.Deferred<void> | undefined
  26. let releaseRemove: Deferred.Deferred<void> | undefined
  27. let afterEditApproval = (): Effect.Effect<void> => Effect.void
  28. const permission = Layer.succeed(
  29. PermissionV2.Service,
  30. PermissionV2.Service.of({
  31. assert: (input) =>
  32. Effect.sync(() => {
  33. assertions.push(input)
  34. if (input.action === "edit") editApproved = true
  35. }).pipe(
  36. Effect.andThen(input.action === "edit" ? Effect.suspend(afterEditApproval) : Effect.void),
  37. Effect.andThen(
  38. input.action === denyAction ? Effect.fail(new PermissionV2.DeniedError({ rules: [] })) : Effect.void,
  39. ),
  40. ),
  41. ask: () => Effect.die("unused"),
  42. reply: () => Effect.die("unused"),
  43. get: () => Effect.die("unused"),
  44. forSession: () => Effect.die("unused"),
  45. list: () => Effect.die("unused"),
  46. }),
  47. )
  48. const reset = () => {
  49. assertions.length = 0
  50. denyAction = undefined
  51. failRemoveTarget = undefined
  52. readsBeforeEditApproval = 0
  53. editApproved = false
  54. blockRemoveTarget = undefined
  55. removeStarted = undefined
  56. releaseRemove = undefined
  57. afterEditApproval = () => Effect.void
  58. }
  59. const filesystem = Layer.effect(
  60. FSUtil.Service,
  61. Effect.gen(function* () {
  62. const fs = yield* FSUtil.Service
  63. return FSUtil.Service.of({
  64. ...fs,
  65. readFile: (target) =>
  66. Effect.sync(() => {
  67. if (!editApproved) readsBeforeEditApproval++
  68. }).pipe(Effect.andThen(fs.readFile(target))),
  69. remove: (target, options) => {
  70. if (failRemoveTarget && path.basename(target) === failRemoveTarget) return Effect.die("forced remove failure")
  71. if (blockRemoveTarget && path.basename(target) === blockRemoveTarget && removeStarted && releaseRemove)
  72. return Deferred.succeed(removeStarted, undefined).pipe(
  73. Effect.andThen(Deferred.await(releaseRemove)),
  74. Effect.andThen(fs.remove(target, options)),
  75. )
  76. return fs.remove(target, options)
  77. },
  78. })
  79. }),
  80. ).pipe(Layer.provide(FSUtil.defaultLayer))
  81. const withTool = <A, E, R>(directory: string, body: (registry: ToolRegistry.Interface) => Effect.Effect<A, E, R>) => {
  82. const activeLocation = Layer.succeed(
  83. Location.Service,
  84. Location.Service.of(location({ directory: AbsolutePath.make(directory) })),
  85. )
  86. const resolution = LocationMutation.layer.pipe(Layer.provide(filesystem), Layer.provide(activeLocation))
  87. const mutation = FileMutation.layer.pipe(Layer.provide(filesystem))
  88. const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
  89. const patch = ApplyPatchTool.layer.pipe(
  90. Layer.provide(registry),
  91. Layer.provide(permission),
  92. Layer.provide(resolution),
  93. Layer.provide(mutation),
  94. Layer.provide(filesystem),
  95. )
  96. return Effect.gen(function* () {
  97. return yield* body(yield* ToolRegistry.Service)
  98. }).pipe(Effect.provide(Layer.mergeAll(registry, resolution, mutation, patch)))
  99. }
  100. const call = (patchText: string, id = "call-apply-patch") => ({
  101. sessionID,
  102. ...toolIdentity,
  103. call: { type: "tool-call" as const, id, name: "apply_patch", input: { patchText } },
  104. })
  105. const exists = (target: string) =>
  106. Effect.promise(() =>
  107. fs.stat(target).then(
  108. () => true,
  109. () => false,
  110. ),
  111. )
  112. const it = testEffect(Layer.empty)
  113. describe("ApplyPatchTool", () => {
  114. it.live("registers and sequentially applies add, update, and delete hunks", () =>
  115. Effect.acquireUseRelease(
  116. Effect.promise(() => tmpdir()),
  117. (tmp) => {
  118. reset()
  119. const update = path.join(tmp.path, "update.txt")
  120. const remove = path.join(tmp.path, "remove.txt")
  121. return Effect.promise(() =>
  122. Promise.all([fs.writeFile(update, "before\n"), fs.writeFile(remove, "remove\n")]),
  123. ).pipe(
  124. Effect.andThen(
  125. withTool(tmp.path, (registry) =>
  126. Effect.gen(function* () {
  127. expect((yield* toolDefinitions(registry)).map((tool) => tool.name)).toEqual(["apply_patch"])
  128. const settled = yield* settleTool(
  129. registry,
  130. call(
  131. "*** Begin Patch\n*** Add File: nested/new.txt\n+created\n*** Update File: update.txt\n@@\n-before\n+after\n*** Delete File: remove.txt\n*** End Patch",
  132. ),
  133. )
  134. expect(settled.result).toEqual({
  135. type: "text",
  136. value: "Applied patch sequentially:\nA nested/new.txt\nM update.txt\nD remove.txt",
  137. })
  138. expect(settled.output?.structured).toMatchObject({
  139. applied: [
  140. { type: "add", resource: "nested/new.txt" },
  141. { type: "update", resource: "update.txt" },
  142. { type: "delete", resource: "remove.txt" },
  143. ],
  144. })
  145. expect(assertions).toMatchObject([
  146. { sessionID, action: "edit", resources: ["nested/new.txt", "update.txt", "remove.txt"], save: ["*"] },
  147. ])
  148. expect(readsBeforeEditApproval).toBe(0)
  149. expect(yield* Effect.promise(() => fs.readFile(path.join(tmp.path, "nested/new.txt"), "utf8"))).toBe(
  150. "created\n",
  151. )
  152. expect(yield* Effect.promise(() => fs.readFile(update, "utf8"))).toBe("after\n")
  153. expect(yield* exists(remove)).toBe(false)
  154. }),
  155. ),
  156. ),
  157. )
  158. },
  159. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  160. ),
  161. )
  162. it.live("rejects moves before applying any hunk", () =>
  163. Effect.acquireUseRelease(
  164. Effect.promise(() => tmpdir()),
  165. (tmp) => {
  166. reset()
  167. const source = path.join(tmp.path, "old.txt")
  168. return Effect.promise(() => fs.writeFile(source, "before\n")).pipe(
  169. Effect.andThen(
  170. withTool(tmp.path, (registry) =>
  171. Effect.gen(function* () {
  172. expect(
  173. yield* executeTool(
  174. registry,
  175. call(
  176. "*** Begin Patch\n*** Add File: created.txt\n+created\n*** Update File: old.txt\n*** Move to: moved.txt\n@@\n-before\n+after\n*** End Patch",
  177. ),
  178. ),
  179. ).toEqual({ type: "error", value: "apply_patch moves are not supported yet" })
  180. expect(yield* exists(path.join(tmp.path, "created.txt"))).toBe(false)
  181. expect(assertions).toEqual([])
  182. }),
  183. ),
  184. ),
  185. )
  186. },
  187. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  188. ),
  189. )
  190. it.live("approves an external directory and the batch before reading external update content", () =>
  191. Effect.acquireUseRelease(
  192. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  193. ([active, outside]) => {
  194. reset()
  195. const target = path.join(outside.path, "external.txt")
  196. return Effect.promise(() => fs.writeFile(target, "before\n")).pipe(
  197. Effect.andThen(
  198. withTool(active.path, (registry) =>
  199. Effect.gen(function* () {
  200. expect(
  201. yield* executeTool(
  202. registry,
  203. call(`*** Begin Patch\n*** Update File: ${target}\n@@\n-before\n+after\n*** End Patch`),
  204. ),
  205. ).toMatchObject({ type: "text" })
  206. expect(assertions.map((input) => input.action)).toEqual(["external_directory", "edit"])
  207. expect(readsBeforeEditApproval).toBe(0)
  208. expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("after\n")
  209. }),
  210. ),
  211. ),
  212. )
  213. },
  214. ([active, outside]) =>
  215. Effect.promise(() =>
  216. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  217. ),
  218. ),
  219. )
  220. it.live("approves one external directory scope for multiple files under the same parent", () =>
  221. Effect.acquireUseRelease(
  222. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  223. ([active, outside]) => {
  224. reset()
  225. const first = path.join(outside.path, "first.txt")
  226. const second = path.join(outside.path, "second.txt")
  227. return Effect.promise(() =>
  228. Promise.all([fs.writeFile(first, "before\n"), fs.writeFile(second, "before\n")]),
  229. ).pipe(
  230. Effect.andThen(
  231. withTool(active.path, (registry) =>
  232. Effect.gen(function* () {
  233. expect(
  234. yield* executeTool(
  235. registry,
  236. call(
  237. `*** Begin Patch\n*** Update File: ${first}\n@@\n-before\n+after\n*** Update File: ${second}\n@@\n-before\n+after\n*** End Patch`,
  238. ),
  239. ),
  240. ).toMatchObject({ type: "text" })
  241. expect(assertions.map((input) => input.action)).toEqual(["external_directory", "edit"])
  242. expect(assertions[0]?.resources).toEqual([
  243. path.join(yield* Effect.promise(() => fs.realpath(outside.path)), "*").replaceAll("\\", "/"),
  244. ])
  245. }),
  246. ),
  247. ),
  248. )
  249. },
  250. ([active, outside]) =>
  251. Effect.promise(() =>
  252. Promise.all([active[Symbol.asyncDispose](), outside[Symbol.asyncDispose]()]).then(() => undefined),
  253. ),
  254. ),
  255. )
  256. it.live("rejects invalid later update before applying an earlier add", () =>
  257. Effect.acquireUseRelease(
  258. Effect.promise(() => tmpdir()),
  259. (tmp) => {
  260. reset()
  261. return withTool(tmp.path, (registry) =>
  262. Effect.gen(function* () {
  263. expect(
  264. yield* executeTool(
  265. registry,
  266. call(
  267. "*** Begin Patch\n*** Add File: created.txt\n+created\n*** Update File: missing.txt\n@@\n-before\n+after\n*** End Patch",
  268. ),
  269. ),
  270. ).toEqual({ type: "error", value: "Unable to apply patch at missing.txt" })
  271. expect(yield* exists(path.join(tmp.path, "created.txt"))).toBe(false)
  272. }),
  273. )
  274. },
  275. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  276. ),
  277. )
  278. it.live("rejects add hunks targeting an existing file without replacing it", () =>
  279. Effect.acquireUseRelease(
  280. Effect.promise(() => tmpdir()),
  281. (tmp) => {
  282. reset()
  283. const target = path.join(tmp.path, "existing.txt")
  284. return Effect.promise(() => fs.writeFile(target, "sentinel\n")).pipe(
  285. Effect.andThen(
  286. withTool(tmp.path, (registry) =>
  287. Effect.gen(function* () {
  288. expect(
  289. yield* executeTool(
  290. registry,
  291. call("*** Begin Patch\n*** Add File: existing.txt\n+replacement\n*** End Patch"),
  292. ),
  293. ).toEqual({ type: "error", value: "Unable to apply patch at existing.txt" })
  294. expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("sentinel\n")
  295. }),
  296. ),
  297. ),
  298. )
  299. },
  300. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  301. ),
  302. )
  303. it.live("rejects an add target that appears during permission approval", () =>
  304. Effect.acquireUseRelease(
  305. Effect.promise(() => tmpdir()),
  306. (tmp) => {
  307. reset()
  308. const target = path.join(tmp.path, "appeared.txt")
  309. afterEditApproval = () => Effect.promise(() => fs.writeFile(target, "winner\n")).pipe(Effect.orDie)
  310. return withTool(tmp.path, (registry) =>
  311. Effect.gen(function* () {
  312. expect(
  313. yield* executeTool(
  314. registry,
  315. call("*** Begin Patch\n*** Add File: appeared.txt\n+replacement\n*** End Patch"),
  316. ),
  317. ).toEqual({ type: "error", value: "Unable to apply patch at appeared.txt" })
  318. expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("winner\n")
  319. }),
  320. )
  321. },
  322. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  323. ),
  324. )
  325. it.live("preserves a later commit defect after earlier sequential applications", () =>
  326. Effect.acquireUseRelease(
  327. Effect.promise(() => tmpdir()),
  328. (tmp) => {
  329. reset()
  330. const first = path.join(tmp.path, "first.txt")
  331. const second = path.join(tmp.path, "second.txt")
  332. failRemoveTarget = path.basename(second)
  333. return Effect.promise(() => Promise.all([fs.writeFile(first, "first"), fs.writeFile(second, "second")])).pipe(
  334. Effect.andThen(
  335. withTool(tmp.path, (registry) =>
  336. Effect.gen(function* () {
  337. expect(
  338. Exit.isFailure(
  339. yield* executeTool(
  340. registry,
  341. call("*** Begin Patch\n*** Delete File: first.txt\n*** Delete File: second.txt\n*** End Patch"),
  342. ).pipe(Effect.exit),
  343. ),
  344. ).toBe(true)
  345. expect(yield* exists(first)).toBe(false)
  346. expect(yield* exists(second)).toBe(true)
  347. }),
  348. ),
  349. ),
  350. )
  351. },
  352. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  353. ),
  354. )
  355. it.live("finishes the sequential commit phase when interrupted after the first mutation", () =>
  356. Effect.acquireUseRelease(
  357. Effect.promise(() => tmpdir()),
  358. (tmp) => {
  359. reset()
  360. const first = path.join(tmp.path, "first.txt")
  361. const second = path.join(tmp.path, "second.txt")
  362. blockRemoveTarget = path.basename(second)
  363. return Effect.gen(function* () {
  364. removeStarted = yield* Deferred.make<void>()
  365. releaseRemove = yield* Deferred.make<void>()
  366. yield* Effect.promise(() => Promise.all([fs.writeFile(first, "first"), fs.writeFile(second, "second")]))
  367. yield* withTool(tmp.path, (registry) =>
  368. Effect.gen(function* () {
  369. const run = yield* executeTool(
  370. registry,
  371. call("*** Begin Patch\n*** Delete File: first.txt\n*** Delete File: second.txt\n*** End Patch"),
  372. ).pipe(Effect.forkChild)
  373. yield* Deferred.await(removeStarted!)
  374. const interrupt = yield* Fiber.interrupt(run).pipe(Effect.forkChild)
  375. yield* Deferred.succeed(releaseRemove!, undefined)
  376. yield* Fiber.join(interrupt)
  377. expect(yield* exists(first)).toBe(false)
  378. expect(yield* exists(second)).toBe(false)
  379. }),
  380. )
  381. })
  382. },
  383. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  384. ),
  385. )
  386. })