Skip to content

The agent that grows with you

fix(desktop): route hand-off spawn failures through describeUpdaterHandoffFailure again

ethernetb44b1010945d
diff --git a/apps/desktop/electron/updater/checkout-handoff.test.ts b/apps/desktop/electron/updater/checkout-handoff.test.ts
new file mode 100644
index 00000000000..41ac8204063
--- /dev/null
+++ b/apps/desktop/electron/updater/checkout-handoff.test.ts
@@ -0,0 +1,119 @@
+import { EventEmitter } from 'node:events'
+import * as fs from 'node:fs'
+import * as os from 'node:os'
+import * as path from 'node:path'
+
+import { afterEach, expect, it, vi } from 'vitest'
+
+import * as updaterProcess from '../updater-process'
+
+import { type CheckoutStrategyDeps, createCheckoutStrategy } from './checkout'
+import type { SourceUpdate } from './checkout-source'
+
+const IS_WINDOWS: boolean = process.platform === 'win32'
+const NO_GATEWAY_FLAG: string = IS_WINDOWS ? '-NoGateway' : '--no-gateway'
+
+afterEach((): void => {
+ vi.restoreAllMocks()
+})
+
+/** A checkout root with the repo hand-off script staged, plus a strategy over it. */
+function handoffFixture(remote: boolean): { root: string; deps: CheckoutStrategyDeps } {
+ const root: string = fs.mkdtempSync(path.join(os.tmpdir(), 'checkout-handoff-'))
+ const home: string = path.join(root, 'profile')
+ const scriptDirectory: string = path.join(root, 'scripts', 'desktop-update')
+ fs.mkdirSync(home)
+ fs.mkdirSync(scriptDirectory, { recursive: true })
+ fs.writeFileSync(path.join(scriptDirectory, IS_WINDOWS ? 'windows.ps1' : 'posix.sh'), '')
+ fs.writeFileSync(path.join(scriptDirectory, 'runtime.ps1'), '')
+ fs.mkdirSync(path.join(root, '.hermes', 'bin'), { recursive: true })
+ fs.writeFileSync(path.join(root, '.hermes', 'bin', 'hermes.exe'), '')
+
+ const status: SourceUpdate = { supported: true, branch: 'main', targetSha: 'a'.repeat(40), updateAvailable: true }
+
+ const deps: CheckoutStrategyDeps = {
+ readSourceUpdate: async (): Promise<SourceUpdate> => status,
+ hermesHome: home,
+ isWindows: IS_WINDOWS,
+ isMac: process.platform === 'darwin',
+ defaultUpdateBranch: 'main',
+ updateHandoffDwellMs: 0,
+ resolveUpdateRoot: (): string => root,
+ resolveUpdaterBinary: (): null => null,
+ remoteGatewayActive: (): boolean => remote,
+ emitUpdateProgress: vi.fn(),
+ rememberLog: vi.fn(),
+ startHermes: vi.fn(async (): Promise<void> => {}),
+ stopBackendsForUpdate: async (): Promise<void> => {},
+ repairMacUpdaterHelper: (): void => {},
+ preflightStateDb: (): void => {},
+ runningAppBundle: (): null => null,
+ markQuittingForHandoff: vi.fn(),
+ quit: vi.fn()
+ }
+
+ return { root, deps }
+}
+
+// One gateway per host (#117529): a Desktop served by a remote gateway must
+// tell the hand-off script not to (re)start a local one, and a locally-owned
+// Desktop must keep the default so its gateway comes back after the update.
+it.each([true, false])(
+ 'hand-off passes the no-gateway flag iff a remote gateway serves the app: %s',
+ async (remote: boolean): Promise<void> => {
+ const { root, deps } = handoffFixture(remote)
+ const spawned: string[][] = []
+ vi.spyOn(updaterProcess, 'spawnUpdaterProcess').mockImplementation(
+ (_command: string, args: string[]): updaterProcess.UpdaterChild => {
+ spawned.push(args)
+
+ return { unref: (): void => {} }
+ }
+ )
+
+ try {
+ expect(await createCheckoutStrategy(deps).apply()).toMatchObject({ ok: true, handedOff: true })
+ expect(spawned).toHaveLength(1)
+ const args: string[] = spawned[0]!
+ expect(args).toContain(IS_WINDOWS ? '-Branch' : '--branch')
+
+ if (remote) {
+ expect(args).toContain(NO_GATEWAY_FLAG)
+ } else {
+ expect(args).not.toContain(NO_GATEWAY_FLAG)
+ }
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
+ }
+)
+
+// A hand-off that never became viable (#66753) must not quit into nothing:
+// the app stays, the backend restarts, and the user reads plain copy with
+// the raw spawn outcome confined to a Details line.
+it('a failed hand-off spawn keeps the app alive and reports the failure in plain copy', async (): Promise<void> => {
+ const { root, deps } = handoffFixture(false)
+ vi.spyOn(updaterProcess, 'spawnUpdaterProcess').mockImplementation((): updaterProcess.UpdaterChild => {
+ const child: EventEmitter & updaterProcess.UpdaterChild = Object.assign(new EventEmitter(), {
+ unref: (): void => {}
+ })
+ queueMicrotask((): void => {
+ child.emit('error', Object.assign(new Error('spawn ENOENT'), { code: 'ENOENT' }))
+ })
+
+ return child
+ })
+
+ try {
+ const result: Awaited<ReturnType<ReturnType<typeof createCheckoutStrategy>['apply']>> =
+ await createCheckoutStrategy(deps).apply()
+ expect(result).toMatchObject({ ok: false, error: 'updater-spawn-failed' })
+ expect(result.message).toMatch(/Hermes keeps running/)
+ expect(result.message).toMatch(/Details: .*ENOENT/)
+ expect(result.message?.indexOf('Details:')).toBeGreaterThan(0)
+ expect(deps.quit).not.toHaveBeenCalled()
+ expect(deps.markQuittingForHandoff).not.toHaveBeenCalled()
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
+})
diff --git a/apps/desktop/electron/updater/checkout-remote-gateway.test.ts b/apps/desktop/electron/updater/checkout-remote-gateway.test.ts
deleted file mode 100644
index 837171e5adf..00000000000
--- a/apps/desktop/electron/updater/checkout-remote-gateway.test.ts
+++ /dev/null
@@ -1,79 +0,0 @@
-import * as fs from 'node:fs'
-import * as os from 'node:os'
-import * as path from 'node:path'
-
-import { afterEach, expect, it, vi } from 'vitest'
-
-import * as updaterProcess from '../updater-process'
-
-import { type CheckoutStrategyDeps, createCheckoutStrategy } from './checkout'
-import type { SourceUpdate } from './checkout-source'
-
-const IS_WINDOWS: boolean = process.platform === 'win32'
-const NO_GATEWAY_FLAG: string = IS_WINDOWS ? '-NoGateway' : '--no-gateway'
-
-afterEach((): void => {
- vi.restoreAllMocks()
-})
-
-// One gateway per host (#117529): a Desktop served by a remote gateway must
-// tell the hand-off script not to (re)start a local one, and a locally-owned
-// Desktop must keep the default so its gateway comes back after the update.
-it.each([true, false])('hand-off passes the no-gateway flag iff a remote gateway serves the app: %s', async (remote: boolean): Promise<void> => {
- const root: string = fs.mkdtempSync(path.join(os.tmpdir(), 'checkout-remote-gateway-'))
- const home: string = path.join(root, 'profile')
- const scriptDirectory: string = path.join(root, 'scripts', 'desktop-update')
- fs.mkdirSync(home)
- fs.mkdirSync(scriptDirectory, { recursive: true })
- fs.writeFileSync(path.join(scriptDirectory, IS_WINDOWS ? 'windows.ps1' : 'posix.sh'), '')
- fs.writeFileSync(path.join(scriptDirectory, 'runtime.ps1'), '')
- fs.mkdirSync(path.join(root, '.hermes', 'bin'), { recursive: true })
- fs.writeFileSync(path.join(root, '.hermes', 'bin', 'hermes.exe'), '')
-
- const status: SourceUpdate = { supported: true, branch: 'main', targetSha: 'a'.repeat(40), updateAvailable: true }
-
- const deps: CheckoutStrategyDeps = {
- readSourceUpdate: async (): Promise<SourceUpdate> => status,
- hermesHome: home,
- isWindows: IS_WINDOWS,
- isMac: process.platform === 'darwin',
- defaultUpdateBranch: 'main',
- updateHandoffDwellMs: 0,
- resolveUpdateRoot: (): string => root,
- resolveUpdaterBinary: (): null => null,
- remoteGatewayActive: (): boolean => remote,
- emitUpdateProgress: vi.fn(),
- rememberLog: vi.fn(),
- startHermes: async (): Promise<void> => {},
- stopBackendsForUpdate: async (): Promise<void> => {},
- repairMacUpdaterHelper: (): void => {},
- preflightStateDb: (): void => {},
- runningAppBundle: (): null => null,
- markQuittingForHandoff: (): void => {},
- quit: (): void => {}
- }
-
- const spawned: string[][] = []
- vi.spyOn(updaterProcess, 'spawnUpdaterProcess').mockImplementation(
- (_command: string, args: string[]): updaterProcess.UpdaterChild => {
- spawned.push(args)
-
- return { unref: (): void => {} }
- }
- )
-
- try {
- expect(await createCheckoutStrategy(deps).apply()).toMatchObject({ ok: true, handedOff: true })
- expect(spawned).toHaveLength(1)
- const args: string[] = spawned[0]!
- expect(args).toContain(IS_WINDOWS ? '-Branch' : '--branch')
-
- if (remote) {
- expect(args).toContain(NO_GATEWAY_FLAG)
- } else {
- expect(args).not.toContain(NO_GATEWAY_FLAG)
- }
- } finally {
- fs.rmSync(root, { recursive: true, force: true })
- }
-})
diff --git a/apps/desktop/electron/updater/checkout.ts b/apps/desktop/electron/updater/checkout.ts
index fc1f95d5607..e85509b6d85 100644
--- a/apps/desktop/electron/updater/checkout.ts
+++ b/apps/desktop/electron/updater/checkout.ts
@@ -6,6 +6,7 @@ import * as path from 'node:path'
import { updateHandoffConflict, writeUpdateMarker } from '../update-marker'
import {
collectRelaunchArgs,
+ describeUpdaterHandoffFailure,
observeUpdaterHandoff,
resolveInstallationLauncher,
resolvePosixScriptHandoff,
@@ -347,7 +348,7 @@ export function createCheckoutStrategy(deps: CheckoutStrategyDeps): UpdaterStrat
const handoffOutcome = await observeUpdaterHandoff(child, deps.updateHandoffDwellMs)
if (!handoffOutcome.ok) {
- const message = `Update failed to start: ${handoffOutcome.message}. Hermes will keep running — try again, or run \`hermes update\` from a terminal.`
+ const message: string = describeUpdaterHandoffFailure(handoffOutcome)
deps.rememberLog(`[updates] hand-off not viable, aborting quit: ${handoffOutcome.message}`)
deps.emitUpdateProgress({ stage: 'error', message, percent: null })
@@ -474,7 +475,7 @@ export function createCheckoutStrategy(deps: CheckoutStrategyDeps): UpdaterStrat
const handoffOutcome = await observeUpdaterHandoff(child, deps.updateHandoffDwellMs)
if (!handoffOutcome.ok) {
- const message = `Update failed to start: ${handoffOutcome.message}. Hermes will keep running — try again, or run \`hermes update\` from a terminal.`
+ const message: string = describeUpdaterHandoffFailure(handoffOutcome)
deps.rememberLog(`[updates] posix hand-off not viable, aborting quit: ${handoffOutcome.message}`)
deps.emitUpdateProgress({ stage: 'error', message, percent: null })