UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

113 lines (112 loc) • 4.21 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { l as isRecord, u as isStringArray } from "./record-coerce-CVPuDqcc.mjs"; import { a as throwIfSettledRejected } from "./error-graph-internal-DdjdM90N.mjs"; import path from "node:path"; //#region src/infra/agent-database-readers.ts const readers = resolveGlobalSingleton(Symbol.for("openclaw.agentDatabaseReaders"), () => ({ closers: /* @__PURE__ */ new Set(), deleted: /* @__PURE__ */ new Map() })); /** Match captured read custody without inspecting files or inferring their owners. */ function matchesAgentDatabaseReadCandidatePath(e, t) { if (e.path === t) return true; const n = path.resolve(e.path); const r = path.resolve(t); if (n === r) return true; if (e.scope !== "sibling-family") return false; const i = path.parse(n); const a = path.parse(r); return a.dir === i.dir && a.base.startsWith(`${i.name}.`) && a.base.endsWith(i.ext); } /** Every cache that retains agent database connections registers once per isolate. */ function registerAgentDatabaseReaderCloser(e) { readers.closers.add(e); return () => { readers.closers.delete(e); }; } /** A deleted agent's database stays closed in this isolate until the roster admits the agent again. */ function isDeletedAgentDatabasePath(e, t) { const n = readers.deleted.get(path.resolve(e)); return n !== void 0 && (t === void 0 || n === t); } /** Task admission carries the parent owner's current fences into every worker generation. */ function captureDeletedAgentDatabaseFences() { return [...readers.deleted]; } function installDeletedAgentDatabaseFences(e) { readers.deleted.clear(); for (const [t, n] of e) readers.deleted.set(t, n); } async function applyAgentDatabaseReaderRequest(e) { if (e.kind === "revive") { for (const [t, n] of readers.deleted) if (e.agentIds.includes(n)) readers.deleted.delete(t); return; } if (e.deleted) for (const t of e.candidates) readers.deleted.set(path.resolve(t.path), e.agentId); const t = e.deleted ? void 0 : new Set(e.retainedPaths?.map((e) => path.resolve(e))); const n = await Promise.allSettled([...readers.closers].map(async (n) => n(e.candidates, t))); throwIfSettledRejected(n, "Agent database reader cleanup failed"); } function normalizeCandidates(e) { if (!Array.isArray(e) || !e.every((e) => isRecord(e) && typeof e.path === "string" && (e.scope === void 0 || e.scope === "sibling-family"))) return; return e.map((e) => e.scope ? { path: e.path, scope: e.scope } : { path: e.path }); } function encodeAgentDatabaseReaderRequest(e) { if (e.kind === "revive") return JSON.stringify({ revive: e.agentIds }); const t = normalizeCandidates(e.candidates) ?? []; return e.deleted ? JSON.stringify({ deleted: t, agentId: e.agentId }) : JSON.stringify(e.retainedPaths?.length ? { candidates: t, retainedPaths: e.retainedPaths } : t); } /** Worker resource keys that name agent databases; other keys belong to their worker's own closer. */ function decodeAgentDatabaseReaderRequest(e) { if (e === void 0) return; let t; try { t = JSON.parse(e); } catch { return; } if (Array.isArray(t)) { const e = normalizeCandidates(t); return e ? { kind: "close", candidates: e, deleted: false } : void 0; } if (!isRecord(t)) return; if ("candidates" in t && !("deleted" in t)) { const e = normalizeCandidates(t.candidates); const n = t.retainedPaths; return e && isStringArray(n) ? { kind: "close", candidates: e, retainedPaths: n, deleted: false } : void 0; } if ("deleted" in t) { const e = normalizeCandidates(t.deleted); return e && typeof t.agentId === "string" && t.agentId.length > 0 ? { kind: "close", candidates: e, deleted: true, agentId: t.agentId } : void 0; } if ("revive" in t && isStringArray(t.revive)) return { kind: "revive", agentIds: t.revive }; } //#endregion export { installDeletedAgentDatabaseFences as a, registerAgentDatabaseReaderCloser as c, encodeAgentDatabaseReaderRequest as i, captureDeletedAgentDatabaseFences as n, isDeletedAgentDatabasePath as o, decodeAgentDatabaseReaderRequest as r, matchesAgentDatabaseReadCandidatePath as s, applyAgentDatabaseReaderRequest as t };