@devries-cameron20 / t3code-1 / commits / 7d8f6c9481
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): show a context handoff when a provider conversation can't be resumed (#28195)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 11, 2026 at 11:58 UTC 7d8f6c9481c369301a8e26b080014e789cc9d98c
6 files changed +116 -2
apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts
+3
@@ -54,6 +54,7 @@ import * as ProviderTurnStart from "./ProviderTurnStartService.ts";
54 import * as RunExecutionService from "./RunExecutionService.ts";
55 import * as ProviderUsageAnalytics from "../telemetry/ProviderUsageAnalytics.ts";
56 import * as RuntimePolicy from "./RuntimePolicy.ts";
57 +import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
58
59 const isDomainEvent = Schema.is(OrchestrationV2DomainEvent);
60
@@ -137,6 +138,7 @@ function startWithMissingWorktree(
138 Layer.mock(ContextHandoffService.ContextHandoffServiceV2)({}),
139 Layer.mock(EventSink.EventSinkV2)({ writeIfRunCurrent }),
140 IdAllocator.layer,
141 + Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}),
142 Layer.succeed(FileSystem.FileSystem, {
143 exists: () => Effect.succeed(false),
144 realPath: (target: string) =>
@@ -960,6 +962,7 @@ function makeLocalCommandHarness(input: {
962 }),
963 }),
964 IdAllocator.layer,
965 + Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}),
966 layerWorktreeLocation(input.worktreesDir),
967 input.fileSystem === undefined
968 ? FileSystem.layerNoop({ exists: () => Effect.succeed(true) })
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+63
@@ -57,6 +57,7 @@ import {
57 } from "@t3tools/provider-core/server/failure";
58 import * as RunExecutionService from "./RunExecutionService.ts";
59 import * as RuntimePolicy from "./RuntimePolicy.ts";
60 +import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
61 import {
62 isRestartNoteContinuation,
63 pendingRestartCancelledBackgroundWork,
@@ -145,6 +146,7 @@ export const layer: Layer.Layer<
146 | ProviderSessionManager.ProviderSessionManagerV2
147 | RunExecutionService.RunExecutionServiceV2
148 | RuntimePolicy.RuntimePolicyV2
149 + | TurnItemPositionStore.TurnItemPositionStoreV2
150 > = Layer.effect(
151 ProviderTurnStartServiceV2,
152 Effect.gen(function* () {
@@ -161,6 +163,7 @@ export const layer: Layer.Layer<
163 const providerSessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
164 const runExecution = yield* RunExecutionService.RunExecutionServiceV2;
165 const runtimePolicy = yield* RuntimePolicy.RuntimePolicyV2;
166 + const turnItemPositions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;
167 const isManagedWorktreePath = yield* makeIsManagedWorktreePath;
168
169 const resolveRecreateBase = Effect.fn("ProviderTurnStartService.resolveRecreateBase")(
@@ -1109,6 +1112,7 @@ export const layer: Layer.Layer<
1112 let resumedRetryHandoff: ReadonlyArray<OrchestrationV2ContextHandoff> | undefined;
1113 let resumedHandoffContext: ReturnType<typeof handoffContextForThread> | undefined;
1114 let portableForkFallback: OrchestrationV2ContextHandoff | undefined;
1115 + let recoveryTurnItem: OrchestrationV2TurnItem | undefined;
1116 const loadedProviderThread = yield* Effect.gen(function* () {
1117 if (nativeForkTransfer !== undefined) {
1118 const sourceProjection = yield* projectionStore.getThreadRecords(
@@ -1363,6 +1367,50 @@ export const layer: Layer.Layer<
1367 const { handoff, transferId, createdAt } = yield* recoveryHandoff;
1368 effectiveHandoffs = withRecoveryHandoff(handoff);
1369 resumeFellBack = true;
1370 + const previousRun = projection.runs.findLast(
1371 + (candidate) =>
1372 + candidate.providerThreadId === providerThread.id &&
1373 + candidate.ordinal < run.ordinal &&
1374 + candidate.startedAt !== null,
1375 + );
1376 + // Stable per run so a retried start reuses the reserved run-start position.
1377 + const handoffTurnItemId = idAllocator.derive.runSignalTurnItem({
1378 + runId,
1379 + signal: "resume-fallback-handoff",
1380 + });
1381 + const handoffTurnItem: OrchestrationV2TurnItem = {
1382 + id: handoffTurnItemId,
1383 + threadId: projection.thread.id,
1384 + runId,
1385 + nodeId: rootNode.id,
1386 + providerThreadId: providerThread.id,
1387 + providerTurnId: null,
1388 + nativeItemRef: null,
1389 + parentItemId: null,
1390 + // The summary is delivered ahead of this run's own handoffs and message.
1391 + ordinal: yield* turnItemPositions.allocate({
1392 + threadId: projection.thread.id,
1393 + turnItemId: handoffTurnItemId,
1394 + runId,
1395 + runOrdinal: run.ordinal,
1396 + placement: "run_start",
1397 + }),
1398 + status: "completed",
1399 + title: "Recovered context",
1400 + startedAt: createdAt,
1401 + completedAt: createdAt,
1402 + updatedAt: createdAt,
1403 + type: "handoff",
1404 + contextHandoffId: handoff.id,
1405 + fromProviderThreadIds: handoff.fromProviderThreadIds,
1406 + toProviderThreadId: handoff.toProviderThreadId,
1407 + fromProviderInstanceIds: [providerThread.providerInstanceId],
1408 + toProviderInstanceId: run.providerInstanceId,
1409 + fromModelSelections: previousRun === undefined ? [] : [previousRun.modelSelection],
1410 + toModel: run.modelSelection.model,
1411 + strategy: handoff.strategy,
1412 + summary: handoff.summaryText,
1413 + };
1414 yield* eventSink.write({
1415 events: [
1416 {
@@ -1402,6 +1450,7 @@ export const layer: Layer.Layer<
1450 },
1451 ],
1452 });
1453 + recoveryTurnItem = handoffTurnItem;
1454 return replacement;
1455 });
1456 if (loadedProviderThread === undefined) return;
@@ -1508,6 +1557,20 @@ export const layer: Layer.Layer<
1557 },
1558 },
1559 ]),
1560 + // Shown only once the replacement session commits with the running transition.
1561 + ...(recoveryTurnItem === undefined
1562 + ? []
1563 + : [
1564 + {
1565 + id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }),
1566 + type: "turn-item.updated" as const,
1567 + threadId: projection.thread.id,
1568 + runId: run.id,
1569 + providerInstanceId: run.providerInstanceId,
1570 + occurredAt: now,
1571 + payload: recoveryTurnItem,
1572 + },
1573 + ]),
1574 {
1575 id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }),
1576 type: "run.updated",
apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts
+21
@@ -56,3 +56,24 @@ it.effect("reuses an item's position without attempting another allocation inser
56 );
57 }).pipe(Effect.provide(layerTest)),
58 );
59 +
60 +it.effect("places a run-start item ahead of the run's items, then appends once taken", () =>
61 + Effect.gen(function* () {
62 + const positions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;
63 + const threadId = ThreadId.make("thread:run-start");
64 + const allocate = (id: string, placement?: "run_start") =>
65 + positions.allocate({
66 + threadId,
67 + turnItemId: TurnItemId.make(id),
68 + runId: RunId.make("run:2"),
69 + runOrdinal: 2,
70 + ...(placement === undefined ? {} : { placement }),
71 + });
72 + const message = yield* allocate("item:message");
73 + const recovery = yield* allocate("item:recovery", "run_start");
74 + assert.isBelow(recovery, message);
75 + assert.isAbove(recovery, 1_999_999);
76 + assert.equal(yield* allocate("item:retry-recovery", "run_start"), message + 1);
77 + assert.equal(yield* allocate("item:reply"), message + 2);
78 + }).pipe(Effect.provide(layerTest)),
79 +);
apps/server/src/orchestration-v2/TurnItemPositionStore.ts
+14 -2
@@ -22,6 +22,8 @@ export interface TurnItemPositionStoreV2Shape {
22 readonly turnItemId: TurnItemId;
23 readonly runId: RunId | null;
24 readonly runOrdinal?: number;
25 + // `run_start` takes the run's unused first slot, ahead of its items, and appends once it is taken.
26 + readonly placement?: "append" | "run_start";
27 }) => Effect.Effect<number, TurnItemPositionStoreError>;
28 readonly normalize: (
29 item: OrchestrationV2TurnItem,
@@ -42,7 +44,13 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
44 const allocate: TurnItemPositionStoreV2Shape["allocate"] =
45 /* This helper runs for individual orchestration events; the enclosing operation owns the span. */
46 Effect.fnUntraced(
45 - function* ({ threadId, turnItemId, runId, runOrdinal: suppliedRunOrdinal }) {
47 + function* ({
48 + threadId,
49 + turnItemId,
50 + runId,
51 + runOrdinal: suppliedRunOrdinal,
52 + placement = "append",
53 + }) {
54 const existing = yield* sql<{ readonly ordinal: number }>`
55 SELECT ordinal
56 FROM orchestration_v2_turn_item_positions
@@ -68,7 +76,11 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
76 SELECT
77 ${threadId},
78 ${turnItemId},
71 - COALESCE(MAX(ordinal), ${lowerBound}) + 1
79 + ${
80 + placement === "run_start"
81 + ? sql`CASE WHEN COALESCE(MIN(ordinal), ${upperBound}) > ${lowerBound} THEN ${lowerBound} ELSE MAX(ordinal) + 1 END`
82 + : sql`COALESCE(MAX(ordinal), ${lowerBound}) + 1`
83 + }
84 FROM orchestration_v2_turn_item_positions
85 WHERE thread_id = ${threadId}
86 AND ordinal >= ${lowerBound}
apps/server/src/orchestration-v2/runtimeLayer.ts
+1
@@ -173,6 +173,7 @@ const layerProviderTurnStartServiceProvided = ProviderTurnStartService.layer.pip
173 layerProviderAuthServiceProvided,
174 layerRunExecutionServiceProvided,
175 layerRuntimePolicyProvided,
176 + TurnItemPositionStore.layer,
177 ),
178 ),
179 );
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+14
@@ -1335,6 +1335,20 @@ describe("orchestration v2 provider switching", () => {
1335 assert.isFalse(turns[1]?.nativeThreadHasTurns);
1336 assert.equal(turns[2]?.nativeThreadId, turns[1]?.nativeThreadId);
1337 assert.isTrue(turns[2]?.nativeThreadHasTurns);
1338 +
1339 + const projection = yield* orchestrator.getThreadProjection(threadId);
1340 + const run2 = projection.runs[1]!;
1341 + const handoff = projection.contextHandoffs.find(
1342 + (candidate) => candidate.targetRunId === run2.id,
1343 + );
1344 + assert.deepEqual(
1345 + projection.turnItems
1346 + .filter((item) => item.runId === run2.id)
1347 + .toSorted((left, right) => left.ordinal - right.ordinal)
1348 + .slice(0, 2)
1349 + .map((item) => (item.type === "handoff" ? item.contextHandoffId : item.type)),
1350 + [handoff?.id, "user_message"],
1351 + );
1352 }).pipe(
1353 Effect.provide(
1354 ProviderReplayHarness.layerWithRegistry(