Repository navigation
fix(realtime): merge agent writes made with an editor's own account - #727
Conversation
The collaborative-editing hub skipped merging a REST write into a live room whenever the writer's account was a member of that room, taking it for that editor's own save coming back. A personal API key acts as its owner, so an agent or MCP write made with the user's key while the user had the doc open was never merged, and the browser's next save overwrote it. The echo is now recognised by the editor's socket instead of the account: the web client names its connected realtime socket on every mutation (X-Collab-Socket), the API passes it on as the realtime envelope's collab_socket (not the payload, activity log or change log), and the hub skips a write only when that socket is a member of the room. Writes without it are always merged. Remote members of a room on another replica join the owner's hub under their own socket id, so the check works across replicas. Refs Paca-AI/paca discussion 722.
typeAtStart / typeAtEnd / typeInBlock pressed Ctrl+Home, Ctrl+End and End. In Chromium on macOS those do not move the caret to the start or end of a contenteditable, so text typed "at the start" landed in the second paragraph and two agent-merge tests failed before the agent wrote anything (also on unmodified master). Use Cmd+ArrowUp, Cmd+ArrowDown and Cmd+ArrowRight on macOS; Linux keeps the old keys.
There was a problem hiding this comment.
ℹ️ No critical issues — one minor documentation note below.
Reviewed changes
- Echo identified by socket, not account.
applyExternalnow merges a REST write unless the request names a realtime socket that is a member of the room, so an agent/MCP write made with the editor's own API key is no longer discarded. - Web stamps the socket.
api-clientaddsX-Collab-Socketto non-GET mutations from the tab's connected socket;client-ops/socket-clientown the header name and the provider. - API keeps it envelope-only.
WithCollabSocketcarries the header on the request context, andpublishRealtimestamps it ascollab_socketwithout adding it to the payload, activity log or change log. - Realtime reads it from the envelope.
routeEventignores payload values and forwardsContentChangeMeta;handleContentEvent/applyExternaluse it, andCollabClusterforwards it across replicas. - Tests and docs. New realtime, web and API tests cover the own-account / non-member cases and the envelope-versus-payload split; the e2e spec gains the own-account scenario and the caret helpers use macOS keys. The README REST-writes section and the feature file are updated.
ℹ️ Cross-origin deployments need X-Collab-Socket in the CORS allowlist
The new request header is not listed in the same-origin note at services/realtime/README.md:248, which already enumerates the request headers a cross-origin deployment must allow (If-Match, X-Client-Op-Id). The API's CORS allowlist in services/api/internal/transport/http/router/router.go:1337 has the same gap, so a cross-origin deployment following that note would drop the header at preflight and fall back to the old account-based echo check.
Technical details
# Cross-origin header allowlist
## Affected sites
- `services/realtime/README.md:248` — same-origin note lists request headers but not `X-Collab-Socket`.
- `services/api/internal/transport/http/router/router.go:1337` — `Access-Control-Allow-Headers` does not include it (nor `X-Client-Op-Id`, which the note already calls out).
## Required outcome
- A cross-origin deployment that follows the README's instructions can send `X-Collab-Socket`, so the echo check keeps working.deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
|
Re: pullfrog review, "Cross-origin deployments need Changed: Not changed: the
|
pikann
left a comment
There was a problem hiding this comment.
LGTM! Thank you for the contribution! 🚀

Summary
The collaborative-editing hub skipped merging a REST write into a live room whenever the writer's account was a member of that room, treating it as that editor's own save coming back. A personal API key acts as its owner, so a write by an agent or MCP using the user's key, made while that user had the doc open, was never merged into the room. The browser's next save then overwrote it. In a single-user setup with agents (the common case behind discussion https://github.com/orgs/Paca-AI/discussions/722), every agent write to an open doc was lost.
The echo is now recognised by the editor's socket instead of the account:
X-Collab-Socket(api-client.ts,client-ops.ts,socket-client.ts).collab_socket. It is not added to the event payload, the activity log or the change log (events/collabsocket.go,authz.go,middleware/authn.go).routeEventreadscollab_socketfrom the envelope (never from the payload) and passes it tohandleContentEvent.applyExternalskips a write only when that socket is currently a member of the room; writes without it are always merged. A remote member on another replica joins the owner's hub under its own socket id, so the check works across replicas.Follow-up to #723 / #724 / #726. It is the first, self-contained piece of the docs push/pull work discussed in #722.
Type of Change
Checklist
services/realtime/README.md(REST writes section) and thecollaborative-editing.featurescenario.collab.ts,subscriber.tsandevents/collabsocket.go.Testing
bun test: 147 pass (new: an own-account write outside the room is merged; a non-member socket is merged; the envelope socket is passed and payload values are ignored);src/lib: 580 pass (new: the socket header is sent on mutations only, and not when there is no connected socket);internal/eventsandmiddleware: pass (new: token validation; envelope carriescollab_socket; payload and change log don't);tsc/biomeclean in realtime, web and e2e (the files touched);go build ./...ok.collaborative-editing.spec.ts7/7 pass, including the new scenario "reach the editor even when written with that editor's own account". Against unmodifiedmaster, that scenario fails: the agent's text never reaches the editor.typeAtStart/typeAtEnd/typeInBlockpressedCtrl+Home/Ctrl+End/End. On macOS Chromium these don't move the caret in a contenteditable, so "next to a live edit" and "add a block without ids exactly once" failed on macOS, on unmodifiedmastertoo. The helpers now useCmd+Arrowkeys on macOS; Linux keeps the old keys.apps/e2e, Firefox / WebKit, the multi-replicasync-replicasspec.Notes