mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-06-24 10:52:21 +00:00
The port-announcement clock in waitForDashboardPort starts the instant the backend process is spawned — before uvicorn binds its socket. On a cold install the child first compiles and imports the whole hermes_cli.main -> web_server -> FastAPI/uvicorn chain, and on Windows real-time AV scans every freshly written .pyc. That pre-bind cost can exceed the old hardcoded 45s deadline, so the desktop killed a healthy-but-still-starting backend and respawned it, piling up orphaned processes (#50209). Raise the default to 90s and make it overridable via HERMES_DESKTOP_PORT_ANNOUNCE_TIMEOUT_MS, clamped to a 45s floor so a bad override can't reintroduce the loop. Warm starts still announce in well under a second; both call sites inherit the new default with no change. Adds backend-ready.test.cjs (wired into test:desktop:platforms).
121 lines
4.5 KiB
JavaScript
121 lines
4.5 KiB
JavaScript
/**
|
|
* Tests for electron/backend-ready.cjs.
|
|
*
|
|
* Run with: node --test electron/backend-ready.test.cjs
|
|
* (Wired into npm test:desktop:platforms in package.json.)
|
|
*
|
|
* Covers the cold-start port-announcement deadline (issue #50209): the clock
|
|
* starts before the backend binds its port, so a tight 45s deadline killed a
|
|
* healthy-but-still-compiling backend on cold Windows installs. The default is
|
|
* now cold-start tolerant and overridable via
|
|
* HERMES_DESKTOP_PORT_ANNOUNCE_TIMEOUT_MS, clamped to a 45s floor.
|
|
*/
|
|
|
|
const test = require('node:test')
|
|
const assert = require('node:assert/strict')
|
|
const { EventEmitter } = require('node:events')
|
|
|
|
const {
|
|
waitForDashboardPort,
|
|
resolvePortAnnounceTimeoutMs,
|
|
DEFAULT_PORT_ANNOUNCE_TIMEOUT_MS,
|
|
MIN_PORT_ANNOUNCE_TIMEOUT_MS,
|
|
} = require('./backend-ready.cjs')
|
|
|
|
// A minimal stand-in for a spawned child process: an EventEmitter with a
|
|
// stdout EventEmitter, matching the surface waitForDashboardPort consumes
|
|
// (child.stdout.on('data'), child.on('exit'|'error') + the .off() teardown).
|
|
function makeFakeChild() {
|
|
const child = new EventEmitter()
|
|
child.stdout = new EventEmitter()
|
|
return child
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// resolvePortAnnounceTimeoutMs
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test('default is cold-start tolerant (> the historical 45s floor)', () => {
|
|
assert.equal(resolvePortAnnounceTimeoutMs({}), DEFAULT_PORT_ANNOUNCE_TIMEOUT_MS)
|
|
assert.ok(
|
|
DEFAULT_PORT_ANNOUNCE_TIMEOUT_MS > MIN_PORT_ANNOUNCE_TIMEOUT_MS,
|
|
'cold-start default must exceed the warm-start floor'
|
|
)
|
|
})
|
|
|
|
test('honors a valid HERMES_DESKTOP_PORT_ANNOUNCE_TIMEOUT_MS override', () => {
|
|
const env = { HERMES_DESKTOP_PORT_ANNOUNCE_TIMEOUT_MS: '120000' }
|
|
assert.equal(resolvePortAnnounceTimeoutMs(env), 120_000)
|
|
})
|
|
|
|
test('clamps an override below the floor up to the 45s minimum', () => {
|
|
const env = { HERMES_DESKTOP_PORT_ANNOUNCE_TIMEOUT_MS: '1000' }
|
|
assert.equal(resolvePortAnnounceTimeoutMs(env), MIN_PORT_ANNOUNCE_TIMEOUT_MS)
|
|
})
|
|
|
|
test('rounds a fractional override', () => {
|
|
const env = { HERMES_DESKTOP_PORT_ANNOUNCE_TIMEOUT_MS: '60000.7' }
|
|
assert.equal(resolvePortAnnounceTimeoutMs(env), 60_001)
|
|
})
|
|
|
|
test('falls back to the default for malformed / non-positive overrides', () => {
|
|
for (const bad of ['', 'abc', '0', '-5', 'NaN', undefined]) {
|
|
const env = bad === undefined ? {} : { HERMES_DESKTOP_PORT_ANNOUNCE_TIMEOUT_MS: bad }
|
|
assert.equal(
|
|
resolvePortAnnounceTimeoutMs(env),
|
|
DEFAULT_PORT_ANNOUNCE_TIMEOUT_MS,
|
|
`override ${JSON.stringify(bad)} should fall through to the default`
|
|
)
|
|
}
|
|
})
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// waitForDashboardPort
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test('resolves with the announced port', async () => {
|
|
const child = makeFakeChild()
|
|
const p = waitForDashboardPort(child, 1000)
|
|
child.stdout.emit('data', 'noise before\nHERMES_DASHBOARD_READY port=54321\n')
|
|
assert.equal(await p, 54321)
|
|
})
|
|
|
|
test('parses the port even when the line arrives split across chunks', async () => {
|
|
const child = makeFakeChild()
|
|
const p = waitForDashboardPort(child, 1000)
|
|
child.stdout.emit('data', 'HERMES_DASHBOARD_READY po')
|
|
child.stdout.emit('data', 'rt=8080\n')
|
|
assert.equal(await p, 8080)
|
|
})
|
|
|
|
test('rejects when the child exits before announcing', async () => {
|
|
const child = makeFakeChild()
|
|
const p = waitForDashboardPort(child, 1000)
|
|
child.emit('exit', 1, null)
|
|
await assert.rejects(p, /exited before port announcement/)
|
|
})
|
|
|
|
test('rejects on a child error event', async () => {
|
|
const child = makeFakeChild()
|
|
const p = waitForDashboardPort(child, 1000)
|
|
child.emit('error', new Error('spawn ENOENT'))
|
|
await assert.rejects(p, /spawn ENOENT/)
|
|
})
|
|
|
|
test('rejects with the timeout message after the deadline', async () => {
|
|
const child = makeFakeChild()
|
|
await assert.rejects(
|
|
waitForDashboardPort(child, 20),
|
|
/Timed out waiting for Hermes backend port announcement \(20ms\)/
|
|
)
|
|
})
|
|
|
|
test('a late announcement after timeout does not throw (listeners torn down)', async () => {
|
|
const child = makeFakeChild()
|
|
await assert.rejects(waitForDashboardPort(child, 20), /Timed out/)
|
|
// The orphaned backend may still print its READY line later; the watcher
|
|
// must have detached so this emit is a no-op rather than a double-settle.
|
|
assert.doesNotThrow(() => {
|
|
child.stdout.emit('data', 'HERMES_DASHBOARD_READY port=9999\n')
|
|
})
|
|
})
|