Skip to content

Commit 6b7d45e

Browse files
fix(apply-diff): retire the abandoned read's observation after a stale rejection
open() re-reads the file and records that newer version as the save baseline (src/integrations/editor/DiffViewProvider.ts:149-157). When apply_diff then rejects the diff as stale, the rollback retires the editor buffer but the observation stayed current, so a later write_to_file - focus-disruption save enabled - could publish on a read this tool had abandoned. ObservationRegistry gains a per-path forget so the stale flow drops exactly that path; clear() would have stripped observations that belong to other files. Answers review root PRRT_kwDOSK-bwM61KUm3 (src/core/tools/ApplyDiffTool.ts:238). Controls and their exits at the tree this replaces (b49fa5d): - src/core/task/__tests__/observationRegistry.spec.ts "forget drops one path and leaves every other path observed" - exit 1, TypeError: "reg.forget is not a function"; exit 0 here (14 passed). - src/core/tools/__tests__/applyDiffTool.guardedWrite.spec.ts "retires the diff view observation for this path when it rejects a stale diff" - exit 1, "expected true to be false // Object.is equality"; exit 0 here (12 passed). applyDiffTool.failureReporting.spec.ts also re-run green (35 passed).
1 parent b49fa5d commit 6b7d45e

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎src/core/task/__tests__/observationRegistry.spec.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ describe("ObservationRegistry", () => {
3636
expect(reg.has("/y.ts")).toBe(false)
3737
})
3838

39+
it("forget drops one path and leaves every other path observed", () => {
40+
const reg = new ObservationRegistry()
41+
reg.observe("/x.ts", "t1")
42+
reg.observe("/y.ts", "t2")
43+
44+
expect(reg.forget("/x.ts")).toBe(true)
45+
expect(reg.has("/x.ts")).toBe(false)
46+
expect(reg.get("/y.ts")?.version).toBe("t2")
47+
expect(reg.size).toBe(1)
48+
49+
// Forgetting a path nobody observed reports rather than throws.
50+
expect(reg.forget("/z.ts")).toBe(false)
51+
})
3952
it("size reflects the number of observed entries", () => {
4053
const reg = new ObservationRegistry()
4154
expect(reg.size).toBe(0)

‎src/core/task/observationRegistry.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ export class ObservationRegistry {
5656
return this.entries.has(absolutePath)
5757
}
5858

59+
/**
60+
* Drop the observation for one path, leaving every other path observed.
61+
*
62+
* A tool that abandons its own read - a stale diff it will not save - must not leave
63+
* that read's version token behind as authority for some later write. Retiring the
64+
* whole registry would strip observations that belong to other files and to other
65+
* tasks' work, so the removal is per path. Reports whether an entry was removed.
66+
*/
67+
forget(absolutePath: string): boolean {
68+
return this.entries.delete(absolutePath)
69+
}
70+
5971
clear(): void {
6072
this.entries.clear()
6173
}

‎src/core/tools/ApplyDiffTool.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> {
244244
await task.say("error", staleError)
245245
task.didToolFailInCurrentTurn = true
246246
await task.diffViewProvider.revertChanges()
247+
// The rollback retires the editor buffer, but open() had already recorded the
248+
// newer on-disk version as this flow's observation (DiffViewProvider.ts:149-157).
249+
// Leaving it current would let a later write - write_to_file with the
250+
// focus-disruption save - publish on a read this tool abandoned, so retire that
251+
// path only; other files keep what their own reads recorded.
252+
task.observationRegistry.forget(absolutePath)
247253
task.processQueuedMessages()
248254
pushToolResult(staleError)
249255
return

‎src/core/tools/__tests__/applyDiffTool.guardedWrite.spec.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,4 +441,51 @@ describe("ApplyDiffTool.execute - guarded write (S4b, epic #1375)", () => {
441441
expect.stringContaining("changed after it was read for this diff"),
442442
)
443443
})
444+
it("retires the diff view observation for this path when it rejects a stale diff", async () => {
445+
// open() re-reads the file and records that newer version as the save baseline
446+
// (src/integrations/editor/DiffViewProvider.ts:149-157). When the tool then rejects
447+
// the diff as stale, the rollback retires the buffer but leaves that observation
448+
// current, and a later write_to_file - focus-disruption save enabled - can spend it
449+
// on an overwrite the model never read. Retiring it must not retire other paths.
450+
const absolutePath = path.resolve("/workspace/project", "src/thing.ts")
451+
const otherPath = path.resolve("/workspace/project", "src/other.ts")
452+
mockTask.observationRegistry.observe(otherPath, "7:4242:1234:1700000000123456789:1700000000789999999")
453+
454+
const diffViewStub = mockTask.diffViewProvider as unknown as Record<string, unknown>
455+
diffViewStub.open = vi.fn().mockImplementation(async () => {
456+
diffViewStub.originalContent = "written by another process\n"
457+
// What production open() does after re-reading the file: observe the newer token.
458+
mockTask.observationRegistry.observe(absolutePath, "7:4242:1234:1700000000123456799:1700000000789000001")
459+
})
460+
diffViewStub.update = vi.fn().mockResolvedValue(undefined)
461+
diffViewStub.scrollToFirstDiff = vi.fn()
462+
diffViewStub.revertChanges = vi.fn().mockResolvedValue(undefined)
463+
diffViewStub.saveChanges = vi.fn().mockResolvedValue({
464+
newProblemsMessage: "",
465+
userEdits: undefined,
466+
finalContent: "new content",
467+
})
468+
mockTask.providerRef = {
469+
deref: vi.fn().mockReturnValue({
470+
getState: vi.fn().mockResolvedValue({
471+
diagnosticsEnabled: true,
472+
writeDelayMs: 1000,
473+
experiments: {},
474+
}),
475+
}),
476+
} as unknown as Task["providerRef"]
477+
478+
await tool.execute({ path: "src/thing.ts", diff: "unified diff" }, mockTask as Task, {
479+
askApproval: mockAskApproval,
480+
handleError: mockHandleError,
481+
pushToolResult: mockPushToolResult,
482+
toolCallId: "test-tool-call-id",
483+
})
484+
485+
expect(diffViewStub.update).not.toHaveBeenCalled()
486+
expect(diffViewStub.saveChanges).not.toHaveBeenCalled()
487+
expect(diffViewStub.revertChanges).toHaveBeenCalled()
488+
expect(mockTask.observationRegistry.has(absolutePath)).toBe(false)
489+
expect(mockTask.observationRegistry.has(otherPath)).toBe(true)
490+
})
444491
})

0 commit comments

Comments
 (0)