From 4bbfdaf0b72c8c3ceca3a9d9abd4942297069759 Mon Sep 17 00:00:00 2001 From: joaomarcos Date: Sat, 5 Sep 2026 15:41:51 -0300 Subject: [PATCH] fix(desktop): recover a READY sentinel spliced into the merged output tail (#103792) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The spawn-time output tail feeds stdout AND stderr into ONE buffer, so its contents are not line-accurate. uvicorn logs to stderr in raw chunks, and a chunk that ends without a newline is concatenated directly onto the stdout sentinel that follows it: INFO Started server process [4711]HERMES_BACKEND_READY port=65238 The late-attach seed scan added for #60323 matched that buffer with the line-anchored `_READY_RE`, so `^` never lined up and the seed silently recovered nothing. The live stdout listener cannot cover the gap either: the sentinel was already consumed by the tail, and flowing-mode streams never replay chunks to late listeners. The wait then runs out its full 90s deadline and a healthy, listening backend is SIGTERMed — while desktop.log, which reads both streams, plainly shows the READY line. That contradiction (READY logged, boot timed out anyway) is the reported macOS regression. Scan the tail with a boundary-guarded pattern instead of a line-anchored one. `port=` keeps the token unambiguous, so prose naming the sentinel and longer identifiers ending in it still do not match. The live per-line scanner keeps `^`: individual stream chunks ARE line-accurate there, and loosening it would let unrelated child output settle the boot on a bogus port. Scope: the seed only. Watching stderr on the live path is #97086's change and is deliberately untouched here — the two compose. --- apps/desktop/electron/backend-ready.test.ts | 88 +++++++++++++++++++++ apps/desktop/electron/backend-ready.ts | 18 ++++- 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/apps/desktop/electron/backend-ready.test.ts b/apps/desktop/electron/backend-ready.test.ts index 7aa36648db..7255f6e8a5 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=` 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 133b08ff76..1adc8a4e59 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=` 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()