UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

148 lines (147 loc) • 6.95 kB
import { a as resolveSubagentChildAuthority } from "./subagent-child-owner-match-BFVxB68G.mjs"; import { i as getLatestLiveSubagentRunByChildSessionKey } from "./subagent-registry-read-DHkmVSx2.mjs"; import { D as readPreparedRuntimeSelfHold } from "./prepared-model-runtime-Bbo9uBdt.mjs"; import { i as ensureSubagentControllerOwnsRun, u as resolveSubagentController } from "./subagent-control-scope-ir7nv0U5.mjs"; //#region src/gateway/session-subagent-resume.ts /** Exact parent-owned paused-task binding for explicit model-tool resume admission. */ /** Select task continuation from recorded ownership; admission still binds and revalidates it. */ 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; } /** Captures the exact paused generation after ordinary session visibility checks. */ 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 }); } /** Revalidates caller ownership and the exact paused generation at admission. */ 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; } /** Fences queued execution after the paused task transfers away from its parent caller. */ 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."); } /** Loads the replacement owner; final admission awaits its durable ownership transfer. */ async function prepareParentSubagentResume(e) { const t = await import("./subagent-registry-DxTolEfW.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; }; } //#endregion //#region src/gateway/agent-turn/agent-admission-refusal.ts const refusals = /* @__PURE__ */ new WeakMap(); /** Admission alone stamps the original guard cause after joining its cleanup. */ 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 }); } /** Read the paired host facts; an error flag or plugin-authored JSON never supplies proof. */ 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; } //#endregion export { bindParentSubagentResume as a, assertParentSubagentResumeSuccessorCurrent as i, readAgentSelfHeldRefusal as n, prepareParentSubagentResume as o, assertParentSubagentResumeCurrent as r, shouldResumeParentSubagent as s, createAgentPreacceptRefusal as t };