Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(updates): confirm versions that report no version, and roll back fast when the new one crashes

Salman Qureshifb70860b312f
diff --git a/apps/desktop/src/updates/store.ts b/apps/desktop/src/updates/store.ts
index cbcb3c1f..b28cc9a0 100644
--- a/apps/desktop/src/updates/store.ts
+++ b/apps/desktop/src/updates/store.ts
@@ -484,8 +484,14 @@ async function followJob(conn: Connection, first: UpdateJob): Promise<void> {
await pause(1500);
try {
const info = await fetchServerUpdates(conn);
+ if (!info) {
+ // Up again on a version without the updates API (1.4.2 and older): health says it's back.
+ const v = await serverVersion(conn);
+ if (v === null) throw new Error("down");
+ job = { ...job, state: v === "" || v === job.to ? "done" : "rolled-back", percent: 100, message: v === "" || v === job.to ? `Updated to ${job.to}` : `Running ${v}` };
+ break;
+ }
downSince = null;
- if (!info) throw new Error("no updates API");
setServer(conn.id, { info, version: info.current });
if (info.job && info.job.id === job.id) job = info.job;
else if (!info.job && info.current === job.to) job = { ...job, state: "done", percent: 100, message: `Updated to ${job.to}` };
diff --git a/src/updates/server-updater.ts b/src/updates/server-updater.ts
index 819d9f48..7a053944 100644
--- a/src/updates/server-updater.ts
+++ b/src/updates/server-updater.ts
@@ -522,6 +522,30 @@ function realPrepareSteps(): PrepareSteps {
};
}
+/** Is Mercury running (as the daemon or the login service)? */
+async function runtimeAlive(): Promise<boolean> {
+ try {
+ const d = await import('../cli/daemon.js');
+ const sv = await import('../cli/service.js');
+ return d.getDaemonStatus().running || d.getForegroundRuntimeStatus().running || (sv.isServiceInstalled() && sv.isServiceRunning());
+ } catch {
+ return true; // can't tell: keep waiting for health
+ }
+}
+
+/** `mercury --version` of the program installed now (what the next start runs), or null. */
+async function installedVersion(): Promise<string | null> {
+ const { buildDaemonSpawnArgs } = await import('../cli/daemon.js');
+ const { command, args } = buildDaemonSpawnArgs();
+ const argv = [...args.slice(0, -2), '--version'];
+ return new Promise((resolve) => {
+ execFile(command, argv, { timeout: 15_000, encoding: 'utf-8' }, (err, stdout) => {
+ const line = String(stdout ?? '').trim().split('\n').pop()?.trim() ?? '';
+ resolve(!err && /^\d+\.\d+\.\d+/.test(line) ? line : null);
+ });
+ });
+}
+
async function healthVersion(): Promise<{ version: string; booting: boolean } | null> {
const { getWebPort } = await import('../web/auth.js');
try {
@@ -565,10 +589,27 @@ export function realWorkerSteps(): WorkerSteps {
d.ensureDaemonRunning();
},
async health(version, timeoutMs) {
- const until = Date.now() + timeoutMs;
+ const started = Date.now();
+ const until = started + timeoutMs;
+ let installed: string | null | undefined;
+ let goneSince: number | null = null;
while (Date.now() < until) {
+ // A new version that crashed on start doesn't get the full wait: no
+ // process for 5 s (after a 10 s grace) is an answer already.
+ if (Date.now() - started > 10_000 && !(await runtimeAlive())) {
+ goneSince ??= Date.now();
+ if (Date.now() - goneSince > 5_000) return false;
+ } else goneSince = null;
const h = await healthVersion();
- if (h && h.version === version && !h.booting) return true;
+ if (h && !h.booting) {
+ if (h.version === version) return true;
+ // Standalone builds up to 1.4.2 answer health with an empty version:
+ // then the program now installed says which one it is.
+ if (h.version === '') {
+ installed ??= await installedVersion();
+ if (installed === version) return true;
+ }
+ }
await new Promise((r) => setTimeout(r, 1000));
}
return false;