openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
218 lines (217 loc) • 9.36 kB
JavaScript
import { o as measureGatewayBootstrapStep } from "./startup-trace-DWZsuODq.mjs";
import { n as createNonExitingRuntime, r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import { s as raceWithTimeout } from "./src-BPmQBLJp.mjs";
import { C as resolveIsNixMode } from "./paths-DAcAg5aV.mjs";
import { o as getAsyncWorkSignal } from "./async-work-scope-BXagjUap.mjs";
import { r as createUpdateFailureFact } from "./update-failure-facts-Bk_Ty8lS.mjs";
import { f as withCommandProcessScope, i as resolveCommandProcessSignal } from "./exec-spawn-DMKoejF-.mjs";
import { t as exitCliAfterOutput } from "./one-shot-exit-DkbGSQD5.mjs";
import { t as formatDoctorProgressPhase } from "./doctor-progress-C3vWlya1.mjs";
import { r as stylePromptTitle } from "./prompt-style-CLys-6GQ.mjs";
import { c as showDoctorHealthSummary } from "./doctor-health-contribution-t31v9kPi.mjs";
import { n as resolveDoctorRepairMode } from "./doctor-repair-mode-D2HKy2yZ.mjs";
import fs from "node:fs";
import { intro, outro } from "@clack/prompts";
//#region src/flows/doctor-health-startup.ts
const showDoctorIntro = (e) => intro(stylePromptTitle(e) ?? e);
const showDoctorOutro = (e) => outro(stylePromptTitle(e) ?? e);
/** Keep slow checks visible without writing human progress into machine output. */
async function withDoctorProgress(e, t, n) {
if (!n) return await t();
const r = performance.now();
const report = (t = "") => process.stderr.write(`Doctor: ${e}${t}\n`);
report();
const i = setInterval(() => {
report(` (${Math.floor((performance.now() - r) / 1e3)}s elapsed)`);
}, 1e4);
i.unref();
try {
return await t();
} finally {
clearInterval(i);
}
}
function createDoctorProgressMeasure(e) {
return (t, n) => withDoctorProgress(formatDoctorProgressPhase(t), () => measureGatewayBootstrapStep(t, n), e.json !== true);
}
function exitDoctorHealthFlow(e, t) {
if (e === defaultRuntime) exitCliAfterOutput(e, t);
e.exit(t);
}
function stateDirectoryExistsAtDoctorStart() {
try {
return fs.statSync(resolveStateDir()).isDirectory();
} catch {
return false;
}
}
function readDoctorStartup(e, t) {
return withCommandProcessScope(async () => {
const n = resolveCommandProcessSignal();
return raceWithTimeout(() => t(n), 6e4, () => {
throw new Error(`Doctor startup timed out after 60000ms (${e}). Retry with OPENCLAW_DEBUG=1 openclaw doctor --lint; if it still stalls, capture a Node diagnostic report before retrying repairs.`);
}, { signal: n });
}, getAsyncWorkSignal());
}
async function prepareDoctorHealthFlow(e, t, n) {
const r = e ?? (await import("./runtime-PcIG_sHE.mjs")).defaultRuntime;
const i = {
...r,
exit: createNonExitingRuntime().exit
};
const a = stateDirectoryExistsAtDoctorStart();
n("OpenClaw doctor");
const o = await readDoctorStartup("package root", async (e) => {
const { resolveOpenClawPackageRoot: t } = await import("./openclaw-root-y32pjg1o.mjs");
return t({
moduleUrl: import.meta.url,
argv1: process.argv[1],
cwd: process.cwd(),
signal: e
});
});
if (resolveIsNixMode() && (t.repair === true || t.yes === true || t.generateGatewayToken === true)) {
const { assertConfigWriteAllowedInCurrentMode: e } = await import("./config-write-guard-C8xOz4HA.mjs");
e();
}
if (!resolveDoctorRepairMode(t).updateInProgress) {
const e = await readDoctorStartup("source config", async () => {
const { readSourceConfigBestEffort: e } = await import("./io.runtime.js");
return e();
});
const t = await readDoctorStartup("Tailscale prerequisite", async () => {
const { inspectDoctorTailscalePrerequisite: t } = await import("./doctor-tailscale-BfCQZYHm.mjs");
return t(e);
});
if (t) {
const { note: e } = await import("./terminal-core/note.js");
e(t, "Gateway startup prerequisite");
}
}
return {
effectiveRuntime: r,
repairRuntime: i,
stateDirExistedAtStart: a,
root: o
};
}
async function prepareDoctorInteractiveMaintenance(e) {
const { createDoctorPrompter: t } = await import("./doctor-prompter-SEbkj_Tb.mjs");
const { prepareDoctorDatabasePreflight: n } = await import("./doctor-database-preflight-Dhck-D-u.mjs");
const r = t({
runtime: e.runtime,
options: e.options
});
if (!e.databasePreflight) await n({ scope: "state" });
const { maybeOfferUpdateBeforeDoctor: i } = await import("./doctor-update-BFrrTByl.mjs");
if (await i({
options: e.options,
root: e.root,
confirm: (e) => r.confirm(e),
outro: e.outro
})) return "handled";
if (!e.databasePreflight) await n();
if (await r.confirmRuntimeRepair({
message: "Pause the managed Gateway while you review repairs? Doctor restores its prior service state when finished.",
initialValue: true,
requiresInteractiveConfirmation: true
})) return "accepted";
e.runtime.log("Doctor repairs skipped. Continuing read-only diagnosis.");
const { runDoctorLintCli: a } = await import("./doctor-lint-EwDQ7u8I.mjs");
return { diagnosticExitCode: await a(e.runtime, {
allowExec: e.options.allowExec,
deep: e.options.deep
}) };
}
function showDoctorRepairRefusal(e) {
if (!e.configWriteRefusal) {
if (e.postInstallDoctorResult?.status !== "error") return;
showDoctorHealthSummary(e);
showDoctorOutro("Doctor blocked; required repairs remain incomplete.");
return e.postInstallDoctorResult;
}
showDoctorHealthSummary(e);
showDoctorOutro(e.configResultWriteCommitted === true ? "Doctor finished, but some config fixes were not applied." : "Doctor finished, but config fixes were not applied.");
return {
status: "error",
failureFacts: [createUpdateFailureFact({
check: "config-write",
code: e.configWriteRefusal,
message: "Doctor config fixes were not applied."
})]
};
}
async function prepareDoctorMaintenanceArchives(e, t, n = {}) {
const r = resolveCommandProcessSignal(getAsyncWorkSignal());
let i;
try {
const { prepareDoctorDatabasePreflight: t } = await import("./doctor-database-preflight-Dhck-D-u.mjs");
i = (e ?? await t(n)).agentDatabaseMigrationDiscovery;
if (i) {
const { prepareCanonicalTranscriptArchiveMigrations: e } = await import("./state-migrations.transcript-directives-archives-CxAKgi0F.mjs");
await e(i);
}
} catch (e) {
t.log(`Archive verification preparation deferred: ${String(e)}`);
}
r?.throwIfAborted();
return i;
}
async function prepareDoctorStateSchema(e, t, n) {
if (!t || !e.pendingMigrations?.some((e) => e.kind === "state")) return;
const { prepareLegacyStateDatabaseSchema: r } = await import("./state-migrations.doctor-CODMy_h9.mjs");
const { throwIfDoctorStateMigrationRefused: i } = await import("./state-migrations.messages-B4pupCuT.mjs");
const a = await r(process.env, n);
i([a]);
return a;
}
async function repairDoctorStateDatabase(e, t) {
const { repairOpenClawStateDatabaseIndexesForDoctor: n, repairOpenClawStateDatabaseReadabilityForDoctor: r } = await import("./openclaw-state-db-Ds4jcYSd.mjs");
let i = false;
for (const a of [n, r]) {
const n = a({
env: process.env,
integrityCheck: t
});
i ||= n.changes.length > 0;
if (n.warnings.length > 0) throw new Error(n.warnings.join("\n"));
for (const t of n.changes) e.log(t);
}
return i;
}
async function prepareDoctorRuntimeChecks(e, t, n, r, i) {
const measure = (e, t) => withDoctorProgress(formatDoctorProgressPhase(e), t, i.json !== true);
const { repairDoctorAgentDeletionJournal: a } = await import("./doctor-agent-deletion-journal-BJWlSybf.mjs");
const o = await measure("Agent deletion journal", () => a({
preflight: e,
shouldRepair: r.shouldRepair,
env: process.env
}));
for (const e of [...o.changes, ...o.warnings]) n.log(e);
const s = o.changes.length > 0 ? await measure("Database preflight", async () => (await import("./doctor-database-preflight-Dhck-D-u.mjs")).prepareDoctorDatabasePreflight()) : e;
const { inspectDoctorSqliteNoCow: c } = await import("./doctor-sqlite-nocow-peoHwNsc.mjs");
const { resolveOpenClawStateSqlitePath: l } = await import("./openclaw-state-db.paths-DiM3pkKt.mjs");
const u = c([l(), ...s.agentDatabaseMigrationDiscovery?.discovery.targets.map((e) => e.path) ?? []]);
for (const e of u.notes) n.log(e);
const { maybeRepairUiProtocolFreshness: d } = await import("./doctor-ui-Mjd6HXuG.mjs");
const { noteSourceInstallIssues: f } = await import("./doctor-install-DVHlFsvF.mjs");
const { noteBunCliLauncherIssues: p } = await import("./doctor-bun-cli-launcher-Bde_93_F.mjs");
const { noteStalePluginRuntimeSymlinks: m } = await import("./plugin-runtime-symlinks-DtuEVBTr.mjs");
const { noteStartupOptimizationHints: h } = await import("./doctor-platform-notes-D5JnL-um.mjs");
await measure("UI protocol", () => d(n, r));
await measure("Source install", () => f(t));
await measure("CLI launcher", () => p({
root: t,
prompter: r
}));
await measure("Plugin runtime links", () => m(t));
h();
return {
schemas: s,
nocowPaths: u.paths,
warnings: o.warnings
};
}
//#endregion
export { prepareDoctorMaintenanceArchives as a, repairDoctorStateDatabase as c, showDoctorRepairRefusal as d, withDoctorProgress as f, prepareDoctorInteractiveMaintenance as i, showDoctorIntro as l, exitDoctorHealthFlow as n, prepareDoctorRuntimeChecks as o, prepareDoctorHealthFlow as r, prepareDoctorStateSchema as s, createDoctorProgressMeasure as t, showDoctorOutro as u };