Skip to content

factory: character-level co-editing relay — live documents as opaque Yjs updates (E7) - #2636

Merged
santoshkumarradha merged 4 commits into
work/teamsfrom
w2/e7-docs
Nov 13, 2026
Merged

santoshkumarradha merged 4 commits into
work/teamsfrom
w2/e7-docs

Conversation

@santoshkumarradha

@santoshkumarradha santoshkumarradha commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Bottom line: The organization server now relays and stores the live co-editing documents (Yjs, Google-Docs style) described in WAVE2.md §4.4. It keeps each document as an append-only log of opaque updates and never parses Yjs. Two engines on one real server measured p50 0.56 ms, max 0.95 ms from update to the other engine's relay. Against the real codeaf factory serve binary on localhost: p50 1.3 ms, p95 9 ms, max 113 ms. The target was < 500 ms.

What it does

  • Server (internal/factory/docs, internal/factory/serve/docs.go, wire in internal/factory/api/docs.go). Per document it keeps the log plus the latest client-made snapshot. The server stamps seq/by/at itself. Routes:
    • GET /v1/docs/{doc}?since=
    • POST …/updates {base_seq, update, seed?}
    • PUT …/snapshot {seq, state}
    • POST …/awareness {client, state|null}, with a 30 s TTL
    • GET …/events?since=, a per-doc SSE stream (doc.update with id = seq, doc.snapshot, doc.awareness, doc.removed, doc.lagged)
  • Access goes through E1's can: view reads, follows and sends awareness; edit sends updates and snapshots. ws/<id>/page/<pid> requires pid to be one of the workstream's pages. An archived workstream's pages are read-only. A private page is 404 to the operator. canvas/<id>/frame/<fid> is 404 until E4 calls SetCanvasDocAccess.
  • Engine: hello org_docs. Methods factory.org_doc_read, _send, _snapshot, _aware, _follow and _unfollow. Events factory.org_doc_update, factory.org_doc_awareness and factory.org_doc_closed. The follower keeps the last relayed seq and reopens from it after a drop or doc.lagged, so it never skips an update. The 409 reasons (seeded, reset, snapshot_needed) reach the desk as detail.reason.

Decisions

  • Updates travel on a per-doc stream, not /v1/events. That stream is a durable 200-event window that is fsynced and rewritten on every append, and keystrokes would flush job and workstream events out of it.
  • Compaction: the server keeps going and asks. Past 500 updates or 512 KiB, every answer says compact:true. Past 5000 updates or 2 MiB, new updates get 409 snapshot_needed until a snapshot arrives. A kept snapshot drops the prefix it covers. This bounds both the disk and a newcomer's read, which must fit the engine's 8 MiB answer.
  • Limits:
    • Size: one update ≤ 256 KiB, one snapshot ≤ 2 MiB, one awareness state ≤ 4 KiB.
    • Rate, per person per doc: 30 updates/s (burst 120) and 10 awareness/s (burst 20). Past either: 429 with Retry-After: 1.
  • Seeding: seed:true is accepted only into an empty doc, so two desks opening a page for the first time cannot both insert its body_md.
  • E2 seam (agreed with E2): E2 owns page versions. On 30 s idle, the client that wrote last first PUTs the snapshot here, then POSTs to E2's /pages/{pid}/versions {body_md,state,reason}. Restore answers {body_md,state} and the restorer applies it as a normal update. On page delete, E2 calls h.dropDoc(...).

Contract

I edited WAVE2.md §4 (point 4, "E7 as built") in place at ~/work/teams-wave/WAVE2.md; that file is outside the repo. docs/design/app-v3/PROTOCOL.md has the same contract.

Proof

  • internal/factory/docs covers the store (stamping, snapshot replacing its prefix, seed, ahead/reset, soft and hard compaction, drop) and the hub (a lagging reader is ended rather than skipped, awareness TTL lapse, token buckets, remove).
  • internal/factory/serve/docs_test.go uses the org scene (santosh, abir, mia, operator). It covers access by owner, team, share, view-only, private, archived, hidden and canvas; stamping; snapshots; the rate limit; and a real-HTTP SSE stream with replay, live updates, awareness and drop.
  • cmd/codeaf/appfactory_org_docs_test.go runs two engines (alice, bob) against one real serve.Handler over HTTP. It covers seed refusal with its reason, follow, 50 measured live updates, awareness, snapshot and unfollow.
  • The real binary run was solo mode with one token and two separate HTTP clients. Two people on the real binary needs GitHub sign-in, so the two-person half is covered in-process above with the same handler.

santoshkumarradha and others added 4 commits November 13, 2026 14:36
… Yjs updates

Assisted-by: CodeAF
Co-Authored-By: CodeAF <[email protected]>
…ngine relays them and the protocol says so

Assisted-by: CodeAF
Co-Authored-By: CodeAF <[email protected]>
Assisted-by: CodeAF
Co-Authored-By: CodeAF <[email protected]>
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