openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.16 kB
JavaScript
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{r as getAgentRunLifecycleGeneration}from"./agent-run-registry-state-CQA4cgq2.mjs";import{v as registerAgentEventLifecycleRotationHandler}from"./agent-events-Dz03BVGI.mjs";import{o as captureExternalSessionCommitGuard,s as composeSessionSourceAssertion}from"./session-source-authority-Cun3s-rH.mjs";import"./agent-run-registry-ONwZSqWq.mjs";import{a as captureAdmittedRunActiveAssertion,i as bindOperatorModelExecution,p as readAdmittedRunOperatorAuthority,t as assertAdmittedRunOperatorAuthority,x as requiresOperatorModelExecutionPolicy}from"./admitted-run-context-xwC9VVgE.mjs";import{n as captureGatewayToolReceiptAssertion}from"./gateway-caller-context-Dw9IijqV.mjs";
//!src/agents/harness/host-source-authority.ts
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()}}function bindHarnessNativeSpawnAuthority(e,t){return e?()=>{t();e.resolve(void 0,{allowTurnOwner:()=>true})}:void 0}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`))});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()}}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?.()}}}export{bindHarnessSettledTurnCallback as a,bindHarnessNativeSpawnAuthority as i,bindHarnessHostSourceAuthority as n,retainAgentHarnessCompactionSource as o,bindHarnessModelExecution as r,retainHarnessSource as s,assertCompactionSource as t};