@@ -176,6 +176,7 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
176176import {
177177 makeProviderFailure ,
178178 makeProviderRetryTurnItem ,
179+ NATIVE_SESSION_STALLED_FAILURE_CODE ,
179180} from "@t3tools/provider-core/server/failure" ;
180181import { turnScopedSelectionTransition } from "@t3tools/provider-core/server/selectionTransition" ;
181182import { providerMessageTextWithAttachmentPaths } from "@t3tools/provider-core/server/attachmentPrompt" ;
@@ -312,6 +313,7 @@ const WORKFLOW_MEMBER_TRANSCRIPT_FLUSH_MS = 500;
312313// A steer waits at most this long, from its request, for its foreground
313314// subagents to move to the background before it is sent.
314315const BACKGROUND_MOVE_TIMEOUT_MS = 5_000 ;
316+ const CLAUDE_STALLED_PHASE_TIMEOUT = "2 minutes" ;
315317
316318const CLAUDE_GROUP_WATCH_SCRIPT = [
317319 "{ trap '' TERM; read -r _ <&3; kill -TERM 0; sleep 5; kill -KILL 0; } </dev/null >/dev/null 2>&1 &" ,
@@ -3477,6 +3479,13 @@ interface ActiveClaudeTurnContext {
34773479 nativeWakeStarted : boolean ;
34783480 gatedFramesBeforeEcho : number ;
34793481 readonly heldRootFrames : Array < SDKMessage > ;
3482+ stalledPhase : ClaudeStalledPhase | null ;
3483+ }
3484+
3485+ type ClaudeStalledPhaseKind = "compaction" | "request" ;
3486+
3487+ interface ClaudeStalledPhase {
3488+ readonly kind : ClaudeStalledPhaseKind ;
34803489}
34813490
34823491interface ActiveClaudeProviderRetry {
@@ -3519,6 +3528,10 @@ interface ClaudeLiveQueryContext {
35193528 receivedInit : boolean ;
35203529 permissionMode : PermissionMode ;
35213530 stopping : boolean ;
3531+ terminalFailure : OrchestrationV2ProviderFailure | undefined ;
3532+ // Registry entries still running when this process opened. Their process
3533+ // is gone and never reports their end; any later task_started replaces the
3534+ // entry, so an entry still in this set runs nowhere.
35223535 readonly subagentsFromEarlierProcesses : ReadonlySet < ActiveClaudeSubagent > ;
35233536}
35243537
@@ -7031,10 +7044,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
70317044 ...( interrupted
70327045 ? { }
70337046 : {
7034- failure : makeProviderFailure ( {
7035- cause : cause === undefined ? undefined : Cause . squash ( cause ) ,
7036- class : "transport_error" ,
7037- } ) ,
7047+ failure :
7048+ closedQuery . terminalFailure ??
7049+ makeProviderFailure ( {
7050+ cause : cause === undefined ? undefined : Cause . squash ( cause ) ,
7051+ class : "transport_error" ,
7052+ } ) ,
70387053 } ) ,
70397054 } ) ;
70407055 } else {
@@ -8724,6 +8739,98 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
87248739 ) ,
87258740 ) ;
87268741
8742+ const stalledPhaseFailure = ( kind : ClaudeStalledPhaseKind ) =>
8743+ makeProviderFailure ( {
8744+ class : "transport_error" ,
8745+ code : NATIVE_SESSION_STALLED_FAILURE_CODE ,
8746+ message :
8747+ kind === "compaction"
8748+ ? "Claude context compaction made no progress for two minutes. T3 closed the stalled session; the next turn will continue in a fresh Claude session."
8749+ : "Claude returned no model or tool activity for two minutes. T3 closed the stalled session; the next turn will continue in a fresh Claude session." ,
8750+ retryable : true ,
8751+ } ) ;
8752+
8753+ const closeStalledQuery = Effect . fn ( "ClaudeAdapterV2.closeStalledQuery" ) ( function * (
8754+ context : ActiveClaudeTurnContext ,
8755+ liveQuery : ClaudeLiveQueryContext ,
8756+ phase : ClaudeStalledPhase ,
8757+ ) {
8758+ if (
8759+ ( yield * Ref . get ( activeTurn ) ) !== context ||
8760+ ( yield * Ref . get ( queryContext ) ) !== liveQuery ||
8761+ context . stalledPhase !== phase
8762+ ) {
8763+ return ;
8764+ }
8765+ context . stalledPhase = null ;
8766+ liveQuery . terminalFailure = stalledPhaseFailure ( phase . kind ) ;
8767+ yield * Effect . logWarning ( "orchestration-v2.claude-stalled-phase-timeout" , {
8768+ providerSessionId : input . providerSessionId ,
8769+ providerThreadId : context . input . providerThread . id ,
8770+ providerTurnId : context . providerTurnId ,
8771+ nativeThreadId : context . nativeThreadId ,
8772+ phase : phase . kind ,
8773+ } ) ;
8774+ const closed = yield * liveQuery . query . close . pipe (
8775+ Effect . ignore ,
8776+ Effect . andThen ( Deferred . await ( liveQuery . closed ) ) ,
8777+ Effect . timeoutOption ( "10 seconds" ) ,
8778+ ) ;
8779+ if ( Option . isSome ( closed ) ) return ;
8780+
8781+ yield * Effect . logWarning ( "orchestration-v2.claude-stalled-query-close-timeout" , {
8782+ providerSessionId : input . providerSessionId ,
8783+ providerThreadId : context . input . providerThread . id ,
8784+ providerTurnId : context . providerTurnId ,
8785+ nativeThreadId : context . nativeThreadId ,
8786+ phase : phase . kind ,
8787+ } ) ;
8788+ const ownsLiveQuery = yield * Ref . modify ( queryContext , ( current ) =>
8789+ current ?. query === liveQuery . query ? [ true , null ] : [ false , current ] ,
8790+ ) ;
8791+ if ( ownsLiveQuery ) {
8792+ yield * finalizeActiveTurnAfterQueryExit ( liveQuery ) ;
8793+ yield * endSubagentCallsOfClosedQuery ( liveQuery . nativeThreadId , "failed" ) ;
8794+ }
8795+ yield * Deferred . succeed ( liveQuery . closed , undefined ) ;
8796+ } ) ;
8797+
8798+ // This runs for every SDK frame, including streaming events; avoid a span per frame.
8799+ const observeClaudeStalledPhase = Effect . fnUntraced ( function * ( observation : {
8800+ readonly query : ClaudeAgentSdkQuerySession ;
8801+ readonly message : SDKMessage ;
8802+ } ) {
8803+ const context = yield * Ref . get ( activeTurn ) ;
8804+ const liveQuery = yield * Ref . get ( queryContext ) ;
8805+ if ( context === null || liveQuery ?. query !== observation . query ) return ;
8806+ const message = observation . message ;
8807+ const kind : ClaudeStalledPhaseKind | null =
8808+ message . type === "system" &&
8809+ message . subtype === "status" &&
8810+ message . status === "compacting"
8811+ ? "compaction"
8812+ : ( message . type === "system" &&
8813+ message . subtype === "status" &&
8814+ message . status === "requesting" ) ||
8815+ ( message . type === "system" && message . subtype === "api_retry" )
8816+ ? "request"
8817+ : null ;
8818+ if ( kind === null ) {
8819+ if ( message . type === "rate_limit_event" ) return ;
8820+ context . stalledPhase = null ;
8821+ return ;
8822+ }
8823+ if ( context . stalledPhase ?. kind === kind ) return ;
8824+
8825+ const phase : ClaudeStalledPhase = { kind } ;
8826+ context . stalledPhase = phase ;
8827+ yield * Effect . sleep ( CLAUDE_STALLED_PHASE_TIMEOUT ) . pipe (
8828+ Effect . andThen ( closeStalledQuery ( context , liveQuery , phase ) ) ,
8829+ Effect . forkIn ( sessionScope ) ,
8830+ ) ;
8831+ } ) ;
8832+
8833+ // Gives held root output to the pending prompt turn.
87278834 const releaseHeldRootFrames = Effect . fnUntraced ( function * (
87288835 context : ActiveClaudeTurnContext ,
87298836 ) {
@@ -8743,6 +8850,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
87438850 readonly message : SDKMessage ;
87448851 } ) {
87458852 const message = input . message ;
8853+ yield * observeClaudeStalledPhase ( input ) ;
87468854 const context = yield * Ref . get ( activeTurn ) ;
87478855 const liveQuery = yield * Ref . get ( queryContext ) ;
87488856 if (
@@ -9413,6 +9521,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
94139521 receivedInit : false ,
94149522 permissionMode : queryOptions . permissionMode ,
94159523 stopping : false ,
9524+ terminalFailure : undefined ,
94169525 subagentsFromEarlierProcesses : new Set (
94179526 [ ...( yield * Ref . get ( sessionSubagentsByTaskId ) ) . values ( ) ] . filter (
94189527 ( subagent ) => subagent . task . status === "running" ,
@@ -9580,6 +9689,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
95809689 nativeWakeStarted : false ,
95819690 gatedFramesBeforeEcho : 0 ,
95829691 heldRootFrames : [ ] ,
9692+ stalledPhase : null ,
95839693 } ;
95849694 const attachTurn = Effect . gen ( function * ( ) {
95859695 querySession . lastTurn = context ;
0 commit comments