Skip to content

Commit 86777f9

Browse files
author
Bertan Ari
committed
fix: reject unrecorded or malformed checkpoint hashes before any Git call
checkpointRestore and checkpointDiff now require a full 40-hex hash recorded as a checkpoint_saved message of the current task, covering delete, edit, checkpointRestore, completionCheckpointRestore and checkpointDiff. Adds tests for delete restores that succeed and then fail while saving or rehydrating.
1 parent 22cb84b commit 86777f9

21 files changed

Lines changed: 126 additions & 21 deletions

File tree

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

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
22
import { checkpointSave, checkpointRestore, checkpointDiff, getCheckpointService } from "../index"
33
import { MessageManager } from "../../message-manager"
4+
import { RepoPerTaskCheckpointService } from "../../../services/checkpoints"
45
import * as vscode from "vscode"
56

7+
const RESTORE_HASH = "a".repeat(40)
8+
const HASH_1 = "1".repeat(40)
9+
const HASH_2 = "2".repeat(40)
10+
611
// Mock vscode
712
vi.mock("vscode", () => ({
813
window: {
@@ -207,6 +212,7 @@ describe("Checkpoint functionality", () => {
207212
{ ts: 1, say: "user", text: "Message 1" },
208213
{ ts: 2, say: "assistant", text: "Message 2" },
209214
{ ts: 3, say: "user", text: "Message 3" },
215+
{ ts: 4, say: "checkpoint_saved", text: RESTORE_HASH },
210216
]
211217
mockTask.apiConversationHistory = [
212218
{ ts: 1, role: "user", content: [{ type: "text", text: "Message 1" }] },
@@ -218,12 +224,12 @@ describe("Checkpoint functionality", () => {
218224
it("should restore checkpoint for delete operation", async () => {
219225
await checkpointRestore(mockTask, {
220226
ts: 2,
221-
commitHash: "abc123",
227+
commitHash: RESTORE_HASH,
222228
mode: "restore",
223229
operation: "delete",
224230
})
225231

226-
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith("abc123")
232+
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith(RESTORE_HASH)
227233
expect(mockTask.overwriteApiConversationHistory).toHaveBeenCalledWith([
228234
{ ts: 1, role: "user", content: [{ type: "text", text: "Message 1" }] },
229235
])
@@ -234,12 +240,12 @@ describe("Checkpoint functionality", () => {
234240
it("should restore checkpoint for edit operation", async () => {
235241
await checkpointRestore(mockTask, {
236242
ts: 2,
237-
commitHash: "abc123",
243+
commitHash: RESTORE_HASH,
238244
mode: "restore",
239245
operation: "edit",
240246
})
241247

242-
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith("abc123")
248+
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith(RESTORE_HASH)
243249
expect(mockTask.overwriteApiConversationHistory).toHaveBeenCalledWith([
244250
{ ts: 1, role: "user", content: [{ type: "text", text: "Message 1" }] },
245251
])
@@ -254,11 +260,11 @@ describe("Checkpoint functionality", () => {
254260
it("should handle preview mode without modifying messages", async () => {
255261
await checkpointRestore(mockTask, {
256262
ts: 2,
257-
commitHash: "abc123",
263+
commitHash: RESTORE_HASH,
258264
mode: "preview",
259265
})
260266

261-
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith("abc123")
267+
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith(RESTORE_HASH)
262268
expect(mockTask.overwriteApiConversationHistory).not.toHaveBeenCalled()
263269
expect(mockTask.overwriteClineMessages).not.toHaveBeenCalled()
264270
expect(mockProvider.cancelTask).toHaveBeenCalled()
@@ -267,7 +273,7 @@ describe("Checkpoint functionality", () => {
267273
it("should handle missing message gracefully", async () => {
268274
await checkpointRestore(mockTask, {
269275
ts: 999, // Non-existent timestamp
270-
commitHash: "abc123",
276+
commitHash: RESTORE_HASH,
271277
mode: "restore",
272278
})
273279

@@ -285,7 +291,7 @@ describe("Checkpoint functionality", () => {
285291
await expect(
286292
checkpointRestore(mockTask, {
287293
ts: 2,
288-
commitHash: "abc123",
294+
commitHash: RESTORE_HASH,
289295
mode: "restore",
290296
operation,
291297
}),
@@ -302,15 +308,31 @@ describe("Checkpoint functionality", () => {
302308
expect(mockProvider.log).toHaveBeenCalledWith("[checkpointRestore] failed to restore checkpoint")
303309
},
304310
)
311+
312+
it.each([
313+
["a malformed hash", "HEAD~1"],
314+
["an abbreviated hash", "a".repeat(7)],
315+
["a well-formed but unrecorded hash", "b".repeat(40)],
316+
["a non-string", 42],
317+
])("rejects %s before any Git call", async (_label, commitHash) => {
318+
await expect(
319+
checkpointRestore(mockTask, { ts: 2, commitHash: commitHash as string, mode: "restore" }),
320+
).rejects.toThrow("common:errors.checkpoint_invalid_hash")
321+
322+
expect(RepoPerTaskCheckpointService.create).not.toHaveBeenCalled()
323+
expect(mockCheckpointService.restoreCheckpoint).not.toHaveBeenCalled()
324+
expect(mockTask.overwriteClineMessages).not.toHaveBeenCalled()
325+
expect(mockProvider.cancelTask).not.toHaveBeenCalled()
326+
})
305327
})
306328

307329
describe("checkpointDiff", () => {
308330
beforeEach(() => {
309331
mockTask.clineMessages = [
310332
{ ts: 1, say: "user", text: "Message 1" },
311-
{ ts: 2, say: "checkpoint_saved", text: "commit1" },
333+
{ ts: 2, say: "checkpoint_saved", text: HASH_1 },
312334
{ ts: 3, say: "user", text: "Message 2" },
313-
{ ts: 4, say: "checkpoint_saved", text: "commit2" },
335+
{ ts: 4, say: "checkpoint_saved", text: HASH_2 },
314336
]
315337
})
316338

@@ -325,12 +347,12 @@ describe("Checkpoint functionality", () => {
325347

326348
await checkpointDiff(mockTask, {
327349
ts: 4,
328-
commitHash: "commit2",
350+
commitHash: HASH_2,
329351
mode: "to-current",
330352
})
331353

332354
expect(mockCheckpointService.getDiff).toHaveBeenCalledWith({
333-
from: "commit2",
355+
from: HASH_2,
334356
to: undefined,
335357
})
336358
expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
@@ -350,13 +372,13 @@ describe("Checkpoint functionality", () => {
350372
mockCheckpointService.getDiff.mockResolvedValue(mockChanges)
351373
await checkpointDiff(mockTask, {
352374
ts: 4,
353-
commitHash: "commit1",
375+
commitHash: HASH_1,
354376
mode: "checkpoint",
355377
})
356378

357379
expect(mockCheckpointService.getDiff).toHaveBeenCalledWith({
358-
from: "commit1",
359-
to: "commit2",
380+
from: HASH_1,
381+
to: HASH_2,
360382
})
361383
expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
362384
"vscode.changes",
@@ -376,13 +398,13 @@ describe("Checkpoint functionality", () => {
376398

377399
await checkpointDiff(mockTask, {
378400
ts: 4,
379-
commitHash: "commit1",
401+
commitHash: HASH_1,
380402
mode: "checkpoint",
381403
})
382404

383405
expect(mockCheckpointService.getDiff).toHaveBeenCalledWith({
384-
from: "commit1", // Should find the next checkpoint
385-
to: "commit2",
406+
from: HASH_1, // Should find the next checkpoint
407+
to: HASH_2,
386408
})
387409
})
388410

@@ -391,20 +413,32 @@ describe("Checkpoint functionality", () => {
391413

392414
await checkpointDiff(mockTask, {
393415
ts: 4,
394-
commitHash: "commit2",
416+
commitHash: HASH_2,
395417
mode: "to-current",
396418
})
397419

398420
expect(vscode.window.showInformationMessage).toHaveBeenCalledWith("common:errors.checkpoint_no_changes")
399421
expect(vscode.commands.executeCommand).not.toHaveBeenCalled()
400422
})
401423

424+
it.each([
425+
["a malformed hash", "HEAD~1"],
426+
["a well-formed but unrecorded hash", "b".repeat(40)],
427+
["a non-string", 42],
428+
])("rejects %s before any Git call", async (_label, commitHash) => {
429+
await checkpointDiff(mockTask, { commitHash: commitHash as string, mode: "to-current" })
430+
431+
expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("common:errors.checkpoint_invalid_hash")
432+
expect(mockCheckpointService.getDiff).not.toHaveBeenCalled()
433+
expect(mockTask.enableCheckpoints).toBe(true)
434+
})
435+
402436
it("should disable checkpoints on error", async () => {
403437
mockCheckpointService.getDiff.mockRejectedValue(new Error("Diff failed"))
404438

405439
await checkpointDiff(mockTask, {
406440
ts: 4,
407-
commitHash: "commit2",
441+
commitHash: HASH_2,
408442
mode: "to-current",
409443
})
410444

‎src/core/checkpoints/index.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ export async function checkpointSave(task: Task, force = false, suppressMessage
227227
})
228228
}
229229

230+
// The shadow repo is created with `git init` defaults (SHA-1) and stores full hashes.
231+
const FULL_COMMIT_HASH = /^[0-9a-f]{40}$/
232+
233+
// The hash arrives from the webview, so it must name a checkpoint this task recorded before reaching Git.
234+
function assertRecordedCheckpoint(task: Task, commitHash: unknown): asserts commitHash is string {
235+
const isRecorded =
236+
typeof commitHash === "string" &&
237+
FULL_COMMIT_HASH.test(commitHash) &&
238+
task.clineMessages.some(({ say, text }) => say === "checkpoint_saved" && text === commitHash)
239+
240+
if (!isRecorded) {
241+
throw new Error(t("common:errors.checkpoint_invalid_hash"))
242+
}
243+
}
244+
230245
export type CheckpointRestoreOptions = {
231246
ts: number
232247
commitHash: string
@@ -238,6 +253,8 @@ export async function checkpointRestore(
238253
task: Task,
239254
{ ts, commitHash, mode, operation = "delete" }: CheckpointRestoreOptions,
240255
) {
256+
assertRecordedCheckpoint(task, commitHash)
257+
241258
const service = await getCheckpointService(task)
242259

243260
if (!service) {
@@ -315,6 +332,13 @@ export type CheckpointDiffOptions = {
315332
}
316333

317334
export async function checkpointDiff(task: Task, { commitHash, mode }: CheckpointDiffOptions) {
335+
try {
336+
assertRecordedCheckpoint(task, commitHash)
337+
} catch (error) {
338+
void vscode.window.showErrorMessage(error instanceof Error ? error.message : String(error))
339+
return
340+
}
341+
318342
const service = await getCheckpointService(task)
319343

320344
if (!service) {

‎src/core/webview/__tests__/checkpointRestoreHandler.spec.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ function createFixture() {
116116
checkpoint: { hash: "abc123" },
117117
operation: "delete",
118118
}
119-
return { task, taskDouble, provider, providerDouble, historyItem, store, config }
119+
const failure = new Error("Disk full")
120+
return { task, taskDouble, provider, providerDouble, historyItem, store, config, failure }
120121
}
121122

122123
describe("checkpointRestoreHandler", () => {
@@ -226,6 +227,34 @@ describe("checkpointRestoreHandler", () => {
226227
},
227228
)
228229

230+
it.each([
231+
{
232+
step: "saving messages",
233+
fail: (f: typeof fixture) => vi.mocked(saveTaskMessages).mockRejectedValueOnce(f.failure),
234+
},
235+
{
236+
step: "loading history",
237+
fail: (f: typeof fixture) => f.providerDouble.getTaskWithId.mockRejectedValueOnce(f.failure),
238+
},
239+
{
240+
step: "rehydrating the task",
241+
fail: (f: typeof fixture) => f.providerDouble.createTaskWithHistoryItem.mockRejectedValueOnce(f.failure),
242+
},
243+
])(
244+
"delete restore that succeeds then fails while $step surfaces the error without a restart prompt",
245+
async ({ fail }) => {
246+
fail(fixture)
247+
await expect(handleCheckpointRestoreOperation(fixture.config)).rejects.toBe(fixture.failure)
248+
expect(fixture.taskDouble.checkpointRestore).toHaveBeenCalledOnce()
249+
expect(vscode.window.showErrorMessage).toHaveBeenCalledExactlyOnceWith(
250+
"common:errors.checkpoint_restore_failed: Disk full",
251+
)
252+
expect(console.error).toHaveBeenCalledWith("Error in checkpoint restore (delete):", fixture.failure)
253+
expect(showWarningMessage).not.toHaveBeenCalled()
254+
expect(fixture.providerDouble.restartTaskIfCurrent).not.toHaveBeenCalled()
255+
},
256+
)
257+
229258
it("restarts the failed task when it is still current and the user accepts", async () => {
230259
fixture.taskDouble.checkpointRestore.mockRejectedValueOnce(new Error("Missing commit"))
231260
showWarningMessage.mockResolvedValueOnce("common:checkpoints.restart")

‎src/i18n/locales/ca/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/de/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/en/common.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"checkpoint_failed": "Failed to restore checkpoint.",
3131
"checkpoint_restore_failed": "Error during checkpoint restore: {{error}}",
3232
"checkpoint_missing": "Cannot restore checkpoint {{commitHash}}: this checkpoint no longer exists in the checkpoint repository. No files were changed.",
33+
"checkpoint_invalid_hash": "Cannot restore or compare checkpoint: the checkpoint reference is not a valid checkpoint of this task. No files were changed.",
3334
"git_not_installed": "Git is required for the checkpoints feature. Please install Git to enable checkpoints.",
3435
"checkpoint_no_first": "No first checkpoint to compare.",
3536
"checkpoint_no_previous": "No previous checkpoint to compare.",

‎src/i18n/locales/es/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/fr/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/hi/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/id/common.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)