Skip to content

Commit e46136a

Browse files
committed
fix(checkpoints): rewind the conversation in folders without Git
Threads in folders without Git record every checkpoint as `missing`, and both the server and the web client only accepted `ready` checkpoints as rollback targets, so "Edit from here" never appeared and the conversation could not be rewound at all. A conversation-only rollback (restoreFiles=false) now accepts any checkpoint a rollback has not already passed (`canRollbackToCheckpoint` in contracts); restoring files still requires `ready`. The orchestrator, the rollback service, and the client-runtime fallback share that rule. The edit dialog drops "Revert files too" when the target has no file snapshot. Capture no longer lets a snapshot-less baseline overwrite the previous run's checkpoint. Past the thread start the baseline shares that checkpoint's id, so in folders without Git every turn erased its predecessor's run identity and left only the newest turn as a target. Rollback also skips deleting refs for `missing` checkpoints, which never wrote one and would otherwise run git outside a repository.
1 parent 6f75bcb commit e46136a

14 files changed

Lines changed: 432 additions & 34 deletions

‎apps/server/src/orchestration-v2/CheckpointCaptureService.test.ts‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,4 +719,197 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
719719
assert.deepEqual([...projected.readyCheckpointOrdinals].toSorted(), [0, 1]);
720720
}),
721721
);
722+
723+
it.effect("keeps the previous run's checkpoint when the workspace has no snapshots", () =>
724+
Effect.gen(function* () {
725+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
726+
const now = yield* DateTime.now;
727+
const noGitThreadId = ThreadId.make("thread:checkpoint-capture-no-git");
728+
const noGitRunId = RunId.make("run:checkpoint-capture-no-git-2");
729+
const noGitScopeId = CheckpointScopeId.make("scope:checkpoint-capture-no-git");
730+
const noGitRootNodeId = NodeId.make("node:checkpoint-capture-no-git-root");
731+
const noGitProviderThreadId = ProviderThreadId.make("provider-thread:checkpoint-no-git");
732+
const scope = {
733+
id: noGitScopeId,
734+
threadId: noGitThreadId,
735+
runId: noGitRunId,
736+
nodeId: noGitRootNodeId,
737+
parentScopeId: null,
738+
providerThreadId: noGitProviderThreadId,
739+
kind: "root_run" as const,
740+
ordinalWithinParent: 0,
741+
advancesAppRunCount: true,
742+
cwd: "/tmp",
743+
createdAt: now,
744+
};
745+
const missingCheckpoint = (input: {
746+
readonly ordinalWithinScope: number;
747+
readonly runId: RunId | null;
748+
}) => ({
749+
id: CheckpointId.make(`checkpoint:no-git-${input.ordinalWithinScope}`),
750+
threadId: noGitThreadId,
751+
scopeId: noGitScopeId,
752+
runId: input.runId,
753+
nodeId: noGitRootNodeId,
754+
parentCheckpointId: null,
755+
ordinalWithinScope: input.ordinalWithinScope,
756+
appRunOrdinal: input.runId === null ? null : input.ordinalWithinScope,
757+
ref: CheckpointRef.make(`checkpoint-ref:no-git-${input.ordinalWithinScope}`),
758+
status: "missing" as const,
759+
files: [],
760+
capturedAt: now,
761+
});
762+
yield* projectionStore.apply({
763+
id: EventId.make("event:checkpoint-capture-no-git:thread"),
764+
type: "thread.created",
765+
threadId: noGitThreadId,
766+
occurredAt: now,
767+
payload: {
768+
createdBy: "user",
769+
creationSource: "web",
770+
id: noGitThreadId,
771+
projectId,
772+
title: "Checkpoint capture without Git",
773+
providerInstanceId,
774+
modelSelection,
775+
runtimeMode: "full-access",
776+
interactionMode: "default",
777+
branch: null,
778+
worktreePath: null,
779+
activeProviderThreadId: null,
780+
lineage: {
781+
parentThreadId: null,
782+
relationshipToParent: null,
783+
rootThreadId: noGitThreadId,
784+
},
785+
forkedFrom: null,
786+
createdAt: now,
787+
updatedAt: now,
788+
archivedAt: null,
789+
settledOverride: null,
790+
settledAt: null,
791+
lastVisitedAt: null,
792+
deletedAt: null,
793+
},
794+
});
795+
yield* projectionStore.apply({
796+
id: EventId.make("event:checkpoint-capture-no-git:run"),
797+
type: "run.updated",
798+
threadId: noGitThreadId,
799+
runId: noGitRunId,
800+
nodeId: noGitRootNodeId,
801+
providerInstanceId,
802+
occurredAt: now,
803+
payload: {
804+
id: noGitRunId,
805+
threadId: noGitThreadId,
806+
ordinal: 2,
807+
providerInstanceId,
808+
modelSelection,
809+
providerThreadId: noGitProviderThreadId,
810+
userMessageId: MessageId.make("message:checkpoint-capture-no-git"),
811+
rootNodeId: noGitRootNodeId,
812+
activeAttemptId: null,
813+
status: "waiting",
814+
requestedAt: now,
815+
startedAt: now,
816+
completedAt: null,
817+
checkpointId: null,
818+
contextHandoffId: null,
819+
},
820+
});
821+
yield* projectionStore.apply({
822+
id: EventId.make("event:checkpoint-capture-no-git:node"),
823+
type: "node.updated",
824+
threadId: noGitThreadId,
825+
runId: noGitRunId,
826+
nodeId: noGitRootNodeId,
827+
providerInstanceId,
828+
occurredAt: now,
829+
payload: {
830+
id: noGitRootNodeId,
831+
threadId: noGitThreadId,
832+
runId: noGitRunId,
833+
parentNodeId: null,
834+
rootNodeId: noGitRootNodeId,
835+
kind: "root_turn",
836+
status: "waiting",
837+
countsForRun: true,
838+
providerThreadId: noGitProviderThreadId,
839+
providerTurnId: null,
840+
nativeItemRef: null,
841+
runtimeRequestId: null,
842+
checkpointScopeId: noGitScopeId,
843+
startedAt: now,
844+
completedAt: null,
845+
},
846+
});
847+
yield* projectionStore.apply({
848+
id: EventId.make("event:checkpoint-capture-no-git:provider-thread"),
849+
type: "provider-thread.updated",
850+
threadId: noGitThreadId,
851+
nodeId: noGitRootNodeId,
852+
driver,
853+
providerInstanceId,
854+
occurredAt: now,
855+
payload: {
856+
id: noGitProviderThreadId,
857+
driver,
858+
providerInstanceId,
859+
providerSessionId: null,
860+
appThreadId: noGitThreadId,
861+
ownerNodeId: noGitRootNodeId,
862+
nativeThreadRef: null,
863+
nativeConversationHeadRef: null,
864+
status: "idle",
865+
firstRunOrdinal: 1,
866+
lastRunOrdinal: 2,
867+
handoffIds: [],
868+
forkedFrom: null,
869+
createdAt: now,
870+
updatedAt: now,
871+
},
872+
});
873+
yield* projectionStore.apply({
874+
id: EventId.make("event:checkpoint-capture-no-git:scope"),
875+
type: "checkpoint-scope.created",
876+
threadId: noGitThreadId,
877+
runId: noGitRunId,
878+
nodeId: noGitRootNodeId,
879+
occurredAt: now,
880+
payload: scope,
881+
});
882+
883+
const committed = yield* Ref.make<ReadonlyArray<OrchestrationV2DomainEvent>>([]);
884+
const layerCapture = CheckpointCaptureService.layer.pipe(
885+
Layer.provide(
886+
Layer.mergeAll(
887+
IdAllocator.layer,
888+
Layer.mock(CheckpointService.CheckpointServiceV2)({
889+
materializeBaselineCheckpoint: ({ ordinalWithinScope }) =>
890+
Effect.succeed(missingCheckpoint({ ordinalWithinScope, runId: null })),
891+
capture: () =>
892+
Effect.succeed(missingCheckpoint({ ordinalWithinScope: 2, runId: noGitRunId })),
893+
}),
894+
Layer.mock(EventSink.EventSinkV2)({
895+
commitCommand: (input) =>
896+
Ref.set(committed, input.events).pipe(Effect.as({ committed: true } as never)),
897+
}),
898+
),
899+
),
900+
);
901+
902+
yield* CheckpointCaptureService.CheckpointCaptureServiceV2.pipe(
903+
Effect.flatMap((service) =>
904+
service.execute({ threadId: noGitThreadId, runId: noGitRunId, scopeId: noGitScopeId }),
905+
),
906+
Effect.provide(layerCapture),
907+
);
908+
909+
const capturedOrdinals = (yield* Ref.get(committed)).flatMap((event) =>
910+
event.type === "checkpoint.captured" ? [event.payload.ordinalWithinScope] : [],
911+
);
912+
assert.deepEqual(capturedOrdinals, [0, 2]);
913+
}),
914+
);
722915
});

