diff --git a/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts b/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts index 2873070fdb..718c23f95a 100644 --- a/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts +++ b/apps/server/src/checkpointing/CheckpointDiffQuery.test.ts @@ -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({ diff --git a/apps/server/src/checkpointing/CheckpointDiffQuery.ts b/apps/server/src/checkpointing/CheckpointDiffQuery.ts index 9950f6c8e0..e4c34528a0 100644 --- a/apps/server/src/checkpointing/CheckpointDiffQuery.ts +++ b/apps/server/src/checkpointing/CheckpointDiffQuery.ts @@ -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), diff --git a/apps/server/src/orchestration-v2/CheckpointCaptureService.ts b/apps/server/src/orchestration-v2/CheckpointCaptureService.ts index 7666b72109..a25c1b607c 100644 --- a/apps/server/src/orchestration-v2/CheckpointCaptureService.ts +++ b/apps/server/src/orchestration-v2/CheckpointCaptureService.ts @@ -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, diff --git a/apps/server/src/orchestration-v2/CheckpointService.test.ts b/apps/server/src/orchestration-v2/CheckpointService.test.ts new file mode 100644 index 0000000000..1edfe541e3 --- /dev/null +++ b/apps/server/src/orchestration-v2/CheckpointService.test.ts @@ -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)); +}); diff --git a/apps/server/src/orchestration-v2/CheckpointService.ts b/apps/server/src/orchestration-v2/CheckpointService.ts index e11caa2143..8021141830 100644 --- a/apps/server/src/orchestration-v2/CheckpointService.ts +++ b/apps/server/src/orchestration-v2/CheckpointService.ts @@ -133,6 +133,7 @@ export interface CheckpointServiceV2Shape { }) => Effect.Effect; readonly materializeBaselineCheckpoint: (input: { readonly scope: OrchestrationV2CheckpointScope; + readonly ordinalWithinScope: number; }) => Effect.Effect; 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",