fix: preserve corrupt checkpoint baselines

This commit is contained in:
Rishin Georgy Anil authored and Julius Marminge committed 2026-07-20 17:45:27 +02:00
1 parent 1accfbf4a0
commit c74caeb46e
3 files changed
+26 -95

No files matched your search

@@ -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" });
}),
);
@@ -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,
@@ -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,
});