UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

179 lines (178 loc) • 6.64 kB
import { r as resolveGlobalMap } from "./global-singleton-D7yf05nR.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { r as sqlPublicationAnchor } from "./sql-connection-Beiyg5Dt.mjs"; import { n as SQLITE_WORKER_MAX_MESSAGE_BYTES } from "./sqlite-worker-contract--dxXkOty.mjs"; import { a as deferSqliteWorkerCommitReceipt, s as observeSqliteWorkerAdmissionFinish } from "./sqlite-worker-operation-settlement-OZQM-wtd.mjs"; import { a as observeSqliteWorkerCommittedFacts } from "./sqlite-worker-operation-admission-9E_X-oRy.mjs"; import { x as registerOpenClawStateDatabaseLifecycleListener } from "./openclaw-state-db-cache-CkwtTqzF.mjs"; import { n as createKeyedStateDomainPublication } from "./state-domain-publication-DiGC-tAS.mjs"; import { serialize } from "node:v8"; //#region src/state/github-publication-receipts.ts function isPublicationFact(e, t, n) { const r = JSON.parse(e); return Array.isArray(r) && r.length === 2 && (n ? [ "shared", "personal", "repository", "shared-lifecycle", "personal-lifecycle" ] : [ "personal", "repository", "personal-lifecycle" ]).includes(r[0]) && typeof r[1] === "string" && (t.kind === "absent" || n && t.kind === "postimage" && isRecord(t.value) && t.value.request_id === r[1]); } const publication = createKeyedStateDomainPublication({ domain: "github-publication", isFact: (e, t) => isPublicationFact(e, t, true), invalidReceiptMessage: "GitHub publication authority receipt is invalid", installFailureMessage: "GitHub publication fact installation failed" }); /** Exact owner writes only: arbitrary raw SQL remains outside this coverage. */ const githubPublicationReceipts = { stageRow: stageGitHubPublicationRow, subscribeFacts: (e) => publication.subscribeFacts((t) => e(t, t.kind === "pending" && typeof t.identity === "string" && owners.get(t.identity)?.pendingDeletion === true)) }; const owners = resolveGlobalMap(Symbol.for("openclaw.githubPublicationLifecycleAuthority"), "close-and-restart"); function ownerFor(e) { let t = owners.get(e); if (!t) { t = { cells: /* @__PURE__ */ new Map(), pending: /* @__PURE__ */ new Set(), uncertain: false }; owners.set(e, t); } return t; } githubPublicationReceipts.subscribeFacts((e, t) => { const n = e.kind === "committed" ? e.receipt.source.identity : e.identity; if (typeof n !== "string") return; const r = ownerFor(n); if (e.kind === "pending") { if (t) r.pending.add(e.operationId); } else if (e.kind === "settled") r.pending.delete(e.operationId); else if (e.kind === "unknown") { r.uncertain = true; r.cells.clear(); } else if (e.kind === "committed") { for (const [t, n] of e.receipt.facts) if (n.kind !== "postimage") r.cells.delete(t); else if ("lifecycle_revision" in n.value) { const e = r.cells.get(t); const i = e && !e.loaded ? e : { loaded: false }; i.value = { lifecycle_revision: n.value.lifecycle_revision, requester_authority_json: n.value.requester_authority_json }; i.loaded = true; r.cells.set(t, i); } } }); registerOpenClawStateDatabaseLifecycleListener((e) => { if (e.kind !== "opened") { if (e.identity) owners.delete(e.identity.key); else owners.clear(); } }); /** Capture before the cold read; this module observes pending even before the first reader. */ function captureGitHubPublicationLifecycle(e, t) { const n = e.admission.identity.key; const r = ownerFor(n); const i = JSON.stringify([`${t.publicationKind}-lifecycle`, t.requestId]); let a = r.cells.get(i); if (!a) { a = { loaded: false }; r.cells.set(i, a); } const o = a; const readCurrent = () => { e.admission.assertCurrent(); if (owners.get(n) !== r || r.cells.get(i) !== o || r.pending.size || r.uncertain) throw new Error("GitHub publication lifecycle authority is unavailable; retry after owner recovery"); return o.value && { ...o.value }; }; readCurrent(); return { loaded: o.loaded, readCurrent, observe(e) { readCurrent(); if (!o.loaded) { o.value = e; o.loaded = true; } } }; } function deferGitHubPublicationDeletionReceipt(e, t) { const n = publication.receipt(sqlPublicationAnchor(e), t); deferSqliteWorkerCommitReceipt(sqlPublicationAnchor(e), serialize(n).byteLength <= 33554432 ? n : { kind: "unknown", identity: n.source.identity }, t.size ? "commit" : "settlement"); } /** Capture the same authority rows as native writes, including their lifecycle sidecars. */ function captureGitHubPublicationWorkerReceipt(e, t) { const { result: n, receipt: r } = publication.capture(sqlPublicationAnchor(e), t); const i = { ...n, authority: r }; if (serialize(i).byteLength > 33554432) i.authority = { kind: "unknown", identity: r.source.identity }; return i; } function stageGitHubPublicationRow(e, t, n) { const r = { ...n }; delete r.title; delete r.body; delete r.next_action; publication.stagePostimages(sqlPublicationAnchor(e), [[JSON.stringify([t, n.request_id]), r]]); } /** The deletion worker sends tombstones, including lifecycle rows, before its ordinary reply. */ function withGitHubPublicationDeletionReceipt(e, t) { return withGitHubPublicationReceipt(e, t, false); } /** Register before presentation observers so every reply sees installed authority facts. */ function withGitHubPublicationWorkerReceipt(e, t, n) { return withGitHubPublicationReceipt(e, t, true, n); } function withGitHubPublicationReceipt(e, t, n, r) { return (i) => { const a = e(i); let o; const s = ownerFor(t.admission.identity.key); const c = s.pendingDeletion; try { s.pendingDeletion = !n; o = publication.begin({ identity: t.admission.identity.key, assertCurrent: t.assertPublicationCurrent ?? t.admission.assertCurrent }, n ? void 0 : (e, t) => isPublicationFact(e, t, false)); } catch (e) { a.admission.finish(); throw e; } finally { s.pendingDeletion = c; } observeSqliteWorkerAdmissionFinish(a.admission, () => { if (a.admission.settlement) { const e = a.admission.settlement.kind === "completed"; o.finish(e, e && !a.admission.committed); } }); i.settled.then((e) => { const t = e.kind !== "unknown"; o.finish(t, t && !a.admission.committed); }); observeSqliteWorkerCommittedFacts(a.admission, ({ facts: e }) => { o.committed(n && isRecord(e) ? e.authority : e, () => r?.(e)); }); return a; }; } //#endregion export { withGitHubPublicationDeletionReceipt as a, githubPublicationReceipts as i, captureGitHubPublicationWorkerReceipt as n, withGitHubPublicationWorkerReceipt as o, deferGitHubPublicationDeletionReceipt as r, captureGitHubPublicationLifecycle as t };