openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.07 kB
JavaScript
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{n as openOpenClawStateDatabase}from"./openclaw-state-db-BlgxYkYw.mjs";import{a as parseExecutionIdentityAdmissionToken}from"./execution-identity-admission-CBoPnz1k.mjs";import{t as validateDecisionReceiptV1}from"./audit-run-validators-CsD4tvFG.mjs";import{n as recordExecutionDecisionFactInDatabase,o as pseudonymizeExecutionIdentityRef}from"./execution-decision-facts-BHXtqsNv.mjs";
//!src/audit/execution-decision-work.ts
const EXECUTION_DECISION_WORK_MAX_BYTES=16384;const EXECUTION_DECISION_RAW_REF_MAX_LENGTH=4096;const state=resolveGlobalSingleton(Symbol.for(`openclaw.executionDecisionWorkSink`),()=>({sink:void 0}));function isClosedPayloadRecord(e){if(!isRecord(e))return false;const t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function hasOnlyKeys(e,t){const n=new Set(t);return Object.keys(e).every(e=>n.has(e))}function parseRawRef(e){if(!isClosedPayloadRecord(e.value)||!hasOnlyKeys(e.value,[`namespace`,`value`])||typeof e.value.namespace!==`string`||!e.namespaces.includes(e.value.namespace)||typeof e.value.value!==`string`||e.value.value.length<1||e.value.value.length>EXECUTION_DECISION_RAW_REF_MAX_LENGTH)throw new Error(`execution decision work violates its bounded ref contract`);return{namespace:e.value.namespace,value:e.value.value}}function buildReceipt(e){return{...e.receipt,...e.representedSubject?{representedSubject:e.representedSubject}:{},contextId:e.token.contextId,executionId:e.token.executionId,runId:e.token.runId,action:{...e.receipt.action,...e.resourceRef?{resourceRef:e.resourceRef}:{},...e.targetRef?{targetRef:e.targetRef}:{}}}}function parseExecutionDecisionWork(e){if(!isClosedPayloadRecord(e)||!hasOnlyKeys(e,[`workVersion`,`token`,`receipt`,`refs`])||e.workVersion!==1||!isClosedPayloadRecord(e.receipt)||!hasOnlyKeys(e.receipt,[`schemaVersion`,`receiptId`,`actionId`,`occurredAt`,`action`,`decision`,`enforcement`,`source`,`missingEvidence`,`remediation`])||!isClosedPayloadRecord(e.receipt.action)||!hasOnlyKeys(e.receipt.action,[`family`,`operation`,`summary`]))throw new Error(`execution decision work violates its bounded contract`);const t=parseExecutionIdentityAdmissionToken(e.token);let n;if(e.refs!==void 0){if(!isClosedPayloadRecord(e.refs)||!hasOnlyKeys(e.refs,[`resource`,`target`,`representedSubject`]))throw new Error(`execution decision work violates its bounded ref contract`);const t=e.refs.representedSubject;let r;if(t!==void 0){if(!isClosedPayloadRecord(t)||!hasOnlyKeys(t,[`state`,`kind`,`rawPrincipalRef`])||t.state!==`present`||t.kind!==`person`||typeof t.rawPrincipalRef!==`string`||t.rawPrincipalRef.length<1||t.rawPrincipalRef.length>EXECUTION_DECISION_RAW_REF_MAX_LENGTH)throw new Error(`execution decision work violates its represented subject contract`);r={state:`present`,kind:`person`,rawPrincipalRef:t.rawPrincipalRef}}n={...r?{representedSubject:r}:{},...e.refs.resource!==void 0?{resource:parseRawRef({value:e.refs.resource,namespaces:[`credential-profile`]})}:{},...e.refs.target!==void 0?{target:parseRawRef({value:e.refs.target,namespaces:[`model-route`,`session`]})}:{}}}const r=e.receipt;const i=buildReceipt({token:t,receipt:r,...n?.resource?{resourceRef:`private-resource-ref`}:{},...n?.target?{targetRef:`private-target-ref`}:{}});if(!validateDecisionReceiptV1(i))throw new Error(`execution decision work receipt violates DecisionReceiptV1`);const a=JSON.stringify(e);if(Buffer.byteLength(a,`utf8`)>EXECUTION_DECISION_WORK_MAX_BYTES)throw new Error(`execution decision work exceeds 16 KiB`);return{workVersion:1,token:t,receipt:r,...n?{refs:n}:{}}}function processExecutionDecisionWorkInDatabase(e,t){const n=parseExecutionDecisionWork(e);const r=openOpenClawStateDatabase(t).db;const i=n.refs?.resource?pseudonymizeExecutionIdentityRef({db:r,kind:`credential`,scope:n.refs.resource.namespace,value:n.refs.resource.value}):void 0;const a=n.refs?.target?pseudonymizeExecutionIdentityRef({db:r,kind:`target`,scope:n.refs.target.namespace,value:n.refs.target.value}):void 0;const o=n.refs?.representedSubject;const s=o?pseudonymizeExecutionIdentityRef({db:r,kind:`domain`,scope:`gateway-cell`,value:`gateway-cell`}):void 0;const c=buildReceipt({token:n.token,...o&&s?{representedSubject:{state:`present`,principal:{kind:`person`,domainRef:s,principalRef:pseudonymizeExecutionIdentityRef({db:r,kind:`principal`,scope:`${s}:person`,value:o.rawPrincipalRef})}}}:{},receipt:n.receipt,...i?{resourceRef:i}:{},...a?{targetRef:a}:{}});if(!validateDecisionReceiptV1(c))throw new Error(`execution decision work projection violates DecisionReceiptV1`);return recordExecutionDecisionFactInDatabase(c,t)}function configureExecutionDecisionWorkSink(e){state.sink=e;return()=>{if(state.sink===e)state.sink=void 0}}function recordExecutionDecisionWork(e){return state.sink?.(e)??false}export{recordExecutionDecisionWork as i,parseExecutionDecisionWork as n,processExecutionDecisionWorkInDatabase as r,configureExecutionDecisionWorkSink as t};