Skip to content

The agent that grows with you

fix(desktop): recover a READY sentinel spliced into the merged output tail (#103792)

joaomarcos4bbfdaf0b72c
diff --git a/apps/desktop/electron/backend-ready.test.ts b/apps/desktop/electron/backend-ready.test.ts
index 7aa36648db1..7255f6e8a5a 100644
--- a/apps/desktop/electron/backend-ready.test.ts
+++ b/apps/desktop/electron/backend-ready.test.ts
@@ -319,3 +319,91 @@ test('bufferedOutput without a sentinel still times out (no false positive)', as
await assert.rejects(wait, /Timed out waiting/)
})
+
+// ---------------------------------------------------------------------------
+// merged-tail seed (#103792): the spawn-time tail holds stdout AND stderr in
+// ONE buffer, so it is not line-accurate. uvicorn logs to stderr in raw
+// chunks, and a chunk that ends without a newline is concatenated straight
+// onto the stdout sentinel that follows it. A `^`-anchored scan then recovers
+// nothing from a backend that announced perfectly, the wait hits its 90s
+// deadline, and a healthy listening backend is killed — while desktop.log
+// (which reads both streams) plainly shows the READY line.
+// ---------------------------------------------------------------------------
+
+test('resolves when a partial stderr line is spliced onto the sentinel in the merged tail (#103792)', async () => {
+ const child = makeFakeChild()
+
+ // No trailing newline on the stderr chunk, exactly as uvicorn emits it.
+ const mergedTail = 'INFO Started server process [4711]HERMES_BACKEND_READY port=65238\n'
+
+ const port = await waitForDashboardPortAnnouncement(child, {
+ bufferedOutput: () => mergedTail,
+ timeoutMs: 500
+ })
+
+ assert.equal(port, 65238)
+})
+
+test('splice recovery also covers the legacy HERMES_DASHBOARD_READY sentinel', async () => {
+ const child = makeFakeChild()
+
+ const port = await waitForDashboardPortAnnouncement(child, {
+ bufferedOutput: () => 'INFO Application startup complete.HERMES_DASHBOARD_READY port=65239\n',
+ timeoutMs: 500
+ })
+
+ assert.equal(port, 65239)
+})
+
+test('a spliced sentinel with no trailing newline is still recovered from the tail', async () => {
+ const child = makeFakeChild()
+
+ // The tail is a snapshot: the sentinel's own newline may not have been
+ // flushed into it yet. Line-splitting would drop it; the tail scan must not.
+ const port = await waitForDashboardPortAnnouncement(child, {
+ bufferedOutput: () => 'waiting for application startupHERMES_BACKEND_READY port=65240',
+ timeoutMs: 500
+ })
+
+ assert.equal(port, 65240)
+})
+
+test('the merged-tail scan does not match prose that merely names the sentinel', async () => {
+ const child = makeFakeChild()
+
+ // `port=<digits>` is what makes the token unambiguous. Without it, a log
+ // line discussing the sentinel must never be mistaken for an announcement.
+ const wait = waitForDashboardPort(
+ child,
+ 50,
+ () => '',
+ () => 'still waiting for HERMES_BACKEND_READY from the backend\n'
+ )
+
+ await assert.rejects(wait, /Timed out waiting/)
+})
+
+test('the merged-tail scan does not match a longer token ending in the sentinel', async () => {
+ const child = makeFakeChild()
+
+ const wait = waitForDashboardPort(
+ child,
+ 50,
+ () => '',
+ () => 'X_HERMES_BACKEND_READY port=1234\n'
+ )
+
+ await assert.rejects(wait, /Timed out waiting/)
+})
+
+test('the LIVE per-line scanner stays line-anchored (splice tolerance is tail-only)', async () => {
+ const child = makeFakeChild()
+
+ const wait = waitForDashboardPort(child, 60)
+ // Live stdout chunks ARE line-accurate for this stream, so a sentinel that
+ // is a suffix of some other line is not an announcement. Loosening this
+ // would let unrelated child output settle the boot on a bogus port.
+ child.stdout.emit('data', 'downloading HERMES_BACKEND_READY port=1234\n')
+
+ await assert.rejects(wait, /Timed out waiting/)
+})
diff --git a/apps/desktop/electron/backend-ready.ts b/apps/desktop/electron/backend-ready.ts
index 133b08ff764..1adc8a4e59b 100644
--- a/apps/desktop/electron/backend-ready.ts
+++ b/apps/desktop/electron/backend-ready.ts
@@ -5,6 +5,20 @@ import fs from 'node:fs'
// works against both the headless backend and old/dashboard runtimes.
const _READY_RE = /^HERMES_(?:BACKEND|DASHBOARD)_READY port=(\d+)/m
+// Same sentinel, matched inside the spawn-time output tail rather than inside
+// a single line. The tail merges stdout AND stderr into one buffer, so it is
+// NOT line-accurate: uvicorn's stderr startup logging is appended in raw
+// chunks, and a chunk that ends without a newline is concatenated directly
+// onto the stdout sentinel that follows it, producing
+// `INFO Started server process [4711]HERMES_BACKEND_READY port=65238`.
+// A `^`-anchored match then fails on a backend that announced perfectly: the
+// seed below silently recovers nothing, the wait hits its 90s deadline, and a
+// healthy, listening backend is killed while desktop.log plainly shows the
+// READY line (#103792). Guard on a token boundary instead of a line start.
+// `port=<digits>` keeps the token unambiguous, so prose that merely mentions
+// the sentinel cannot match.
+const _READY_IN_MERGED_TAIL_RE = /(?:^|[^0-9A-Z_])HERMES_(?:BACKEND|DASHBOARD)_READY port=(\d+)/
+
// The announcement clock starts the instant the backend process is spawned —
// before uvicorn binds its socket. On a cold install the child must first
// compile and import the whole `hermes_cli.main` → `web_server` → FastAPI/
@@ -121,9 +135,11 @@ function waitForDashboardPort(
// Listener is live — now recover a sentinel that was already flushed and
// consumed before this promise existed. The snapshot is taken AFTER the
// listener attaches, so no chunk can fall between snapshot and listener.
+ // Scanned with the merged-tail regex, not the line-anchored one: the tail
+ // interleaves both streams and can splice a partial line onto the sentinel.
if (!done) {
const alreadyBuffered = bufferedOutput()
- const m = alreadyBuffered ? alreadyBuffered.match(_READY_RE) : null
+ const m = alreadyBuffered ? alreadyBuffered.match(_READY_IN_MERGED_TAIL_RE) : null
if (m) {
cleanup()