@@ -52,6 +52,7 @@ import * as ProviderEventIngestor from "./ProviderEventIngestor.ts";
5252import type { ProjectionStoreV2Error } from "./ProjectionStore.ts" ;
5353import { makeProviderFailure , makeProviderFailureTurnItem } from "./ProviderFailure.ts" ;
5454import * as RunFinalizationService from "./RunFinalizationService.ts" ;
55+ import { stripUnservedToolOutputImageBytes } from "./toolOutputImageBytes.ts" ;
5556
5657export interface ProviderEventRoutingState {
5758 readonly ownedThreadIds : ReadonlySet < ThreadId > ;
@@ -318,6 +319,71 @@ export function cascadeTerminalizeRunOwnedSubagents(input: {
318319 } ) ;
319320}
320321
322+ /**
323+ * A root run's tool calls and their nodes that have not ended yet. Items are
324+ * kept as the ingestor persists them, without unserved image bytes.
325+ */
326+ type OpenRunToolCalls = {
327+ readonly turnItems : ReadonlyMap < TurnItemId , OrchestrationV2TurnItem > ;
328+ readonly nodes : ReadonlyMap < NodeId , OrchestrationV2ExecutionNode > ;
329+ } ;
330+
331+ function emptyOpenRunToolCalls ( ) : OpenRunToolCalls {
332+ return { turnItems : new Map ( ) , nodes : new Map ( ) } ;
333+ }
334+
335+ /**
336+ * Ends the tool calls a run left open when its provider event stream was lost,
337+ * such as when a provider switch released the session mid-turn. No process is
338+ * left to report how they ended, so they would otherwise stay running. They
339+ * were cut short, not failed: a call such as t3_thread_configure may have done
340+ * its work before the session went away.
341+ */
342+ function interruptOpenRunToolCalls ( input : {
343+ readonly run : OrchestrationV2Run ;
344+ readonly open : OpenRunToolCalls ;
345+ readonly completedAt : DateTime . Utc ;
346+ readonly allocateEventId : ( ) => Effect . Effect < EventId , IdAllocator . IdAllocatorV2AllocationError > ;
347+ } ) : Effect . Effect <
348+ ReadonlyArray < OrchestrationV2DomainEvent > ,
349+ IdAllocator . IdAllocatorV2AllocationError
350+ > {
351+ return Effect . gen ( function * ( ) {
352+ const events : Array < OrchestrationV2DomainEvent > = [ ] ;
353+ for ( const turnItem of input . open . turnItems . values ( ) ) {
354+ const node = turnItem . nodeId === null ? undefined : input . open . nodes . get ( turnItem . nodeId ) ;
355+ if ( node !== undefined ) {
356+ events . push ( {
357+ id : yield * input . allocateEventId ( ) ,
358+ type : "node.updated" ,
359+ threadId : node . threadId ,
360+ runId : input . run . id ,
361+ nodeId : node . id ,
362+ providerInstanceId : input . run . providerInstanceId ,
363+ occurredAt : input . completedAt ,
364+ payload : { ...node , status : "interrupted" , completedAt : input . completedAt } ,
365+ } ) ;
366+ }
367+ events . push ( {
368+ id : yield * input . allocateEventId ( ) ,
369+ type : "turn-item.updated" ,
370+ threadId : turnItem . threadId ,
371+ runId : input . run . id ,
372+ ...( turnItem . nodeId === null ? { } : { nodeId : turnItem . nodeId } ) ,
373+ providerInstanceId : input . run . providerInstanceId ,
374+ occurredAt : input . completedAt ,
375+ payload : {
376+ ...turnItem ,
377+ status : "interrupted" ,
378+ completedAt : input . completedAt ,
379+ updatedAt : input . completedAt ,
380+ } ,
381+ } ) ;
382+ }
383+ return events ;
384+ } ) ;
385+ }
386+
321387export function finalProviderThreadStatus (
322388 disposition : ProviderTerminalEvent [ "threadDisposition" ] ,
323389) : OrchestrationV2ProviderThread [ "status" ] {
@@ -566,6 +632,8 @@ export const layer: Layer.Layer<
566632 readonly shouldFinalizeRun ?: ( ) => Effect . Effect < boolean , ProjectionStoreV2Error > ;
567633 readonly hasUnpairedRunInterruptRequest ?: ( ) => Effect . Effect < boolean , never > ;
568634 readonly openRunOwnedSubagents ?: OpenRunOwnedSubagentProjection ;
635+ /** Set only when the provider stream was lost before the turn ended. */
636+ readonly abandonedToolCalls ?: OpenRunToolCalls ;
569637 readonly terminal : ProviderTerminalEvent ;
570638 readonly failureItemPersisted : boolean ;
571639 readonly refreshAfterTurn : Effect . Effect < void > ;
@@ -636,6 +704,15 @@ export const layer: Layer.Layer<
636704 allocateEventId,
637705 } )
638706 : [ ] ;
707+ const abandonedToolCallEvents =
708+ input . abandonedToolCalls === undefined
709+ ? [ ]
710+ : yield * interruptOpenRunToolCalls ( {
711+ run : input . run ,
712+ open : input . abandonedToolCalls ,
713+ completedAt,
714+ allocateEventId,
715+ } ) ;
639716 const persistedStatus =
640717 input . terminal . status === "completed" ? "waiting" : input . terminal . status ;
641718 // Completion cohorts are advanced by Orchestrator while a provider
@@ -690,6 +767,7 @@ export const layer: Layer.Layer<
690767 // Terminalize open run-owned subagent rows before the root run
691768 // settles so projections never keep a forever-running subagent card.
692769 ...cascadedSubagentEvents ,
770+ ...abandonedToolCallEvents ,
693771 ...( finalizedAttempt === null
694772 ? [ ]
695773 : [
@@ -960,6 +1038,7 @@ export const layer: Layer.Layer<
9601038 ReadonlySet < OrchestrationV2TurnItem [ "id" ] >
9611039 > ( new Set ( inheritedBackgroundTurnItemsById . keys ( ) ) ) ;
9621040 const openRunOwnedSubagents = yield * Ref . make ( emptyOpenRunOwnedSubagentProjection ( ) ) ;
1041+ const openToolCalls = yield * Ref . make ( emptyOpenRunToolCalls ( ) ) ;
9631042 const finalizeRootRun = ( terminal : ProviderTerminalEvent ) =>
9641043 Effect . gen ( function * ( ) {
9651044 if ( yield * Ref . get ( rootRunFinalized ) ) {
@@ -1055,6 +1134,23 @@ export const layer: Layer.Layer<
10551134 } ) ;
10561135 }
10571136 }
1137+ if (
1138+ event . type === "node.updated" &&
1139+ event . node . kind === "tool_call" &&
1140+ event . node . threadId === input . run . threadId &&
1141+ event . node . runId === input . run . id
1142+ ) {
1143+ const node = event . node ;
1144+ yield * Ref . update ( openToolCalls , ( current ) => {
1145+ const nodes = new Map ( current . nodes ) ;
1146+ if ( isOpenExecutionNodeStatus ( node . status ) ) {
1147+ nodes . set ( node . id , node ) ;
1148+ } else {
1149+ nodes . delete ( node . id ) ;
1150+ }
1151+ return { ...current , nodes } ;
1152+ } ) ;
1153+ }
10581154 if ( event . type === "node.updated" ) {
10591155 const belongsToRootSubagent =
10601156 event . node . kind === "subagent" && event . node . runId === input . run . id ;
@@ -1110,6 +1206,23 @@ export const layer: Layer.Layer<
11101206 return { ...current , childTurnItems } ;
11111207 } ) ;
11121208 }
1209+ if (
1210+ belongsToRootRun &&
1211+ event . turnItem . threadId === input . run . threadId &&
1212+ ( event . turnItem . type === "command_execution" ||
1213+ event . turnItem . type === "dynamic_tool" )
1214+ ) {
1215+ const toolItem = event . turnItem ;
1216+ yield * Ref . update ( openToolCalls , ( current ) => {
1217+ const turnItems = new Map ( current . turnItems ) ;
1218+ if ( isSettledTurnItemStatus ( toolItem . status ) ) {
1219+ turnItems . delete ( toolItem . id ) ;
1220+ } else {
1221+ turnItems . set ( toolItem . id , stripUnservedToolOutputImageBytes ( toolItem ) ) ;
1222+ }
1223+ return { ...current , turnItems } ;
1224+ } ) ;
1225+ }
11131226 if ( belongsToRootRun && event . turnItem . type === "subagent" ) {
11141227 const subagentItem = event . turnItem ;
11151228 yield * Ref . update ( openRunOwnedSubagents , ( current ) => {
@@ -1293,8 +1406,11 @@ export const layer: Layer.Layer<
12931406 Effect . flatMap ( ( providerThread ) =>
12941407 Ref . get ( latestTurnItemOrdinal ) . pipe (
12951408 Effect . flatMap ( ( latestItemOrdinal ) =>
1296- Ref . get ( openRunOwnedSubagents ) . pipe (
1297- Effect . flatMap ( ( openSubagents ) =>
1409+ Effect . all ( [
1410+ Ref . get ( openRunOwnedSubagents ) ,
1411+ Ref . get ( openToolCalls ) ,
1412+ ] ) . pipe (
1413+ Effect . flatMap ( ( [ openSubagents , abandonedToolCalls ] ) =>
12981414 writeFinalRunEvents ( {
12991415 run : input . run ,
13001416 rootNode : input . rootNode ,
@@ -1308,6 +1424,7 @@ export const layer: Layer.Layer<
13081424 expectedStatus : "running" ,
13091425 } ,
13101426 openRunOwnedSubagents : openSubagents ,
1427+ abandonedToolCalls,
13111428 terminal : makeFailedTerminalEvent (
13121429 makeProviderFailure ( {
13131430 cause : Cause . squash ( cause ) ,
0 commit comments