Skip to content

Commit ef28865

Browse files
committed
fix(tools): defer the queued-message handoff until the patch checkpoint completes
The per-file handlers processed queued messages right after each successful write, before execute() awaited the whole-patch checkpoint. processQueuedMessages schedules submitUserMessage on a timer, so a queued turn - and the writes it triggers - could start while the patch checkpoint was still staging and committing, interleaving its own checkpoint with this commit. The handoff is now deferred to after the checkpoint attempt: one handoff per successful patch, independent of the checkpoint setting or outcome, so no path strands queued messages and no queued turn can race the commit. This matches the semantics the journal-carrying unit of this series already settled on and brings the remaining units in line. Review feedback on PR 1404; ports carry the equivalent deferral and ordering tests.
1 parent af18969 commit ef28865

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎src/core/tools/ApplyPatchTool.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
239239
)
240240
}
241241
}
242+
243+
// B2: deferred from the per-file handlers - a queued write must not
244+
// start until the post-patch checkpoint above has persisted. The
245+
// handlers used to process queued messages right after each
246+
// successful write; a write happened if and only if
247+
// `successfulChanges` is non-empty, so one call here preserves that.
248+
if (successfulChanges.length > 0) {
249+
task.processQueuedMessages()
250+
}
242251
} catch (error) {
243252
await handleError("apply patch", error as Error)
244253
await task.diffViewProvider.reset()
@@ -349,7 +358,6 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
349358
const message = await task.diffViewProvider.pushToolWriteResult(task, task.cwd, true)
350359
pushToolResult(message)
351360
await task.diffViewProvider.reset()
352-
task.processQueuedMessages()
353361
return { succeeded: true, wrote: true }
354362
}
355363

@@ -421,7 +429,6 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
421429

422430
task.didEditFile = true
423431
pushToolResult(`Successfully deleted ${relPath}`)
424-
task.processQueuedMessages()
425432
return { succeeded: true, wrote: true }
426433
}
427434

@@ -616,7 +623,6 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
616623
const message = await task.diffViewProvider.pushToolWriteResult(task, task.cwd, false)
617624
pushToolResult(message)
618625
await task.diffViewProvider.reset()
619-
task.processQueuedMessages()
620626
if (!moveSucceeded) {
621627
// The destination file was written on disk before the source
622628
// deletion failed, so the write must still be checkpointed and

‎src/core/tools/__tests__/applyPatchTool.execute.spec.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,36 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
195195
expect(mockTask.recordToolError).toHaveBeenCalledWith("apply_patch")
196196
expect(mockedCheckpointSave).not.toHaveBeenCalled()
197197
})
198+
it("awaits the post-patch checkpoint before processing queued messages", async () => {
199+
// A pending checkpoint must not let queued messages (and the
200+
// writes they trigger) start first: execute() awaits the
201+
// post-patch checkpointSave and only then processes queued
202+
// messages, so a queued write's own checkpoint cannot interleave
203+
// with this commit.
204+
const order: string[] = []
205+
;(mockTask.processQueuedMessages as MockedFunction<() => void>).mockImplementation(() => {
206+
order.push("processQueuedMessages")
207+
})
208+
mockedCheckpointSave.mockImplementationOnce(() => {
209+
order.push("checkpointSave:start")
210+
return new Promise<void>((resolve) => {
211+
setTimeout(() => {
212+
order.push("checkpointSave:done")
213+
resolve()
214+
}, 10)
215+
}).then(() => undefined)
216+
})
217+
218+
await tool.execute({ patch: deletePatch }, mockTask as Task, {
219+
askApproval: mockAskApproval,
220+
handleError: mockHandleError,
221+
pushToolResult: mockPushToolResult,
222+
})
223+
224+
// The checkpoint fully completes before queued messages are
225+
// processed.
226+
expect(order).toEqual(["checkpointSave:start", "checkpointSave:done", "processQueuedMessages"])
227+
})
198228
})
199229

200230
describe("checkpoint only for fully successful patches (B1)", () => {

0 commit comments

Comments
 (0)