From 60d4d87e02f9963eae364dd53f2e68d062e1afb0 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 31 Oct 2026 15:56:14 +0000 Subject: [PATCH 1/5] test(server): reproduce missing handoff item after resume fallback (#28178) When a same-provider native resume fails and the turn recovers in a fresh native session, the recovery handoff is recorded but no `handoff` turn item is written, so the timeline shows nothing. This test reproduces pingdotgg/t3code#28178 and fails on main. Co-Authored-By: Claude Opus 5.5 --- .../ProviderSwitch.integration.test.ts | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts index bb21d8ca9929a..08863e11112f2 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts @@ -1344,6 +1344,116 @@ describe("orchestration v2 provider switching", () => { ), ); + it.live("shows the recovery handoff in the timeline when a native resume fails", () => + Effect.scoped( + Effect.gen(function* () { + const cwd = yield* checkpointWorkspace("resume-fallback-handoff-item"); + const failResumeOnce = yield* Ref.make(false); + const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([ + makeTestAdapter({ + instanceId: CLAUDE_MODEL_SELECTION.instanceId, + driver: CLAUDE_DRIVER, + capabilities: ClaudeProviderCapabilitiesV2, + modelSelection: CLAUDE_MODEL_SELECTION, + responseByRunOrdinal: {}, + capturedTurns: yield* Ref.make>([]), + failResumeOnce, + nativeThreadGeneration: yield* Ref.make(0), + }), + ]); + yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const runToCompletion = Effect.fn("runToCompletion")(function* (ordinal: number) { + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make(`resume-handoff-item:${ordinal}`), + threadId, + messageId: MessageId.make(`resume-handoff-item:${ordinal}`), + createdBy: "user", + creationSource: "web", + text: `Request ${ordinal}`, + attachments: [], + modelSelection: CLAUDE_MODEL_SELECTION, + dispatchMode: { type: "start_immediately" }, + }); + yield* orchestrator.streamStoredEvents.pipe( + Stream.filter( + ({ event }) => + event.type === "run.updated" && + event.payload.ordinal === ordinal && + (event.payload.status === "completed" || event.payload.status === "failed"), + ), + Stream.runHead, + ); + yield* worker.drain(); + }); + yield* orchestrator.dispatch({ + type: "thread.create", + commandId: CommandId.make("resume-handoff-item:create"), + threadId, + projectId, + createdBy: "user", + creationSource: "web", + title: "Resume fallback handoff item", + modelSelection: CLAUDE_MODEL_SELECTION, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + }); + yield* runToCompletion(1); + const providerSessionId = (yield* orchestrator.getThreadProjection(threadId)) + .providerThreads[0]?.providerSessionId; + if (providerSessionId == null) { + return yield* Effect.die("Expected a provider session after the first turn."); + } + yield* orchestrator.dispatch({ + type: "provider-session.detach", + commandId: CommandId.make("resume-handoff-item:detach"), + threadId, + providerSessionId, + }); + yield* worker.drain(); + yield* Ref.set(failResumeOnce, true); + yield* runToCompletion(2); + + const projection = yield* orchestrator.getThreadProjection(threadId); + const run2 = projection.runs[1]!; + assert.equal(run2.status, "completed"); + const handoff = projection.contextHandoffs.find( + (candidate) => candidate.targetRunId === run2.id, + ); + assert.isDefined(handoff, "Expected the resume fallback to record a recovery handoff."); + assert.deepInclude( + projection.turnItems + .filter((item) => item.runId === run2.id) + .map((item) => ({ + type: item.type, + contextHandoffId: item.type === "handoff" ? item.contextHandoffId : null, + })), + { type: "handoff", contextHandoffId: handoff!.id }, + "Expected a handoff turn item for the recovered run.", + ); + }).pipe( + Effect.provide( + ProviderReplayHarness.layerWithRegistry( + { + name: "resume-fallback-handoff-item", + runtimePolicyOverride: { + cwd, + approvalPolicy: "never", + sandboxPolicy: { type: "readOnly" }, + }, + }, + layerRegistry, + ), + ), + ); + }), + ), + ); + it.live.each(["an older handoff", "a missed request"] as const)( "keeps the latest turn when a resume fallback also delivers %s", (older) => From 9e4299152ede4514520d37791b603e2613095d51 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 31 Oct 2026 16:07:07 +0000 Subject: [PATCH 2/5] 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 --- .../ProviderTurnStartService.test.ts | 3 ++ .../ProviderTurnStartService.ts | 53 +++++++++++++++++++ .../TurnItemPositionStore.test.ts | 21 ++++++++ .../orchestration-v2/TurnItemPositionStore.ts | 16 +++++- .../src/orchestration-v2/runtimeLayer.ts | 1 + .../ProviderSwitch.integration.test.ts | 8 +++ 6 files changed, 100 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index 05fc1b8545bb2..2ff083ac776c7 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -52,6 +52,7 @@ import * as ProviderSessionManager from "./ProviderSessionManager.ts"; import * as ProviderTurnStart from "./ProviderTurnStartService.ts"; import * as RunExecutionService from "./RunExecutionService.ts"; import * as RuntimePolicy from "./RuntimePolicy.ts"; +import * as TurnItemPositionStore from "./TurnItemPositionStore.ts"; const isDomainEvent = Schema.is(OrchestrationV2DomainEvent); @@ -135,6 +136,7 @@ function startWithMissingWorktree( Layer.mock(ContextHandoffService.ContextHandoffServiceV2)({}), Layer.mock(EventSink.EventSinkV2)({ writeIfRunCurrent }), IdAllocator.layer, + Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}), Layer.succeed(FileSystem.FileSystem, { exists: () => Effect.succeed(false), realPath: (target: string) => @@ -893,6 +895,7 @@ function makeLocalCommandHarness(input: { }), }), IdAllocator.layer, + Layer.mock(TurnItemPositionStore.TurnItemPositionStoreV2)({}), layerWorktreeLocation(input.worktreesDir), input.fileSystem === undefined ? FileSystem.layerNoop({ exists: () => Effect.succeed(true) }) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 402b2eaf3e04e..59e90455fb2a8 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -56,6 +56,7 @@ import { } from "@t3tools/provider-core/server/failure"; import * as RunExecutionService from "./RunExecutionService.ts"; import * as RuntimePolicy from "./RuntimePolicy.ts"; +import * as TurnItemPositionStore from "./TurnItemPositionStore.ts"; import { isRestartNoteContinuation, pendingRestartCancelledBackgroundWork, @@ -144,6 +145,7 @@ export const layer: Layer.Layer< | ProviderSessionManager.ProviderSessionManagerV2 | RunExecutionService.RunExecutionServiceV2 | RuntimePolicy.RuntimePolicyV2 + | TurnItemPositionStore.TurnItemPositionStoreV2 > = Layer.effect( ProviderTurnStartServiceV2, Effect.gen(function* () { @@ -159,6 +161,7 @@ export const layer: Layer.Layer< const providerSessions = yield* ProviderSessionManager.ProviderSessionManagerV2; const runExecution = yield* RunExecutionService.RunExecutionServiceV2; const runtimePolicy = yield* RuntimePolicy.RuntimePolicyV2; + const turnItemPositions = yield* TurnItemPositionStore.TurnItemPositionStoreV2; const isManagedWorktreePath = yield* makeIsManagedWorktreePath; const resolveRecreateBase = Effect.fn("ProviderTurnStartService.resolveRecreateBase")( @@ -1325,6 +1328,47 @@ export const layer: Layer.Layer< const { handoff, transferId, createdAt } = yield* recoveryHandoff; effectiveHandoffs = withRecoveryHandoff(handoff); resumeFellBack = true; + const previousRun = projection.runs.findLast( + (candidate) => + candidate.providerThreadId === providerThread.id && candidate.ordinal < run.ordinal, + ); + const handoffTurnItemId = idAllocator.derive.runSignalTurnItem({ + runId, + signal: `context-handoff:${handoff.id}`, + }); + const handoffTurnItem: OrchestrationV2TurnItem = { + id: handoffTurnItemId, + threadId: projection.thread.id, + runId, + nodeId: rootNode.id, + providerThreadId: providerThread.id, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + // The summary is delivered ahead of this run's own handoffs and message. + ordinal: yield* turnItemPositions.allocate({ + threadId: projection.thread.id, + turnItemId: handoffTurnItemId, + runId, + runOrdinal: run.ordinal, + placement: "run_start", + }), + status: "completed", + title: "Recovered context", + startedAt: createdAt, + completedAt: createdAt, + updatedAt: createdAt, + type: "handoff", + contextHandoffId: handoff.id, + fromProviderThreadIds: handoff.fromProviderThreadIds, + toProviderThreadId: handoff.toProviderThreadId, + fromProviderInstanceIds: [providerThread.providerInstanceId], + toProviderInstanceId: run.providerInstanceId, + fromModelSelections: previousRun === undefined ? [] : [previousRun.modelSelection], + toModel: run.modelSelection.model, + strategy: handoff.strategy, + summary: handoff.summaryText, + }; yield* eventSink.write({ events: [ { @@ -1362,6 +1406,15 @@ export const layer: Layer.Layer< consumedAt: null, }, }, + { + id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), + type: "turn-item.updated", + threadId: projection.thread.id, + runId: run.id, + providerInstanceId: run.providerInstanceId, + occurredAt: createdAt, + payload: handoffTurnItem, + }, ], }); return replacement; diff --git a/apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts b/apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts index 579c82f03ee40..ea3a70bdf22af 100644 --- a/apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts +++ b/apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts @@ -56,3 +56,24 @@ it.effect("reuses an item's position without attempting another allocation inser ); }).pipe(Effect.provide(layerTest)), ); + +it.effect("places a run-start item ahead of the run's items, then appends once taken", () => + Effect.gen(function* () { + const positions = yield* TurnItemPositionStore.TurnItemPositionStoreV2; + const threadId = ThreadId.make("thread:run-start"); + const allocate = (id: string, placement?: "run_start") => + positions.allocate({ + threadId, + turnItemId: TurnItemId.make(id), + runId: RunId.make("run:2"), + runOrdinal: 2, + ...(placement === undefined ? {} : { placement }), + }); + const message = yield* allocate("item:message"); + const recovery = yield* allocate("item:recovery", "run_start"); + assert.isBelow(recovery, message); + assert.isAbove(recovery, 1_999_999); + assert.equal(yield* allocate("item:retry-recovery", "run_start"), message + 1); + assert.equal(yield* allocate("item:reply"), message + 2); + }).pipe(Effect.provide(layerTest)), +); diff --git a/apps/server/src/orchestration-v2/TurnItemPositionStore.ts b/apps/server/src/orchestration-v2/TurnItemPositionStore.ts index 03636b34153c9..95eb9cdc2415f 100644 --- a/apps/server/src/orchestration-v2/TurnItemPositionStore.ts +++ b/apps/server/src/orchestration-v2/TurnItemPositionStore.ts @@ -22,6 +22,8 @@ export interface TurnItemPositionStoreV2Shape { readonly turnItemId: TurnItemId; readonly runId: RunId | null; readonly runOrdinal?: number; + // `run_start` takes the run's unused first slot, ahead of its items, and appends once it is taken. + readonly placement?: "append" | "run_start"; }) => Effect.Effect; readonly normalize: ( item: OrchestrationV2TurnItem, @@ -42,7 +44,13 @@ export const layer: Layer.Layer` SELECT ordinal FROM orchestration_v2_turn_item_positions @@ -68,7 +76,11 @@ export const layer: Layer.Layer ${lowerBound} THEN ${lowerBound} ELSE MAX(ordinal) + 1 END` + : sql`COALESCE(MAX(ordinal), ${lowerBound}) + 1` + } FROM orchestration_v2_turn_item_positions WHERE thread_id = ${threadId} AND ordinal >= ${lowerBound} diff --git a/apps/server/src/orchestration-v2/runtimeLayer.ts b/apps/server/src/orchestration-v2/runtimeLayer.ts index 1b54627595372..1569d80927771 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.ts @@ -172,6 +172,7 @@ const layerProviderTurnStartServiceProvided = ProviderTurnStartService.layer.pip layerProviderAuthServiceProvided, layerRunExecutionServiceProvided, layerRuntimePolicyProvided, + TurnItemPositionStore.layer, ), ), ); diff --git a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts index 08863e11112f2..896b0f4f280f5 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts @@ -1435,6 +1435,14 @@ describe("orchestration v2 provider switching", () => { { type: "handoff", contextHandoffId: handoff!.id }, "Expected a handoff turn item for the recovered run.", ); + assert.deepEqual( + projection.turnItems + .filter((item) => item.runId === run2.id) + .toSorted((left, right) => left.ordinal - right.ordinal) + .map((item) => item.type) + .slice(0, 2), + ["handoff", "user_message"], + ); }).pipe( Effect.provide( ProviderReplayHarness.layerWithRegistry( From 81d30189e5ea2e849ca28485542336d73e0fddcd Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 23:45:42 +0000 Subject: [PATCH 3/5] test(server): assert the recovery handoff in the existing resume fallback case AI-assisted; verified with the tests and tools listed above. --- .../ProviderSwitch.integration.test.ts | 132 ++---------------- 1 file changed, 14 insertions(+), 118 deletions(-) diff --git a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts index 4720c2eb5f641..728e4f2189c26 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts @@ -1335,6 +1335,20 @@ describe("orchestration v2 provider switching", () => { assert.isFalse(turns[1]?.nativeThreadHasTurns); assert.equal(turns[2]?.nativeThreadId, turns[1]?.nativeThreadId); assert.isTrue(turns[2]?.nativeThreadHasTurns); + + const projection = yield* orchestrator.getThreadProjection(threadId); + const run2 = projection.runs[1]!; + const handoff = projection.contextHandoffs.find( + (candidate) => candidate.targetRunId === run2.id, + ); + assert.deepEqual( + projection.turnItems + .filter((item) => item.runId === run2.id) + .toSorted((left, right) => left.ordinal - right.ordinal) + .slice(0, 2) + .map((item) => (item.type === "handoff" ? item.contextHandoffId : item.type)), + [handoff?.id, "user_message"], + ); }).pipe( Effect.provide( ProviderReplayHarness.layerWithRegistry( @@ -1354,124 +1368,6 @@ describe("orchestration v2 provider switching", () => { ), ); - it.live("shows the recovery handoff in the timeline when a native resume fails", () => - Effect.scoped( - Effect.gen(function* () { - const cwd = yield* checkpointWorkspace("resume-fallback-handoff-item"); - const failResumeOnce = yield* Ref.make(false); - const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([ - makeTestAdapter({ - instanceId: CLAUDE_MODEL_SELECTION.instanceId, - driver: CLAUDE_DRIVER, - capabilities: ClaudeProviderCapabilitiesV2, - modelSelection: CLAUDE_MODEL_SELECTION, - responseByRunOrdinal: {}, - capturedTurns: yield* Ref.make>([]), - failResumeOnce, - nativeThreadGeneration: yield* Ref.make(0), - }), - ]); - yield* Effect.gen(function* () { - const orchestrator = yield* Orchestrator.OrchestratorV2; - const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; - const runToCompletion = Effect.fn("runToCompletion")(function* (ordinal: number) { - yield* orchestrator.dispatch({ - type: "message.dispatch", - commandId: CommandId.make(`resume-handoff-item:${ordinal}`), - threadId, - messageId: MessageId.make(`resume-handoff-item:${ordinal}`), - createdBy: "user", - creationSource: "web", - text: `Request ${ordinal}`, - attachments: [], - modelSelection: CLAUDE_MODEL_SELECTION, - dispatchMode: { type: "start_immediately" }, - }); - yield* orchestrator.streamStoredEvents.pipe( - Stream.filter( - ({ event }) => - event.type === "run.updated" && - event.payload.ordinal === ordinal && - (event.payload.status === "completed" || event.payload.status === "failed"), - ), - Stream.runHead, - ); - yield* worker.drain(); - }); - yield* orchestrator.dispatch({ - type: "thread.create", - commandId: CommandId.make("resume-handoff-item:create"), - threadId, - projectId, - createdBy: "user", - creationSource: "web", - title: "Resume fallback handoff item", - modelSelection: CLAUDE_MODEL_SELECTION, - runtimeMode: "full-access", - interactionMode: "default", - branch: null, - worktreePath: null, - }); - yield* runToCompletion(1); - const providerSessionId = (yield* orchestrator.getThreadProjection(threadId)) - .providerThreads[0]?.providerSessionId; - if (providerSessionId == null) { - return yield* Effect.die("Expected a provider session after the first turn."); - } - yield* orchestrator.dispatch({ - type: "provider-session.detach", - commandId: CommandId.make("resume-handoff-item:detach"), - threadId, - providerSessionId, - }); - yield* worker.drain(); - yield* Ref.set(failResumeOnce, true); - yield* runToCompletion(2); - - const projection = yield* orchestrator.getThreadProjection(threadId); - const run2 = projection.runs[1]!; - assert.equal(run2.status, "completed"); - const handoff = projection.contextHandoffs.find( - (candidate) => candidate.targetRunId === run2.id, - ); - assert.isDefined(handoff, "Expected the resume fallback to record a recovery handoff."); - assert.deepInclude( - projection.turnItems - .filter((item) => item.runId === run2.id) - .map((item) => ({ - type: item.type, - contextHandoffId: item.type === "handoff" ? item.contextHandoffId : null, - })), - { type: "handoff", contextHandoffId: handoff!.id }, - "Expected a handoff turn item for the recovered run.", - ); - assert.deepEqual( - projection.turnItems - .filter((item) => item.runId === run2.id) - .toSorted((left, right) => left.ordinal - right.ordinal) - .map((item) => item.type) - .slice(0, 2), - ["handoff", "user_message"], - ); - }).pipe( - Effect.provide( - ProviderReplayHarness.layerWithRegistry( - { - name: "resume-fallback-handoff-item", - runtimePolicyOverride: { - cwd, - approvalPolicy: "never", - sandboxPolicy: { type: "readOnly" }, - }, - }, - layerRegistry, - ), - ), - ); - }), - ), - ); - it.live.each(["an older handoff", "a missed request"] as const)( "keeps the latest turn when a resume fallback also delivers %s", (older) => From 8dde9583ab75d0ddeb0b01f09bbd89f698e6627d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 00:45:25 +0000 Subject: [PATCH 4/5] fix(server): keep the recovery handoff item id stable across start retries Derive the item id from the run instead of the per-attempt handoff id, so a retried start reuses the reserved run-start slot, and only take the previous model from a run that actually started. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/ProviderTurnStartService.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index fa458271b45cb..0aa05ceca1a8d 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1374,11 +1374,14 @@ export const layer: Layer.Layer< resumeFellBack = true; const previousRun = projection.runs.findLast( (candidate) => - candidate.providerThreadId === providerThread.id && candidate.ordinal < run.ordinal, + candidate.providerThreadId === providerThread.id && + candidate.ordinal < run.ordinal && + candidate.startedAt !== null, ); + // Stable per run so a retried start reuses the reserved run-start position. const handoffTurnItemId = idAllocator.derive.runSignalTurnItem({ runId, - signal: `context-handoff:${handoff.id}`, + signal: "resume-fallback-handoff", }); const handoffTurnItem: OrchestrationV2TurnItem = { id: handoffTurnItemId, From dccdab75429ce59679ec08d30948bf4e3fd8125f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 03:10:16 +0000 Subject: [PATCH 5/5] fix(server): commit the recovery handoff item with the running transition An interrupted start no longer leaves a Recovered context item on a run that never reached running. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 --- .../ProviderTurnStartService.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 0aa05ceca1a8d..c692335f8ff08 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1118,6 +1118,7 @@ export const layer: Layer.Layer< let resumedRetryHandoff: ReadonlyArray | undefined; let resumedHandoffContext: ReturnType | undefined; let portableForkFallback: OrchestrationV2ContextHandoff | undefined; + let recoveryTurnItem: OrchestrationV2TurnItem | undefined; const loadedProviderThread = yield* Effect.gen(function* () { if (nativeForkTransfer !== undefined) { const sourceProjection = yield* projectionStore.getThreadRecords( @@ -1453,17 +1454,9 @@ export const layer: Layer.Layer< consumedAt: null, }, }, - { - id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), - type: "turn-item.updated", - threadId: projection.thread.id, - runId: run.id, - providerInstanceId: run.providerInstanceId, - occurredAt: createdAt, - payload: handoffTurnItem, - }, ], }); + recoveryTurnItem = handoffTurnItem; return replacement; }); if (loadedProviderThread === undefined) return; @@ -1570,6 +1563,20 @@ export const layer: Layer.Layer< }, }, ]), + // Shown only once the replacement session commits with the running transition. + ...(recoveryTurnItem === undefined + ? [] + : [ + { + id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), + type: "turn-item.updated" as const, + threadId: projection.thread.id, + runId: run.id, + providerInstanceId: run.providerInstanceId, + occurredAt: now, + payload: recoveryTurnItem, + }, + ]), { id: yield* idAllocator.allocate.event({ threadId: projection.thread.id }), type: "run.updated",