From c74caeb46e7f4b9938a0bcb213ba41e69b71c71a Mon Sep 17 00:00:00 2001 From: Rishin Georgy Anil Date: Sun, 19 Jul 2026 19:03:48 +0100 Subject: [PATCH] fix: preserve corrupt checkpoint baselines --- .../src/checkpointing/CheckpointStore.test.ts | 17 +++-- .../src/checkpointing/CheckpointStore.ts | 66 +------------------ .../orchestration/Layers/CheckpointReactor.ts | 38 +++++------ 3 files changed, 26 insertions(+), 95 deletions(-) diff --git a/apps/server/src/checkpointing/CheckpointStore.test.ts b/apps/server/src/checkpointing/CheckpointStore.test.ts index 92cdacd9be..d089fcbec6 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 corrupt shadow refs on lookup and repairs them during recapture", () => + it.effect("surfaces corrupted shadow checkpoint refs instead of treating them as missing", () => Effect.gen(function* () { const tmp = yield* makeTmpDir("checkpoint-store-shadow-corrupt-ref-test-"); const checkpointStore = yield* CheckpointStore.CheckpointStore; @@ -329,14 +329,13 @@ it.layer(TestLayer)("CheckpointStore.layer", (it) => { expect(error._tag).toBe("VcsProcessExitError"); 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"); + const captureError = yield* Effect.flip( + checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef }), + ); + expect(captureError._tag).toBe("VcsProcessExitError"); + expect( + yield* Effect.flip(checkpointStore.hasCheckpointRef({ cwd: tmp, checkpointRef })), + ).toMatchObject({ _tag: "VcsProcessExitError" }); }), ); diff --git a/apps/server/src/checkpointing/CheckpointStore.ts b/apps/server/src/checkpointing/CheckpointStore.ts index 050b8b2c2a..5f0af97419 100644 --- a/apps/server/src/checkpointing/CheckpointStore.ts +++ b/apps/server/src/checkpointing/CheckpointStore.ts @@ -24,7 +24,6 @@ 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"; @@ -155,7 +154,7 @@ export const make = Effect.gen(function* () { }); const refStdout = refResult.stdout.trim(); const refStderr = refResult.stderr.trim(); - if (refResult.exitCode !== 0 || refStderr.length > 0) { + if (refResult.exitCode !== 0 || (refStdout.length === 0 && refStderr.length > 0)) { return yield* new VcsProcessExitError({ operation: "CheckpointStore.shadow.resolveCheckpointRef", command: "git for-each-ref", @@ -188,72 +187,9 @@ export const make = Effect.gen(function* () { 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 14df103aad..7d85571af2 100644 --- a/apps/server/src/orchestration/Layers/CheckpointReactor.ts +++ b/apps/server/src/orchestration/Layers/CheckpointReactor.ts @@ -219,21 +219,6 @@ 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. @@ -257,10 +242,21 @@ const make = Effect.gen(function* () { const fromCheckpointRef = checkpointRefForThreadTurn(input.threadId, fromTurnCount); const targetCheckpointRef = checkpointRefForThreadTurn(input.threadId, input.turnCount); - const fromCheckpointExists = yield* hasCheckpointRefForCapture({ - cwd: input.cwd, - checkpointRef: fromCheckpointRef, - }); + const fromCheckpointExists = yield* checkpointStore + .hasCheckpointRef({ + cwd: input.cwd, + checkpointRef: fromCheckpointRef, + }) + .pipe( + Effect.catchTags({ + VcsProcessExitError: (error) => + Effect.logWarning("previous checkpoint ref probe failed", { + checkpointRef: fromCheckpointRef, + cwd: input.cwd, + detail: error.message, + }).pipe(Effect.as(false)), + }), + ); if (!fromCheckpointExists) { yield* Effect.logWarning("checkpoint capture missing pre-turn baseline", { threadId: input.threadId, @@ -524,7 +520,7 @@ const make = Effect.gen(function* () { Math.max(0, countConversationTurns(thread.messages) - 1), ); const baselineCheckpointRef = checkpointRefForThreadTurn(thread.id, currentTurnCount); - const baselineExists = yield* hasCheckpointRefForCapture({ + const baselineExists = yield* checkpointStore.hasCheckpointRef({ cwd: checkpointCwd, checkpointRef: baselineCheckpointRef, }); @@ -610,7 +606,7 @@ const make = Effect.gen(function* () { : projectedConversationTurnCount, ); const baselineCheckpointRef = checkpointRefForThreadTurn(threadId, currentTurnCount); - const baselineExists = yield* hasCheckpointRefForCapture({ + const baselineExists = yield* checkpointStore.hasCheckpointRef({ cwd: checkpointCwd, checkpointRef: baselineCheckpointRef, });