UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

4 lines • 5.39 kB
import{c as execRequestMatches,l as readExecRequestOwners,r as cancelExecRequestOwners,t as activeExecRequestOwners}from"./exec-request-context-BB4c5QQi.mjs";import{g as peekExecRequestSystemEventEntries,i as consumeSelectedSystemEventEntries}from"./system-events-BXet8b2T.mjs";import{t as getProcessSupervisor}from"./supervisor-4YHLp8V6.mjs";import{n as forgetReleasedProcesses}from"./released-processes-1XlfHxCd.mjs";import{i as captureAgentToolSourceExecutionGuard}from"./agent-tool-source-execution-guard-D2z6vTFX.mjs";import{A as waitForExecSession,C as removeNotifyOnExit,_ as markExecSessionReleased,f as listExecSessionsForCancellation,g as markCancellationRequested,k as waitForExecScope,u as getSession}from"./bash-process-registry-BL4zA8Yf.mjs"; //!src/agents/bash-process-control.ts function isBackgroundExecCancellable(e){return Boolean(e?.backgrounded&&!e.exited&&!e.finalizing&&e.processActivity&&!e.processActivity.resultSettled)}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 releaseIndependentExecServices(t);const r=await Promise.allSettled([n(),waitForExecScope(t)]);const i=[...e,...await forgetReleasedProcesses(t)];if(i.length>0)process.stderr.write(`[openclaw] Left running after this run (OpenClaw no longer tracks them; stop with kill <pid>):\n${i.map(({pid:e,command:t})=>` pid ${e}${t?` ${t}`:``}`).join(` `)}\n`);const a=r.find(e=>e.status===`rejected`);if(a)throw a.reason})}async function releaseIndependentExecServices(e){const t=getProcessSupervisor();const n=listExecSessionsForCancellation().filter(t=>t.scopeKey===e&&t.independentService===true&&!t.durable&&isBackgroundExecCancellable(t));return(await Promise.all(n.map(async e=>{const n=await t.release(e.id);if(n===void 0)return[];markExecSessionReleased(e);return n.length>0?n:[{pid:e.pid,command:e.command}]}))).flat()}function cancelBackgroundExecSession(e){const t=getSession(e);if(!isBackgroundExecCancellable(t))return false;if(t.adopted){markCancellationRequested(t);t.adopted.stop();return true}if(t.durable)t.durable.cancel(captureAgentToolSourceExecutionGuard());else getProcessSupervisor().cancel(e,`manual-cancel`);markCancellationRequested(t);return true}function isConfirmedRequestedStop(e){return e.cancellationRequested===true&&e.exitReason===`manual-cancel`&&e.finalizationFailed!==true}async function drainExecSessions(e,t,n){const r=[];const i=[];const a=[];try{for(const n of e){t();r.push(n);n.requestCancelled=true;if(!n.exited&&!n.finalizing){markCancellationRequested(n);if(n.adopted)i.push(n.adopted.stop());else if(n.durable)n.durable.cancel(t);else getProcessSupervisor().cancel(n.id,`manual-cancel`)}removeNotifyOnExit(n)}}catch(e){a.push(e)}await Promise.all([...i,...r.filter(e=>!e.adopted).map(waitForExecSession)]);if(r.some(e=>e.finalizationFailed||e.cleanupUncertain))a.push(/* @__PURE__ */ new Error(`Command cleanup could not be confirmed; inspect retained process output.`));if(a.length>0)throw new AggregateError(a,`${n} command cleanup is still draining`)}async function drainAgentExecProcesses(e,t){await drainExecSessions(listExecSessionsForCancellation().filter(t=>t.agentId===e),t,`Agent ${e}`)}async function stopSessionExecProcesses(e,t,n=Number.POSITIVE_INFINITY){const r=new Set(e);await drainExecSessions(listExecSessionsForCancellation().filter(e=>e.scopeKey!==void 0&&r.has(e.scopeKey)&&e.startedAt<=n&&!e.durable&&(!e.exited||e.finalizing)),t,`Session`)}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.`)}}}export{isConfirmedRequestedStop as a,isBackgroundExecCancellable as i,captureExecRequestCancellation as n,registerOneShotExecScopeCleanup as o,drainAgentExecProcesses as r,stopSessionExecProcesses as s,cancelBackgroundExecSession as t};