Skip to content

Commit e406c09

Browse files
Fix the two checkpoint-queue findings the review raised, and stop a self-move deleting its own file
The unit is at 295 of the gate's 500 counted executable lines, so these are implemented here rather than deferred: this reconciliation adds 30 counted lines (src/core/checkpoints/index.ts +27, src/core/tools/ApplyPatchTool.ts +3), measured with scripts/stryker-diff.mjs's own functions against 48be786 before the commit, for 325 of 500 afterwards. The five new test rows are free budget because the counter excludes __tests__ paths. Checkpoint queue (threads 7pkQ on Task.ts and 7p-r on checkpoints/index.ts). The per-task queue serialized checkpoint work but never expired it: after a baseline whose git add/commit outlives checkpointTimeout, the caller was released by the Promise.race in initiateTaskLoop while the queued saves still started, however late, committing whatever the workspace had become by then, and a save queued by a task that had already been canceled still performed checkpoint work. The queue now gates the start of each save: one past its checkpoint window, or requested by a canceled task, is dropped instead of started late and resolves to the caller exactly as a missing service does - with no commit - which is how the task-start baseline already treats a save that produced no commit, by retrying it on a later loop entry. Restores keep no expiry: dropping a queued restore would silently discard an explicit user action. Four rows in src/core/checkpoints/__tests__/checkpoint.test.ts cover the expired drop, the canceled drop, a fresh save running normally after the stall settles (the drop does not poison the queue) and a restore that is never dropped; one row in Task.spec.ts covers the case the reviewer asked about - a save that commits after the task already gave up on it does not retire the baseline. Self-move (thread 7p-w on ApplyPatchTool.ts). A patch whose Move to resolves onto its own Update File path is a rewrite of that path, not a relocation: the staged content is renamed onto it and the trailing source unlink then deletes the file that was just committed. The unlink is skipped when the two resolved paths match. A row in applyPatchTool.execute.spec.ts asserts the rename happened and no unlink happened. Nothing else from the review's open rows is implemented here: preserving a symlink destination instead of replacing it (7pkY), the recovery artifact for a host exit between the two renames (7p-u), and the resolved- target workspace and protection checks belong to the single guarded writer in unit 2 of the split proposed on PR 1408 (refs/tmp/s3-1408-u2-guardedwrite), where the compare-and-swap write, its rollback and its lock live. Implementing symlink semantics twice, once in this move path and once in that writer, would give the workspace two rollback stories.
1 parent 48be786 commit e406c09

5 files changed

Lines changed: 280 additions & 7 deletions

File tree

‎src/core/checkpoints/__tests__/checkpoint.test.ts‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,151 @@ describe("Checkpoint functionality", () => {
140140
expect(order).toEqual(["first:start", "second:start"])
141141
})
142142

