openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
100 lines (99 loc) • 4.87 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { k as tryResolveConfiguredAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs";
import { t as resolveDefaultAgentWorkspaceDir } from "./workspace-default-path-CWX4WiCH.mjs";
import "./workspace-default-DvctYF3A.mjs";
import { r as selectDatabaseEngine } from "./registry-Dd7FIY0t.mjs";
import { a as resolveUpdateRehearsalRoot, r as isUpdateRehearsalReadOnlyPath } from "./update-rehearsal-paths-Dr1MbUA_.mjs";
import { t as loadBundledPluginPublicArtifactModuleFromCandidatesSync } from "./public-surface-loader-DENdCm5a.mjs";
import path from "node:path";
//#region src/plugins/doctor-migration-applicability.ts
/** Decide before any legacy reader opens a PostgreSQL anchor as SQLite application data. */
function sqliteOnlyMigrationNotice(e, t) {
const n = e.sqliteOnly;
if (!n || selectDatabaseEngine({
id: n.family,
postgres: true
}, t) !== "postgres") return;
return `${e.label}: legacy SQLite migration not applicable on PostgreSQL; legacy data is migrated before port.${n.postgresNote ? ` ${n.postgresNote}` : ""}`;
}
//#endregion
//#region src/plugins/doctor-migration-resources.ts
/** Select one whole-plugin data scope for both rehearsal inventory and execution. */
async function preparePluginDoctorMigrationResources(e, t) {
const n = resolveUpdateRehearsalRoot(t.env);
const r = /* @__PURE__ */ new Map();
const i = [];
const a = /* @__PURE__ */ new Map();
for (const { pluginId: n, migration: o } of e) {
const e = sqliteOnlyMigrationNotice(o, t.config);
if (e) {
i.push(e);
continue;
}
if (o.collectBackupResources === void 0) {
if (!t.warnings.some((e) => e.pluginId === n)) t.warnings.push({
kind: "undeclared-migration-resources",
pluginId: n,
message: `${n} migration recovery coverage is unknown: no backup inventory was declared. Update the plugin to a version with migration backup support. Until then, keep a separate backup of its configured data before relying on rollback; ask its maintainer to add backup support. See https://docs.openclaw.ai/cli/doctor/state-migrations#plugin-migration-recovery-coverage`
});
continue;
}
const s = await o.collectBackupResources({
config: t.config,
env: t.env,
stateDir: t.stateDir,
serviceWorkspaceDir: tryResolveConfiguredAgentWorkspaceDir(t.config, t.env) ?? resolveDefaultAgentWorkspaceDir(t.env),
...t.requireLocalResources ? { requireLocalResources: true } : {}
});
if (!Array.isArray(s)) throw new Error(`Invalid migration backup inventory from ${n}/${o.id}`);
const c = s;
for (const e of c) {
if (!isMigrationBackupResource(e)) throw new Error(`Invalid migration backup resource from ${n}/${o.id}`);
const t = e.path;
const i = r.get(t);
if (i && i.kind !== e.kind) throw new Error(`Conflicting migration backup resource kinds for ${t}`);
r.set(t, {
path: t,
kind: e.kind
});
const s = a.get(n) ?? /* @__PURE__ */ new Set();
s.add(t);
a.set(n, s);
}
}
const o = new Set(n ? [...a].flatMap(([e, n]) => [...n].some((e) => isUpdateRehearsalReadOnlyPath(e, t.env)) ? [e] : []) : []);
const s = /* @__PURE__ */ new Set();
for (const [e, t] of a) if (!o.has(e)) for (const e of t) s.add(e);
const c = [...r.values()].filter((e) => s.has(e.path)).toSorted((e, t) => e.path.localeCompare(t.path));
const assertCurrent = () => {
if (!n) return;
if (resolveUpdateRehearsalRoot(t.env) !== n) throw new Error("Plugin migration rehearsal scope changed during preparation");
for (const e of c) if (isUpdateRehearsalReadOnlyPath(e.path, t.env)) throw new Error(`Plugin migration data escaped the rehearsal root: ${e.path}`);
};
assertCurrent();
return {
resources: c,
deferredPluginIds: o,
notices: [...i, ...[...o].map((e) => `rehearsal: ${e} state migrations deferred; declared data outside the rehearsal root left untouched`)],
assertCurrent
};
}
/** Inspect sources for selected trusted owners before the updater replaces the service. */
async function assertPluginStateRetention(e, t) {
for (const n of e) {
const e = n.doctorContract?.stateMigrations;
if (!Array.isArray(e)) continue;
const r = loadBundledPluginPublicArtifactModuleFromCandidatesSync({
dirName: n.id,
artifactCandidates: ["state-retention-api.js"],
retainedAt: t.candidateRoot
});
if (!r || r.packageName !== n.packageName) continue;
for (const n of r.stateMigrations) if (e.some(({ id: e }) => e === n.id)) await n.assertSupportedState(t);
}
}
function isMigrationBackupResource(e) {
return isRecord(e) && typeof e.path === "string" && path.isAbsolute(e.path) && path.normalize(e.path) === e.path && (e.kind === "sqlite" || e.kind === "file" || e.kind === "directory");
}
//#endregion
export { preparePluginDoctorMigrationResources as n, sqliteOnlyMigrationNotice as r, assertPluginStateRetention as t };