openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 5.91 kB
JavaScript
import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{a as captureExecRequestCustodySelection}from"./exec-request-context-BB4c5QQi.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import{j as captureAgentHarnessLifecycleHooks}from"./session-accessor.sqlite-entry-store-Cxqr6m6F.mjs";import{l as isSameSubagentRunOwner}from"./subagent-run-generation-BxCMg1j-.mjs";import{n as captureExecRequestCancellation}from"./bash-process-control-ByEEGn0n.mjs";import{t as killAllControlledSubagentRuns}from"./subagent-control-kill-DbyzZdT-.mjs";import{d as resolveSubagentControllerIdentity,r as captureExecRequestSubagentSelection}from"./subagent-control-scope-C92F2k-3.mjs";import"./subagent-control-CIH3KBI5.mjs";
//!src/agents/harness/descendant-cancellation.ts
async function withAgentHarnessDescendantCancellation(e,t){const n=captureAgentHarnessLifecycleHooks(`withDescendantCancellation`);const r=[];const prepareNext=async i=>{const a=n[i];if(!a){let e=false;return t(async()=>{if(e)throw new Error(`Descendant cancellation was already consumed`);e=true;const t=await Promise.allSettled(r.map(e=>e()));const n=t.flatMap(e=>e.status===`rejected`?[e.reason]:[...e.value.errors??[]]);return t.reduce((e,t)=>{if(t.status===`fulfilled`){e.killed+=t.value.killed;e.cancelledRequesters.push(...t.value.cancelledRequesters)}return e},{killed:0,cancelledRequesters:[],...n.length?{errors:n}:{}})})}let o=true;const assertCurrent=()=>{e.assertCurrent();if(!o||!a.isCurrent())throw new Error(`Descendant cancellation harness owner changed`)};try{assertCurrent();return await a.prepare({...e,assertCurrent},async e=>{assertCurrent();r.push(async()=>{assertCurrent();return e()});return prepareNext(i+1)})}finally{o=false}};return prepareNext(0)}
//!src/gateway/server-methods/chat-abort-descendants.ts
async function abortControlledSubagents(e){const t={cfg:e.cfg,agentSessionKey:e.sessionKey,agentId:e.agentId};const n={...resolveSubagentControllerIdentity(t),controlScope:`children`};const assertCurrent=()=>{e.assertCurrent?.()};let r=e.execCancellation??captureExecRequestCancellation({runId:e.requesterTurnRunId,sessionKey:e.sessionKey,agentId:e.agentId,sessionId:e.sessionId});let i=captureExecRequestSubagentSelection({...e,controller:n,owners:r.owners,candidateOrigin:e.prepareSelection?{target:{sessionKey:n.controllerSessionKey,agentId:n.controllerAgentId},acceptsRequest:e.prepareSelection.acceptsRequest}:void 0});let a=e.sessionOrigin;const o=e.prepareSelection;const s=o?async()=>{const t=await o.prepare();r=t.execCancellation;a=t.sessionOrigin;i=captureExecRequestSubagentSelection({...e,controller:n,owners:r.owners,sessionOrigin:t.sessionOrigin})}:void 0;const c={runs:i.runs,ownsRoot:e=>i.ownsRoot(e),selectPublishedRoot:o||i.selectPublishedRoot?e=>i.selectPublishedRoot?.(e)===true:void 0,get sessionGeneration(){return i.sessionGeneration},prepare:s};let l=false;const u=[];let d=[];let f;const p=[];const beforeKill=async(t=()=>{})=>{t();let o=false;let s=false;const stop=async u=>{o=true;if(await e.beforeKill?.(t)===false)return false;try{assertCurrent()}catch(e){if(c.runs.length===0)throw e;p.push(e);return false}s=true;const m=e.sessionId??a?.target.sessionId;d=m?c.runs.flatMap(t=>{const r=e.yieldedRequesterRunId??(t.requesterTurnYielded?t.requesterTurnRunId:void 0);return i.ownsRoot(t)&&t.requesterSessionKey===n.controllerSessionKey&&(t.requesterAgentId??n.controllerAgentId)===n.controllerAgentId&&r&&(t.execution.status!==`terminal`||t.pauseReason===`sessions_yield`||t.requesterSettleWake)?[{entry:t,requester:{agentId:n.controllerAgentId,sessionKey:n.controllerSessionKey,sessionId:m,lifecycleRevision:e.lifecycleRevision??a?.target.lifecycleRevision??void 0,requesterTurnRunId:r}}]:[]}):[];f=Promise.allSettled([u()]).then(([e])=>e);try{l=r.cancel()}finally{await f}return true};const u=e.sessionId??a?.target.sessionId;if(!u)return stop(async()=>({killed:0,cancelledRequesters:[]}));try{return await withAgentHarnessDescendantCancellation({agentId:n.controllerAgentId,sessionKey:n.controllerSessionKey,sessionId:u,lifecycleRevision:e.lifecycleRevision??a?.target.lifecycleRevision??void 0,requesterTurnRunId:e.requesterTurnRunId,acceptsRequest:captureExecRequestCustodySelection(r.owners,e.requesterTurnRunId===void 0?a:void 0),assertCurrent},stop)}catch(e){if(o){if(!s)throw e;p.push(e);return true}assertCurrent();return stop(async()=>{throw e})}};let m;let h;try{if(c.runs.length===0&&!s)await beforeKill();else m=await killAllControlledSubagentRuns({cfg:e.cfg,controller:n,runs:c.runs,requestSelection:c,suppressTaskDelivery:true,onCancelled:e=>{const t=d.find(t=>isSameSubagentRunOwner(e,t.entry));if(t&&!u.includes(t.requester))u.push(t.requester)},assertCurrent,beforeKill})}catch(e){h={error:e}}try{await r.settle()}catch(e){p.push(e)}const g=await f;if(g?.status===`fulfilled`)p.push(...g.value.errors??[]);if(g?.status===`rejected`)p.push(g.reason);const _=(m?.killed??0)+(g?.status===`fulfilled`?g.value.killed:0);const v=[...u,...g?.status===`fulfilled`?g.value.cancelledRequesters:[]];if(h&&!g&&!m&&!l&&!e.parentStopAccepted?.())throw p.length>0?new AggregateError([h.error,...p],`Request cancellation was incomplete`):h.error;if(h)p.push(h.error);if(p.length>0)return{...m??{killed:0,labels:[]},killed:_,cancelledRequesters:v,status:`error`,failed:(m?.status===`error`?m.failed:0)+p.length,error:[...m&&m.status!==`ok`?[m.error]:[],...p.map(formatErrorMessage)].join(`; `),execAborted:l||m?.execAborted===true};return m||l||_?{...m??{status:`ok`,killed:0,labels:[]},killed:_,cancelledRequesters:v,execAborted:l||m?.execAborted===true}:void 0}function descendantAbortError(e,t){return e&&e.status!==`ok`?errorShape(ErrorCodes.UNAVAILABLE,`${t} stopped, but descendant cancellation was incomplete: ${e.error}`):void 0}export{descendantAbortError as n,abortControlledSubagents as t};