@@ -57,6 +57,7 @@ import {
5757} from "@t3tools/provider-core/server/failure" ;
5858import * as RunExecutionService from "./RunExecutionService.ts" ;
5959import * as RuntimePolicy from "./RuntimePolicy.ts" ;
60+ import * as TurnItemPositionStore from "./TurnItemPositionStore.ts" ;
6061import {
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" ,
0 commit comments