Skip to content

The agent that grows with you

test(desktop-core): boot-lifecycle encodes the attach-client contract

teknium199fb871d9e58
diff --git a/apps/desktop/e2e/core/boot-lifecycle.spec.ts b/apps/desktop/e2e/core/boot-lifecycle.spec.ts
index 5b1c23dcb78..fa276084ced 100644
--- a/apps/desktop/e2e/core/boot-lifecycle.spec.ts
+++ b/apps/desktop/e2e/core/boot-lifecycle.spec.ts
@@ -10,11 +10,15 @@
* and passes the transcript oracle.
* 2. kill -9 the backend: the supervisor respawns EXACTLY one replacement
* (no crash-loop, no double spawn), and the app serves a new turn.
- * 3. quit while a turn is streaming and a tool subprocess is running: zero
- * sandbox processes remain — no backend, no tool child, no Electron helper.
- * 4. relaunch the same HERMES_HOME repeatedly: each boot has exactly one
- * backend, each quit leaves zero processes, and the transcript persisted
- * by the first launch cold-hydrates exactly once every time.
+ * 3. quit while a turn is streaming and a tool subprocess is running: every
+ * Electron process is gone, and exactly ONE gateway remains — the same
+ * daemon that served the app, still answering. Desktop is an attach
+ * client of the profile's gateway (`hermes gateway ensure`): closing the
+ * app never takes cron, messaging adapters, bots or an in-flight turn down
+ * with it. `hermes gateway stop` then proves teardown works when asked.
+ * 4. relaunch the same HERMES_HOME repeatedly: each boot ATTACHES to the one
+ * gateway (same PID throughout, never a second backend), and the transcript
+ * persisted by the first launch cold-hydrates exactly once every time.
*/
import * as fs from 'node:fs'
@@ -31,6 +35,7 @@ import {
recordWebSockets,
sandboxProcesses,
send,
+ stopSandboxGateway,
waitForInteractive,
writeProviderHome
} from './harness'
@@ -157,7 +162,7 @@ test('boot handshake, supervised respawn, and zero orphans on quit', async () =>
expect(seen.size, `backend pids ever seen: ${describeSeen()}`).toBe(2)
})
- await test.step('quit mid-turn with a running tool child: zero processes remain', async () => {
+ await test.step('quit mid-turn with a running tool child: the app is gone, the one gateway is not', async () => {
const hold = gate()
provider.script(U(3), [
{
@@ -170,25 +175,40 @@ test('boot handshake, supervised respawn, and zero orphans on quit', async () =>
await expect
.poll(() => taggedProcesses(TOOL_TAG).length, { timeout: 120_000, message: 'tool child running' })
.toBeGreaterThan(0)
- expect(sandboxProcesses(sandbox).length).toBeGreaterThan(0)
+ const [gateway] = backendProcesses(sandbox)
+ expect(gateway).toBeTruthy()
await app.close()
closed = true
clearInterval(census)
+ // Electron main + helpers leave; the gateway they attached to stays and keeps its turn.
+ await expect
+ .poll(
+ () =>
+ sandboxProcesses(sandbox)
+ .filter(p => /electron/i.test(p.cmdline.split(' ')[0]))
+ .map(p => `${p.pid} ${p.cmdline.slice(0, 120)}`),
+ { timeout: 60_000, message: 'no Electron process survives quit' }
+ )
+ .toEqual([])
+ expect(backendProcesses(sandbox).map(p => p.pid), 'the same gateway keeps serving after quit').toEqual([gateway!.pid])
+ expect(seen.size, `backend pids ever seen: ${describeSeen()}`).toBe(2)
+ hold.open()
+
+ // Teardown on request: the CLI stop takes the daemon and its tool child down.
+ const stopped = stopSandboxGateway(sandbox)
+ expect(stopped.code, stopped.output).toBe(0)
await expect
.poll(
() =>
[...sandboxProcesses(sandbox), ...taggedProcesses(TOOL_TAG)].map(
p => `${p.pid} ${p.cmdline.slice(0, 120)}`
),
- {
- timeout: 60_000,
- message: 'no sandbox process (backend, tool child, Electron helper) survives quit'
- }
+ { timeout: 60_000, message: 'no sandbox process (gateway, tool child) survives hermes gateway stop' }
)
.toEqual([])
- hold.open()
})
+
} finally {
clearInterval(census)
@@ -210,12 +230,13 @@ test('boot handshake, supervised respawn, and zero orphans on quit', async () =>
}
})
-test('relaunching the same home: one backend per boot, zero after each quit, transcript intact', async () => {
+test('relaunching the same home: one gateway across every boot, transcript intact', async () => {
const provider = await startScriptedProvider()
const sandbox = createCoreSandbox('relaunch')
writeProviderHome(sandbox.hermesHome, provider.url)
const session: OracleTarget = { sessionId: '', expectUserMarkers: [U(1)] }
let live: Awaited<ReturnType<typeof launchCoreApp>> | null = null
+ let gatewayPid: number | null = null
try {
for (let launch = 1; launch <= 3; launch++) {
@@ -229,6 +250,11 @@ test('relaunching the same home: one backend per boot, zero after each quit, tra
.poll(() => backendProcesses(sandbox).length, { message: `one backend on launch ${launch}` })
.toBe(1)
+ // Launch 1 starts the profile's gateway; every later launch attaches to that same daemon.
+ const [gateway] = backendProcesses(sandbox)
+ gatewayPid ??= gateway!.pid
+ expect(gateway!.pid, `launch ${launch} attaches to the gateway launch 1 started`).toBe(gatewayPid)
+
if (launch === 1) {
provider.script(U(1), [{ text: [`${A(1)} `, 'persisted ', 'across ', 'launches'] }])
await send(page, `${U(1)} remember me`, 'Enter', ws)
@@ -252,13 +278,26 @@ test('relaunching the same home: one backend per boot, zero after each quit, tra
await app.close()
live = null
await expect
- .poll(() => sandboxProcesses(sandbox).map(p => `${p.pid} ${p.cmdline.slice(0, 120)}`), {
- timeout: 60_000,
- message: `no sandbox process survives quit #${launch}`
- })
+ .poll(
+ () =>
+ sandboxProcesses(sandbox)
+ .filter(p => /electron/i.test(p.cmdline.split(' ')[0]))
+ .map(p => `${p.pid} ${p.cmdline.slice(0, 120)}`),
+ { timeout: 60_000, message: `no Electron process survives quit #${launch}` }
+ )
.toEqual([])
+ expect(backendProcesses(sandbox).map(p => p.pid), `gateway outlives quit #${launch}`).toEqual([gatewayPid])
})
}
+
+ const stopped = stopSandboxGateway(sandbox)
+ expect(stopped.code, stopped.output).toBe(0)
+ await expect
+ .poll(() => sandboxProcesses(sandbox).map(p => `${p.pid} ${p.cmdline.slice(0, 120)}`), {
+ timeout: 60_000,
+ message: 'no sandbox process survives hermes gateway stop'
+ })
+ .toEqual([])
} finally {
if (live) {
await (live as Awaited<ReturnType<typeof launchCoreApp>>).app.close().catch(() => undefined)
diff --git a/apps/desktop/e2e/core/harness.ts b/apps/desktop/e2e/core/harness.ts
index 7f87a7c4f73..5d4ec91f94b 100644
--- a/apps/desktop/e2e/core/harness.ts
+++ b/apps/desktop/e2e/core/harness.ts
@@ -8,6 +8,7 @@
* fixed sleep.
*/
+import { spawnSync } from 'node:child_process'
import * as fs from 'node:fs'
import * as net from 'node:net'
import * as os from 'node:os'
@@ -268,6 +269,26 @@ export function backendProcesses(sandbox: CoreSandbox): ProcInfo[] {
return serve.filter(proc => !pids.has(proc.ppid))
}
+
+/**
+ * `hermes gateway stop` for the sandbox profile, run with the backend's own
+ * interpreter (argv[0] of the live `gateway run`). Attach-mode Desktop never
+ * owns the gateway, so this is how a test proves teardown works when asked.
+ */
+export function stopSandboxGateway(sandbox: CoreSandbox): { code: number | null; output: string } {
+ const [backend] = backendProcesses(sandbox)
+ const python = backend?.cmdline.split(' ')[0] || 'python3'
+
+ const result = spawnSync(python, ['-m', 'hermes_cli.main', 'gateway', 'stop'], {
+ cwd: REPO_ROOT,
+ env: coreAppEnv(sandbox),
+ encoding: 'utf8',
+ timeout: 90_000
+ })
+
+ return { code: result.status, output: `${result.stdout ?? ''}${result.stderr ?? ''}` }
+}
+
// ─── WebSocket recorder ─────────────────────────────────────────────────
export interface GatewayEventFrame {