Repository navigation
fix(realtime): ignore a late save from an editor socket that already left the room - #824
Merged
Merged
Conversation
…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.
Contributor
There was a problem hiding this comment.
ℹ️ 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
departedmap of socket ids with a 10 minute TTL and a 50-entry cap, pruned oldest-first whenever a member leaves (anyevictpath, including revocation and cluster sweeps). - Echo check widened —
applyExternalnow 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
isEditorSocketis narrowed back to a membership check.biome checkandtsc --noEmitare clean.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
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
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-Socketnames a socket that is no longer in the room, soapplyExternaltreats 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
collab.test.ts(late save from a departed socket; expiry after the TTL).bun testfor collab, collab-cluster and collab-blocks: 75 pass. biome and tsc clean.