UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

76 lines (75 loc) • 3.49 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { r as sqlPublicationAnchor } from "./sql-connection-Beiyg5Dt.mjs"; import { h as resolveDatabasePathKey, m as readDatabasePathIdentitySync, u as normalizeDatabasePath } from "./sqlite-worker-identity-BH8ZkJU9.mjs"; import { s as getSqliteDatabaseAdmissionIdentityForPath } from "./sqlite-database-admission-DQIqro3d.mjs"; import { statSync } from "node:fs"; import { randomUUID } from "node:crypto"; //#region src/state/openclaw-agent-db-identity.ts const identities = resolveGlobalSingleton(Symbol.for("openclaw.agentDatabaseIdentities"), () => /* @__PURE__ */ new WeakMap()); /** Prepare physical and connection identity once at open; cached aliases are not resolved again. */ function registerOpenClawAgentDatabaseIdentity(e, t) { const n = sqlPublicationAnchor(e); const r = normalizeDatabasePath(n.location() ?? ""); const i = r ? getSqliteDatabaseAdmissionIdentityForPath(r) : void 0; const a = i ? { ...i, canonicalPath: resolveDatabasePathKey(r) } : r ? readDatabasePathIdentitySync(r) : void 0; if (!a || !a.key.startsWith("file:")) throw new Error("OpenClaw agent database requires a physical file for identity registration"); const o = a.key.slice(5); identities.set(n, { identity: o, birthtime: a.birthtime, incarnation: randomUUID(), filename: r, canonicalPath: a.canonicalPath, ownerDatabasePath: t ? resolveDatabasePathKey(t) : void 0 }); } /** Reuse facts captured at open; aliases must never be resolved again at a handoff. */ function readOpenClawAgentDatabaseIdentity(e) { const t = findOpenClawAgentDatabaseIdentity(e); if (t === void 0) throw new Error("OpenClaw agent database identity was not prepared at open"); return t; } /** Raw diagnostic connections have no admitted physical identity. */ function findOpenClawAgentDatabaseIdentity(e) { return identities.get(sqlPublicationAnchor(e.db)); } /** A retained connection can outlive its pathname or be deserialized away from that file. */ function isOpenClawAgentDatabasePathCurrent(e) { const t = sqlPublicationAnchor(e.db); if (!e.db.isOpen || !t.isOpen) return false; const { identity: n, filename: r } = readOpenClawAgentDatabaseIdentity(e); if (normalizeDatabasePath(t.location() ?? "") !== r) return false; const i = statSync(e.path, { bigint: true, throwIfNoEntry: false }); return i !== void 0 && n === `${i.dev}:${i.ino}`; } function assertOpenClawAgentDatabaseIdentity(e, t) { const n = readOpenClawAgentDatabaseIdentity(e); if (`file:${n.identity}` !== t.key || t.birthtime !== void 0 && n.birthtime !== t.birthtime || !isOpenClawAgentDatabasePathCurrent(e)) throw new Error("Agent database changed during repair admission"); } function createOpenClawAgentDatabaseClaim(e, t) { let n = false; const isCurrent = () => !n && e.db.isOpen && sqlPublicationAnchor(e.db).isOpen; const { identity: r, incarnation: i } = readOpenClawAgentDatabaseIdentity(e); return { identity: r, incarnation: i, isCurrent, assertCurrent: () => { if (!isCurrent()) throw new Error("OpenClaw agent database claim is no longer current"); }, release: () => { if (!n) { n = true; t(); } } }; } //#endregion export { readOpenClawAgentDatabaseIdentity as a, isOpenClawAgentDatabasePathCurrent as i, createOpenClawAgentDatabaseClaim as n, registerOpenClawAgentDatabaseIdentity as o, findOpenClawAgentDatabaseIdentity as r, assertOpenClawAgentDatabaseIdentity as t };