@@ -47,6 +47,7 @@ import {
4747 attachCliMessagingDeliveryEvidence ,
4848 getCliMessagingDeliveryEvidence ,
4949} from "./cli-runner/delivery-evidence.js" ;
50+ import { createCliFailoverError } from "./cli-runner/exit-error.js" ;
5051import { cliBackendLog , formatCliBackendOutputDigest } from "./cli-runner/log.js" ;
5152import { hashCliReseedPrompt } from "./cli-runner/reseed-envelope.js" ;
5253import {
@@ -63,13 +64,12 @@ import type {
6364 RunCliAgentParams ,
6465} from "./cli-runner/types.js" ;
6566import { claudeCliSessionTranscriptHasContent as claudeCliSessionTranscriptHasContentImpl } from "./command/attempt-execution.helpers.js" ;
66- import { classifyFailoverReason , isFailoverErrorMessage } from "./embedded-agent-helpers.js" ;
6767import type { EmbeddedAgentRunResult } from "./embedded-agent-runner.js" ;
6868import { waitForDeferredTurnMaintenanceForSession } from "./embedded-agent-runner/context-engine-maintenance.js" ;
6969import { resolveExplicitFinalSourceReplyDeliveryEvidence } from "./embedded-agent-runner/delivery-evidence.js" ;
7070import { resolveAuthProfileFailureReason } from "./embedded-agent-runner/run/auth-profile-failure-policy.js" ;
7171import { buildEmbeddedRunPayloads } from "./embedded-agent-runner/run/payloads.js" ;
72- import { FailoverError , isFailoverError , resolveFailoverStatus } from "./failover-error.js" ;
72+ import { coerceToFailoverError , FailoverError , isFailoverError } from "./failover-error.js" ;
7373import {
7474 awaitAgentEndSideEffects ,
7575 runAgentEndSideEffects ,
@@ -216,31 +216,10 @@ function isUnsupportedCliResumeAtError(error: unknown, resumeAtArg: string): boo
216216 const message = formatErrorMessage ( error ) . toLowerCase ( ) ;
217217 return (
218218 message . includes ( resumeAtArg . toLowerCase ( ) ) &&
219- [ "unknown" , "unexpected" , "unrecognized" , "not recognized" ] . some ( ( token ) =>
220- message . includes ( token ) ,
221- )
219+ / \b (?: u n k n o w n | u n e x p e c t e d | u n r e c o g n i z e d ) \b | \b n o t \s + r e c o g n i z e d \b / . test ( message )
222220 ) ;
223221}
224222
225- function formatCliEmptyOutputDiagnostics ( output : CliOutput ) : string | undefined {
226- const process = output . diagnostics ?. process ;
227- if ( ! process ) {
228- return undefined ;
229- }
230- return [
231- `backend=${ process . backendId } ` ,
232- `reason=${ process . processReason } ` ,
233- `exitCode=${ process . exitCode ?? "null" } ` ,
234- `exitSignal=${ process . exitSignal ?? "null" } ` ,
235- `durationMs=${ process . durationMs } ` ,
236- `stdoutBytes=${ process . stdoutBytes } ` ,
237- `stdoutHash=${ process . stdoutHash } ` ,
238- `stderrBytes=${ process . stderrBytes } ` ,
239- `stderrHash=${ process . stderrHash } ` ,
240- `useResume=${ process . useResume ? "true" : "false" } ` ,
241- ] . join ( " " ) ;
242- }
243-
244223/** Checks whether a Claude CLI session binding has reached its transcript file. */
245224export async function isCliBindingFlushed (
246225 sessionId : string | undefined ,
@@ -802,6 +781,12 @@ export async function runPreparedCliAgent(
802781) : Promise < EmbeddedAgentRunResult > {
803782 const { executePreparedCliRun } = await import ( "./cli-runner/execute.runtime.js" ) ;
804783 const { params } = context ;
784+ const cliFailoverContext = {
785+ provider : params . provider ,
786+ model : context . modelId ,
787+ sessionId : params . sessionId ,
788+ lane : params . lane ,
789+ } ;
805790 const sessionBindingDisabled = context . preparedBackend . backend . sessionMode === "none" ;
806791 const preparedContextAgentMeta =
807792 isClaudeCliProvider ( params . provider ) && context . contextWindowInfo
@@ -1145,26 +1130,6 @@ export async function runPreparedCliAgent(
11451130 }
11461131 } ;
11471132
1148- const toCliRunFailure = ( error : unknown ) : never => {
1149- if ( isFailoverError ( error ) ) {
1150- throw error ;
1151- }
1152- const message = formatErrorMessage ( error ) ;
1153- if ( isFailoverErrorMessage ( message , { provider : params . provider } ) ) {
1154- const reason = classifyFailoverReason ( message , { provider : params . provider } ) ?? "unknown" ;
1155- const status = resolveFailoverStatus ( reason ) ;
1156- throw new FailoverError ( message , {
1157- reason,
1158- provider : params . provider ,
1159- model : context . modelId ,
1160- sessionId : params . sessionId ,
1161- lane : params . lane ,
1162- status,
1163- } ) ;
1164- }
1165- throw error ;
1166- } ;
1167-
11681133 const executeCliAttempt = async (
11691134 cliSessionIdToUse ?: string ,
11701135 options ?: {
@@ -1223,18 +1188,28 @@ export async function runPreparedCliAgent(
12231188 ! output . didSendViaMessagingTool &&
12241189 params . allowEmptyAssistantReplyAsSilent !== true
12251190 ) {
1226- const emptyOutputDiagnostics = formatCliEmptyOutputDiagnostics ( output ) ;
1227- if ( emptyOutputDiagnostics ) {
1228- cliBackendLog . warn ( `cli empty response diagnostics: ${ emptyOutputDiagnostics } ` ) ;
1191+ const process = output . diagnostics ?. process ;
1192+ if ( process ) {
1193+ const diagnostics = [
1194+ `backend=${ process . backendId } ` ,
1195+ `reason=${ process . processReason } ` ,
1196+ `exitCode=${ process . exitCode ?? "null" } ` ,
1197+ `exitSignal=${ process . exitSignal ?? "null" } ` ,
1198+ `durationMs=${ process . durationMs } ` ,
1199+ `stdoutBytes=${ process . stdoutBytes } ` ,
1200+ `stdoutHash=${ process . stdoutHash } ` ,
1201+ `stderrBytes=${ process . stderrBytes } ` ,
1202+ `stderrHash=${ process . stderrHash } ` ,
1203+ `useResume=${ process . useResume ? "true" : "false" } ` ,
1204+ ] . join ( " " ) ;
1205+ cliBackendLog . warn ( `cli empty response diagnostics: ${ diagnostics } ` ) ;
12291206 }
12301207 throw attachCliMessagingDeliveryEvidence (
1231- new FailoverError ( "CLI backend returned an empty response." , {
1232- reason : "empty_response" ,
1233- provider : params . provider ,
1234- model : context . modelId ,
1235- sessionId : params . sessionId ,
1236- lane : params . lane ,
1237- } ) ,
1208+ createCliFailoverError (
1209+ "CLI backend returned an empty response." ,
1210+ "empty_response" ,
1211+ cliFailoverContext ,
1212+ ) ,
12381213 output ,
12391214 ) ;
12401215 }
@@ -1673,15 +1648,12 @@ export async function runPreparedCliAgent(
16731648 context . preparedBackend . backend . resumeAtArg &&
16741649 isUnsupportedCliResumeAtError ( err , context . preparedBackend . backend . resumeAtArg )
16751650 ) {
1676- recoveryError = new FailoverError ( "CLI backend cannot resume from the stored checkpoint." , {
1677- reason : "session_expired" ,
1678- provider : params . provider ,
1679- model : context . modelId ,
1680- sessionId : params . sessionId ,
1681- lane : params . lane ,
1682- status : resolveFailoverStatus ( "session_expired" ) ,
1683- cause : err ,
1684- } ) ;
1651+ recoveryError = createCliFailoverError (
1652+ "CLI backend cannot resume from the stored checkpoint." ,
1653+ "session_expired" ,
1654+ cliFailoverContext ,
1655+ { cause : err } ,
1656+ ) ;
16851657 }
16861658 if ( isFailoverError ( recoveryError ) ) {
16871659 if (
@@ -1777,7 +1749,7 @@ export async function runPreparedCliAgent(
17771749 ctx : hookContext ,
17781750 hookRunner,
17791751 } ) ;
1780- return toCliRunFailure ( retryErr ) ;
1752+ throw retryErr ;
17811753 }
17821754 }
17831755 }
@@ -1795,7 +1767,7 @@ export async function runPreparedCliAgent(
17951767 ctx : hookContext ,
17961768 hookRunner,
17971769 } ) ;
1798- return toCliRunFailure ( recoveryError ) ;
1770+ throw recoveryError ;
17991771 }
18001772 } ;
18011773
@@ -1825,7 +1797,7 @@ export async function runPreparedCliAgent(
18251797 ) ;
18261798 }
18271799 if ( runFailed ) {
1828- throw runError ;
1800+ throw coerceToFailoverError ( runError , cliFailoverContext ) ?? runError ;
18291801 }
18301802 if ( ! runResult ) {
18311803 throw new Error ( "CLI run completed without a result" ) ;
0 commit comments