From 1accfbf4a0333521f8578d977693a0aaec8bab6d Mon Sep 17 00:00:00 2001 From: Rishin Georgy Anil Date: Sun, 19 Jul 2026 18:57:57 +0100 Subject: [PATCH] fix: recover checkpoint capture from corrupt refs --- .../src/checkpointing/CheckpointStore.test.ts | 13 ++- .../src/checkpointing/CheckpointStore.ts | 90 ++++++++++++++++++- .../orchestration/Layers/CheckpointReactor.ts | 21 ++++- 3 files changed, 115 insertions(+), 9 deletions(-) diff --git a/apps/server/src/checkpointing/CheckpointStore.test.ts b/apps/server/src/checkpointing/CheckpointStore.test.ts index 800a544932..92cdacd9be 100644 --- a/apps/server/src/checkpointing/CheckpointStore.test.ts +++ b/apps/server/src/checkpointing/CheckpointStore.test.ts @@ -301,7 +301,7 @@ it.layer(TestLayer)("CheckpointStore.layer", (it) => { }), ); - it.effect("surfaces corrupted shadow checkpoint refs instead of treating them as missing", () => + it.effect("surfaces corrupt shadow refs on lookup and repairs them during recapture", () => Effect.gen(function* () { const tmp = yield* makeTmpDir("checkpoint-store-shadow-corrupt-ref-test-"); const checkpointStore = yield* CheckpointStore.CheckpointStore; @@ -327,7 +327,16 @@ it.layer(TestLayer)("CheckpointStore.layer", (it) => { checkpointStore.hasCheckpointRef({ cwd: tmp, checkpointRef }), ); expect(error._tag).toBe("VcsProcessExitError"); - expect(error.message).toContain("git rev-parse"); + expect(error.message).toContain("git for-each-ref"); + + const sourcePath = NodePath.join(tmp, "source.txt"); + yield* writeTextFile(sourcePath, "recaptured\n"); + yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef }); + expect(yield* checkpointStore.hasCheckpointRef({ cwd: tmp, checkpointRef })).toBe(true); + + yield* writeTextFile(sourcePath, "changed again\n"); + expect(yield* checkpointStore.restoreCheckpoint({ cwd: tmp, checkpointRef })).toBe(true); + expect(yield* fileSystem.readFileString(sourcePath)).toBe("recaptured\n"); }), ); diff --git a/apps/server/src/checkpointing/CheckpointStore.ts b/apps/server/src/checkpointing/CheckpointStore.ts index f4c8423ad3..050b8b2c2a 100644 --- a/apps/server/src/checkpointing/CheckpointStore.ts +++ b/apps/server/src/checkpointing/CheckpointStore.ts @@ -24,6 +24,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import type { CheckpointStoreError } from "./Errors.ts"; +import { CHECKPOINT_REFS_PREFIX } from "./Utils.ts"; import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts"; import { ServerConfig } from "../config.ts"; import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; @@ -146,6 +147,27 @@ export const make = Effect.gen(function* () { readonly cwd: string; readonly checkpointRef: CheckpointRef; }) { + const refResult = yield* runShadowGit({ + operation: "CheckpointStore.shadow.resolveCheckpointRef", + cwd: input.cwd, + args: ["for-each-ref", "--format=%(objectname)", "--", input.checkpointRef], + allowNonZeroExit: true, + }); + const refStdout = refResult.stdout.trim(); + const refStderr = refResult.stderr.trim(); + if (refResult.exitCode !== 0 || refStderr.length > 0) { + return yield* new VcsProcessExitError({ + operation: "CheckpointStore.shadow.resolveCheckpointRef", + command: "git for-each-ref", + cwd: input.cwd, + exitCode: refResult.exitCode === 0 ? 1 : refResult.exitCode, + detail: refStderr || "Failed to resolve shadow checkpoint ref.", + }); + } + if (refStdout.length === 0) { + return null; + } + const result = yield* runShadowGit({ operation: "CheckpointStore.shadow.resolveCheckpointCommit", cwd: input.cwd, @@ -154,24 +176,84 @@ export const make = Effect.gen(function* () { }); const stdout = result.stdout.trim(); const stderr = result.stderr.trim(); - if (result.exitCode === 1 && stdout.length === 0 && stderr.length === 0) { - return null; - } if (result.exitCode !== 0) { return yield* new VcsProcessExitError({ operation: "CheckpointStore.shadow.resolveCheckpointCommit", command: "git rev-parse", cwd: input.cwd, exitCode: result.exitCode, - detail: stderr || "Failed to resolve shadow checkpoint commit.", + detail: stderr || `Checkpoint ref ${input.checkpointRef} does not resolve to a commit.`, }); } return stdout.length > 0 ? stdout : null; }); + const repairBrokenLooseShadowRef = Effect.fn("CheckpointStore.repairBrokenLooseShadowRef")( + function* (input: { + readonly cwd: string; + readonly checkpointRef: CheckpointRef; + readonly error: VcsProcessExitError; + }) { + const checkpointRef = String(input.checkpointRef); + const segments = checkpointRef.split("/"); + const isManagedCheckpointRef = + checkpointRef.startsWith(`${CHECKPOINT_REFS_PREFIX}/`) && + segments.every( + (segment) => + segment.length > 0 && segment !== "." && segment !== ".." && !segment.includes("\\"), + ); + const isBrokenRefError = + /(?:ignoring broken ref|bad ref|reference broken|bad object|does not resolve to a commit)/iu.test( + input.error.detail, + ) && input.error.detail.includes(checkpointRef); + if (!isManagedCheckpointRef || !isBrokenRefError) { + return yield* input.error; + } + + const looseRefPath = NodePath.join(shadowRepositoryPath(input.cwd), ...segments); + const looseRefExists = yield* fileSystem.exists(looseRefPath).pipe( + Effect.mapError( + (error) => + new VcsProcessExitError({ + operation: "CheckpointStore.shadow.repairBrokenLooseRef.exists", + command: "filesystem exists", + cwd: input.cwd, + exitCode: -1, + detail: error.message, + }), + ), + ); + if (!looseRefExists) { + return yield* input.error; + } + yield* fileSystem.remove(looseRefPath, { force: true }).pipe( + Effect.mapError( + (error) => + new VcsProcessExitError({ + operation: "CheckpointStore.shadow.repairBrokenLooseRef.remove", + command: "filesystem remove", + cwd: input.cwd, + exitCode: -1, + detail: error.message, + }), + ), + ); + yield* Effect.logWarning("removed corrupt loose shadow checkpoint ref before recapture", { + checkpointRef: input.checkpointRef, + cwd: input.cwd, + }); + }, + ); + const shadowCheckpoints: VcsCheckpointOps = { captureCheckpoint: Effect.fn("CheckpointStore.shadow.captureCheckpoint")(function* (input) { const gitDir = shadowRepositoryPath(input.cwd); + yield* resolveShadowCommit(input).pipe( + Effect.asVoid, + Effect.catchTag("VcsProcessExitError", (error) => + repairBrokenLooseShadowRef({ ...input, error }), + ), + ); const tempIndexPath = NodePath.join(gitDir, `t3-checkpoint-index-${NodeCrypto.randomUUID()}`); const env: NodeJS.ProcessEnv = { ...process.env, diff --git a/apps/server/src/orchestration/Layers/CheckpointReactor.ts b/apps/server/src/orchestration/Layers/CheckpointReactor.ts index 23a410795b..14df103aad 100644 --- a/apps/server/src/orchestration/Layers/CheckpointReactor.ts +++ b/apps/server/src/orchestration/Layers/CheckpointReactor.ts @@ -219,6 +219,21 @@ const make = Effect.gen(function* () { return cwd; }); + const hasCheckpointRefForCapture = Effect.fn("hasCheckpointRefForCapture")(function* (input: { + readonly cwd: string; + readonly checkpointRef: CheckpointRef; + }) { + return yield* checkpointStore.hasCheckpointRef(input).pipe( + Effect.catchTag("VcsProcessExitError", (error) => + Effect.logWarning("checkpoint ref probe failed; attempting recapture", { + checkpointRef: input.checkpointRef, + cwd: input.cwd, + detail: error.message, + }).pipe(Effect.as(false)), + ), + ); + }); + // Shared tail for both capture paths: creates the git checkpoint ref, diffs // it against the previous turn, then dispatches the domain events to update // the orchestration read model. @@ -242,7 +257,7 @@ const make = Effect.gen(function* () { const fromCheckpointRef = checkpointRefForThreadTurn(input.threadId, fromTurnCount); const targetCheckpointRef = checkpointRefForThreadTurn(input.threadId, input.turnCount); - const fromCheckpointExists = yield* checkpointStore.hasCheckpointRef({ + const fromCheckpointExists = yield* hasCheckpointRefForCapture({ cwd: input.cwd, checkpointRef: fromCheckpointRef, }); @@ -509,7 +524,7 @@ const make = Effect.gen(function* () { Math.max(0, countConversationTurns(thread.messages) - 1), ); const baselineCheckpointRef = checkpointRefForThreadTurn(thread.id, currentTurnCount); - const baselineExists = yield* checkpointStore.hasCheckpointRef({ + const baselineExists = yield* hasCheckpointRefForCapture({ cwd: checkpointCwd, checkpointRef: baselineCheckpointRef, }); @@ -595,7 +610,7 @@ const make = Effect.gen(function* () { : projectedConversationTurnCount, ); const baselineCheckpointRef = checkpointRefForThreadTurn(threadId, currentTurnCount); - const baselineExists = yield* checkpointStore.hasCheckpointRef({ + const baselineExists = yield* hasCheckpointRefForCapture({ cwd: checkpointCwd, checkpointRef: baselineCheckpointRef, });