Fix per-keystroke render-grid replay loop on iOS, instrument sync latency
Measurement-first pass at iOS↔Mac sync latency. The instrumentation found a structural bug: on screen-anchored render-grid connections (every modern host), every typed keystroke's echo rendered through a full mobile.terminal.replay RPC plus the verified-replay freeze→fence→reveal pipeline instead of the live delta path.
Mechanism of the bug: the live delivery tail records terminalRenderGridHistoryContinuityBySurfaceID for screen-anchored frames, but the replay-response apply path never did. Cold attach always baselines via replay, so continuity started nil, every live delta failed the history chain check (base=0 delivered=nil), requested another replay, which again recorded no continuity — a permanent loop. The Mac side already rebases its emission chain onto served replays (MobileTerminalRenderObserver.adoptReplayBaseline) expecting exactly this contract. Introduced by https://github.com/manaflow-ai/cmux/pull/8860. Trace evidence: 40/40 probe keystrokes hit sync.render_grid_history_chain_break, zero live frames delivered.
Fixes (commit-by-commit, regression test red→green):
fff620d0c3failing tests: replay-applied baseline must let the next linked live delta deliver without a new replay; a fresh input ack must not re-subscribe while events are flowing.359b8d7f81fix: sharedrecordTerminalRenderGridHistoryContinuityhelper called from both the live tail and the replay apply path; input-ack resubscribe (input_seq_wait) now skipped when the last terminal event is under 2 s old (terminalInputAckResubscribeSilenceThreshold), preserving lost-registration repair after silence. The per-ackmobile.events.subscribematters on real-RTT links where acks outrun echo frames.
Instrumentation (all DEBUG-only, off unless CMUX_LATENCY_TRACE=1 or cmux.debug.latency-trace defaults): LAT <stage> t=<uptime_us> stamps across probe.send → host.in.recv → host.in.applied → host.grid → host.write → ev.grid → gate → ap.yield → ap.done → rd.present, plus state-sync emit/apply. CMUX_LATENCY_PROBE=<n>:<ms> types n synthetic keystrokes through the production input path (auto-navigating to the first workspace so headless sims work). scripts/mobile-latency-trace/analyze.py joins both logs (sim and Mac share a clock domain) into the tables below; see scripts/mobile-latency-trace/README.md.
Measured (iPhone 17 Pro Max sim ↔ tagged Mac over QUIC, same host, 40 keystrokes at 250 ms):
| Metric | Before | After |
|---|---|---|
| Echo keystroke→visible p50 | 81 ms | 15.2 ms |
| Echo keystroke→visible p95 | 133 ms | 42.7 ms |
| Live frames delivered at gate | 0 of 43 (all replay) | 41 of 44 (3 = cold attach) |
| History chain breaks | 40/40 keys | 0 |
| State sync Mac emit→iOS applied p50 | — | 0.6 ms |
After-fix echo decomposition (p50): input wire+Mac apply 1.5 ms, echo capture→frame encode 2.6 ms, write+wire+decode+gate+apply 1.5 ms, display-link present 7.9 ms (120 Hz floor). On a physical iPhone the fix additionally removes one full wire RTT (the replay round trip) and ~31 ms of verified-replay freeze/reveal per keystroke.
Remaining known cost, intentionally not touched here: the stop-and-wait input batching RTT (probe.send→host.in.recv p95 29.7 ms when batches queue) is owned by https://github.com/manaflow-ai/cmux/pull/9036, which this PR avoids conflicting with (no CmuxMobileRPC edits).
Verification: CmuxMobileShell 813 tests (only the 3 known pre-existing failures), CmuxMobileShellModel 179 green, analyzer selftest green, live before/after traces above on tag slat.
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Fixes an iOS render-grid continuity bug that forced a full replay on every keystroke; live deltas now link and apply normally. Adds opt-in iOS↔Mac latency tracing with a DEBUG typing probe and analyzer to measure end-to-end echo.
-
Bug Fixes
- Preserve screen-anchored history continuity on both live-delivery and replay-apply paths; fail closed on missing/stale continuity and request replay. Retries catch-up replay when the pending-input gate requires it.
- Defer input-ACK re-subscribe until 2s of terminal-event silence; cancel re-subscribe retries on replay success, fail-open recovery, or retry exhaustion. Thread terminal byte end-sequence through delivery and UI to tighten gating and continuity; tests cover replay-baseline→delta linking, stale-history rejection, ACK recency, and retry behavior.
- Impact: echo p50 81ms → 15.2ms, p95 133ms → 42.7ms; live frames 41/44 (vs 0/43); history chain breaks 0/40.
-
New Features
- DEBUG-only latency stamps across input→host→grid→enqueue→write→receive→gate→apply→present, with queue depth, host-side capture times, and FIFO terminal sequence to join logs by surface identity. Bounded writers on iOS and Mac batch to sinks and report visible drop counts; enable with
CMUX_LATENCY_TRACE=1orcmux.debug.latency-trace. - DEBUG typing probe
CMUX_LATENCY_PROBE=<n>:<ms>with auto-navigation to the first workspace. Analyzer atscripts/mobile-latency-trace/analyze.pywith README; joins iOS/Mac logs, supports same-clock echo decomposition on Simulator, and state-sync emit→apply timing.
- DEBUG-only latency stamps across input→host→grid→enqueue→write→receive→gate→apply→present, with queue depth, host-side capture times, and FIFO terminal sequence to join logs by surface identity. Bounded writers on iOS and Mac batch to sinks and report visible drop counts; enable with
Summary by CodeRabbit
- New Features
- Added opt-in, DEBUG-only latency tracing across the Mac↔iOS workflow, including mobile trace writers and a log analyzer for LAT records.
- Added DEBUG-only mobile latency probing with optional auto-navigation, plus additional latency stamps for grid, input, ACK, and state-sync paths.
- Bug Fixes
- Improved terminal byte sequencing by propagating an optional end-sequence “high-water mark” through live and verified-replay delivery/completion.
- Tightened terminal input ACK resubscribe/retry retry/cancellation handling during replay and delivery transitions.
- Documentation
- Added setup/usage instructions for enabling tracing and running the analyzer.
- Tests
- Expanded render-grid replay staleness and ACK retry tests, plus new liveness and fixtures coverage.