Skip to content

fix(realtime): ignore a late save from an editor socket that already left the room - #824

Merged
pikann merged 2 commits into
masterfrom
fix/collab-late-save-duplicates
Nov 23, 2026
Merged

pikann merged 2 commits into
masterfrom
fix/collab-late-save-duplicates

Conversation

@pikann

@pikann pikann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Problem

On an unstable connection, a doc save can stall in flight while the editor's socket is dropped and replaced. When the save finally lands, its X-Collab-Socket names a socket that is no longer in the room, so applyExternal treats it as an outside (agent) write and merges the older snapshot into the room. The sentences typed since are inserted a second time, and the duplicate is persisted.

Fix

Each room remembers sockets that left it for 10 minutes (capped at 50). A save naming one of them is treated as the editor's own and ignored. A socket that never joined the room, or left longer ago, is still merged, so agent/MCP writes are unchanged.

Testing

  • Reproduced in a browser (Playwright, e2e stack) by delaying one save and sending a stale socket id while editing: duplicate block, persisted across reload.
  • New unit tests in collab.test.ts (late save from a departed socket; expiry after the TTL). bun test for collab, collab-cluster and collab-blocks: 75 pass. biome and tsc clean.
  • Not verified: the new tests failing without the fix (the stashed run errored on a missing import), and the fix against the browser repro.

…left the room

A save stalled on a bad link can land after its socket was dropped and
replaced. Its X-Collab-Socket named a socket no longer in the room, so it
was merged as an outside write and inserted a second time next to what the
editor typed since. Rooms now remember departed sockets for 10 minutes and
treat their saves as the editor's own.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one documentation gap inline.

Approach: sound — the save is identified by the editor's socket rather than the account, and a bounded departed set extends that to the reconnect case without changing how agent/MCP writes are merged.

Reviewed changes — read the full diff at a645932f and traced the merge path through applyExternal, the subscriber's envelope stamping and the cluster forwarding:

  • Departed-socket tracking — each room keeps a departed map of socket ids with a 10 minute TTL and a 50-entry cap, pruned oldest-first whenever a member leaves (any evict path, including revocation and cluster sweeps).
  • Echo check widened — applyExternal now drops a save naming a socket that left within the window instead of merging it; sockets that never joined or aged out still merge, so agent/MCP writes are unaffected.
  • Unit coverage — one case for the late save from a departed socket, one for a save past the TTL. I ran the suite locally: 38/38 pass, and the first new case fails when isEditorSocket is narrowed back to a membership check. biome check and tsc --noEmit are clean.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Comment thread services/realtime/src/collab.ts Resolved
@pikann
pikann merged commit c047c69 into master Nov 23, 2026
2 checks passed
@pikann
pikann deleted the fix/collab-late-save-duplicates branch November 23, 2026 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant