fix(bridge): stop the mesh SharedWorker merging patches on top of stale local state - #145
Merged
Merged
Conversation
…le local state The worker's own applyPatch reimplemented a partial CRDT merge for agent_upsert/room_upsert -- unioning the existing local subscribedRooms/ members onto whatever the server sent, rather than replacing them. Since the worker holds a single WebSocket connection to exactly one server, and the server has already computed the fully-merged, authoritative record (delivery-engine.ts's own version-gated merge) before ever broadcasting a patch, this local merge could only make things worse: it silently resurrected a room or member the server had already removed, because the worker has no version field of its own to know its local copy might already be stale. applyPatch now overwrites directly from whatever the patch carries, with no merge attempt. agent_offline, room_delete, message_add, dm_add, and message_read are untouched -- none of them had this problem. The pure reducer functions (applyPatch, applyStateSync, getStateSnapshot, the four state Maps) are now exported and covered by real unit tests -- this file had none before. The module's own self.addEventListener registration is now guarded on self actually existing, since importing the module directly for testing has no SharedWorkerGlobalScope the way a real browser environment does.
Mearman
marked this pull request as ready for review
September 16, 2026 05:54
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Closed
|
🎉 This PR is included in version 3.0.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
Part of #52.
Investigated
mesh-worker.ts(the web bridge's browser-side SharedWorker) against #52's own claim that it needed deleting rather than migrating.relay-worker.ts/relay-client.ts, the other file #52 names, are already gone -- no trace of them anywhere in the tree, so that half of #52 is already done.mesh-worker.tsitself is still live, real code: instantiated bymesh-client.ts, imported bymain.tsx, connected to the server's/ws/meshendpoint.The actual defect #52 described (
applyPatchreimplementing a naive, partial CRDT merge foragent_upsert/room_upsert) is real: it unions the existing localsubscribedRooms/membersonto whatever the server sends, rather than replacing them. Confirmed by writing a test against the original code before fixing it (then reverting to the fix) -- the old logic silently resurrects a room or member the server already removed, since the worker has no version field of its own and blindly assumes a merge is always safe.The fix isn't "delete the file" -- the worker is genuinely needed (it owns the single shared WebSocket connection across browser tabs and fans out state to each tab). What needed deleting was the merge logic specifically: the worker holds exactly one connection to exactly one server, and the server has already computed the fully-merged, authoritative record (
delivery-engine.ts's own version-gated CRDT merge) before ever broadcasting a patch -- so the worker doesn't need to merge anything, it needs to overwrite.MeshClient's owncase "patch"handler on the main thread already treats the worker's state as a black box it re-fetches wholesale after every patch (this.postToWorker({ type: "getState" })), confirming the plan's own framing that "the client already treats the server's own state as authoritative."This file had zero tests before this PR. The pure reducer functions (
applyPatch,applyStateSync,getStateSnapshot, the four state Maps) are now exported and directly unit-tested; the module's realself.addEventListenerentry point is guarded so importing the module for testing doesn't throw in a plain Node environment with noSharedWorkerGlobalScope.Test plan
pnpm typecheckpnpm lintpnpm test(772/772)pnpm build