UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

91 lines (90 loc) • 3.4 kB
import { i as restoreRuntimeTerminalState, r as defaultRuntime, t as ExitError } from "./runtime-B-SLYP-m.mjs"; import { d as waitForCliSignalExit } from "./signal-exit-barrier-BOGtFHQL.mjs"; import { a as waitForPendingCliDisposers } from "./runtime-cleanup-BKu6ZJ6-.mjs"; import { writeSync } from "node:fs"; //#region src/cli/one-shot-exit.ts let requestedExitCode; let settledExitWatchdogMs; function resolveProcessExitCode(e = 0) { const t = process.exitCode; if (typeof t === "number") return Number.isInteger(t) ? t : e; if (typeof t === "string" && /^-?\d+$/u.test(t.trim())) return Number.parseInt(t, 10); return e; } async function runCliWithExitFinalization(e) { const t = e.runtime ?? defaultRuntime; let n; try { await e.run(); } catch (n) { if (n instanceof ExitError) { if (!requestExitAfterOneShotOutput(t, n.code)) throw n; } else { await e.onError(n); requestExitAfterOneShotOutput(t, resolveProcessExitCode(1)); } } finally { let r = await waitForCliSignalExit(); if (t === defaultRuntime) await waitForPendingCliDisposers(); if (e.finalize) try { await e.finalize(); } catch (r) { try { await e.onError(r); } catch (e) { n = { error: e }; } if (!requestExitAfterOneShotOutput(t, 1)) n ??= { error: r }; } if (t === defaultRuntime) { await waitForPendingCliDisposers(); r = await waitForCliSignalExit() ?? r; const e = requestedExitCode; requestedExitCode = void 0; if (r !== void 0) process.exitCode = r; else if (e !== void 0) process.exitCode = e === "process" ? resolveProcessExitCode() : e; if (r !== void 0 || e !== void 0) restoreRuntimeTerminalState("CLI exit", { resumeStdinIfPaused: false }); if (settledExitWatchdogMs !== void 0) { armSettledExitWatchdog(settledExitWatchdogMs); settledExitWatchdogMs = void 0; } } } if (n) throw n.error; } /** Unwind an already-reported CLI outcome before shared cleanup and output draining. */ function exitCliAfterOutput(e, t) { if (e !== defaultRuntime) e.exit(t); throw new ExitError(t); } function requestExitAfterOneShotOutput(e = defaultRuntime, t) { if (e !== defaultRuntime) return false; requestedExitCode = t ?? "process"; return true; } /** * Long-lived owners retire their deadline watchdogs once shutdown settles. Ask the CLI to bound * a referenced handle nobody closed after every command-owned job, disposer and finalizer ends. */ function requestSettledExitWatchdog(e) { settledExitWatchdogMs = e; } function armSettledExitWatchdog(e) { setTimeout(() => { const t = /* @__PURE__ */ new Map(); for (const e of process.getActiveResourcesInfo()) t.set(e, (t.get(e) ?? 0) + 1); const n = [...t].map(([e, t]) => `${e}x${t}`).join(", "); const r = process.platform !== "win32" && resolveProcessExitCode() === 0; writeSync(2, `command settled but the process is still alive after ${e}ms; active resources: ${n || "unreported"}${r ? "; terminating" : ""}\n`); if (r) { process.removeAllListeners("SIGTERM"); process.kill(process.pid, "SIGTERM"); } }, e).unref(); } /** Report unsettled cleanup without bypassing its native resource owner. */ function watchCliExitAfterOutput(e) { setTimeout(e, 1e4).unref(); } //#endregion export { watchCliExitAfterOutput as a, runCliWithExitFinalization as i, requestExitAfterOneShotOutput as n, requestSettledExitWatchdog as r, exitCliAfterOutput as t };