Skip to content

Commit 6ee6d04

Browse files
committed
fix(checkpoints): surface unreadable checkpoint trees instead of deleting live files
fileExistsInCommit read any cat-file -e failure as "file absent", so a shadow repo whose commit object resolves but whose tree object is corrupt or missing routed restoreFile into the delete branch and rm'd a live file the checkpoint does contain. Existence now uses ls-tree: an empty successful result is genuine absence, while a command failure propagates and fails the restore loudly. The :(literal) pathspec magic keeps paths containing glob characters exact (also applied to the mode check). Regression test corrupts the checkpoint tree object and pins both the rejection and the surviving file. Review feedback on PR 1412; owned by PR 1410's service and byte-copied to the sibling unit carrying the identical file.
1 parent e4fe6ba commit 6ee6d04

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

‎src/services/checkpoints/ShadowCheckpointService.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ export abstract class ShadowCheckpointService extends EventEmitter {
505505
// The checkpoint entry can be a tree or a symlink (whose object
506506
// is also a blob); only regular-file modes may be restored through
507507
// this single-file path. ls-tree reports the mode, unlike cat-file.
508-
const entry = await this.git!.raw(["ls-tree", commitHash, "--", gitPath])
508+
const entry = await this.git!.raw(["ls-tree", commitHash, "--", `:(literal)${gitPath}`])
509509
const mode = entry.trim() ? entry.split("\t")[0].split(" ")[0] : ""
510510
if (mode !== "100644" && mode !== "100755") {
511511
throw new Error(`restoreFile target is not a regular file in the checkpoint: ${filePath}`)
@@ -544,12 +544,15 @@ export abstract class ShadowCheckpointService extends EventEmitter {
544544
} catch {
545545
throw new Error(`Checkpoint unavailable: ${commitHash}`)
546546
}
547-
try {
548-
await this.git!.raw(["cat-file", "-e", `${commitHash}:${filePath}`])
549-
return true
550-
} catch {
551-
return false
552-
}
547+
// Existence must distinguish "absent" from "unreadable": ls-tree exits 0
548+
// with empty output only when the path is genuinely absent from the
549+
// tree, while a corrupt or missing tree object makes it fail - and that
550+
// failure must propagate rather than be misread as absence, which would
551+
// route restoreFile into the delete branch and rm a live file the
552+
// checkpoint does contain. The :(literal) magic keeps paths containing
553+
// glob characters exact.
554+
const entry = await this.git!.raw(["ls-tree", commitHash, "--", `:(literal)${filePath}`])
555+
return entry.trim().length > 0
553556
}
554557

555558
public async getDiff({ from, to }: { from?: string; to?: string }): Promise<CheckpointDiff[]> {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,26 @@ describe.each([[RepoPerTaskCheckpointService, "RepoPerTaskCheckpointService"]])(
260260
expect(await fs.readFile(testFile, "utf-8")).toBe("Ahoy, world!")
261261
})
262262

263+
it("does not delete a live file when the checkpoint tree object is unreadable", async () => {
264+
// The commit object can resolve while its tree is corrupt or missing
265+
// (partial shadow-repo damage). An existence lookup that swallows the
266+
// read failure would report "absent" and delete a live file the
267+
// checkpoint does contain; the failure must propagate instead.
268+
await fs.writeFile(testFile, "Ahoy, world!")
269+
const commit1 = await service.saveCheckpoint("First checkpoint")
270+
expect(commit1?.commit).toBeTruthy()
271+
272+
const git = service["git"]!
273+
const gitDir = (await git.raw(["rev-parse", "--absolute-git-dir"])).trim()
274+
const treeOid = (await git.raw(["show", "-s", "--format=%T", commit1!.commit])).trim()
275+
await fs.rm(path.join(gitDir, "objects", treeOid.slice(0, 2), treeOid.slice(2)), { force: true })
276+
277+
await expect(service.restoreFile(commit1!.commit, "test.txt")).rejects.toThrow()
278+
279+
expect(await fileExistsAtPath(testFile)).toBe(true)
280+
expect(await fs.readFile(testFile, "utf-8")).toBe("Ahoy, world!")
281+
})
282+
263283
it("rejects a target that escapes the workspace through a symlinked ancestor", async () => {
264284
// A lexical prefix check passes for a path that goes through a
265285
// symlink inside the workspace pointing outside it; the resolved

‎webview-ui/src/i18n/locales/ru/chat.json‎

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

0 commit comments

Comments
 (0)