openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
249 lines (248 loc) • 10.3 kB
JavaScript
import { a as resolveRequiredHomeDir, c as resolveUserPath, t as expandHomePrefix } from "./home-dir-EKmgmdPG.mjs";
import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs";
import { c as resolveOpenClawAgentSqlitePath, s as resolveIncognitoOpenClawAgentSqlitePath } from "./openclaw-agent-db.paths-DJxv80ws.mjs";
import { n as listSqliteTargetCandidatePathsForSessionStorePath } from "./session-sqlite-target-paths-Dw0pT36E.mjs";
import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs";
import { F as resolveRegisteredAgentIdForDir, L as unregisterResolvedAgentDir, M as isPathOwnedByAnotherRegisteredAgent, N as normalizeAgentDirRegistryPath, P as registerResolvedAgentDir, a as resolveAgentDir } from "./agent-scope-config-DmXgFGKP.mjs";
import { m as readDatabasePathIdentitySync } from "./sqlite-worker-identity-BH8ZkJU9.mjs";
import { _ as withSqliteReadOnlyWorkerScope, f as runSqliteReadOnlyOperation } from "./sqlite-readonly-worker-D3pRJU7r.mjs";
import { a as resolveSqliteDatabaseFilePaths } from "./sqlite-files-rWisKj_V.mjs";
import { r as cloneEnvWithPlatformSemantics } from "./config-env-vars-ByeGN-9N.mjs";
import { n as executeExistingOpenClawStateRead } from "./openclaw-state-db-readonly-B6YHb1oF.mjs";
import { n as captureOpenClawStateReadWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { n as resolveSessionStoreCompatibilityAgentId } from "./legacy.default-agent-owner-DpmHgK0A.mjs";
import { r as assertNoAgentDatabaseLeasesAsync } from "./agent-deletion-journal-DgNbVVSp.mjs";
import { d as prepareOpenClawAgentDatabaseRegistrySnapshotRead } from "./openclaw-agent-db-registry-listing-CbXBL50a.mjs";
import { o as closeOpenClawAgentDatabaseByPathAsync } from "./openclaw-agent-db-lifecycle-DqaZZnYh.mjs";
import "./openclaw-agent-db-CwXW4hGa.mjs";
import { r as closeAuthProfileReadPool } from "./sqlite-read-pool-C0HEzJkG.mjs";
import { t as memorySessionActorOwners } from "./session-actor-memory-owner-BkB9Vtu4.mjs";
import { t as findOverlappingWorkspaceAgentIds } from "./agent-delete-safety-CGn0sYfd.mjs";
import path from "node:path";
import { isDeepStrictEqual } from "node:util";
//#region src/agents/agent-delete-session-store-safety.ts
var AgentSharedStoreOwnerError = class extends Error {};
function assertAgentSessionStoreDeletionBlocker(e, t) {
if (t !== void 0) throw new AgentSharedStoreOwnerError(`Agent "${normalizeAgentId(e)}" owns the session database still used by agent "${t}" and cannot be deleted. Keep this owner configured until shared history can be moved with a supported migration; no such migration is currently available.`);
}
//#endregion
//#region src/agents/agent-delete-session-store-safety.targets.ts
function captureCandidates(e) {
return [...new Set(e.flatMap((e) => listSqliteTargetCandidatePathsForSessionStorePath(e)))].toSorted().map((e) => ({
path: e,
identity: readDatabasePathIdentitySync(e)
}));
}
/** Retain lexical families, physical aliases, and absences before discovery yields. */
function prepareAgentSessionStoreDeletionSafety(e, t, n) {
const r = normalizeAgentId(t);
const i = cloneEnvWithPlatformSemantics(n);
const a = structuredClone(e);
const o = e.session?.store;
if (o?.trim()) {
a.agents = {
...a.agents,
defaults: {
...a.agents?.defaults,
sessionStore: { agentId: resolveSessionStoreCompatibilityAgentId(e) }
}
};
a.session = {
...a.session,
store: path.resolve(resolveUserPath("."), o.startsWith("~") ? expandHomePrefix(o, {
home: resolveRequiredHomeDir(i),
env: i
}) : o)
};
}
const s = o?.trim() ? listAgentIds(a).filter((e) => normalizeAgentId(e) !== r).map((e) => resolveSessionStorePathCore(a.session?.store, {
agentId: e,
env: i
})) : [];
return {
config: a,
agentId: r,
env: i,
targets: {
stores: s,
candidates: captureCandidates(s)
}
};
}
function assertAgentSessionStoreDeletionTargetsCurrent(e) {
if (!isDeepStrictEqual(captureCandidates(e.stores), e.candidates)) throw new Error("Agent session database changed during deletion planning.");
}
//#endregion
//#region src/agents/agent-delete-databases.ts
function resolveAgentDeleteRuntimeDirs(e, t, n) {
const r = normalizeAgentId(e);
return [t, ...n.filter((e) => normalizeAgentId(e.agentId) === r).map((e) => path.dirname(e.path))];
}
function resolveAgentDeleteDatabasePaths(e, t, n, r) {
return /* @__PURE__ */ new Set([
resolveOpenClawAgentSqlitePath({
agentId: e,
env: r
}),
resolveOpenClawAgentSqlitePath({
agentId: e,
env: r,
path: path.join(t, "openclaw-agent.sqlite")
}),
...n
]);
}
/** Settle admitted usage cleanup before the journal retires native execution owners. */
async function drainAgentDeleteDatabaseWork(e, t = {}) {
const { retireSessionHistoryWorkerDatabases: n } = await import("./session-transcript-worker-resources-uVWPrOtH.mjs");
const { agentId: r, agentDir: i, databasePaths: a } = e.entry;
await e.assertCurrentAsync();
await n({
agentId: r,
paths: [...resolveAgentDeleteDatabasePaths(r, i, a, t.env), resolveIncognitoOpenClawAgentSqlitePath({
agentId: r,
env: t.env
})]
});
await e.assertCurrentAsync();
}
async function retireAgentDeleteRuntime(e, t) {
const n = e.entry.agentId;
const { retirePreparedModelRuntimeAgent: r } = await import("./prepared-model-runtime-D6x7ghYr.mjs");
await e.assertCurrentAsync();
await r({
agentId: n,
agentDirs: t
});
await e.assertCurrentAsync();
}
async function finishAgentDeleteDatabases(e) {
const { deletion: t, agentDir: n, deleteFiles: r, complete: i } = e;
await t.assertCurrentAsync();
if (!i) return;
const a = t.entry.agentId;
unregisterResolvedAgentDir({
agentId: a,
agentDir: n
});
await t.finish({ unregisterDatabases: r });
}
/** Destructive planning includes every registered owner, regardless of runtime schema readiness. */
async function readAgentDeleteDatabaseRegistry(e = {}) {
const t = await prepareOpenClawAgentDatabaseRegistrySnapshotRead({
...e,
includeIncompatibleSchemaVersions: true,
fresh: true
}).read();
t.assertCurrent();
if (t.result.status !== "available") throw new Error("OpenClaw agent database registry is unavailable during deletion.");
return t.result.entries;
}
function prepareJournaledAgentDirOwnership(e, t, n) {
for (const t of listAgentIds(e)) resolveAgentDir(e, t);
if (resolveRegisteredAgentIdForDir(n) !== void 0) return;
registerResolvedAgentDir({
agentId: t,
agentDir: n
});
}
/** Check before journaling: retaining the file alone would still fence its shared owner. */
async function assertAgentSessionStoreDeletionSafe(e, t, n = {}) {
if (!e.session?.store?.trim()) return;
const r = prepareAgentSessionStoreDeletionSafety(e, t, n.env ?? process.env);
const i = captureOpenClawStateReadWorkerContext({
path: n.database?.path ?? n.path,
env: r.env
});
const a = await executeExistingOpenClawStateRead({
path: i.admission.databasePath,
env: i.environment
}, {
type: "agentDeletion.sessionStoreBlocker",
input: {
...r,
databasePath: i.admission.databasePath,
env: i.environment
}
}, {
context: i,
current: true
});
i.admission.assertCurrent();
assertAgentSessionStoreDeletionTargetsCurrent(r.targets);
let o = a?.blocker;
if (!a) {
const e = r.targets.candidates.find(({ identity: e }) => e.key.startsWith("file:"));
if (e) {
const t = await withSqliteReadOnlyWorkerScope(() => runSqliteReadOnlyOperation(e.identity.canonicalPath, {
type: "agentRetirement.sessionStoreBlocker",
input: r
}, {
source: "canonical",
expectedIdentity: e.identity.key,
env: r.env
}));
i.admission.assertCurrent();
assertAgentSessionStoreDeletionTargetsCurrent(r.targets);
o = t.blocker;
}
}
assertAgentSessionStoreDeletionBlocker(r.agentId, o);
}
function resolveSurvivingDatabaseFilePaths(e, t, n) {
return [...new Set(e.filter((e) => normalizeAgentId(e.agentId) !== t).flatMap((e) => resolveSqliteDatabaseFilePaths(e.path)).map((e) => normalizeAgentDirRegistryPath(e, n)))];
}
function isPathOwnedBySurvivingAgent(e, t, n, r = [], i) {
const a = normalizeAgentDirRegistryPath(n, i);
return isPathOwnedByAnotherRegisteredAgent({
agentId: t,
pathname: n,
env: i
}) || findOverlappingWorkspaceAgentIds(e, t, n, i).length > 0 || r.some((e) => e === a || isPathInside(e, a) || isPathInside(a, e));
}
async function prepareAgentDeleteDatabases(e, t, n, r = {}, i) {
const a = await readAgentDeleteDatabaseRegistry(r);
const o = resolveSurvivingDatabaseFilePaths(a, t, r.env);
const s = resolveAgentDeleteDatabasePaths(t, n, a.filter((e) => normalizeAgentId(e.agentId) === t).map((e) => e.path), r.env);
for (const e of s) {
if (i) await i.assertCurrentAsync();
await closeOpenClawAgentDatabaseByPathAsync(e, t);
}
if (i) await i.assertCurrentAsync();
memorySessionActorOwners.closeDatabase({
agentId: t,
path: resolveIncognitoOpenClawAgentSqlitePath({
agentId: t,
env: r.env
})
});
const c = [...s].filter((n) => resolveSqliteDatabaseFilePaths(n).every((n) => !isPathOwnedBySurvivingAgent(e, t, n, o, r.env)));
if (i) await i.assertCurrentAsync();
for (const e of c) closeAuthProfileReadPool({
kind: "database",
databasePath: e
});
if (i) await i.runWithWorker((e, t) => e.execute({
type: "agentDeletion.assertNoDatabaseLeases",
input: { guard: t }
}));
else await assertNoAgentDatabaseLeasesAsync(t, r);
const l = c.map(resolveSqliteDatabaseFilePaths);
const u = l.filter((e) => {
const t = path.relative(n, e[0] ?? n);
return t.startsWith("..") || path.isAbsolute(t);
});
return {
agentDirs: resolveAgentDeleteRuntimeDirs(t, n, a),
registrationPaths: [...s],
readerPaths: c,
fileGroups: l,
relocatedFileGroups: u
};
}
//#endregion
export { prepareAgentDeleteDatabases as a, resolveSurvivingDatabaseFilePaths as c, isPathOwnedBySurvivingAgent as i, retireAgentDeleteRuntime as l, drainAgentDeleteDatabaseWork as n, prepareJournaledAgentDirOwnership as o, finishAgentDeleteDatabases as r, readAgentDeleteDatabaseRegistry as s, assertAgentSessionStoreDeletionSafe as t, AgentSharedStoreOwnerError as u };