UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

129 lines (128 loc) • 6.98 kB
//#region src/agents/bash-tools.exec-output.ts const EXEC_NO_OUTPUT_PLACEHOLDER = "(no output)"; const EXEC_MANUAL_COLLECTION_FOLLOW_UP = "Automatic completion wake is unavailable after ending this turn. If the task needs this result, use poll with a timeout to collect it before ending the turn, unless another continuation is already arranged."; const MAX_EXEC_YIELD_MS = 12e4; const BACKGROUND_EXEC_FOLLOW_UP = "Use process (list/poll/log/write/send-keys/submit/paste/kill/clear/remove) to control it. New output while it runs does not start another turn. Ending the turn does not stop it; use process kill when it is no longer needed. This is not a saved, restartable automation."; /** One waiting recipe for every surface that tells the agent how to wait. */ const EXEC_WAIT_RECIPE = "To wait for a condition or a time, run one command that loops until it holds, exits non-zero on failure states and prints the result."; /** Whether every exit, silent success included, wakes the conversation after the originating turn ends. */ function hasExitWake(e) { return e.notifyOnExit === true && e.notifyOnExitEmptySuccess === true && e.notifySessionTarget !== void 0 && e.assertExitNotificationAcceptanceCurrent === void 0; } /** Exit-wake fact plus the condition for ending the turn, or undefined when the exit starts no turn. */ function describeExitWake(e) { return hasExitWake(e) ? "Its exit wakes this conversation with the result, silent success included. If only waiting remains, end this turn" : void 0; } /** Guidance for a running background handle, read when the handle is returned. */ function buildBackgroundExecFollowUp(e, t, n) { const r = describeExitWake(e); return [ (t ?? 0) > MAX_EXEC_YIELD_MS && `yieldMs is capped at ${MAX_EXEC_YIELD_MS}.`, r && `${r} now; do not poll it or start another command for the same result. ${"To wait for a condition or a time, run one command that loops until it holds, exits non-zero on failure states and prints the result."}`, n.length > 0 && `Also running in this conversation: ${n.join("; ")}. Keep one command per awaited result.`, BACKGROUND_EXEC_FOLLOW_UP, !r && "Automatic completion wake is unavailable after ending this turn. If the task needs this result, use poll with a timeout to collect it before ending the turn, unless another continuation is already arranged." ].filter(Boolean).join(" "); } const EXEC_TIMEOUT_RETRY_GUIDANCE = "The command was terminated, but external side effects may already have completed. Verify the resulting state before retrying. Do not automatically rerun commands unless repeating them is safe. Use a higher timeout only when the command is known to be safe to retry."; const EXEC_RETENTION_CAP_NOTE = "[earlier output was discarded at the retention cap and cannot be recovered]\n\n"; /** Render command output with a stable placeholder for empty output. */ function renderExecOutputText(e) { return e || "(no output)"; } /** Render the authoritative process exit without inventing a successful code. */ function renderExecExitLabel(e) { if (e.exitSignal != null) return `signal ${e.exitSignal}`; return e.exitCode != null ? `code ${e.exitCode}` : "unknown exit code"; } /** Render the text shown in exec progress updates, including warnings first. */ function renderExecUpdateText(e) { return (e.warnings.length ? `${e.warnings.join("\n")}\n\n` : "") + renderExecOutputText(e.tailText); } /** Add retry-safety guidance only for supervisor timeout exits. */ function appendExecTimeoutRetryGuidance(e, t) { if (t !== "overall-timeout" && t !== "no-output-timeout") return e; return `${e}\n\n${EXEC_TIMEOUT_RETRY_GUIDANCE}`; } function normalizeNotifyOutput(e) { return e.replace(/\s+/g, " ").trim(); } function joinExecFailureOutput(e, t) { return e ? `${e}\n\n${t}` : t; } function buildExecExitOutcome(e) { const t = e.exit.exitCode ?? 0; const n = e.exit.reason === "exit"; const r = t === 126 || t === 127; if ((n && !r ? "completed" : "failed") === "completed") { const n = t !== 0 ? `\n\n(Command exited with code ${t})` : ""; return { status: "completed", exitCode: t, exitSignal: e.exit.exitSignal, exitReason: e.exit.reason, durationMs: e.durationMs, aggregated: (n ? renderExecOutputText(e.aggregated) : e.aggregated) + n, timedOut: false, noOutputTimedOut: e.exit.noOutputTimedOut }; } const i = e.exit.reason === "memory-limit" ? "memory-limit" : r ? t === 127 ? "shell-command-not-found" : "shell-not-executable" : e.exit.reason === "overall-timeout" ? "overall-timeout" : e.exit.reason === "no-output-timeout" ? "no-output-timeout" : e.exit.exitSignal != null ? "signal" : "aborted"; let a; switch (i) { case "memory-limit": a = "Command killed for exceeding its host memory bound. Reduce concurrency or input size, or split the work."; break; case "shell-command-not-found": a = "Command not found"; break; case "shell-not-executable": a = "Command not executable (permission denied)"; break; case "overall-timeout": { const t = appendExecTimeoutRetryGuidance(typeof e.timeoutSec === "number" && e.timeoutSec > 0 ? `Command timed out after ${e.timeoutSec} seconds.` : "Command timed out.", i); a = e.processContinuationAvailable ? `${t}\n\nIf it should keep running, start it with exec background=true or yieldMs so OpenClaw can register a pollable process session. Do not rely on shell backgrounding with a trailing &.` : t; break; } case "no-output-timeout": a = appendExecTimeoutRetryGuidance("Command timed out waiting for output.", i); break; case "signal": a = `Command aborted by signal ${e.exit.exitSignal}`; break; case "aborted": a = "Command aborted before exit code was captured"; break; default: throw new Error("Unsupported exec failure kind"); } return { status: "failed", exitCode: e.exit.exitCode, exitSignal: e.exit.exitSignal, exitReason: e.exit.reason, durationMs: e.durationMs, aggregated: e.aggregated, timedOut: e.exit.timedOut, noOutputTimedOut: e.exit.noOutputTimedOut, failureKind: i, oomScoreWrapperSelected: e.exit.oomScoreWrapperSelected, message: a, reason: joinExecFailureOutput(e.aggregated, a) }; } function buildExecRuntimeErrorOutcome(e) { const t = String(e.error); return { status: "failed", exitCode: null, exitSignal: null, durationMs: e.durationMs, aggregated: e.aggregated, timedOut: false, failureKind: "runtime-error", message: t, reason: joinExecFailureOutput(e.aggregated, t) }; } //#endregion export { EXEC_WAIT_RECIPE as a, buildExecExitOutcome as c, joinExecFailureOutput as d, normalizeNotifyOutput as f, renderExecUpdateText as h, EXEC_TIMEOUT_RETRY_GUIDANCE as i, buildExecRuntimeErrorOutcome as l, renderExecOutputText as m, EXEC_NO_OUTPUT_PLACEHOLDER as n, appendExecTimeoutRetryGuidance as o, renderExecExitLabel as p, EXEC_RETENTION_CAP_NOTE as r, buildBackgroundExecFollowUp as s, EXEC_MANUAL_COLLECTION_FOLLOW_UP as t, hasExitWake as u };