fix(checkpoints): preserve valid run history

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-07-28 14:12:25 +02:00
1 parent f117a4aa43
commit e6796a7db6
5 files changed
+134 -9

No files matched your search

@@ -30,8 +30,8 @@ const secondRef = CheckpointRef.make("refs/t3/test/second");
function makeProjection(): OrchestrationV2ThreadProjection {
return {
runs: [
{ id: firstRunId, ordinal: 1 },
{ id: secondRunId, ordinal: 2 },
{ id: firstRunId, ordinal: 1, status: "completed" },
{ id: secondRunId, ordinal: 2, status: "completed" },
],
checkpointScopes: [
{ id: firstScopeId, runId: firstRunId, kind: "root_run", cwd: "/repo" },
@@ -40,6 +40,7 @@ function makeProjection(): OrchestrationV2ThreadProjection {
checkpoints: [
{
scopeId: secondScopeId,
runId: secondRunId,
appRunOrdinal: 2,
status: "ready",
ref: secondRef,
@@ -134,6 +135,44 @@ it.effect("preserves the typed unavailable-range error contract", () => {
}).pipe(Effect.provide(layer));
});
it.effect("excludes ready checkpoints from rolled-back runs", () => {
const projection = makeProjection();
const layer = makeLayer({
projection: Effect.succeed({
...projection,
runs: projection.runs.map((run) =>
run.id === secondRunId ? { ...run, status: "rolled_back" as const } : run,
),
checkpoints: [
{
...projection.checkpoints[0]!,
scopeId: firstScopeId,
runId: firstRunId,
appRunOrdinal: 1,
ref: CheckpointRef.make("refs/t3/test/first"),
},
...projection.checkpoints,
],
}),
});
return Effect.gen(function* () {
const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
const error = yield* query
.getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
.pipe(Effect.flip);
assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
assert.deepEqual(
{
requestedTurnCount: error.requestedTurnCount,
availableTurnCount: error.availableTurnCount,
},
{ requestedTurnCount: 2, availableTurnCount: 1 },
);
}).pipe(Effect.provide(layer));
});
it.effect("preserves the typed missing-baseline-ref error contract", () => {
const projection = makeProjection();
const layer = makeLayer({
@@ -109,8 +109,15 @@ export const make = Effect.gen(function* () {
),
Effect.withSpan("checkpoint.turnDiff.lookupProjection"),
);
const completedRunIds = new Set(
projection.runs.filter((run) => run.status === "completed").map((run) => run.id),
);
const readyCheckpoints = projection.checkpoints.filter(
(checkpoint) => checkpoint.status === "ready" && checkpoint.appRunOrdinal !== null,
(checkpoint) =>
checkpoint.status === "ready" &&
checkpoint.appRunOrdinal !== null &&
checkpoint.runId !== null &&
completedRunIds.has(checkpoint.runId),
);
const maxTurnCount = readyCheckpoints.reduce(
(max, checkpoint) => Math.max(max, checkpoint.appRunOrdinal ?? 0),
@@ -91,11 +91,18 @@ export const layer: Layer.Layer<
}
const capturedAt = yield* DateTime.now;
const baselineOrdinalWithinScope = Math.max(0, run.ordinal - 1);
const baselineCheckpoint = projection.checkpoints.some(
(candidate) => candidate.scopeId === scope.id && candidate.ordinalWithinScope === 0,
(candidate) =>
candidate.scopeId === scope.id &&
candidate.ordinalWithinScope === baselineOrdinalWithinScope &&
candidate.status === "ready",
)
? null
: yield* checkpoints.materializeBaselineCheckpoint({ scope });
: yield* checkpoints.materializeBaselineCheckpoint({
scope,
ordinalWithinScope: baselineOrdinalWithinScope,
});
const checkpoint = yield* checkpoints.capture({
scope,
runId: run.id,
@@ -0,0 +1,72 @@
import { assert, it, vi } from "@effect/vitest";
import {
CheckpointScopeId,
NodeId,
ProviderThreadId,
RunId,
ThreadId,
type OrchestrationV2CheckpointScope,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
import {
CheckpointServiceV2,
checkpointRefForScopeOrdinal,
layer as checkpointServiceLayer,
} from "./CheckpointService.ts";
import { layer as idAllocatorLayer } from "./IdAllocator.ts";
it.effect("materializes the captured baseline at the requested scope ordinal", () => {
const scope: OrchestrationV2CheckpointScope = {
id: CheckpointScopeId.make("checkpoint-scope:materialize-baseline"),
threadId: ThreadId.make("thread:materialize-baseline"),
runId: RunId.make("run:materialize-baseline:3"),
nodeId: NodeId.make("node:materialize-baseline:3"),
parentScopeId: null,
providerThreadId: ProviderThreadId.make("provider-thread:materialize-baseline"),
kind: "root_run",
ordinalWithinParent: 0,
advancesAppRunCount: true,
cwd: "/repo",
createdAt: DateTime.makeUnsafe("2026-07-28T00:00:00.000Z"),
};
const hasCheckpointRef = vi.fn((_input: CheckpointStore.RestoreCheckpointInput) =>
Effect.succeed(true),
);
const testLayer = checkpointServiceLayer.pipe(
Layer.provide(
Layer.mergeAll(
idAllocatorLayer,
Layer.mock(CheckpointStore.CheckpointStore)({
isGitRepository: () => Effect.succeed(true),
hasCheckpointRef,
}),
),
),
);
return Effect.gen(function* () {
const checkpoints = yield* CheckpointServiceV2;
const baseline = yield* checkpoints.materializeBaselineCheckpoint({
scope,
ordinalWithinScope: 2,
});
assert.equal(baseline.ordinalWithinScope, 2);
assert.equal(
baseline.ref,
checkpointRefForScopeOrdinal({
scopeId: scope.id,
ordinalWithinScope: 2,
}),
);
assert.equal(baseline.status, "ready");
assert.deepEqual(hasCheckpointRef.mock.calls[0]?.[0], {
cwd: scope.cwd,
checkpointRef: baseline.ref,
});
}).pipe(Effect.provide(testLayer));
});
@@ -133,6 +133,7 @@ export interface CheckpointServiceV2Shape {
}) => Effect.Effect<void, CheckpointServiceV2Error>;
readonly materializeBaselineCheckpoint: (input: {
readonly scope: OrchestrationV2CheckpointScope;
readonly ordinalWithinScope: number;
}) => Effect.Effect<OrchestrationV2Checkpoint, CheckpointServiceV2Error>;
readonly capture: (input: {
readonly scope: OrchestrationV2CheckpointScope;
@@ -318,14 +319,13 @@ export const layer: Layer.Layer<
withWorkspaceLock(
input.scope.cwd,
Effect.gen(function* () {
const ordinalWithinScope = 0;
const checkpointRef = checkpointRefForScopeOrdinal({
scopeId: input.scope.id,
ordinalWithinScope,
ordinalWithinScope: input.ordinalWithinScope,
});
const checkpointId = yield* checkpointIdForScopeOrdinal(idAllocator, {
scopeId: input.scope.id,
ordinalWithinScope,
ordinalWithinScope: input.ordinalWithinScope,
});
const checkpointable = yield* isGitCheckpointable(input.scope.cwd);
const available = checkpointable
@@ -340,7 +340,7 @@ export const layer: Layer.Layer<
runId: null,
nodeId: input.scope.nodeId,
parentCheckpointId: null,
ordinalWithinScope,
ordinalWithinScope: input.ordinalWithinScope,
appRunOrdinal: null,
ref: checkpointRef,
status: available ? "ready" : "missing",