@@ -2371,7 +2371,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23712371 } ) ;
23722372 }
23732373 // Only the agent's watch_pull_request links as "agent". A call that raced a Stop may land
2374- // after its run ended, so the latest run decides. A user can still watch after a Stop .
2374+ // after its run ended, so the latest run that executed decides. A user can still watch.
23752375 if (
23762376 command . type === "thread.pull-request.watch" &&
23772377 command . watching &&
@@ -2380,8 +2380,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
23802380 const { runs } = yield * projectionStore
23812381 . getThreadRecords ( command . threadId , [ "runs" ] )
23822382 . pipe ( mapDispatchError ( command ) ) ;
2383- const latest = latestDequeuedRun ( runs ) ;
2384- if ( latest !== undefined && ( yield * stopReachedRun ( command , command . threadId , latest . id ) ) ) {
2383+ const latest = latestExecutedRun ( runs ) ;
2384+ if ( latest !== null && ( yield * stopReachedRun ( command , command . threadId , latest . id ) ) ) {
23852385 return yield * new OrchestratorDispatchError ( {
23862386 commandId : command . commandId ,
23872387 commandType : command . type ,
@@ -7997,15 +7997,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
79977997 mapDispatchError ( command ) ,
79987998 ) ;
79997999
8000- /**
8001- * The thread's latest run that left the queue: the one an agent's tool call or a restart
8002- * continuation can come from. A run a restart cut before its provider started counts.
8003- */
8004- const latestDequeuedRun = ( runs : ReadonlyArray < OrchestrationV2Run > ) =>
8005- runs
8006- . filter ( ( run ) => run . status !== "queued" )
8007- . toSorted ( ( left , right ) => right . ordinal - left . ordinal ) [ 0 ] ;
8008-
80098000 /**
80108001 * What Stop holds besides the run it interrupts: queued runs wait for the user, the
80118002 * thread's pull request watches end, and every wake its delegated tasks still owe is
@@ -8588,39 +8579,53 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
85888579 } ) ;
85898580 }
85908581 const now = yield * DateTime . now ;
8591- // Record that Stop reached the thread's finished run too. A late tool call from its
8592- // agent then starts nothing, and a restart that cut it does not continue it.
8593- const latest = latestDequeuedRun ( projection . runs ) ;
8594- if (
8595- latest !== undefined &&
8596- ! hasLiveRun ( { runs : [ latest ] } ) &&
8597- latest . rootNodeId !== null &&
8598- latest . providerThreadId !== null &&
8599- ! ( yield * stopReachedRun ( command , command . threadId , latest . id ) )
8600- ) {
8582+ // Record that Stop reached the thread's last executed run, so a late tool call from its
8583+ // agent starts nothing, and every run a restart cut, so its continuation never starts.
8584+ const executed = latestExecutedRun ( projection . runs ) ;
8585+ const reached = new Map < RunId , OrchestrationV2Run > ( ) ;
8586+ if ( executed !== null && ! hasLiveRun ( { runs : [ executed ] } ) )
8587+ reached . set ( executed . id , executed ) ;
8588+ for ( const run of projection . runs ) {
8589+ if (
8590+ run . status === "cancelled" &&
8591+ ( executed === null || run . ordinal >= executed . ordinal ) &&
8592+ ( yield * awaitsRestartContinuation ( run ) . pipe ( mapDispatchError ( command ) ) )
8593+ ) {
8594+ reached . set ( run . id , run ) ;
8595+ }
8596+ }
8597+ let ordinal = yield * nextTurnItemOrdinal ( projection ) ;
8598+ for ( const run of reached . values ( ) ) {
8599+ if (
8600+ run . rootNodeId === null ||
8601+ run . providerThreadId === null ||
8602+ ( yield * stopReachedRun ( command , command . threadId , run . id ) )
8603+ ) {
8604+ continue ;
8605+ }
86018606 yield * emit (
86028607 events ,
86038608 command ,
86048609 ) ( {
86058610 type : "turn-item.updated" ,
86068611 threadId : command . threadId ,
8607- runId : latest . id ,
8608- nodeId : latest . rootNodeId ,
8609- providerInstanceId : latest . providerInstanceId ,
8612+ runId : run . id ,
8613+ nodeId : run . rootNodeId ,
8614+ providerInstanceId : run . providerInstanceId ,
86108615 occurredAt : now ,
86118616 payload : {
86128617 id : idAllocator . derive . runSignalTurnItem ( {
8613- runId : latest . id ,
8618+ runId : run . id ,
86148619 signal : "interrupt-request" ,
86158620 } ) ,
86168621 threadId : command . threadId ,
8617- runId : latest . id ,
8618- nodeId : latest . rootNodeId ,
8619- providerThreadId : latest . providerThreadId ,
8622+ runId : run . id ,
8623+ nodeId : run . rootNodeId ,
8624+ providerThreadId : run . providerThreadId ,
86208625 providerTurnId : null ,
86218626 nativeItemRef : null ,
86228627 parentItemId : null ,
8623- ordinal : yield * nextTurnItemOrdinal ( projection ) ,
8628+ ordinal : ordinal ++ ,
86248629 status : "completed" ,
86258630 title : "Interrupt requested" ,
86268631 startedAt : now ,
0 commit comments