Skip to content

Commit 9e42991

Browse files
fix(server): show a context handoff when a provider conversation can't be resumed
When a native resume fails (or the history no longer fits, or delivery was uncertain), the turn starts a fresh native session primed with a full-thread summary, but the timeline showed nothing at the point where the provider's native history was lost. The resume fallback now writes the existing `handoff` turn item in the same event write as its context handoff and transfer. Its position is reserved at the start of the run, so it sits ahead of the run's own handoffs and user message, in the order the context is delivered. Refs #28178 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 60d4d87 commit 9e42991

6 files changed

Lines changed: 100 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
@@ -52,6 +52,7 @@ import * as ProviderSessionManager from "./ProviderSessionManager.ts";
5252
import * as ProviderTurnStart from "./ProviderTurnStartService.ts";
5353
import * as RunExecutionService from "./RunExecutionService.ts";
5454
import * as RuntimePolicy from "./RuntimePolicy.ts";
55+
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
5556

5657
const isDomainEvent = Schema.is(OrchestrationV2DomainEvent);
5758

@@ -135,6 +136,7 @@ function startWithMissingWorktree(
135136
Layer.mock(ContextHandoffService.ContextHandoffServiceV2)({}),
136137
Layer.mock(EventSink.EventSinkV2)({ writeIfRunCurrent }),
137138
IdAllocator.layer,
139+
Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}),
138140
Layer.succeed(FileSystem.FileSystem, {
139141
exists: () => Effect.succeed(false),
140142
realPath: (target: string) =>
@@ -893,6 +895,7 @@ function makeLocalCommandHarness(input: {
893895
}),
894896
}),
895897
IdAllocator.layer,
898+
Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}),
896899
layerWorktreeLocation(input.worktreesDir),
897900
input.fileSystem === undefined
898901
? FileSystem.layerNoop({ exists: () => Effect.succeed(true) })

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
} from "@t3tools/provider-core/server/failure";
5757
import * as RunExecutionService from "./RunExecutionService.ts";
5858
import * as RuntimePolicy from "./RuntimePolicy.ts";
59+
import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
5960
import {
6061
isRestartNoteContinuation,
6162
pendingRestartCancelledBackgroundWork,
@@ -144,6 +145,7 @@ export const layer: Layer.Layer<
144145
| ProviderSessionManager.ProviderSessionManagerV2
145146
| RunExecutionService.RunExecutionServiceV2
146147
| RuntimePolicy.RuntimePolicyV2
148+
| TurnItemPositionStore.TurnItemPositionStoreV2
147149
> = Layer.effect(
148150
ProviderTurnStartServiceV2,
149151
Effect.gen(function* () {
@@ -159,6 +161,7 @@ export const layer: Layer.Layer<
159161
const providerSessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
160162
const runExecution = yield* RunExecutionService.RunExecutionServiceV2;
161163
const runtimePolicy = yield* RuntimePolicy.RuntimePolicyV2;
164+
const turnItemPositions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;
162165
const isManagedWorktreePath = yield* makeIsManagedWorktreePath;
163166

164167
const resolveRecreateBase = Effect.fn("ProviderTurnStartService.resolveRecreateBase")(
@@ -1325,6 +1328,47 @@ export const layer: Layer.Layer<
13251328
const { handoff, transferId, createdAt } = yield* recoveryHandoff;
13261329
effectiveHandoffs = withRecoveryHandoff(handoff);
13271330
resumeFellBack = true;
1331+
const previousRun = projection.runs.findLast(
1332+
(candidate) =>
1333+
candidate.providerThreadId === providerThread.id && candidate.ordinal < run.ordinal,
1334+
);
1335+
const handoffTurnItemId = idAllocator.derive.runSignalTurnItem({
1336+
runId,
1337+
signal: `context-handoff:${handoff.id}`,
1338+
});
1339+
const handoffTurnItem: OrchestrationV2TurnItem = {
1340+
id: handoffTurnItemId,
1341+
threadId: projection.thread.id,
1342+
runId,
1343+
nodeId: rootNode.id,
1344+
providerThreadId: providerThread.id,
1345+
providerTurnId: null,
1346+
nativeItemRef: null,
1347+
parentItemId: null,
1348+
// The summary is delivered ahead of this run's own handoffs and message.
1349+
ordinal: yield* turnItemPositions.allocate({
1350+
threadId: projection.thread.id,
1351+
turnItemId: handoffTurnItemId,
1352+
runId,
1353+
runOrdinal: run.ordinal,
1354+
placement: "run_start",
1355+
}),
1356+
status: "completed",
1357+
title: "Recovered context",
1358+
startedAt: createdAt,
1359+
completedAt: createdAt,
1360+
updatedAt: createdAt,
1361+
type: "handoff",
1362+
contextHandoffId: handoff.id,
1363+
fromProviderThreadIds: handoff.fromProviderThreadIds,
1364+
toProviderThreadId: handoff.toProviderThreadId,
1365+
fromProviderInstanceIds: [providerThread.providerInstanceId],
1366+
toProviderInstanceId: run.providerInstanceId,
1367+
fromModelSelections: previousRun === undefined ? [] : [previousRun.modelSelection],
1368+
toModel: run.modelSelection.model,
1369+
strategy: handoff.strategy,
1370+
summary: handoff.summaryText,
1371+
};
13281372
yield* eventSink.write({
13291373
events: [
13301374
{
@@ -1362,6 +1406,15 @@ export const layer: Layer.Layer<
13621406
consumedAt: null,
13631407
},
13641408
},
1409+
{
1410+
id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }),
1411+
type: "turn-item.updated",
1412+
threadId: projection.thread.id,
1413+
runId: run.id,
1414+
providerInstanceId: run.providerInstanceId,
1415+
occurredAt: createdAt,
1416+
payload: handoffTurnItem,
1417+
},
13651418
],
13661419
});
13671420
return replacement;

‎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
@@ -172,6 +172,7 @@ const layerProviderTurnStartServiceProvided = ProviderTurnStartService.layer.pip
172172
layerProviderAuthServiceProvided,
173173
layerRunExecutionServiceProvided,
174174
layerRuntimePolicyProvided,
175+
TurnItemPositionStore.layer,
175176
),
176177
),
177178
);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,14 @@ describe("orchestration v2 provider switching", () => {
14351435
{ type: "handoff", contextHandoffId: handoff!.id },
14361436
"Expected a handoff turn item for the recovered run.",
14371437
);
1438+
assert.deepEqual(
1439+
projection.turnItems
1440+
.filter((item) => item.runId === run2.id)
1441+
.toSorted((left, right) => left.ordinal - right.ordinal)
1442+
.map((item) => item.type)
1443+
.slice(0, 2),
1444+
["handoff", "user_message"],
1445+
);
14381446
}).pipe(
14391447
Effect.provide(
14401448
ProviderReplayHarness.layerWithRegistry(

0 commit comments

Comments
 (0)