UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

221 lines (220 loc) • 8.5 kB
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs"; import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs"; import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs"; import "./paths-DAcAg5aV.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import { realpathSync, statSync } from "node:fs"; import path from "node:path"; import { isDeepStrictEqual } from "node:util"; import { probePathSuffixAliasesSync, resolvePathPrefixSync } from "@openclaw/fs-safe/advanced"; //#region src/state/openclaw-agent-db.paths.ts const INCOGNITO_AGENT_SQLITE_BASENAME = "incognito-openclaw-agent.sqlite"; const agentSqlitePaths = /* @__PURE__ */ new Map(); const agentSqlitePathKeys = agentSqlitePaths.keys(); function resolveOpenClawAgentSqlitePath(e) { if (e.path != null) return path.resolve(e.path); const t = normalizeAgentId(e.agentId); const n = resolveStateDir(e.env ?? process.env); const r = `${t}:${n}`; const i = agentSqlitePaths.get(r); if (i !== void 0) return i; const a = path.resolve(n, "agents", t, "agent", "openclaw-agent.sqlite"); agentSqlitePaths.set(r, a); if (agentSqlitePaths.size > 256) { const e = agentSqlitePathKeys.next(); if (!e.done) agentSqlitePaths.delete(e.value); } return a; } /** Resolve the lexical sentinel path used by an agent's memory session owner. */ function resolveIncognitoOpenClawAgentSqlitePath(e) { return path.join(path.dirname(resolveOpenClawAgentSqlitePath(e)), INCOGNITO_AGENT_SQLITE_BASENAME); } /** Identify the reserved incognito sentinel without touching its filesystem path. */ function isIncognitoOpenClawAgentSqlitePath(e, t) { const n = path.resolve(e); return path.basename(n) === "incognito-openclaw-agent.sqlite" && n === resolveIncognitoOpenClawAgentSqlitePath(t); } /** An explicit sentinel names its physical root; ambient state cannot redirect it. */ function resolveExplicitIncognitoAgentSqliteTarget(e, t = {}) { if (!e || path.basename(e) !== "incognito-openclaw-agent.sqlite") return; const n = path.resolve(e); const r = normalizeAgentId(t.agentId ?? path.basename(path.dirname(path.dirname(n)))); const i = t.env ?? { OPENCLAW_STATE_DIR: path.resolve(n, "../../../..") }; if (!isIncognitoOpenClawAgentSqlitePath(n, { agentId: r, env: i })) throw new Error("Explicit incognito database target does not match its agent and state root"); return { agentId: r, env: i, path: n }; } const missingSuffixAliasCache = /* @__PURE__ */ new Map(); function shouldProbeUnicodeCaseVariants(e, t) { const hasNonAscii = (e) => e.split("").some((e) => e.charCodeAt(0) > 127); if (!hasNonAscii(e) && !hasNonAscii(t)) return false; const n = e.toLowerCase() === t.toLowerCase(); return e.toUpperCase() === t.toUpperCase() || n && Array.from(e).length === Array.from(t).length; } function areMissingSuffixAliases(e) { if (e.left === e.right) return true; const t = e.left.split(path.sep); const n = e.right.split(path.sep); if (t.length !== n.length || [...t, ...n].some((e) => !e || e === "." || e === "..")) return false; const r = [e.left, e.right].toSorted(); const i = JSON.stringify([ e.parentDevice.toString(), e.parentInode.toString(), e.parentRealPath, ...r ]); const a = missingSuffixAliasCache.get(i); if (a !== void 0) return a; let o; let s; try { o = probePathSuffixAliasesSync({ directory: e.parentRealPath, left: e.left, right: e.right, maxDepth: t.length, shouldProbeCaseVariants: shouldProbeUnicodeCaseVariants }); } catch (e) { s = e; } if (o === void 0) throw new Error(`Cannot determine whether database paths alias under ${JSON.stringify(e.parentRealPath)}: ${JSON.stringify(e.left)} and ${JSON.stringify(e.right)}. Check directory access and retry.`, { cause: s }); missingSuffixAliasCache.set(i, o); return o; } function anchorDatabasePathWithoutNormalizing(e) { const t = path.sep === "\\" ? e.replaceAll("/", "\\") : e; if (path.isAbsolute(t)) return t; if (path.sep === "\\") { const e = /^([A-Za-z]:)(.*)$/u.exec(t); if (e) { const t = path.resolve(`${e[1]}.`); return e[2] ? `${t}${t.endsWith(path.sep) ? "" : path.sep}${e[2]}` : t; } } const n = process.cwd(); return `${n}${n.endsWith(path.sep) ? "" : path.sep}${t}`; } function resolveAgentDatabasePathIdentity(e) { const t = anchorDatabasePathWithoutNormalizing(e); try { const e = realpathSync.native(t); const n = statSync(e, { bigint: true }); return { lexicalPath: t, realPath: e, device: n.dev, inode: n.ino }; } catch (e) { if (!hasErrnoCode(e, "ENOENT")) throw e; const n = path.parse(t).root; const r = resolvePathPrefixSync(n + t.slice(n.length).split(path.sep).filter(Boolean).join(path.sep)); const i = r.existingPath; const a = statSync(i, { bigint: true }); return { lexicalPath: t, parentDevice: a.dev, parentInode: a.ino, parentRealPath: i, unresolvedSuffix: r.unresolvedSegments.join(path.sep) }; } } function areSameAgentDatabasePathIdentities(e, t) { if (e.lexicalPath === t.lexicalPath) return true; if ("realPath" in e) return "realPath" in t && (e.realPath === t.realPath || e.device === t.device && e.inode === t.inode); return !("realPath" in t) && e.parentDevice === t.parentDevice && e.parentInode === t.parentInode && areMissingSuffixAliases({ left: e.unresolvedSuffix, right: t.unresolvedSuffix, parentDevice: e.parentDevice, parentInode: e.parentInode, parentRealPath: e.parentRealPath }); } /** Create a synchronous-operation matcher that prepares each exact locator once. */ function createOpenClawAgentDatabasePathMatcher() { const e = /* @__PURE__ */ new Map(); const resolveIdentity = (t) => { const n = anchorDatabasePathWithoutNormalizing(t); const r = e.get(n); if (r) return r; const i = resolveAgentDatabasePathIdentity(n); e.set(n, i); return i; }; return Object.assign((e, t) => areSameAgentDatabasePathIdentities(resolveIdentity(e), resolveIdentity(t)), { createIndex() { const e = /* @__PURE__ */ new Map(); const t = []; const n = /* @__PURE__ */ new Map(); const r = /* @__PURE__ */ new Map(); const i = /* @__PURE__ */ new Map(); return (a) => { const o = resolveIdentity(a); const s = e.get(o.lexicalPath); if (s !== void 0) return s; let c; if ("realPath" in o) { const e = `${o.device}:${o.inode}`; const i = n.get(e); const a = r.get(o.realPath); c = i === void 0 ? a : a === void 0 ? i : Math.min(i, a); if (c === void 0) { c = t.push(o) - 1; n.set(e, c); r.set(o.realPath, c); } } else { const e = `${o.parentDevice}:${o.parentInode}`; const n = i.get(e) ?? []; c = n.find((e) => areSameAgentDatabasePathIdentities(t[e], o)); if (c === void 0) { c = t.push(o) - 1; n.push(c); i.set(e, n); } } const l = t[c].lexicalPath; e.set(o.lexicalPath, l); return l; }; }, isCurrent() { for (const t of e.values()) { const e = resolveAgentDatabasePathIdentity(t.lexicalPath); if (!isDeepStrictEqual(t, e)) return false; } return true; } }); } /** Compare two database locators by canonical filesystem identity when available. */ function isSameOpenClawAgentDatabasePath(e, t) { return areSameAgentDatabasePathIdentities(resolveAgentDatabasePathIdentity(e), resolveAgentDatabasePathIdentity(t)); } function canonicalPathForRegistryBoundary(e) { const t = resolveAgentDatabasePathIdentity(e); if ("realPath" in t) return t.realPath; const n = t.unresolvedSuffix.split(path.sep); return n.includes("..") ? t.parentRealPath : path.join(t.parentRealPath, ...n); } /** Named import artifacts are offline archives, not durable runtime discovery state. */ function isPersistentOpenClawAgentDatabasePath(e, t = process.env) { const n = path.resolve(e); const r = path.join(path.resolve(resolveStateDir(t)), "imports"); if (isPathInside(r, n)) return false; const i = canonicalPathForRegistryBoundary(e); const a = canonicalPathForRegistryBoundary(resolveStateDir(t)); const o = canonicalPathForRegistryBoundary(path.join(a, "imports")); return !isPathInside(o, i); } //#endregion export { isSameOpenClawAgentDatabasePath as a, resolveOpenClawAgentSqlitePath as c, isPersistentOpenClawAgentDatabasePath as i, createOpenClawAgentDatabasePathMatcher as n, resolveExplicitIncognitoAgentSqliteTarget as o, isIncognitoOpenClawAgentSqlitePath as r, resolveIncognitoOpenClawAgentSqlitePath as s, INCOGNITO_AGENT_SQLITE_BASENAME as t };