openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
106 lines (105 loc) • 4.31 kB
JavaScript
import { i as FailoverError, u as isFailoverError } from "./error-BCRqSFIY.mjs";
import { r as formatErrorMessageForDisplay } from "./error-diagnostics-CD3FocGA.mjs";
import "./failover-error-CbIiC30n.mjs";
import { a as isCliSessionInvalidatingFailoverReason } from "./cli-session-BsEOjZRc.mjs";
import { n as cliBackendLog } from "./log-C54MYINf.mjs";
//#region 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;
}
/**
* Remaining retry budget measured against the run's monotonic anchor. Elapsed
* monotonic time is fractional, so the result is floored to a whole millisecond:
* this keeps the retry within the operator-configured budget and satisfies the
* paired-node remote decoder's `Number.isInteger` timeout contract.
*/
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 failTerminal = async (r) => {
cliBackendLog.warn(`cli terminal failure: provider=${n.provider} model=${t.modelId} durationMs=${Date.now() - t.started} runId=${n.runId} error=${formatErrorMessageForDisplay(r)}`);
await e.onTerminalFailure(r);
throw r;
};
try {
return await e.finishAttempt(await e.executeAttempt(r, n.forkCliSessionOnResume ? { onForkSuccessorPersisted } : void 0), r);
} catch (o) {
const s = await e.finishDeliveredFailure(o, a !== r);
if (s) return s;
n.assertCurrent?.();
let c = o;
for (const s of [true, false]) {
if (!isFailoverError(c) || !a || !n.sessionKey) break;
const l = s ? n.onBeforeForkedCliSessionRetry : n.onBeforeFreshCliSessionRetry;
if (!(s ? !n.forkCliSessionOnResume && c.reason === "timeout" && c.code === "cli_no_output_timeout" && i && t.preparedBackend.backend.forkArg && t.preparedBackend.backend.resumeAtArg && l : shouldRetryFreshCliSessionAfterFailover({
error: c,
hasHistoryPrompt: Boolean(t.openClawHistoryPrompt),
recoveryPolicy: t.preparedBackend.backend.freshSessionRecovery
}))) continue;
try {
const r = remainingCliRecoveryBudgetMs(n.timeoutMs, t.startedMonotonicMs);
if (r <= 0) throw c;
if (l && !await l.call(n, {
provider: n.provider,
reason: c.reason,
sessionId: a
})) throw c;
cliBackendLog.warn(`cli session recovery ${s ? "fork" : "retry"}: provider=${n.provider} reason=${c.reason} sessionKey=${n.sessionKey}`);
return await e.finishAttempt(await e.executeAttempt(s ? a : void 0, {
timeoutMs: r,
forkCliSessionOnResume: s,
...s ? {
resumeAt: i,
onForkSuccessorPersisted
} : {}
}));
} catch (t) {
const i = await e.finishDeliveredFailure(t, a !== r);
if (i) return i;
if (!s) return await failTerminal(t);
n.assertCurrent?.();
c = isFailoverError(t) && t.code === "cli_resume_at_unsupported" ? o : t;
}
}
return await failTerminal(c);
}
}
//#endregion
//#region src/agents/cli-runner/auth-profile-preparation-error.ts
/** Typed terminal fact for a selected profile that fails before CLI spawn. */
var CliAuthProfilePreparationError = class extends FailoverError {
constructor(e) {
super(e.message, {
reason: "auth",
provider: e.provider,
profileId: e.profileId,
cause: e.cause
});
this.name = "CliAuthProfilePreparationError";
this.agentDir = e.agentDir;
}
};
//#endregion
export { resolveCliSessionId as n, runCliRecovery as r, CliAuthProfilePreparationError as t };