Skip to content

Commit 7d8f6c9

Browse files
fix(server): show a context handoff when a provider conversation can't be resumed (pingdotgg#28195)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f796df8 commit 7d8f6c9

6 files changed

Lines changed: 116 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import * as ProviderTurnStart from "./ProviderTurnStartService.ts";
5454
import * as RunExecutionService from "./RunExecutionService.ts";
5555
import * as ProviderUsageAnalytics from "../telemetry/ProviderUsageAnalytics.ts";
5656
import * as RuntimePolicy from "./RuntimePolicy.ts";
57+
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
5758

5859
const isDomainEvent = Schema.is(OrchestrationV2DomainEvent);
5960

@@ -137,6 +138,7 @@ function startWithMissingWorktree(
137138
Layer.mock(ContextHandoffService.ContextHandoffServiceV2)({}),
138139
Layer.mock(EventSink.EventSinkV2)({ writeIfRunCurrent }),
139140
IdAllocator.layer,
141+
Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}),
140142
Layer.succeed(FileSystem.FileSystem, {
141143
exists: () => Effect.succeed(false),
142144
realPath: (target: string) =>
@@ -960,6 +962,7 @@ function makeLocalCommandHarness(input: {
960962
}),
961963
}),
962964
IdAllocator.layer,
965+
Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}),
963966
layerWorktreeLocation(input.worktreesDir),
964967
input.fileSystem === undefined
965968
? FileSystem.layerNoop({ exists: () => Effect.succeed(true) })

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
} from "@t3tools/provider-core/server/failure";
5858
import * as RunExecutionService from "./RunExecutionService.ts";
5959
import * as RuntimePolicy from "./RuntimePolicy.ts";
60+
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
6061
import {
6162
isRestartNoteContinuation,
6263
pendingRestartCancelledBackgroundWork,
@@ -145,6 +146,7 @@ export const layer: Layer.Layer<
145146
| ProviderSessionManager.ProviderSessionManagerV2
146147
| RunExecutionService.RunExecutionServiceV2
147148
| RuntimePolicy.RuntimePolicyV2
149+
| TurnItemPositionStore.TurnItemPositionStoreV2
148150
> = Layer.effect(
149151
ProviderTurnStartServiceV2,
150152
Effect.gen(function* () {
@@ -161,6 +163,7 @@ export const layer: Layer.Layer<
161163
const providerSessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
162164
const runExecution = yield* RunExecutionService.RunExecutionServiceV2;
163165
const runtimePolicy = yield* RuntimePolicy.RuntimePolicyV2;
166+
const turnItemPositions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;
164167
const isManagedWorktreePath = yield* makeIsManagedWorktreePath;
165168

166169
const resolveRecreateBase = Effect.fn("ProviderTurnStartService.resolveRecreateBase")(
@@ -1109,6 +1112,7 @@ export const layer: Layer.Layer<
11091112
let resumedRetryHandoff: ReadonlyArray<OrchestrationV2ContextHandoff> | undefined;
11101113
let resumedHandoffContext: ReturnType<typeof handoffContextForThread> | undefined;
11111114
let portableForkFallback: OrchestrationV2ContextHandoff | undefined;
1115+
let recoveryTurnItem: OrchestrationV2TurnItem | undefined;
11121116
const loadedProviderThread = yield* Effect.gen(function* () {
11131117
if (nativeForkTransfer !== undefined) {
11141118
const sourceProjection = yield* projectionStore.getThreadRecords(
@@ -1363,6 +1367,50 @@ export const layer: Layer.Layer<
13631367
const { handoff, transferId, createdAt } = yield* recoveryHandoff;
13641368
effectiveHandoffs = withRecoveryHandoff(handoff);
13651369
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+
};
13661414
yield* eventSink.write({
13671415
events: [
13681416
{
@@ -1402,6 +1450,7 @@ export const layer: Layer.Layer<
14021450
},
14031451
],
14041452
});
1453+
recoveryTurnItem = handoffTurnItem;
14051454
return replacement;
14061455
});
14071456
if (loadedProviderThread === undefined) return;
@@ -1508,6 +1557,20 @@ export const layer: Layer.Layer<
15081557
},
15091558
},
15101559
]),
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+
]),
15111574
{
15121575
id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }),
15131576
type: "run.updated",

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,24 @@ it.effect("reuses an item's position without attempting another allocation inser
5656
);
5757
}).pipe(Effect.provide(layerTest)),
5858
);
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‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export interface TurnItemPositionStoreV2Shape {
2222
readonly turnItemId: TurnItemId;
2323
readonly runId: RunId | null;
2424
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";
2527
}) => Effect.Effect<number, TurnItemPositionStoreError>;
2628
readonly normalize: (
2729
item: OrchestrationV2TurnItem,
@@ -42,7 +44,13 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
4244
const allocate: TurnItemPositionStoreV2Shape["allocate"] =
4345
/* This helper runs for individual orchestration events; the enclosing operation owns the span. */
4446
Effect.fnUntraced(
45-
function* ({ threadId, turnItemId, runId, runOrdinal: suppliedRunOrdinal }) {
47+
function* ({
48+
threadId,
49+
turnItemId,
50+
runId,
51+
runOrdinal: suppliedRunOrdinal,
52+
placement = "append",
53+
}) {
4654
const existing = yield* sql<{ readonly ordinal: number }>`
4755
SELECT ordinal
4856
FROM orchestration_v2_turn_item_positions
@@ -68,7 +76,11 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
6876
SELECT
6977
${threadId},
7078
${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+
}
7284
FROM orchestration_v2_turn_item_positions
7385
WHERE thread_id = ${threadId}
7486
AND ordinal >= ${lowerBound}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ const layerProviderTurnStartServiceProvided = ProviderTurnStartService.layer.pip
173173
layerProviderAuthServiceProvided,
174174
layerRunExecutionServiceProvided,
175175
layerRuntimePolicyProvided,
176+
TurnItemPositionStore.layer,
176177
),
177178
),
178179
);

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,20 @@ describe("orchestration v2 provider switching", () => {
13351335
assert.isFalse(turns[1]?.nativeThreadHasTurns);
13361336
assert.equal(turns[2]?.nativeThreadId, turns[1]?.nativeThreadId);
13371337
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+
);
13381352
}).pipe(
13391353
Effect.provide(
13401354
ProviderReplayHarness.layerWithRegistry(

0 commit comments

Comments
 (0)