Skip to content

fix(bridge): stop the mesh SharedWorker merging patches on top of stale local state - #145

Merged
Mearman merged 1 commit into
mainfrom
fix/mesh-worker-blind-apply
Sep 16, 2026
Merged

Mearman merged 1 commit into
mainfrom
fix/mesh-worker-blind-apply

Conversation

@Mearman

@Mearman Mearman commented Sep 16, 2026

Copy link
Copy Markdown
Member

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.ts itself is still live, real code: instantiated by mesh-client.ts, imported by main.tsx, connected to the server's /ws/mesh endpoint.

The actual defect #52 described (applyPatch reimplementing a naive, partial CRDT merge for agent_upsert/room_upsert) is real: it unions the existing local subscribedRooms/members onto 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 own case "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 real self.addEventListener entry point is guarded so importing the module for testing doesn't throw in a plain Node environment with no SharedWorkerGlobalScope.

Test plan

  • New RED tests confirmed failing against the original merge logic before fixing it
  • pnpm typecheck
  • pnpm lint
  • pnpm test (772/772)
  • pnpm build

…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
Mearman marked this pull request as ready for review September 16, 2026 05:54
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review Completed 2026-09-16T05:59:36.357782Z 0421467 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Mearman
Mearman merged commit 92f352e into main Sep 16, 2026
6 checks passed
@Mearman
Mearman deleted the fix/mesh-worker-blind-apply branch September 16, 2026 06:00
@Mearman Mearman mentioned this pull request Sep 16, 2026
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 3.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant