Skip to content

Commit ab406db

Browse files
committed
chore: integrate upstream checkpoint changes
2 parents 8c3cf86 + e5a87e8 commit ab406db

23 files changed

Lines changed: 1111 additions & 279 deletions

‎apps/mobile/src/features/settings/SettingsRouteScreen.tsx‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
type ServerSettingsPatch,
4545
} from "@t3tools/contracts";
4646
import {
47+
filterSharedServerPatch,
4748
findSharedSettingsMismatches,
4849
pickSharedServerSettings,
4950
supportsSharedSettingsSync,
@@ -584,11 +585,13 @@ function AutoSettleSettingsRows() {
584585
const mismatches = findSharedSettingsMismatches({
585586
primaryEnvironmentId: reference.environmentId,
586587
primarySettings: referenceSettings,
588+
primaryCapabilities: reference.serverConfig?.environment.capabilities,
587589
environments: environments.map((environment) => ({
588590
environmentId: environment.environmentId,
589591
label: environment.label,
590592
syncEligible: supportsSharedSettingsSync(environment),
591593
settings: environment.serverConfig?.settings ?? null,
594+
capabilities: environment.serverConfig?.environment.capabilities,
592595
})),
593596
});
594597

@@ -652,11 +655,22 @@ function AutoSettleSettingsRows() {
652655
<Pressable
653656
accessibilityRole="button"
654657
onPress={() => {
655-
const patch = pickSharedServerSettings(referenceSettings);
658+
const patch = pickSharedServerSettings(
659+
referenceSettings,
660+
reference.serverConfig?.environment.capabilities,
661+
);
656662
for (const mismatch of mismatches) {
663+
const target = environments.find(
664+
(candidate) => candidate.environmentId === mismatch.environmentId,
665+
);
657666
void updateSettings({
658667
environmentId: mismatch.environmentId,
659-
input: { patch },
668+
input: {
669+
patch: filterSharedServerPatch(
670+
patch,
671+
target?.serverConfig?.environment.capabilities,
672+
),
673+
},
660674
});
661675
}
662676
}}

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ export const make = Effect.gen(function* () {
219219
pullRequests: true,
220220
threadSettlement: true,
221221
threadAutoSettlement: true,
222+
threadRestartContinuation: true,
222223
threadSnooze: true,
223224
environmentThemes: true,
224225
usageLimitSources: true,

‎apps/server/src/orchestration/Layers/CheckpointReactor.test.ts‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "@t3tools/contracts";
1313
import {
1414
CommandId,
15+
CheckpointRef,
1516
DEFAULT_PROVIDER_INTERACTION_MODE,
1617
EventId,
1718
MessageId,
@@ -667,6 +668,167 @@ describe("CheckpointReactor", () => {
667668
}),
668669
);
669670

671+
effectIt.effect.each(["turn.completed", "turn.aborted"] as const)(
672+
"captures every edit after a mid-turn diff update on %s",
673+
(terminalEventType) =>
674+
Effect.gen(function* () {
675+
const harness = yield* Effect.promise(() =>
676+
createHarness({ seedFilesystemCheckpoints: false }),
677+
);
678+
const threadId = ThreadId.make("thread-1");
679+
const turnId = asTurnId("turn-1");
680+
const assistantMessageId = MessageId.make("assistant:mid-turn");
681+
const createdAt = "2026-01-01T00:00:00.000Z";
682+
yield* harness.engine.dispatch({
683+
type: "thread.session.set",
684+
commandId: CommandId.make("cmd-mid-turn-running"),
685+
threadId,
686+
session: {
687+
threadId,
688+
status: "running",
689+
providerName: "codex",
690+
runtimeMode: "approval-required",
691+
activeTurnId: turnId,
692+
lastError: null,
693+
updatedAt: createdAt,
694+
},
695+
createdAt,
696+
});
697+
harness.provider.emit({
698+
type: "turn.started",
699+
eventId: EventId.make("evt-mid-turn-start"),
700+
provider: ProviderDriverKind.make("codex"),
701+
createdAt,
702+
threadId,
703+
turnId,
704+
});
705+
expect(yield* harness.nextReceipt).toMatchObject({
706+
type: "checkpoint.baseline.captured",
707+
});
708+
709+
NodeFS.writeFileSync(NodePath.join(harness.cwd, "early.ts"), "export const early = 1;\n");
710+
yield* harness.engine.dispatch({
711+
type: "thread.turn.diff.complete",
712+
commandId: CommandId.make("cmd-mid-turn-diff"),
713+
threadId,
714+
turnId,
715+
completedAt: createdAt,
716+
checkpointRef: CheckpointRef.make("provider-diff:mid-turn"),
717+
assistantMessageId,
718+
status: "missing",
719+
files: [],
720+
checkpointTurnCount: 1,
721+
createdAt,
722+
});
723+
yield* Effect.promise(harness.drain);
724+
725+
NodeFS.writeFileSync(NodePath.join(harness.cwd, "late.ts"), "export const late = 2;\n");
726+
yield* harness.engine.dispatch({
727+
type: "thread.session.set",
728+
commandId: CommandId.make("cmd-mid-turn-settled"),
729+
threadId,
730+
session: {
731+
threadId,
732+
status: terminalEventType === "turn.aborted" ? "interrupted" : "ready",
733+
providerName: "codex",
734+
runtimeMode: "approval-required",
735+
activeTurnId: null,
736+
lastError: null,
737+
updatedAt: createdAt,
738+
},
739+
createdAt,
740+
});
741+
harness.provider.emit({
742+
eventId: EventId.make("evt-mid-turn-complete"),
743+
provider: ProviderDriverKind.make("codex"),
744+
createdAt,
745+
threadId,
746+
turnId,
747+
...(terminalEventType === "turn.completed"
748+
? { type: "turn.completed", payload: { state: "completed" } }
749+
: { type: "turn.aborted", payload: { reason: "Interrupted by user." } }),
750+
});
751+
yield* Effect.promise(harness.drain);
752+
expect(gitRefExists(harness.cwd, checkpointRefForThreadTurn(threadId, 1))).toBe(true);
753+
expect(yield* harness.nextReceipt).toMatchObject({
754+
type: "checkpoint.diff.finalized",
755+
turnId,
756+
checkpointTurnCount: 1,
757+
});
758+
expect(yield* harness.nextReceipt).toMatchObject({
759+
type: "turn.processing.quiesced",
760+
turnId,
761+
});
762+
yield* Effect.promise(harness.drain);
763+
const thread = (yield* Effect.promise(harness.readModel)).threads.find(
764+
(entry) => entry.id === threadId,
765+
);
766+
expect(thread?.checkpoints).toHaveLength(1);
767+
expect(thread?.checkpoints[0]?.status).toBe("ready");
768+
expect(thread?.latestTurn?.state).toBe(
769+
terminalEventType === "turn.aborted" ? "interrupted" : "completed",
770+
);
771+
expect(thread?.checkpoints[0]?.assistantMessageId).toBe(assistantMessageId);
772+
expect(thread?.checkpoints[0]?.files.map((file) => file.path)).toEqual([
773+
"early.ts",
774+
"late.ts",
775+
]);
776+
expect(
777+
gitShowFileAtRef(harness.cwd, checkpointRefForThreadTurn(threadId, 1), "late.ts"),
778+
).toBe("export const late = 2;\n");
779+
780+
const followUpTurnId = asTurnId("turn-2");
781+
harness.provider.emit({
782+
type: "turn.started",
783+
eventId: EventId.make("evt-follow-up-start"),
784+
provider: ProviderDriverKind.make("codex"),
785+
createdAt,
786+
threadId,
787+
turnId: followUpTurnId,
788+
});
789+
harness.provider.emit({
790+
type: "turn.completed",
791+
eventId: EventId.make("evt-follow-up-complete"),
792+
provider: ProviderDriverKind.make("codex"),
793+
createdAt,
794+
threadId,
795+
turnId: followUpTurnId,
796+
payload: { state: "completed" },
797+
});
798+
expect(yield* harness.nextReceipt).toMatchObject({
799+
type: "checkpoint.diff.finalized",
800+
turnId: followUpTurnId,
801+
checkpointTurnCount: 2,
802+
});
803+
const followUp = (yield* Effect.promise(harness.readModel)).threads.find(
804+
(entry) => entry.id === threadId,
805+
);
806+
expect(
807+
followUp?.checkpoints.find((checkpoint) => checkpoint.turnId === followUpTurnId),
808+
).toMatchObject({ checkpointTurnCount: 2, files: [] });
809+
}),
810+
);
811+
812+
it("does not capture an aborted turn without a matching start or active session", async () => {
813+
const harness = await createHarness({ seedFilesystemCheckpoints: false });
814+
harness.provider.emit({
815+
type: "turn.aborted",
816+
eventId: EventId.make("evt-untracked-abort"),
817+
provider: ProviderDriverKind.make("codex"),
818+
createdAt: "2026-01-01T00:00:00.000Z",
819+
threadId: ThreadId.make("thread-1"),
820+
turnId: asTurnId("turn-untracked"),
821+
payload: { reason: "Interrupted before the turn started." },
822+
});
823+
await harness.drain();
824+
825+
const thread = (await harness.readModel()).threads.find((entry) => entry.id === "thread-1");
826+
expect(thread?.checkpoints).toEqual([]);
827+
expect(
828+
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.make("thread-1"), 1)),
829+
).toBe(false);
830+
});
831+
670832
it("refreshes local git status state on turn completion using the session cwd", async () => {
671833
const gitStatusRefreshCalls: string[] = [];
672834
const harness = await createHarness({

‎apps/server/src/orchestration/Layers/CheckpointReactor.ts‎

Lines changed: 16 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ const make = Effect.gen(function* () {
216216
return cwd;
217217
});
218218

219-
// Shared tail for both capture paths: creates the git checkpoint ref, diffs
220-
// it against the previous turn, then dispatches the domain events to update
221-
// the orchestration read model.
219+
// Capture the completed turn's files, then publish its summary and receipts.
222220
const captureAndDispatchCheckpoint = Effect.fn("captureAndDispatchCheckpoint")(function* (input: {
223221
readonly threadId: ThreadId;
224222
readonly turnId: TurnId;
@@ -353,9 +351,9 @@ const make = Effect.gen(function* () {
353351
});
354352
});
355353

356-
// Captures a real git checkpoint when a turn completes via a runtime event.
354+
// Capture the files left by a completed or interrupted turn.
357355
const captureCheckpointFromTurnCompletion = Effect.fn("captureCheckpointFromTurnCompletion")(
358-
function* (event: Extract<ProviderRuntimeEvent, { type: "turn.completed" }>) {
356+
function* (event: Extract<ProviderRuntimeEvent, { type: "turn.completed" | "turn.aborted" }>) {
359357
const turnId = toTurnId(event.turnId);
360358
if (!turnId) {
361359
return;
@@ -412,75 +410,16 @@ const make = Effect.gen(function* () {
412410
thread,
413411
cwd: checkpointCwd,
414412
turnCount: nextTurnCount,
415-
status: checkpointStatusFromRuntime(event.payload.state),
416-
assistantMessageId: undefined,
413+
status:
414+
event.type === "turn.aborted"
415+
? "ready"
416+
: checkpointStatusFromRuntime(event.payload.state),
417+
assistantMessageId: existingPlaceholder?.assistantMessageId ?? undefined,
417418
createdAt: event.createdAt,
418419
});
419420
},
420421
);
421422

422-
// Captures a real git checkpoint when a placeholder checkpoint (status "missing")
423-
// is detected via a domain event. This replaces the placeholder with a real
424-
// git-ref-based checkpoint.
425-
//
426-
// ProviderRuntimeIngestion creates placeholder checkpoints on turn.diff.updated
427-
// events from the Codex runtime. This handler fires when the corresponding
428-
// domain event arrives, allowing the reactor to capture the actual filesystem
429-
// state into a git ref and dispatch a replacement checkpoint.
430-
const captureCheckpointFromPlaceholder = Effect.fn("captureCheckpointFromPlaceholder")(function* (
431-
event: Extract<OrchestrationEvent, { type: "thread.turn-diff-completed" }>,
432-
) {
433-
const { threadId, turnId, checkpointTurnCount, status } = event.payload;
434-
435-
// Only replace placeholders; skip events from our own real captures.
436-
if (status !== "missing") {
437-
return;
438-
}
439-
440-
const thread = yield* resolveThreadDetail(threadId);
441-
if (!thread) {
442-
yield* Effect.logWarning("checkpoint capture from placeholder skipped: thread not found", {
443-
threadId,
444-
});
445-
return;
446-
}
447-
448-
// If a real checkpoint already exists for this turn, skip.
449-
if (
450-
thread.checkpoints.some(
451-
(checkpoint) => checkpoint.turnId === turnId && checkpoint.status !== "missing",
452-
)
453-
) {
454-
yield* Effect.logDebug(
455-
"checkpoint capture from placeholder skipped: real checkpoint already exists",
456-
{ threadId, turnId },
457-
);
458-
return;
459-
}
460-
461-
const projects = yield* resolveThreadProjects(thread.projectId);
462-
const checkpointCwd = yield* resolveCheckpointCwd({
463-
threadId,
464-
thread,
465-
projects,
466-
preferSessionRuntime: true,
467-
});
468-
if (!checkpointCwd) {
469-
return;
470-
}
471-
472-
yield* captureAndDispatchCheckpoint({
473-
threadId,
474-
turnId,
475-
thread,
476-
cwd: checkpointCwd,
477-
turnCount: checkpointTurnCount,
478-
status: "ready",
479-
assistantMessageId: event.payload.assistantMessageId ?? undefined,
480-
createdAt: event.payload.completedAt,
481-
});
482-
});
483-
484423
const ensurePreTurnBaselineFromTurnStart = Effect.fn("ensurePreTurnBaselineFromTurnStart")(
485424
function* (event: Extract<ProviderRuntimeEvent, { type: "turn.started" }>) {
486425
const turnId = toTurnId(event.turnId);
@@ -876,25 +815,6 @@ const make = Effect.gen(function* () {
876815
);
877816
return;
878817
}
879-
880-
// When ProviderRuntimeIngestion creates a placeholder checkpoint (status "missing")
881-
// from a turn.diff.updated runtime event, capture the real git checkpoint to
882-
// replace it. ProviderService broadcasts runtime events to each subscriber.
883-
// This domain-event path also captures checkpoints from turn diff updates.
884-
if (event.type === "thread.turn-diff-completed") {
885-
yield* captureCheckpointFromPlaceholder(event).pipe(
886-
Effect.catch((error) =>
887-
Effect.flatMap(nowIso, (createdAt) =>
888-
appendCaptureFailureActivity({
889-
threadId: event.payload.threadId,
890-
turnId: event.payload.turnId,
891-
detail: error.message,
892-
createdAt,
893-
}).pipe(Effect.catch(() => Effect.void)),
894-
),
895-
),
896-
);
897-
}
898818
});
899819

900820
const processRuntimeEvent = Effect.fn("processRuntimeEvent")(function* (
@@ -939,7 +859,13 @@ const make = Effect.gen(function* () {
939859
pending.delete(event.threadId);
940860
yield* pullRequests.refreshAfterTurn;
941861
}
942-
if (event.type === "turn.aborted") return;
862+
if (
863+
event.type === "turn.aborted" &&
864+
!isTrackedTurn &&
865+
!sameId(thread?.session?.activeTurnId, turnId)
866+
) {
867+
return;
868+
}
943869
yield* captureCheckpointFromTurnCompletion(event).pipe(
944870
Effect.catch((error) =>
945871
Effect.flatMap(nowIso, (createdAt) =>
@@ -987,8 +913,7 @@ const make = Effect.gen(function* () {
987913
if (
988914
event.type !== "thread.turn-start-requested" &&
989915
event.type !== "thread.message-sent" &&
990-
event.type !== "thread.checkpoint-revert-requested" &&
991-
event.type !== "thread.turn-diff-completed"
916+
event.type !== "thread.checkpoint-revert-requested"
992917
) {
993918
return Effect.void;
994919
}

‎apps/server/src/orchestration/Layers/ProjectionPipeline.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,10 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
15731573
yield* projectionTurnRepository.upsertByTurnId({
15741574
...existingTurn.value,
15751575
assistantMessageId: event.payload.assistantMessageId,
1576-
state: turnStillRunning ? existingTurn.value.state : nextState,
1576+
state:
1577+
turnStillRunning || existingTurn.value.state === "interrupted"
1578+
? existingTurn.value.state
1579+
: nextState,
15771580
checkpointTurnCount: event.payload.checkpointTurnCount,
15781581
checkpointRef: event.payload.checkpointRef,
15791582
checkpointStatus: event.payload.status,

0 commit comments

Comments
 (0)