Skip to content

Commit e619a65

Browse files
author
Bertan Ari
committed
fix: validate checkpoint before restoring workspace
1 parent f92cb93 commit e619a65

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎src/services/checkpoints/ShadowCheckpointService.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,17 @@ export abstract class ShadowCheckpointService extends EventEmitter {
377377
}
378378
}
379379

380+
// MUST run before workspace-mutating Git commands: discovering a missing commit
381+
// after git clean would leave untracked user files permanently deleted.
382+
private async commitExists(commitHash: string): Promise<boolean> {
383+
try {
384+
await this.git!.raw(["cat-file", "-e", `${commitHash}^{commit}`])
385+
return true
386+
} catch {
387+
return false
388+
}
389+
}
390+
380391
public async restoreCheckpoint(commitHash: string) {
381392
try {
382393
this.log(`[${this.constructor.name}#restoreCheckpoint] starting checkpoint restore`)
@@ -385,6 +396,12 @@ export abstract class ShadowCheckpointService extends EventEmitter {
385396
throw new Error("Shadow git repo not initialized")
386397
}
387398

399+
if (!(await this.commitExists(commitHash))) {
400+
throw new Error(
401+
`Cannot restore checkpoint ${commitHash}: this checkpoint no longer exists in the checkpoint repository. No files were changed.`,
402+
)
403+
}
404+
388405
const start = Date.now()
389406
await this.git.clean("f", ["-d", "-f"])
390407
await this.git.reset(["--hard", commitHash])

‎src/services/checkpoints/__tests__/ShadowCheckpointService.spec.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,50 @@ describe.each([[RepoPerTaskCheckpointService, "RepoPerTaskCheckpointService"]])(
320320
await expect(fs.readFile(untrackedFile, "utf-8")).rejects.toThrow()
321321
})
322322

323+
it("does not modify the workspace when restoring a missing commit", async () => {
324+
await fs.writeFile(testFile, "Committed content")
325+
const commit = await service.saveCheckpoint("Checkpoint before loss")
326+
expect(commit?.commit).toBeTruthy()
327+
328+
const untrackedFile = path.join(service.workspaceDir, "precious-untracked.txt")
329+
await fs.writeFile(untrackedFile, "Unsaved user work")
330+
await fs.writeFile(testFile, "Uncommitted edit")
331+
332+
const errorHandler = vi.fn()
333+
const restoreHandler = vi.fn()
334+
service.on("error", errorHandler)
335+
service.on("restore", restoreHandler)
336+
const checkpointsBeforeRestore = service.getCheckpoints()
337+
const missingCommitHash = "0".repeat(40)
338+
const expectedMessage = `Cannot restore checkpoint ${missingCommitHash}: this checkpoint no longer exists in the checkpoint repository. No files were changed.`
339+
340+
await expect(service.restoreCheckpoint(missingCommitHash)).rejects.toThrow(expectedMessage)
341+
342+
expect(await fs.readFile(untrackedFile, "utf-8")).toBe("Unsaved user work")
343+
expect(await fs.readFile(testFile, "utf-8")).toBe("Uncommitted edit")
344+
expect(service.getCheckpoints()).toEqual(checkpointsBeforeRestore)
345+
expect(errorHandler).toHaveBeenCalledExactlyOnceWith({
346+
type: "error",
347+
error: new Error(expectedMessage),
348+
})
349+
expect(restoreHandler).not.toHaveBeenCalled()
350+
})
351+
352+
it("still restores normally to a valid commit", async () => {
353+
await fs.writeFile(testFile, "Checkpointed content")
354+
const commit = await service.saveCheckpoint("Valid checkpoint")
355+
expect(commit?.commit).toBeTruthy()
356+
357+
await fs.writeFile(testFile, "Later edit")
358+
const laterTrackedFile = path.join(service.workspaceDir, "created-after.txt")
359+
await fs.writeFile(laterTrackedFile, "Created after checkpoint")
360+
await service.saveCheckpoint("After checkpoint")
361+
362+
await service.restoreCheckpoint(commit!.commit)
363+
expect(await fs.readFile(testFile, "utf-8")).toBe("Checkpointed content")
364+
await expect(fs.readFile(laterTrackedFile, "utf-8")).rejects.toThrow()
365+
})
366+
323367
it("does not create a checkpoint for ignored files", async () => {
324368
// Create a file that matches an ignored pattern (e.g., .log file).
325369
const ignoredFile = path.join(service.workspaceDir, "ignored.log")

0 commit comments

Comments
 (0)