Skip to content

Commit a044e48

Browse files
author
Bertan Ari
committed
fix(checkpoints): surface restore failures to the user and recover safely
Rethrow the original restore error from ShadowCheckpointService and checkpointRestore. On a failed delete restore, skip saving and rehydration and offer a localized Restart prompt; on a failed edit restore, discard the pending edit. Show localized, specific failure reasons for plain and completion restores.
1 parent e7a5c57 commit a044e48

29 files changed

Lines changed: 879 additions & 480 deletions

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

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,32 @@ vi.mock("@roo-code/telemetry", () => ({
3030
},
3131
}))
3232

33-
vi.mock("../../../utils/path", () => ({
34-
getWorkspacePath: vi.fn(() => "/test/workspace"),
35-
}))
36-
37-
vi.mock("../../../utils/git", () => ({
38-
checkGitInstalled: vi.fn().mockResolvedValue(true),
39-
}))
40-
41-
vi.mock("../../../i18n", () => ({
42-
t: vi.fn((key: string, options?: Record<string, any>) => {
43-
if (key === "common:errors.wait_checkpoint_long_time") {
44-
return `Checkpoint initialization is taking longer than ${options?.timeout} seconds...`
45-
}
46-
if (key === "common:errors.init_checkpoint_fail_long_time") {
47-
return `Checkpoint initialization failed after ${options?.timeout} seconds`
48-
}
49-
return key
50-
}),
51-
}))
52-
5333
// Mock p-wait-for to control timeout behavior
5434
vi.mock("p-wait-for", () => ({
5535
default: vi.fn(),
5636
}))
5737

58-
vi.mock("../../../services/checkpoints")
59-
6038
describe("Checkpoint functionality", () => {
6139
let mockProvider: any
6240
let mockTask: any
6341
let mockCheckpointService: any
6442

6543
beforeEach(async () => {
44+
const pathModule = await import("../../../utils/path")
45+
const gitModule = await import("../../../utils/git")
46+
const i18nModule = await import("../../../i18n")
47+
vi.spyOn(pathModule, "getWorkspacePath").mockReturnValue("/test/workspace")
48+
vi.spyOn(gitModule, "checkGitInstalled").mockResolvedValue(true)
49+
vi.spyOn(i18nModule, "t").mockImplementation((key: string, options?: Record<string, unknown>) => {
50+
if (key === "common:errors.wait_checkpoint_long_time") {
51+
return `Checkpoint initialization is taking longer than ${options?.timeout} seconds...`
52+
}
53+
if (key === "common:errors.init_checkpoint_fail_long_time") {
54+
return `Checkpoint initialization failed after ${options?.timeout} seconds`
55+
}
56+
return key
57+
})
58+
6659
// Create mock checkpoint service
6760
mockCheckpointService = {
6861
isInitialized: true,
@@ -105,11 +98,12 @@ describe("Checkpoint functionality", () => {
10598

10699
// Update the mock to return our mockCheckpointService
107100
const checkpointsModule = await import("../../../services/checkpoints")
108-
vi.mocked(checkpointsModule.RepoPerTaskCheckpointService.create).mockReturnValue(mockCheckpointService)
101+
vi.spyOn(checkpointsModule.RepoPerTaskCheckpointService, "create").mockReturnValue(mockCheckpointService)
109102
})
110103

111104
afterEach(() => {
112105
vi.clearAllMocks()
106+
vi.restoreAllMocks()
113107
})
114108

115109
describe("checkpointSave", () => {
@@ -280,18 +274,34 @@ describe("Checkpoint functionality", () => {
280274
expect(mockCheckpointService.restoreCheckpoint).not.toHaveBeenCalled()
281275
})
282276

283-
it("should disable checkpoints on error", async () => {
284-
mockCheckpointService.restoreCheckpoint.mockRejectedValue(new Error("Restore failed"))
285-
286-
await checkpointRestore(mockTask, {
287-
ts: 2,
288-
commitHash: "abc123",
289-
mode: "restore",
290-
})
291-
292-
expect(mockTask.enableCheckpoints).toBe(false)
293-
expect(mockProvider.log).toHaveBeenCalledWith("[checkpointRestore] disabling checkpoints for this task")
294-
})
277+
it.each(["delete", "edit"] as const)(
278+
"propagates restore failures for %s without rewinding",
279+
async (operation) => {
280+
const restoreError = new Error("Restore failed")
281+
mockCheckpointService.restoreCheckpoint.mockRejectedValueOnce(restoreError)
282+
const messagesBeforeRestore = [...mockTask.clineMessages]
283+
const historyBeforeRestore = [...mockTask.apiConversationHistory]
284+
285+
await expect(
286+
checkpointRestore(mockTask, {
287+
ts: 2,
288+
commitHash: "abc123",
289+
mode: "restore",
290+
operation,
291+
}),
292+
).rejects.toBe(restoreError)
293+
294+
expect(mockTask.enableCheckpoints).toBe(true)
295+
expect(mockTask.clineMessages).toEqual(messagesBeforeRestore)
296+
expect(mockTask.apiConversationHistory).toEqual(historyBeforeRestore)
297+
expect(mockTask.overwriteClineMessages).not.toHaveBeenCalled()
298+
expect(mockTask.overwriteApiConversationHistory).not.toHaveBeenCalled()
299+
expect(mockTask.say).not.toHaveBeenCalled()
300+
expect(mockProvider.postMessageToWebview).not.toHaveBeenCalled()
301+
expect(mockProvider.cancelTask).not.toHaveBeenCalled()
302+
expect(mockProvider.log).toHaveBeenCalledWith("[checkpointRestore] failed to restore checkpoint")
303+
},
304+
)
295305
})
296306

297307
describe("checkpointDiff", () => {
@@ -426,7 +436,7 @@ describe("Checkpoint functionality", () => {
426436
mockTask.checkpointService = undefined
427437
mockTask.checkpointServiceInitializing = false
428438

429-
getCheckpointService(mockTask)
439+
await getCheckpointService(mockTask)
430440

431441
const checkpointsModule = await import("../../../services/checkpoints")
432442
expect(vi.mocked(checkpointsModule.RepoPerTaskCheckpointService.create)).toHaveBeenCalledWith({
@@ -439,7 +449,7 @@ describe("Checkpoint functionality", () => {
439449

440450
it("should disable checkpoints if workspace path is not found", async () => {
441451
const pathModule = await import("../../../utils/path")
442-
vi.mocked(pathModule.getWorkspacePath).mockReturnValue(null as any)
452+
vi.mocked(pathModule.getWorkspacePath).mockReturnValue("")
443453

444454
mockTask.checkpointService = undefined
445455
mockTask.checkpointServiceInitializing = false

‎src/core/checkpoints/index.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ export async function checkpointRestore(
295295
// task flow and the communication between the webview and the
296296
// `Task` instance.
297297
provider?.cancelTask()
298-
} catch {
299-
provider?.log("[checkpointRestore] disabling checkpoints for this task")
300-
task.enableCheckpoints = false
298+
} catch (error) {
299+
provider?.log("[checkpointRestore] failed to restore checkpoint")
300+
throw error
301301
}
302302
}
303303

‎src/core/webview/ClineProvider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ export class ClineProvider
810810
/**
811811
* Clears a specific pending edit operation
812812
*/
813-
private clearPendingEditOperation(operationId: string): boolean {
813+
public clearPendingEditOperation(operationId: string): boolean {
814814
return this.pendingEditOperations.clear(operationId)
815815
}
816816

0 commit comments

Comments
 (0)