Skip to content

The agent that grows with you

fix(desktop): record intentional quit sentinels as expected transitions, not crash stacks

Brooklyn Nicholsonb0da99e0a102
diff --git a/apps/desktop/electron/crash-forensics.test.ts b/apps/desktop/electron/crash-forensics.test.ts
index 59a867ff477..e28ea16eb4f 100644
--- a/apps/desktop/electron/crash-forensics.test.ts
+++ b/apps/desktop/electron/crash-forensics.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from 'vitest'
-import { describeCrashReason, installCrashForensics } from './crash-forensics'
+import { describeCrashReason, installCrashForensics, isExpectedTransition, markExpectedTransition } from './crash-forensics'
const harness = () => {
const listeners = new Map<string, (value: unknown) => void>()
@@ -61,4 +61,35 @@ describe('installCrashForensics', () => {
expect(log).toHaveBeenCalledWith(expect.stringContaining('gateway ticket mint failed'))
expect(flush).toHaveBeenCalledTimes(1)
})
+
+ it('records a marked quit sentinel as a one-line expected transition, not a crash stack', () => {
+ const { flush, listeners, log } = harness()
+
+ // The exact shape an intentional quit leaves behind: the AbortController
+ // reason from local-backend-lifecycle rejecting an in-flight start.
+ const sentinel = markExpectedTransition(new Error('Hermes Desktop is quitting.'))
+ sentinel.stack = 'Error: Hermes Desktop is quitting.\n at Object.run [as shutdown] (file:///app.asar/dist/electron-main.mjs:1374:40)'
+
+ listeners.get('unhandledRejection')?.(sentinel)
+
+ expect(log).toHaveBeenCalledTimes(1)
+ const message = log.mock.calls[0]?.[0] as string
+ expect(message).toContain('expected shutdown transition')
+ expect(message).toContain('Hermes Desktop is quitting.')
+ // No stack frames: the line must not read as a crash in desktop.log.
+ expect(message).not.toContain('electron-main.mjs')
+ expect(flush).toHaveBeenCalledTimes(1)
+ })
+
+ it('marks only stamped errors — a same-message unmarked error still renders its stack', () => {
+ const { listeners, log } = harness()
+
+ const lookalike = new Error('Hermes Desktop is quitting.')
+ lookalike.stack = 'Error: Hermes Desktop is quitting.\n at main'
+
+ listeners.get('unhandledRejection')?.(lookalike)
+
+ expect(isExpectedTransition(lookalike)).toBe(false)
+ expect(log).toHaveBeenCalledWith(expect.stringContaining('at main'))
+ })
})
diff --git a/apps/desktop/electron/crash-forensics.ts b/apps/desktop/electron/crash-forensics.ts
index 7ee0aedd48f..cbc49523e10 100644
--- a/apps/desktop/electron/crash-forensics.ts
+++ b/apps/desktop/electron/crash-forensics.ts
@@ -36,12 +36,45 @@ export function describeCrashReason(reason: unknown): string {
}
}
+/** Property stamped on intentional app-transition sentinel errors. */
+const EXPECTED_TRANSITION_FLAG = '__hermesExpectedTransition'
+
+/**
+ * Stamp a sentinel Error as an expected, intentional app transition (#119409).
+ *
+ * The shutdown path aborts in-flight work on purpose; the sentinel's rejection
+ * reaches the unhandledRejection hook on every clean quit. Marking it lets
+ * crash forensics record the transition in one line instead of a crash-shaped
+ * stack, so error-shaped entries in desktop.log keep meaning "something died".
+ */
+export function markExpectedTransition<T extends Error>(error: T): T {
+ ;(error as T & { [EXPECTED_TRANSITION_FLAG]?: boolean })[EXPECTED_TRANSITION_FLAG] = true
+
+ return error
+}
+
+/** True when a rejection reason is a marked intentional-transition sentinel. */
+export function isExpectedTransition(reason: unknown): reason is Error {
+ if (!(reason instanceof Error)) {
+ return false
+ }
+
+ return (reason as unknown as Record<string, unknown>)[EXPECTED_TRANSITION_FLAG] === true
+}
+
/**
* Record main-process faults to desktop.log and flush synchronously, since a
* fault that does prove fatal leaves no chance for the batched async flush.
*/
export function installCrashForensics({ flush, log, target = process }: CrashForensicsOptions): void {
const record = (label: string) => (reason: unknown) => {
+ if (isExpectedTransition(reason)) {
+ log(`[main] ${label}: expected shutdown transition — ${reason.message}`)
+ flush()
+
+ return
+ }
+
log(`[main] ${label}: ${describeCrashReason(reason)}`)
flush()
}
diff --git a/apps/desktop/electron/local-backend-lifecycle.test.ts b/apps/desktop/electron/local-backend-lifecycle.test.ts
index af0cead5001..afe2fc193a2 100644
--- a/apps/desktop/electron/local-backend-lifecycle.test.ts
+++ b/apps/desktop/electron/local-backend-lifecycle.test.ts
@@ -4,6 +4,7 @@ import { once } from 'node:events'
import { test, vi } from 'vitest'
+import { isExpectedTransition } from './crash-forensics'
import { createFirstRunSetupGate } from './first-run-setup-gate'
import { createLocalBackendLifecycle } from './local-backend-lifecycle'
import { runPrimaryBackendStartup } from './primary-backend-startup'
@@ -179,3 +180,17 @@ test('a child spawned before claim remains owned after the routing entry disappe
}
}
})
+
+test('the quit abort sentinel is marked as an expected shutdown transition', async () => {
+ const lifecycle = createLocalBackendLifecycle({
+ stopChild: () => {},
+ waitForExit: async () => {},
+ cancelSetup: () => {}
+ })
+
+ await lifecycle.shutdown()
+
+ assert.equal(lifecycle.signal.aborted, true)
+ assert.equal(isExpectedTransition(lifecycle.signal.reason), true)
+ assert.equal((lifecycle.signal.reason as Error).message, 'Hermes Desktop is quitting.')
+})
diff --git a/apps/desktop/electron/local-backend-lifecycle.ts b/apps/desktop/electron/local-backend-lifecycle.ts
index fecacb3707b..c9afaa2d255 100644
--- a/apps/desktop/electron/local-backend-lifecycle.ts
+++ b/apps/desktop/electron/local-backend-lifecycle.ts
@@ -1,4 +1,5 @@
import { createBackendShutdownCoordinator } from './backend-ownership'
+import { markExpectedTransition } from './crash-forensics'
/** Observe every branch, but never leave quit parked on a lost exit/SSH callback. */
export async function waitForTeardown(tasks: readonly Promise<unknown>[], timeoutMs: number): Promise<void> {
@@ -74,7 +75,7 @@ export function createLocalBackendLifecycle<Child>(
}
const shutdown = createBackendShutdownCoordinator((): Promise<void> => {
- controller.abort(new Error('Hermes Desktop is quitting.'))
+ controller.abort(markExpectedTransition(new Error('Hermes Desktop is quitting.')))
deps.cancelSetup()
return waitForTeardown([...starts, ...[...children].map(stop), ...stops.values()], deps.timeoutMs ?? 7_000)
diff --git a/apps/desktop/electron/ssh-bootstrap-coordinator.test.ts b/apps/desktop/electron/ssh-bootstrap-coordinator.test.ts
index ae4daf60ede..73a32ba795c 100644
--- a/apps/desktop/electron/ssh-bootstrap-coordinator.test.ts
+++ b/apps/desktop/electron/ssh-bootstrap-coordinator.test.ts
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict'
import { test } from 'vitest'
+import { isExpectedTransition } from './crash-forensics'
import { createBootstrapCoordinator, sshConfigFingerprint } from './ssh-bootstrap-coordinator'
function deferred() {
@@ -311,3 +312,15 @@ test('a second cancelAndWait on the same scope composes with the teardown still
assert.deepEqual(events.slice(0, 2), ['teardown-start', 'teardown-done'])
assert.deepEqual(events.slice(2).sort(), ['apply-drained', 'new-start'])
})
+
+test('a start after shutdown rejects with a marked expected-transition sentinel', async () => {
+ const coordinator = createBootstrapCoordinator()
+ coordinator.shutdown()
+
+ await assert.rejects(
+ coordinator.start('', 'fingerprint', async () => {}),
+ (error: unknown) =>
+ isExpectedTransition(error) &&
+ (error as Error).message === 'SSH bootstrap was cancelled because Desktop is quitting.'
+ )
+})
diff --git a/apps/desktop/electron/ssh-bootstrap-coordinator.ts b/apps/desktop/electron/ssh-bootstrap-coordinator.ts
index 9b062eac8bf..1539d481b94 100644
--- a/apps/desktop/electron/ssh-bootstrap-coordinator.ts
+++ b/apps/desktop/electron/ssh-bootstrap-coordinator.ts
@@ -1,5 +1,7 @@
import crypto from 'node:crypto'
+import { markExpectedTransition } from './crash-forensics'
+
function sshConfigFingerprint(scope, config) {
const parts = [
scope,
@@ -30,7 +32,7 @@ function createBootstrapCoordinator() {
const error: any = new Error('SSH bootstrap was cancelled because Desktop is quitting.')
error.kind = 'superseded'
- return Promise.reject(error)
+ return Promise.reject(markExpectedTransition(error))
}
const current = pending.get(scope)