UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

172 lines (171 loc) • 7.4 kB
import { r as resolveDiagnosticProcessEnv } from "./process-env-DVJdcOcM.mjs"; import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs"; import { r as getChildLogger } from "./logger-1QDNfT7Y.mjs"; import { n as resolveOpenClawPackageRoot } from "./openclaw-root-C-ayo09z.mjs"; import { c as readWindowsProcessStartTimeSync } from "./pid-alive-D899x-ak.mjs"; import { n as runCommandWithTimeout } from "./exec-runner-Br73vG-X.mjs"; import "./exec-aRIvS9s4.mjs"; import { n as quotePowerShellArg, t as quoteCliArg } from "./quote-cli-arg-J7A9UE6m.mjs"; import { a as executeGitCommand } from "./git-exec-DHBL622T.mjs"; import { u as renderGatewayServiceStartHints } from "./shared-cDUnFvXF.mjs"; import { n as resolveLaunchAgentLabel } from "./launchd-label-DdmmL_TH.mjs"; import { c as resolveUpdateInstallKind } from "./update-check-BfPo_IXH.mjs"; import fs from "node:fs/promises"; import { readFileWindowFully } from "@openclaw/fs-safe/advanced"; //#region src/commands/doctor-update-refusal.ts async function formatUpdateDoctorServiceStopRefusal(e) { const t = [ "The update parent must stop the managed Gateway before Doctor maintenance; Doctor left the service unchanged.", "The managed Gateway is still loaded or running. If the stop command already returned successfully, its managed-service shutdown did not complete.", `After the current update exits, run ${formatCliCommand("openclaw gateway stop", e)} from an independent shell, then retry ${formatCliCommand("openclaw update repair", e)}.` ]; if (process.platform === "darwin") { const { resolveLaunchAgentGuiDomain: n } = await import("./launchd-runtime-CpKCV4oj.mjs"); t.push(`If that command cannot unload the LaunchAgent, run launchctl bootout ${n()}/${resolveLaunchAgentLabel(e)} from a terminal in the owning user's logged-in macOS GUI session.`); } return t.join("\n"); } async function readParentStartTime(e) { if (process.platform === "win32") return readWindowsProcessStartTimeSync(e); const t = await runCommandWithTimeout([ "ps", "-o", "lstart=", "-p", String(e) ], { baseEnv: { ...resolveDiagnosticProcessEnv(process.env), LC_ALL: "C", TZ: "UTC" }, timeoutMs: 1e3 }); const n = Date.parse(`${t.stdout.trim()} UTC`); return t.code === 0 && t.termination === "exit" && Number.isFinite(n) ? n : null; } async function readPreviousSwitchCommit(e) { const t = process.ppid; const n = await readParentStartTime(t); if (n === null) return; const git = async (t) => { const n = await executeGitCommand(e, t, { env: { GIT_NO_LAZY_FETCH: "1" } }); if (n.code !== 0 || n.termination !== "exit") throw new Error("Git recovery evidence unavailable"); return n.stdout.trim(); }; const r = await git(["rev-parse", "HEAD"]); const i = await git([ "rev-parse", "--path-format=absolute", "--git-path", "logs/HEAD" ]); const a = await fs.open(i, "r"); let o; try { const e = await a.stat(); if (!e.isFile()) return; const t = Math.max(0, e.size - 131072); const n = Buffer.alloc(e.size - t); const r = await readFileWindowFully(a, n, t); o = n.subarray(0, r).toString("utf8").split("\n"); if (o.pop() !== "") return; if (t > 0) o.shift(); } finally { await a.close(); } const s = []; for (const e of o.toReversed()) { const t = /^([a-f0-9]{40}) ([a-f0-9]{40}) .* (\d+) [+-]\d{4}\t(.*)$/u.exec(e); if (!t) return; s.push({ previous: t[1], sha: t[2], atMs: Number(t[3]) * 1e3, action: t[4] ?? "" }); } if (s[0]?.sha !== r) return; const c = s.findIndex((e) => !(e.atMs > n) || !/^(?:checkout: |rebase \((?:start|pick|finish)\): )/u.test(e.action)); if (c < 1) return; const l = s.slice(0, c); const u = l.filter((e) => e.action.startsWith("checkout: ")); if (u.length !== 1 || l.at(-1) !== u[0] || l.slice(0, -1).some((e, t) => e.previous !== l[t + 1]?.sha)) return; const d = l.filter((e) => e.action.startsWith("rebase ")); if (d.length > 0 && (!d[0]?.action.startsWith("rebase (finish): ") || !d.at(-1)?.action.startsWith("rebase (start): ") || d.filter((e) => e.action.startsWith("rebase (start): ")).length !== 1 || d.filter((e) => e.action.startsWith("rebase (finish): ")).length !== 1)) return; const f = u[0]?.previous; if (!f || !/^[a-f0-9]{40}$/u.test(f) || f === r) return; const [p, m] = (await git([ "rev-parse", `${f}^{tree}`, `${r}^{tree}` ])).split("\n"); const h = await executeGitCommand(e, [ "merge-base", "--is-ancestor", r, f ], { env: { GIT_NO_LAZY_FETCH: "1" } }); if (p === m || h.termination !== "exit" || h.code !== 1 || process.ppid !== t || await git(["rev-parse", "HEAD"]) !== r) return; return f; } /** Diagnostic guidance only: never changes the checkout, state, or service. */ async function resolveUpdateDoctorGitRecovery(e = {}) { if (process.env.OPENCLAW_UPDATE_IN_PROGRESS !== "1" || process.env.OPENCLAW_UPDATE_PARENT_SUPPORTS_DOCTOR_CONFIG_WRITE !== "1" || process.env.OPENCLAW_UPDATE_PARENT_SUPPORTS_GATEWAY_RESTART !== "1") return; const t = e.root === void 0 ? await resolveOpenClawPackageRoot({ moduleUrl: import.meta.url, argv1: process.argv[1] }) : e.root; if (!t || await resolveUpdateInstallKind(t).catch(() => "unknown") !== "git") return; if (e.stateRepaired) return { message: "Checking out the previous source is not enough: state repairs have already run. Follow the migration recovery instructions above before starting the Gateway.", commands: [] }; const n = process.platform === "win32" ? quotePowerShellArg : quoteCliArg; const r = await readPreviousSwitchCommit(t).catch(() => void 0); if (!r) return { message: `The previous commit could not be determined; inspect git -C ${n(t)} reflog.`, commands: [] }; const i = `git -C ${n(t)} checkout ${r}`; const a = process.platform === "win32" ? `Set-Location -LiteralPath ${n(t)}` : `cd ${n(t)}`; const o = [ i, a, "pnpm install", "pnpm build" ]; const s = process.platform === "win32" ? `${i}; if ($? -and $LASTEXITCODE -eq 0) { ${a}; if ($?) { pnpm install; if ($? -and $LASTEXITCODE -eq 0) { pnpm build } } }` : o.join(" && "); return { commands: o, message: [ `The previous source is intact in Git. Previous source commit: ${r}`, "After the updater exits, run each command from an independent shell only after the previous one succeeds:", s, `Service hints (choose the appropriate one): ${renderGatewayServiceStartHints().join("; ")}`, "Fix the refusal cause, then rerun openclaw update. Once the upgrade succeeds, subsequent updates validate before activation." ].join("\n") }; } /** Without a Doctor result channel, the deferral stays visible in the run's update history. */ async function recordDeferredUpdateDoctorWarning(e, t) { const n = process.env.OPENCLAW_UPDATE_RUN_ID?.trim(); if (!n) return; try { const { recordUpdateRunStep: t } = await import("./update-run-ledger-BKb5EA0D.mjs"); t(n, { step: "warning:doctor-maintenance", status: "completed", endedAtMs: Date.now(), detail: e }); } catch { t.error("Doctor maintenance warning could not be saved to update history."); } } function recordUpdateDoctorRefusal(e) { const t = getChildLogger({ subsystem: "update" }); for (const n of e.split("\n")) t.warn(n); } //#endregion export { resolveUpdateDoctorGitRecovery as i, recordDeferredUpdateDoctorWarning as n, recordUpdateDoctorRefusal as r, formatUpdateDoctorServiceStopRefusal as t };