Skip to content

The agent that grows with you

fix(desktop): show a reload notice when the renderer's boot fails

dokterdokb573af6a1328
diff --git a/apps/desktop/index.html b/apps/desktop/index.html
index c227e98c5c9..45d2cd5afca 100644
--- a/apps/desktop/index.html
+++ b/apps/desktop/index.html
@@ -20,7 +20,7 @@
// `transparent: true` already have a clear canvas, and pre-painting a
// theme colour there does not prevent a flash, it IS the flash: an
// opaque rectangle standing until that window's module graph executes.
- // The list is TRANSPARENT_WINDOWS in src/main.tsx — keep the two in step.
+ // The list is TRANSPARENT_WINDOWS in src/lib/transparent-windows.ts — keep the two in step.
// `secondary` (a session window) and `browser` are opaque and DO want the
// pre-paint.
try {
diff --git a/apps/desktop/src/entry.test.ts b/apps/desktop/src/entry.test.ts
new file mode 100644
index 00000000000..29ce76cd68f
--- /dev/null
+++ b/apps/desktop/src/entry.test.ts
@@ -0,0 +1,30 @@
+import { screen } from '@testing-library/dom'
+import { afterEach, expect, it, vi } from 'vitest'
+
+vi.mock('./main', () => {
+ throw new Error('App chunk unavailable')
+})
+
+afterEach(() => {
+ document.body.replaceChildren()
+ vi.restoreAllMocks()
+ vi.unstubAllGlobals()
+})
+
+it('catches a failed app import at the renderer entry and shows recovery', async () => {
+ const root = document.createElement('div')
+ root.id = 'root'
+ document.body.append(root)
+ vi.spyOn(console, 'error').mockImplementation(() => {})
+
+ const reload = vi.fn()
+ vi.stubGlobal('location', { reload })
+
+ await import('./entry')
+
+ const button = await screen.findByRole('button', { name: 'Reload' })
+ expect(console.error).toHaveBeenCalledOnce()
+ expect(reload).not.toHaveBeenCalled()
+ button.click()
+ expect(reload).toHaveBeenCalledOnce()
+})
diff --git a/apps/desktop/src/entry.ts b/apps/desktop/src/entry.ts
index 2dae07753bd..dbd20e51094 100644
--- a/apps/desktop/src/entry.ts
+++ b/apps/desktop/src/entry.ts
@@ -3,6 +3,7 @@
// read a shared preference before this machine's store hydrated it (#145213),
// in the production chunk graph and in dev's unbundled ESM alike. Keep this
// file's static imports free of app modules.
+import { showBootFailure } from './lib/boot-failure'
import { bootRenderer } from './lib/ui-preferences/boot'
-void bootRenderer(() => import('./main'))
+void bootRenderer(() => import('./main')).catch(showBootFailure)
diff --git a/apps/desktop/src/lib/boot-failure.test.ts b/apps/desktop/src/lib/boot-failure.test.ts
new file mode 100644
index 00000000000..87526850bc5
--- /dev/null
+++ b/apps/desktop/src/lib/boot-failure.test.ts
@@ -0,0 +1,79 @@
+import { within } from '@testing-library/dom'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+import { showBootFailure } from './boot-failure'
+import { bootRenderer } from './ui-preferences/boot'
+
+let root: HTMLDivElement
+
+beforeEach(() => {
+ root = document.createElement('div')
+ root.id = 'root'
+ document.body.append(root)
+ vi.spyOn(console, 'error').mockImplementation(() => {})
+})
+
+afterEach(() => {
+ root.remove()
+ window.history.replaceState(null, '', '/')
+ vi.restoreAllMocks()
+})
+
+it('shows a rejected loader’s cause and reloads only when the user clicks Reload', async () => {
+ const error = new Error('Unable to preload CSS')
+ const reload = vi.fn()
+
+ await bootRenderer(() => Promise.reject(error)).catch(error => showBootFailure(error, reload))
+
+ expect(console.error).toHaveBeenCalledWith('Hermes failed to load', error)
+ expect(within(root).getByRole('heading', { level: 1 }).textContent).toBe('Hermes couldn’t load')
+ expect(within(root).getByRole('alert').textContent).toContain('Part of the app didn’t load; reloading usually fixes it.')
+ // The notice stays the page's main landmark; only its message is the alert.
+ expect(within(root).getByRole('main').contains(within(root).getByRole('alert'))).toBe(true)
+
+ const button = within(root).getByRole<HTMLButtonElement>('button', { name: 'Reload' })
+ expect(reload).not.toHaveBeenCalled()
+ button.click()
+ expect(reload).toHaveBeenCalledTimes(1)
+})
+
+it.each(['element', 'text'])('leaves mounted %s content untouched and still logs the cause', async kind => {
+ const content = kind === 'element' ? document.createElement('section') : document.createTextNode('')
+ content.textContent = 'Already mounted'
+ root.append(content)
+
+ const error = new Error('App failed after mounting')
+ const reload = vi.fn()
+
+ await bootRenderer(() => Promise.reject(error)).catch(error => showBootFailure(error, reload))
+
+ expect([...root.childNodes]).toEqual([content])
+ expect(root.textContent).toBe('Already mounted')
+ expect(console.error).toHaveBeenCalledWith('Hermes failed to load', error)
+ expect(reload).not.toHaveBeenCalled()
+})
+
+it('uses no inline handler attributes under script-src-attr none', () => {
+ showBootFailure(new Error('Chunk failed'), vi.fn())
+ expect(within(root).getByRole('button', { name: 'Reload' })).toBeTruthy()
+
+ for (const element of root.querySelectorAll('*')) {
+ expect(element.getAttributeNames().filter(name => /^on/i.test(name))).toEqual([])
+ }
+})
+
+it.each(['hud', 'overlay', 'quick', 'wake'])('only logs in the transparent %s window, which cannot show a notice', kind => {
+ window.history.replaceState(null, '', `/?win=${kind}`)
+ const error = new Error('Overlay chunk failed')
+
+ showBootFailure(error, vi.fn())
+
+ expect(root.childNodes).toHaveLength(0)
+ expect(console.error).toHaveBeenCalledWith('Hermes failed to load', error)
+})
+
+it('shows the notice in an opaque secondary window', () => {
+ window.history.replaceState(null, '', '/?win=secondary')
+ showBootFailure(new Error('Session window chunk failed'), vi.fn())
+ expect(within(root).getByRole('button', { name: 'Reload' })).toBeTruthy()
+})
diff --git a/apps/desktop/src/lib/boot-failure.ts b/apps/desktop/src/lib/boot-failure.ts
new file mode 100644
index 00000000000..dd3d9c62c30
--- /dev/null
+++ b/apps/desktop/src/lib/boot-failure.ts
@@ -0,0 +1,51 @@
+import { isTransparentWindow } from './transparent-windows'
+
+// Keep this independent of the app graph: its JavaScript or CSS may have failed to load.
+export function showBootFailure(error: unknown, reload: () => void = () => location.reload()): void {
+ console.error('Hermes failed to load', error)
+
+ const root = document.getElementById('root')
+
+ // A transparent window (the pet, the wake indicator) is click-through or too small for a
+ // notice, and would only paint it over the user's desktop: there the cause is logged alone.
+ if (!root || root.hasChildNodes() || isTransparentWindow()) {
+ return
+ }
+
+ const notice = document.createElement('main')
+ notice.lang = 'en'
+ Object.assign(notice.style, {
+ alignItems: 'center',
+ boxSizing: 'border-box',
+ colorScheme: 'inherit',
+ display: 'flex',
+ flexDirection: 'column',
+ font: '16px/1.5 system-ui, sans-serif',
+ justifyContent: 'center',
+ minHeight: '100vh',
+ padding: '24px',
+ textAlign: 'center'
+ })
+
+ // English is intentional: i18n lives in the app graph that failed to load.
+ const heading = document.createElement('h1')
+ heading.textContent = 'Hermes couldn’t load'
+ heading.style.cssText = 'font-size: 1.5em; margin: 0'
+
+ const message = document.createElement('p')
+ message.textContent = 'Part of the app didn’t load; reloading usually fixes it.'
+ message.style.cssText = 'margin: 16px 0; max-width: 36em'
+
+ const button = document.createElement('button')
+ button.type = 'button'
+ button.textContent = 'Reload'
+ button.style.cssText =
+ 'font: inherit; color: inherit; background: transparent; border: 1px solid currentColor; padding: 8px 16px; cursor: pointer'
+ button.addEventListener('click', () => reload())
+
+ const alert = document.createElement('div')
+ alert.setAttribute('role', 'alert')
+ alert.append(heading, message)
+ notice.append(alert, button)
+ root.append(notice)
+}
diff --git a/apps/desktop/src/lib/transparent-windows.ts b/apps/desktop/src/lib/transparent-windows.ts
new file mode 100644
index 00000000000..aee780fa972
--- /dev/null
+++ b/apps/desktop/src/lib/transparent-windows.ts
@@ -0,0 +1,12 @@
+// The `?win=` kinds whose Electron window is `transparent: true` and so paints
+// nothing but its own surface over the user's desktop. `secondary` (a session
+// window) and `browser` are ordinary opaque windows and are deliberately not
+// in here. index.html's pre-paint script skips exactly this list — keep the
+// two in step. No imports: the renderer entry reads it before the app loads.
+export const TRANSPARENT_WINDOWS = new Set(['hud', 'overlay', 'quick', 'wake'])
+
+export function isTransparentWindow(search = window.location.search): boolean {
+ const win = new URLSearchParams(search).get('win')
+
+ return Boolean(win && TRANSPARENT_WINDOWS.has(win))
+}
diff --git a/apps/desktop/src/main.tsx b/apps/desktop/src/main.tsx
index f9d91247e47..cc9fc75098c 100644
--- a/apps/desktop/src/main.tsx
+++ b/apps/desktop/src/main.tsx
@@ -38,6 +38,7 @@ import { installClipboardShim } from './lib/clipboard'
import { queryClient } from './lib/query-client'
import { installRendererAnimationPauseState } from './lib/renderer-loop-pause'
import { installSelectionCopyColorGuard } from './lib/selection-copy-colors'
+import { isTransparentWindow } from './lib/transparent-windows'
import { startUiPreferencesSync } from './lib/ui-preferences/sync'
import { installBrowserTabTitle } from './store/browser-tab-title'
import { ThemeProvider } from './themes/context'
@@ -66,13 +67,6 @@ if (winParam === 'hud') {
document.title = 'Hermes HUD'
}
-// The `?win=` kinds whose Electron window is `transparent: true` and so paints
-// nothing but its own surface over the user's desktop. `secondary` (a session
-// window) and `browser` are ordinary opaque windows and are deliberately not
-// in here. index.html's pre-paint script skips exactly this list — keep the
-// two in step.
-const TRANSPARENT_WINDOWS = new Set(['hud', 'overlay', 'quick', 'wake'])
-
// Each transparent root used to force its host layers see-through when it
// MOUNTED. That is far too late: `styles.css` above paints the theme's opaque
// `--background` as soon as it lands, and the root behind it is a dynamic
@@ -80,7 +74,7 @@ const TRANSPARENT_WINDOWS = new Set(['hud', 'overlay', 'quick', 'wake'])
// rendered as a full-screen near-white rectangle. Claim it here instead, in the
// same task as the stylesheet, so no window ever paints a background it does
// not want.
-if (winParam && TRANSPARENT_WINDOWS.has(winParam)) {
+if (isTransparentWindow()) {
const transparent = document.createElement('style')
transparent.textContent = 'html,body,#root{background:transparent !important;}'