openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
114 lines (113 loc) • 5.35 kB
JavaScript
import { n as isPostgresSqlConnection } from "./sql-connection-Beiyg5Dt.mjs";
import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs";
import { i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { D as requireNativeDatabaseFile, t as admitSqliteSchema } from "./sqlite-schema-facts-B1bVE8IB.mjs";
import { i as resolveImmutableSqliteFileUri, n as openNodeSqliteDatabase } from "./node-sqlite-CU4puofF.mjs";
import { a as resolveOpenClawRegisteredAgentDatabasePath, i as resolveOpenClawAgentDatabaseStoredPath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import { a as resolveSqliteDatabaseFilePaths } from "./sqlite-files-rWisKj_V.mjs";
import { n as assertOpenClawStateDatabaseForMaintenance } from "./openclaw-state-db-maintenance-Dvr7ztmv.mjs";
import { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs";
import { a as readMainTableList } from "./sqlite-strict-BeSerG73.mjs";
import { a as invalidateRegisteredAgentDatabasesMemo } from "./openclaw-agent-db-registry-listing-CbXBL50a.mjs";
import { i as readActiveUpdateRun } from "./update-run-read.kernel-DD6zqiXU.mjs";
import { f as isUpdateDoctorLintPass } from "./update-phase-YdZvaQsy.mjs";
import { t as note } from "./note-CnLgHvug.mjs";
import fs from "node:fs";
import path from "node:path";
//#region src/state/openclaw-agent-db-path-repair.ts
/** The caller retains shared-state write admission through this synchronous repair. */
function repairOpenClawAgentDatabasePaths(e) {
const { db: t, path: n } = e;
const r = isPostgresSqlConnection(t);
if (!r) assertOpenClawStateDatabaseForMaintenance(t, { pathname: n });
const i = readActiveUpdateRun(t);
if (i) return {
repaired: 0,
warnings: [`Skipped agent database path repair while update ${i.runId} is in progress. Run openclaw doctor --fix after the update finishes.`]
};
const a = getNodeSqliteKysely(t);
const o = executeSqliteQuerySync(t, a.selectFrom("agent_databases").selectAll().orderBy("last_seen_at", "desc").orderBy("path")).rows;
const s = /* @__PURE__ */ new Map();
const c = [];
let l = 0;
for (const e of o) {
const t = resolveOpenClawAgentDatabaseStoredPath(n, resolveOpenClawRegisteredAgentDatabasePath(n, e.path));
const r = `${e.agent_id}\0${t}`;
const i = s.get(r) ?? {
stored: t,
aliases: []
};
i.aliases.push(e);
s.set(r, i);
}
for (const { stored: e, aliases: i } of s.values()) {
const o = i[0];
const s = resolveOpenClawRegisteredAgentDatabasePath(n, o.path);
const d = path.basename(s).replace(/\.\d+(?=\.sqlite$)/, "");
const f = d === "(multiple).sqlite" || d === `(multiple).${o.agent_id}.sqlite`;
if (f) {
if (r) requireNativeDatabaseFile("postgres", "repair");
try {
const e = resolveSqliteDatabaseFilePaths(s);
const t = e.map((e) => fs.lstatSync(e, { throwIfNoEntry: false }));
if ([t[1], t[3]].some((e) => e?.size)) throw new Error("database or sidecar needs inspection");
if (t[0]?.size) try {
var u = _usingCtx();
const e = u.u(openNodeSqliteDatabase(resolveImmutableSqliteFileUri(s), { readOnly: true }));
admitSqliteSchema(e);
const t = getNodeSqliteKysely(e);
const n = /* @__PURE__ */ new Set([
"schema_meta",
"session_key_contract",
"memory_index_state"
]);
for (const { type: r, name: i } of readMainTableList(e)) {
if (r === "shadow" || n.has(i)) continue;
const a = t.selectFrom(i).selectAll().limit(1);
if (executeSqliteQuerySync(e, a).rows.length) throw new Error("contains user rows");
}
} catch (e) {
u.e = e;
} finally {
u.d();
}
for (const t of e) fs.rmSync(t, { force: true });
} catch (e) {
c.push(`Removed invalid agent database registration; kept ${s} for inspection (${String(e)}).`);
}
} else {
if (process.platform !== "win32" || path.isAbsolute(e) || !i.some((e) => e.path.startsWith("\\\\?\\"))) continue;
if (r) requireNativeDatabaseFile("postgres", "repair");
const n = {
...o,
path: e
};
executeSqliteQuerySync(t, a.insertInto("agent_databases").values(n).onConflict((e) => e.columns(["agent_id", "path"]).doUpdateSet(n)));
}
const p = i.map((e) => e.path).filter((t) => f || t !== e);
executeSqliteQuerySync(t, a.deleteFrom("agent_databases").where("agent_id", "=", o.agent_id).where("path", "in", p));
l += f ? i.length : 1;
}
return {
repaired: l,
warnings: c
};
}
//#endregion
//#region src/commands/doctor-agent-database-paths.ts
/** Repair the inventory only before an updater captures its exact-path baseline. */
function noteDoctorAgentDatabasePathHealth(e) {
if (!e.shouldRepair) return [];
const t = runOpenClawStateWriteTransaction((t) => isUpdateDoctorLintPass(e.env) ? {
repaired: 0,
warnings: ["Skipped agent database path repair during update Doctor. Run openclaw doctor --fix to repair agent database paths."]
} : repairOpenClawAgentDatabasePaths(t), { env: e.env }, { operationLabel: "doctor.agent-database-paths" });
if (t.repaired > 0) {
invalidateRegisteredAgentDatabasesMemo({ env: e.env });
note(`Repaired ${t.repaired} agent database path registration(s).`, "Doctor changes");
}
if (t.warnings.length > 0) note(t.warnings.join("\n"), "Doctor warnings");
return t.warnings;
}
//#endregion
export { noteDoctorAgentDatabasePathHealth };