openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
304 lines (303 loc) • 12.9 kB
JavaScript
import { m as readNonBlankString } from "./string-coerce-CCIiB4H7.mjs";
import { s as asFiniteNumber } from "./number-coercion-D6u-4YD8.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { d as resolveAgentRunTerminalFacts, f as resolveAgentRunWaitTerminalFacts, n as AGENT_RUN_RESTART_ABORT_STOP_REASON, r as AGENT_RUN_SUPERSEDED_STOP_REASON, u as resolveAgentRunLifecycleTerminalFacts } from "./agent-run-terminal-outcome-CurjWZiA.mjs";
import { d as isAgentRunSupersededAbortReason, p as resolveAgentRunAbortLifecycleFields, t as AGENT_RUN_ABORTED_ERROR, u as isAgentRunRestartAbortReason } from "./run-termination-0Ig0-bk6.mjs";
//#region src/shared/agent-liveness.ts
function isBlockedLivenessState(e) {
return typeof e === "string" && e.trim().toLowerCase() === "blocked";
}
function isAbandonedLivenessState(e) {
return typeof e === "string" && e.trim().toLowerCase() === "abandoned";
}
function formatBlockedLivenessError(e) {
return (typeof e === "string" ? e.trim() : "") || "Agent run blocked before producing a usable result.";
}
function formatAbandonedLivenessError(e) {
return (typeof e === "string" ? e.trim() : "") || "Agent run ended before producing a complete result.";
}
/** Coerce any blocked liveness state into an error status while preserving other statuses. */
function normalizeBlockedLivenessWaitStatus(e) {
const t = typeof e.error === "string" ? e.error : void 0;
if (!isBlockedLivenessState(e.livenessState)) return {
status: e.status,
error: t
};
return {
status: "error",
error: formatBlockedLivenessError(t)
};
}
//#endregion
//#region src/agents/agent-run-terminal-outcome.ts
const ATTEMPT_TIMEOUT_PHASE_RANK = {
prompt: 0,
tool_execution: 1,
compaction: 2
};
const ATTEMPT_TIMEOUT_SOURCE_RANK = {
observation: 0,
runtime: 1,
idle: 2,
run_budget: 3,
external: 4
};
const ATTEMPT_ABORT_SOURCE_RANK = {
yield_cleanup: 0,
runtime: 1,
external: 2
};
function mergeAgentRunAttemptTimeoutPhase(e, t) {
return t && ATTEMPT_TIMEOUT_PHASE_RANK[t] > ATTEMPT_TIMEOUT_PHASE_RANK[e] ? t : e;
}
function getAgentRunAttemptFailure(e) {
return e.kind === "failed" ? {
source: e.source,
error: e.error
} : e.kind === "ok" ? void 0 : e.failure;
}
function withAgentRunAttemptFailure(e, t) {
if (!t || e.kind === "ok") return e;
if (e.kind === "failed") return {
...e,
...t
};
return {
...e,
failure: t
};
}
function withAgentRunAttemptTimeoutObservation(e, t) {
const n = e.timeoutObservation === "compaction" || t === "compaction" ? "compaction" : "tool_execution";
return {
...e,
timeoutObservation: n
};
}
function hasAgentRunAttemptTimeoutAbort(e) {
return e.kind === "timeout" && e.source !== "observation" && e.aborted === true;
}
function mergeAgentRunAttemptTimeoutInterruption(e, t) {
if (e.source === "observation") return withAgentRunAttemptTimeoutObservation(t, e.phase);
return {
...e,
phase: mergeAgentRunAttemptTimeoutPhase(e.phase, t.timeoutObservation),
source: t.kind === "aborted" && t.source === "external" ? "external" : e.source,
...(t.kind === "aborted" && t.source !== "yield_cleanup" || e.aborted === true) && { aborted: true }
};
}
/** Replaces attempt failure detail without changing a stronger interruption. */
function setAgentRunAttemptTerminalFailure(e, t) {
if (!t) {
if (e.kind === "failed") return e.timeoutObservation ? {
kind: "timeout",
phase: e.timeoutObservation,
source: "observation"
} : { kind: "ok" };
if (e.kind === "aborted" || e.kind === "timeout") {
const { failure: t, ...n } = e;
return n;
}
return e;
}
if (e.kind === "timeout" && e.source === "observation") return {
kind: "failed",
...t,
timeoutObservation: e.phase
};
if (e.kind === "failed" || e.kind === "ok") return {
kind: "failed",
...t,
...e.kind === "failed" && e.timeoutObservation && { timeoutObservation: e.timeoutObservation }
};
return {
...e,
failure: t
};
}
/** Merges attempt observations while keeping terminal precedence in one owner. */
function mergeAgentRunAttemptTerminal(e, t) {
if (t.kind === "ok") return e.kind === "ok" && !e.settlementWarning ? t : e;
const n = getAgentRunAttemptFailure(t) ?? getAgentRunAttemptFailure(e);
let r = t;
if (e.kind === "timeout" && t.kind === "timeout") {
const n = mergeAgentRunAttemptTimeoutPhase(e.phase, t.phase);
const i = ATTEMPT_TIMEOUT_SOURCE_RANK[t.source] > ATTEMPT_TIMEOUT_SOURCE_RANK[e.source] ? t : e;
if (i.source === "observation") r = {
kind: "timeout",
phase: e.phase === "compaction" || t.phase === "compaction" ? "compaction" : "tool_execution",
source: "observation"
};
else r = {
kind: "timeout",
phase: n,
source: i.source,
...(hasAgentRunAttemptTimeoutAbort(e) || hasAgentRunAttemptTimeoutAbort(t)) && { aborted: true }
};
} else if ((e.kind === "aborted" || e.kind === "failed") && t.kind === "timeout") r = mergeAgentRunAttemptTimeoutInterruption(t, e);
else if (e.kind === "timeout" && (t.kind === "aborted" || t.kind === "failed")) r = mergeAgentRunAttemptTimeoutInterruption(e, t);
else if ((e.kind === "aborted" || e.kind === "failed") && (t.kind === "aborted" || t.kind === "failed")) {
let n;
if (e.kind === "aborted" && t.kind === "aborted") n = {
kind: "aborted",
source: ATTEMPT_ABORT_SOURCE_RANK[t.source] > ATTEMPT_ABORT_SOURCE_RANK[e.source] ? t.source : e.source
};
else n = e.kind === "aborted" ? e : t;
for (const r of [e.timeoutObservation, t.timeoutObservation]) if (r) n = withAgentRunAttemptTimeoutObservation(n, r);
r = n;
}
return withAgentRunAttemptFailure(r, n);
}
/** Normalizes the shipped harness result shape at the Plugin SDK boundary. */
function normalizeAgentRunAttemptTerminal(e) {
let t = {
kind: "ok",
...e.settlementWarning && { settlementWarning: e.settlementWarning }
};
if (e.aborted || e.externalAbort) t = mergeAgentRunAttemptTerminal(t, {
kind: "aborted",
source: e.externalAbort ? "external" : "runtime"
});
if (e.timedOut || e.idleTimedOut || e.timedOutByRunBudget) t = mergeAgentRunAttemptTerminal(t, {
kind: "timeout",
phase: e.timedOutDuringCompaction ? "compaction" : e.timedOutDuringToolExecution ? "tool_execution" : "prompt",
source: e.externalAbort ? "external" : e.timedOutByRunBudget ? "run_budget" : e.idleTimedOut ? "idle" : "runtime",
...(e.aborted || e.externalAbort) && { aborted: true }
});
else if (e.timedOutDuringCompaction || e.timedOutDuringToolExecution) t = mergeAgentRunAttemptTerminal(t, {
kind: "timeout",
phase: e.timedOutDuringCompaction ? "compaction" : "tool_execution",
source: "observation"
});
if (e.promptError !== null && e.promptError !== void 0) t = setAgentRunAttemptTerminalFailure(t, {
error: e.promptError,
source: e.promptErrorSource ?? "prompt"
});
return t;
}
/** Projects the closed attempt terminal into legacy event/meta fields. */
function projectAgentRunAttemptTerminal(e) {
const t = getAgentRunAttemptFailure(e);
const n = (e.kind === "aborted" || e.kind === "timeout") && e.source === "external";
const r = e.kind === "timeout" && e.source !== "observation";
const i = e.kind === "timeout" ? e.phase : e.kind === "ok" ? void 0 : e.timeoutObservation;
return {
...e.kind === "ok" && e.settlementWarning && { settlementWarning: e.settlementWarning },
aborted: e.kind === "aborted" && e.source !== "yield_cleanup" || hasAgentRunAttemptTimeoutAbort(e),
cleanupYieldAborted: e.kind === "aborted" && e.source === "yield_cleanup",
externalAbort: n,
failed: t !== void 0,
idleTimedOut: e.kind === "timeout" && e.source === "idle",
interrupted: n || r,
promptError: t ? t.error : null,
promptErrorSource: t?.source ?? null,
timedOut: r,
timedOutByRunBudget: e.kind === "timeout" && e.source === "run_budget",
timedOutDuringCompaction: i === "compaction",
timedOutDuringToolExecution: i === "tool_execution"
};
}
function completedBeforeOrAtTimeout(e) {
return e.completed.reason === "completed" && e.completed.endedAt !== void 0 && e.timeout.endedAt !== void 0 && e.completed.endedAt <= e.timeout.endedAt;
}
/** Merges observations without overwriting a proven cancellation or hard timeout. */
function mergeAgentRunTerminalOutcome(e, t) {
if (!e) return t;
if (e.reason === "superseded" || e.reason === "cancelled") {
if (t.reason === "hard_timeout" && t.endedAt !== void 0 && e.endedAt !== void 0 && t.endedAt <= e.endedAt) return t;
return e.reason === "superseded" || t.reason !== "superseded" ? e : t;
}
if (e.reason === "hard_timeout") {
if ((t.reason === "superseded" || t.reason === "cancelled") && t.endedAt !== void 0 && e.endedAt !== void 0 && t.endedAt < e.endedAt) return t;
return completedBeforeOrAtTimeout({
completed: t,
timeout: e
}) ? t : e;
}
if (t.reason === "hard_timeout") return completedBeforeOrAtTimeout({
completed: e,
timeout: t
}) ? e : t;
return t;
}
/** Shared grace window for terminal observations that may still be followed by a retry. */
const AGENT_RUN_TERMINAL_RETRY_GRACE_MS = 15e3;
/** True when an outcome should not be overwritten by ordinary later status. */
function isStickyAgentRunTerminalOutcome(e) {
return e?.reason === "hard_timeout" || e?.reason === "superseded" || e?.reason === "cancelled";
}
function formatAgentRunTerminalOutcome(e, t) {
const { reason: n, status: r, ...i } = e;
const a = asFiniteNumber(t.startedAt);
const o = asFiniteNumber(t.endedAt);
const s = t.error == null ? void 0 : readNonBlankString(formatErrorMessage(t.error));
const c = n === "hard_timeout" ? s : isBlockedLivenessState(e.livenessState) ? formatBlockedLivenessError(s) : n === "aborted" && !s ? AGENT_RUN_ABORTED_ERROR : n === "superseded" || n === "aborted" || n === "cancelled" ? s : isAbandonedLivenessState(e.livenessState) ? formatAbandonedLivenessError(s) : s;
return {
reason: n,
status: r,
...c ? { error: c } : {},
...i,
...a !== void 0 ? { startedAt: a } : {},
...o !== void 0 ? { endedAt: o } : {}
};
}
function buildAgentRunTerminalOutcome(e) {
return formatAgentRunTerminalOutcome(resolveAgentRunTerminalFacts(e), e);
}
function buildAgentRunTerminalOutcomeFromLifecycleEvent(e) {
const t = e.data;
const n = typeof t?.aborted === "boolean" ? {} : resolveAgentRunAbortLifecycleFields(e.abortSignal);
return formatAgentRunTerminalOutcome(resolveAgentRunLifecycleTerminalFacts({
phase: e.phase,
data: t,
abortFields: n
}), {
error: t?.error,
startedAt: e.startedAt ?? t?.startedAt,
endedAt: e.endedAt ?? t?.endedAt
});
}
function hasNestedAbortReason(e, t) {
let n = e;
for (let e = 0; e < 3; e += 1) {
if (t(n)) return true;
if (!(n instanceof Error)) return false;
try {
if (n.cause === void 0) return false;
n = n.cause;
} catch {
return false;
}
}
return false;
}
function buildAgentRunTerminalOutcomeFromAttempt(e) {
const t = projectAgentRunAttemptTerminal(e.terminal);
const n = t.cleanupYieldAborted ? void 0 : e.assistant;
const r = resolveAgentRunAbortLifecycleFields(e.abortSignal);
const i = t.timedOut || r.stopReason === "timeout";
const a = t.timedOut && e.terminal.kind === "timeout" ? e.terminal.phase : void 0;
const o = a === "prompt";
const s = e.promptTimeoutOutcome?.timeoutPhase ?? (a === "tool_execution" ? "tool_execution" : o ? "provider" : void 0);
const c = e.promptTimeoutOutcome?.providerStarted ?? (o ? true : void 0);
const l = hasNestedAbortReason(t.promptError, isAgentRunRestartAbortReason);
const u = hasNestedAbortReason(t.promptError, isAgentRunSupersededAbortReason);
const d = t.promptErrorSource !== null ? void 0 : n?.stopReason;
const f = t.timedOut && s === void 0 && c !== true ? void 0 : (u ? "superseded" : void 0) ?? r.stopReason ?? (l ? "restart" : void 0) ?? (!i && t.aborted ? "aborted" : void 0) ?? (!i ? d : void 0);
return buildAgentRunTerminalOutcome({
status: i ? "timeout" : r.aborted || t.aborted || t.promptErrorSource !== null || d === "error" ? "error" : "ok",
error: t.promptErrorSource !== null ? t.promptError : n?.errorMessage,
stopReason: f,
livenessState: e.promptTimeoutOutcome?.livenessState,
timeoutPhase: s,
providerStarted: c
});
}
/** Builds a terminal outcome from wait paths where status may still be pending/unknown. */
function buildAgentRunTerminalOutcomeFromWaitResult(e) {
if (!e) return;
const t = resolveAgentRunWaitTerminalFacts(e);
return t ? formatAgentRunTerminalOutcome(t, e) : void 0;
}
//#endregion
export { buildAgentRunTerminalOutcomeFromWaitResult as a, mergeAgentRunTerminalOutcome as c, setAgentRunAttemptTerminalFailure as d, normalizeBlockedLivenessWaitStatus as f, buildAgentRunTerminalOutcomeFromLifecycleEvent as i, normalizeAgentRunAttemptTerminal as l, buildAgentRunTerminalOutcome as n, isStickyAgentRunTerminalOutcome as o, buildAgentRunTerminalOutcomeFromAttempt as r, mergeAgentRunAttemptTerminal as s, AGENT_RUN_TERMINAL_RETRY_GRACE_MS as t, projectAgentRunAttemptTerminal as u };