openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
147 lines (146 loc) • 6.06 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { a as isSameOpenClawAgentDatabasePath } from "./openclaw-agent-db.paths-DJxv80ws.mjs";
import { m as readDatabasePathIdentitySync, n as assertDatabasePathIdentity } from "./sqlite-worker-identity-BH8ZkJU9.mjs";
import { u as throwSqliteLifecycleErrors } from "./sqlite-lifecycle-errors-D8BU4VLa.mjs";
import { u as withAgentDatabaseCloseFence } from "./openclaw-agent-db-resources-Dp8HvLsg.mjs";
import { a as observeSqliteWorkerCommittedFacts } from "./sqlite-worker-operation-admission-9E_X-oRy.mjs";
import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs";
import { n as createSqliteWorkerWriteAdmission } from "./sqlite-worker-store-D51tD0Vp.mjs";
import { i as runOpenClawStateWorkerOperation } from "./openclaw-state-worker-store-2LoAv_8B.mjs";
import { d as prepareOpenClawAgentDatabaseRegistrySnapshotRead, t as captureOpenClawAgentDatabaseRegistration } from "./openclaw-agent-db-registry-listing-CbXBL50a.mjs";
import { m as invalidateOpenClawAgentDatabaseValidation } from "./openclaw-agent-db-validation-cache-lnjWuypr.mjs";
import { _ as revokePendingAgentDatabaseOpen, o as closeOpenClawAgentDatabaseByPathAsync, t as cache } from "./openclaw-agent-db-lifecycle-DqaZZnYh.mjs";
import path from "node:path";
//#region src/state/openclaw-agent-db-disposal.ts
/** Join one unambiguous transient owner before removing its durable registration. */
async function disposeOpenClawAgentDatabaseByPath(e, t = {}) {
const n = path.resolve(e);
const r = [...cache.activePending].filter((e) => isSameOpenClawAgentDatabasePath(e.path, n));
const i = [...cache.retainedCloses].filter((e) => isSameOpenClawAgentDatabasePath(e.path, n));
const a = [...cache.databases.values()].filter((e) => isSameOpenClawAgentDatabasePath(e.path, n));
const o = a[0];
const s = readDatabasePathIdentitySync(n);
const c = a.length <= 1 && (s.key.startsWith("file:") || o) ? captureOpenClawStateWorkerContext({ env: t.env ?? (o && cache.leases.get(o.path)?.env) }) : void 0;
const l = [.../* @__PURE__ */ new Set([
n,
s.canonicalPath,
...r.map((e) => e.path),
...i.map((e) => e.path),
...a.map((e) => e.path)
])];
const assertCurrent = () => {
c?.admission.assertCurrent();
c?.maintenanceScope?.assertAdmission();
for (const e of l) assertDatabasePathIdentity(e, s);
};
const dispose = async () => {
for (const e of l) invalidateOpenClawAgentDatabaseValidation(e);
for (const e of r) revokePendingAgentDatabaseOpen(e.path, e.agentId);
for (const e of i) e.close();
await Promise.allSettled(r.map((e) => e.promise));
assertCurrent();
if (a.length > 1) throw new Error(`Agent database disposal has multiple native owners: ${n}`);
const closeCapturedPaths = async (e) => {
assertCurrent();
const t = e ? [.../* @__PURE__ */ new Set([...l, e])] : l;
const n = await Promise.allSettled(t.map((e) => closeOpenClawAgentDatabaseByPathAsync(e)));
assertCurrent();
throwSqliteLifecycleErrors(n.flatMap((e) => e.status === "rejected" ? [e.reason] : []), "Agent disposal resource drainage failed");
return n.some((e) => e.status === "fulfilled" && e.value);
};
if (!c) {
await closeCapturedPaths();
return false;
}
let e = o && {
agentId: o.agentId,
path: o.path
};
if (!e) {
const t = await prepareOpenClawAgentDatabaseRegistrySnapshotRead({ env: c.environment }).read();
assertCurrent();
t.assertCurrent();
if (t.result.status !== "available") throw new Error("Transient agent disposal cannot read its shared registry");
const [r, ...i] = t.result.entries.filter((e) => isSameOpenClawAgentDatabasePath(e.path, n));
if (i.length) throw new Error(`Agent database disposal has multiple registered owners: ${n}`);
if (!r) {
await closeCapturedPaths();
return false;
}
e = r;
}
const t = e;
const assertOwned = () => {
assertCurrent();
assertDatabasePathIdentity(t.path, s);
};
return withAgentDatabaseCloseFence({ path: t.path }, async () => {
assertOwned();
invalidateOpenClawAgentDatabaseValidation(t.path);
await closeCapturedPaths(t.path);
assertOwned();
const e = captureOpenClawAgentDatabaseRegistration({
kind: "remove",
agentId: t.agentId,
agentPath: t.path,
admission: c.admission,
assertPublicationCurrent: c.assertPublicationCurrent
});
let n;
let r = false;
const record = (t) => {
e.recordCommitted(t);
r = true;
};
const i = [];
try {
const i = await runOpenClawStateWorkerOperation(c, (e) => e.execute({
type: "agentDatabaseRegistry.remove",
input: {
agentId: t.agentId,
agentPath: t.path,
identity: s
}
}), {
assertCurrent: assertOwned,
createAdmission(r) {
n = r.settled;
const i = createSqliteWorkerWriteAdmission((t) => {
assertOwned();
if (t.stage === "transaction") e.begin();
}, [
c.admission.databasePath,
t.path,
...l
])(r);
observeSqliteWorkerCommittedFacts(i.admission, ({ facts: e }) => {
if (!isRecord(e) || typeof e.agentId !== "string" || typeof e.agentPath !== "string" || typeof e.stateDatabasePath !== "string" || typeof e.stateDatabaseIdentity !== "string") throw new Error("Agent disposal returned an invalid registry receipt");
record({
agentId: e.agentId,
agentPath: e.agentPath,
stateDatabasePath: e.stateDatabasePath,
stateDatabaseIdentity: e.stateDatabaseIdentity
});
});
return i;
}
});
if (!r) record(i);
} catch (e) {
i.push(e);
}
try {
e.finish(await n);
} catch (e) {
i.push(e);
}
throwSqliteLifecycleErrors(i, "Agent disposal and registry publication failed");
assertOwned();
return true;
});
};
const fence = (e) => e === l.length ? dispose() : withAgentDatabaseCloseFence({ path: l[e] }, () => fence(e + 1));
return fence(0);
}
//#endregion
export { disposeOpenClawAgentDatabaseByPath as t };