Repository navigation
fix(realtime): drop an empty collab room when the stored content changes - #756
Conversation
There was a problem hiding this comment.
ℹ️ No critical issues — one doc update and one open question below.
Reviewed changes
Reviewed the complete diff of 118bc8a against master (9e88b2e): the CollabHub room-drop change, its four new tests, and the surrounding join / evict / grace-timer / cluster-lease paths. I also ran the suite on the branch (all four new tests pass; reverting collab.ts to master makes the first three fail as the PR body claims; tsc and biome clean).
- Empty rooms are dropped on an external write —
applyExternalnow destroys and forgets a room with no members instead of ignoring the write, both before the merge and after waiting on the pending editor-update chains. - Identity-checked teardown — the new
dropRoomonly removes the map entry when the name still points at that same room object, and the grace timer clears its own handle before dropping, so a stale timer cannot destroy a newer room. - Timer bookkeeping —
destroyclears and resets both timers;joinstill cancels the grace timer for the room it reuses. - Four new hub tests — drop after the last editor left, drop when the last editor leaves while the write waits on their updates, a join racing the drop, and the negative case that a room with editors is never dropped.
The synchrony argument holds as written: dropRoom has no await, and join only reads this.rooms after its single access-check await, so no join can observe a dropped-but-present room.
ℹ️ The realtime README still promises a room only dies after its 30 s grace period
services/realtime/README.md:305 describes the room lifecycle as "an empty room is dropped 30 s after its last editor leaves", which is now incomplete: an empty room is also dropped immediately when the stored content changes. The PR body says no documentation change is needed, but this is the service's main protocol/behaviour writeup, not just an inline comment.
Technical details
# Update the room-lifetime line in services/realtime/README.md
## Affected sites
- `services/realtime/README.md:305` — "An empty room is dropped 30 s after its last editor leaves and re-seeded from the stored content." now omits the new drop-on-external-write path.
- `services/realtime/README.md:313` — "With no room open nothing happens: the next editor seeds from the stored content." is still accurate; no change needed, but worth reading together with the line above.
## Required outcome
- The persistence bullet states both lifetimes: dropped once the grace period after the last editor expires, or at once if a `doc.updated` / `task.updated` content write lands while the room is empty.ℹ️ The reseed assumes the next joiner brings an empty document
Dropping the room and letting the next editor seed from stored content only preserves the write when that editor starts from an empty Y.Doc. A client that merely lost its socket rejoins with its live doc, and applyJoin pushes whatever the server's room lacks, so it uploads its pre-write copy into the fresh room. This path already exists on master (after the grace expiry or a replica restart); the PR makes room destruction on write more frequent, so it is worth deciding explicitly whether that is acceptable.
Technical details
# Reconnect-after-drop can reintroduce pre-write content
## Affected sites
- `services/realtime/src/collab.ts:506-510`, `:527-531` — the new drop; correctness of the reseed depends on the next joiner having no local content.
- `apps/web/src/lib/collab-session.ts:244` — reconnect re-join sends the live doc's state vector, so `seed` is withheld (`collab.ts:344-356` requires `clientSv.byteLength <= 1`).
- `apps/web/src/lib/collab-session.ts:251-257` — `applyJoin` then emits `encodeStateAsUpdate(this.doc, res.stateVector)` as a `collab:update`, which is the entire stale document when the fresh room is empty.
- `apps/web/src/lib/collab-session.ts:378-395` — `onConnect` runs that re-join after every socket reconnect (`status === "offline"`).
## Required outcome
- Decide whether a reconnecting editor should be allowed to seed a room that was dropped because of an external write, rather than receiving the stored content. Two observable outcomes today: the write is overwritten by the client's next save (when it is the first back), or the room ends up holding the seeded content plus the client's stale copy (when another editor seeded first).
## Open questions for the human
- Is the reconnect-with-live-doc case out of scope for this fix, and tracked separately?
- If not, should the client discard its local doc when it joins a room that was recreated (as opposed to after an owner/lease loss, where the client's copy is the only remaining one), or should the server signal which of the two cases it is?deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
pikann
left a comment
There was a problem hiding this comment.
LGTM! Thank you for the contribution! 🚀

Summary
Fixes a lost update in collaborative editing: a write made through the API (an agent, the MCP server, a push by path) could be overwritten by a browser that reopened the document within 30 seconds of the last editor leaving.
What went wrong
DESTROY_GRACE_MS(30 s), so a quick reload does not have to load it again.applyExternalignored the change because the room had no members, so the room kept the old content.Found while building the web part of #747, where a locked managed copy is pushed by an agent while nobody has it open; it does not depend on that work and happens on
masterwith any API write.The fix
applyExternalnow drops a room that has no members when the stored content changes, instead of returning early. The next person to open the document gets a new room, seeded from what is stored.destroyclears both timers, and the grace timer only removes the room it was set for, so it cannot remove a newer room with the same name.Only
services/realtime/src/collab.tsand its test change. No API, protocol or web change.Type of Change
Testing
services/realtime:bun run lint,bun run check,bun test(151 pass). Four new tests incollab.test.ts:The first three fail without the fix.
Browser specs (
apps/e2e, chromium,tests/docsandtests/realtime) on a stack built with this fix: 210 passed, 6 skipped, 2 failed, 1 passed on retry.docs.spec.ts:350(rename from the sidebar) anddocs.spec.ts:640(edit own comment, usesControl+aon macOS) fail the same way onmaster.collaborative-editing.spec.ts:395failed once before any API write, with both editors in the room, where this change does not act. The whole file run 4 times without retries failed 13 of 28 with the fix and 19 of 28 on unmodifiedmaster, so these specs are racy under parallel load today; run one at a time, spec 395 passed 15 of 15 with the fix.Checklist