‎apps/server/src/orchestration-v2/CheckpointCaptureService.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,18 @@ export const layer: Layer.Layer<
114114
scope,
115115
ordinalWithinScope: 0,
116116
});
117-
const baselineCheckpoint = hasReadyCheckpoint(baselineOrdinalWithinScope)
117+
const materializedBaseline = hasReadyCheckpoint(baselineOrdinalWithinScope)
118118
? null
119119
: yield* checkpoints.materializeBaselineCheckpoint({
120120
scope,
121121
ordinalWithinScope: baselineOrdinalWithinScope,
122122
});
123+
// Past the thread start, the baseline shares the previous run's checkpoint id. One without
124+
// a snapshot (no Git) would erase that run's identity, which conversation rewind targets.
125+
const baselineCheckpoint =
126+
materializedBaseline?.status === "ready" || baselineOrdinalWithinScope === 0
127+
? materializedBaseline
128+
: null;
123129
const checkpoint = yield* checkpoints.capture({
124130
scope,
125131
runId: run.id,

‎apps/server/src/orchestration-v2/CheckpointRollbackService.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,81 @@ it.effect.each([
347347
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
348348
);
349349

350+
it.effect.each([true, false])(
351+
"rewinds only the conversation to a checkpoint without a snapshot, restoreFiles=%s",
352+
(restoreFiles) => {
353+
const threadId = ThreadId.make("thread:rollback-no-snapshot");
354+
const providerThreadId = ProviderThreadId.make("provider-thread:rollback-no-snapshot");
355+
const providerSessionId = ProviderSessionId.make("provider-session:rollback-no-snapshot");
356+
const providerInstanceId = ProviderInstanceId.make("provider_rollback_no_snapshot");
357+
const checkpointId = CheckpointId.make("checkpoint:rollback-no-snapshot");
358+
const scopeId = CheckpointScopeId.make("checkpoint-scope:rollback-no-snapshot");
359+
const calls: string[] = [];
360+
const providerThread = { id: providerThreadId, providerSessionId, providerInstanceId };
361+
const projection = {
362+
thread: {
363+
id: threadId,
364+
worktreePath: null,
365+
branch: null,
366+
activeProviderThreadId: providerThreadId,
367+
modelSelection: { instanceId: providerInstanceId, model: "test-model" },
368+
},
369+
providerThread,
370+
providerSession: undefined,
371+
checkpoint: { id: checkpointId, scopeId, status: "missing", appRunOrdinal: 0 },
372+
scope: { id: scopeId, cwd: "/tmp" },
373+
runsToRollback: [],
374+
targetAttempt: undefined,
375+
rolledBackAttemptIds: [],
376+
staleCheckpoints: [],
377+
rootNodes: [],
378+
providerTurns: [],
379+
} as unknown as ProjectionStore.ProjectionCheckpointRollbackContext;
380+
const testLayer = layerCheckpointRollbackService.pipe(
381+
Layer.provide(
382+
Layer.mergeAll(
383+
ThreadCommandExecutor.layer,
384+
Layer.mock(CheckpointService.CheckpointServiceV2)({
385+
restore: () => Effect.die("a checkpoint without a snapshot has no files to restore"),
386+
}),
387+
Layer.mock(EventSink.EventSinkV2)({
388+
write: () => Effect.sync(() => (calls.push("projection"), [])),
389+
}),
390+
IdAllocator.layer,
391+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
392+
getThread: () => Effect.succeed(projection.thread),
393+
getCheckpointRollbackContext: () => Effect.succeed(projection),
394+
}),
395+
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
396+
open: () => Effect.sync(() => (calls.push("session"), {} as never)),
397+
}),
398+
Layer.mock(RuntimePolicy.RuntimePolicyV2)({
399+
resolve: () => Effect.succeed({ cwd: "/tmp" } as never),
400+
}),
401+
),
402+
),
403+
);
404+
405+
return Effect.gen(function* () {
406+
const service = yield* CheckpointRollbackService.CheckpointRollbackServiceV2;
407+
const rollback = service.execute({
408+
threadId,
409+
providerThreadId,
410+
checkpointId,
411+
scopeId,
412+
restoreFiles,
413+
});
414+
if (restoreFiles) {
415+
assert.equal((yield* Effect.flip(rollback)).reason, "rollback-target-invalid");
416+
assert.deepEqual(calls, []);
417+
} else {
418+
yield* rollback;
419+
assert.deepEqual(calls, ["session", "projection"]);
420+
}
421+
}).pipe(Effect.provide(testLayer));
422+
},
423+
);
424+
350425
const seedRollbackHistory = Effect.gen(function* () {
351426
const store = yield* ProjectionStore.ProjectionStoreV2;
352427
const now = yield* DateTime.now;

‎apps/server/src/orchestration-v2/CheckpointRollbackService.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
canRollbackToCheckpoint,
23
CheckpointId,
34
CheckpointScopeId,
45
latestProviderTurnForAttempt,
@@ -150,7 +151,7 @@ export const layer: Layer.Layer<
150151
checkpoint === undefined ||
151152
scope === undefined ||
152153
checkpoint.scopeId !== scope.id ||
153-
checkpoint.status !== "ready"
154+
!canRollbackToCheckpoint(checkpoint.status, input.restoreFiles !== false)
154155
) {
155156
return yield* new CheckpointRollbackExecutionError({
156157
reason: "rollback-target-invalid",

‎apps/server/src/orchestration-v2/CheckpointService.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
22
import { assert, it } from "@effect/vitest";
33
import {
4+
CheckpointId,
5+
CheckpointRef,
46
CheckpointScopeId,
57
NodeId,
68
ProviderThreadId,
79
RunId,
810
ThreadId,
11+
type OrchestrationV2Checkpoint,
912
type OrchestrationV2CheckpointScope,
1013
VcsProcessTimeoutError,
1114
VcsUnsupportedOperationError,
@@ -94,3 +97,57 @@ it.effect(
9497
}
9598
}),
9699
);
100+
101+
it.effect("deletes only the refs that stale checkpoints wrote", () =>
102+
Effect.gen(function* () {
103+
const scope: OrchestrationV2CheckpointScope = {
104+
id: CheckpointScopeId.make("checkpoint-scope:stale-refs"),
105+
threadId: ThreadId.make("thread:stale-refs"),
106+
runId: RunId.make("run:stale-refs:1"),
107+
nodeId: NodeId.make("node:stale-refs:1"),
108+
parentScopeId: null,
109+
providerThreadId: ProviderThreadId.make("provider-thread:stale-refs"),
110+
kind: "root_run",
111+
ordinalWithinParent: 0,
112+
advancesAppRunCount: true,
113+
cwd: "/tmp",
114+
createdAt: DateTime.makeUnsafe("2026-07-28T00:00:00.000Z"),
115+
};
116+
const checkpoint = (status: OrchestrationV2Checkpoint["status"]) => ({
117+
id: CheckpointId.make(`checkpoint:stale-refs:${status}`),
118+
threadId: scope.threadId,
119+
scopeId: scope.id,
120+
runId: scope.runId,
121+
nodeId: scope.nodeId!,
122+
parentCheckpointId: null,
123+
ordinalWithinScope: 1,
124+
appRunOrdinal: 1,
125+
ref: CheckpointRef.make(`refs/t3/stale-refs/${status}`),
126+
status,
127+
files: [],
128+
capturedAt: scope.createdAt,
129+
});
130+
const deleted: Array<ReadonlyArray<string>> = [];
131+
const layerTest = CheckpointService.layer.pipe(
132+
Layer.provide(
133+
Layer.mergeAll(
134+
IdAllocator.layer,
135+
Layer.mock(CheckpointStore.CheckpointStore)({
136+
deleteCheckpointRefs: ({ checkpointRefs }) =>
137+
Effect.sync(() => void deleted.push(checkpointRefs)),
138+
}),
139+
),
140+
),
141+
Layer.provideMerge(NodeCrypto.layer),
142+
);
143+
yield* Effect.gen(function* () {
144+
const checkpoints = yield* CheckpointService.CheckpointServiceV2;
145+
yield* checkpoints.deleteStaleRefs({ scope, checkpoints: [checkpoint("missing")] });
146+
yield* checkpoints.deleteStaleRefs({
147+
scope,
148+
checkpoints: [checkpoint("missing"), checkpoint("ready")],
149+
});
150+
}).pipe(Effect.provide(layerTest));
151+
assert.deepEqual(deleted, [["refs/t3/stale-refs/ready"]]);
152+
}),
153+
);

0 commit comments

Comments
 (0)