openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
135 lines (134 loc) • 6.4 kB
JavaScript
import { s as resolveOpenClawCliInvocation } from "./cli-invocation-ZSPsKRWa.mjs";
import { s as normalizeNullableString } from "./string-coerce-CCIiB4H7.mjs";
import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs";
import { p as statIfExists } from "./path-existence-Co7-GyVr.mjs";
import { t as resolveRuntimeArgs } from "./runtime-worker-url-DRDkoS4J.mjs";
import { r as resolveExternalPackageRuntimeEntrypointUrl } from "./runtime-process-url-CoHTemu-.mjs";
import { i as parsePackageOpenClawSchemaVersions, n as OPENCLAW_STATE_SCHEMA_VERSION, t as OPENCLAW_AGENT_SCHEMA_VERSION } from "./openclaw-schema-versions-E_10NoI6.mjs";
import "./openclaw-state-db-contract-DfPjvL0M.mjs";
import { n as openNodeSqliteDatabase } from "./node-sqlite-CU4puofF.mjs";
import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import { r as prepareSqliteReadOnlyLocation } from "./sqlite-snapshot-source-Q9S7_uQN.mjs";
import { c as resolveSqliteInspectionBudget, p as runSqliteReadOnlyWorker } from "./sqlite-readonly-worker-D3pRJU7r.mjs";
import "./openclaw-agent-db-contract-aCH9b8Uc.mjs";
import { l as tryReadJson } from "./json-files-Cb1h8pYs.mjs";
import { n as runCommandWithTimeout } from "./exec-runner-Br73vG-X.mjs";
import "./exec-aRIvS9s4.mjs";
import { t as readAgentDatabasePreflightTargets } from "./openclaw-agent-db-registry.read-DpiFiW8m.mjs";
import { t as prepareGitCandidateNodeRuntime } from "./update-runner-git-node-preflight-K3bd4X78.mjs";
import { r as preflightOpenClawDatabaseSchemas } from "./openclaw-database-preflight-cTCc9orG.mjs";
import { fileURLToPath } from "node:url";
import path from "node:path";
import fs from "node:fs/promises";
//#region src/node-host/auto-update-compatibility.ts
const MANUAL_UPDATE_GUIDANCE = "Update OpenClaw manually with openclaw update, then restart the node.";
function assertNodeRuntimeSchemaVersions(e) {
if (e?.state !== 36 || e.agent !== 38) throw new Error(`Node auto-update deferred: the release changes database schemas. ${MANUAL_UPDATE_GUIDANCE}`);
}
/** Read the candidate's real startup contract without loading its execution graph. */
async function readNodeRuntimeUpdateManifest(e) {
const t = asNullableRecord(await tryReadJson(path.join(e, "package.json"), { maxBytes: 1048576 }));
const n = normalizeNullableString(t?.version);
if (t?.name !== "openclaw" || !n) throw new Error("Node auto-update candidate has no valid OpenClaw package manifest.");
const r = parsePackageOpenClawSchemaVersions(t);
assertNodeRuntimeSchemaVersions(r);
for (const t of [
"openclaw.mjs",
"node-host-launcher.mjs",
"dist/node-host-launcher-bootstrap.js"
]) if (!(await fs.stat(path.join(e, t))).isFile()) throw new Error(`Node auto-update candidate is missing ${t}.`);
if (!(await Promise.all(["dist/entry.js", "dist/entry.mjs"].map(async (t) => {
return (await statIfExists(path.join(e, t)))?.isFile() ?? false;
}))).some(Boolean)) throw new Error("Node auto-update candidate is missing its built CLI entrypoint.");
return {
version: n,
schemaVersions: r
};
}
/** Recheck after waiting for idle; these observations do not authorize state migration. */
async function assertNodeRuntimeUpdateCompatible(e) {
e.signal?.throwIfAborted();
const { schemaVersions: t } = await readNodeRuntimeUpdateManifest(e.packageRoot);
const n = process.versions.bun ? null : (await prepareGitCandidateNodeRuntime(e.packageRoot, void 0, "current-runtime")).step;
if (n) throw new Error(n.stderrTail ?? "Node runtime is incompatible with the update.");
const r = {
...process.env,
OPENCLAW_STATE_DIR: e.stateDir
};
const i = await preflightOpenClawDatabaseSchemas({
env: r,
signal: e.signal,
supportedVersions: t,
verifyCurrentSchemaShape: true
});
if (i.incompatible.length || i.indeterminate.length || i.pendingMigrations?.length) throw new Error(`Node auto-update deferred: existing databases require migration or repair. ${MANUAL_UPDATE_GUIDANCE}`);
const a = resolveOpenClawStateSqlitePath(r);
if (!await statIfExists(a)) return;
const preflightCopy = async (t, n) => {
const i = await prepareSqliteReadOnlyLocation(await fs.realpath(t), {
preserveSourceArtifacts: true,
signal: e.signal
});
let o;
try {
const s = await runSqliteReadOnlyWorker(i.location, {
mode: "consolidated",
stagingRoot: path.dirname(i.location),
signal: e.signal
});
const { timeoutMs: c } = resolveSqliteInspectionBudget("node update compatibility", t, (await fs.stat(s)).size);
const l = resolveOpenClawCliInvocation({
execArgv: resolveRuntimeArgs(),
entryPath: fileURLToPath(resolveExternalPackageRuntimeEntrypointUrl("cliLauncher", e.packageRoot)),
args: [
"database",
n === void 0 ? "preflight" : "preflight-agent",
s,
...n === void 0 ? [] : ["--agent-id", n],
"--json"
]
});
const u = await runCommandWithTimeout([l.command, ...l.args], {
timeoutMs: c,
signal: e.signal,
killProcessTree: true,
env: r
});
let d = null;
try {
d = asNullableRecord(JSON.parse(u.stdout));
} catch {}
const f = n === void 0 ? "openclaw.state-schema-preflight.v1" : "openclaw.agent-schema-preflight.v1";
if (u.code !== 0 || d?.schema !== f || d.status !== "exact") {
const e = normalizeNullableString(d?.reason) ?? u.stderr.trim();
throw new Error(`Node auto-update candidate rejected the copied database${e ? `: ${e}` : "."} ${MANUAL_UPDATE_GUIDANCE}`);
}
if (n !== void 0) o = { agents: [] };
else {
const e = openNodeSqliteDatabase(s, { readOnly: true });
try {
o = { agents: readAgentDatabasePreflightTargets(e, a) };
} finally {
e.close();
}
}
} catch (e) {
o = { error: e };
}
try {
if (!await i.cleanupAsync()) throw new Error(`Node auto-update database snapshot cleanup failed: ${i.cleanupRoot ?? i.location}`);
} catch (e) {
throw "error" in o ? new AggregateError([o.error, e], "Node auto-update compatibility and cleanup failed") : e;
}
if ("error" in o) throw o.error;
return o.agents;
};
const o = await preflightCopy(a);
for (const e of o) {
if (!await statIfExists(e.path)) continue;
await preflightCopy(e.path, e.agentId);
}
e.signal?.throwIfAborted();
}
//#endregion
export { assertNodeRuntimeUpdateCompatible as n, readNodeRuntimeUpdateManifest as r, assertNodeRuntimeSchemaVersions as t };