@@ -18,7 +18,11 @@ import type { NormalizedUsage } from "../../usage.js";
1818import { log } from "../logger.js" ;
1919import type { PromptCacheBreak , PromptCacheChange } from "../prompt-cache-observability.js" ;
2020import { clearActiveEmbeddedRun } from "../runs.js" ;
21- import { joinWithRunLivenessDeadline , RUN_LIVENESS_JOIN_TIMEOUT_MS } from "./abortable.js" ;
21+ import {
22+ isOpenClawAbortableWrapper ,
23+ joinWithRunLivenessDeadline ,
24+ RUN_LIVENESS_JOIN_TIMEOUT_MS ,
25+ } from "./abortable.js" ;
2226import type {
2327 EmbeddedAttemptExecutionPhaseInput ,
2428 EmbeddedAttemptExecutionState ,
@@ -335,28 +339,60 @@ export async function runEmbeddedAttemptSettledPhase(
335339 source : "yield_cleanup" ,
336340 } ) ;
337341 } ,
342+ isRunBudgetTimeoutAbort : ( error ) =>
343+ readTerminal ( ) . timedOutByRunBudget &&
344+ isOpenClawAbortableWrapper ( error ) &&
345+ error instanceof Error &&
346+ error . cause === input . runAbortController . signal . reason ,
338347 readYieldState : input . lifecycle . readYieldState ,
339348 stopAcceptingSteerMessages,
340349 takePendingMidTurnPrecheckRequest : contextGuards . takePendingMidTurnPrecheckRequest ,
341350 } ,
342351 } ) ;
343352
344- // Queued subscription handlers (block-reply delivery, tool events) are
345- // fire-and-forget during the turn; the pending-events join below is the only
346- // place the run waits for them. One hung handler (e.g. a stuck delivery
347- // dispatch lane) must not dead-end the turn until the run budget — 48h by
348- // default — so the join is bounded and settlement proceeds with a recorded
349- // warning instead of producing no visible outcome at all.
350- await joinWithRunLivenessDeadline ( {
351- joinWork : waitForPendingEvents ,
352- runAbortSignal : input . runAbortController . signal ,
353- onTimeout : ( ) => {
354- log . warn (
355- `pending subscription events did not settle within ${ RUN_LIVENESS_JOIN_TIMEOUT_MS } ms; ` +
356- `proceeding to stream settlement: runId=${ attempt . runId } ` ,
357- ) ;
358- } ,
359- } ) ;
353+ // Only a failure-free run-budget terminal may publish buffered text.
354+ const isFailureFreeRunBudgetTimeout = ( ) : boolean => {
355+ const terminal = readTerminal ( ) ;
356+ return terminal . timedOutByRunBudget && ! terminal . failed ;
357+ } ;
358+ const runBudgetTimeoutTerminal = isFailureFreeRunBudgetTimeout ( ) ;
359+ const drainPendingEventsBounded = ( ) =>
360+ joinWithRunLivenessDeadline ( {
361+ // Partial-reply callbacks cannot mutate the buffer and may be stalled
362+ // on transport; timeout salvage needs only the serialized event chain.
363+ joinWork : ( ) => waitForPendingEvents ( { includePartialReplies : false } ) ,
364+ onTimeout : ( ) => {
365+ log . warn (
366+ `pending subscription events did not settle within ${ RUN_LIVENESS_JOIN_TIMEOUT_MS } ms; ` +
367+ `proceeding to stream settlement: runId=${ attempt . runId } ` ,
368+ ) ;
369+ } ,
370+ } ) ;
371+ if ( runBudgetTimeoutTerminal ) {
372+ // The timeout already aborted the signal; drain without racing it.
373+ await drainPendingEventsBounded ( ) ;
374+ } else {
375+ await joinWithRunLivenessDeadline ( {
376+ joinWork : waitForPendingEvents ,
377+ runAbortSignal : input . runAbortController . signal ,
378+ onTimeout : ( ) => {
379+ log . warn (
380+ `pending subscription events did not settle within ${ RUN_LIVENESS_JOIN_TIMEOUT_MS } ms; ` +
381+ `proceeding to stream settlement: runId=${ attempt . runId } ` ,
382+ ) ;
383+ } ,
384+ } ) ;
385+ // A timeout can fire during the abort-aware join and resolve it before
386+ // its queue drains. Re-read terminal ownership, then drain if eligible.
387+ if ( isFailureFreeRunBudgetTimeout ( ) ) {
388+ await drainPendingEventsBounded ( ) ;
389+ }
390+ }
391+ // Ownership can change during the drain; publish only after the final read.
392+ const salvageTerminal = readTerminal ( ) ;
393+ if ( salvageTerminal . timedOutByRunBudget && ! salvageTerminal . failed ) {
394+ subscription . flushPartialAssistantText ( ) ;
395+ }
360396 const beforeAgentFinalizeRevisionReason = getBeforeAgentFinalizeRevisionReason ( ) ;
361397 const beforeAgentFinalizeRevisionEntryId = getBeforeAgentFinalizeRevisionEntryId ( ) ;
362398 let rewoundBeforeAgentFinalizeRevision = false ;
0 commit comments