fix: parallel edits sometimes would override each other (#23483)

This commit is contained in:
Aiden Cline
2026-04-20 00:14:21 -05:00
committed by GitHub
parent 93e633fb7d
commit 8bc4f91fd9
2 changed files with 97 additions and 73 deletions
+17 -3
View File
@@ -5,7 +5,7 @@
import z from "zod" import z from "zod"
import * as path from "path" import * as path from "path"
import { Effect } from "effect" import { Effect, Semaphore } from "effect"
import * as Tool from "./tool" import * as Tool from "./tool"
import { LSP } from "../lsp" import { LSP } from "../lsp"
import { createTwoFilesPatch, diffLines } from "diff" import { createTwoFilesPatch, diffLines } from "diff"
@@ -32,6 +32,18 @@ function convertToLineEnding(text: string, ending: "\n" | "\r\n"): string {
return text.replaceAll("\n", "\r\n") return text.replaceAll("\n", "\r\n")
} }
const locks = new Map<string, Semaphore.Semaphore>()
function lock(filePath: string) {
const resolvedFilePath = AppFileSystem.resolve(filePath)
const hit = locks.get(resolvedFilePath)
if (hit) return hit
const next = Semaphore.makeUnsafe(1)
locks.set(resolvedFilePath, next)
return next
}
const Parameters = z.object({ const Parameters = z.object({
filePath: z.string().describe("The absolute path to the file to modify"), filePath: z.string().describe("The absolute path to the file to modify"),
oldString: z.string().describe("The text to replace"), oldString: z.string().describe("The text to replace"),
@@ -68,7 +80,8 @@ export const EditTool = Tool.define(
let diff = "" let diff = ""
let contentOld = "" let contentOld = ""
let contentNew = "" let contentNew = ""
yield* Effect.gen(function* () { yield* lock(filePath).withPermits(1)(
Effect.gen(function* () {
if (params.oldString === "") { if (params.oldString === "") {
const existed = yield* afs.existsSafe(filePath) const existed = yield* afs.existsSafe(filePath)
contentNew = params.newString contentNew = params.newString
@@ -137,7 +150,8 @@ export const EditTool = Tool.define(
normalizeLineEndings(contentNew), normalizeLineEndings(contentNew),
), ),
) )
}).pipe(Effect.orDie) }).pipe(Effect.orDie),
)
const filediff: Snapshot.FileDiff = { const filediff: Snapshot.FileDiff = {
file: filePath, file: filePath,
+26 -16
View File
@@ -29,11 +29,6 @@ afterEach(async () => {
await Instance.disposeAll() await Instance.disposeAll()
}) })
async function touch(file: string, time: number) {
const date = new Date(time)
await fs.utimes(file, date, date)
}
const runtime = ManagedRuntime.make( const runtime = ManagedRuntime.make(
Layer.mergeAll( Layer.mergeAll(
LSP.defaultLayer, LSP.defaultLayer,
@@ -639,44 +634,59 @@ describe("tool.edit", () => {
}) })
describe("concurrent editing", () => { describe("concurrent editing", () => {
test("serializes concurrent edits to same file", async () => { test("preserves concurrent edits to different sections of the same file", async () => {
await using tmp = await tmpdir() await using tmp = await tmpdir()
const filepath = path.join(tmp.path, "file.txt") const filepath = path.join(tmp.path, "file.txt")
await fs.writeFile(filepath, "0", "utf-8") await fs.writeFile(filepath, "top = 0\nmiddle = keep\nbottom = 0\n", "utf-8")
await Instance.provide({ await Instance.provide({
directory: tmp.path, directory: tmp.path,
fn: async () => { fn: async () => {
const edit = await resolve() const edit = await resolve()
let asks = 0
const firstAsk = Promise.withResolvers<void>()
const delayedCtx = {
...ctx,
ask: () =>
Effect.gen(function* () {
asks++
if (asks !== 1) return
firstAsk.resolve()
yield* Effect.promise(() => Bun.sleep(50))
}),
}
// Two concurrent edits
const promise1 = Effect.runPromise( const promise1 = Effect.runPromise(
edit.execute( edit.execute(
{ {
filePath: filepath, filePath: filepath,
oldString: "0", oldString: "top = 0",
newString: "1", newString: "top = 1",
}, },
ctx, delayedCtx,
), ),
) )
await firstAsk.promise
const promise2 = Effect.runPromise( const promise2 = Effect.runPromise(
edit.execute( edit.execute(
{ {
filePath: filepath, filePath: filepath,
oldString: "0", oldString: "bottom = 0",
newString: "2", newString: "bottom = 2",
}, },
ctx, delayedCtx,
), ),
) )
// Both should complete without error (though one might fail due to content mismatch)
const results = await Promise.allSettled([promise1, promise2]) const results = await Promise.allSettled([promise1, promise2])
expect(results.some((r) => r.status === "fulfilled")).toBe(true) expect(results[0]?.status).toBe("fulfilled")
expect(results[1]?.status).toBe("fulfilled")
expect(await fs.readFile(filepath, "utf-8")).toBe("top = 1\nmiddle = keep\nbottom = 2\n")
}, },
}) })
}) })
}) })
}) })