openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
98 lines (97 loc) • 4.78 kB
JavaScript
import { a as extractErrorCode, i as collectNestedErrorCandidates } from "./error-coercion-hDrHOSGy.mjs";
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs";
import { t as classifyGatewayStorageFailure } from "./sqlite-error-diagnostics-6_hREUnq.mjs";
import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import { n as getDatabaseEngineSelection } from "./context-DE59wyBs.mjs";
import { a as resolveSqliteDatabaseFilePaths } from "./sqlite-files-rWisKj_V.mjs";
import { r as isUpdateRehearsalReadOnlyPath } from "./update-rehearsal-paths-Dr1MbUA_.mjs";
import { t as shellEscape } from "./shell-quote-DeRN90_2.mjs";
import { t as DoctorUnreadableStateDatabaseError } from "./state-repair-message-CG0JUAZp.mjs";
import { n as resolveRelativeSessionStoreMigrationTargets } from "./doctor-relative-session-store-paths-JTOj2pad.mjs";
import fs from "node:fs";
import path from "node:path";
import { randomUUID } from "node:crypto";
//#region src/commands/doctor-database-preflight.ts
/** Diagnose failed storage admission by effective access, never file ownership. */
function diagnoseDoctorStateStorageFailure(e) {
const isPermissionFailure = (e) => [
"EACCES",
"EPERM",
"EROFS"
].some((t) => hasErrnoCode(e, t));
if (!collectNestedErrorCandidates(e).some((e) => e instanceof DoctorUnreadableStateDatabaseError || extractErrorCode(e) === "OPENCLAW_STATE_LEASE_STORAGE_FAILED" || classifyGatewayStorageFailure(e) === "SQLITE_READONLY" || isPermissionFailure(e))) return;
const t = resolveOpenClawStateSqlitePath();
let n = t;
try {
for (n of resolveSqliteDatabaseFilePaths(t)) try {
fs.closeSync(fs.openSync(n, "r+"));
} catch (e) {
if (!hasErrnoCode(e, "ENOENT")) throw e;
}
n = path.dirname(t);
const e = path.join(n, `.openclaw-write-probe-${randomUUID()}`);
const r = fs.openSync(e, "wx", 384);
try {
fs.closeSync(r);
} finally {
fs.unlinkSync(e);
}
} catch (r) {
if (isPermissionFailure(r)) {
const r = shellEscape(n);
return new Error(`State storage is not writable at ${n} (database: ${t}). Fix ownership and permissions for the user running OpenClaw (for example: sudo chown "$(id -u):$(id -g)" ${r}); the database and sidecars need read/write access and parent directories need write/search access. Or remount the volume writable. If the file or directory is immutable, clear its immutable flag (on macOS: chflags nouchg ${r}). Then rerun openclaw doctor. For read-only diagnostics, use openclaw doctor --lint.`, { cause: e });
}
}
}
/** Prepare fleet facts through the artifact-preserving schema readers. */
async function prepareDoctorDatabasePreflight(e = {}) {
if (!getDatabaseEngineSelection()) {
const { readConfigSnapshotForEngineSelection: e } = await import("./config-preflight-snapshot-DXY8QhxC.mjs");
await e();
}
const { scope: t, preserveSourceArtifacts: n } = e;
const r = await import("./openclaw-database-preflight-BB0y4HYJ.mjs");
const [{ createConfigIO: i }, a, { listAgentIds: o, resolveAgentDir: s }, { openDoctorStateSchemaReadAdmission: c }] = await Promise.all([
import("./io-DeTUDO0h.mjs"),
import("./targets-cXkr4Sqy.mjs"),
import("./agent-scope-config-B1XZecUN.mjs"),
import("./openclaw-state-db-doctor-schema-tmG-5gPM.mjs")
]);
const l = t === "state" || e.cfg ? void 0 : await i({
env: { ...process.env },
observe: false,
pluginValidation: "core-only"
}).readConfigFileSnapshot();
const u = t === "state" ? void 0 : e.cfg ?? l?.sourceConfig ?? l?.config;
let d;
const f = await r.preflightOpenClawDatabaseSchemas({
env: process.env,
scope: t,
openStateSchemaReadAdmission: c,
preserveSourceArtifacts: n,
verifyCurrentSchemaShape: e.verifyCurrentSchemaShape,
...u ? {
configuredAgentDatabaseTargets: (e) => [...o(u).map((e) => ({
agentId: e,
path: path.join(s(u, e), "openclaw-agent.sqlite")
})), ...resolveRelativeSessionStoreMigrationTargets(u, process.env, e).filter((e) => !isUpdateRehearsalReadOnlyPath(e.path, process.env))],
configuredAgentDatabaseCandidatePaths: a.resolveConfiguredAgentDatabaseCandidatePaths(u, { env: process.env }),
agentAdmissionConfig: u,
onAgentDatabaseDiscovery: (e) => {
d = e;
}
} : {}
});
if (f.incompatible.length > 0) throw new r.OpenClawDatabaseSchemaPreflightError(f.incompatible, { operation: "doctor" });
const p = f.indeterminate.find((e) => e.kind === "state");
if (p) throw new DoctorUnreadableStateDatabaseError(p.path, p.reason, { cause: r.getOpenClawDatabaseReadinessFailure(p) });
return {
...f,
...d ? {
agentDatabaseMigrationDiscovery: d,
agentDatabaseRecoveryConfigValid: l?.valid === true
} : {}
};
}
//#endregion
export { prepareDoctorDatabasePreflight as n, diagnoseDoctorStateStorageFailure as t };