66 abortReplyRunBySessionId ,
77 expireStaleReplyRunBySessionId ,
88 forceClearReplyRunBySessionId ,
9+ isReplyRunEvidenceStaleBySessionId ,
910 isReplyRunActiveForSessionId ,
1011 isReplyRunAbortableForCompaction ,
1112 isReplyRunStreamingForSessionId ,
@@ -14,6 +15,7 @@ import {
1415 waitForReplyRunEndBySessionId ,
1516} from "../../auto-reply/reply/reply-run-registry.js" ;
1617import {
18+ BLOCKED_TOOL_CALL_ABORT_FLOOR_MS ,
1719 getDiagnosticSessionActivitySnapshot ,
1820 markDiagnosticEmbeddedRunEnded ,
1921 markDiagnosticEmbeddedRunStarted ,
@@ -448,6 +450,13 @@ function prepareEmbeddedAgentQueueMessage(
448450) : PreparedEmbeddedAgentQueueMessage {
449451 const handle = ACTIVE_EMBEDDED_RUNS . get ( sessionId ) ;
450452 if ( ! handle ) {
453+ // A stale reply-backed run must produce the same closed reason as the
454+ // embedded gate so announce delivery falls through to direct instead of
455+ // reading the wedged op as active and dropping the handoff.
456+ if ( isReplyRunEvidenceStaleBySessionId ( sessionId ) ) {
457+ diag . debug ( `queue message failed: sessionId=${ sessionId } reason=stale_run` ) ;
458+ return { kind : "complete" , outcome : createQueueFailureOutcome ( sessionId , "stale_run" ) } ;
459+ }
451460 const queuedReplyRunMessage = queueReplyRunMessage ( sessionId , text , options ) ;
452461 if ( queuedReplyRunMessage ) {
453462 logMessageQueued ( { sessionId, source : "embedded-agent-runner" } ) ;
@@ -479,9 +488,16 @@ function prepareEmbeddedAgentQueueMessage(
479488 return { kind : "complete" , outcome : createQueueFailureOutcome ( sessionId , "not_streaming" ) } ;
480489 }
481490 const activity = getDiagnosticSessionActivitySnapshot ( { sessionId } ) ;
491+ // Quiet tool phases stay steerable until the blocked-tool floor: refusing at
492+ // the shorter window would push the message into admission takeover of a run
493+ // the diagnostic layer still considers healthy.
494+ const steerStaleCaptureMs =
495+ activity . activeWorkKind === "tool_call"
496+ ? Math . max ( EMBEDDED_STEER_STALE_CAPTURE_MS , BLOCKED_TOOL_CALL_ABORT_FLOOR_MS )
497+ : EMBEDDED_STEER_STALE_CAPTURE_MS ;
482498 if (
483499 typeof activity . lastProgressAgeMs === "number" &&
484- activity . lastProgressAgeMs > EMBEDDED_STEER_STALE_CAPTURE_MS
500+ activity . lastProgressAgeMs > steerStaleCaptureMs
485501 ) {
486502 diag . debug ( `queue message failed: sessionId=${ sessionId } reason=stale_run` ) ;
487503 return { kind : "complete" , outcome : createQueueFailureOutcome ( sessionId , "stale_run" ) } ;
0 commit comments