UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.15 kB
import{h as isFailoverError}from"./error-oegzzI05.mjs";import{r as formatErrorMessageForDisplay}from"./error-diagnostics-BkK6-KHH.mjs";import"./failover-error-DWH-eolC.mjs";import{a as isCliSessionInvalidatingFailoverReason}from"./cli-session-DVohIxV9.mjs";import{n as cliBackendLog}from"./log-CF03K_DW.mjs"; //!src/agents/cli-runner/cli-run-recovery.ts function resolveCliSessionId(e){return e.mode===`reuse`||e.mode===`reuse-with-drift`?e.sessionId:void 0}const FRESH_SESSION_RECOVERY_CODES={unknown:`cli_unknown_empty_failure`,empty_response:`cli_unknown_empty_failure`,format:`cli_synthetic_no_response`,timeout:`cli_no_output_timeout`,context_overflow:`cli_context_overflow`};function shouldRetryFreshCliSessionAfterFailover(e){if(!e.hasHistoryPrompt)return false;if(e.recoveryPolicy===`invalidated-only`&&!isCliSessionInvalidatingFailoverReason(e.error.reason))return false;const t=FRESH_SESSION_RECOVERY_CODES[e.error.reason];return e.error.reason===`session_expired`||t!==void 0&&e.error.code===t}function remainingCliRecoveryBudgetMs(e,t){return Math.floor(e-(performance.now()-t))}async function runCliRecovery(e){const{context:t}=e;const n=t.params;const r=resolveCliSessionId(t.reusableCliSession);const i=n.cliSessionBinding?.resumeCheckpointId;let a=r;const onForkSuccessorPersisted=e=>{a=e};const logFailure=(e,r)=>cliBackendLog.warn(`cli ${e} failure: provider=${n.provider} model=${t.modelId} durationMs=${Date.now()-t.started} runId=${n.runId} error=${formatErrorMessageForDisplay(r)}`);const failTerminal=async t=>{logFailure(`terminal`,t);await e.onTerminalFailure(t);throw t};const finishDeliveredFailure=async t=>{const n=await e.finishDeliveredFailure(t,a!==r);if(n)logFailure(`delivered`,t);return n};try{return await e.finishAttempt(await e.executeAttempt(r,n.forkCliSessionOnResume?{onForkSuccessorPersisted}:void 0),r)}catch(r){const o=await finishDeliveredFailure(r);if(o)return o;n.assertCurrent?.();let s=r;for(const o of[true,false]){if(!isFailoverError(s)||!a||!n.sessionKey)break;const c=o?n.onBeforeForkedCliSessionRetry:n.onBeforeFreshCliSessionRetry;if(!(o?!n.forkCliSessionOnResume&&s.reason===`timeout`&&s.code===`cli_no_output_timeout`&&i&&t.preparedBackend.backend.forkArg&&t.preparedBackend.backend.resumeAtArg&&c:shouldRetryFreshCliSessionAfterFailover({error:s,hasHistoryPrompt:Boolean(t.openClawHistoryPrompt),recoveryPolicy:t.preparedBackend.backend.freshSessionRecovery})))continue;try{const r=remainingCliRecoveryBudgetMs(n.timeoutMs,t.startedMonotonicMs);if(r<=0)throw s;if(c&&!await c.call(n,{provider:n.provider,reason:s.reason,sessionId:a}))throw s;cliBackendLog.warn(`cli session recovery ${o?`fork`:`retry`}: provider=${n.provider} reason=${s.reason} sessionKey=${n.sessionKey}`);return await e.finishAttempt(await e.executeAttempt(o?a:void 0,{timeoutMs:r,forkCliSessionOnResume:o,...o?{resumeAt:i,onForkSuccessorPersisted}:{}}))}catch(e){const t=await finishDeliveredFailure(e);if(t)return t;if(!o)return await failTerminal(e);n.assertCurrent?.();s=isFailoverError(e)&&e.code===`cli_resume_at_unsupported`?r:e}}return await failTerminal(s)}}export{runCliRecovery as n,resolveCliSessionId as t};