@@ -576,6 +576,12 @@ function rootProviderThreadsForProvider(
576576 ) ;
577577}
578578
579+ // Failed and interrupted turns still contain conversation the next provider needs.
580+ // Queued, cancelled, and rolled-back runs must not be replayed as conversation.
581+ function isHandoffSourceRun ( run : OrchestrationV2Run ) : boolean {
582+ return run . status === "completed" || run . status === "failed" || run . status === "interrupted" ;
583+ }
584+
579585function lastCompletedRunForProviderThread (
580586 projection : OrchestrationV2ThreadProjection ,
581587 providerThreadId : OrchestrationV2ProviderThread [ "id" ] ,
@@ -1099,20 +1105,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
10991105 ) ,
11001106 ) ;
11011107 const latestCompletedRun = projection . runs . findLast ( ( run ) => run . status === "completed" ) ;
1108+ const latestHandoffRun = projection . runs . findLast ( isHandoffSourceRun ) ;
11021109 const targetLastCompletedRun = lastCompletedRunForProviderThread (
11031110 projection ,
11041111 queuedProviderThread . id ,
11051112 ) ;
11061113 const coveredRuns =
11071114 canResumeAcrossInstances ||
1108- latestCompletedRun === undefined ||
1109- latestCompletedRun . providerInstanceId === queuedRun . providerInstanceId
1115+ latestHandoffRun === undefined ||
1116+ latestHandoffRun . providerInstanceId === queuedRun . providerInstanceId
11101117 ? [ ]
11111118 : projection . runs . filter (
11121119 ( run ) =>
1113- run . status === "completed" &&
1120+ isHandoffSourceRun ( run ) &&
11141121 run . ordinal > ( targetLastCompletedRun ?. ordinal ?? 0 ) &&
1115- run . ordinal <= latestCompletedRun . ordinal ,
1122+ run . ordinal <= latestHandoffRun . ordinal ,
11161123 ) ;
11171124 const needsFullContext = deliveryProviderThread . nativeThreadRef === null ;
11181125 const legacyImportItems =
@@ -1151,7 +1158,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
11511158 } ) ;
11521159 }
11531160 const handoff =
1154- transferId === null || latestCompletedRun === undefined
1161+ transferId === null || latestHandoffRun === undefined
11551162 ? null
11561163 : yield * contextHandoffService
11571164 . prepareProviderHandoff ( {
@@ -1166,15 +1173,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
11661173 ) ,
11671174 ) ,
11681175 toProviderThreadId : queuedProviderThread . id ,
1169- fromProviderInstanceId : latestCompletedRun . providerInstanceId ,
1176+ fromProviderInstanceId : latestHandoffRun . providerInstanceId ,
11701177 toProviderInstanceId : queuedRun . providerInstanceId ,
11711178 coveredRunOrdinals : {
11721179 from : coveredRuns [ 0 ] ! . ordinal ,
11731180 to : coveredRuns . at ( - 1 ) ! . ordinal ,
11741181 } ,
11751182 strategy : handoffStrategy ,
11761183 items : [
1177- ...( needsFullContext ? legacyImportItems : [ ] ) ,
1184+ ...( needsFullContext && latestCompletedRun !== undefined
1185+ ? legacyImportItems
1186+ : [ ] ) ,
11781187 ...projection . turnItems . filter (
11791188 ( item ) =>
11801189 item . runId !== null && coveredRuns . some ( ( run ) => run . id === item . runId ) ,
@@ -1269,8 +1278,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
12691278 firstRunOrdinal : queuedProviderThread . firstRunOrdinal ?? queuedRun . ordinal ,
12701279 lastRunOrdinal : queuedRun . ordinal ,
12711280 handoffIds : appendContextHandoffId (
1272- queuedProviderThread . handoffIds ,
1273- activeHandoff ?. id ?? null ,
1281+ appendContextHandoffId ( queuedProviderThread . handoffIds , handoff ?. id ?? null ) ,
1282+ legacyImportRecoveryHandoff ?. id ?? null ,
12741283 ) ,
12751284 updatedAt : now ,
12761285 } ;
@@ -1401,7 +1410,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14011410 } ,
14021411 ]
14031412 : [ ] ) ,
1404- ...( handoff === null || transferId === null || latestCompletedRun === undefined
1413+ ...( handoff === null || transferId === null || latestHandoffRun === undefined
14051414 ? [ ]
14061415 : [
14071416 {
@@ -1415,12 +1424,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
14151424 type : "provider_handoff" as const ,
14161425 sourceThreadId : threadId ,
14171426 targetThreadId : threadId ,
1418- sourcePoint : contextSourcePointForRun ( projection , latestCompletedRun ) ,
1427+ sourcePoint : contextSourcePointForRun ( projection , latestHandoffRun ) ,
14191428 basePoint :
14201429 needsFullContext || targetLastCompletedRun === undefined
14211430 ? null
14221431 : contextSourcePointForRun ( projection , targetLastCompletedRun ) ,
1423- sourceProviderInstanceId : latestCompletedRun . providerInstanceId ,
1432+ sourceProviderInstanceId : latestHandoffRun . providerInstanceId ,
14241433 targetProviderInstanceId : queuedRun . providerInstanceId ,
14251434 targetRunId : queuedRun . id ,
14261435 status : "consumed" as const ,
@@ -4345,6 +4354,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
43454354 const ordinal = nextRunOrdinal ( projection ) ;
43464355 const runId = idAllocator . derive . run ( { threadId : command . threadId , ordinal } ) ;
43474356 const latestCompletedRun = projection . runs . findLast ( ( run ) => run . status === "completed" ) ;
4357+ const latestHandoffRun = projection . runs . findLast ( isHandoffSourceRun ) ;
43484358 const legacyImportItems =
43494359 projection . thread . historyOrigin === "v1_import"
43504360 ? projection . turnItems . filter ( ( item ) => item . runId === null )
@@ -4912,22 +4922,23 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49124922 ? undefined
49134923 : lastCompletedRunForProviderThread ( projection , targetProviderThread . id ) ;
49144924 const providerSwitchCoveredRuns =
4915- ! isProviderSwitch || canResumeAcrossInstances || latestCompletedRun === undefined
4925+ ! isProviderSwitch || canResumeAcrossInstances || latestHandoffRun === undefined
49164926 ? [ ]
49174927 : projection . runs . filter (
49184928 ( run ) =>
4919- run . status === "completed" &&
4929+ isHandoffSourceRun ( run ) &&
49204930 run . ordinal >
49214931 ( requiresFullProviderSwitchContext
49224932 ? 0
49234933 : ( targetLastCompletedRun ?. ordinal ?? 0 ) ) &&
4924- run . ordinal <= latestCompletedRun . ordinal ,
4934+ run . ordinal <= latestHandoffRun . ordinal ,
49254935 ) ;
49264936 const providerSwitchItems =
49274937 providerSwitchCoveredRuns . length === 0
49284938 ? [ ]
49294939 : [
4930- ...( targetProviderThread === undefined || requiresFullProviderSwitchContext
4940+ ...( latestCompletedRun !== undefined &&
4941+ ( targetProviderThread === undefined || requiresFullProviderSwitchContext )
49314942 ? legacyImportItems
49324943 : [ ] ) ,
49334944 ...projection . turnItems . filter (
@@ -4937,7 +4948,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49374948 ) ,
49384949 ] ;
49394950 const providerSwitchTransferId =
4940- providerSwitchCoveredRuns . length === 0 || latestCompletedRun === undefined
4951+ providerSwitchCoveredRuns . length === 0 || latestHandoffRun === undefined
49414952 ? null
49424953 : yield * mapDispatchError ( command ) (
49434954 idAllocator . allocate . contextTransfer ( {
@@ -4961,7 +4972,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49614972 ) ;
49624973 }
49634974 const providerSwitchHandoff =
4964- providerSwitchTransferId === null || latestCompletedRun === undefined
4975+ providerSwitchTransferId === null || latestHandoffRun === undefined
49654976 ? null
49664977 : yield * contextHandoffService
49674978 . prepareProviderHandoff ( {
@@ -4976,7 +4987,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49764987 ) ,
49774988 ) ,
49784989 toProviderThreadId : ensuredProviderThread . id ,
4979- fromProviderInstanceId : latestCompletedRun . providerInstanceId ,
4990+ fromProviderInstanceId : latestHandoffRun . providerInstanceId ,
49804991 toProviderInstanceId : modelSelection . instanceId ,
49814992 coveredRunOrdinals : {
49824993 from : providerSwitchCoveredRuns [ 0 ] ! . ordinal ,
@@ -5400,19 +5411,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54005411 if (
54015412 providerSwitchTransferId !== null &&
54025413 providerSwitchHandoff !== null &&
5403- latestCompletedRun !== undefined
5414+ latestHandoffRun !== undefined
54045415 ) {
54055416 const transfer : OrchestrationV2ContextTransfer = {
54065417 id : providerSwitchTransferId ,
54075418 type : "provider_handoff" ,
54085419 sourceThreadId : command . threadId ,
54095420 targetThreadId : command . threadId ,
5410- sourcePoint : contextSourcePointForRun ( projection , latestCompletedRun ) ,
5421+ sourcePoint : contextSourcePointForRun ( projection , latestHandoffRun ) ,
54115422 basePoint :
54125423 requiresFullProviderSwitchContext || targetLastCompletedRun === undefined
54135424 ? null
54145425 : contextSourcePointForRun ( projection , targetLastCompletedRun ) ,
5415- sourceProviderInstanceId : latestCompletedRun . providerInstanceId ,
5426+ sourceProviderInstanceId : latestHandoffRun . providerInstanceId ,
54165427 targetProviderInstanceId : modelSelection . instanceId ,
54175428 targetRunId : runId ,
54185429 status : "consumed" ,
0 commit comments