tool-apply-patch.test.ts 14 KB

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