From a8d87ac1d9e055b33aaa45dd8b5ca2cae6e84f04 Mon Sep 17 00:00:00 2001 From: Brooklyn Nicholson Date: Wed, 19 Aug 2026 02:52:20 -0500 Subject: [PATCH] fix(desktop): keep the preload bridge alive under the sandbox Deciding whether the OS can back glass needs os.release(), but every Hermes window runs its preload with sandbox: true, where require is a polyfill limited to electron, events, timers and url. The node:os import threw before contextBridge ran, so window.hermesDesktop was never defined and the app booted straight into "Desktop IPC bridge is unavailable". Main already computes both verdicts, so preload asks for them over a synchronous channel instead. No reply degrades to no glass, which is an ordinary opaque window rather than a page thinned over nothing. --- apps/desktop/e2e/boot.spec.ts | 19 +++++++++++++++++++ apps/desktop/electron/main.ts | 11 +++++++++++ apps/desktop/electron/preload.ts | 14 ++++++++++---- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/apps/desktop/e2e/boot.spec.ts b/apps/desktop/e2e/boot.spec.ts index 3a74fa4cc5..19f2d81fa4 100644 --- a/apps/desktop/e2e/boot.spec.ts +++ b/apps/desktop/e2e/boot.spec.ts @@ -50,6 +50,25 @@ test.describe('dev-mode boot with mock backend', () => { }) }) + // A preload that throws never reaches contextBridge, so the renderer boots + // into "Desktop IPC bridge is unavailable" and every test below it dies on a + // 120s never-became-ready timeout instead. Checking the bridge by name makes + // that failure legible. The sandbox lets preload require only electron, + // events, timers and url — adding any other node builtin lands here. + test('the preload bridge reaches the renderer', async () => { + const bridge = await fixture!.page.evaluate(() => { + const desktop = (window as unknown as { hermesDesktop?: Record }).hermesDesktop + + return { + present: typeof desktop, + glassSupported: typeof desktop?.glassSupported, + translucencySupported: typeof desktop?.translucencySupported + } + }) + + expect(bridge).toEqual({ present: 'object', glassSupported: 'boolean', translucencySupported: 'boolean' }) + }) + test('backend boots and app becomes ready', async () => { // This is the big one — wait for the full boot chain to complete: // electron starts → hermes serve is spawned → WS connects → config diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index c4746fc313..d4e3fcf165 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -306,6 +306,7 @@ import { glassActive, glassSupportedOn, normalizeState as normalizeTranslucency, + translucencySupportedOn, vibrancyFor as vibrancyForTranslucency, windowBackingOptions, windowOpacityFor @@ -402,6 +403,9 @@ const DARWIN_MAJOR = IS_MAC ? Number.parseInt(os.release(), 10) || 0 : 0 // Glass: macOS vibrancy, or Windows 11 22H2+ system backdrop. Computed once // so the renderer, the persisted default, and every chat window agree. const GLASS_SUPPORTED = glassSupportedOn(process.platform, os.release()) +// Clear rides setOpacity, a documented no-op on Linux, so neither mode works +// there and Settings drops the row entirely. +const TRANSLUCENCY_SUPPORTED = translucencySupportedOn(process.platform) const APP_ROOT = app.getAppPath() // Device-local preference: block F12 from opening DevTools. @@ -14106,6 +14110,13 @@ app.on('before-quit', () => { } }) +// Answered synchronously so preload can publish the verdict before the +// renderer's first script — see the note there on why it cannot decide this +// itself. Registered at module scope, which runs long before any window. +ipcMain.on('hermes:translucency:support', event => { + event.returnValue = { glass: GLASS_SUPPORTED, translucency: TRANSLUCENCY_SUPPORTED } +}) + ipcMain.on('hermes:translucency', (_event, payload) => { const next = normalizeTranslucency(payload, GLASS_SUPPORTED) const previous = translucencyState diff --git a/apps/desktop/electron/preload.ts b/apps/desktop/electron/preload.ts index 1296d08ff9..2d1ba597ce 100644 --- a/apps/desktop/electron/preload.ts +++ b/apps/desktop/electron/preload.ts @@ -1,11 +1,17 @@ import { contextBridge, ipcRenderer, webFrame, webUtils } from 'electron' -import os from 'node:os' -import { glassSupportedOn, translucencySupportedOn } from '../../shared/src/translucency' +// Which translucency the OS can back. Asked synchronously because the renderer +// needs it before its first paint, and answered by main because deciding it +// needs `os.release()` — a sandboxed preload may only require electron, events, +// timers and url, so importing node:os here throws before contextBridge runs +// and takes the ENTIRE bridge down with it (window.hermesDesktop undefined => +// "Desktop IPC bridge is unavailable"). No reply means no glass, which degrades +// to an ordinary opaque window rather than a page thinned over nothing. +const translucencySupport = ipcRenderer.sendSync('hermes:translucency:support') contextBridge.exposeInMainWorld('hermesDesktop', { - glassSupported: glassSupportedOn(process.platform, os.release()), - translucencySupported: translucencySupportedOn(process.platform), + glassSupported: translucencySupport?.glass === true, + translucencySupported: translucencySupport?.translucency === true, getConnection: profile => ipcRenderer.invoke('hermes:connection', profile), // Registry-scoped backend resolution: { connectionId, profile } → descriptor. getConnectionFor: payload => ipcRenderer.invoke('hermes:connection:for', payload),