| 1 | # Live push: slice plan |
| 2 | |
| 3 | Spec: `../specs/2026-11-26-live-push-design.md` (revision 3, on main bde01ef; |
| 4 | reviewed by Claude Fable 5.2 on oficina/docs #172, APPROVE AFTER FIXES, |
| 5 | fixes applied, no further round). |
| 6 | |
| 7 | **Status:** revision 1, 2026-11-26. Paul: "Let's go". |
| 8 | |
| 9 | **Ground rules for every slice:** |
| 10 | - One worktree and branch per slice, a branch discussion, and a whole-branch |
| 11 | review by the highest available model before the squash merge. |
| 12 | - Hub CI exits 0 before the merge. The Swift and browser checks the hub does |
| 13 | not run are run locally and recorded in the slice's discussion. |
| 14 | - Server slices deploy server-first through the coordinator's canary. Each is |
| 15 | behind its switch (spec §8), so switching it off returns every client to |
| 16 | today's path without a release. |
| 17 | - Each new test was seen to fail for the intended reason (AGENTS.md: break |
| 18 | the rule it holds, watch it go red, read why). |
| 19 | - Measurements are labelled measured or projected. Each slice's numbers go in |
| 20 | its discussion, with the raw summaries under |
| 21 | `native/benchmarks/live-push/results-<date>/`. |
| 22 | |
| 23 | **Order:** 0 → 1a → 1b → 2 → 3. Slices 0 and 1a can share a branch (they |
| 24 | touch different code, and 0 is a test fix). 1b waits for 1a to be proven on |
| 25 | production. 2 waits for 1b's done list (§1b below, the edge check first). |
| 26 | 3 waits for 1b to be live. |
| 27 | |
| 28 | ## Slice 0: restore the native end-to-end probe |
| 29 | |
| 30 | Branch `feat/live-push-1a` (with 1a). Spec §10 slice 0. |
| 31 | |
| 32 | **Work:** `RealtimeAuditE2ETests` observes `projectionGeneration`, not |
| 33 | `projection`. Since S1a, `projection` is `@ObservationIgnored`, so |
| 34 | `testEndToEndRemoteUpdates` saw no publication and reported n=0. |
| 35 | |
| 36 | **Done when:** `run-e2e.py <p1|p20> testEndToEndRemoteUpdates` reports a |
| 37 | "peer POST start -> visible" median with n equal to the updates posted, at |
| 38 | 1 and 20 pages. |
| 39 | |
| 40 | ## Slice 1a: the warm handle (server) |
| 41 | |
| 42 | Branch `feat/live-push-1a`. Spec §3.5 rules 1–8, §8 `DOCS_WARM_HANDLE`. |
| 43 | |
| 44 | **Work:** |
| 45 | 1. `internal/core`: `Handle.RetainedUpdates()` and `Handle.ResidentBytes()` |
| 46 | (linear memory plus retained updates). |
| 47 | 2. `internal/docs/warm.go`: the cache and `applyWarm`. |
| 48 | - **Cache:** a per-document lock; LRU by count (64) and bytes (128 MB); |
| 49 | one handle over the byte cap is not kept; idle 60 s. |
| 50 | - **Sweep:** by every POST and every stream heartbeat; no timers of its |
| 51 | own. |
| 52 | - **Drop:** on `close` (drain and app close). |
| 53 | - **Rules:** |
| 54 | - rule 2: any Apply error drops the handle (a rejection can be half |
| 55 | applied, review #174 finding 1); a full log, trap or recovered |
| 56 | instance also replays cold in the same POST; a cancelled request |
| 57 | does neither quietly; |
| 58 | - rule 3: an applied-but-uncommitted flag closes the handle on every |
| 59 | way out short of a commit, a panic included; seq advances after |
| 60 | commit; a `MAX(seq)` mismatch drops; |
| 61 | - rule 4: snapshot at the handle's seq, Apply, commit, drop; |
| 62 | - release closes an entry that is no longer the cache's (dropped |
| 63 | between acquire's lookup and lock), and removes entries left without |
| 64 | a handle from the map, so they don't count toward the cap. |
| 65 | 3. `Store.ApplyUpdate`: split into `applyCold` (unchanged behaviour) and |
| 66 | `commitUpdate` (the one transaction, returning `MAX(seq)` before the |
| 67 | insert), with `applyWarm` in front when the switch is on. |
| 68 | 4. Rule 6 drops: `ownerMutation` (grants, revoke, visibility), |
| 69 | `ownerDeletion`, `deleteDocument`. Sealing is activated offline, with the |
| 70 | app stopped. |
| 71 | 5. The agent paths' transaction-bound Store copies (`boundTo`) never use warm |
| 72 | handles. Their commit is a savepoint the caller can still roll back. |
| 73 | 6. `$DOCS_WARM_HANDLE` (on/off; unset is off; anything else refuses to |
| 74 | start) in `cmd/docs`, as `Config.WarmHandle`. **Default off** until 1a is |
| 75 | proven on production (spec §8). A follow-up commit makes it on by default |
| 76 | after the canary. |
| 77 | |
| 78 | **Tests (CI):** |
| 79 | - More than 4,096 POSTs to one warm document, none refused, retained updates |
| 80 | ≤ cadence + 2. |
| 81 | - Re-seed order and the next POST's replay from the new snapshot. |
| 82 | - A writer revoked between Apply and commit: the POST fails, the handle is |
| 83 | dropped, and the next snapshot equals a raw replay of the rows it covers. |
| 84 | - Cold fallback on `ErrUpdateLogFull` and on a trap. |
| 85 | - A rejected update drops the handle: the review's half-applying update |
| 86 | (`halfApplyingUpdate`) and undecodable bytes. Neither reaches a snapshot. |
| 87 | - A panic before commit; an entry orphaned between lookup and lock; |
| 88 | handle-less entries not counted; a cancelled request neither retried nor |
| 89 | logged. |
| 90 | - 8 goroutines × 150 POSTs to two documents, with drops and sweeps; log, |
| 91 | cold replay and warm handle agree. |
| 92 | - Team mode with sealed rows: a linked member's POSTs warm and cross the |
| 93 | snapshot cadence through the team branch; a removed member is refused. |
| 94 | - Property test (8 seeds × 60 steps): POSTs, failed commits, cancelled |
| 95 | contexts, injected traps, undecodable and half-applying rejections, drops, |
| 96 | and commits by another Store on the same database. The log always holds exactly the committed updates, |
| 97 | and a warm handle after its own POST equals a raw replay. |
| 98 | - Count and byte caps, LRU order, one over-cap handle not kept. |
| 99 | - Drops on grant, revoke, visibility, delete and drain. |
| 100 | - Heartbeat sweep (fake clock, a real stream). |
| 101 | - `boundTo` has no cache. The switch parses as specified and defaults off. |
| 102 | - **Relative timing gate:** a warm POST is faster than a replayed one on a |
| 103 | ~20-page document, same process, best of three medians. |
| 104 | |
| 105 | **Measurements (local, this branch):** `run-probe.py` with `WARM=0`/`1`, |
| 106 | sealed rows, 1 and 20 pages: |
| 107 | - 1 typist; 5 typists in their own documents; 5 typists + 5 watchers in one |
| 108 | document; 20 typists; |
| 109 | - today's pacing (`native250`, `web250`) and sending at once (`idle50`, for |
| 110 | headroom); |
| 111 | - three repetitions at 20 pages; |
| 112 | - latency at 50 ms RTT; |
| 113 | - warm-handle memory (`TestLivePushProbeWarmMemory`). |
| 114 | |
| 115 | **Race detector:** the gate builds with `CGO_ENABLED=0`, so a dedicated |
| 116 | gate step runs |
| 117 | `CGO_ENABLED=1 go test -race -short -count=1 -run 'Warm|BoundToATransaction|Stream' ./internal/docs/` |
| 118 | (about 2.5 minutes). Run the same command locally before each server |
| 119 | release that touches warm.go or stream.go. |
| 120 | |
| 121 | **Not dropped in 1a, on purpose** (spec §3.5 rule 6): |
| 122 | - team roster changes: retention up to 60 s, not access; a candidate for |
| 123 | 1b; |
| 124 | - version restore: it doesn't exist yet, and must drop the handle when it |
| 125 | does (a note sits at the `updated_at` invariant in `store.go`). |
| 126 | |
| 127 | **Done when:** hub CI green; Fable branch review; canary with |
| 128 | `DOCS_WARM_HANDLE=on`. Then re-run `load-p1`/`load-p20` and the 5 + 5 shared |
| 129 | document against production-like settings, and flip the default on. |
| 130 | |
| 131 | ## Slice 1b: push (server) |
| 132 | |
| 133 | Branch `feat/live-push-1b`. Spec §3.1, §3.2 (server side), §3.4, §3.6 |
| 134 | (`bye`), §5, §8 `DOCS_LIVE_PUSH`. |
| 135 | |
| 136 | **Work:** |
| 137 | 1. **Stream frames.** On `push=1`, `stream.go` sends: |
| 138 | - `hello <stream id> <current seq> <log epoch>`, then rows after |
| 139 | `Last-Event-ID` or `since` as `push`, `own` or `update`; |
| 140 | - without either, today's connect-time `update`. |
| 141 | |
| 142 | Rows are read with `updatesGet`'s query and `openContentUpdate`. Over |
| 143 | 32 KiB, unreadable, or more than 256 rows / 512 KiB behind becomes |
| 144 | `update`, and `sent` moves. There is no `id:` on `update`. |
| 145 | 2. **`Docs-Stream: <id>.<counter>`** on POST. |
| 146 | - The stream id is bound to the session that opened it; a header from |
| 147 | another session is ignored. |
| 148 | - `commitUpdate` records `(doc, seq) → (stream, counter, SHA-256)` inside |
| 149 | the transaction, in the hub's per-document registry (last 64). |
| 150 | - The stream writes `own` only on a matching id and hash. |
| 151 | 3. **Log epoch:** a random value in the database. It is rotated by log |
| 152 | rewinds and at a start without the clean-shutdown marker (written at the |
| 153 | end of a clean drain). It is sent as `Docs-Log-Epoch` on fetch responses. |
| 154 | 4. **`bye ttl | lease | drain`** before those closes. |
| 155 | 5. **Late answers** (spec §5): |
| 156 | - more than 30 updates/s per session per document is delayed, before the |
| 157 | lock or the writer; |
| 158 | - delay at most 1 s, ended by cancel or drain; |
| 159 | - at most 2 waiting per session, beyond that `503` with `Retry-After: 1`. |
| 160 | 6. **Frame cache measurement:** each seq read and unsealed once for all |
| 161 | streams, measured on and off. |
| 162 | 7. `$DOCS_LIVE_PUSH`: off ignores `push=1`. |
| 163 | |
| 164 | **Tests (CI):** spec §9.1's Go list; the §9.2 timing gate (push's |
| 165 | server-added p50 ≤ the fetch path's, best of three). |
| 166 | |
| 167 | **Done when** (spec §10): |
| 168 | - hub CI green, Fable review, canary; |
| 169 | - **Paul's edge check** (`edge-timing.js`) run on production; |
| 170 | - the 5 + 5 shared-document fan-out re-run **in team mode too**, deciding |
| 171 | the frame cache and any authorisation-check throttle; |
| 172 | - **replica bytes per minute of typing** measured on CARLOS' Litestream |
| 173 | version (today's pacing against the 50 ms floor), withdrawing or reopening |
| 174 | question 2. |
| 175 | |
| 176 | **As built, and what later slices inherit** (branch review #175): |
| 177 | - **Team switch-on is gated.** |
| 178 | - Docs' lease retries (four reads, 2 ms apart, per delivery) ride out |
| 179 | only brief contention on roster.Lease's TryLock. |
| 180 | - `DOCS_LIVE_PUSH` stays off on team instances until both: |
| 181 | - Home's roster fix (#158: an RLock or an atomic snapshot) is re-pinned |
| 182 | here; |
| 183 | - the "team streams closed under concurrent editing" measurement (8 of |
| 184 | 10 of today's streams during 300 POSTs) is re-run and shows |
| 185 | none. |
| 186 | - A stream opened on a Member lease still asks the roster up to four |
| 187 | times per delivery, each ask able to wait on an in-flight refresh, |
| 188 | bounded by the delivery's deadline. Revisit with the roster fix. |
| 189 | - **Accepted costs, switch on:** |
| 190 | - one extra row read per empty or snapshot-only fetch (for |
| 191 | `Docs-Seq-Tag`); |
| 192 | - the late-answer limit is per person per document, so one person's |
| 193 | devices and an agent acting as them share 30 updates a second. |
| 194 | - **Client rules slices 2 and 3 must follow:** |
| 195 | - **`resync` closes the stream.** The client resyncs from 0, then opens |
| 196 | a new stream with its new cursor. With `EventSource`, call `close()` |
| 197 | on `resync`, or the browser's own reconnect would loop on it. |
| 198 | - **`update` frames carry no `id:`** (their bytes were not delivered). |
| 199 | So after a fetch, `Last-Event-ID` is older than the client's cursor, |
| 200 | and it wins over `since`. After any fetch, open the next stream with |
| 201 | the fetched cursor as `since` (and its `Docs-Seq-Tag` as a request |
| 202 | header); never rely on `Last-Event-ID` after a fetch. |
| 203 | - **The tag travels in a header** (`Docs-Seq-Tag` on the stream request, |
| 204 | or `Last-Event-ID: <seq>.<tag>`), never the query string. |
| 205 | `EventSource` cannot set request headers. The web client therefore |
| 206 | either streams with `fetch` (a ReadableStream body), or opens without |
| 207 | a tag and accepts the seq-and-epoch check for that one connection. |
| 208 | |
| 209 | ## Slice 2: web |
| 210 | |
| 211 | Branch `feat/live-push-web`. Spec §3.2, §3.3, §3.6, §3.7 (web). |
| 212 | |
| 213 | **Work:** |
| 214 | 1. **`live-updates.mjs`:** |
| 215 | - `push=1&since=<cursor>`; |
| 216 | - frame handling with §3.2's queue rules; |
| 217 | - `own` adoption by counter; |
| 218 | - the epoch check; |
| 219 | - §3.6's visible-tab rule: 8 h since local activity or frames; reopen |
| 220 | after any end (`bye` choosing the delay); fetch first; stop on |
| 221 | 401/403/404; backoff capped at 60 s; the same on non-push streams. |
| 222 | 2. **`save-queue.mjs`:** the leading-edge send with the 50 ms floor, |
| 223 | switched on by `hello` per connection; 250 ms on `saveData` or cellular. |
| 224 | 3. **`editor-entry.js`:** wire it; `Docs-Stream` on saves. |
| 225 | 4. Rebuild the bundle (AGENTS.md: the bundle is never merged by hand). |
| 226 | |
| 227 | **Tests:** spec §9.1's JS list with fake clocks; `scripts/browser-drive.sh`; |
| 228 | the release step (§9.3) with real browsers at 0 and 50 ms. |
| 229 | |
| 230 | **Done when:** 1b's done list is complete (edge check first); review; |
| 231 | deployed with a server deploy. |
| 232 | |
| 233 | ## Slice 3: native |
| 234 | |
| 235 | Branch `feat/live-push-native`. Spec §3.2–§3.4, §3.7, §3.8 (native). |
| 236 | Independent of S1b. |
| 237 | |
| 238 | **Work:** |
| 239 | 1. **`DocsModel.listen`:** `push=1&since=`, the frame handling and queue |
| 240 | rules, `own` adoption, the epoch check. |
| 241 | 2. **Apply frames as `refresh` does,** without its GET. Coalesce queued |
| 242 | frames into one `apply`. |
| 243 | 3. **`DocsOfflineAccount`:** |
| 244 | - stream-id registration per lane; |
| 245 | - `Docs-Stream` only when the open model holds the whole batch; |
| 246 | - `sendInterval` 50 ms after `hello` (250 ms on `isExpensive` or |
| 247 | `isConstrained`); |
| 248 | - `pace` stops holding the drainer when the lane is non-empty. |
| 249 | |
| 250 | **Tests:** spec §9.1's Swift list (fake clock); `RealtimeSyncTests`; |
| 251 | `run-e2e.py` (slice 0); `run-clients.sh`. |
| 252 | |
| 253 | **Done when:** review; a native release (Mac and TestFlight) after 1b is |
| 254 | live, with Paul's go-ahead; the release step (§9.3), with today's path and |
| 255 | push interleaved and recorded. |
| 256 | |