Skip to content

The agent that grows with you

test(desktop-e2e): Desktop attaches to a local gateway that was already running, on Windows

Tekniume268ccba2922
diff --git a/.github/workflows/windows-desktop-attach-e2e.yml b/.github/workflows/windows-desktop-attach-e2e.yml
new file mode 100644
index 00000000000..f03f2197e2d
--- /dev/null
+++ b/.github/workflows/windows-desktop-attach-e2e.yml
@@ -0,0 +1,106 @@
+name: Windows Desktop attach E2E
+
+# Desktop attaching to a local gateway on a real Windows host (#134889 W3):
+# apps/desktop/e2e/core/local-gateway-attach.spec.ts against a gateway that is
+# already running (`hermes gateway run`, as a login item or console leaves it),
+# driven through Electron main's real attach path (`hermes gateway ensure`, the
+# SID-checked named-pipe ticket mint through the runtime helper), first on the
+# dev tree, then on the electron-builder `--dir` build of the same commit.
+#
+# Dispatch-only plus pushes to wine2e-desktop/** (no other workflow listens
+# there), for iteration without the rest of CI.
+
+on:
+ workflow_dispatch:
+ push:
+ branches: ['wine2e-desktop/**']
+
+permissions:
+ contents: read
+
+concurrency:
+ group: windows-desktop-attach-e2e-${{ github.ref }}
+ cancel-in-progress: true
+
+jobs:
+ attach:
+ name: Desktop attach to a running local gateway (Windows)
+ runs-on: blacksmith-32vcpu-windows-2025
+ timeout-minutes: 60
+ steps:
+ - uses: useblacksmith/checkout@25227e61ff9dafe400e22fa487b673eac4e4409a # v1
+
+ - name: Prepare locked Desktop build and test toolchain
+ id: pm
+ uses: ./.github/actions/setup-pm
+ with:
+ toolchain: all
+ extras: '[]'
+ test-environment: 'true'
+
+ # Full npm ci (not --ignore-scripts): electron's postinstall downloads the binary we launch.
+ - uses: ./.github/actions/retry
+ with:
+ command: npm ci
+
+ - name: Build the desktop app
+ working-directory: apps/desktop
+ shell: bash
+ timeout-minutes: 15
+ run: npm run build
+
+ - name: Attach spec on the dev tree
+ id: dev
+ working-directory: apps/desktop
+ shell: bash
+ timeout-minutes: 15
+ run: npx playwright test -c e2e/core/playwright.config.ts local-gateway-attach.spec.ts
+ env:
+ CI: 'true'
+ HERMES_E2E_PYTHON: ${{ steps.pm.outputs.python-path }}
+ OPENROUTER_API_KEY: ''
+ OPENAI_API_KEY: ''
+ NOUS_API_KEY: ''
+ ANTHROPIC_API_KEY: ''
+
+ # The Windows app the way electron-builder lays it out (no msix, unsigned): the packaged
+ # main bundle, asar and preload, attaching through the same ensure + pipe-ticket path.
+ - name: Package the desktop app (electron-builder --dir)
+ if: always() && steps.dev.outcome != 'skipped'
+ working-directory: apps/desktop
+ shell: bash
+ timeout-minutes: 15
+ run: npm run builder -- --dir --publish never --win --x64
+
+ - name: Attach spec on the packaged build
+ if: always() && steps.dev.outcome != 'skipped'
+ working-directory: apps/desktop
+ shell: pwsh
+ timeout-minutes: 15
+ run: |
+ $exe = Get-ChildItem -LiteralPath release -Recurse -Filter '*.exe' |
+ Where-Object { $_.Directory.Name -like 'win*unpacked' -and $_.Name -notlike 'elevate*' -and $_.Name -notlike 'Uninstall*' } |
+ Select-Object -First 1
+ if (-not $exe) { Get-ChildItem -Recurse release | Select-Object -First 50 FullName; throw 'no packaged exe under release/win*-unpacked' }
+ "packaged exe: $($exe.FullName)"
+ $env:HERMES_E2E_PACKAGED_EXE = $exe.FullName
+ npx playwright test -c e2e/core/playwright.config.ts local-gateway-attach.spec.ts
+ if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
+ env:
+ CI: 'true'
+ HERMES_E2E_PYTHON: ${{ steps.pm.outputs.python-path }}
+ OPENROUTER_API_KEY: ''
+ OPENAI_API_KEY: ''
+ NOUS_API_KEY: ''
+ ANTHROPIC_API_KEY: ''
+
+ - name: Upload failure artifacts
+ if: failure() || cancelled()
+ uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
+ with:
+ name: windows-desktop-attach-e2e-${{ github.run_attempt }}
+ path: |
+ apps/desktop/test-results/core
+ apps/desktop/playwright-report/core
+ retention-days: 14
+ if-no-files-found: ignore
diff --git a/apps/desktop/e2e/core/local-gateway-attach.spec.ts b/apps/desktop/e2e/core/local-gateway-attach.spec.ts
new file mode 100644
index 00000000000..4d5526e6245
--- /dev/null
+++ b/apps/desktop/e2e/core/local-gateway-attach.spec.ts
@@ -0,0 +1,227 @@
+/**
+ * Desktop attaching to a local gateway that was already running (#134889 W3).
+ *
+ * The shape every Windows login item, `hermes gateway run` console and CLI
+ * session leaves behind: the profile's gateway is up BEFORE the Desktop opens.
+ * Desktop must attach to that process through the real main-process path
+ * (electron/local-gateway.ts `hermes gateway ensure`, then the ticket mint:
+ * the SID-checked named pipe through the installed runtime's helper on
+ * Windows, the private control socket elsewhere), never start a second one,
+ * and serve a turn on it. Quitting leaves the gateway it did not start
+ * running; a relaunch attaches to the same process again.
+ *
+ * Runs on every OS. The Windows Desktop lane (.github/workflows/
+ * windows-desktop-attach-e2e.yml) runs it on a real Windows runner against the
+ * dev tree and, with HERMES_E2E_PACKAGED_EXE, against the electron-builder
+ * `--dir` build of the same commit.
+ */
+
+import { spawn } from 'node:child_process'
+import * as fs from 'node:fs'
+import * as path from 'node:path'
+
+import { _electron, type ElectronApplication, expect, type Page, test } from '@playwright/test'
+
+import {
+ appLogTail,
+ backendProcesses,
+ coreAppEnv,
+ type CoreSandbox,
+ createCoreSandbox,
+ currentSessionId,
+ launchCoreApp,
+ recordWebSockets,
+ REPO_ROOT,
+ sandboxProcesses,
+ send,
+ stopSandboxGateway,
+ storedSessionForMarker,
+ waitForInteractive,
+ writeProviderHome
+} from './harness'
+import { assertTranscriptOracle, installDuplicateSampler } from './oracle'
+import { startScriptedProvider } from './provider'
+
+const nonce = Math.random().toString(36).slice(2, 8).replace(/[^a-z0-9]/g, 'x').padEnd(4, 'q')
+const U = (n: number) => `U${n}-attach${nonce}`
+const A = (n: number) => `A${n}-attach${nonce}`
+
+const PYTHON = process.env.HERMES_E2E_PYTHON || (process.platform === 'win32' ? 'python' : 'python3')
+const PACKAGED_EXE = process.env.HERMES_E2E_PACKAGED_EXE || ''
+
+function gatewayState(sandbox: CoreSandbox): Record<string, any> {
+ try {
+ return JSON.parse(fs.readFileSync(path.join(sandbox.hermesHome, 'gateway_state.json'), 'utf8').replace(/^\uFEFF/, ''))
+ } catch {
+ return {}
+ }
+}
+
+function alive(pid: number): boolean {
+ try {
+ process.kill(pid, 0)
+
+ return true
+ } catch (error) {
+ return (error as NodeJS.ErrnoException).code === 'EPERM'
+ }
+}
+
+/** `hermes gateway run`, detached and windowless, as a login item / console starts it. */
+async function startGatewayFirst(sandbox: CoreSandbox): Promise<{ pid: number; log: string }> {
+ const log = path.join(sandbox.root, 'gateway-run.log')
+ const out = fs.openSync(log, 'a')
+
+ const child = spawn(PYTHON, ['-m', 'hermes_cli.main', 'gateway', 'run'], {
+ cwd: REPO_ROOT,
+ env: coreAppEnv(sandbox),
+ detached: true,
+ windowsHide: true,
+ stdio: ['ignore', out, out]
+ })
+
+ child.unref()
+ fs.closeSync(out)
+
+ await expect
+ .poll(
+ () => {
+ const state = gatewayState(sandbox)
+
+ return state.gateway_state === 'running' && Number(state.pid) > 0 && alive(Number(state.pid))
+ },
+ {
+ timeout: 180_000,
+ intervals: [500],
+ message: `the pre-started gateway reports running\n${fs.existsSync(log) ? fs.readFileSync(log, 'utf8').slice(-4000) : ''}`
+ }
+ )
+ .toBe(true)
+
+ return { pid: Number(gatewayState(sandbox).pid), log }
+}
+
+async function launchApp(sandbox: CoreSandbox): Promise<{ app: ElectronApplication; page: Page }> {
+ if (!PACKAGED_EXE) {
+ return launchCoreApp(coreAppEnv(sandbox))
+ }
+
+ const app = await _electron.launch({
+ executablePath: PACKAGED_EXE,
+ args: ['--disable-gpu', '--no-sandbox'],
+ env: coreAppEnv(sandbox),
+ cwd: sandbox.root
+ })
+
+ return { app, page: await app.firstWindow() }
+}
+
+test(`Desktop${PACKAGED_EXE ? ' (packaged)' : ''} attaches to the gateway already running and serves a turn`, async () => {
+ const provider = await startScriptedProvider()
+ const sandbox = createCoreSandbox('attach')
+ writeProviderHome(sandbox.hermesHome, provider.url)
+ let live: null | { app: ElectronApplication; page: Page } = null
+
+ try {
+ const gateway = await startGatewayFirst(sandbox)
+ expect(backendProcesses(sandbox).map(p => p.pid), 'only the pre-started gateway runs before Desktop opens').toEqual([
+ gateway.pid
+ ])
+
+ let sessionId = ''
+
+ for (let launch = 1; launch <= 2; launch++) {
+ await test.step(`launch ${launch}: attach to pid ${gateway.pid}`, async () => {
+ live = await launchApp(sandbox)
+ const { app, page } = live
+
+ if (PACKAGED_EXE) {
+ const facts = await app.evaluate(({ app: a }) => ({ isPackaged: a.isPackaged }))
+ expect(facts.isPackaged, 'launched the packaged build').toBe(true)
+ }
+
+ const ws = recordWebSockets(page)
+ await waitForInteractive(app, page)
+ await installDuplicateSampler(page)
+
+ // Main's descriptor names the very gateway that was running first.
+ const descriptor = await page.evaluate(() => (window as any).hermesDesktop.getConnection())
+ const state = gatewayState(sandbox)
+ expect(descriptor?.mode, `connection descriptor: ${JSON.stringify(descriptor)}`).toBe('local')
+ expect(String(descriptor?.gatewayEndpoint?.api_origin ?? descriptor?.baseUrl ?? '')).toMatch(
+ /^http:\/\/(127\.0\.0\.1|\[::1\]):\d+\/?$/
+ )
+ expect(Number(state.pid), 'gateway_state.json still names the pre-started gateway').toBe(gateway.pid)
+ expect(backendProcesses(sandbox).map(p => p.pid), `launch ${launch} attached, never spawned a second gateway`).toEqual([
+ gateway.pid
+ ])
+
+ if (launch === 1) {
+ provider.script(U(1), [{ text: [`${A(1)} `, 'served ', 'by ', 'the ', 'running ', 'gateway'] }])
+ await send(page, `${U(1)} hello`, 'Enter', ws)
+ await expect
+ .poll(() => provider.completions.some(c => c.marker === U(1) && c.finished), {
+ timeout: 120_000,
+ message: `provider finished ${U(1)}\n${appLogTail(app)}`
+ })
+ .toBe(true)
+ await expect.poll(() => currentSessionId(page)).not.toBe('')
+ sessionId = await currentSessionId(page)
+ await expect
+ .poll(() => storedSessionForMarker(sandbox, 'default', U(1)), { message: 'turn persisted by the gateway' })
+ .toBe(sessionId)
+ } else {
+ await page.evaluate(id => {
+ window.location.hash = `#/${encodeURIComponent(id)}`
+ }, sessionId)
+ provider.script(U(2), [{ text: [`${A(2)} `, 'after ', 'relaunch'] }])
+ await send(page, `${U(2)} again`, 'Enter', ws)
+ await expect
+ .poll(() => provider.completions.some(c => c.marker === U(2) && c.finished), {
+ timeout: 120_000,
+ message: `provider finished ${U(2)}\n${appLogTail(app)}`
+ })
+ .toBe(true)
+ }
+
+ await assertTranscriptOracle(
+ page,
+ ws,
+ provider,
+ { sessionId, expectUserMarkers: launch === 1 ? [U(1)] : [U(1), U(2)] },
+ `attach launch ${launch}`
+ )
+
+ const electronPid = app.process().pid!
+ await app.close()
+ live = null
+ await expect
+ .poll(() => alive(electronPid), { timeout: 60_000, message: `Electron main (pid ${electronPid}) exits on quit #${launch}` })
+ .toBe(false)
+ expect(alive(gateway.pid), `the gateway Desktop attached to outlives quit #${launch}`).toBe(true)
+ expect(backendProcesses(sandbox).map(p => p.pid)).toEqual([gateway.pid])
+ })
+ }
+
+ const stopped = stopSandboxGateway(sandbox)
+ expect(stopped.code, stopped.output).toBe(0)
+ await expect
+ .poll(() => alive(gateway.pid), { timeout: 90_000, message: 'hermes gateway stop ends the gateway' })
+ .toBe(false)
+ } finally {
+ if (live) {
+ await (live as { app: ElectronApplication }).app.close().catch(() => undefined)
+ }
+
+ for (const proc of sandboxProcesses(sandbox)) {
+ try {
+ process.kill(proc.pid, 'SIGKILL')
+ } catch {
+ /* already gone */
+ }
+ }
+
+ await provider.close()
+ sandbox.cleanup()
+ }
+})