Context
Promoted from Ada's unowned close-event defect note after an independent native WebSocket reproduction on the current Socket source (unchanged between the reviewed source and dev 4802862b7347ca7c55ca41685bbefedab784d7af). #8012 defines the emitted close payload; #8177 defines automatic recovery from abnormal closure. The merged #18970 deliberately preserves the Neural Link client's ability to recover after a bridge goes away.
The Problem
beforeSetSocket binds onClose as the native socket's onclose. That callback receives one CloseEvent, but onClose(event, reason, wasClean) reads the other two arguments. Both are undefined, and !wasClean consequently reconnects even after a clean code-1000 close.
Independent probe (real loopback server, reconnect budget limited to one for observation):
| Native close |
Emitted reason / wasClean |
Reconnect attempts |
code 1000, reason job complete, wasClean true |
undefined / undefined |
1 |
| code 1006, reason empty, wasClean false |
undefined / undefined |
1 |
Correcting only the argument read would also silently remove clean-close recovery from the default Neural Link client. Its later window-return retry starts only after the socket exhausts a reconnect cycle.
The Architectural Reality
src/data/connection/WebSocket.mjs#beforeSetSocket owns native callback binding; #onClose owns the observable payload and automatic retry decision.
src/ai/Client.mjs#connect creates its socket through ClassSystem.beforeSetInstance; explicit object config overrides supplied defaults, while an existing instance retains its configuration.
Client#redial is exhaustion-driven. An explicit opt-out of clean-close recovery must not be silently undone by a focus event.
- Existing tests:
unit/data/connection/WebSocket.spec.mjs and e2e/core/AiClientLateBridge.spec.mjs.
The Fix
Read reason and wasClean from CloseEvent and preserve the observable payload shape. Keep automatic abnormal-close recovery. Add reconnectOnCleanClose: false to the generic socket; the default socket created by the Neural Link client opts in explicitly.
An explicit false or a caller-supplied socket keeps its own clean-close policy. In the client, a clean close with that policy disabled ends automatic recovery, including implicit focus/visibility re-dials; it does not fabricate an exhausted retry cycle. No second retry scheduler or duplicated close predicate belongs in the client.
Prescription checked: WebSocket#onClose owns payload/transport policy; Client#connect owns the client's default socket policy.
Contract Ledger
| Surface |
Authority |
Behavior |
Fallback / edge |
Docs |
Evidence |
Socket close event |
#8012, native callback binding |
{event, reason, wasClean} reads fields from the one event |
event reference retained |
method JSDoc |
callback-shaped unit arm + native probe |
reconnectOnCleanClose |
#8177's abnormal-close default; explicit consumer policy |
false by default; true includes clean code-1000 closure |
abnormal closure still retries |
config JSDoc |
default/opt-in/abnormal matrix |
| Client-created socket |
#18970 recovery contract |
defaults clean-close retry to true |
explicit config wins; supplied instance retained |
connect/socketConfig JSDoc |
clean bridge-close browser arm |
| Client explicit clean-close opt-out |
socket policy + exhaustion-driven redial |
clean close stays closed on window returns |
abnormal loss still uses existing cycle |
socketConfig JSDoc |
opt-out browser control |
Acceptance Criteria
Out of Scope
Changing the backoff budget, clean-close protocol codes, bridge server behavior, message queue semantics or unrelated connection lifecycle cleanup. No Brain runtime/config changes.
Decision Record impact: none — bounded transport-contract correction and explicit policy for its existing consumer.
Sweeps and Related
Live latest-open sweep: latest 20 Engine issues checked immediately before filing; no equivalent. Searches over all issue states for WebSocket close / wasClean found #8012, #8177 and the closed #18956 lineage, not an open repair. A2A latest 30 all-read-states: no competing claim; Ada supports the fork and requested the explicit-opt-out control. Own-assignment sweep: #16151 and #15526 do not overlap. Memory/KB sweeps surfaced historical connection work; primary tickets/source determine the contract. Structure-map run: the Brain Neural Link server remains outside this Engine-side change.
Related: #8012 · #8177 · #18956 · #18970
Origin Session ID: 4412eba5-6723-412d-a1c5-d9b2c22aff69
Retrieval Hint: WebSocket onClose native CloseEvent reason wasClean clean 1000 reconnectOnCleanClose Neural Link
Context
Promoted from Ada's unowned close-event defect note after an independent native WebSocket reproduction on the current Socket source (unchanged between the reviewed source and dev
4802862b7347ca7c55ca41685bbefedab784d7af). #8012 defines the emitted close payload; #8177 defines automatic recovery from abnormal closure. The merged #18970 deliberately preserves the Neural Link client's ability to recover after a bridge goes away.The Problem
beforeSetSocketbindsonCloseas the native socket'sonclose. That callback receives one CloseEvent, butonClose(event, reason, wasClean)reads the other two arguments. Both are undefined, and!wasCleanconsequently reconnects even after a clean code-1000 close.Independent probe (real loopback server, reconnect budget limited to one for observation):
job complete, wasClean trueCorrecting only the argument read would also silently remove clean-close recovery from the default Neural Link client. Its later window-return retry starts only after the socket exhausts a reconnect cycle.
The Architectural Reality
src/data/connection/WebSocket.mjs#beforeSetSocketowns native callback binding;#onCloseowns the observable payload and automatic retry decision.src/ai/Client.mjs#connectcreates its socket throughClassSystem.beforeSetInstance; explicit object config overrides supplied defaults, while an existing instance retains its configuration.Client#redialis exhaustion-driven. An explicit opt-out of clean-close recovery must not be silently undone by a focus event.unit/data/connection/WebSocket.spec.mjsande2e/core/AiClientLateBridge.spec.mjs.The Fix
Read
reasonandwasCleanfrom CloseEvent and preserve the observable payload shape. Keep automatic abnormal-close recovery. AddreconnectOnCleanClose: falseto the generic socket; the default socket created by the Neural Link client opts in explicitly.An explicit false or a caller-supplied socket keeps its own clean-close policy. In the client, a clean close with that policy disabled ends automatic recovery, including implicit focus/visibility re-dials; it does not fabricate an exhausted retry cycle. No second retry scheduler or duplicated close predicate belongs in the client.
Prescription checked:
WebSocket#onCloseowns payload/transport policy;Client#connectowns the client's default socket policy.Contract Ledger
closeevent{event, reason, wasClean}reads fields from the one eventreconnectOnCleanCloseAcceptance Criteria
Out of Scope
Changing the backoff budget, clean-close protocol codes, bridge server behavior, message queue semantics or unrelated connection lifecycle cleanup. No Brain runtime/config changes.
Decision Record impact: none — bounded transport-contract correction and explicit policy for its existing consumer.
Sweeps and Related
Live latest-open sweep: latest 20 Engine issues checked immediately before filing; no equivalent. Searches over all issue states for
WebSocket close/wasCleanfound #8012, #8177 and the closed #18956 lineage, not an open repair. A2A latest 30 all-read-states: no competing claim; Ada supports the fork and requested the explicit-opt-out control. Own-assignment sweep: #16151 and #15526 do not overlap. Memory/KB sweeps surfaced historical connection work; primary tickets/source determine the contract. Structure-map run: the Brain Neural Link server remains outside this Engine-side change.Related: #8012 · #8177 · #18956 · #18970
Origin Session ID: 4412eba5-6723-412d-a1c5-d9b2c22aff69 Retrieval Hint:
WebSocket onClose native CloseEvent reason wasClean clean 1000 reconnectOnCleanClose Neural Link