Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
fix(desktop): "Update Mercury" installs the matching runtime and restarts onto it
Salman Qureshi1b4979ed8dc6
diff --git a/apps/desktop/src/app/App.tsx b/apps/desktop/src/app/App.tsxindex 93e2dfb9..6ecfa661 100644--- a/apps/desktop/src/app/App.tsx+++ b/apps/desktop/src/app/App.tsx@@ -8,6 +8,7 @@ import { Workspace } from "~/layout/Workspace";import { Wordmark } from "~/components/Logo";import { Button } from "~/components/ui/Button";import { desktop } from "~/shell/tauri";+import { updateLocalRuntime } from "~/shell/updateLocal";import { configureApi } from "~/api/client";import { useRuntimeState } from "./store";@@ -84,10 +85,13 @@ function Connected({ connection, onSwitch }: { connection: Connection; onSwitch:<p className="text-sm text-ink3">{status.compat.reason === "desktop-too-old"? `Mercury ${status.health.version} speaks a newer API (v${status.compat.apiVersion}) than this app. Update Mercury Desktop, or open the dashboard in your browser.`- : `Mercury ${status.health.version || "(unknown)"} on ${connection.name} is older than this app supports. Run \`mercury upgrade\` there.`}+ : connection.kind === "local"+ ? `Mercury ${status.health.version || "(unknown)"} on this computer is older than this app. Update it here: the app installs the matching version and restarts Mercury.`+ : `Mercury ${status.health.version || "(unknown)"} on ${connection.name} is older than this app. Update Mercury on that computer to a version with the desktop API.`}</p>+ {status.compat.reason === "runtime-too-old" && connection.kind === "local" && <UpdateLocal onUpdated={retry} />}<div className="flex gap-2">- <Button variant="primary" onClick={() => void desktop.openExternal(connection.baseUrl)}>Open in browser</Button>+ <Button variant="outline" onClick={() => void desktop.openExternal(connection.baseUrl)}>Open in browser</Button><Button variant="outline" onClick={onSwitch}>Use another connection</Button></div></>@@ -100,6 +104,30 @@ function Connected({ connection, onSwitch }: { connection: Connection; onSwitch:);}+/** An older Mercury on this computer: install the matching one and restart onto it. */+function UpdateLocal({ onUpdated }: { onUpdated: () => void }) {+ const [step, setStep] = useState<"idle" | "download" | "restart" | "wait" | "failed">("idle");+ const [detail, setDetail] = useState("");+ async function update() {+ setDetail("");+ try {+ await updateLocalRuntime(setStep);+ onUpdated();+ } catch (err) {+ setStep("failed");+ setDetail(err instanceof Error ? err.message : String(err));+ }+ }+ const busy = step === "download" || step === "restart" || step === "wait";+ const label = step === "download" ? "Downloading Mercury…" : step === "restart" ? "Restarting Mercury…" : step === "wait" ? "Waiting for Mercury…" : "Update Mercury";+ return (+ <div className="flex flex-col items-center gap-2">+ <Button variant="primary" loading={busy} onClick={() => void update()}>{label}</Button>+ {step === "failed" && <p className="max-w-[380px] text-xs text-red">{detail}</p>}+ </div>+ );+}+function StartLocal({ onStarted }: { onStarted: () => void }) {const [state, setState] = useState<"idle" | "starting" | "failed">("idle");const [detail, setDetail] = useState("");diff --git a/apps/desktop/src/shell/InstallRuntime.tsx b/apps/desktop/src/shell/InstallRuntime.tsxindex 7f8c1c08..ec5798cb 100644--- a/apps/desktop/src/shell/InstallRuntime.tsx+++ b/apps/desktop/src/shell/InstallRuntime.tsx@@ -6,6 +6,7 @@ import { Switch } from "~/components/ui/Switch";import { Mono } from "~/components/ui/bits";import { desktop, isTauri, type InstallProgress } from "./tauri";import { LOCAL_CONNECTION_ID, useConnections } from "./connections";+import { localIsOutdated, restartLocalRuntime } from "./updateLocal";/*** "Install Mercury here": download the matching release, verify it, put it@@ -57,6 +58,9 @@ export function InstallRuntime({ platform, onInstalled }: { platform: string; onif (!(await isUp(baseUrl))) {const start = await desktop.runMercury(["start"]);if (start.code !== 0) throw new Error(firstLine(start.stderr || start.stdout) || "mercury start failed");+ } else if (await localIsOutdated(runtime.port)) {+ // An older Mercury was already running: it would keep running the old code.+ await restartLocalRuntime();}const deadline = Date.now() + 30_000;while (!(await isUp(baseUrl))) {diff --git a/apps/desktop/src/shell/Onboarding.tsx b/apps/desktop/src/shell/Onboarding.tsxindex cee8e2cc..1504b3be 100644--- a/apps/desktop/src/shell/Onboarding.tsx+++ b/apps/desktop/src/shell/Onboarding.tsx@@ -10,6 +10,7 @@ import { InstallRuntime } from "./InstallRuntime";import { desktop, type LocalRuntime } from "./tauri";import { LOCAL_CONNECTION_ID, useConnections } from "./connections";import { checkCompat } from "./compat";+import { updateLocalRuntime } from "./updateLocal";/*** First run: where should Mercury run? Three short cards, one action each.@@ -58,10 +59,10 @@ export function Onboarding({ onDone, canCancel }: { onDone: () => void; canCanceawait setActive(LOCAL_CONNECTION_ID);onDone();});+ // Install the runtime this app is built for and restart onto it+ // (`mercury upgrade` only knows stable, and a running copy keeps old code).const upgradeLocal = () => run("upgrade", async () => {- const out = await desktop.runMercury(["upgrade"]);- if (out.code !== 0) throw new Error(firstLine(out.stderr || out.stdout) || `mercury upgrade exited with ${out.code}`);- await new Promise((r) => setTimeout(r, 4000));+ await updateLocalRuntime();await probe();});const startLocal = () => run("start", async () => {@@ -94,13 +95,13 @@ export function Onboarding({ onDone, canCancel }: { onDone: () => void; canCance<CardTitle>Use the Mercury here</CardTitle>{local.state === "probing" && <CardBody>Looking for a running Mercury…</CardBody>}{local.state === "running" && <CardBody>Mercury {version} is running{local.runtime.serviceInstalled ? " as a service" : ""}. The app attaches to it; nothing moves.</CardBody>}- {local.state === "outdated" && <CardBody>Mercury {version} is running, but it predates the desktop API (needs 1.5 or newer). Upgrade it and the app attaches.</CardBody>}+ {local.state === "outdated" && <CardBody>Mercury {version} is running, but it's older than this app. Update it here: the app installs the matching version and restarts Mercury.</CardBody>}{local.state === "installed" && <CardBody>Mercury {version} is installed but not running.</CardBody>}{local.state === "missing" && <CardBody>No Mercury on {platform} yet.</CardBody>}{found && <Mono className="block truncate">127.0.0.1:{local.runtime.port} · API v{local.health.apiVersion ?? 0} · pid {local.health.pid}</Mono>}<div className="flex-1" />{local.state === "running" && <Button variant="primary" size="lg" loading={busy === "local"} onClick={() => void useLocal()}>Connect</Button>}- {local.state === "outdated" && <Button variant="primary" size="lg" loading={busy === "upgrade"} onClick={() => void upgradeLocal()}>Upgrade Mercury</Button>}+ {local.state === "outdated" && <Button variant="primary" size="lg" loading={busy === "upgrade"} onClick={() => void upgradeLocal()}>Update Mercury</Button>}{local.state === "installed" && <Button variant="primary" size="lg" loading={busy === "start"} onClick={() => void startLocal()}>Start Mercury</Button>}{(local.state === "missing" || local.state === "probing") && <Button variant="outline" size="lg" disabled={local.state === "probing"} onClick={() => void probe()}>Check again</Button>}</Card>diff --git a/apps/desktop/src/shell/updateLocal.ts b/apps/desktop/src/shell/updateLocal.tsnew file mode 100644index 00000000..95c1812a--- /dev/null+++ b/apps/desktop/src/shell/updateLocal.ts@@ -0,0 +1,59 @@+import { checkCompat } from "./compat";+import { desktop } from "./tauri";++/**+ * Bring the Mercury on this computer up to what this app needs.+ *+ * Installs the runtime this build is made for (the latest stable, or the+ * release a tester build is pinned to; `mercury upgrade` would only ever+ * fetch stable), then stops whatever Mercury is running (an older copy,+ * perhaps installed another way) and starts the new one, and waits until+ * it answers with the desktop API. Installing alone isn't enough: a+ * running Mercury keeps running the old code.+ */+export async function updateLocalRuntime(onStep?: (step: "download" | "restart" | "wait") => void): Promise<void> {+ onStep?.("download");+ await desktop.installRuntime();+ await restartLocalRuntime(onStep);+}++/** Stop the running Mercury (whichever copy) and start the installed one. */+export async function restartLocalRuntime(onStep?: (step: "restart" | "wait") => void): Promise<void> {+ onStep?.("restart");+ const service = await desktop.runMercury(["service", "status"]).catch(() => null);+ if (service && /service installed/i.test(service.stdout) && !/not installed/i.test(service.stdout)) {+ // A login service runs Mercury and would bring the old copy straight+ // back: install it again from the new copy, which points it there and+ // restarts it.+ const svc = await desktop.runMercury(["service", "install"]);+ if (svc.code !== 0) throw new Error(firstLine(svc.stderr || svc.stdout) || "mercury service install failed");+ } else {+ // Not running is fine: stop is only there to clear an old copy.+ await desktop.runMercury(["stop"]).catch(() => null);+ const start = await desktop.runMercury(["start"]);+ if (start.code !== 0) throw new Error(firstLine(start.stderr || start.stdout) || "mercury start failed");+ }+ onStep?.("wait");+ const { port } = await desktop.detectLocalRuntime();+ const deadline = Date.now() + 30_000;+ for (;;) {+ const health = await desktop.probeLocalHealth(port).catch(() => null);+ if (health && checkCompat(health.apiVersion as number | undefined).ok) return;+ if (Date.now() > deadline) {+ throw new Error(health+ ? "Mercury restarted but still runs an older copy. Another install may be starting it (check `which mercury`)."+ : "Mercury didn't come back within 30 seconds. Check `mercury logs`.");+ }+ await new Promise((r) => setTimeout(r, 1000));+ }+}++/** Does a running Mercury on this port predate the desktop API? */+export async function localIsOutdated(port: number): Promise<boolean> {+ const health = await desktop.probeLocalHealth(port).catch(() => null);+ return !!health && !checkCompat(health.apiVersion as number | undefined).ok;+}++function firstLine(text: string): string {+ return text.trim().split(/\r?\n/)[0]?.slice(0, 300) ?? "";+}