UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

9 lines • 14.9 kB
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{t as freezeJsonSnapshot}from"./immutable-data-CyaF4H_G.mjs";import{n as prepareGatewayContextBindingOwner}from"./gateway-context-binding-owner-D1XU3_jb.mjs";import{r as getAgentRunLifecycleGeneration}from"./agent-run-registry-state-CQA4cgq2.mjs";import{d as prepareSessionSourceScope,p as runWithSessionSourceScope,s as composeSessionSourceAssertion}from"./session-source-authority-Cun3s-rH.mjs";import{O as validateAgentRunDelegatedAuthority,a as claimAgentRunDelegatedAuthority,h as readAgentRunDelegatedAuthorityFailure,n as captureAgentRunDelegatedSourceAssertion,x as releaseAgentRunDelegatedAuthority}from"./agent-run-registry-ONwZSqWq.mjs";import{r as isOperatorAuthorityUnavailable}from"./operator-authority-error-B9EU9j0W.mjs";import{n as isExecutionIdentityCollectionEnabled}from"./audit-config-Dqz9Raz7.mjs";import{n as createExecutionIdentityAdmissionToken,r as enqueueExecutionIdentityContextAtAdmission,s as executionIdentitySpawnAdmission}from"./execution-identity-admission-CBoPnz1k.mjs";import{randomUUID}from"node:crypto"; //!src/infra/operator-access-decision.ts const decisions=resolveGlobalSingleton(Symbol.for(`openclaw.operatorAccessDecision`),()=>({}));function scopeOperatorAccessDecisions(e){return t=>{if(decisions.current)return e(t);decisions.current=[];try{e(t)}finally{decisions.current=void 0}}}function assertOperatorAccessDecision(e,t,n,r){const i=decisions.current;if(e&&i?.some(t=>t.decision.policy===e.policy&&t.decision.config===e.config&&t.decision.subject===e.subject&&t.scope===n&&t.call===r))return;t();if(e&&i)i.push({decision:e,scope:n,call:r})} //!src/agents/operational-run-instance.ts function createOperationalRunInstanceRef(e){return Object.freeze({instanceId:randomUUID(),runId:e})} //!src/agents/operator-access-program.ts function isOperatorAccessProgramAllowed(e,t){if(!e?.rolePolicy||!t||t===`standard`)return true;const n=e.rolePolicy.accessPrograms;return n?n.cyber.includes(t):e.scopes.includes(`operator.admin`)}function requiresOperatorModelExecutionPolicy(e){return e?.modelPolicy!==void 0||!isOperatorAccessProgramAllowed(e,`daybreak_blue`)||!isOperatorAccessProgramAllowed(e,`daybreak_red`)}function resolveAgentHarnessOperatorPolicyError(e,t){return e&&requiresOperatorModelExecutionPolicy(t)&&e.nativeModelPolicySupport!==`exact`?`Agent harness ${e.id} cannot enforce your operator role's model policy or Access grants. Choose a compatible runtime or ask a gateway administrator to update the harness.`:void 0} //!src/agents/admitted-run-context.ts const operatorAuthorityIssuers=resolveGlobalSingleton(Symbol.for(`openclaw.admittedRunOperatorAuthority.issuers`),()=>/* @__PURE__ */ new WeakSet);function createAdmittedRunOperatorAuthority(e){const t=e.assertCurrent;const n=new AbortController;const r=e.signal?AbortSignal.any([e.signal,n.signal]):n.signal;const i=composeSessionSourceAssertion([t],scopeOperatorAccessDecisions(e=>{r.throwIfAborted();try{e()}catch(e){if(!isOperatorAuthorityUnavailable(e))n.abort(e);throw e}}),{synchronousCheck:true});const a=e.readCurrentRoleFacts;const o=Object.freeze({profileId:e.profileId,openclawIdPrincipal:e.openclawIdPrincipal?freezeJsonSnapshot(structuredClone(e.openclawIdPrincipal)):void 0,recoverySnapshot:e.recoverySnapshot?freezeJsonSnapshot(structuredClone(e.recoverySnapshot)):void 0,channelRecoverySource:e.channelRecoverySource?Object.freeze({reference:Object.freeze({...e.channelRecoverySource.reference}),modelPolicy:e.channelRecoverySource.modelPolicy}):void 0,scopes:Object.freeze([...e.scopes]),gatewayAccessGrant:e.gatewayAccessGrant?Object.freeze({...e.gatewayAccessGrant}):e.gatewayAccessGrant,source:e.source??{},invocation:e.invocation,signal:r,retain:e.retain,reauthorize:e.reauthorize,onModelPolicyChanged:e.onModelPolicyChanged,get modelPolicy(){return e.modelPolicy},assertCurrent:i,readCurrentRoleFacts:a?()=>{i();return a()}:void 0,rolePolicy:e.rolePolicy?Object.freeze({...e.rolePolicy,...e.rolePolicy.accessPrograms?{accessPrograms:Object.freeze({cyber:Object.freeze([...new Set(e.rolePolicy.accessPrograms.cyber)].toSorted())})}:{},agents:e.rolePolicy.agents===`*`?`*`:Object.freeze([...new Set(e.rolePolicy.agents)].toSorted())}):void 0});operatorAuthorityIssuers.add(o);return o}function assertAdmittedRunOperatorAuthority(e){if(!e||typeof e!==`object`||!operatorAuthorityIssuers.has(e))throw new Error(`operator run authority must be issued by the host`)}function assertOperatorModelAllowed(e,t){createOperatorModelSelectionAssertion(e,t)()}function createOperatorModelSelectionAssertion(e,t){return composeSessionSourceAssertion([e?.assertCurrent],n=>{if(!e)return;assertAdmittedRunOperatorAuthority(e);n();const r=e.modelPolicy;if(r&&(!t||!r.allows(t)))throw new Error(`Your operator role cannot use this model. Choose an allowed model or ask a gateway administrator to update your role's model policy.`);assertOperatorAccessProgramAllowed(e,t?.cyberAccessProgram)})}function assertOperatorAccessProgramAllowed(e,t){if(!e)return;assertAdmittedRunOperatorAuthority(e);e.assertCurrent();if(!isOperatorAccessProgramAllowed(e,t))throw new Error(`Your operator role cannot use this Access level. Choose Standard or ask a gateway administrator to grant this Access level to your role.`)}function bindOperatorModelExecution(e,t,n){if(!e)return;const r=createOperatorModelSelectionAssertion(e,t?{...t}:void 0);const mapError=e=>isOperatorAuthorityUnavailable(e)?e:n?.(e)??e;let i;try{r();i=e.retain?.()}catch(e){throw mapError(e)}const a=new AbortController;let o=false;const assertCurrent=()=>{if(o)throw mapError(/* @__PURE__ */ new Error(`operator model execution authority is no longer active`));a.signal.throwIfAborted();try{r()}catch(e){const t=mapError(e);if(!isOperatorAuthorityUnavailable(e))a.abort(t);throw t}};const recheck=()=>{try{assertCurrent()}catch{}};const onSourceAbort=()=>a.abort(mapError(e.signal?.reason));e.signal?.addEventListener(`abort`,onSourceAbort,{once:true});const s=e.onModelPolicyChanged?.(recheck);recheck();return{signal:a.signal,assertCurrent,release:()=>{if(!o){o=true;s?.();e.signal?.removeEventListener(`abort`,onSourceAbort);i?.()}}}}function readRunOperatorAuthority(e){return readAdmittedRunOperatorAuthority(e.admittedRunContext)??readPreparedRunOperatorAuthority(e.preparedRunAdmission)}const delegatedAuthorityLeases=/* @__PURE__ */ new WeakMap;const admittedContextsByAuthority=/* @__PURE__ */ new WeakMap;const activeNativeHookRecoveryLeases=/* @__PURE__ */ new Map;function runWithPreparedRunSourceScope(e,t){let n=e.preparedRunAdmission?.assertSourceCurrent;if(e.admittedRunContext){const t=delegatedAuthorityLeases.get(e.admittedRunContext);const r=t&&captureAdmittedRunActiveAssertion(e.admittedRunContext,t.authority);if(!r)return Promise.reject(/* @__PURE__ */ new Error(`admitted run authority is no longer active`));n=r}return runWithSessionSourceScope(n,t,{checkAfter:false})}function bindAdmittedRunDelegatedAuthority(e,t,n){const r=claimAgentRunDelegatedAuthority(e.operationalRunInstance,t);const i=activeNativeHookRecoveryLeases.get(e.operationalRunInstance.runId);activeNativeHookRecoveryLeases.delete(e.operationalRunInstance.runId);i?.releaseOperatorAuthority?.();const a={authority:r,foregroundClosed:false,assertSourceCurrent:t,operatorAuthority:n};delegatedAuthorityLeases.set(e,a);if(!admittedContextsByAuthority.has(r))admittedContextsByAuthority.set(r,e)}function getAdmittedRunDelegatedAuthority(e){const t=delegatedAuthorityLeases.get(e);return t&&!t.foregroundClosed&&validateAgentRunDelegatedAuthority(t.authority)?t.authority:void 0}function readAdmittedRunOperatorAuthority(e){if(!e)return;const t=delegatedAuthorityLeases.get(e)?.operatorAuthority;if(!t)return;if(!getAdmittedRunDelegatedAuthority(e))throw new Error(`admitted run operator authority is no longer active`);return t}function readPreparedRunOperatorAuthority(e){const t=e?.readOperatorAuthority?.();if(t!==void 0)assertAdmittedRunOperatorAuthority(t);return t}function getAdmittedRunSource(e){const t=e&&admittedContextsByAuthority.get(e);return t&&getAdmittedRunDelegatedAuthority(t)===e?t.admissionSource:void 0}function resolveAdmittedRunActiveAssertion(e,t){const n=getAdmittedRunDelegatedAuthority(e);return n?captureAdmittedRunActiveAssertion(e,n,t):void 0}function captureAdmittedRunActiveAssertion(e,t,n){const r=e.operationalRunInstance;const i=delegatedAuthorityLeases.get(e);const refuse=()=>{throw new Error(`admitted run authority is no longer active`,readAgentRunDelegatedAuthorityFailure(t))};const a=captureAgentRunDelegatedSourceAssertion(t,refuse);if(!i||i.foregroundClosed||i.authority!==t||!a)return;const assertLocalCurrent=t=>{if(n?.aborted||e.operationalRunInstance!==r||delegatedAuthorityLeases.get(e)!==i||i.foregroundClosed)refuse();t()};return Object.assign(composeSessionSourceAssertion([a.assertCurrent],scopeOperatorAccessDecisions(assertLocalCurrent),{synchronousCheck:true}),{assertScopeCurrent:()=>assertLocalCurrent(a.assertBinding)})}function closeAdmittedRunDelegatedAuthority(e){const t=delegatedAuthorityLeases.get(e);if(!t||t.foregroundClosed)return false;t.foregroundClosed=true;releaseAgentRunDelegatedAuthority(t.authority);return true}function retainAdmittedRunBeforeToolCallRecovery(e){const t=delegatedAuthorityLeases.get(e);const n=e.operationalRunInstance.runId;if(!t||t.foregroundClosed||activeNativeHookRecoveryLeases.has(n)||!validateAgentRunDelegatedAuthority(t.authority))return;const r={lease:t,releaseOperatorAuthority:t.operatorAuthority?.retain?.()};activeNativeHookRecoveryLeases.set(n,r);const assertActive=()=>{t.assertSourceCurrent?.();if(getAgentRunLifecycleGeneration()!==t.authority.lifecycleGeneration||activeNativeHookRecoveryLeases.get(n)!==r)throw new Error(`admitted run native hook recovery is no longer active`)};return{assertActive,release:()=>{if(activeNativeHookRecoveryLeases.get(n)===r){activeNativeHookRecoveryLeases.delete(n);r.releaseOperatorAuthority?.()}}}}function prepareSystemAgentRunAdmission(e,t,n,r,i,a){return prepareAgentRunAdmission({cfg:e,operationalRunInstance:createOperationalRunInstanceRef(t),assertSourceCurrent:i,operatorAuthority:a,facts:{runId:t,agentId:n,ingress:{kind:`system`,boundary:r,state:`present`}}})}function prepareAgentRunAdmission(e){const t=e.operationalRunInstance;if(t.runId!==e.facts.runId)throw new Error(`operational run instance disagrees with prepared admission`);const n=e.assertSourceCurrent;const r=e.operatorAuthority;if(r!==void 0)assertAdmittedRunOperatorAuthority(r);const i=r?.assertCurrent;const a=r?.retain?.();let o;const s=(n||i)&&composeSessionSourceAssertion([n,i],scopeOperatorAccessDecisions(e=>{if(o)throw o;try{e()}catch(e){if(!isOperatorAuthorityUnavailable(e))o=new Error(`source execution authority is no longer active`,{cause:e});throw e}}),{synchronousCheck:true});let c;let l;let u;let d;let f=false;const assertPreparationOpen=()=>{if(f)throw new Error(`prepared execution context is already closed`)};const p=composeSessionSourceAssertion([s],e=>{assertPreparationOpen();e()},{synchronousCheck:true});return Object.freeze({operationalRunInstance:t,assertCurrent:()=>{assertPreparationOpen();if(!d)return;const e=delegatedAuthorityLeases.get(d);const t=e&&captureAdmittedRunActiveAssertion(d,e.authority);if(!t)throw new Error(`prepared execution authority is no longer active`);t.assertScopeCurrent()},assertSourceCurrent:Object.assign(composeSessionSourceAssertion([s],void 0,{synchronousCheck:true}),{assertScopeCurrent:assertPreparationOpen,prepareSessionSourceScope:()=>{assertPreparationOpen();return prepareSessionSourceScope(p)}}),readOperatorAuthority:()=>{if(r){if(f)throw new Error(`prepared operator authority is no longer active`);r.assertCurrent()}return r},close:()=>{if(f)return;f=true;if(d)closeAdmittedRunDelegatedAuthority(d);else u?.then(closeAdmittedRunDelegatedAuthority).catch(()=>void 0);a?.()},admit:(n,i)=>{if(f)return Promise.reject(/* @__PURE__ */ new Error(`prepared execution context is already closed`));const a=c??=n;l??=i?.trim()||void 0;u??=runWithSessionSourceScope(s,async()=>{r?.reauthorize?.();s?.();const n=executionIdentitySpawnAdmission({operation:`attach`,value:{...e.facts,runtime:{kind:a}},extra:executionIdentitySpawnAdmission({operation:`read`,value:e.facts})});const i=admitPreparedAgentRun({cfg:e.cfg,admissionSource:e.admissionSource,facts:n,operationalRunInstance:t,runtimeInstanceId:l});bindAdmittedRunDelegatedAuthority(i,s,r);d=i;try{await e.onAdmitted?.(i);if(f||!getAdmittedRunDelegatedAuthority(i))throw new Error(`prepared execution authority closed during admission`);return i}catch(e){closeAdmittedRunDelegatedAuthority(i);throw e}},{checkAfter:false});return u}})}async function resolvePreparedRunAdmission(e){if(e.admittedRunContext&&e.preparedRunAdmission)throw new Error(`run cannot carry both prepared and admitted execution contexts`);const t=e.preparedRunAdmission?await e.preparedRunAdmission.admit(e.runtimeKind,e.runtimeInstanceId):e.admittedRunContext;if(!t||t.operationalRunInstance.runId!==e.runId)throw new Error(`prepared execution context is unavailable or disagrees with the run`);if(delegatedAuthorityLeases.get(t)&&!getAdmittedRunDelegatedAuthority(t))throw new Error(`prepared execution authority is no longer active`);return t}function admitPreparedAgentRun(e){if(e.operationalRunInstance.runId!==e.facts.runId)throw new Error(`operational run instance disagrees with prepared admission`);const t={operationalRunInstance:e.operationalRunInstance,...e.admissionSource?{admissionSource:e.admissionSource}:{}};if(!isExecutionIdentityCollectionEnabled(e.cfg))return Object.freeze(prepareGatewayContextBindingOwner(t));const n=createExecutionIdentityAdmissionToken(e.facts.runId);enqueueExecutionIdentityContextAtAdmission(e.facts,{enabled:true,token:n,runtimeInstanceId:e.runtimeInstanceId});return Object.freeze(prepareGatewayContextBindingOwner({...t,executionIdentityToken:n}))}export{createOperationalRunInstanceRef as C,resolveAgentHarnessOperatorPolicyError as S,resolvePreparedRunAdmission as _,captureAdmittedRunActiveAssertion as a,isOperatorAccessProgramAllowed as b,createOperatorModelSelectionAssertion as c,prepareAgentRunAdmission as d,prepareSystemAgentRunAdmission as f,resolveAdmittedRunActiveAssertion as g,readRunOperatorAuthority as h,bindOperatorModelExecution as i,getAdmittedRunDelegatedAuthority as l,readPreparedRunOperatorAuthority as m,assertOperatorAccessProgramAllowed as n,closeAdmittedRunDelegatedAuthority as o,readAdmittedRunOperatorAuthority as p,assertOperatorModelAllowed as r,createAdmittedRunOperatorAuthority as s,assertAdmittedRunOperatorAuthority as t,getAdmittedRunSource as u,retainAdmittedRunBeforeToolCallRecovery as v,assertOperatorAccessDecision as w,requiresOperatorModelExecutionPolicy as x,runWithPreparedRunSourceScope as y};