UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

191 lines (190 loc) • 7.7 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { n as ok, t as err } from "./result-CV1-31DB.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import { c as resolveOpenClawAgentSqlitePath } from "./openclaw-agent-db.paths-DJxv80ws.mjs"; import { m as readDatabasePathIdentitySync } from "./sqlite-worker-identity-BH8ZkJU9.mjs"; import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs"; import path from "node:path"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/state/agent-deletion-cleanup.ts const databaseCleanup = resolveGlobalSingleton(Symbol.for("openclaw.agentDeletionDatabaseCleanup"), () => new AsyncLocalStorage()); const cleanupHandles = resolveGlobalSingleton(Symbol.for("openclaw.agentDeletionDatabaseCleanupHandles"), () => /* @__PURE__ */ new Map()); const cleanupExecutions = resolveGlobalSingleton(Symbol.for("openclaw.agentDeletionDatabaseCleanupExecutions"), () => /* @__PURE__ */ new Map()); /** The lifecycle owner supplies live closures, never a transferable operation id. */ function createAgentDeletionDatabaseCleanup(e) { return async (t, n) => { const r = normalizeAgentId(t.agentId); const i = path.resolve(t.path); const a = readDatabasePathIdentitySync(i); const assertTargetCurrent = () => { const e = readDatabasePathIdentitySync(i); if (e.key !== a.key || e.birthtime !== a.birthtime || e.canonicalPath !== a.canonicalPath) throw new Error("Agent deletion database target changed before cleanup"); }; const runCleanup = async (t) => { let o = true; const s = /* @__PURE__ */ new Set(); const closeHandles = async () => { const e = []; for (const t of [...s].toReversed()) try { await t(); s.delete(t); } catch (t) { e.push(t); } if (e.length === 0) cleanupExecutions.delete(c); return e; }; const assertActive = () => { if (!o) throw new Error("Agent deletion database cleanup is no longer active."); assertTargetCurrent(); }; const assertCurrentAsync = async () => { assertActive(); if (t && e.assertCurrentAsync) { await e.assertCurrentAsync(); assertActive(); t.lease.assertCurrent(); } else e.assertCurrent(); }; const c = { agentId: r, ownsDatabase: r === normalizeAgentId(e.agentId), path: i, canonicalPath: a.canonicalPath, statePath: path.resolve(e.statePath), worker: t, assertCurrentHost: () => { assertActive(); if (t) { t.lease.assertCurrent(); e.workerAuthority.assertCurrentHost(); } else e.assertCurrent(); }, assertCurrent: () => { assertActive(); e.assertCurrent(); }, assertJournal: (t, n) => { assertActive(); return e.assertJournal(t, n); }, registerClose: (e) => { assertActive(); s.add(e); }, retryClose: async () => { if (o) throw new Error("Agent database belongs to an active deletion cleanup."); const e = await closeHandles(); if (e.length > 0) throw new AggregateError(e, "Agent deletion database close retry failed."); }, withCommit: (t) => { assertActive(); e.withCommit(t); } }; return await databaseCleanup.run(c, async () => { let r; const i = []; try { await assertCurrentAsync(); for (const e of /* @__PURE__ */ new Set([...cleanupHandles.values(), ...cleanupExecutions.values()])) if (e.statePath === c.statePath && e.agentId === c.agentId && e.path === c.path) { await e.retryClose(); await assertCurrentAsync(); } await e.assertAdmission(); c.assertCurrentHost(); if (c.ownsDatabase) cleanupExecutions.set(c, c); const t = await n(); o = false; r = ok(t); } catch (e) { r = err(e); } finally { o = false; i.push(...await closeHandles()); } if (!r.ok) throw i.length > 0 ? new AggregateError([r.error, ...i], "Agent deletion database cleanup failed.") : r.error; if (i.length > 0) throw i.length === 1 ? i[0] : new AggregateError(i, "Agent deletion database cleanup failed."); if (t && e.assertCurrentAsync) await e.assertCurrentAsync(); else e.assertCurrent(); assertTargetCurrent(); return r.value; }); }; return e.workerAuthority ? e.workerAuthority.runWithLeaseAdmission((e, t) => runCleanup({ lease: e, guard: t })) : runCleanup(); }; } /** The canonical agent executor owns native close and settlement in its worker. */ function withAgentDeletionWorkerDatabaseCleanup(e, t) { return databaseCleanup.run({ ...e, workerOwned: true, assertCurrentHost: e.assertCurrent, registerClose: () => {}, retryClose: async () => {} }, t); } function getAgentDeletionDatabaseCleanup(e) { const t = databaseCleanup.getStore(); if (!t) return; const n = resolveOpenClawAgentSqlitePath(e); if (t.agentId !== normalizeAgentId(e.agentId) || t.path !== n && t.canonicalPath !== n) return; const r = e.statePath ?? resolveOpenClawStateSqlitePath(e.env ?? process.env); if (t.statePath !== path.resolve(r)) throw new Error("Agent deletion database cleanup belongs to another state database."); return t; } /** Resource registration captures only the cleanup scope that admitted this target. */ function captureAgentDeletionResourceOwner(e) { const t = databaseCleanup.getStore(); if (!t?.ownsDatabase || !e(t) && (!t.canonicalPath || !e({ agentId: t.agentId, path: t.canonicalPath }))) return; t.assertCurrentHost(); return t; } function assertAgentDeletionDatabaseCleanupAccess(e, t) { const n = getAgentDeletionDatabaseCleanup(t); const r = cleanupHandles.get(e); if (r && r !== n) throw new Error("Agent database belongs to an active deletion cleanup."); n?.assertCurrent(); } function assertAgentDeletionCleanupAliases(e, t) { const n = resolveOpenClawAgentSqlitePath(e); for (const r of cleanupHandles.keys()) if (t(r.path, n)) assertAgentDeletionDatabaseCleanupAccess(r, e); for (const r of cleanupExecutions.values()) if (t(r.path, n) && getAgentDeletionDatabaseCleanup(e) !== r) throw new Error("Agent database belongs to an active deletion cleanup."); } function assertAgentDeletionExecutionCleanupAccess(e, t) { const n = cleanupExecutions.get(e); const r = getAgentDeletionDatabaseCleanup(t); if (n && n !== r) throw new Error("Agent database belongs to an active deletion cleanup."); r?.assertCurrentHost(); } /** Only a newly created executor belongs to cleanup; borrowing never transfers ownership. */ function registerAgentDeletionExecutionCleanup(e, t, n) { const r = getAgentDeletionDatabaseCleanup(t); if (!r?.worker || !r.ownsDatabase) return; r.assertCurrentHost(); if (cleanupExecutions.get(e) === r) return; cleanupExecutions.set(e, r); r.registerClose(async () => { await n(); cleanupExecutions.delete(e); }); } function registerAgentDeletionDatabaseCleanup(e, t) { const n = getAgentDeletionDatabaseCleanup(t); n?.assertCurrent(); if (!n?.ownsDatabase || n.workerOwned) return; cleanupHandles.set(e, n); return n; } /** Release the tag only after the native owner has closed and released its lease. */ function releaseAgentDeletionDatabaseCleanup(e) { cleanupHandles.delete(e); } //#endregion export { createAgentDeletionDatabaseCleanup as a, registerAgentDeletionExecutionCleanup as c, captureAgentDeletionResourceOwner as i, releaseAgentDeletionDatabaseCleanup as l, assertAgentDeletionDatabaseCleanupAccess as n, getAgentDeletionDatabaseCleanup as o, assertAgentDeletionExecutionCleanupAccess as r, registerAgentDeletionDatabaseCleanup as s, assertAgentDeletionCleanupAliases as t, withAgentDeletionWorkerDatabaseCleanup as u };