fix: parallel edits sometimes would override each other (#23483)
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
import z from "zod"
|
||||
import * as path from "path"
|
||||
import { Effect } from "effect"
|
||||
import { Effect, Semaphore } from "effect"
|
||||
import * as Tool from "./tool"
|
||||
import { LSP } from "../lsp"
|
||||
import { createTwoFilesPatch, diffLines } from "diff"
|
||||
@@ -32,6 +32,18 @@ function convertToLineEnding(text: string, ending: "\n" | "\r\n"): string {
|
||||
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({
|
||||
filePath: z.string().describe("The absolute path to the file to modify"),
|
||||
oldString: z.string().describe("The text to replace"),
|
||||
@@ -68,7 +80,8 @@ export const EditTool = Tool.define(
|
||||
let diff = ""
|
||||
let contentOld = ""
|
||||
let contentNew = ""
|
||||
yield* Effect.gen(function* () {
|
||||
yield* lock(filePath).withPermits(1)(
|
||||
Effect.gen(function* () {
|
||||
if (params.oldString === "") {
|
||||
const existed = yield* afs.existsSafe(filePath)
|
||||
contentNew = params.newString
|
||||
@@ -137,7 +150,8 @@ export const EditTool = Tool.define(
|
||||
normalizeLineEndings(contentNew),
|
||||
),
|
||||
)
|
||||
}).pipe(Effect.orDie)
|
||||
}).pipe(Effect.orDie),
|
||||
)
|
||||
|
||||
const filediff: Snapshot.FileDiff = {
|
||||
file: filePath,
|
||||
|
||||
@@ -29,11 +29,6 @@ afterEach(async () => {
|
||||
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(
|
||||
Layer.mergeAll(
|
||||
LSP.defaultLayer,
|
||||
@@ -639,44 +634,59 @@ describe("tool.edit", () => {
|
||||
})
|
||||
|
||||
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()
|
||||
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({
|
||||
directory: tmp.path,
|
||||
fn: async () => {
|
||||
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(
|
||||
edit.execute(
|
||||
{
|
||||
filePath: filepath,
|
||||
oldString: "0",
|
||||
newString: "1",
|
||||
oldString: "top = 0",
|
||||
newString: "top = 1",
|
||||
},
|
||||
ctx,
|
||||
delayedCtx,
|
||||
),
|
||||
)
|
||||
|
||||
await firstAsk.promise
|
||||
|
||||
const promise2 = Effect.runPromise(
|
||||
edit.execute(
|
||||
{
|
||||
filePath: filepath,
|
||||
oldString: "0",
|
||||
newString: "2",
|
||||
oldString: "bottom = 0",
|
||||
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])
|
||||
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")
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user