UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 5.52 kB
import{a as resolveSubagentChildAuthority}from"./subagent-child-owner-match-CeqIsf5C.mjs";import{i as getLatestLiveSubagentRunByChildSessionKey}from"./subagent-registry-read-tETbCW7T.mjs";import{n as readPreparedRuntimeSelfHold}from"./prepared-model-runtime-admission-VorRinXK.mjs";import{i as ensureSubagentControllerOwnsRun,u as resolveSubagentController}from"./subagent-control-scope-C92F2k-3.mjs"; //!src/gateway/session-subagent-resume.ts function shouldResumeParentSubagent(e){const t=getLatestLiveSubagentRunByChildSessionKey(e.childSessionKey,e=>e.pauseReason===`sessions_yield`);if(!t||t.expectsCompletionMessage!==true)return false;if((t.controllerSessionKey?.trim()?t.controllerStorePath:t.requesterStorePath)===void 0)return false;const n=resolveSubagentController({cfg:e.cfg,agentId:e.caller.agentId,agentSessionKey:e.caller.sessionKey});return n.controlScope===`children`&&ensureSubagentControllerOwnsRun({cfg:e.cfg,controller:n,entry:t})===void 0}function requirePausedChild(e,t,n){if(!t.assertCurrent)throw new Error(`Task resume requires an admitted parent tool caller.`);t.assertCurrent();const r=resolveSubagentController({cfg:e,agentId:t.agentId,agentSessionKey:t.sessionKey});const i=getLatestLiveSubagentRunByChildSessionKey(n,e=>e.pauseReason===`sessions_yield`);const a=i&&resolveSubagentChildAuthority(i);if(!i||a?.status===`mismatch`||typeof i.execution.endedAt!==`number`||i.killIntent||i.killReconciliation||i.suppressAnnounceReason||i.cleanupCompletedAt!==void 0||i.execution.suppressSessionEffects&&a?.status!==`legacy-unverified`)throw new Error(`Task resume requires a currently paused native child; inspect subagents first.`);if(r.controlScope!==`children`||ensureSubagentControllerOwnsRun({cfg:e,controller:r,entry:i}))throw new Error(`Task resume is limited to children controlled by the calling session.`);if(i.expectsCompletionMessage===false)throw new Error(`Task resume requires a child with task-owned completion.`);return i}function bindParentSubagentResume(e){if(!e.childSessionId)throw new Error(`Task resume requires an existing child session.`);const t=requirePausedChild(e.cfg,e.caller,e.childSessionKey);if(resolveSubagentChildAuthority(t,{sessionId:e.childSessionId,lifecycleRevision:e.childLifecycleRevision}).status===`mismatch`)throw new Error(`Paused child session incarnation changed or is unresolved; inspect subagents again.`);return Object.freeze({caller:e.caller,childSessionKey:e.childSessionKey,childSessionId:e.childSessionId,childLifecycleRevision:e.childLifecycleRevision,previousRunId:t.runId,taskRunId:t.taskRunId??t.runId,generation:t.generation,createdAt:t.createdAt})}function assertParentSubagentResumeCurrent(e){const{resume:t}=e;const n=requirePausedChild(e.cfg,t.caller,t.childSessionKey);if(e.sessionKey!==t.childSessionKey||e.sessionId!==t.childSessionId||e.sessionLifecycleRevision!==t.childLifecycleRevision||resolveSubagentChildAuthority(n,{sessionId:t.childSessionId,lifecycleRevision:t.childLifecycleRevision}).status===`mismatch`||n.runId!==t.previousRunId||(n.taskRunId??n.runId)!==t.taskRunId||n.generation!==t.generation||n.createdAt!==t.createdAt||n.execution.transcriptTarget?.sessionId!==void 0&&n.execution.transcriptTarget.sessionId!==t.childSessionId)throw new Error(`Paused task or child session changed before resume; inspect subagents again.`);return n}function assertParentSubagentResumeSuccessorCurrent(e,t){const n=getLatestLiveSubagentRunByChildSessionKey(e.childSessionKey);if(!n||n.runId!==t||n.taskRunId!==e.taskRunId||n.pauseReason||n.killIntent||n.killReconciliation||typeof n.execution.endedAt===`number`)throw new Error(`Resumed task no longer owns this execution.`)}async function prepareParentSubagentResume(e){const t=await import(`./subagent-registry-BP2GLmsn.mjs`);return async()=>{const n=e.assertAdmissionCurrent();const r=assertParentSubagentResumeCurrent({...e,sessionId:e.getSessionId(),sessionLifecycleRevision:n?.lifecycleRevision});if(!await t.adoptPausedSubagentRunForFollowUp({childSessionKey:e.resume.childSessionKey,runId:e.runId,task:e.task,expected:r,onPublished:e.onAdopted,gatewayContextResolver:e.gatewayContextResolver,assertCurrent:()=>{const t=e.assertAdmissionCurrent();assertParentSubagentResumeCurrent({...e,sessionId:e.getSessionId(),sessionLifecycleRevision:t?.lifecycleRevision})}}))throw new Error(`Paused task replacement was rejected; no continuation was started.`);assertParentSubagentResumeSuccessorCurrent(e.resume,e.runId);return e.resume.taskRunId}} //!src/gateway/agent-turn/agent-admission-refusal.ts const refusals=/* @__PURE__ */ new WeakMap;function recordAgentAdmissionRefusal(e,t,n){const r=readPreparedRuntimeSelfHold(e);if(r)refusals.set(r.cause,{acceptance:t===`runtime-preparation`&&n?`not_accepted`:`uncertain`,stage:t})}function readAgentSelfHeldRefusal(e){const t=readPreparedRuntimeSelfHold(e);if(!t)return;return{...t,admission:refusals.get(t.cause)??{acceptance:`uncertain`,stage:`acceptance`}}}function createAgentPreacceptRefusal(e,t){const n=Object.assign(async r=>{let i=false;try{await t(false,r.message);if(readPreparedRuntimeSelfHold(r))i=e.releaseUnacceptedReservation()}finally{recordAgentAdmissionRefusal(r,n.stage,i);e.io.emitAcceptance([false,void 0,r])}},{stage:`runtime-preparation`});return n}export{bindParentSubagentResume as a,assertParentSubagentResumeSuccessorCurrent as i,readAgentSelfHeldRefusal as n,prepareParentSubagentResume as o,assertParentSubagentResumeCurrent as r,shouldResumeParentSubagent as s,createAgentPreacceptRefusal as t};