UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

182 lines (181 loc) • 6.43 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import "./agent-run-registry-D7vkrqFT.mjs"; import { v as registerAgentEventLifecycleRotationHandler } from "./agent-events-BdfWYmot.mjs"; import { a as captureExternalSessionCommitGuard, o as composeSessionSourceAssertion } from "./session-source-authority-TlTpfRVE.mjs"; import { r as getAgentRunLifecycleGeneration } from "./agent-run-registry-state-BFfw9a4W.mjs"; import { a as captureAdmittedRunActiveAssertion, i as bindOperatorModelExecution, p as readAdmittedRunOperatorAuthority, t as assertAdmittedRunOperatorAuthority, x as requiresOperatorModelExecutionPolicy } from "./admitted-run-context-D3Jvk_f_.mjs"; import { n as captureGatewayToolReceiptAssertion } from "./gateway-caller-context-BNidAW8G.mjs"; //#region src/agents/harness/host-source-authority.ts /** Keep the original run and caller predicates attached to every host capability. */ function bindHarnessHostSourceAuthority(e) { const { attempt: t, sourceCaller: n, inactiveError: r } = e; const i = t.admittedRunContext.operationalRunInstance; const a = captureAdmittedRunActiveAssertion(t.admittedRunContext, e.delegatedAuthority); const checkAdmitted = (n) => { if (!e.isActive() || !a || t.admittedRunContext.operationalRunInstance !== i) throw r("agent harness host capability is no longer active"); try { n(); } catch { throw r("agent harness host capability is no longer active"); } if (!e.isGatewayCurrent()) throw r("agent harness host capability is no longer active"); }; const o = composeSessionSourceAssertion([a], checkAdmitted); const s = n?.receiptAuthority; const c = s && captureGatewayToolReceiptAssertion(s, "agent harness host capability lost its source execution claim"); const checkCaller = (e) => { if (n && (n.agentId !== t.agentId || n.sessionKey !== t.sessionKey) || n?.workerTurnClaim && (n.workerTurnClaim.sessionId !== t.sessionId || n.workerTurnClaim.runId !== t.runId) || n?.workerTurnClaim && !s) throw new Error("agent harness host capability lost its source execution claim"); e(); }; const l = composeSessionSourceAssertion([captureExternalSessionCommitGuard(c)], checkCaller); return Object.assign(composeSessionSourceAssertion([o, l]), { assertScopeCurrent: () => { checkAdmitted(() => a?.assertScopeCurrent()); checkCaller(() => (c?.assertScopeCurrent ?? c)?.()); } }); } function bindHarnessSettledTurnCallback(e, t, n) { if (!e) return; const assertCurrent = () => { n?.throwIfAborted(); t(); }; return async (t, n) => { assertCurrent(); await e(t, async () => { assertCurrent(); const e = await n(); assertCurrent(); return e; }); assertCurrent(); }; } /** Native delegation keeps the incumbent requester and its live turn authority. */ function bindHarnessNativeSpawnAuthority(e, t) { return e ? () => { t(); e.resolve(void 0, { allowTurnOwner: () => true }); } : void 0; } /** Acquires original-source model authority while the issuing host is active. */ function bindHarnessModelExecution(e, t, n) { n(); return bindOperatorModelExecution(readAdmittedRunOperatorAuthority(e), t); } const retainedSources = resolveGlobalSingleton(Symbol.for("openclaw.harness.retainedSources"), () => /* @__PURE__ */ new Set()); registerAgentEventLifecycleRotationHandler("harness-retained-sources", () => { const e = [...retainedSources]; retainedSources.clear(); for (const t of e) t.abort(/* @__PURE__ */ new Error("agent harness retained source is no longer active")); }); /** Transfers original-source custody while the issuing foreground host is still live. */ function retainHarnessSource(e, t, n = false) { t(); const r = getAgentRunLifecycleGeneration(); const i = readAdmittedRunOperatorAuthority(e); if (!i) return; const a = n ? void 0 : bindOperatorModelExecution(i, void 0); const o = n ? i.retain?.() : a?.release; const assertSourceCurrent = () => { if (n) i.assertCurrent(); else a?.assertCurrent(); }; try { t(); assertSourceCurrent(); t(); } catch (e) { o?.(); throw e; } let s = false; let c; const l = new AbortController(); retainedSources.add(l); const u = a?.signal ?? i.signal; const d = u ? AbortSignal.any([u, l.signal]) : l.signal; const assertRetained = () => { if (s || getAgentRunLifecycleGeneration() !== r) throw new Error("agent harness retained source is no longer active"); d.throwIfAborted(); }; const assertCurrent = () => { assertRetained(); assertSourceCurrent(); assertRetained(); }; return Object.freeze({ signal: d, assertCurrent, get modelPolicyRequired() { assertCurrent(); const e = requiresOperatorModelExecutionPolicy(i); assertCurrent(); return e; }, get sourceIdentity() { assertCurrent(); return i.source; }, bindModelExecution: (e) => { assertCurrent(); const t = bindOperatorModelExecution(i, e); if (!t) return; const assertBindingCurrent = () => { t.assertCurrent(); assertRetained(); }; try { assertBindingCurrent(); } catch (e) { t.release(); throw e; } c ??= new AbortController(); return { signal: AbortSignal.any([ d, c.signal, t.signal ]), assertCurrent: assertBindingCurrent, release: t.release }; }, release: () => { if (!s) { s = true; retainedSources.delete(l); c?.abort(/* @__PURE__ */ new Error("agent harness retained source is no longer active")); o?.(); } } }); } function assertCompactionSource(e) { if (!e || !Object.hasOwn(e, "operatorAuthority")) throw new Error("Compaction requires its original host source authority"); e.assertActive(); if (e.operatorAuthority !== void 0) { assertAdmittedRunOperatorAuthority(e.operatorAuthority); e.operatorAuthority.assertCurrent(); } } /** Retains the existing issuer through queueing; the compaction work owner releases it. */ function retainAgentHarnessCompactionSource(e) { assertCompactionSource(e); const t = e.operatorAuthority?.retain?.(); try { assertCompactionSource(e); } catch (e) { t?.(); throw e; } let n = false; return () => { if (!n) { n = true; t?.(); } }; } //#endregion export { bindHarnessSettledTurnCallback as a, bindHarnessNativeSpawnAuthority as i, bindHarnessHostSourceAuthority as n, retainAgentHarnessCompactionSource as o, bindHarnessModelExecution as r, retainHarnessSource as s, assertCompactionSource as t };