Context
Split from #18939 on 2026-09-19. #18939 keeps the query_component dotted-path defect. This ticket takes its second half, whose acceptance criteria turned out to be jointly unsatisfiable as filed (measurement below).
The Problem
An app that boots while no Neural Link bridge listens never attaches to one started later. Neo.ai.Client's socket (Neo.data.connection.WebSocket, maxReconnectAttempts = 5) gives up, Client#onSocketReconnectFailed warns "reconnecting stopped", and nothing dials again. Observed 2026-09-18: the Portal booted with no bridge running. After the bridge started, healthcheck showed sessions: [] until the tab was reloaded.
Why "a silent slow poll" cannot be the fix — measured 2026-09-19. In headless Chromium, a dedicated worker dialled a closed port three times with onerror handled. Each failed dial produced a browser-level DevTools error (CDP Log.entryAdded, source worker):
WebSocket connection to 'ws://127.0.0.1:9/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
JavaScript saw nothing (no console API call), and cannot suppress the line. So any background poll prints one error per attempt in the DevTools of every developer running an app without the bridge. The public Portal is unaffected (worker.App skips the client when isGitHubPages), so the audience is local dev and dist runs.
The Architectural Reality
src/ai/Client.mjs#onSocketReconnectFailed; src/data/connection/WebSocket.mjs (maxReconnectAttempts, backoffStrategy).
src/main/DomEvents.mjs#onVisibilitychange already posts {action: 'visibilityChange'} to the App Worker. Its receiver, Neo.worker.App#onVisibilityChange, fires visibilitychange on that window's app only, so a worker-wide singleton like the client cannot hear it. Window focus is not forwarded at all.
src/worker/App.mjs loads the client only when useAiClient matches the environment.
src/worker/Base.mjs forwards every App Worker console call to the bridge while client.isConnected is true.
The Fix — a fork, decided: A
| option |
late-bridge attach |
output with no bridge |
| A. re-dial on visibility or focus gain (chosen) |
on the next tab switch or window focus. The observed case is covered in practice, because the operator's click from the terminal back into the browser fires focus |
none while nobody returns to the window. Each return costs one refused dial, so one DevTools line |
| B. capped slow poll while visible |
within one poll interval, no gesture |
one DevTools error per attempt |
| C. both |
fastest |
B's lines |
Correction to the first recommendation. It said A "costs nothing while idle", and that B's cost falls on every developer who never uses the Neural Link. A's cost falls on those developers too, once per return to the window, and focus fires often: each click from DevTools back into the page is one. So returns are gated. The client dials only after its socket gave up, one dial at a time, and at most once per backoff step since the last failure. With the default strategy that is 16 s after the boot give-up and 30 s after each refused re-dial. A refused re-dial prints nothing through the console API.
Found while implementing: losing a connected bridge floods the console, measured on dev@ad44280df2. Client#onSocketClose logged "Disconnected" while isConnected was still true. The worker forwarded that line to the bridge, and a send on the closed socket starts a reconnect. That reconnect logs its attempt, which is forwarded again. Once the attempts run out, the give-up warning is forwarded the same way, and the loop runs until the stack overflows. Stopping a connected bridge produced 527 "reconnecting stopped" warnings within 3 s, plus Duplicate event handler attached: open Socket. Before this ticket that only happened when a connected bridge was lost. Once a late bridge can attach, every attach can end that way, so it belongs here. The first fix dropped the flag in onSocketClose before logging. Review of PR #18970 found the second entry: a failing socket reports error before close, and the error's own warning took the same path (537 attempts, 527 reports and a stack overflow, triggered by an invalid UTF-8 frame). So isConnected stopped being a flag. It reads the socket's readyState, and no line is ever sent through a socket that is not open, in whatever order the socket fails.
Contract Ledger
"M" names the PR #18970 mutation row whose arm fails when that behaviour is undone.
| Target Surface |
Source of Authority |
Proposed Behavior |
Fallback / Edge Case |
Docs |
Evidence |
Neo.main.DomEvents#onWindowFocus (new) |
option A |
on window focus, posts {action: 'windowFocus', data: {windowId}} to the App Worker, in every Neo app |
the listener is on window without capture, and focus does not bubble, so an element gaining focus inside the page does not fire it |
JSDoc |
M6 |
Neo.worker.App#onWindowFocus (new) |
option A |
fires the worker-level event windowFocus with {windowId} |
no app-level event |
JSDoc |
M6 |
Neo.worker.App#onVisibilityChange |
option A |
unchanged: hiddenTick.sync(data), then the app-level visibilitychange with {hidden, visibilityState, windowId}. New: the worker-level visibilityChange with the same payload |
hidden reports are fired too; the client ignores hidden: true |
JSDoc |
M7 |
Neo.ai.Client#redial (new, public) |
option A + the gate above |
dials only if all three hold: the socket gave up (gaveUpAt set), its socket is CLOSED (no dial in flight), and at least socket.backoffStrategy(socket.reconnectAttempts) ms passed since the last failure |
a return inside the step does nothing and schedules nothing: no later retry, only the next return after the step dials. Default gap: 16 s after the boot give-up, 30 s after each refused re-dial; a custom backoffStrategy sets it |
JSDoc |
M1 · M2 · M9 |
| client output after the give-up |
the onSocketError / onSocketReconnectFailed docblocks |
the first give-up warns once, "reconnecting stopped until a window is focused or shown again". A refused re-dial prints nothing through the console API |
the browser still writes one DevTools line per refused dial, which no script can suppress |
JSDoc |
M3 · M4b · M5 |
Neo.ai.Client#isConnected (now a getter) |
the flood above |
true only while the socket's readyState is OPEN, so the worker's console forwarding and the client's own sends never reach a socket that is not open |
covers error before close, close alone and a closing handshake alike. Nothing outside the client writes it |
JSDoc |
M4 · M4-dev |
Neo.ai.Client#onSocketClose |
— |
logs "Disconnected" only when the socket had opened, which a private flag remembers |
a refused dial disconnects nothing |
JSDoc |
M4b |
Neo.ai.Client#onSocketOpen |
— |
clears gaveUpAt, so a bridge lost later is reported again and its own reconnect cycle runs first. It logs "Connected" after flushing the buffered lines, which keeps the bridge's log in order |
— |
JSDoc |
M8 · M10 |
Neo.data.connection.WebSocket#onClose |
— |
no longer warns "closed abnormally, attempting reconnect…"; attemptReconnect() logs each attempt it schedules and, after a give-up, schedules none |
pre-existing and out of scope: onClose(event, reason, wasClean) never receives reason or wasClean, so a clean 1000 close reconnects too (defect-note 2026-09-19 14:18Z, corrected 14:18Z) |
inline comment |
M5 |
Unchanged: the app-level visibilitychange name and payload. Neo.Main.windowFocus, the main-thread remote that focuses a window, is unrelated to the new App Worker action of the same name.
Acceptance Criteria
Out of Scope
- #18939's
query_component dotted paths.
- Brain-side Neural Link surfaces (neomjs/neo-agent-brain#371).
Related
#18939 (split from) · D#18940 (the action layer this client carries)
unowned-rationale: the design fork is recorded with a recommendation and one measurement; one PR; claimable. The reporter holds #253, #18939 and #18955.
Live latest-open sweep: checked the latest 20 open issues at 2026-09-19T13:05Z; searches for ai.Client reconnect and late bridge found only #18939 (its source).
Origin Session ID: 6ecb7b5f-dc26-48a3-8e49-7232159377c1
Retrieval Hint: "ai.Client reconnecting stopped late bridge attach visibility focus re-dial DevTools WebSocket error"
Context
Split from #18939 on 2026-09-19. #18939 keeps the
query_componentdotted-path defect. This ticket takes its second half, whose acceptance criteria turned out to be jointly unsatisfiable as filed (measurement below).The Problem
An app that boots while no Neural Link bridge listens never attaches to one started later.
Neo.ai.Client's socket (Neo.data.connection.WebSocket,maxReconnectAttempts = 5) gives up,Client#onSocketReconnectFailedwarns "reconnecting stopped", and nothing dials again. Observed 2026-09-18: the Portal booted with no bridge running. After the bridge started,healthcheckshowedsessions: []until the tab was reloaded.Why "a silent slow poll" cannot be the fix — measured 2026-09-19. In headless Chromium, a dedicated worker dialled a closed port three times with
onerrorhandled. Each failed dial produced a browser-level DevTools error (CDPLog.entryAdded, sourceworker):JavaScript saw nothing (no console API call), and cannot suppress the line. So any background poll prints one error per attempt in the DevTools of every developer running an app without the bridge. The public Portal is unaffected (
worker.Appskips the client whenisGitHubPages), so the audience is local dev and dist runs.The Architectural Reality
src/ai/Client.mjs#onSocketReconnectFailed;src/data/connection/WebSocket.mjs(maxReconnectAttempts,backoffStrategy).src/main/DomEvents.mjs#onVisibilitychangealready posts{action: 'visibilityChange'}to the App Worker. Its receiver,Neo.worker.App#onVisibilityChange, firesvisibilitychangeon that window's app only, so a worker-wide singleton like the client cannot hear it. Windowfocusis not forwarded at all.src/worker/App.mjsloads the client only whenuseAiClientmatches the environment.src/worker/Base.mjsforwards every App Worker console call to the bridge whileclient.isConnectedis true.The Fix — a fork, decided: A
focusCorrection to the first recommendation. It said A "costs nothing while idle", and that B's cost falls on every developer who never uses the Neural Link. A's cost falls on those developers too, once per return to the window, and
focusfires often: each click from DevTools back into the page is one. So returns are gated. The client dials only after its socket gave up, one dial at a time, and at most once per backoff step since the last failure. With the default strategy that is 16 s after the boot give-up and 30 s after each refused re-dial. A refused re-dial prints nothing through the console API.Found while implementing: losing a connected bridge floods the console, measured on
dev@ad44280df2.Client#onSocketCloselogged "Disconnected" whileisConnectedwas still true. The worker forwarded that line to the bridge, and a send on the closed socket starts a reconnect. That reconnect logs its attempt, which is forwarded again. Once the attempts run out, the give-up warning is forwarded the same way, and the loop runs until the stack overflows. Stopping a connected bridge produced 527 "reconnecting stopped" warnings within 3 s, plusDuplicate event handler attached: open Socket. Before this ticket that only happened when a connected bridge was lost. Once a late bridge can attach, every attach can end that way, so it belongs here. The first fix dropped the flag inonSocketClosebefore logging. Review of PR #18970 found the second entry: a failing socket reportserrorbeforeclose, and the error's own warning took the same path (537 attempts, 527 reports and a stack overflow, triggered by an invalid UTF-8 frame). SoisConnectedstopped being a flag. It reads the socket'sreadyState, and no line is ever sent through a socket that is not open, in whatever order the socket fails.Contract Ledger
"M" names the PR #18970 mutation row whose arm fails when that behaviour is undone.
Neo.main.DomEvents#onWindowFocus(new)focus, posts{action: 'windowFocus', data: {windowId}}to the App Worker, in every Neo appwindowwithout capture, andfocusdoes not bubble, so an element gaining focus inside the page does not fire itNeo.worker.App#onWindowFocus(new)windowFocuswith{windowId}Neo.worker.App#onVisibilityChangehiddenTick.sync(data), then the app-levelvisibilitychangewith{hidden, visibilityState, windowId}. New: the worker-levelvisibilityChangewith the same payloadhidden: trueNeo.ai.Client#redial(new, public)gaveUpAtset), its socket isCLOSED(no dial in flight), and at leastsocket.backoffStrategy(socket.reconnectAttempts)ms passed since the last failurebackoffStrategysets itonSocketError/onSocketReconnectFaileddocblocksNeo.ai.Client#isConnected(now a getter)readyStateisOPEN, so the worker's console forwarding and the client's own sends never reach a socket that is not openerrorbeforeclose,closealone and a closing handshake alike. Nothing outside the client writes itNeo.ai.Client#onSocketCloseNeo.ai.Client#onSocketOpengaveUpAt, so a bridge lost later is reported again and its own reconnect cycle runs first. It logs "Connected" after flushing the buffered lines, which keeps the bridge's log in orderNeo.data.connection.WebSocket#onCloseattemptReconnect()logs each attempt it schedules and, after a give-up, schedules noneonClose(event, reason, wasClean)never receivesreasonorwasClean, so a clean 1000 close reconnects too (defect-note 2026-09-19 14:18Z, corrected 14:18Z)Unchanged: the app-level
visibilitychangename and payload.Neo.Main.windowFocus, the main-thread remote that focuses a window, is unrelated to the new App Worker action of the same name.Acceptance Criteria
Log.entryAdded, not only the console API.Out of Scope
query_componentdotted paths.Related
#18939 (split from) · D#18940 (the action layer this client carries)
unowned-rationale: the design fork is recorded with a recommendation and one measurement; one PR; claimable. The reporter holds #253, #18939 and #18955.
Live latest-open sweep: checked the latest 20 open issues at 2026-09-19T13:05Z; searches for
ai.Client reconnectandlate bridgefound only #18939 (its source).Origin Session ID: 6ecb7b5f-dc26-48a3-8e49-7232159377c1 Retrieval Hint: "ai.Client reconnecting stopped late bridge attach visibility focus re-dial DevTools WebSocket error"