The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): relaunch into the swapped bundle instead of booting a torn renderer
Brooklyn Nicholsonc19537fb033f
diff --git a/apps/desktop/electron/bundle-swap.test.ts b/apps/desktop/electron/bundle-swap.test.tsnew file mode 100644index 00000000000..8640b0657f2--- /dev/null+++ b/apps/desktop/electron/bundle-swap.test.ts@@ -0,0 +1,49 @@+import { describe, expect, it } from 'vitest'++import { detectBundleSwap } from './bundle-swap'++const RUNNING = { builtAt: '2026-08-29T04:00:00.000Z', commit: 'a'.repeat(40), source: 'local' }++describe('detectBundleSwap', () => {+ it('reports a swap when the on-disk stamp carries a different commit', () => {+ const onDisk = { ...RUNNING, commit: 'b'.repeat(40) }++ expect(detectBundleSwap(RUNNING, onDisk)).toBe(true)+ })++ it('reports a swap when the same commit was rebuilt (builtAt moved)', () => {+ const onDisk = { ...RUNNING, builtAt: '2026-08-31T23:55:41.149Z' }++ expect(detectBundleSwap(RUNNING, onDisk)).toBe(true)+ })++ // The Windows locked-binary case (#92233): the swap leg failed, so the+ // bundle on disk is still the one we are running. A relaunch would repair+ // nothing and cost the user their window.+ it('is quiet when the on-disk stamp matches the running one', () => {+ expect(detectBundleSwap(RUNNING, { ...RUNNING })).toBe(false)+ })++ it('is quiet without a running stamp (dev runs)', () => {+ expect(detectBundleSwap(null, { ...RUNNING })).toBe(false)+ })++ it('is quiet without an on-disk stamp (unreadable resources)', () => {+ expect(detectBundleSwap(RUNNING, null)).toBe(false)+ })++ it('is quiet on a fallback stamp on either side (non-git build)', () => {+ const fallbackTagged = { ...RUNNING, source: 'fallback' }+ const fallbackCommit = { ...RUNNING, commit: '0'.repeat(40) }++ expect(detectBundleSwap(fallbackTagged, { ...RUNNING, commit: 'b'.repeat(40) })).toBe(false)+ expect(detectBundleSwap(RUNNING, fallbackCommit)).toBe(false)+ })++ it('treats a missing builtAt on either side as unprovable at the same commit', () => {+ const noBuiltAt = { commit: RUNNING.commit, source: 'local' }++ expect(detectBundleSwap(noBuiltAt, { ...RUNNING })).toBe(false)+ expect(detectBundleSwap(RUNNING, noBuiltAt)).toBe(false)+ })+})diff --git a/apps/desktop/electron/bundle-swap.ts b/apps/desktop/electron/bundle-swap.tsnew file mode 100644index 00000000000..48f2b3af8af--- /dev/null+++ b/apps/desktop/electron/bundle-swap.ts@@ -0,0 +1,61 @@+/**+ * Swapped-bundle detection.+ *+ * The detached updater (scripts/desktop-update/posix.sh mac_swap /+ * windows.ps1) rebuilds and swaps the packaged app on disk AFTER+ * `hermes update` exits. An instance that was launched from the PRE-swap+ * bundle — the user reopened Hermes mid-update, the #50238 gesture the boot+ * gate exists for — would otherwise proceed to run the NEW runtime under the+ * OLD renderer. The updater's own `open`/relaunch leg cannot rescue it: the+ * single-instance lock turns that into a focus of the parked process, so no+ * process ever loads the new build.+ *+ * That is the stale-renderer tail of a FULLY SUCCESSFUL update: the "App+ * build out of date" banner appears right after the update, while the Updates+ * card says "You're on the latest version" and so offers nothing that would+ * clear it.+ *+ * Detection: compare the install stamp this process loaded at boot with the+ * one on disk now. A different commit — or a different builtAt at the same+ * commit (a dirty-tree or content-hash rebuild) — means the bundle under our+ * feet is not the one we are running, and a plain relaunch loads it.+ *+ * Fail-quiet like bundle-skew: a missing stamp on either side (dev runs,+ * unreadable resources) or a fallback all-zero commit reports "not swapped".+ * This must never false-positive — a positive triggers an automatic relaunch.+ *+ * Pure so it is testable without booting Electron.+ */++import { isFallbackCommit } from './bundle-skew'++export interface BundleSwapStamp {+ /** write-build-stamp.mjs build timestamp — differs on every rebuild. */+ builtAt?: null | string+ commit: string+ /** write-build-stamp.mjs source tag — 'fallback' means the commit is fake. */+ source?: null | string+}++/** True only on positive proof that the bundle on disk is not the running one. */+export function detectBundleSwap(running: BundleSwapStamp | null, onDisk: BundleSwapStamp | null): boolean {+ if (!running?.commit || !onDisk?.commit) {+ return false+ }++ if (running.source === 'fallback' || isFallbackCommit(running.commit)) {+ return false+ }++ if (onDisk.source === 'fallback' || isFallbackCommit(onDisk.commit)) {+ return false+ }++ if (running.commit !== onDisk.commit) {+ return true+ }++ // Same commit: only a builtAt PRESENT ON BOTH sides can prove a rebuild —+ // a missing timestamp (older stamp schema) proves nothing.+ return Boolean(running.builtAt && onDisk.builtAt && running.builtAt !== onDisk.builtAt)+}diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 2d149f3743d..e6ed368d825 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -88,6 +88,7 @@ import {buildBrowserWindowUrl} from './browser-windows'import { detectBundleSkew } from './bundle-skew'+import { detectBundleSwap } from './bundle-swap'import { applyConnectionChange, sshQuitShouldBlock, teardownSshState } from './connection-apply'import {apiRequestRegistryConnectionId,@@ -2156,6 +2157,59 @@ function updateGateDeps() {}}+// One-shot guard for the automatic bundle-swap relaunch below: the relaunched+// instance carries this flag so a stamp that still mismatches (unreadable+// resources, exotic packaging) can never produce a relaunch loop.+const BUNDLE_SWAP_RELAUNCH_FLAG = '--hermes-bundle-swap-relaunched'++// How long the parked instance waits for its own scheduled exit to land before+// giving up and booting the stale build anyway. Better a torn renderer with a+// banner than a window that never comes back.+const BUNDLE_SWAP_RELAUNCH_FAILSAFE_MS = 15_000++// The detached updater swaps the packaged bundle on disk AFTER `hermes update`+// exits (posix.sh mac_swap / windows.ps1). An instance reopened mid-update —+// the #50238 gesture the gate above exists for — was launched from the+// PRE-swap bundle, and the updater's `open` leg then merely focuses us (single+// instance), so no process ever loads the new build. Letting boot proceed here+// runs the new runtime under the old renderer: exactly the skew+// detectRendererSkew() warns about, except the Updates card already says+// "latest", so the warning's own remedy has nothing to run.+//+// This is the earliest point where the swap is PROVABLE — it happens while we+// are parked on the gate, so checking any sooner (at `ready`, before the gate)+// only ever compares a stamp with itself. Relaunching here also keeps the+// boot-progress window up for the whole wait instead of leaving the user with+// no window at all.+//+// Returns true when the relaunch was scheduled; the caller must park rather+// than continue booting, because the process exits underneath it.+function relaunchIntoSwappedBundle() {+ if (!IS_PACKAGED || process.argv.includes(BUNDLE_SWAP_RELAUNCH_FLAG)) {+ return false+ }++ if (!detectBundleSwap(INSTALL_STAMP, loadInstallStamp())) {+ return false+ }++ rememberLog('[updates] app bundle was swapped during the update; relaunching into the new build')++ try {+ app.relaunch({+ args: [...buildNoSandboxRelaunchArgs(process.argv.slice(1)), BUNDLE_SWAP_RELAUNCH_FLAG]+ })+ } catch (err) {+ rememberLog(`[updates] bundle-swap relaunch failed: ${err?.message || err}; continuing with the current build`)++ return false+ }++ void exitAfterBackendShutdown(0)++ return true+}+// Block until no live update is in progress (or we hit the wait timeout).// Emits a boot-progress phase so the renderer shows "Update in progress…"// rather than a frozen splash. Returns true if it parked at all.@@ -2218,6 +2272,14 @@ async function waitForUpdateToFinish() {if (outcome === 'timeout') {rememberLog('[updates] update still in progress after wait timeout; starting backend anyway')+ } else if (relaunchIntoSwappedBundle()) {+ await advanceBootProgress('backend.update-restart', 'Restarting Hermes to load the updated app…', 14)+ // Park while the scheduled exit lands so this stale build never starts a+ // backend; the failsafe below only runs if the exit somehow does not.+ await new Promise(resolve => setTimeout(resolve, BUNDLE_SWAP_RELAUNCH_FAILSAFE_MS))+ rememberLog(+ `[updates] relaunch did not land within ${BUNDLE_SWAP_RELAUNCH_FAILSAFE_MS}ms; continuing with the current build`+ )} else {rememberLog('[updates] update finished; proceeding with backend start')}