11import type { ThinkLevel } from "../../../auto-reply/thinking.js" ;
22import type { AssistantMessage } from "../../../llm/types.js" ;
3+ import { projectAgentRunAttemptTerminal } from "../../agent-run-terminal-outcome.js" ;
34import type { AuthProfileFailureReason , AuthProfileStore } from "../../auth-profiles.js" ;
45import {
56 classifyAssistantFailoverReason ,
@@ -24,6 +25,10 @@ import { createFailoverDecisionLogger } from "./failover-observation.js";
2425import { resolveRunFailoverDecision } from "./failover-policy.js" ;
2526import { shouldRetrySilentErrorAssistantTurn } from "./incomplete-turn.js" ;
2627import type { RunEmbeddedAgentParams } from "./params.js" ;
28+ import {
29+ isEmbeddedRunTerminalInterrupted ,
30+ type EmbeddedRunTerminalState ,
31+ } from "./terminal-outcome.js" ;
2732import type { EmbeddedRunAttemptResult } from "./types.js" ;
2833
2934const MAX_EMPTY_ERROR_RETRIES = 3 ;
@@ -46,9 +51,7 @@ export async function handleEmbeddedAssistantFailure(input: {
4651 attempt : EmbeddedRunAttemptResult ;
4752 attemptAssistant ?: AssistantMessage ;
4853 currentAttemptAssistant ?: AssistantMessage ;
49- terminalProviderStarted : boolean ;
50- terminalInterrupted : boolean ;
51- promptError : unknown ;
54+ terminalState : EmbeddedRunTerminalState ;
5255 activeErrorContext : { provider : string ; model : string } ;
5356 provider : string ;
5457 modelId : string ;
@@ -57,14 +60,6 @@ export async function handleEmbeddedAssistantFailure(input: {
5760 // Profile rotation resets thinking inside the runtime; read it after advancing.
5861 getThinkLevel : ( ) => ThinkLevel ;
5962 attemptedThinking : Set < ThinkLevel > ;
60- timedOut : boolean ;
61- idleTimedOut : boolean ;
62- timedOutDuringCompaction : boolean ;
63- timedOutDuringToolExecution : boolean ;
64- timedOutByRunBudget : boolean ;
65- signalOwnedInterruption : boolean ;
66- externalAbort : boolean ;
67- aborted : boolean ;
6863 fallbackConfigured : boolean ;
6964 pluginHarnessOwnsTransport : boolean ;
7065 canRestartForLiveSwitch : boolean ;
@@ -100,11 +95,23 @@ export async function handleEmbeddedAssistantFailure(input: {
10095 agentDir : string ;
10196 isProbeSession : boolean ;
10297} ) : Promise < EmbeddedRunAssistantFailureOutcome > {
98+ const {
99+ aborted,
100+ externalAbort,
101+ idleTimedOut,
102+ promptError,
103+ timedOut,
104+ timedOutByRunBudget,
105+ timedOutDuringCompaction,
106+ timedOutDuringToolExecution,
107+ } = projectAgentRunAttemptTerminal ( input . attempt . terminal ) ;
108+ const terminalInterrupted = isEmbeddedRunTerminalInterrupted ( input . terminalState . outcome ) ;
109+ const { signalOwnedInterruption } = input . terminalState ;
103110 const fallbackThinking = pickFallbackThinkingLevel ( {
104111 message : input . attemptAssistant ?. errorMessage ,
105112 attempted : input . attemptedThinking ,
106113 } ) ;
107- if ( fallbackThinking && ! input . terminalInterrupted ) {
114+ if ( fallbackThinking && ! terminalInterrupted ) {
108115 log . warn (
109116 `unsupported thinking level for ${ input . provider } /${ input . modelId } ; retrying with ${ fallbackThinking } ` ,
110117 ) ;
@@ -122,10 +129,11 @@ export async function handleEmbeddedAssistantFailure(input: {
122129 const failoverFailure = isFailoverAssistantError ( input . attemptAssistant ) ;
123130 const assistantFailoverReason = classifyAssistantFailoverReason ( input . attemptAssistant ) ;
124131 const assistantProviderStarted =
125- Boolean ( input . currentAttemptAssistant ?. provider ) || input . terminalProviderStarted ;
132+ Boolean ( input . currentAttemptAssistant ?. provider ) ||
133+ input . terminalState . outcome . providerStarted === true ;
126134 const assistantProfileFailoverReason =
127135 assistantFailoverReason ??
128- ( assistantProviderStarted && ( input . timedOut || input . idleTimedOut ) ? "timeout" : null ) ;
136+ ( assistantProviderStarted && ( timedOut || idleTimedOut ) ? "timeout" : null ) ;
129137 const assistantProfileFailureReason = input . resolveAuthProfileFailureReason (
130138 assistantProfileFailoverReason ,
131139 {
@@ -154,8 +162,8 @@ export async function handleEmbeddedAssistantFailure(input: {
154162 ! billingFailure &&
155163 ! cloudCodeAssistFormatError &&
156164 ! imageDimensionError &&
157- ! input . terminalInterrupted &&
158- ! input . promptError &&
165+ ! terminalInterrupted &&
166+ ! promptError &&
159167 silentErrorRetryReason &&
160168 shouldRetrySilentErrorAssistantTurn ( {
161169 attempt : input . attempt ,
@@ -192,11 +200,11 @@ export async function handleEmbeddedAssistantFailure(input: {
192200 sourceModel : input . attemptAssistant ?. model ?? input . modelId ,
193201 profileId : failedProfileId ,
194202 fallbackConfigured : input . fallbackConfigured ,
195- timedOut : input . timedOut ,
196- aborted : input . aborted ,
203+ timedOut,
204+ aborted,
197205 } ) ;
198206 if (
199- ! input . signalOwnedInterruption &&
207+ ! signalOwnedInterruption &&
200208 authFailure &&
201209 ( await input . maybeRefreshRuntimeAuthForAuthError (
202210 input . attemptAssistant ?. errorMessage ?? "" ,
@@ -232,35 +240,35 @@ export async function handleEmbeddedAssistantFailure(input: {
232240 const initialDecision = resolveRunFailoverDecision ( {
233241 stage : "assistant" ,
234242 allowFormatRetry : cloudCodeAssistFormatError ,
235- aborted : input . aborted ,
236- externalAbort : input . externalAbort || input . signalOwnedInterruption ,
243+ aborted,
244+ externalAbort : externalAbort || signalOwnedInterruption ,
237245 fallbackConfigured : input . fallbackConfigured ,
238246 failoverFailure,
239247 failoverReason : assistantFailoverReason ,
240- timedOut : input . timedOut ,
241- idleTimedOut : input . idleTimedOut ,
242- timedOutDuringCompaction : input . timedOutDuringCompaction ,
243- timedOutDuringToolExecution : input . timedOutDuringToolExecution ,
248+ timedOut,
249+ idleTimedOut,
250+ timedOutDuringCompaction,
251+ timedOutDuringToolExecution,
244252 harnessOwnsTransport : input . pluginHarnessOwnsTransport ,
245- timedOutByRunBudget : input . timedOutByRunBudget ,
253+ timedOutByRunBudget,
246254 profileRotated : false ,
247255 } ) ;
248256 const outcome = await handleAssistantFailover ( {
249257 initialDecision,
250- aborted : input . aborted ,
251- externalAbort : input . externalAbort || input . signalOwnedInterruption ,
258+ aborted,
259+ externalAbort : externalAbort || signalOwnedInterruption ,
252260 fallbackConfigured : input . fallbackConfigured ,
253261 failoverFailure,
254262 failoverReason : assistantFailoverReason ,
255- timedOut : input . timedOut ,
256- idleTimedOut : input . idleTimedOut ,
257- timedOutDuringCompaction : input . timedOutDuringCompaction ,
258- timedOutDuringToolExecution : input . timedOutDuringToolExecution ,
259- timedOutByRunBudget : input . timedOutByRunBudget ,
263+ timedOut,
264+ idleTimedOut,
265+ timedOutDuringCompaction,
266+ timedOutDuringToolExecution,
267+ timedOutByRunBudget,
260268 allowSameModelIdleTimeoutRetry :
261- input . timedOut &&
262- input . idleTimedOut &&
263- ! input . timedOutDuringCompaction &&
269+ timedOut &&
270+ idleTimedOut &&
271+ ! timedOutDuringCompaction &&
264272 ! input . fallbackConfigured &&
265273 input . canRestartForLiveSwitch &&
266274 input . sameModelIdleTimeoutRetries < MAX_SAME_MODEL_IDLE_TIMEOUT_RETRIES ,
0 commit comments