oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

Live push: slice plan

Spec: ../specs/2026-11-26-live-push-design.md (revision 3, on main bde01ef; reviewed by Claude Fable 5.2 on oficina/docs #172, APPROVE AFTER FIXES, fixes applied, no further round).

Status: revision 1, 2026-11-26. Paul: "Let's go".

Ground rules for every slice:

  • One worktree and branch per slice, a branch discussion, and a whole-branch review by the highest available model before the squash merge.
  • Hub CI exits 0 before the merge. The Swift and browser checks the hub does not run are run locally and recorded in the slice's discussion.
  • Server slices deploy server-first through the coordinator's canary. Each is behind its switch (spec §8), so switching it off returns every client to today's path without a release.
  • Each new test was seen to fail for the intended reason (AGENTS.md: break the rule it holds, watch it go red, read why).
  • Measurements are labelled measured or projected. Each slice's numbers go in its discussion, with the raw summaries under native/benchmarks/live-push/results-<date>/.

Order: 0 → 1a → 1b → 2 → 3. Slices 0 and 1a can share a branch (they touch different code, and 0 is a test fix). 1b waits for 1a to be proven on production. 2 waits for 1b's done list (§1b below, the edge check first). 3 waits for 1b to be live.

Slice 0: restore the native end-to-end probe

Branch feat/live-push-1a (with 1a). Spec §10 slice 0.

Work: RealtimeAuditE2ETests observes projectionGeneration, not projection. Since S1a, projection is @ObservationIgnored, so testEndToEndRemoteUpdates saw no publication and reported n=0.

Done when: run-e2e.py <p1|p20> testEndToEndRemoteUpdates reports a "peer POST start -> visible" median with n equal to the updates posted, at 1 and 20 pages.

Slice 1a: the warm handle (server)

Branch feat/live-push-1a. Spec §3.5 rules 1–8, §8 DOCS_WARM_HANDLE.

Work:

  1. internal/core: Handle.RetainedUpdates() and Handle.ResidentBytes() (linear memory plus retained updates).
  2. internal/docs/warm.go: the cache and applyWarm. - Cache: a per-document lock; LRU by count (64) and bytes (128 MB); one handle over the byte cap is not kept; idle 60 s. - Sweep: by every POST and every stream heartbeat; no timers of its own. - Drop: on close (drain and app close). - Rules: - rule 2: any Apply error drops the handle (a rejection can be half applied, review #174 finding 1); a full log, trap or recovered instance also replays cold in the same POST; a cancelled request does neither quietly; - rule 3: an applied-but-uncommitted flag closes the handle on every way out short of a commit, a panic included; seq advances after commit; a MAX(seq) mismatch drops; - rule 4: snapshot at the handle's seq, Apply, commit, drop; - release closes an entry that is no longer the cache's (dropped between acquire's lookup and lock), and removes entries left without a handle from the map, so they don't count toward the cap.
  3. Store.ApplyUpdate: split into applyCold (unchanged behaviour) and commitUpdate (the one transaction, returning MAX(seq) before the insert), with applyWarm in front when the switch is on.
  4. Rule 6 drops: ownerMutation (grants, revoke, visibility), ownerDeletion, deleteDocument. Sealing is activated offline, with the app stopped.
  5. The agent paths' transaction-bound Store copies (boundTo) never use warm handles. Their commit is a savepoint the caller can still roll back.
  6. $DOCS_WARM_HANDLE (on/off; unset is off; anything else refuses to start) in cmd/docs, as Config.WarmHandle. Default off until 1a is proven on production (spec §8). A follow-up commit makes it on by default after the canary.

Tests (CI):

  • More than 4,096 POSTs to one warm document, none refused, retained updates ≤ cadence + 2.
  • Re-seed order and the next POST's replay from the new snapshot.
  • A writer revoked between Apply and commit: the POST fails, the handle is dropped, and the next snapshot equals a raw replay of the rows it covers.
  • Cold fallback on ErrUpdateLogFull and on a trap.
  • A rejected update drops the handle: the review's half-applying update (halfApplyingUpdate) and undecodable bytes. Neither reaches a snapshot.
  • A panic before commit; an entry orphaned between lookup and lock; handle-less entries not counted; a cancelled request neither retried nor logged.
  • 8 goroutines × 150 POSTs to two documents, with drops and sweeps; log, cold replay and warm handle agree.
  • Team mode with sealed rows: a linked member's POSTs warm and cross the snapshot cadence through the team branch; a removed member is refused.
  • Property test (8 seeds × 60 steps): POSTs, failed commits, cancelled contexts, injected traps, undecodable and half-applying rejections, drops, and commits by another Store on the same database. The log always holds exactly the committed updates, and a warm handle after its own POST equals a raw replay.
  • Count and byte caps, LRU order, one over-cap handle not kept.
  • Drops on grant, revoke, visibility, delete and drain.
  • Heartbeat sweep (fake clock, a real stream).
  • boundTo has no cache. The switch parses as specified and defaults off.
  • Relative timing gate: a warm POST is faster than a replayed one on a ~20-page document, same process, best of three medians.

Measurements (local, this branch): run-probe.py with WARM=0/1, sealed rows, 1 and 20 pages:

  • 1 typist; 5 typists in their own documents; 5 typists + 5 watchers in one document; 20 typists;
  • today's pacing (native250, web250) and sending at once (idle50, for headroom);
  • three repetitions at 20 pages;
  • latency at 50 ms RTT;
  • warm-handle memory (TestLivePushProbeWarmMemory).

Race detector: the gate builds with CGO_ENABLED=0, so a dedicated gate step runs CGO_ENABLED=1 go test -race -short -count=1 -run 'Warm|BoundToATransaction|Stream' ./internal/docs/ (about 2.5 minutes). Run the same command locally before each server release that touches warm.go or stream.go.

Not dropped in 1a, on purpose (spec §3.5 rule 6):

  • team roster changes: retention up to 60 s, not access; a candidate for 1b;
  • version restore: it doesn't exist yet, and must drop the handle when it does (a note sits at the updated_at invariant in store.go).

Done when: hub CI green; Fable branch review; canary with DOCS_WARM_HANDLE=on. Then re-run load-p1/load-p20 and the 5 + 5 shared document against production-like settings, and flip the default on.

Slice 1b: push (server)

Branch feat/live-push-1b. Spec §3.1, §3.2 (server side), §3.4, §3.6 (bye), §5, §8 DOCS_LIVE_PUSH.

Work:

  1. Stream frames. On push=1, stream.go sends: - hello <stream id> <current seq> <log epoch>, then rows after Last-Event-ID or since as push, own or update; - without either, today's connect-time update.

Rows are read with updatesGet's query and openContentUpdate. Over 32 KiB, unreadable, or more than 256 rows / 512 KiB behind becomes update, and sent moves. There is no id: on update.

  1. Docs-Stream: <id>.<counter> on POST. - The stream id is bound to the session that opened it; a header from another session is ignored. - commitUpdate records (doc, seq) → (stream, counter, SHA-256) inside the transaction, in the hub's per-document registry (last 64). - The stream writes own only on a matching id and hash.
  2. Log epoch: a random value in the database. It is rotated by log rewinds and at a start without the clean-shutdown marker (written at the end of a clean drain). It is sent as Docs-Log-Epoch on fetch responses.
  3. bye ttl | lease | drain before those closes.
  4. Late answers (spec §5): - more than 30 updates/s per session per document is delayed, before the lock or the writer; - delay at most 1 s, ended by cancel or drain; - at most 2 waiting per session, beyond that 503 with Retry-After: 1.
  5. Frame cache measurement: each seq read and unsealed once for all streams, measured on and off.
  6. $DOCS_LIVE_PUSH: off ignores push=1.

Tests (CI): spec §9.1's Go list; the §9.2 timing gate (push's server-added p50 ≤ the fetch path's, best of three).

Done when (spec §10):

  • hub CI green, Fable review, canary;
  • Paul's edge check (edge-timing.js) run on production;
  • the 5 + 5 shared-document fan-out re-run in team mode too, deciding the frame cache and any authorisation-check throttle;
  • replica bytes per minute of typing measured on CARLOS' Litestream version (today's pacing against the 50 ms floor), withdrawing or reopening question 2.

As built, and what later slices inherit (branch review #175):

  • Team switch-on is gated. - Docs' lease retries (four reads, 2 ms apart, per delivery) ride out only brief contention on roster.Lease's TryLock. - DOCS_LIVE_PUSH stays off on team instances until both: - Home's roster fix (#158: an RLock or an atomic snapshot) is re-pinned here; - the "team streams closed under concurrent editing" measurement (8 of 10 of today's streams during 300 POSTs) is re-run and shows none. - A stream opened on a Member lease still asks the roster up to four times per delivery, each ask able to wait on an in-flight refresh, bounded by the delivery's deadline. Revisit with the roster fix.
  • Accepted costs, switch on: - one extra row read per empty or snapshot-only fetch (for Docs-Seq-Tag); - the late-answer limit is per person per document, so one person's devices and an agent acting as them share 30 updates a second.
  • Client rules slices 2 and 3 must follow: - resync closes the stream. The client resyncs from 0, then opens a new stream with its new cursor. With EventSource, call close() on resync, or the browser's own reconnect would loop on it. - update frames carry no id: (their bytes were not delivered). So after a fetch, Last-Event-ID is older than the client's cursor, and it wins over since. After any fetch, open the next stream with the fetched cursor as since (and its Docs-Seq-Tag as a request header); never rely on Last-Event-ID after a fetch. - The tag travels in a header (Docs-Seq-Tag on the stream request, or Last-Event-ID: <seq>.<tag>), never the query string. EventSource cannot set request headers. The web client therefore either streams with fetch (a ReadableStream body), or opens without a tag and accepts the seq-and-epoch check for that one connection.

Slice 2: web

Branch feat/live-push-web. Spec §3.2, §3.3, §3.6, §3.7 (web).

Work:

  1. live-updates.mjs: - push=1&since=<cursor>; - frame handling with §3.2's queue rules; - own adoption by counter; - the epoch check; - §3.6's visible-tab rule: 8 h since local activity or frames; reopen after any end (bye choosing the delay); fetch first; stop on 401/403/404; backoff capped at 60 s; the same on non-push streams.
  2. save-queue.mjs: the leading-edge send with the 50 ms floor, switched on by hello per connection; 250 ms on saveData or cellular.
  3. editor-entry.js: wire it; Docs-Stream on saves.
  4. Rebuild the bundle (AGENTS.md: the bundle is never merged by hand).

Tests: spec §9.1's JS list with fake clocks; scripts/browser-drive.sh; the release step (§9.3) with real browsers at 0 and 50 ms.

Done when: 1b's done list is complete (edge check first); review; deployed with a server deploy.

Slice 3: native

Branch feat/live-push-native. Spec §3.2–§3.4, §3.7, §3.8 (native). Independent of S1b.

Work:

  1. DocsModel.listen: push=1&since=, the frame handling and queue rules, own adoption, the epoch check.
  2. Apply frames as refresh does, without its GET. Coalesce queued frames into one apply.
  3. DocsOfflineAccount: - stream-id registration per lane; - Docs-Stream only when the open model holds the whole batch; - sendInterval 50 ms after hello (250 ms on isExpensive or isConstrained); - pace stops holding the drainer when the lane is non-empty.

Tests: spec §9.1's Swift list (fake clock); RealtimeSyncTests; run-e2e.py (slice 0); run-clients.sh.

Done when: review; a native release (Mac and TestFlight) after 1b is live, with Paul's go-ahead; the release step (§9.3), with today's path and push interleaved and recorded.