openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
122 lines (121 loc) • 5.17 kB
JavaScript
import { m as readNonBlankString } from "./string-coerce-CCIiB4H7.mjs";
//#region packages/normalization-core/src/agent-run-terminal-outcome.ts
const AGENT_RUN_ABORTED_STOP_REASON = "aborted";
const AGENT_RUN_RESTART_ABORT_STOP_REASON = "restart";
const AGENT_RUN_SUPERSEDED_STOP_REASON = "superseded";
const AGENT_RUN_TIMEOUT_PHASES = [
"queue",
"preflight",
"provider",
"tool_execution",
"post_turn",
"gateway_draining"
];
const HARD_TIMEOUT_PHASES = /* @__PURE__ */ new Set([
"preflight",
"provider",
"tool_execution",
"post_turn"
]);
const AGENT_RUN_TERMINAL_CLASSIFICATION = {
completed: "success",
hard_timeout: "timeout",
timed_out: "timeout",
superseded: "cancellation",
cancelled: "cancellation",
aborted: "cancellation",
blocked: "failure",
abandoned: "failure",
failed: "failure"
};
function normalizeAgentRunTimeoutPhase(e) {
if (typeof e !== "string") return;
const t = e.trim();
return AGENT_RUN_TIMEOUT_PHASES.find((e) => e === t);
}
function normalizeProviderStarted(e) {
return typeof e === "boolean" ? e : void 0;
}
function isAbortedAgentStopReason(e) {
return e === "aborted" || e === "restart";
}
function classifyAgentRunTerminalOutcome(e) {
return AGENT_RUN_TERMINAL_CLASSIFICATION[e.reason];
}
function isCancellationStopReason(e) {
return e === "rpc" || e === "stop";
}
/** Interprets terminal metadata without runtime abort, error formatting, or storage ownership. */
function resolveAgentRunTerminalFacts(e) {
const t = readNonBlankString(e.stopReason);
const n = readNonBlankString(e.livenessState);
const r = normalizeAgentRunTimeoutPhase(e.timeoutPhase);
const i = normalizeProviderStarted(e.providerStarted);
const a = t === AGENT_RUN_RESTART_ABORT_STOP_REASON;
const o = r !== void 0 && HARD_TIMEOUT_PHASES.has(r) || !a && e.status === "timeout" && i === true;
const s = a || e.status !== "ok" && isCancellationStopReason(t);
const c = n?.trim().toLowerCase();
const l = o ? "hard_timeout" : t === "superseded" ? "superseded" : c === "blocked" ? "blocked" : isAbortedAgentStopReason(t) && !a ? "aborted" : s ? "cancelled" : c === "abandoned" ? "abandoned" : e.status === "timeout" ? "timed_out" : e.status === "error" ? "failed" : "completed";
return {
reason: l,
status: l === "completed" ? "ok" : l === "hard_timeout" || l === "timed_out" ? "timeout" : "error",
...t ? { stopReason: t } : {},
...n ? { livenessState: n } : {},
...r ? { timeoutPhase: r } : {},
...i !== void 0 ? { providerStarted: i } : {}
};
}
/** Applies lifecycle flags before interpreting the same canonical terminal metadata. */
function resolveAgentRunLifecycleTerminalFacts(e) {
const { data: t, abortFields: n } = e;
const r = readNonBlankString(t?.stopReason) ?? n?.stopReason;
const i = normalizeAgentRunTimeoutPhase(t?.timeoutPhase);
const a = readNonBlankString(t?.status)?.toLowerCase();
const o = r === "timeout" || i !== void 0 || a === "timeout" || a === "timed_out";
const s = t?.aborted === true || n?.aborted === true || a === "aborted";
const c = a === "cancelled" || a === "canceled" || a === "aborted" || a === "superseded";
const l = c || s;
const u = e.phase === "error" || a === "error" || a === "failed" || r === "error";
const d = !o && l && !isAbortedAgentStopReason(r) && !isCancellationStopReason(r) && r !== "superseded" && (r === void 0 || c) ? s ? AGENT_RUN_ABORTED_STOP_REASON : "stop" : r;
const f = resolveAgentRunTerminalFacts({
status: o ? "timeout" : l || u ? "error" : "ok",
stopReason: d,
livenessState: t?.livenessState,
timeoutPhase: i,
providerStarted: t?.providerStarted
});
return r && f.stopReason !== r ? {
...f,
stopReason: r
} : f;
}
/** Reads reduced terminal wait snapshots; pending/deadline admission belongs to the caller. */
function resolveAgentRunWaitTerminalFacts(e) {
const t = e.status;
if (t !== "ok" && t !== "error" && t !== "timeout") return;
const n = resolveAgentRunTerminalFacts({
...e,
status: t
});
return t !== "ok" && n.stopReason === "auth-revoked" && (n.reason === "failed" || n.reason === "timed_out" || n.reason === "abandoned") ? {
...n,
reason: "aborted",
status: "error"
} : n;
}
/** Reads the execution owner's publication fact; it grants no runtime authority. */
function hasExecutionSettlement(e) {
return e?.executionSettled === true;
}
/** Distinguishes terminal outcomes from errors that can still be followed by a retry. */
function isDefinitiveRunLifecycle(e) {
if (e.phase === "end") return true;
if (e.phase !== "error") return false;
const t = resolveAgentRunLifecycleTerminalFacts({
phase: "error",
data: e.data
});
return hasExecutionSettlement(e.data) || e.data?.fallbackExhaustedFailure === true || t.reason !== "failed";
}
//#endregion
export { hasExecutionSettlement as a, normalizeAgentRunTimeoutPhase as c, resolveAgentRunTerminalFacts as d, resolveAgentRunWaitTerminalFacts as f, classifyAgentRunTerminalOutcome as i, normalizeProviderStarted as l, AGENT_RUN_RESTART_ABORT_STOP_REASON as n, isAbortedAgentStopReason as o, AGENT_RUN_SUPERSEDED_STOP_REASON as r, isDefinitiveRunLifecycle as s, AGENT_RUN_ABORTED_STOP_REASON as t, resolveAgentRunLifecycleTerminalFacts as u };