fix(bex-bridge): a live extension keeps the slot instead of being evicted#372
Merged
Conversation
…cted The bridge holds one socket and every new connection replaced it unconditionally. With the extension installed in two Chrome profiles, both service workers connect to ws://localhost:1646/bex-bridge and evict each other on every reconnect — the loser's backoff brings it straight back, so the slot flaps indefinitely. Each MCP tool call then lands on whichever wallet happens to hold the slot at that instant, with no error surfaced. Observed as bex_tabs and bex_snapshot answering from different profiles (tab ids from one are tab_not_found in the other) and two distinct SW_STARTED_AT values from bex_status vs bex_ext_console. Silent nondeterministic routing of signing requests is the real hazard, not the broken automation. A newcomer now only takes the slot if the incumbent has gone silent for STALE_MS (45s, comfortably above the BEX's 20s heartbeat), which preserves the case this replacement existed for: a service worker restart whose close frame never arrived. A refused instance is closed with 4409 so the BEX can tell "someone else holds this" from an ordinary disconnect. Ceiling: a genuinely dead incumbent still wedges the bridge for up to 45s. Closing that needs a stable per-instance id from the BEX so the vault can tell "same instance reconnecting" from "second instance"; tracked in the BEX handoff.
Any MCP operation that touches a page must be visible in that page, enforced at the executeBrowserTool dispatcher rather than per call site, plus a registry test so a newly added tool cannot silently opt out.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
bex-bridge.tsholds a single socket and every new connection replaced it unconditionally:With the extension installed in two Chrome profiles — the case that surfaced this — both service workers connect to
ws://localhost:1646/bex-bridge, and each connection evicts the other. The loser's reconnect backoff (mcpBridge.ts:245-252, reset to 5s on every successful open) brings it straight back, so the slot flaps forever.Every MCP tool call then lands on whichever wallet holds the slot at that instant. No error is surfaced.
How it was observed
bridge connectedevery ~10s, indefinitely.bex_statusandbex_ext_consolereturned two differentSW_STARTED_ATvalues, and the mapping flipped between calls. Both read the same module constant (providerLog.ts:48), so two values can only mean two service workers.bex_tabslisted tabs from one profile whilebex_snapshotanswered from the other, so tab ids from the first weretab_not_foundin the second (browserTools.ts:266-273).Default,Profile 1), one Chrome process.The broken browser automation is the visible symptom. The hazard is that a signing request can be routed to a nondeterministically chosen wallet.
Change
A newcomer only takes the slot if the incumbent has gone silent for
STALE_MS(45s). Any inbound frame stamps liveness, heartbeat pings included — the BEX pings every 20s (HEARTBEAT_MS), so a live extension is never judged stale.This preserves the case the unconditional replacement existed for: a service worker restart whose close frame never arrived. A refused instance is closed with 4409 so the BEX can distinguish "someone else holds this slot" from an ordinary disconnect.
Tests
bun test src/bun/mcp.test.ts— 26 pass, 0 fail.Three new cases, all verified to fail against the old behaviour (differential run with the guard neutralized: 3 fail / 23 pass):
30001mstimeout, i.e. the existing fast-fail guarantee is retained)Known ceiling
A genuinely dead incumbent still wedges the bridge for up to 45s. Closing that needs a stable per-instance id from the BEX so the vault can tell "same instance reconnecting" from "second instance". That is a BEX-side change and is written up in the handoff doc; the vault side is deliberately not built ahead of it.
Follow-up owned by the BEX
A refused instance currently retries forever against a slot it can never win. It should treat close code 4409 as terminal and surface it. Handoff:
docs/handoff-bex-bridge-slot-eviction.md.