UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

293 lines (292 loc) • 9.68 kB
import { r as normalizeAgentIdStrict } from "./agent-id-CQjtp2ft.mjs"; import "./session-key-CqAyZeVk.mjs"; import { a as deferSqliteWorkerCommitReceipt } from "./sqlite-worker-operation-settlement-OZQM-wtd.mjs"; import { f as requestSqliteWorkerOperationAdmission } from "./sqlite-worker-operation-admission-9E_X-oRy.mjs"; import { m as stripExecApprovalsSocketCredentials, s as normalizeExecApprovalsInternal } from "./exec-approvals-config-BBzEUJlE.mjs"; import { n as maxAsk, r as minSecurity, t as assertExecApprovalsHostPolicyUnchanged } from "./exec-approvals-policy-BGzNHuNm.mjs"; import { n as assertOpenClawStateLeaseWorkerOwnedInTransaction } from "./openclaw-state-lease-worker-DlXbLNzC.mjs"; import { t as resolveExecApprovalsFromFileInternal } from "./exec-approvals-resolver-C6IwmycS.mjs"; import { t as execApprovalsPublication } from "./exec-approvals-publication-CjvYUNZR.mjs"; import { c as snapshotFromExecApprovalsDatabase, f as writeExecApprovalsConfigRow, l as snapshotFromExecApprovalsRow, n as assertExecApprovalsMutationAllowed, r as deleteExecApprovalsConfigRow, s as serializeExecApprovals, t as ExecApprovalsMutationFencedError } from "./exec-approvals-sqlite-UEkhBKzL.mjs"; import { t as applyExecAuthorizationCommit } from "./exec-approvals-authorization.kernel-DITNT1Lg.mjs"; import { n as assertAgentDeletionWorkerPredicate } from "./agent-deletion.worker-DC3G_2NR.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/infra/exec-approvals-mutation.kernel.ts /** Apply the requested edit to the transaction's current policy, never a host callback. */ function applyExecApprovalsUpdate(e, t) { if (t.kind === "replace") return stripExecApprovalsSocketCredentials(t.file); if (t.kind === "ensure-agent") return e.agents?.[t.agentId] ? null : { ...e, agents: { ...e.agents, [t.agentId]: t.policy } }; const n = resolveExecApprovalsFromFileInternal({ file: t.original }).defaults; const r = resolveExecApprovalsFromFileInternal({ file: e }).defaults; const i = structuredClone(e); let a = false; for (const o of [ "security", "ask", "askFallback" ]) { const s = e.defaults?.[o]; const c = t.original.defaults?.[o]; const l = o === "ask" ? maxAsk(n.ask, r.ask) === n.ask : minSecurity(n[o], r[o]) === n[o]; if (t.policy[o] !== void 0 && s === t.written.defaults?.[o] && s !== c && l) { i.defaults = { ...i.defaults, [o]: c }; a = true; } } return a ? i : null; } //#endregion //#region src/infra/exec-approvals-mutation.worker.ts function mutate(e, t, n) { const r = e.stateOptions(); return e.write(({ db: e }) => { requestSqliteWorkerOperationAdmission({ stage: "transaction", facts: void 0 }); const t = execApprovalsPublication.capture(e, () => { const t = snapshotFromExecApprovalsDatabase(e, r.env); const i = n(t, e); let a = t; if (i.next !== null) { const n = i.raw ?? serializeExecApprovals(i.next); if (i.remove || !t.exists || t.raw !== n) { if (i.remove) { deleteExecApprovalsConfigRow(e); a = snapshotFromExecApprovalsRow({ path: t.path, env: r.env }); } else { const n = writeExecApprovalsConfigRow({ db: e, file: i.next, raw: i.raw }); a = snapshotFromExecApprovalsRow({ path: t.path, row: { raw_json: n } }); } } } return { changed: a.raw !== t.raw, snapshot: a, result: i.result(a) }; }); const { changed: i, snapshot: a, result: o } = t.result; const s = t.receipt.facts.get("current"); const c = { kind: "exec-policy-publication", file: s?.kind === "postimage" ? s.value.file : a.file, execFacts: execApprovalsPublication.bound(t.receipt) }; requestSqliteWorkerOperationAdmission({ stage: "commit", facts: c }); deferSqliteWorkerCommitReceipt(e, c, i ? "commit" : "settlement"); return o; }, { operationLabel: t }); } const execPolicyMutationOperations = { "execApprovals.update": (e, t) => mutate(t, "exec-approvals.update", (t, n) => { if (e.baseHash !== void 0 && t.hash !== e.baseHash) return { next: null, result: () => null }; const r = applyExecApprovalsUpdate(t.file, e.update); if (r) assertExecApprovalsMutationAllowed({ db: n, current: t.file, next: r }); return { next: r, result: (e) => e }; }), "execApprovals.restoreSnapshot": (e, t) => mutate(t, "exec-approvals.restore-cas", (t, n) => { if (t.hash !== e.baseHash) return { next: null, result: () => false }; assertExecApprovalsMutationAllowed({ db: n, current: t.file, next: e.snapshot.file }); return { next: e.snapshot.file, raw: e.snapshot.raw ?? serializeExecApprovals(e.snapshot.file), remove: !e.snapshot.exists, result: () => true }; }), "execApprovals.ensureSnapshot": (e, t) => { return mutate(t, "exec-approvals.ensure", (e, t) => { const n = stripExecApprovalsSocketCredentials(normalizeExecApprovalsInternal(e.file)); assertExecApprovalsMutationAllowed({ db: t, current: e.file, next: n }); return { next: n, result: (e) => e }; }); } }; //#endregion //#region src/infra/exec-approvals-retirement.worker.ts function retireAgentPolicies(e, t) { const { guard: n } = e; const r = n.predicate.agentId; if (n.lease.scope !== "core:agent-deletion" || n.lease.key !== r) throw new ExecApprovalsMutationFencedError(); return t.write((i) => { assertOpenClawStateLeaseWorkerOwnedInTransaction(i.db, n.lease); assertAgentDeletionWorkerPredicate(i, n.predicate); const a = execApprovalsPublication.capture(i.db, () => { const n = snapshotFromExecApprovalsDatabase(i.db, t.stateOptions().env); const matches = (e) => { const t = normalizeAgentIdStrict(e); return t.ok && t.value === r; }; const a = e.action === "remove" ? Object.entries(n.file.agents ?? {}).filter(([e]) => matches(e)) : e.entries; const o = { ...n.file.agents }; for (const [t, n] of a) { if (!matches(t) || e.action === "restore" && o[t] !== void 0 && !isDeepStrictEqual(o[t], n)) throw new ExecApprovalsMutationFencedError(); if (e.action === "remove") delete o[t]; else o[t] = n; } const s = { ...n.file, agents: o }; const c = serializeExecApprovals(s); const l = a.length > 0 && c !== n.raw; if (l) writeExecApprovalsConfigRow({ db: i.db, file: s, raw: c }); return { changed: l, raw: c, entries: a }; }); const { changed: o, raw: s, entries: c } = a.result; assertOpenClawStateLeaseWorkerOwnedInTransaction(i.db, n.lease, "write", "commit", { kind: "exec-approvals-retirement", nonce: e.nonce, agentId: r, operationId: n.predicate.operationId, action: e.action, raw: o ? s : null }); deferSqliteWorkerCommitReceipt(i.db, { kind: "exec-approvals-retirement", nonce: e.nonce, action: e.action, execFacts: execApprovalsPublication.bound(a.receipt) }); return c; }, { operationLabel: `exec-approvals.retirement-${e.action}` }); } const execApprovalRetirementOperations = { "execApprovals.retireAgent": retireAgentPolicies }; //#endregion //#region src/infra/exec-approvals-authorization.worker.ts function applyAuthorizationBatch(e, t, n) { let r = t; const i = n.map((t) => { let n; try { n = applyExecAuthorizationCommit(r.file, t); } catch (e) { return { ok: false, message: e instanceof Error ? e.message : String(e) }; } if (n !== null || t.requireCommit) try { assertExecApprovalsMutationAllowed({ db: e, current: r.file, next: n ?? r.file, ...t.requireCommit ? { agentId: t.agentId } : {} }); } catch (e) { if (!(e instanceof ExecApprovalsMutationFencedError)) throw e; return { ok: false, message: e.message }; } if (n !== null) { assertExecApprovalsHostPolicyUnchanged(r.file, n); const e = serializeExecApprovals(n); if (!r.exists || r.raw !== e) r = snapshotFromExecApprovalsRow({ path: r.path, row: { raw_json: e } }); } return { ok: true, file: r.file }; }); return { snapshot: r, outcomes: i }; } function commitExecAuthorizationsInWorker(e, t) { const n = t.stateOptions(); if (!e.items.some((e) => e.matches.length > 0 || e.requireCommit === true || e.allowAlwaysDecision !== void 0 && e.allowAlwaysDecision.kind !== "one-shot")) { const { db: r } = t.open(); return applyAuthorizationBatch(r, snapshotFromExecApprovalsDatabase(r, n.env), e.items).outcomes; } return t.write(({ db: t }) => { requestSqliteWorkerOperationAdmission({ stage: "transaction", facts: void 0 }); const r = execApprovalsPublication.capture(t, () => { const r = snapshotFromExecApprovalsDatabase(t, n.env); const i = applyAuthorizationBatch(t, r, e.items); if (i.snapshot.raw !== r.raw) writeExecApprovalsConfigRow({ db: t, file: i.snapshot.file, raw: i.snapshot.raw ?? void 0, change: e.items.some((e, t) => i.outcomes[t]?.ok && e.allowAlwaysDecision && e.allowAlwaysDecision.kind !== "one-shot") ? "policy" : "usage" }); return i; }); requestSqliteWorkerOperationAdmission({ stage: "commit", facts: void 0 }); deferSqliteWorkerCommitReceipt(t, { execFacts: execApprovalsPublication.bound(r.receipt) }, r.receipt.facts.size ? "commit" : "settlement"); return r.result.outcomes; }, { operationLabel: "exec-approvals.commit-authorizations" }); } const execAuthorizationOperations = { ...execApprovalRetirementOperations, ...execPolicyMutationOperations, "execApprovals.commitAuthorizations": commitExecAuthorizationsInWorker }; //#endregion export { execAuthorizationOperations };