Skip to content

Commit ab9b902

Browse files
committed
Overlay branch overlay/rewind-no-git on top of origin/main via Merge
# Conflicts: # apps/server/src/orchestration-v2/ProjectionStore.ts * apps/server/src/orchestration-v2/CheckpointCaptureService.ts * apps/server/src/orchestration-v2/CheckpointRollbackService.test.ts * apps/server/src/orchestration-v2/CheckpointRollbackService.ts * apps/server/src/orchestration-v2/Orchestrator.ts * apps/server/src/orchestration-v2/ProjectionStore.ts * apps/server/src/orchestration-v2/runtimeLayer.test.ts * apps/web/src/components/chat/ChatOverlayHosts.tsx * apps/web/src/session-logic.test.ts * apps/web/src/session-logic.ts * packages/client-runtime/src/operations/commands.ts * packages/contracts/src/orchestrationV2.ts • 6a266c1dbb Overlay branch overlay/rewind-no-git on top of origin/main via Merge |\ | • d25174b fix(checkpoints): rewind to earlier turns in folders without Git | • c4c1e7e Overlay branch overlay/rewind-no-git on top of origin/main via Merge | |\ | | • 88e481f Overlay branch overlay/rewind-no-git on top of origin/main via Merge | | |\ | | | • f0429aa Overlay branch overlay/rewind-no-git on top of origin/main via Merge | | | |\ | | | | • e46136a fix(checkpoints): rewind the conversation in folders without Git • | | | | becf853 fix(web): PDF search finds phrases that wrap across lines (pingdotgg#25702) • | | | | 68339a0 refactor(mobile): pull request list, filters and viewed files without synchronizing effects (pingdotgg#25614)
2 parents becf853 + d25174b commit ab9b902

15 files changed

Lines changed: 451 additions & 23 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
@@ -110,12 +110,18 @@ export const layer: Layer.Layer<
110110
scope,
111111
ordinalWithinScope: 0,
112112
});
113-
const baselineCheckpoint = hasReadyCheckpoint(baselineOrdinalWithinScope)
113+
const materializedBaseline = hasReadyCheckpoint(baselineOrdinalWithinScope)
114114
? null
115115
: yield* checkpoints.materializeBaselineCheckpoint({
116116
scope,
117117
ordinalWithinScope: baselineOrdinalWithinScope,
118118
});
119+
// Past the thread start, the baseline shares the previous run's checkpoint id. One without
120+
// a snapshot (no Git) would erase that run's identity, which conversation rewind targets.
121+
const baselineCheckpoint =
122+
materializedBaseline?.status === "ready" || baselineOrdinalWithinScope === 0
123+
? materializedBaseline
124+
: null;
119125
const checkpoint = yield* checkpoints.capture({
120126
scope,
121127
runId: run.id,

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

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,81 @@ it.effect.each([
346346
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
347347
);
348348

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

@@ -696,6 +772,37 @@ it.layer(ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMe
696772
}),
697773
);
698774

775+
it.effect("rewinds only the conversation to a later run whose checkpoint has no snapshot", () =>
776+
Effect.gen(function* () {
777+
const history = yield* seedRollbackHistory;
778+
const sql = yield* SqlClient.SqlClient;
779+
// Folders without Git record every checkpoint as missing.
780+
yield* sql`UPDATE orchestration_v2_projection_checkpoints
781+
SET status = 'missing', payload_json = json_set(payload_json, '$.status', 'missing')
782+
WHERE thread_id = ${history.threadId} AND status = 'ready'`;
783+
784+
yield* history.rollback(2, { restoreFiles: false });
785+
786+
assert.deepEqual(history.rollbackCounts, [4]);
787+
assert.deepEqual(history.restored, []);
788+
const runs = yield* sql<{ status: string }>`
789+
SELECT status FROM orchestration_v2_projection_runs
790+
WHERE thread_id = ${history.threadId} ORDER BY ordinal`;
791+
assert.deepEqual(
792+
runs.map((run) => run.status),
793+
[
794+
"completed",
795+
"completed",
796+
"rolled_back",
797+
"rolled_back",
798+
"rolled_back",
799+
"rolled_back",
800+
"rolled_back",
801+
],
802+
);
803+
}),
804+
);
805+
699806
it.effect(
700807
"rewinds stopped runs repeatedly without counting discarded provider turns again",
701808
() =>

‎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",

0 commit comments

Comments
 (0)