UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

174 lines (173 loc) • 6.32 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { r as openOpenClawStateDatabase } from "./openclaw-state-db-ARhwdPJg.mjs"; import { a as parseExecutionIdentityAdmissionToken } from "./execution-identity-admission-EuMO_R29.mjs"; import { t as validateDecisionReceiptV1 } from "./audit-run-validators-DQArxEAR.mjs"; import { n as recordExecutionDecisionFactInDatabase, o as pseudonymizeExecutionIdentityRef } from "./execution-decision-facts-CD9zMcWU.mjs"; //#region src/audit/execution-decision-work.ts /** Private, bounded decision work projected by the canonical audit writer. */ 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 } : {} } }; } /** Revalidate closed work before queue cloning, key access, or database access. */ 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 } : {} }; } /** Project raw private refs at the audit owner, then persist only the bounded receipt. */ 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); } /** Install the current process writer sink; callers never create a second writer. */ function configureExecutionDecisionWorkSink(e) { state.sink = e; return () => { if (state.sink === e) state.sink = void 0; }; } /** Offer one private work item to the lifecycle-owned FIFO. */ function recordExecutionDecisionWork(e) { return state.sink?.(e) ?? false; } //#endregion export { recordExecutionDecisionWork as i, parseExecutionDecisionWork as n, processExecutionDecisionWorkInDatabase as r, configureExecutionDecisionWorkSink as t };