@@ -56,6 +56,7 @@ import {
5656 requestHeartbeatMock ,
5757 supervisorSpawnMock ,
5858} from "./cli-runner.test-support.js" ;
59+ import { runCliRecovery } from "./cli-runner/cli-run-recovery.js" ;
5960import { executePreparedCliRun } from "./cli-runner/execute.js" ;
6061import {
6162 resolveCliNoOutputTimeoutMs ,
@@ -65,6 +66,7 @@ import { prepareCliRunContext } from "./cli-runner/prepare.js";
6566import { hashCliReseedPrompt } from "./cli-runner/reseed-envelope.js" ;
6667import * as sessionHistoryModule from "./cli-runner/session-history.js" ;
6768import type { PreparedCliRunContext } from "./cli-runner/types.js" ;
69+ import { FailoverError } from "./failover-error.js" ;
6870import { runAgentHarnessBeforeMessageWriteHook } from "./harness/hook-helpers.js" ;
6971import { MAX_AGENT_HOOK_HISTORY_MESSAGES } from "./harness/hook-history.js" ;
7072
@@ -2092,6 +2094,130 @@ describe("runCliAgent reliability", () => {
20922094 expect ( clearBeforeRetry ) . not . toHaveBeenCalled ( ) ;
20932095 } ) ;
20942096
2097+ it ( "does not start a fresh CLI attempt when format recovery retains the binding" , async ( ) => {
2098+ supervisorSpawnMock . mockClear ( ) ;
2099+ supervisorSpawnMock . mockResolvedValueOnce (
2100+ makeManagedRun ( {
2101+ stdout : [
2102+ JSON . stringify ( {
2103+ type : "assistant" ,
2104+ message : {
2105+ model : "<synthetic>" ,
2106+ content : [ { type : "text" , text : "No response requested." } ] ,
2107+ } ,
2108+ } ) ,
2109+ JSON . stringify ( { type : "result" , subtype : "success" , result : "" } ) ,
2110+ ] . join ( "\n" ) ,
2111+ } ) ,
2112+ ) ;
2113+ const clearBeforeRetry = vi . fn ( async ( ) => false ) ;
2114+ const { dir, sessionFile } = createSessionFile ( {
2115+ history : [ { role : "user" , content : "earlier context" } ] ,
2116+ } ) ;
2117+
2118+ try {
2119+ const context = makeClaudePreparedContext ( {
2120+ sessionKey : "agent:main:subagent:retained-format" ,
2121+ runId : "run-retained-format" ,
2122+ cliSessionId : "retained-cli-session" ,
2123+ openClawHistoryPrompt : CLI_RESEED_PROMPT ,
2124+ } ) ;
2125+ context . preparedBackend . backend = {
2126+ ...context . preparedBackend . backend ,
2127+ freshSessionRecovery : "invalidated-only" ,
2128+ output : "jsonl" ,
2129+ input : "stdin" ,
2130+ jsonlDialect : "claude-stream-json" ,
2131+ } ;
2132+ context . backendResolved . config = context . preparedBackend . backend ;
2133+
2134+ await expect (
2135+ runPreparedCliAgent ( {
2136+ ...context ,
2137+ params : {
2138+ ...context . params ,
2139+ agentId : "main" ,
2140+ sessionFile,
2141+ workspaceDir : dir ,
2142+ onBeforeFreshCliSessionRetry : clearBeforeRetry ,
2143+ } ,
2144+ } ) ,
2145+ ) . rejects . toMatchObject ( { reason : "format" , code : "cli_synthetic_no_response" } ) ;
2146+
2147+ expect ( supervisorSpawnMock ) . toHaveBeenCalledTimes ( 1 ) ;
2148+ expect ( clearBeforeRetry ) . not . toHaveBeenCalled ( ) ;
2149+ } finally {
2150+ fs . rmSync ( dir , { recursive : true , force : true } ) ;
2151+ }
2152+ } ) ;
2153+
2154+ it . each ( [
2155+ [ "format" , "cli_synthetic_no_response" ] ,
2156+ [ "timeout" , "cli_no_output_timeout" ] ,
2157+ ] as const ) (
2158+ "keeps undefined Gemini recovery policy compatible after %s failover" ,
2159+ async ( reason , code ) => {
2160+ const context = buildPreparedContext ( {
2161+ provider : "google-gemini-cli" ,
2162+ sessionKey : `agent:main:gemini-${ reason } ` ,
2163+ cliSessionId : "gemini-resumed-session" ,
2164+ openClawHistoryPrompt : CLI_RESEED_PROMPT ,
2165+ } ) ;
2166+ context . preparedBackend . backend = {
2167+ command : "gemini" ,
2168+ args : [ "--prompt" , "{prompt}" ] ,
2169+ resumeArgs : [ "--resume" , "{sessionId}" , "--prompt" , "{prompt}" ] ,
2170+ output : "jsonl" ,
2171+ jsonlDialect : "gemini-stream-json" ,
2172+ input : "arg" ,
2173+ sessionMode : "existing" ,
2174+ } ;
2175+ context . backendResolved . config = context . preparedBackend . backend ;
2176+ expect ( context . preparedBackend . backend . freshSessionRecovery ) . toBeUndefined ( ) ;
2177+
2178+ const executeAttempt = vi
2179+ . fn ( )
2180+ . mockRejectedValueOnce (
2181+ new FailoverError ( `Gemini ${ reason } failure` , {
2182+ reason,
2183+ code,
2184+ provider : "google-gemini-cli" ,
2185+ model : "gemini-3.1-pro-preview" ,
2186+ } ) ,
2187+ )
2188+ . mockResolvedValueOnce ( { sessionId : `gemini-fresh-${ reason } ` } ) ;
2189+ const clearBeforeRetry = vi . fn ( async ( ) => true ) ;
2190+
2191+ const result = await runCliRecovery ( {
2192+ context : {
2193+ ...context ,
2194+ params : {
2195+ ...context . params ,
2196+ onBeforeFreshCliSessionRetry : clearBeforeRetry ,
2197+ } ,
2198+ } ,
2199+ executeAttempt,
2200+ finishAttempt : async ( attempt : { sessionId : string } ) =>
2201+ ( {
2202+ payloads : [ { text : "Gemini recovered" } ] ,
2203+ meta : { cliSessionId : attempt . sessionId } ,
2204+ } ) as never ,
2205+ finishDeliveredFailure : async ( ) => undefined ,
2206+ onTerminalFailure : async ( ) => { } ,
2207+ } ) ;
2208+
2209+ expect ( executeAttempt ) . toHaveBeenCalledTimes ( 2 ) ;
2210+ expect ( executeAttempt . mock . calls [ 0 ] ?. [ 0 ] ) . toBe ( "gemini-resumed-session" ) ;
2211+ expect ( executeAttempt . mock . calls [ 1 ] ?. [ 0 ] ) . toBeUndefined ( ) ;
2212+ expect ( clearBeforeRetry ) . toHaveBeenCalledWith ( {
2213+ provider : "google-gemini-cli" ,
2214+ reason,
2215+ sessionId : "gemini-resumed-session" ,
2216+ } ) ;
2217+ expect ( requireRecord ( result . meta , "result meta" ) . cliSessionId ) . toBe ( `gemini-fresh-${ reason } ` ) ;
2218+ } ,
2219+ ) ;
2220+
20952221 it . each ( [ "timeout" , "unknown" , "context_overflow" , "format" ] as const ) (
20962222 "retries a fresh CLI session after recoverable %s failover without a failed agent_end" ,
20972223 async ( reason ) => {
@@ -4050,6 +4176,7 @@ describe("runCliAgent reliability", () => {
40504176 openClawHistoryPrompt :
40514177 "Continue this conversation using the OpenClaw transcript below.\n\nUser: recovered history\n\n<next_user_message>\nhi\n</next_user_message>" ,
40524178 } ) ;
4179+ context . preparedBackend . backend . freshSessionRecovery = "invalidated-only" ;
40534180 const clearBeforeRetry = vi . fn ( async ( ) => true ) ;
40544181
40554182 try {
0 commit comments