openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
207 lines (206 loc) • 7.96 kB
JavaScript
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs";
import { O as walkDirectory } from "./fs-safe-BbWvmdVn.mjs";
import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import { t as dedupeByKey } from "./dedupe-by-key-Cpowmfct.mjs";
import { o as normalizeWindowsNamespaceAlias } from "./backup-archive-path-policy-Cr8KWL_A.mjs";
import { r as isVolatileBackupPath, t as isTransientBackupPath } from "./backup-volatile-filter-Wvq7R1eh.mjs";
import { n as isUpdateCapturePath } from "./update-capture-paths-B2WcdTCG.mjs";
import { realpathSync, statSync } from "node:fs";
import path from "node:path";
import fs$1 from "node:fs/promises";
import { sameFileIdentity } from "@openclaw/fs-safe/advanced";
import { isNotFoundPathError, isPathInside } from "@openclaw/fs-safe/path";
//#region src/commands/backup-resource-inventory.ts
/** Captured backup ownership and resource policy shared by archive traversal and SQLite discovery. */
const MANAGED_STATE_ROOTS = [
"dev",
"git",
"npm",
"npm-runtime",
"tmp",
"tools"
];
async function listDefaultAgentTemporaryRoots(e, t) {
const n = t.filter(({ agentId: t, sourcePath: n }) => n !== path.join(e, "agents", t, "agent"));
const isCustomAgentPath = (e) => n.some(({ sourcePath: t }) => isPathInside(t, e));
const r = [];
let i;
try {
i = await fs$1.readdir(path.join(e, "agents"), { withFileTypes: true });
} catch (e) {
if (isNotFoundPathError(e)) return r;
throw e;
}
for (const t of i) {
const n = path.join(e, "agents", t.name, "agent");
if (!t.isDirectory() || isCustomAgentPath(n)) continue;
const i = await walkDirectory(n, {
symlinks: "skip",
include: (e) => e.kind === "directory" && (e.name === "tmp" || e.name === ".tmp") && !isCustomAgentPath(e.path),
descend: (e) => e.name !== "tmp" && e.name !== ".tmp" && !isCustomAgentPath(e.path)
});
const a = i.failedDirs.find(({ error: e }) => !isNotFoundPathError(e));
if (a) throw a.error;
r.push(...i.entries.map((e) => e.path));
}
return r;
}
/** Prepare declared backup resources without opening live SQLite databases. */
async function createBackupResourcePlan(e) {
const t = path.resolve(e.stateDir);
const n = [];
const r = new Set(e.configPaths.map((e) => path.resolve(e)));
const i = e.agentRoots.map((e) => ({
agentId: e.agentId,
sourcePath: path.resolve(e.sourcePath),
databasePath: path.resolve(e.databasePath)
}));
const a = /* @__PURE__ */ new Set([...r, resolveOpenClawStateSqlitePath({
...process.env,
OPENCLAW_STATE_DIR: t
})]);
const o = [];
const exclude = (e, t) => {
o.push({
kind: e,
sourcePath: path.resolve(t)
});
};
if (!e.onlyConfig) {
for (const t of [...e.oauthDirs, ...e.workspaceDirs]) a.add(path.resolve(t));
for (const e of i) {
a.add(e.sourcePath);
a.add(e.databasePath);
}
for (const e of MANAGED_STATE_ROOTS) exclude("managed state", path.join(t, e));
for (const e of await listDefaultAgentTemporaryRoots(t, i)) exclude("agent temporary files", e);
exclude("plugin skills", path.join(t, "plugin-skills"));
for (const r of e.pluginResources) {
const e = r.scope === "state" ? [{ sourcePath: t }] : i;
for (const t of e) {
const e = path.resolve(t.sourcePath, ...r.relativePath.split("/"));
if (!isPathInside(t.sourcePath, e)) throw new Error(`Plugin ${r.pluginId} backup resource escapes its ${r.scope} root: ${r.relativePath}`);
if (r.disposition === "include") {
a.add(e);
n.push(e);
} else exclude("plugin resource", e);
}
}
for (const t of e.pluginRoots) exclude("plugin dependencies", path.join(t, "node_modules"));
}
const s = dedupeByKey(o.toSorted((e, t) => e.sourcePath.localeCompare(t.sourcePath) || e.kind.localeCompare(t.kind)), (e) => `${e.kind}\0${e.sourcePath}`);
const c = {
stateDir: t,
agentRoots: i,
regenerableRoots: s,
protectedPaths: [...a].toSorted(),
excludedPaths: [...s.map((e) => e.sourcePath), ...new Set(e.excludedWorkspaceDirs.map((e) => path.resolve(e)))].toSorted((e, t) => t.length - e.length || e.localeCompare(t)),
pluginResourceRoots: n
};
return {
...c,
...createBackupPathPolicy(c)
};
}
function createBackupPathPolicy({ stateDir: e, agentRoots: t, regenerableRoots: n, protectedPaths: r, excludedPaths: i }) {
const isIncludedByOwner = (e) => {
const t = i.find((t) => isPathInside(t, e));
if (!t) return true;
return r.some((n) => isPathInside(n, e) && isPathInside(t, n));
};
const isIncluded = (t) => {
const n = path.resolve(t);
return !isUpdateCapturePath(n, e) && isIncludedByOwner(n);
};
const isTraversable = (t) => {
const n = path.resolve(t);
if (isUpdateCapturePath(n, e)) return false;
return isIncludedByOwner(n) || r.some((e) => isPathInside(n, e));
};
const isPackageContent = (t) => {
const n = path.resolve(t);
if (r.some((e) => isPathInside(e, n) || isPathInside(n, e))) return false;
if (!isPathInside(e, n)) return false;
const i = path.relative(e, n).split(path.sep);
if (i[0] === "agents" && i[1] && (i.length === 2 || i[2] === "agent" && (i.length === 3 || i.length === 4 && /^openclaw-agent\.sqlite(?:-wal|-shm|-journal)?$/u.test(i[3] ?? "")))) return false;
return i.includes("node_modules");
};
const a = { stateDirs: [e] };
const isVolatile = (t) => {
const n = path.resolve(t);
const i = r.some((e) => isPathInside(e, n));
return n !== e && !r.some((e) => isPathInside(n, e)) && isTransientBackupPath(n) || !i && isVolatileBackupPath(n, a);
};
return {
stateDir: e,
agentRoots: t,
regenerableRoots: n,
isIncludedByOwner,
isIncluded,
isTraversable,
isPackageContent,
isVolatile
};
}
/** Bind archive ownership to the registrations captured in its online root snapshot. */
function sealBackupResourceInventory(e, t) {
const n = [];
const r = /* @__PURE__ */ new Map();
const i = /* @__PURE__ */ new Map();
for (const e of t) {
const t = path.resolve(normalizeWindowsNamespaceAlias(e.sourcePath));
const a = e.identity ? realpathSync(t) : t;
const o = i.get(a) ?? n.find((t) => t.identity && e.identity && sameFileIdentity(t.identity, e.identity));
if (o && (o.role !== e.role || o.role === "agent" && e.role === "agent" && o.agentId !== e.agentId)) throw new Error(`SQLite path aliases multiple core database owners: ${t}`);
const s = o ?? {
...e,
sourcePath: t
};
const c = r.get(t);
if (c && c !== s) throw new Error(`SQLite path aliases multiple core database owners: ${t}`);
if (!o) n.push(s);
r.set(t, s);
i.set(a, s);
}
const a = [.../* @__PURE__ */ new Set([...e.protectedPaths, ...r.keys()])].toSorted();
const resolveSqliteSource = (t, i) => {
const a = path.resolve(normalizeWindowsNamespaceAlias(t));
const o = r.get(a);
let s = i;
let c = false;
if (!o && !s) try {
s = statSync(a, {
bigint: true,
throwIfNoEntry: false
});
} catch (e) {
if (!hasErrnoCode(e, "ELOOP")) throw e;
c = true;
}
return o ?? (s ? n.find((e) => e.identity && sameFileIdentity(e.identity, s)) : void 0) ?? (e.pluginResourceRoots.some((e) => isPathInside(e, a)) ? { role: "plugin" } : c ? { role: "unresolvable-link" } : void 0);
};
return {
...createBackupPathPolicy({
...e,
protectedPaths: a
}),
coreDatabases: n,
coreDatabaseSourcePaths: [...r].filter(([, e]) => e.identity).map(([e]) => e),
resolveSqliteSource
};
}
/** Report only canonical sources present in the completed snapshot generation. */
function describeCapturedBackupSqliteSnapshots(e, t) {
const n = new Set(t);
return e.coreDatabases.flatMap((e) => (e.role === "global" || e.role === "agent") && e.identity && n.has(e.sourcePath) ? [{
sourcePath: e.sourcePath,
dev: e.identity.dev,
ino: e.identity.ino,
...e.role === "agent" ? {
role: "agent",
agentId: e.agentId
} : { role: "global" }
}] : []);
}
//#endregion
export { describeCapturedBackupSqliteSnapshots as n, sealBackupResourceInventory as r, createBackupResourcePlan as t };