Skip to content

fix(realtime): merge agent writes made with an editor's own account - #727

Merged
pikann merged 3 commits into
Paca-AI:masterfrom
msammu07:fix/collab-echo-by-socket
Nov 12, 2026
Merged

pikann merged 3 commits into
Paca-AI:masterfrom
msammu07:fix/collab-echo-by-socket

Conversation

@msammu07

Copy link
Copy Markdown
Contributor

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:

  • Web: every mutation names the tab's connected realtime socket in X-Collab-Socket (api-client.ts, client-ops.ts, socket-client.ts).
  • API: the header is carried on the request context and stamped on the realtime envelope as 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).
  • Realtime: routeEvent reads collab_socket from the envelope (never from the payload) and passes it to handleContentEvent. applyExternal skips 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

  • Other: bug fix (realtime collaborative editing)

Checklist

  • The change is focused and scoped: echo detection only; the merge itself is unchanged.
  • Related documentation is updated: services/realtime/README.md (REST writes section) and the collaborative-editing.feature scenario.
  • New structure or direction is explained clearly: comments in collab.ts, subscriber.ts and events/collabsocket.go.
  • I avoided unnecessary detail or premature abstraction.

Testing

  • Unit:
    • realtime 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);
    • web vitest src/lib: 580 pass (new: the socket header is sent on mutations only, and not when there is no connected socket);
    • API internal/events and middleware: pass (new: token validation; envelope carries collab_socket; payload and change log don't);
    • tsc / biome clean in realtime, web and e2e (the files touched); go build ./... ok.
  • e2e on the isolated e2e stack (Chromium): collaborative-editing.spec.ts 7/7 pass, including the new scenario "reach the editor even when written with that editor's own account". Against unmodified master, that scenario fails: the agent's text never reaches the editor.
  • Second commit, test only: typeAtStart / typeAtEnd / typeInBlock pressed Ctrl+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 unmodified master too. The helpers now use Cmd+Arrow keys on macOS; Linux keeps the old keys.
  • Not run: the rest of apps/e2e, Firefox / WebKit, the multi-replica sync-replicas spec.

Notes

  • Rolling deploys: a browser tab loaded before this change sends no socket id, so its saves are treated as external writes until it reloads. That is normally a no-op (the room already holds what it saved), but a block deleted after the save could reappear once.
  • Reconnects: a save sent just before a socket reconnect carries the old id, and is merged the same way.

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.

@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 minor documentation note below.

Reviewed changes

  • Echo identified by socket, not account. applyExternal now 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-client adds X-Collab-Socket to non-GET mutations from the tab's connected socket; client-ops / socket-client own the header name and the provider.
  • API keeps it envelope-only. WithCollabSocket carries the header on the request context, and publishRealtime stamps it as collab_socket without adding it to the payload, activity log or change log.
  • Realtime reads it from the envelope. routeEvent ignores payload values and forwards ContentChangeMeta; handleContentEvent / applyExternal use it, and CollabCluster forwards 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.

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

@msammu07

msammu07 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Re: pullfrog review, "Cross-origin deployments need X-Collab-Socket in the CORS allowlist"

Changed: services/realtime/README.md, "Same origin" note, now lists X-Collab-Socket with If-Match / X-Client-Op-Id (0377e1e). Why: that note is the list a cross-origin deployment follows, so the new header belongs in it.

Not changed: the Access-Control-Allow-Headers list in router.go. Why:

  • The web client is always same-origin (API_BASE_URL = window.location.origin in apps/web/src/lib/api-client.ts), so its requests never go through a CORS preflight.
  • corsMiddleware can't serve a cross-origin web client today: it sends no Access-Control-Allow-Credentials for allowed origins, and the client uses cookies (withCredentials: true). X-Client-Op-Id and If-Match aren't listed either. Adding one header wouldn't fix that setup.
  • There is no account-based fallback to fall back to. A preflight that refuses the header blocks the whole request, the same as X-Client-Op-Id today.

@pikann pikann 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.

LGTM! Thank you for the contribution! 🚀

@pikann
pikann merged commit a7353f9 into Paca-AI:master Nov 12, 2026
9 checks passed
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.

2 participants