Whole-branch review of perf/native-realtime-phase1 at 9ef6941 (8e54d3a..9ef6941) — reviewer: Claude Fable (claude-fable-5-2), read-only via git show/git diff; nothing built or run by me, so every finding below is from reading (the server, the web's live-updates.mjs, and the kit's OutboxDrainer at the pinned a18c32b were read alongside).
Verdict: APPROVE AFTER FIXES
Findings 1 and 2 should be fixed before merge (both small). 3 needs a decision, not necessarily code. The rest are low.
Findings
1. Medium — the held announcement's 1 s cap does not work. DocsModel.swift:485-495 with :525-530. When the timer fires it calls releaseHeldRemote → refresh(sqid, remoteSequence: held), and refresh re-enters the same hold branch, because sequence + 1 == held still and postingUpdates still contains the key. It holds again with a fresh 1 s timer, and so on until the POST resolves. Scenario: this client's POST stalls on a bad connection (the stream is a separate connection and still delivers); a peer writes row S+1; that one change is not shown until the POST times out at the transport's request timeout, not after 1 s. No loss (a second peer row arrives as S+2 and is fetched at once; POST failure releases), but the stated bound and "peer changes are never held back" are not true, and no test covers the timeout path (testAHeldEventIsFetchedWhenTheOwnSaveFails covers failure only). Fix: the timeout path must bypass the hold (a parameter to refresh), plus a test with a POST that never answers.
2. Medium — the echo rule trusts sequence, and sequence is not always this document's. The model is reused across documents (DocsWorkspace.swift:57, DocsJumpControls.swift:124, iOS beginDocument :183-187), and sequence is assigned only at DocsModel.swift:287, :503, :519. - (a) New in this branch: open publishes self.replica at :285 and sets sequence only after the await at :287. A sent arriving in that suspension (the drainer is already sending this lane's replayed entries, which are in held) compares against the previous document's sequence. If it matches by coincidence, Task { replica.setSequence(assigned) } raises the replica's durable sequence (it is a max) past rows this replica never fetched, and that is what gets persisted. Narrow, but it is the one path I found where a remote row can be skipped. Fix: read persistentState().sequence before assigning self.replica, or have sent ignore results until open has finished. - (b) Pre-existing, not caused by this branch, but the same root: newDraft (:182-206) and discardAll never reset sequence. On iPhone: open a document at seq 500, tap New. The draft keeps sequence == 500, so after it attaches, refresh's remoteSequence > sequence guard ignores every announcement until row 501 and then asks ?since=500. A collaborator's first 500 rows never arrive in that session. Not reproduced, read only. sequence = 0 in newDraft/discardAll closes it; worth doing here since finding 5's safety rests on this variable.
3. Medium (decision) — fuel: bounded and off the main thread, but the bound per user action is several times "~20 s". native/host/src/lib.rs:25-35, :69-76. - Thread: every docs_host_call is inside actor CRDTReplica (CRDTReplica.swift:15, the only call site). Nothing runs on the main actor, so this cannot beachball as 0.1.7 did. The main actor only awaits. A long call does block that replica's actor, so typing, publish and close for that document queue behind it. - The budget is per call. One keystroke is up to 5 calls (CRDTReplica.command: edit, snapshot, apply, project, snapshot), fresh is 3, apply is 3. Worst case per keystroke is therefore ~5x the ceiling, and the figure is M1 Max only; an iPhone will be slower. The description should say so. - Can a peer or server buy fuel? Yes, linearly, and it stays capped. The request term gives ~1.07G fuel per MiB: a writer is limited to 1 MiB per POST by the server (handlers.go:39), a hostile server to 8 MiB per GET (8.6G, about 10 s). The memory term is persistent: content that inflates linear memory raises the allowance of every later call up to 8.2G. Both are bounded by MAX_FUEL, so the sandbox's guarantee (no unbounded run, trap discards the instance) holds on every entry point: instantiate/start, alloc, free and output reads get BASE_FUEL, the call gets budget, all via set_fuel (no carry-over). - What changed in kind: a failing remote update used to cost ~50 ms and now can cost seconds, and it is retried without limit. After a trap, apply rebuilds from lastGood (CRDTReplica.swift:139-147), sequence does not advance, and the next stream event fetches and applies the same bytes again (DocsModel.swift:497-508). A document with one poisoned row burns up to 3 budgets per announcement, and the editor stalls behind each. Suggest remembering a failed (since, Docs-Seq) and backing off, as a follow-up if not here. - Tests: the out-of-fuel test is meaningful (trap, poisoned instance) and the 200-page check is the real regression test. The new mutation case only mutates the test hook (.min(self.ceiling)), which is fine but proves nothing about the production bound; MAX_FUEL < 20G is the only pin on that. Nothing exercises an 8 MiB request, whose alloc now has only BASE_FUEL (unchanged from main, noted only). I think this is acceptable for phase 1 given the engine branch attacks the cost itself, but Paul should know the worst case is "a document that takes a minute per key on a background thread", not "20 s".
4. Low — a stream can start while the iPhone is in the background. suspendLiveUpdates (DocsModel.swift:539-542) cancels the current stream only. An open that completes after backgrounding (:302) or a draft attaching (reloadMetadata :127-129) starts one anyway. Harmless (it errors on suspension and .active finds streamTask != nil, which is then a dead-or-retrying loop rather than a fresh one). A suspended flag checked at both sites would make it exact.
5. Low — the batch is reserved before the wait. DocsOfflineAccount.swift:253 paces after beginSend and after the merge, so keys typed during the 250 ms wait miss that POST and wait for the next (hence ~0.5 s acknowledgement), the entries show as in flight meanwhile (compaction is refused, correctly), and Save after sendNow can still wait up to 250 ms for the second batch because the woken send has just stamped lastUpdatePost. Durability is unaffected: the outbox write is before all of it. Also, pacing blocks the single drainer, so another document's lane waits behind it. Acceptable; noting it because "Save sends at once" is true only of the first batch.
6. Low — a discard during the wait is still sent. DocsOfflineAccount.swift:253-257: send does not re-check replicaCache.generation(key) after pace. Same race as a discard during a POST on main, widened by up to 250 ms. One guard after pace closes the added part.
7. Nits. held keeps the IDs of compacted-away entries for the session (DocsPersistence.swift:78-79); the four new private properties on the @Observable DocsModel (:45-51) are not read by any body but could be @ObservationIgnored to keep it that way.
Checked and found sound
- Echo skip vs convergence. Server seq is per document, gapless,
MAX+1 in the write transaction, never reused, and every accepted POST inserts a row (store.go ApplyUpdate); the header is set and the hub broadcasts before the 204 (handlers.go:1077-1080). With a truthful sequence, assigned == sequence + 1 means no other row lies between. Peer row before ours: not adopted, fetched. Ours then peer: the S+2 event is not held and fetches since S. Lost response and retry: duplicate row, idempotent. Two windows: neither adopts the other's entries, and a cross-window compaction is adopted by neither; every held error is on the safe side (fetch). Reconnect and server restart: the connect-time event (stream.go:175-181) fetches. One batch is in flight per account (OutboxDrainer), so postingUpdates and the single pacing slot are safe. An out-of-order setSequence Task can only lower-bound, since the actor takes the max. - Durability. Per-keystroke outbox write unchanged and precedes any send; kill during the wait leaves the entries on disk; ordering is the drainer's, unchanged. Status is derived from the outbox, so "Saved" cannot show while entries wait; it only arrives later.
- iOS / SwiftUI. All new calls are in
.onChange/.task, none in init/body; the new account state is @ObservationIgnored. Cancelling the stream raises no error (:470, :508). - Metadata cache. Only
reloadMetadata is cached. Every writer goes through saveMetadata/removeMetadata on the shared account, a failed or nil read does not record the revision, and that path already swallowed errors with try?, so nothing that was visible is hidden. The reads in send (:214) and ensureMetadataEntry (:194) are still live, as described. - Kill harness. Picks the session cookie by name; fine.