Skip to content

The agent that grows with you

fix(desktop): stop logging a handler stack for expected session-probe 404s

hernanda-git65db662577a0
diff --git a/apps/desktop/electron/api-expected-404.test.ts b/apps/desktop/electron/api-expected-404.test.ts
new file mode 100644
index 00000000000..bd712f54388
--- /dev/null
+++ b/apps/desktop/electron/api-expected-404.test.ts
@@ -0,0 +1,60 @@
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { HERMES_API_EXPECTED_404, isExpectedNotFoundSentinel, unwrapExpectedNotFound } from './api-expected-404'
+
+test('a handler-produced sentinel is recognized', () => {
+ assert.equal(isExpectedNotFoundSentinel({ [HERMES_API_EXPECTED_404]: '404: {"detail":"Session not found"}' }), true)
+})
+
+test('real backend payloads are never mistaken for a sentinel', () => {
+ const notSentinels: unknown[] = [
+ null,
+ undefined,
+ 'string',
+ 42,
+ [],
+ [{ [HERMES_API_EXPECTED_404]: 'x' }],
+ {},
+ { session_id: 's1' },
+ // right key, wrong value type
+ { [HERMES_API_EXPECTED_404]: 404 },
+ // right key, but carries real data alongside — not our shape
+ { [HERMES_API_EXPECTED_404]: 'x', session_id: 's1' }
+ ]
+
+ for (const value of notSentinels) {
+ assert.equal(isExpectedNotFoundSentinel(value), false, `treated ${JSON.stringify(value)} as a sentinel`)
+ }
+})
+
+test('unwrap rethrows the sentinel as the exact rejection the renderer expects', () => {
+ const message = '404: {"detail":"Session not found"}'
+
+ assert.throws(
+ () => unwrapExpectedNotFound({ [HERMES_API_EXPECTED_404]: message }),
+ (error: Error) => error instanceof Error && error.message === message
+ )
+})
+
+test('unwrap passes real responses through untouched, by reference', () => {
+ const payload = { session_id: 's1', messages: [] }
+
+ assert.equal(unwrapExpectedNotFound(payload), payload)
+ assert.equal(unwrapExpectedNotFound(null), null)
+ assert.equal(unwrapExpectedNotFound(''), '')
+})
+
+test('the rethrown message still matches the renderer 404 probe predicate', () => {
+ // `resolveStoredSession` and friends branch on a `404`-shaped message; the
+ // seam must not change what they see.
+ const message = '404: {"detail":"Session not found"}'
+
+ try {
+ unwrapExpectedNotFound({ [HERMES_API_EXPECTED_404]: message })
+ assert.fail('expected a rejection')
+ } catch (error) {
+ assert.match(String((error as Error).message), /(?:^|\s)404\b/)
+ }
+})
diff --git a/apps/desktop/electron/api-expected-404.ts b/apps/desktop/electron/api-expected-404.ts
new file mode 100644
index 00000000000..da6c1258613
--- /dev/null
+++ b/apps/desktop/electron/api-expected-404.ts
@@ -0,0 +1,42 @@
+// The `hermes:api` expected-404 seam, shared by the main-process handler and
+// preload so both sides agree on one literal.
+//
+// Electron logs "Error occurred in handler for 'hermes:api'" with a full stack
+// trace for every rejected `ipcMain.handle` invoke, and offers no way to opt a
+// handler out. Desktop's session resolution is a deliberate probe ladder
+// (`resolveStoredSession`: cache → active backend → each other profile), so a
+// 404 there is a normal rung outcome the renderer already handles — but each
+// one printed a stack into the launching terminal.
+//
+// The handler therefore RESOLVES with this sentinel for that one expected case,
+// and preload rethrows it as an ordinary `Error` carrying the identical
+// `404: <body>` message. The renderer sees exactly the rejection it saw before;
+// only Electron's logging is bypassed. Any other failure rejects as usual and
+// still logs in full.
+
+const HERMES_API_EXPECTED_404 = '__hermesExpected404__'
+
+// True when `value` is a handler-resolved expected-404 sentinel, not real
+// response data. Kept narrow: a plain object whose ONLY key is the sentinel and
+// whose value is a string, so a backend payload can't be mistaken for one.
+function isExpectedNotFoundSentinel(value: unknown): value is Record<string, string> {
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
+ return false
+ }
+
+ const keys = Object.keys(value as Record<string, unknown>)
+
+ return keys.length === 1 && keys[0] === HERMES_API_EXPECTED_404 && typeof (value as any)[HERMES_API_EXPECTED_404] === 'string'
+}
+
+// Restore the caller-visible contract: a sentinel becomes the rejection the
+// renderer expects; anything else passes through untouched.
+function unwrapExpectedNotFound(value: unknown): unknown {
+ if (isExpectedNotFoundSentinel(value)) {
+ throw new Error(value[HERMES_API_EXPECTED_404])
+ }
+
+ return value
+}
+
+export { HERMES_API_EXPECTED_404, isExpectedNotFoundSentinel, unwrapExpectedNotFound }
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index d9f51df7444..7d515b71c86 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -35,6 +35,7 @@ import {
import type { Session } from 'electron'
import { type ActiveRuntimeState, classifyActiveRuntime } from './active-runtime-state'
+import { HERMES_API_EXPECTED_404 } from './api-expected-404'
import {
destroyKeepaliveAgents,
htmlResponseError,
@@ -17768,10 +17769,29 @@ ipcMain.handle('hermes:api', async (_event, request) => {
return await handleHermesApiRequest(request).finally(releaseProfileDeletion)
} catch (error) {
+ // Electron logs "Error occurred in handler for 'hermes:api'" with a full
+ // stack for EVERY rejected invoke, and there is no opt-out on the handler.
+ // Session resolution is a deliberate probe ladder (`resolveStoredSession`:
+ // cache → active backend → each other profile) and the renderer already
+ // handles a miss by falling to the next rung — so an expected 404 is not an
+ // error condition. Left rejecting, it printed a multi-line stack per probe
+ // on every startup and session switch: pure noise that buries genuine
+ // handler failures.
+ //
+ // So don't reject for that one case — RESOLVE with a sentinel and let
+ // preload (our own code, the other side of the same seam) turn it back into
+ // a rejection with the identical `404: <body>` message. The renderer
+ // contract is unchanged; only Electron's logging is bypassed. Every other
+ // failure still rejects and still logs in full.
+ if (isNotFoundApiError(error)) {
+ return { [HERMES_API_EXPECTED_404]: String((error as any)?.message ?? error) }
+ }
+
// Persist the failure (full stack) before the rejection crosses to the
// renderer, where the invoke wrapper strips it to a one-line message.
rememberLog(formatApiRequestFailure(request, error))
flushDesktopLogBufferSync()
+
throw error
}
})
diff --git a/apps/desktop/electron/preload.ts b/apps/desktop/electron/preload.ts
index 8109790464a..23948840b2f 100644
--- a/apps/desktop/electron/preload.ts
+++ b/apps/desktop/electron/preload.ts
@@ -23,6 +23,8 @@ const launchFlags: { localModels?: boolean; guestOnboarding?: boolean } | undefi
// the built-in palette over the skin configured on this machine.
const localSkin = ipcRenderer.sendSync('hermes:skin:local')
+import { unwrapExpectedNotFound } from './api-expected-404'
+
contextBridge.exposeInMainWorld('hermesDesktop', {
glassSupported: translucencySupport?.glass === true,
translucencySupported: translucencySupport?.translucency === true,
@@ -332,7 +334,10 @@ contextBridge.exposeInMainWorld('hermesDesktop', {
remember: name => ipcRenderer.invoke('hermes:profile:remember', name),
set: name => ipcRenderer.invoke('hermes:profile:set', name)
},
- api: request => ipcRenderer.invoke('hermes:api', request),
+ // The handler resolves an expected 404 with a sentinel instead of rejecting
+ // (Electron logs a stack for every rejected invoke). Turn it back into the
+ // rejection the renderer expects — see electron/api-expected-404.ts.
+ api: request => ipcRenderer.invoke('hermes:api', request).then(unwrapExpectedNotFound),
notify: payload => ipcRenderer.invoke('hermes:notify', payload),
claimStartupLatency: () => ipcRenderer.invoke('hermes:startup-latency:claim'),
requestMicrophoneAccess: () => ipcRenderer.invoke('hermes:requestMicrophoneAccess'),