143+
it("drops a queued save whose checkpoint window expired behind a stalled predecessor", async () => {
144+
// A stalled baseline releases the task after checkpointTimeout, but the queue used to
145+
// start it anyway, however late, committing whatever the workspace had become by then.
146+
// It must not: the save is dropped and the caller resolves with no commit.
147+
mockTask.checkpointTimeout = 0.01
148+
const order: string[] = []
149+
let resolveFirst: (value: { commit: string }) => void = () => {}
150+
mockCheckpointService.saveCheckpoint
151+
.mockImplementationOnce(() => {
152+
order.push("stalled:start")
153+
return new Promise((resolve) => {
154+
resolveFirst = resolve
155+
})
156+
})
157+
.mockImplementation(async () => {
158+
order.push("late:start")
159+
return { commit: "late-commit" }
160+
})
161+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
162+
163+
const stalled = checkpointSave(mockTask, true, true)
164+
const queued = await (async () => {
165+
await new Promise((resolve) => setTimeout(resolve, 5))
166+
const second = checkpointSave(mockTask, false, true)
167+
// Give the expired request its turn behind the still-running baseline, then release
168+
// the baseline so the queue drains.
169+
await new Promise((resolve) => setTimeout(resolve, 30))
170+
resolveFirst({ commit: "stalled-commit" })
171+
return second
172+
})()
173+
174+
const second = await queued
175+
await stalled
176+
// Only the stalled baseline ever reached the shadow git; the expired save never started,
177+
// so no commit describes a workspace that has since moved on.
178+
expect(order).toEqual(["stalled:start"])
179+
expect(mockCheckpointService.saveCheckpoint).toHaveBeenCalledTimes(1)
180+
expect(second).toBeUndefined()
181+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("skipping queued per-write checkpoint save"))
182+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("expired behind a slower checkpoint"))
183+
warnSpy.mockRestore()
184+
})
185+
186+
it("drops a queued save for a task that was canceled while it waited", async () => {
187+
mockTask.checkpointTimeout = 15
188+
const order: string[] = []
189+
let resolveFirst: (value: { commit: string }) => void = () => {}
190+
mockCheckpointService.saveCheckpoint
191+
.mockImplementationOnce(() => {
192+
order.push("stalled:start")
193+
return new Promise((resolve) => {
194+
resolveFirst = resolve
195+
})
196+
})
197+
.mockImplementation(async () => {
198+
order.push("canceled:start")
199+
return { commit: "canceled-commit" }
200+
})
201+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
202+
mockTask.abort = false
203+
204+
const stalled = checkpointSave(mockTask, true, true)
205+
const queued = checkpointSave(mockTask, false, true)
206+
await new Promise((resolve) => setTimeout(resolve, 5))
207+
// Cancellation arrives while the save is queued, before its turn.
208+
mockTask.abort = true
209+
resolveFirst({ commit: "stalled-commit" })
210+
211+
const second = await queued
212+
await stalled
213+
214+
expect(order).toEqual(["stalled:start"])
215+
expect(mockCheckpointService.saveCheckpoint).toHaveBeenCalledTimes(1)
216+
expect(second).toBeUndefined()
217+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("task canceled"))
218+
warnSpy.mockRestore()
219+
mockTask.abort = false
220+
})
221+
222+
it("runs a save requested after a stalled predecessor settles, exactly once each", async () => {
223+
// The gate must not poison the queue: the dropped request stays dropped, while a fresh
224+
// request after the stall settles gets its normal turn.
225+
mockTask.checkpointTimeout = 0.01
226+
const order: string[] = []
227+
let resolveFirst: (value: { commit: string }) => void = () => {}
228+
mockCheckpointService.saveCheckpoint
229+
.mockImplementationOnce(() => {
230+
order.push("stalled:start")
231+
return new Promise((resolve) => {
232+
resolveFirst = resolve
233+
})
234+
})
235+
.mockImplementation(async () => {
236+
order.push("fresh:start")
237+
return { commit: "fresh-commit" }
238+
})
239+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
240+
241+
const stalled = checkpointSave(mockTask, true, true)
242+
// Requested while its window is still open, so the drop below is about the wait behind
243+
// the stalled baseline rather than about an already-expired request.
244+
const expired = checkpointSave(mockTask, false, true)
245+
await new Promise((resolve) => setTimeout(resolve, 30))
246+
resolveFirst({ commit: "stalled-commit" })
247+
const expiredResult = await expired
248+
const settled = await stalled
249+
const fresh = await checkpointSave(mockTask, false, true)
250+
251+
expect(order).toEqual(["stalled:start", "fresh:start"])
252+
expect(mockCheckpointService.saveCheckpoint).toHaveBeenCalledTimes(2)
253+
// The dropped request resolves exactly the way a missing service does: no commit value,
254+
// while the predecessor that held the queue keeps its own result.
255+
expect(expiredResult).toBeUndefined()
256+
expect(settled).toEqual({ commit: "stalled-commit" })
257+
expect(fresh).toEqual({ commit: "fresh-commit" })
258+
warnSpy.mockRestore()
259+
})
260+
261+
it("never drops a user-initiated restore queued behind a stalled save", async () => {
262+
// Restores carry no expiry: dropping one would silently discard an explicit user action.
263+
mockTask.checkpointTimeout = 0.01
264+
mockTask.clineMessages = [{ ts: 2, type: "say", say: "checkpoint_saved", text: RESTORE_HASH }]
265+
const order: string[] = []
266+
let resolveSave: (value: { commit: string }) => void = () => {}
267+
mockCheckpointService.saveCheckpoint.mockImplementationOnce(() => {
268+
order.push("save:start")
269+
return new Promise((resolve) => {
270+
resolveSave = resolve
271+
})
272+
})
273+
mockCheckpointService.restoreCheckpoint.mockImplementationOnce(() => {
274+
order.push("restore:start")
275+
return Promise.resolve(undefined)
276+
})
277+
278+
const stalled = checkpointSave(mockTask, true, true)
279+
const restore = checkpointRestore(mockTask, { ts: 2, commitHash: RESTORE_HASH, mode: "preview" })
280+
await new Promise((resolve) => setTimeout(resolve, 30))
281+
resolveSave({ commit: "stalled-commit" })
282+
await Promise.all([stalled, restore])
283+
284+
expect(order).toEqual(["save:start", "restore:start"])
285+
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith(RESTORE_HASH)
286+
})
287+
143288
it("should wait for checkpoint service initialization before saving", async () => {
144289
// Set up task with uninitialized service
145290
mockCheckpointService.isInitialized = false

‎src/core/checkpoints/index.ts‎

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import pWaitFor from "p-wait-for"
22
import * as vscode from "vscode"
33

44
import type { ClineApiReqInfo } from "@roo-code/types"
5+
import { DEFAULT_CHECKPOINT_TIMEOUT_SECONDS } from "@roo-code/types"
56
import { TelemetryService } from "@roo-code/telemetry"
67

78
import { Task } from "../task/Task"
@@ -233,9 +234,43 @@ async function checkGitInstallation(
233234
// is still running.
234235
const checkpointWorkQueues = new Map<string, Promise<unknown>>()
235236

236-
function enqueueCheckpointWork<T>(taskId: string, operation: () => Promise<T>): Promise<T> {
237+
type QueuedCheckpointWorkOptions = {
238+
/** Human-readable name used in the skip warning, e.g. "queued per-write checkpoint save". */
239+
label: string
240+
/** Wall-clock deadline after which the operation must not start: a save requested behind a
241+
* stalled predecessor stages whatever the workspace looks like when it finally gets its turn,
242+
* which is not the state the caller asked to record. Omit for work that must run whenever its
243+
* turn arrives, such as a user-initiated restore. */
244+
expiresAt?: number
245+
/** Checked when the operation's turn arrives, not when it is requested: a task that was canceled
246+
* while its work was queued performs no checkpoint work. */
247+
isCanceled?: () => boolean
248+
}
249+
250+
const DEFAULT_QUEUED_WORK_OPTIONS: QueuedCheckpointWorkOptions = { label: "checkpoint work" }
251+
252+
function enqueueCheckpointWork<T>(
253+
taskId: string,
254+
operation: () => Promise<T>,
255+
options: QueuedCheckpointWorkOptions = DEFAULT_QUEUED_WORK_OPTIONS,
256+
): Promise<T | undefined> {
237257
const previous = checkpointWorkQueues.get(taskId) ?? Promise.resolve()
238-
const next = previous.then(operation, operation)
258+
// Both arms of the previous settlement get the same gate, so a failed predecessor does not
259+
// smuggle an expired or canceled operation past it.
260+
// attempt may return the operation's promise or nothing at all; Promise.then flattens the
261+
// former and resolves the latter, which is exactly the two outcomes callers need.
262+
const attempt = (): T | Promise<T> | undefined => {
263+
const expired = options.expiresAt !== undefined && Date.now() > options.expiresAt
264+
if (expired || options.isCanceled?.()) {
265+
const reason = expired ? "expired behind a slower checkpoint" : "task canceled"
266+
console.warn(
267+
`[checkpoints] skipping ${options.label} for task ${taskId}: ${reason}; it never reached the shadow git`,
268+
)
269+
return undefined
270+
}
271+
return operation()
272+
}
273+
const next = previous.then(attempt, attempt)
239274
const tail = next.then(
240275
() => undefined,
241276
() => undefined,
@@ -259,9 +294,22 @@ export async function checkpointSave(task: Task, force = false, suppressMessage
259294
TelemetryService.instance.captureCheckpointCreated(task.taskId)
260295

261296
// Start the checkpoint process in the background, queued behind any checkpoint work
262-
// already running for this task so stageAll + commit never overlap.
263-
return enqueueCheckpointWork(task.taskId, () =>
264-
service.saveCheckpoint(`Task: ${task.taskId}, Time: ${Date.now()}`, { allowEmpty: force, suppressMessage }),
297+
// already running for this task so stageAll + commit never overlap. The queue is not only a
298+
// lock: a save that is still queued past the caller's own checkpoint window is describing a
299+
// workspace that has moved on, so it is dropped rather than started late, and a save queued by a
300+
// task that has since been canceled performs no checkpoint work at all. A dropped save is
301+
// reported to the caller the same way a missing service is (a resolve with no commit), which is
302+
// how the task-start baseline already treats it - by retrying on a later loop entry.
303+
const timeoutSeconds = task.checkpointTimeout > 0 ? task.checkpointTimeout : DEFAULT_CHECKPOINT_TIMEOUT_SECONDS
304+
return enqueueCheckpointWork(
305+
task.taskId,
306+
() =>
307+
service.saveCheckpoint(`Task: ${task.taskId}, Time: ${Date.now()}`, { allowEmpty: force, suppressMessage }),
308+
{
309+
label: force ? "queued task-start checkpoint baseline" : "queued per-write checkpoint save",
310+
expiresAt: Date.now() + Math.max(1, timeoutSeconds * 1000),
311+
isCanceled: () => task.abort,
312+
},
265313
).catch((err) => {
266314
console.error("[Task#checkpointSave] caught unexpected error, disabling checkpoints", err)
267315
task.enableCheckpoints = false

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7368,6 +7368,46 @@ describe("Cline", () => {
73687368
warnSpy.mockRestore()
73697369
})
73707370

7371+
it("retries the baseline on a later loop entry when the stalled save commits after the timeout", async () => {
7372+
// The reviewer's question, tested at the layer that owns it: what becomes of the
7373+
// baseline once a save the task already gave up on finally settles. A late commit
7374+
// must not retire the baseline, because writes have been performed in the meantime,
7375+
// so a later loop entry re-takes it.
7376+
const task = new Task({
7377+
provider: mockProvider,
7378+
apiConfiguration: mockApiConfig,
7379+
task: "late baseline commit task",
7380+
startTask: false,
7381+
})
7382+
const taskAccess = getTaskTestAccess(task)
7383+
type SaveResult = Awaited<ReturnType<typeof task.checkpointSave>>
7384+
let resolveStalled: (value: SaveResult) => void = () => {}
7385+
const saveSpy = vi
7386+
.spyOn(task, "checkpointSave")
7387+
.mockImplementationOnce(() => new Promise<SaveResult>((resolve) => (resolveStalled = resolve)))
7388+
.mockResolvedValueOnce({ commit: "retry-baseline-commit" } satisfies Awaited<
7389+
ReturnType<typeof task.checkpointSave>
7390+
>)
7391+
const state = await mockProvider.getState()
7392+
vi.spyOn(mockProvider, "getState").mockResolvedValue(state)
7393+
const requestSpy = vi.spyOn(task, "recursivelyMakeClineRequests").mockResolvedValue(true)
7394+
task.checkpointTimeout = 0.01
7395+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {})
7396+
7397+
await taskAccess.initiateTaskLoop([])
7398+
expect(saveSpy).toHaveBeenCalledOnce()
7399+
expect(requestSpy).toHaveBeenCalled()
7400+
7401+
// The abandoned save commits long after the task moved on.
7402+
resolveStalled({ commit: "late-baseline-commit" } as SaveResult)
7403+
await new Promise((resolve) => setTimeout(resolve, 0))
7404+
7405+
await taskAccess.initiateTaskLoop([])
7406+
// The late commit did not mark the baseline done: this loop entry asked for it again.
7407+
expect(saveSpy).toHaveBeenCalledTimes(2)
7408+
warnSpy.mockRestore()
7409+
})
7410+
73717411
it("continues task startup when the baseline checkpoint fails", async () => {
73727412
const task = new Task({
73737413
provider: mockProvider,

‎src/core/tools/ApplyPatchTool.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,13 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
530530
return { ok: false, mutated }
531531
}
532532

533+
// A patch whose "Move to" resolves back onto its own source path is a rewrite of
534+
// that path, not a relocation: once the staged content is renamed onto the destination
535+
// there is no second file left, and unlinking the source would delete the content that
536+
// was just committed. Computed from resolved paths because case-insensitive and
537+
// normalized spellings can name the same file differently.
538+
const isMoveOntoSource = path.resolve(moveAbsolutePath) === path.resolve(absolutePath)
539+
533540
// Capture the destination state before either supported write path: a
534541
// failed move must restore an existing destination instead of deleting
535542
// whatever lived there. A read error other than ENOENT means the
@@ -638,9 +645,13 @@ export class ApplyPatchTool extends BaseTool<"apply_patch"> {
638645
return { ok: false, mutated }
639646
}
640647

641-
// Delete the original file
648+
// Delete the original file, unless the move renamed its own path: there is no
649+
// second copy to remove in that case, and this is the step that would erase the
650+
// freshly written content.
642651
try {
643-
await fs.unlink(absolutePath)
652+
if (!isMoveOntoSource) {
653+
await fs.unlink(absolutePath)
654+
}
644655
} catch (error) {
645656
// The source is still present (locked or protected): undo the destination
646657
// so the file does not live in two places, and report the move as

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,35 @@ describe("ApplyPatchTool.execute - delete file success path", () => {
178178
expect(mockHandleError).toHaveBeenCalled()
179179
})
180180

181+
it("keeps the written content when a move targets the file's own path", async () => {
182+
// A patch whose Move to resolves back onto its Update File path rewrites that path;
183+
// it does not create a second copy. Unlinking the source afterwards - the same inode
184+
// the destination now holds - would delete the content the patch just committed.
185+
const patch = `*** Begin Patch
186+
*** Update File: src/old.ts
187+
*** Move to: src/old.ts
188+
@@
189+
-original file content
190+
+new content
191+
*** End Patch`
192+
193+
await tool.execute({ patch }, mockTask as Task, {
194+
askApproval: mockAskApproval,
195+
handleError: mockHandleError,
196+
pushToolResult: mockPushToolResult,
197+
toolCallId: "test-tool-call-id",
198+
})
199+
200+
// The staged content was renamed onto the path, and nothing was unlinked.
201+
expect(mockedFsPromises.default.rename).toHaveBeenCalledWith(
202+
expect.stringContaining(".staging"),
203+
expect.stringMatching(/[\\/]old\.ts$/),
204+
)
205+
expect(mockedFsPromises.default.unlink).not.toHaveBeenCalled()
206+
expect(mockHandleError).not.toHaveBeenCalled()
207+
expect(mockTask.diffViewProvider.pushToolWriteResult).toHaveBeenCalledOnce()
208+
})
209+
181210
it("restores the pre-move state and fails the operation when source deletion fails", async () => {
182211
const patch = `*** Begin Patch
183212
*** Update File: src/old.ts

0 commit comments

Comments
 (0)