Skip to content

Commit 9a430d6

Browse files
committed
feat(webview): add per-file restore-latest to change cards and correct rollback confirm copy (B3b, #1375)
1 parent 21f64eb commit 9a430d6

23 files changed

Lines changed: 476 additions & 16 deletions

File tree

‎packages/types/src/vscode-extension-host.ts‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,13 @@ export interface ExtensionMessage {
262262
/**
263263
* CheckpointRollbackResult
264264
*
265-
* Outcome of a change-card rollback request (B3b), posted back to the webview
266-
* that sent `checkpointRollbackFile` / `checkpointRollbackStep`. `cardTs`
267-
* echoes the change-card message timestamp so the requesting card can
268-
* correlate the result: per-file results carry `filePath`, per-step results
269-
* carry the per-file outcomes in `files`.
265+
* Outcome of a change-card restore request (B3b), posted back to the webview
266+
* that sent `checkpointRollbackFile` / `checkpointRollbackStep` /
267+
* `checkpointRestoreLatestFile`. `cardTs` echoes the change-card message
268+
* timestamp so the requesting card can correlate the result: per-file results
269+
* carry `filePath`, per-step results carry the per-file outcomes in
270+
* `files`, and `kind` tells a per-file result which control it belongs to
271+
* (absent = rollback, so results posted before `kind` existed still route).
270272
*/
271273
export interface CheckpointRollbackResult {
272274
/** The `ts` of the change_card message the result belongs to. */
@@ -277,6 +279,10 @@ export interface CheckpointRollbackResult {
277279
error?: string
278280
/** Per-step scope: the per-file outcomes. */
279281
files?: { filePath: string; success: boolean; error?: string }[]
282+
/** Per-file scope: which control the result belongs to. Absent = rollback. */
283+
kind?: "rollback" | "restore-latest"
284+
/** Per-file scope: true when a restore-latest found no recorded write and left the file as-is. */
285+
noOp?: boolean
280286
}
281287

282288
export interface OpenAiCodexRateLimitsMessage {
@@ -571,6 +577,7 @@ export interface WebviewMessage {
571577
| "checkpointRestore"
572578
| "checkpointRollbackFile"
573579
| "checkpointRollbackStep"
580+
| "checkpointRestoreLatestFile"
574581
| "completionCheckpointDiff"
575582
| "completionCheckpointRestore"
576583
| "deleteMcpServer"
@@ -839,6 +846,20 @@ export const checkpointRollbackStepPayloadSchema = z.object({
839846

840847
export type CheckpointRollbackStepPayload = z.infer<typeof checkpointRollbackStepPayloadSchema>
841848

849+
/**
850+
* Payload of the `checkpointRestoreLatestFile` webview message (B3b): restore
851+
* one change-card file to the latest recorded version of that file (the
852+
* content of its most recent write checkpoint — the forward direction to a
853+
* rollback).
854+
*/
855+
export const checkpointRestoreLatestFilePayloadSchema = z.object({
856+
/** The `ts` of the change_card message the request comes from (echoed on the result). */
857+
cardTs: z.number(),
858+
filePath: z.string(),
859+
})
860+
861+
export type CheckpointRestoreLatestFilePayload = z.infer<typeof checkpointRestoreLatestFilePayloadSchema>
862+
842863
export interface IndexingStatusPayload {
843864
state: "Standby" | "Indexing" | "Indexed" | "Error" | "Stopping"
844865
message: string
@@ -854,6 +875,7 @@ export type WebViewMessagePayload =
854875
| CheckpointRestorePayload
855876
| CheckpointRollbackFilePayload
856877
| CheckpointRollbackStepPayload
878+
| CheckpointRestoreLatestFilePayload
857879
| IndexingStatusPayload
858880
| IndexClearedPayload
859881
| UpdateTodoListPayload

‎src/core/webview/__tests__/webviewMessageHandler.rollback.spec.ts‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe, expect, it, vi, beforeEach } from "vitest"
44
import type { ExtensionMessage, WebviewMessage } from "@roo-code/types"
55

66
import { webviewMessageHandler } from "../webviewMessageHandler"
7-
import { rollbackFile, rollbackStep } from "../../checkpoints/rollback"
7+
import { restoreLatestFile, rollbackFile, rollbackStep } from "../../checkpoints/rollback"
88
import type { Task } from "../../task/Task"
99
import type { ClineProvider } from "../ClineProvider"
1010

@@ -14,6 +14,7 @@ import type { ClineProvider } from "../ClineProvider"
1414
vi.mock("../../checkpoints/rollback", () => ({
1515
rollbackFile: vi.fn(),
1616
rollbackStep: vi.fn(),
17+
restoreLatestFile: vi.fn(),
1718
}))
1819

1920
vi.mock("vscode", () => ({
@@ -197,10 +198,104 @@ describe("webviewMessageHandler - change card rollback", () => {
197198
type: "checkpointRollbackStep",
198199
payload: { cardTs: 1000, filePaths: [] },
199200
})
201+
await webviewMessageHandler(provider, {
202+
type: "checkpointRestoreLatestFile",
203+
payload: { cardTs: 1000 } as unknown as WebviewMessage["payload"],
204+
})
200205

201206
expect(rollbackFile).not.toHaveBeenCalled()
202207
expect(rollbackStep).not.toHaveBeenCalled()
208+
expect(restoreLatestFile).not.toHaveBeenCalled()
203209
expect(postMessageToWebview).not.toHaveBeenCalled()
204210
})
205211
})
212+
213+
describe("checkpointRestoreLatestFile", () => {
214+
it("restores the file to its latest recorded version and posts the success outcome", async () => {
215+
vi.mocked(restoreLatestFile).mockResolvedValueOnce({ filePath: "src/a.ts", success: true })
216+
217+
await webviewMessageHandler(provider, {
218+
type: "checkpointRestoreLatestFile",
219+
payload: { cardTs: 1000, filePath: "src/a.ts" },
220+
})
221+
222+
expect(restoreLatestFile).toHaveBeenCalledWith(mockTask, "src/a.ts")
223+
expect(postMessageToWebview).toHaveBeenCalledWith({
224+
type: "checkpointRollbackResult",
225+
checkpointRollbackResult: {
226+
cardTs: 1000,
227+
kind: "restore-latest",
228+
filePath: "src/a.ts",
229+
success: true,
230+
},
231+
})
232+
})
233+
234+
it("flags a no-op restore-latest so the card can report it", async () => {
235+
vi.mocked(restoreLatestFile).mockResolvedValueOnce({ filePath: "src/a.ts", success: true, noOp: true })
236+
237+
await webviewMessageHandler(provider, {
238+
type: "checkpointRestoreLatestFile",
239+
payload: { cardTs: 1000, filePath: "src/a.ts" },
240+
})
241+
242+
expect(postMessageToWebview).toHaveBeenCalledWith({
243+
type: "checkpointRollbackResult",
244+
checkpointRollbackResult: {
245+
cardTs: 1000,
246+
kind: "restore-latest",
247+
filePath: "src/a.ts",
248+
success: true,
249+
noOp: true,
250+
},
251+
})
252+
})
253+
254+
it("posts the error outcome when the restore fails", async () => {
255+
vi.mocked(restoreLatestFile).mockResolvedValueOnce({
256+
filePath: "src/a.ts",
257+
success: false,
258+
error: "Checkpoints are not enabled for this task",
259+
})
260+
261+
await webviewMessageHandler(provider, {
262+
type: "checkpointRestoreLatestFile",
263+
payload: { cardTs: 1000, filePath: "src/a.ts" },
264+
})
265+
266+
expect(postMessageToWebview).toHaveBeenCalledWith({
267+
type: "checkpointRollbackResult",
268+
checkpointRollbackResult: {
269+
cardTs: 1000,
270+
kind: "restore-latest",
271+
filePath: "src/a.ts",
272+
success: false,
273+
error: "Checkpoints are not enabled for this task",
274+
},
275+
})
276+
})
277+
278+
it("posts a correlated failure result when there is no current task", async () => {
279+
const emptyProvider = makeProvider(undefined)
280+
281+
await webviewMessageHandler(emptyProvider, {
282+
type: "checkpointRestoreLatestFile",
283+
payload: { cardTs: 1000, filePath: "src/a.ts" },
284+
})
285+
286+
expect(restoreLatestFile).not.toHaveBeenCalled()
287+
// The requesting card must clear its pending state, so the handler
288+
// posts a correlated failure instead of nothing.
289+
expect(postMessageToWebview).toHaveBeenCalledWith({
290+
type: "checkpointRollbackResult",
291+
checkpointRollbackResult: {
292+
cardTs: 1000,
293+
kind: "restore-latest",
294+
filePath: "src/a.ts",
295+
success: false,
296+
error: "No active task to restore from.",
297+
},
298+
})
299+
})
300+
})
206301
})

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ExperimentId,
2222
checkpointRollbackFilePayloadSchema,
2323
checkpointRollbackStepPayloadSchema,
24+
checkpointRestoreLatestFilePayloadSchema,
2425
checkoutDiffPayloadSchema,
2526
checkoutRestorePayloadSchema,
2627
getCompletionCheckpoint,
@@ -1679,6 +1680,49 @@ export const webviewMessageHandler = async (
16791680

16801681
break
16811682
}
1683+
case "checkpointRestoreLatestFile": {
1684+
// B3b: restore one change-card file to the latest recorded version of
1685+
// that file (the forward direction to a rollback) and report the
1686+
// outcome back to the requesting card (correlated by cardTs, kind
1687+
// "restore-latest").
1688+
const result = checkpointRestoreLatestFilePayloadSchema.safeParse(message.payload)
1689+
1690+
if (result.success) {
1691+
const task = provider.getCurrentTask()
1692+
1693+
if (task) {
1694+
// Lazy import (see the checkpointRollbackFile case above).
1695+
const { restoreLatestFile } = await import("../checkpoints/rollback")
1696+
const outcome = await restoreLatestFile(task, result.data.filePath)
1697+
await provider.postMessageToWebview({
1698+
type: "checkpointRollbackResult",
1699+
checkpointRollbackResult: {
1700+
cardTs: result.data.cardTs,
1701+
kind: "restore-latest",
1702+
filePath: outcome.filePath,
1703+
success: outcome.success,
1704+
...(outcome.noOp ? { noOp: true } : {}),
1705+
...(outcome.error ? { error: outcome.error } : {}),
1706+
},
1707+
})
1708+
} else {
1709+
// No active task: post the correlated failure so the requesting
1710+
// card can clear its pending state.
1711+
await provider.postMessageToWebview({
1712+
type: "checkpointRollbackResult",
1713+
checkpointRollbackResult: {
1714+
cardTs: result.data.cardTs,
1715+
kind: "restore-latest",
1716+
filePath: result.data.filePath,
1717+
success: false,
1718+
error: "No active task to restore from.",
1719+
},
1720+
})
1721+
}
1722+
}
1723+
1724+
break
1725+
}
16821726
case "completionCheckpointDiff": {
16831727
const currentCline = provider.getCurrentTask()
16841728
const checkpoint = currentCline ? resolveCompletionCheckpoint(currentCline) : undefined

0 commit comments

Comments
 (0)