openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
87 lines (86 loc) • 4.72 kB
JavaScript
import { c as execRequestMatches, l as readExecRequestOwners, r as cancelExecRequestOwners, t as activeExecRequestOwners } from "./exec-request-context-C8VKkMcB.mjs";
import { g as peekExecRequestSystemEventEntries, i as consumeSelectedSystemEventEntries } from "./system-events-BFJiZPP0.mjs";
import { C as removeNotifyOnExit, D as waitForExecScope, O as waitForExecSession, _ as markCancellationRequested, p as listExecSessionsForCancellation, u as getSession } from "./bash-process-registry-Dh1460VO.mjs";
import { t as getProcessSupervisor } from "./supervisor-BeB1eaLi.mjs";
//#region src/agents/bash-process-control.ts
function isBackgroundExecCancellable(e) {
return Boolean(e?.backgrounded && !e.exited && !e.finalizing && e.processActivity && !e.processActivity.resultSettled);
}
/** One-shot runs retain process cleanup and backend finalization for the same exec scope. */
function registerOneShotExecScopeCleanup(e, t) {
if (!e?.oneShotCliRun || !t || !e.registerRunCleanup) return;
const n = getProcessSupervisor().acquireScopeCleanup(t, { processTree: "owned-only" });
e.registerRunCleanup(async () => {
const e = (await Promise.allSettled([n(), waitForExecScope(t)])).find((e) => e.status === "rejected");
if (e) throw e.reason;
});
}
function cancelBackgroundExecSession(e) {
const t = getSession(e);
if (!isBackgroundExecCancellable(t)) return false;
getProcessSupervisor().cancel(e, "manual-cancel");
markCancellationRequested(t);
return true;
}
/** A requested cancellation is successful only after its own terminal reason and cleanup. */
function isConfirmedRequestedStop(e) {
return e.cancellationRequested === true && e.exitReason === "manual-cancel" && e.finalizationFailed !== true;
}
/** Agent retirement also owns independent services after their creating turns settle. */
async function drainAgentExecProcesses(e, t) {
const n = listExecSessionsForCancellation().filter((t) => t.agentId === e);
const r = [];
const i = [];
try {
for (const e of n) {
t();
r.push(e);
e.requestCancelled = true;
if (!e.exited && !e.finalizing) {
markCancellationRequested(e);
getProcessSupervisor().cancel(e.id, "manual-cancel");
}
removeNotifyOnExit(e);
}
} catch (e) {
i.push(e);
}
await Promise.all(r.map(waitForExecSession));
if (r.some((e) => e.finalizationFailed || e.cleanupUncertain)) i.push(/* @__PURE__ */ new Error("Command cleanup could not be confirmed; inspect retained process output."));
if (i.length > 0) throw new AggregateError(i, `Agent ${e} command cleanup is still draining`);
}
/** Select before awaited cancellation work; a later human turn cannot enter this plan. */
function captureExecRequestCancellation(e, t = () => true) {
const n = peekExecRequestSystemEventEntries(e);
const r = listExecSessionsForCancellation();
const i = [...r.flatMap((e) => !e.exited || e.finalizing ? readExecRequestOwners(e) ?? [] : []), ...n.flatMap(({ events: e }) => e.flatMap((e) => readExecRequestOwners(e) ?? []))].filter((n) => execRequestMatches(n, e) && t(n.identity));
const a = /* @__PURE__ */ new Set([...activeExecRequestOwners(e, t), ...i]);
const matching = (e) => readExecRequestOwners(e)?.some((e) => a.has(e)) === true;
const o = r.filter(matching);
let s = false;
return {
owners: [...a],
requestRunIds: [...new Set([...a].flatMap((e) => Array.from(e.turnRunIds)))],
cancel(e = () => true) {
if (s || a.size === 0) return false;
s = true;
for (const t of a) if (!e(t.identity)) a.delete(t);
if (a.size === 0) return false;
cancelExecRequestOwners([...a]);
for (const e of listExecSessionsForCancellation().filter(matching)) removeNotifyOnExit(e);
for (const { sessionKey: e, events: t } of n) consumeSelectedSystemEventEntries(e, t.filter((e) => readExecRequestOwners(e)?.some((e) => a.has(e))));
return true;
},
async settle() {
const e = new Set([...a].filter((e) => e.signal.aborted));
if (e.size === 0) return;
const t = [.../* @__PURE__ */ new Set([...o, ...listExecSessionsForCancellation()])].filter((t) => readExecRequestOwners(t)?.some((t) => e.has(t)));
const n = t.map(waitForExecSession);
for (const t of e) for (const e of t.pendingProcesses) n.push(e);
await Promise.all(n);
if ([...e].some((e) => e.cleanupUncertain) || t.some((e) => e.finalizationFailed || e.cleanupUncertain)) throw new Error("Request stopped, but command cleanup could not be confirmed. Inspect its retained process output before retrying.");
}
};
}
//#endregion
export { isConfirmedRequestedStop as a, isBackgroundExecCancellable as i, captureExecRequestCancellation as n, registerOneShotExecScopeCleanup as o, drainAgentExecProcesses as r, cancelBackgroundExecSession as t };