openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
105 lines (104 loc) • 3.85 kB
JavaScript
import { c as parseAuditEntryRecord, d as reefAuditEntryKey, f as verifyReefAuditWindow, i as REEF_AUDIT_MIGRATION_KEY, l as parseAuditStateRecord, p as createAuditEntry, t as REEF_AUDIT_HEAD_KEY, u as parseReefAuditHead } from "./audit-state-format-BgwDlxZw.mjs";
//#region extensions/reef/src/audit-state-operation.ts
function readAuditHead(e, t) {
const [n, r] = e.lookupMany([{
store: t.head,
key: REEF_AUDIT_HEAD_KEY
}, {
store: t.migration,
key: REEF_AUDIT_MIGRATION_KEY
}]);
if (r !== void 0) throw new Error("Reef audit migration is incomplete; repair audit.jsonl and rerun openclaw doctor --fix");
return parseReefAuditHead(n);
}
function appendReefAuditEvents(e, t, n) {
if (n.length === 0) return [];
let r = readAuditHead(e, t);
if (r.pending && r.pending.expiresAt > Date.now()) throw new Error("Reef audit append is owned by an active legacy writer");
const i = r.pending?.entryKey;
if (i && (!i.startsWith("entry:") || i === "entry:")) throw new Error("invalid Reef audit staged entry key");
let a = r;
const o = n.map(({ type: e, payload: n, ts: r }) => {
const i = createAuditEntry(e, n, r, t.auditKey, a);
a = {
...a,
hash: i.entryHash,
seq: i.event.seq
};
return i;
});
const s = [.../* @__PURE__ */ new Set([
...o.map((e) => reefAuditEntryKey(e.entryHash)),
...r.hash ? [reefAuditEntryKey(r.hash)] : [],
...r.seq >= t.maxEntries ? [reefAuditEntryKey(r.oldestHash)] : []
])];
const c = e.lookupMany(s.map((e) => ({
store: t.entries,
key: e
})));
const l = new Map(s.map((e, t) => [e, c[t]]));
const u = /* @__PURE__ */ new Map();
const read = (n) => {
if (u.has(n)) return u.get(n);
if (!l.has(n)) l.set(n, e.lookup(t.entries, n));
return l.get(n);
};
for (const e of [i, r.garbageEntryKey]) if (e) u.set(e, void 0);
for (const e of o) {
const n = reefAuditEntryKey(e.entryHash);
if (read(n) !== void 0) throw new Error("Reef audit entry already exists before head advancement");
const a = r.hash ? reefAuditEntryKey(r.hash) : void 0;
if (a) {
const t = parseAuditStateRecord(read(a));
if (t.entry.entryHash !== r.hash) throw new Error("Reef audit head entry differs before linking append");
if (t.nextHash !== void 0 && reefAuditEntryKey(t.nextHash) !== i) throw new Error("Reef audit head already links a committed successor");
u.set(a, {
...t,
nextHash: e.entryHash
});
}
let o = r.seq === 0 ? e.entryHash : r.oldestHash;
if (r.seq >= t.maxEntries) {
const e = reefAuditEntryKey(r.oldestHash);
const t = parseAuditStateRecord(read(e));
if (!t.nextHash) throw new Error("Reef audit retention pointer is missing");
o = t.nextHash;
u.set(e, void 0);
}
u.set(n, {
kind: "entry",
entry: e
});
r = {
kind: "head",
hash: e.entryHash,
seq: e.event.seq,
oldestHash: o
};
}
for (const [n, r] of u) if (r === void 0 && (!l.has(n) || l.get(n) !== void 0)) e.delete(t.entries, n);
for (const [n, r] of u) if (r !== void 0) e.set(t.entries, n, r);
e.set(t.head, REEF_AUDIT_HEAD_KEY, r);
return o;
}
function readReefAuditEntries(e, t) {
const n = readAuditHead(e, t);
if (n.seq === 0) return [];
const r = new Map(e.entries(t.entries).map(({ key: e, value: t }) => [e, t]));
const i = [];
let a = n.hash;
for (let e = n.seq; e > 0 && i.length < t.maxEntries; e--) {
const t = r.get(reefAuditEntryKey(a));
if (!t) break;
const n = parseAuditEntryRecord(t);
if (n.entryHash !== a || n.event.seq !== e) throw new Error("invalid Reef audit chain state");
i.push(n);
a = n.prevHash;
}
return verifyReefAuditWindow(i, n, t.maxEntries);
}
function executeReefAuditOperation(e, t) {
return e.type === "append" ? appendReefAuditEvents(t, e.input, e.input.events) : readReefAuditEntries(t, e.input);
}
//#endregion
export { executeReefAuditOperation as n, appendReefAuditEvents as t };