/** * WebSocket connection manager with exponential backoff reconnection * and message buffering during disconnection. */ import { normalizeQuiesceMs } from "@blackbelt-technology/pi-dashboard-shared/recovery-timing.js"; import { WebSocket as WsWebSocket } from "ws"; import { certValidationFor } from "./bridge-tls-checks.js"; import { bridgeDialUrl } from "./connect-target.js"; import { isLoopbackEndpoint as defaultLoopbackClassifier } from "./endpoint-resolution.js"; import { isRemoteEndpoint } from "./remote-registration-gate.js"; /** One migration decision, accepted or refused. The bridge's ONLY record * channel for re-targeting — wired to `transportDiagnostics` in bridge.ts so * the record reaches the server even under the default * `keeperLog.capturePiOutput:false` (fix-bridge-mdns-migration-hijack D5). * Every accepted re-target and every refusal produces exactly one record. */ export interface ConnectionMigrationRecord { from: string; to: string; accepted: boolean; trigger: string; reason: string; } /** * State captured at the instant the liveness watchdog decided to force-close. * See `ConnectionManagerOptions.onWatchdogFire`. */ export interface WatchdogFireInfo { /** Age of the last received frame. Cannot alone distinguish a silent peer from a blocked loop — see `maxTickDriftMs`. */ silentForMs: number; /** The configured threshold, so the ratio is readable without knowing config. */ watchdogTimeout: number; /** Socket readyState before teardown — 1 (OPEN) means we hung up on a live socket. */ readyState: number; /** * Depth of the PARSED-frame dispatch queue. Non-zero means frames were read * and parsed but their handlers had not run — slow dispatch. NOT a probe for * a blocked event loop: a blocked loop never parses, so this stays 0 while * `silentForMs` overshoots. */ inboundQueueDepth: number; /** * Worst observed lateness of the watchdog's OWN 15s check tick, since the * watchdog started. This is a DIRECT measurement of event-loop blocking, * not an inference from `silentForMs`: * * drift ~0 + silence at threshold → the server really stopped sending * drift large → our loop was blocked; the silence is * partly our own starvation, and acks * may have been pending unread * * Without it the two are indistinguishable, because a blocked loop produces * the same `silentForMs` as a silent peer. */ maxTickDriftMs: number; /** Cumulative inbound frames refused for a full queue. */ refusedInbound: number; } /** Why a marked bridge refused the server it reached. */ export type ServerProofFailure = "server-proof-missing" | "server-proof-invalid"; /** * Everything one dial attempt needs, produced fresh per attempt so a * nonce-bearing proof is never reused on a reconnect. * See change: loopback-credential-proof (D6). */ export interface DialPlan { /** Per-dial URL override (a freshly minted ticket). */ url?: string; /** Fresh proof headers for THIS dial. */ headers?: Record; /** The pre-connected TCP socket the proof is bound to, handed to `ws`. */ createConnection?: () => unknown; /** * Verifies the server's upgrade response BEFORE `open`. Present only for a * marked endpoint; absent means no server verification is demanded. */ verifyUpgrade?: (res: { headers: Record; }) => { ok: true } | { ok: false; cause: ServerProofFailure }; /** Release resources acquired for the plan when the dial never starts. */ dispose?: () => void; } /** One identity failure, reported only over a later VERIFIED connection to the same endpoint. */ export interface IdentityFailureReport { ts: number; pid: number; endpoint: string; cause: ServerProofFailure; } export interface ConnectionManagerOptions { /** * Transform every outgoing message BEFORE it is serialized or buffered: * returns the frames to send, in order. The vault's value-scoped masking * (and its tail flush) lives here so no plaintext value ever sits in the send * buffer. Must not throw. See change: add-credential-vault (D5). */ outboundFilter?: (message: unknown) => unknown[]; url: string; WebSocketImpl?: any; /** * Runs before EVERY connection attempt, including reconnects, and may * override the URL for that attempt. Exists because a remote bridge's * gateway ticket is single-use with a 15s TTL: minting it once at startup * would authorise the first connection and no other, so a reconnect after * any drop would be refused. Returning nothing leaves the URL as-is. */ prepareConnect?: () => Promise<{ url?: string } | undefined>; /** * Extra static WebSocket upgrade headers. The loopback local credential is a * per-dial PROOF produced by `planDial`, never a static header (D6). Requires the `ws` client — the global WebSocket * cannot set headers at all. */ headers?: Record; /** * Per-endpoint dial credentials. A re-target MUST NOT carry the previous * endpoint's credentials: a loopback local token presented to a discovered * remote host is a credential leak, and a ticket minted against the old * host is worthless to the new one. When supplied, adoption and reversal * both re-derive `{headers, prepareConnect}` for the endpoint actually * being dialled. (CodeRabbit review, fix-bridge-mdns-migration-hijack.) */ credentialsFor?: (url: string) => { headers?: Record; prepare?: () => Promise<{ url?: string } | undefined>; planDial?: (baseUrl: string) => Promise; }; /** * Per-dial plan (fresh proof headers, the pre-connected socket the proof is * bound to, and optional server verification). Called on EVERY attempt with * the base endpoint; merged with `prepareConnect` and the static `headers`. * See change: loopback-credential-proof (D6). */ planDial?: (baseUrl: string) => Promise; /** * Fired once per server-proof failure so the owner can record it where the * suspect server cannot observe it (owner-only log). The manager itself keeps * the report for delivery over a later verified connection (D8). */ onIdentityFailure?: (report: IdentityFailureReport) => void; maxBufferSize?: number; /** * Bound on the SERIALIZED INBOUND queue. Distinct from `maxBufferSize`, which * bounds the OUTGOING send ring. On overflow the newest inbound message is * refused. Default 1000. * * NOTE: the drain loop dequeues with `Array.prototype.shift()`, which * reindexes the remainder — so draining is O(n²) in the queue length. That is * irrelevant at the default bound (1000 → sub-millisecond), but raising this * value by orders of magnitude would make the cost noticeable; switch the * queue to a read-index/deque first if you ever do. * See change: serialize-bridge-message-pump. */ maxInboundQueue?: number; /** Server liveness watchdog: force reconnect after this many ms without any received message. Default 60000. Set 0 to disable. */ watchdogTimeout?: number; /** * Fired immediately BEFORE the watchdog force-closes a socket. * * A watchdog close is indistinguishable from a network drop in server.log — * both surface as `connection closed` followed by a re-register — so a * reconnect storm cannot be attributed without this. `maxTickDriftMs` is the * load-bearing field: near zero means the peer really did go quiet, while a * large value means THIS loop stalled — a different failure with a different * fix. `silentForMs` cannot separate them, because a blocked loop and a * silent peer produce the same number. * * Only reached after the deferred re-check has confirmed the silence, so a * report here means the peer stayed quiet across a full loop turn — not that * the reads were merely pending. * * Runs synchronously before teardown: an exception is swallowed, but a slow * callback delays the close by its own runtime. Keep it cheap. */ onWatchdogFire?: (info: WatchdogFireInfo) => void; onMessage?: (data: unknown) => void | Promise; /** * Fired on EVERY open, including the first — unlike `onReconnect`, which * deliberately skips it. A move's target connection needs the first one: * that is when it must announce its provisional registration, and `send()` * before the socket is live is silently dropped (task 9.4). */ onOpen?: () => void; /** * Fired whenever the live socket goes away — a drop or an intentional * `disconnect()`. Lets request/reply callers fail pending work fast. * See change: expose-plugin-credential-and-oauth-seams (D7). */ onClose?: () => void; onReconnect?: () => void; /** * Fired after the buffered frames queued during a drop have been flushed * onto the freshly opened socket. Paired with `onReconnect`: it deliberately * skips the first open, where there is no drop to heal from. * * `onopen` fires `onReconnect()` BEFORE the flush, so anything sent from * there OVERTAKES a buffered real event. A status correction sent that early * would settle the session to `idle` first, and the buffered real * `agent_end` would then arrive with `before.status === "idle"` — the * `streaming→idle` edge `isUnreadTrigger` needs is gone and the unread * stripe is silently lost. Ordering is pinned by construction here rather * than by comment. * * See change: fix-stuck-streaming-status-latch (design D8). */ onPostFlush?: () => void; /** * Fired when the server terminally refuses this bridge's registration for a * session id (another live bridge already serves it). Reconnection is NOT * retried afterwards. See change: fix-duplicate-bridge-registration (D2). */ onRegisterRejected?: (sessionId: string, reason: string) => void; /** * The bridge's own session id, used as the ROUTING field of a drop report. * The id a dropped message named travels as payload instead, because the * gateway refuses any inbound frame whose routing id belongs to another * connection. Reporting is skipped until this resolves. * See change: fix-spawn-correlation-ttl-coupling (D6). */ getSessionId?: () => string | undefined; /** * Records EVERY migration decision this manager makes — accepted or * refused, with both endpoints, the trigger and the reason. The single * chokepoint that makes a re-target un-silent (D5): without it a migration * is visible only as an unexplained `connection closed` in server.log. * See change: fix-bridge-mdns-migration-hijack. */ onMigrationEvent?: (record: ConnectionMigrationRecord) => void; /** * Failed connection attempts against a newly adopted endpoint after which * the manager returns to the last registered endpoint (reversibility, D2). * Default 4 — see MIGRATION_MAX_FAILED_OPENS. */ migrationAttemptsBound?: number; /** How long a rejected endpoint may not be re-adopted (anti-ping-pong, D2). Default 60s. */ migrationCooldownMs?: number; /** * Loopback classifier for the localhost-preference invariant (D3). * Defaults to the pure lexical classifier in endpoint-resolution.ts. */ isLoopbackEndpoint?: (url: string) => boolean; } /** Reports per session per window, so an overflow burst cannot amplify. */ const DROP_REPORT_LIMIT = 10; const DROP_REPORT_WINDOW_MS = 60_000; export class ConnectionManager { private url: string; private WS: any; private ws: any | null = null; private buffer: string[] = []; private maxBufferSize: number; private reconnectTimer: ReturnType | null = null; private backoff = 0; private intentionalClose = false; private hasConnectedBefore = false; private onMessage?: (data: unknown) => void | Promise; private outboundFilter?: (message: unknown) => unknown[]; private onOpen?: () => void; private onClose?: () => void; private onReconnect?: () => void; private onPostFlush?: () => void; private onRegisterRejected?: (sessionId: string, reason: string) => void; private getSessionIdForReports?: () => string | undefined; private dropReportWindowStart = 0; private dropReportsInWindow = 0; private dropReportsSuppressed = 0; /** * Session the counters above belong to. A `ConnectionManager` outlives a * `new`/`fork`/`resume` session change, so without this the previous * session's exhausted budget would suppress the NEW session's reports for the * rest of the window, and its `suppressed` count would ride a report that * names a different session. */ private dropReportSessionId: string | undefined; /** * Report an inbound message this bridge threw away, best-effort. * * Gated on a LIVE socket with no buffering fallback: `send()` silently * buffers when the socket is down, and a buffered report would surface after * reconnect and misdescribe when the drop happened. * See change: fix-spawn-correlation-ttl-coupling (D6). */ reportInboundDrop(drop: { dropClass: "session_mismatch" | "queue_overflow"; messageType?: string; droppedSessionId?: string; }): void { if (this.ws?.readyState !== 1) return; const sessionId = this.getSessionIdForReports?.(); if (!sessionId) return; const now = Date.now(); if (sessionId !== this.dropReportSessionId) { this.dropReportSessionId = sessionId; this.dropReportWindowStart = now; this.dropReportsInWindow = 0; this.dropReportsSuppressed = 0; } if (now - this.dropReportWindowStart >= DROP_REPORT_WINDOW_MS) { this.dropReportWindowStart = now; this.dropReportsInWindow = 0; } if (this.dropReportsInWindow >= DROP_REPORT_LIMIT) { this.dropReportsSuppressed++; return; } this.dropReportsInWindow++; const suppressed = this.dropReportsSuppressed; this.dropReportsSuppressed = 0; // Raw `ws.send`, never `this.send`: a report must never be buffered. try { this.ws.send( JSON.stringify({ type: "inbound_drop_report", sessionId, dropClass: drop.dropClass, ...(drop.messageType ? { messageType: drop.messageType } : {}), ...(drop.droppedSessionId ? { droppedSessionId: drop.droppedSessionId } : {}), ...(suppressed ? { suppressed } : {}), }), ); } catch { // Best-effort by contract: the socket died between the check and the send. } } /** * Serialized inbound pump. `ws.onmessage` enqueues; a single drain loop * awaits each handler to completion before dispatching the next, so a * state-mutating message (`set_model`) can no longer be overtaken by a * dependent one (`send_prompt`) that runs during its first `await`. * See change: serialize-bridge-message-pump. */ private inboundQueue: unknown[] = []; private draining = false; /** * Bumped on every teardown. The drain loop captures it on entry and retires * itself when it no longer matches, so a loop parked on an uncancellable * in-flight handler can never dispatch against a replacement connection. */ private drainEpoch = 0; private maxInboundQueue: number; private droppedInboundCount = 0; private discardedInboundCount = 0; private lastInboundWarnAt = 0; /** * Types dispatched WITHOUT waiting for the serialized queue. Allow-list: an * unrecognized type falls through to the serialized lane. Each member is * incapable of invalidating a message queued behind it — * `prompt_response` is correlated by request id (queueing it behind the * handler awaiting it would deadlock permanently), `server_restarting` is a * time-critical lifecycle signal delivered immediately before the socket * closes, and `kill_process` is pgid-keyed and is the only mechanism able to * terminate a child that is itself occupying the queue. * `abort` is deliberately NOT here: dispatched early it would run ahead of * the `send_prompt` it cancels and silently lose the cancellation. * `request_models` touches only the dashboard's own model catalogue (an * auth reload + a provider refresh) — never pi's turn state — and that * refresh is network-bound: serialized, a slow or * hung refresh blocks the head of the queue and every later message — * including `send_prompt` — never dispatches. * See change: fix-optimistic-prompt-stuck-sending. */ private static readonly IMMEDIATE_TYPES = new Set(["prompt_response", "server_restarting", "kill_process", "request_models"]); private static readonly INITIAL_BACKOFF = 1000; private static readonly MAX_BACKOFF = 30000; private static readonly WATCHDOG_CHECK_INTERVAL = 15_000; private static readonly DEFAULT_WATCHDOG_TIMEOUT = 60_000; /** * Failed connection attempts against a migrated-to endpoint before the * manager gives up on it and returns to the last registered endpoint. * * Basis for 4 (design D2, recorded per task 3.4): the reconnect backoff is * 1s → 2s → 4s → 8s, so the fourth failure lands ~15s after adoption. A * deliberate server restart answers again within ~5–10s (observed on * `POST /api/restart`), so 4 attempts ride out a real restart without ever * backing off unbounded against a dead candidate — while still returning * the bridge to the working endpoint within seconds of a mistaken * migration. */ private static readonly MIGRATION_MAX_FAILED_OPENS = 4; /** How long a reversed-away endpoint may not be re-adopted (anti-ping-pong, D2). * Long enough to outlive the window in which a flapping pair would * otherwise swap on every discovery tick; short enough that a deliberate * re-discovery a minute later is unaffected. */ private static readonly MIGRATION_COOLDOWN_MS = 60_000; private lastMessageAt = 0; private watchdogTimer: ReturnType | null = null; private onWatchdogFire?: (info: WatchdogFireInfo) => void; private watchdogLastTickAt = 0; private watchdogMaxTickDrift = 0; private watchdogRecheckPending = false; private watchdogTimeout: number; /** * Auto-start suppression deadline (epoch ms). When the server announces * a deliberate restart/shutdown via `server_restarting`, the bridge sets * this to `Date.now() + quiesceMs` so the spawn step in `autoStartServer` * is skipped while the orchestrator does its work. Discovery + reconnect * are NOT suppressed. * See change: fix-restart-bridge-auto-start-race. */ private suppressUntil = -Infinity; /** Upgrade headers presented on every (re)connect. */ private headers?: Record; private prepareConnect?: () => Promise<{ url?: string } | undefined>; private planDial?: (baseUrl: string) => Promise; private readonly onIdentityFailure?: (report: IdentityFailureReport) => void; /** Set by a failed `verifyUpgrade`: the attempt that is closing was an identity failure. */ private identityFailure: ServerProofFailure | null = null; /** * Generation of the newest dial. An async plan (pre-connect, ticket mint) can * outlive the dial that asked for it — a re-target or `disconnect()` while it * awaits — and its late result must not open an orphaned socket that no * handler owns. See change: loopback-credential-proof (audit). */ private dialGen = 0; /** Consecutive identity failures; indexes the quarantine backoff, cleared by an open. */ private quarantineFailures = 0; /** True while the open socket's server proof verified (gates the report flush). */ private dialVerified = false; /** The single report slot: only a marked dial can fail identity, and a process has one marker. */ private pendingIdentityReports: IdentityFailureReport[] = []; private static readonly MAX_IDENTITY_REPORTS = 20; private static readonly QUARANTINE_FIRST_MS = 5_000; private static readonly QUARANTINE_SECOND_MS = 30_000; private static readonly QUARANTINE_MAX_MS = 300_000; // ── Migration state (fix-bridge-mdns-migration-hijack) ───────────────── /** The endpoint on which a `session_register` was last sent on a LIVE * socket. The reversal target (D2) and the marker that the incumbent is * protected by the admission gate. */ private lastRegisteredUrl: string | undefined; /** A registration noted while the socket was still connecting; promoted * when the open flushes the buffered register onto the live socket. */ private pendingRegisterNote = false; /** The configured endpoint, retained as the reversal anchor for a COLD-START * adoption (nothing was ever registered, so `lastRegisteredUrl` is * undefined and reversal would otherwise have nowhere to go). */ private fallbackUrl: string | undefined; /** Consecutive failed opens against a migrated-to endpoint (D2 counter). */ private failedOpensSinceMigration = 0; /** Endpoint → epoch-ms until which it may not be re-adopted (D2 cooldown). */ private cooldownUntil = new Map(); private readonly onMigrationEvent?: (record: ConnectionMigrationRecord) => void; private readonly migrationAttemptsBound: number; private readonly migrationCooldownMs: number; private readonly isLoopbackEndpoint: (url: string) => boolean; private readonly credentialsFor?: ConnectionManagerOptions["credentialsFor"]; constructor(options: ConnectionManagerOptions) { this.url = options.url; this.fallbackUrl = options.url; this.headers = options.headers; // The `ws` package, NOT `globalThis.WebSocket`. // // Two independent requirements force this, and one swap satisfies both: // - `ws+unix://:/` is rejected outright by the global/undici // WebSocket (`DOMException: expected a ws: or wss: url`), so the // local socket transport is unreachable without it (D1); // - the loopback local-credential proof rides an `X-Pi-Local-Proof` // upgrade header, and the global WebSocket cannot set headers (D6). // See change: add-pi-gateway-transport-identity (task 2.6). this.WS = options.WebSocketImpl ?? WsWebSocket; this.prepareConnect = options.prepareConnect; this.planDial = options.planDial; this.onIdentityFailure = options.onIdentityFailure; this.credentialsFor = options.credentialsFor; // Validate the numeric options up front: a negative bound would refuse // every message and a NaN/Infinity bound would disable the limit entirely // (`length >= NaN` is always false), both silently. this.maxBufferSize = ConnectionManager.intOption(options.maxBufferSize, 10000, "maxBufferSize", 1); this.maxInboundQueue = ConnectionManager.intOption(options.maxInboundQueue, 1000, "maxInboundQueue", 1); // `watchdogTimeout` accepts 0 — that documented value disables the watchdog. this.watchdogTimeout = ConnectionManager.intOption( options.watchdogTimeout, ConnectionManager.DEFAULT_WATCHDOG_TIMEOUT, "watchdogTimeout", 0, ); this.onWatchdogFire = options.onWatchdogFire; this.onMessage = options.onMessage; this.outboundFilter = options.outboundFilter; this.onOpen = options.onOpen; this.onClose = options.onClose; this.onReconnect = options.onReconnect; this.onPostFlush = options.onPostFlush; this.onRegisterRejected = options.onRegisterRejected; this.getSessionIdForReports = options.getSessionId; this.onMigrationEvent = options.onMigrationEvent; this.migrationAttemptsBound = ConnectionManager.intOption( options.migrationAttemptsBound, ConnectionManager.MIGRATION_MAX_FAILED_OPENS, "migrationAttemptsBound", 1, ); this.migrationCooldownMs = ConnectionManager.intOption( options.migrationCooldownMs, ConnectionManager.MIGRATION_COOLDOWN_MS, "migrationCooldownMs", 1, ); this.isLoopbackEndpoint = options.isLoopbackEndpoint ?? defaultLoopbackClassifier; } /** * Resolve a numeric constructor option, rejecting values that would silently * break the behaviour they configure. `min` is the smallest legal value (1 for * the bounds, 0 for `watchdogTimeout` where 0 means "disabled"). */ private static intOption(value: number | undefined, fallback: number, name: string, min: number): number { const resolved = value ?? fallback; if (!Number.isSafeInteger(resolved) || resolved < min) { throw new RangeError(`${name} must be a safe integer >= ${min} (received ${resolved})`); } return resolved; } connect(): void { this.intentionalClose = false; this.createConnection(); this.startWatchdog(); } disconnect(): void { this.intentionalClose = true; this.dialGen++; this.resetInbound(); this.stopWatchdog(); if (this.reconnectTimer) { clearTimeout(this.reconnectTimer); this.reconnectTimer = null; } if (this.ws) { this.ws.onclose = null; this.ws.close(); this.ws = null; this.fireClose(); } } private fireClose(): void { try { this.onClose?.(); } catch { /* a listener must not break teardown */ } } /** * Send only when the socket is OPEN — never buffers. For request frames * whose caller must learn synchronously that nothing was sent (a buffered * request could execute after reconnect with its reply dropped). * See change: expose-plugin-credential-and-oauth-seams (D7). */ sendIfOpen(message: unknown): boolean { if (this.ws?.readyState !== 1) return false; try { for (const frame of this.outbound(message)) this.ws.send(JSON.stringify(frame)); return true; } catch { return false; } } send(message: unknown): void { for (const frame of this.outbound(message)) this.sendOne(JSON.stringify(frame)); } /** Frames to put on the wire for `message`: itself, unless the outbound filter rewrites or adds frames. */ private outbound(message: unknown): unknown[] { if (!this.outboundFilter) return [message]; try { return this.outboundFilter(message); } catch { // Fail CLOSED for forwarded events: an event the masker could not process // might carry a value, so it is not sent. Other frames carry no values. return (message as { type?: unknown } | null)?.type === "event_forward" ? [] : [message]; } } private sendOne(data: string): void { if (this.ws?.readyState === 1) { try { this.ws.send(data); } catch { // Connection died between readyState check and send — buffer instead this.bufferMessage(data); } } else { this.bufferMessage(data); } } /** * Count of outgoing messages evicted from the bounded send buffer while the * WS was not OPEN (ring-buffer overflow). Silent before this change — an * evicted event never reaches the server store, leaves no seq gap, and is * only recovered by `replaySessionEntries()` on reconnect. Exposed for * observability. See change: fix-stuck-tool-card-on-dropped-event. */ private droppedBufferedCount = 0; private static readonly DROP_WARN_WINDOW_MS = 5_000; private lastDropWarnAt = 0; /** Total messages evicted from the send buffer on overflow (for diagnostics). */ getDroppedBufferedCount(): number { return this.droppedBufferedCount; } /** * Inbound messages REFUSED because the serialized queue was at * `maxInboundQueue`. Kept separate from `getDiscardedInboundCount()` because * an overflow is a bug signal while a disconnect discard is routine — one * counter would let reconnect churn mask the overflow. */ getDroppedInboundCount(): number { return this.droppedInboundCount; } /** Inbound messages discarded because the socket went away before dispatch. */ getDiscardedInboundCount(): number { return this.discardedInboundCount; } /** * Route an inbound message: immediate lane, or the serialized queue with * drop-newest back-pressure. */ private enqueueInbound(parsed: unknown): void { const type = (parsed as { type?: unknown } | null)?.type; if (typeof type === "string" && ConnectionManager.IMMEDIATE_TYPES.has(type)) { this.dispatchInbound(parsed); return; } if (this.inboundQueue.length >= this.maxInboundQueue) { // Drop NEWEST: refusing the tail keeps the ordering guarantee of the // already-accepted prefix intact. (Dropping oldest would silently discard // the `set_model` whose ordering this pump exists to protect.) this.droppedInboundCount++; const now = Date.now(); if (now - this.lastInboundWarnAt >= ConnectionManager.DROP_WARN_WINDOW_MS) { this.lastInboundWarnAt = now; console.warn( `[bridge] refused inbound message (queue full) hop=server→bridge refusedType=${typeof type === "string" ? type : "unknown"} (total refused=${this.droppedInboundCount}, maxInboundQueue=${this.maxInboundQueue})`, ); } // The warn above is throttled to one per 5 s AND lands in /dev/null under // the default `capturePiOutput:false`; the report is what the server can // actually record. See change: fix-spawn-correlation-ttl-coupling (D6). this.reportInboundDrop({ dropClass: "queue_overflow", messageType: typeof type === "string" ? type : undefined, }); return; } this.inboundQueue.push(parsed); if (!this.draining) void this.drainInbound(); } /** Invoke the handler without awaiting it, isolating sync throws + rejections. */ private dispatchInbound(parsed: unknown): void { try { const result = this.onMessage?.(parsed); if (result && typeof (result as Promise).catch === "function") { (result as Promise).catch((err: unknown) => { console.error("[bridge] inbound handler failed:", err); }); } } catch (err) { console.error("[bridge] inbound handler failed:", err); } } private async drainInbound(): Promise { this.draining = true; const epoch = this.drainEpoch; while (this.inboundQueue.length > 0) { const msg = this.inboundQueue.shift(); try { await this.onMessage?.(msg); } catch (err) { // Failure isolation lives here: the pump owns the loop that could stall. console.error("[bridge] inbound handler failed:", err); } // Superseded by a teardown while awaiting: retire WITHOUT touching the // queue or the guard — a replacement loop already owns both. if (epoch !== this.drainEpoch) return; } this.draining = false; } /** * Drop the pending inbound queue and retire the current drain loop. The * running-guard is released HERE, not when the superseded loop finally * exits: an in-flight handler cannot be cancelled, so waiting for it would * stall the replacement connection for the handler's full duration. */ private resetInbound(): void { if (this.inboundQueue.length > 0) { this.discardedInboundCount += this.inboundQueue.length; this.inboundQueue = []; } this.drainEpoch++; this.draining = false; } private bufferMessage(data: string): void { this.buffer.push(data); if (this.buffer.length > this.maxBufferSize) { const evicted = this.buffer.shift(); this.droppedBufferedCount++; let droppedType = "unknown"; try { const parsed = JSON.parse(evicted ?? ""); if (parsed && typeof parsed.type === "string") droppedType = parsed.type; } catch { // best-effort: leave "unknown" } const now = Date.now(); if (now - this.lastDropWarnAt >= ConnectionManager.DROP_WARN_WINDOW_MS) { this.lastDropWarnAt = now; console.warn( `[bridge] dropped buffered message (ring overflow) hop=bridge→server droppedType=${droppedType} (total dropped=${this.droppedBufferedCount}, bufferSize=${this.maxBufferSize})`, ); } } } get isConnected(): boolean { return this.ws?.readyState === 1; } /** * Pause auto-start spawn for `ms` milliseconds. Idempotent: only extends * the suppression window, never shortens it. See change: * fix-restart-bridge-auto-start-race. */ pauseAutoStart(ms: number): number { // Every caller is bounded here: invalid -> ignored, fractional -> rounded // up, > MAX_QUIESCE_MS (incl. Infinity) -> clamped. Returns the applied // pause (0 = ignored). See change: harden-bridge-inbound-and-discovery (D17). const applied = normalizeQuiesceMs(ms); if (applied === undefined) return 0; const next = Date.now() + applied; if (next > this.suppressUntil) this.suppressUntil = next; return applied; } /** * Returns true while the auto-start spawn step should be suppressed. * See change: fix-restart-bridge-auto-start-race. */ shouldSuppressAutoStart(): boolean { // `<=`: the pause is "at least quiesceMs" (inclusive deadline). return Date.now() <= this.suppressUntil; } /** * Record that a `session_register` was just sent over this connection. * * "Acknowledged" here means: delivered on a LIVE socket (the server's only * register response is a terminal `register_rejected`; silence is * success). Called by the bridge right after each register send — the * initial registration and every `sendStateSync` — so the manager always * knows the last endpoint that actually accepted it. That endpoint is the * reversal target (D2) and marks the connection as protected by the * admission gate. * * Called while still connecting, the note is promoted on open, when the * buffered register flushes onto the live socket. */ noteRegistered(): void { if (this.ws?.readyState === 1) { this.lastRegisteredUrl = this.url; this.pendingRegisterNote = false; this.failedOpensSinceMigration = 0; return; } this.pendingRegisterNote = true; } /** * Re-target this connection at a new endpoint — the ONLY way an endpoint * changes after construction (task 2.2). Replaces the ambient `updateUrl`. * * Gates, in order (design D1–D3): * cooldown — a previously rejected endpoint may not be re-adopted yet; * protection — an ESTABLISHED AND REGISTERED connection is never dropped * for an unverified candidate (a connection that is merely dialling or * established-but-unregistered — the cold-start window — migrates * freely, which is exactly how init-time discovery attaches); * localhost preference — a remote candidate never displaces an * established loopback connection; * admission — the candidate must pass the caller's health probe BEFORE * the incumbent is dropped: the drop-then-discover-dead window is the * outage this gate exists to prevent. * * Resolves true when the connection ends up targeting `newUrl` (including * the current-URL no-op), false when a gate refused. EVERY decision — * accepted or refused — is reported via `onMigrationEvent` (D5); a * re-target cannot happen without a corresponding record. */ async retargetTo( newUrl: string, opts: { trigger: string; verify?: () => Promise }, ): Promise { const from = this.url; if (newUrl === from) return true; const record = (accepted: boolean, reason: string): boolean => { this.onMigrationEvent?.({ from, to: newUrl, accepted, trigger: opts.trigger, reason }); return accepted; }; // A manager that was terminally closed (explicit disconnect, or a // `register_rejected` contention refusal) must stay closed: discovery // callbacks fire regardless of connection state, and adoption's // `createConnection` would otherwise resurrect a dead manager. // (CodeRabbit review, fix-bridge-mdns-migration-hijack.) if (this.intentionalClose) { return record(false, "refused: connection is terminally closed"); } // Cooldown applies regardless of establishment: an endpoint this manager // reversed away from must not be re-adopted into a ping-pong, even in // the cold-start window. const cooledUntil = this.cooldownUntil.get(newUrl); if (cooledUntil !== undefined && Date.now() < cooledUntil) { const remainingS = Math.ceil((cooledUntil - Date.now()) / 1000); return record(false, `refused: candidate is in migration cooldown (${remainingS}s remaining)`); } if (this.lastRegisteredUrl === undefined) { // Nothing established AND registered: no incumbent to protect. The // configured endpoint is retained as the reversal anchor — a candidate // adopted in the cold-start window that never opens must fall back to // it after the failed-open bound instead of retrying forever. this.fallbackUrl = from; this.adoptEndpoint(newUrl); return record(true, "adopted: no established registered connection to protect"); } // Admission gate (D1): verify the candidate BEFORE dropping the // incumbent. No probe result is a refusal, not a pass. Probing runs // before the preference rule so a refusal names the candidate's actual // failure (D5's "failure reason"), per the spec's own scenario order — // "reachable" is judged first, "preferred under the localhost-preference // rule" second. if (!opts.verify) { return record(false, "refused: no health check was performed for the candidate"); } let reachable: boolean; try { reachable = await opts.verify(); } catch { reachable = false; } if (!reachable) { return record(false, "refused: candidate health or identity check did not pass"); } // Localhost preference as an invariant on the connection (D3) — keyed on // the incumbent being ESTABLISHED *right now*. A loopback endpoint that // has already dropped protects nothing: refusing a reachable remote // candidate for a dead localhost incumbent would be the strand class // this change exists to eliminate. (Round-1 review finding #1.) if ( this.isConnected && this.isLoopbackEndpoint(from) && !this.isLoopbackEndpoint(newUrl) ) { return record( false, "refused: localhost preference — a remote candidate must not displace the established loopback connection", ); } this.adoptEndpoint(newUrl); return record(true, "adopted: candidate verified reachable before the incumbent was dropped"); } /** * Point the manager at a new endpoint and dial it now. The incumbent — * when one exists, even mid-dial — is torn down WITHOUT arming the * reconnect loop; the fresh dial is made directly. */ private adoptEndpoint(newUrl: string): void { const hadSocket = this.ws !== null; this.url = newUrl; this.applyCredentials(newUrl); this.failedOpensSinceMigration = 0; this.backoff = 0; // Re-adoption clears the endpoint's own cooldown (round-1 review #4): // a deliberate adopt is the opposite of the ping-pong the cooldown blocks. this.cooldownUntil.delete(newUrl); if (hadSocket) { this.intentionalClose = true; try { this.handleDisconnect(); } finally { this.intentionalClose = false; } } // A pending reconnect timer would race the fresh dial against the OLD // endpoint — cancel it and dial the adopted endpoint now. A manager that // is actively trying to stay connected — live socket, PENDING RECONNECT // TIMER (cold-start first dial already failed into backoff), or a prior // success — must dial the adopted endpoint; only a manager that was // never asked to connect does not self-start. (Round-2 review: a timer // cancelled without redialling stranded a mid-backoff cold start.) const hadReconnectTimer = this.reconnectTimer !== null; if (this.reconnectTimer) { clearTimeout(this.reconnectTimer); this.reconnectTimer = null; } if (hadSocket || hadReconnectTimer || this.hasConnectedBefore) this.createConnection(); } /** Re-derive `{headers, prepareConnect}` for the endpoint about to be * dialled. Adoption and reversal both route through here so credentials * always match the endpoint on the wire — a loopback token must never * reach a remote host, and a remote host needs its own ticket. */ private applyCredentials(url: string): void { const c = this.credentialsFor?.(url); if (!c) return; this.headers = c.headers; this.prepareConnect = c.prepare; this.planDial = c.planDial; } private createConnection(): void { const gen = ++this.dialGen; if (!this.prepareConnect && !this.planDial) { this.openSocket(); return; } void this.prepareThenOpen(gen).catch(() => { // A stale dial's failure must not schedule a reconnect for a newer one. if (gen === this.dialGen) this.onPrepareFailed(); }); } /** Apply a per-attempt URL override (e.g. a freshly minted ticket) and a * per-attempt dial plan (fresh proof). Both apply to THIS DIAL only — the * base endpoint (`this.url`) is never mutated by preparation, so * `lastRegisteredUrl` and every migration record name the real endpoint, not * a ticketed one-off. */ private async prepareThenOpen(gen: number): Promise { const base = this.url; const override = await this.prepareConnect?.(); const plan = await this.planDial?.(base); if (gen !== this.dialGen || this.intentionalClose) { // Superseded (reconnect / disconnect) while the plan was being built: // release what it acquired and open nothing. plan?.dispose?.(); return; } if (this.url !== base) { // Re-targeted mid-plan: the plan (and any pre-connected socket) belongs to // the OLD endpoint and must never be dialled against the new one. This // manager was asked to connect, so dial the new endpoint instead. plan?.dispose?.(); this.createConnection(); return; } this.openSocket(plan?.url ?? override?.url, plan); } /** * Preparation failed (no credential, dashboard unreachable). Retry on the * normal backoff rather than dialling without it: the server would refuse * the upgrade anyway, and the operator would see a connection error instead * of a credential one. */ private onPrepareFailed(): void { if (!this.intentionalClose) this.scheduleReconnect(); } /** * `"enforced"` when the TLS checks apply to the current endpoint (a remote * `wss:` one with validation not disabled); declared on `session_register` and * the vault sync request. See change: prove-vault-bridge-tls (D7). */ get certValidation(): "enforced" | undefined { return certValidationFor(this.url); } private openSocket(rawDialUrl: string = this.url, plan?: DialPlan): void { // A path-less `wss://` endpoint dials the bridge route; a remote `wss` dial // always validates the certificate, whatever the process env says. const dialUrl = bridgeDialUrl(rawDialUrl); const headers = this.headers || plan?.headers ? { ...this.headers, ...plan?.headers } : undefined; const wsOptions: Record = {}; if (headers) wsOptions.headers = headers; if (dialUrl.startsWith("wss:") && isRemoteEndpoint(dialUrl)) wsOptions.rejectUnauthorized = true; if (plan?.createConnection) wsOptions.createConnection = plan.createConnection; this.identityFailure = null; this.dialVerified = false; try { this.ws = Object.keys(wsOptions).length > 0 ? new this.WS(dialUrl, wsOptions) : new this.WS(dialUrl); } catch { // Constructor failed — schedule reconnect this.ws = null; plan?.dispose?.(); if (!this.intentionalClose) { this.scheduleReconnect(); } return; } // Verify the server's identity on the upgrade response, BEFORE `open`: the // buffered `session_register` flushes in `onopen`, so nothing is sent to — // and nothing is dispatched from — a peer that cannot prove the credential. // A WebSocket without an `upgrade` event fails closed. // See change: loopback-credential-proof (D6, D8). let failDial: ((cause: ServerProofFailure) => void) | undefined; if (plan?.verifyUpgrade) { const ws = this.ws; const verify = plan.verifyUpgrade; const fail = (cause: ServerProofFailure): void => { if (this.ws !== ws) { // A superseded socket must never be left to open unverified. try { ws.terminate?.(); } catch { /* already torn down */ } return; } this.recordIdentityFailure(cause); // `terminate()` mid-handshake emits `error` on the next tick; `handleDisconnect` // detaches `onerror`, and an `error` with no listener would throw. try { ws.on?.("error", () => {}); } catch { /* fake without an emitter */ } try { ws.terminate?.(); } catch { /* already torn down */ } this.handleDisconnect(); }; failDial = fail; if (typeof ws.on !== "function") { fail("server-proof-missing"); return; } ws.on("upgrade", (res: { headers: Record }) => { if (this.ws !== ws) return fail("server-proof-missing"); let result: ReturnType; try { result = verify(res); } catch { result = { ok: false, cause: "server-proof-invalid" }; } if (result.ok) this.dialVerified = true; else fail(result.cause); }); } this.ws.onopen = () => { // A marked dial may open ONLY after its counter-proof verified. Production // `ws` always emits `upgrade` first; an injectable implementation that does // not must not flush buffered frames to an unverified peer. if (failDial && !this.dialVerified) { failDial("server-proof-missing"); return; } // Reset backoff on successful connection this.backoff = 0; this.quarantineFailures = 0; this.lastMessageAt = Date.now(); // A registration noted while this dial was in flight is now real: the // buffered `session_register` flushes onto THIS live socket below, so // this is the endpoint the server accepted. if (this.pendingRegisterNote) { this.pendingRegisterNote = false; this.lastRegisteredUrl = this.url; this.failedOpensSinceMigration = 0; } // Notify reconnect if this isn't the first connection const isReconnect = this.hasConnectedBefore; if (isReconnect) { this.onReconnect?.(); } this.hasConnectedBefore = true; this.onOpen?.(); // Flush buffer const buffered = [...this.buffer]; this.buffer = []; for (const data of buffered) { this.ws?.send(data); } this.flushIdentityReports(); // Strictly after the flush — see the `onPostFlush` doc comment. Paired // with `onReconnect`, so it skips the first open for the same reason. if (isReconnect) { this.onPostFlush?.(); } }; this.ws.onmessage = (ev: { data: string }) => { this.lastMessageAt = Date.now(); try { const parsed = JSON.parse(ev.data); // A contention refusal is TERMINAL. The server closes us right after // sending it, and every close otherwise looks transient, so without // this the refused duplicate reconnects and re-registers forever while // its pi keeps writing into the incumbent's transcript. // See change: fix-duplicate-bridge-registration (D2). if (parsed?.type === "register_rejected") { this.handleRegisterRejected(parsed.sessionId, parsed.reason); return; } // Handler dispatch is SERIALIZED: `enqueueInbound` appends to a queue // drained by a single loop that awaits each handler to completion, so a // `set_model` can no longer be overtaken by a following `send_prompt` // during its first `await`. Three types bypass the queue (see // `IMMEDIATE_TYPES`); everything else, including `abort`, is ordered. // The client-side confirm-before-send gate in the OpenSpec dialogs is // kept as belt-and-suspenders. // See change: serialize-bridge-message-pump. this.enqueueInbound(parsed); } catch { // Ignore malformed messages } }; this.ws.onclose = () => { this.handleDisconnect(); }; this.ws.onerror = () => { // Node 22's built-in WebSocket may fire onerror WITHOUT onclose // on connection failure. Handle once and prevent re-entrant calls // (ws.close() can re-trigger onerror synchronously). this.handleDisconnect(); }; } private handleDisconnect(): void { if (!this.ws) return; // Already handled — idempotent guard this.resetInbound(); const ws = this.ws; this.ws = null; // Detach handlers to prevent re-entrant calls from ws.close() ws.onclose = null; ws.onerror = null; ws.onopen = null; ws.onmessage = null; try { ws.close(); } catch { /* ignore — may already be closed */ } this.fireClose(); if (!this.intentionalClose) { if (this.identityFailure) { this.identityFailure = null; this.scheduleQuarantine(); } else { this.scheduleReconnect(); } } } /** * A marked dial's server failed to prove it holds this HOME's credential. * Keep the report for a later VERIFIED connection to this same endpoint and * hand it to the owner for the owner-only log. It never goes through the * suspect socket, and never to a different endpoint. */ private recordIdentityFailure(cause: ServerProofFailure): void { this.identityFailure = cause; const report: IdentityFailureReport = { ts: Date.now(), pid: process.pid, endpoint: this.url, cause }; this.pendingIdentityReports.push(report); while (this.pendingIdentityReports.length > ConnectionManager.MAX_IDENTITY_REPORTS) { this.pendingIdentityReports.shift(); } try { this.onIdentityFailure?.(report); } catch { /* the log is best-effort; quarantine must proceed */ } } /** Deliver pending identity reports, only over a connection whose proof verified. */ private flushIdentityReports(): void { if (!this.dialVerified || this.pendingIdentityReports.length === 0) return; const sessionId = this.getSessionIdForReports?.(); if (!sessionId) return; // Only this endpoint's failures: a malformed marker enforces on EVERY loopback // dial, so the slot can hold failures from another endpoint, which must not // travel over this connection. const pending = this.pendingIdentityReports.filter((r) => r.endpoint === this.url); this.pendingIdentityReports = this.pendingIdentityReports.filter((r) => r.endpoint !== this.url); for (const r of pending) { try { this.ws?.send( JSON.stringify({ type: "bridge_diagnostic", sessionId, event: "server_proof_failed", detail: JSON.stringify(r), }), ); } catch { /* best-effort, like every other diagnostic */ } } } /** Quarantine backoff: 5 s, 30 s, then doubling to a 5 min cap (D8). */ private scheduleQuarantine(): void { const n = this.quarantineFailures++; const delay = n === 0 ? ConnectionManager.QUARANTINE_FIRST_MS : Math.min(ConnectionManager.QUARANTINE_SECOND_MS * 2 ** (n - 1), ConnectionManager.QUARANTINE_MAX_MS); this.reconnectTimer = setTimeout(() => { this.reconnectTimer = null; this.createConnection(); }, delay); } private startWatchdog(): void { this.stopWatchdog(); if (this.watchdogTimeout <= 0) return; this.watchdogLastTickAt = Date.now(); this.watchdogMaxTickDrift = 0; this.watchdogTimer = setInterval(() => { // Lateness of THIS tick against its own schedule. A starved loop cannot // report its own starvation any other way — every other signal here is // downstream of the same blocked loop. const now = Date.now(); const drift = now - this.watchdogLastTickAt - ConnectionManager.WATCHDOG_CHECK_INTERVAL; this.watchdogLastTickAt = now; if (drift > this.watchdogMaxTickDrift) this.watchdogMaxTickDrift = drift; if (!this.isSilentPastThreshold()) return; // Do NOT believe the silence yet. If the loop was blocked inside an I/O // callback, it wraps to the TIMERS phase before it can re-enter poll, so // frames that would refresh `lastMessageAt` are still unread in the // socket buffer and this tick would force-close a live connection. // A 0ms timer re-enters the timers phase only AFTER the loop has passed // through poll, so a peer that is still sending gets to prove it first. // (`setImmediate` would do the same, but vitest's fake timers never run // it, which would make this defect untestable at the unit level.) // Measured: a 1.5s block inside onMessage force-closed a peer that had // never stopped sending. if (this.watchdogRecheckPending) return; this.watchdogRecheckPending = true; setTimeout(() => { this.watchdogRecheckPending = false; // The watchdog may have been stopped while we waited. if (!this.watchdogTimer) return; if (!this.isSilentPastThreshold()) return; // Report BEFORE tearing down: `this.ws` is nulled by handleDisconnect, // so readyState is only observable here. try { this.onWatchdogFire?.({ silentForMs: Date.now() - this.lastMessageAt, watchdogTimeout: this.watchdogTimeout, readyState: this.ws?.readyState ?? -1, inboundQueueDepth: this.inboundQueue.length, refusedInbound: this.droppedInboundCount, maxTickDriftMs: this.watchdogMaxTickDrift, }); } catch { // Diagnostics are best-effort; never block the reconnect they explain. } // Server has gone silent — force close to trigger reconnect this.handleDisconnect(); }, 0); }, ConnectionManager.WATCHDOG_CHECK_INTERVAL); } /** Inbound silence has passed the configured threshold on a live socket. */ private isSilentPastThreshold(): boolean { return ( !!this.ws && this.lastMessageAt > 0 && Date.now() - this.lastMessageAt >= this.watchdogTimeout ); } private stopWatchdog(): void { if (this.watchdogTimer) { clearInterval(this.watchdogTimer); this.watchdogTimer = null; } } /** * Stop retrying for a session id the server refused, and surface the reason * rather than dying silently. */ private handleRegisterRejected(sessionId: string | undefined, reason: string | undefined): void { console.error( `[bridge] registration refused for session ${sessionId ?? "(unknown)"}: ` + `${reason ?? "no reason given"} — not retrying`, ); this.onRegisterRejected?.(sessionId ?? "", reason ?? ""); // Treat as an intentional close so `handleDisconnect` does not rearm the // backoff loop when the server closes the socket behind this frame. this.intentionalClose = true; this.handleDisconnect(); } private scheduleReconnect(): void { // Reversibility (D2): failed opens are only counted against a MIGRATED // endpoint — one that differs from the reversal anchor: the last // registered endpoint, or — for a cold-start adoption where nothing was // ever registered — the configured endpoint. Reconnect churn against // the anchor itself never reverses anywhere. const anchor = this.lastRegisteredUrl ?? this.fallbackUrl; if (anchor !== undefined && this.url !== anchor) { this.failedOpensSinceMigration++; if (this.failedOpensSinceMigration >= this.migrationAttemptsBound) { const rejected = this.url; this.cooldownUntil.set(rejected, Date.now() + this.migrationCooldownMs); const from = this.url; this.url = anchor; this.applyCredentials(anchor); this.failedOpensSinceMigration = 0; this.backoff = 0; this.onMigrationEvent?.({ from, to: this.url, accepted: true, trigger: "migration-fallback", reason: `returned to the last registered endpoint after ${this.migrationAttemptsBound} failed opens against ${rejected}`, }); // Fall through: normal scheduling dials the restored endpoint. } } if (this.backoff === 0) { this.backoff = ConnectionManager.INITIAL_BACKOFF; } else { this.backoff = Math.min(this.backoff * 2, ConnectionManager.MAX_BACKOFF); } this.reconnectTimer = setTimeout(() => { this.reconnectTimer = null; this.createConnection(); }, this.backoff); } }