Make iOS terminal scrolling local and buttery with screen-anchored render grids
Scrolling on the phone was slow, non-smooth, and blinked during fast scrolls because every scroll frame round-tripped to the Mac: the RPC scrolled the Mac's real (shared) viewport, the row diff saw every row shifted and emitted a full-viewport clear+repaint delta, and the phone applied each one through the serialized freeze -> VT apply -> Metal fence -> reveal pipeline. Rejections opened replay barriers that dropped deltas until a full replay, and 2s timeouts rebuilt the surface blank. On top of that, a fork renderer bug froze presented pixels whenever renders had no other main-thread layer activity.
This makes the primary screen scroll like the Mac app: shared screen state, per-device viewport.
Ghostty fork (branch render-grid-screen-anchor, two commits):
- Screen-anchored render-grid export: frames anchor to the active area with
history_rows+row_space_revisionmetrics, independent of the Mac's scroll position (ghostty_surface_render_grid_json_v2). - Swap-chain slot rotation: iOS
render_nowis fully serial, so the first-free scan handed every frame the same IOSurface and Core Animation deduped the same-objectcontentsassignment - presented pixels froze while the terminal state advanced. Diagnosed by tracing per-frame: viewport pin marched at 60Hz withredraw=truewhilelayer.contentsidentity never changed across 250 consecutive presents. This bug also explains long-standing "renders too late" complaints on the phone.
Mac producer: connections negotiate render_grid_anchor at subscribe; the observer emits per-anchor payloads so old clients keep v1 viewport mirroring byte-for-byte. Screen-anchored deltas turn history growth into exact scrolled_rows via a shifted row-signature diff guarded by the row-space revision (eviction/reflow falls back to in-place repaint); bursts re-export the missed history rows; mobile.terminal.replay honors anchor + max_scrollback_rows; each served replay is adopted as the delta-emission baseline so chains stay exact mid-stream.
iOS consumer: primary-screen scrolling applies only to the local mirror - no RPC, no Mac viewport movement (alt screen still forwards wheel input to TUIs). Scroll deltas replay as a scroll prologue (line feeds at the bottom row push rows into the mirror's own scrollback; bursts flow missed rows through the grid) then repaint only changed rows. Cold attach hydrates 4000 scrollback rows in one replay; steady-state fulls are history-preserving in-place repaints (no ESC[3J, no flow) so resets never destroy local history or yank a scrolled viewport. Screen-anchored primary deltas skip the per-frame verified fence (fulls keep it); scrolling deltas are never queue-superseded; each delta chain-links the producer's previous history count and any miss triggers a full replay instead of silent misalignment. Verified read-back exports the active area for screen-anchored frames so a scrolled viewport cannot fail verification. Mirror scrollback limit raised 2MB -> 8MB.
Verified live on simulator (iPhone 17 Pro, 10k-line seeded session, tag sscrl):
- Fast flicks sweep the entire 4000-row hydrated history (offset 3999 -> 0) at 60Hz with pixels tracking every frame; video frame analysis: 113 frames, 0 blank frames.
- Zero
mobile.terminal.scrollRPCs and zero chain breaks during scrolling; the Mac's own viewport never moves. - Streaming while scrolled up: view pixel-stable, no yanking; typing still snaps to bottom (unchanged).
- 292 CMUXMobileCore tests pass, including new coverage: scroll-detect diff, revision-guard fallback, burst re-export, scroll-prologue and history-preserving replay bytes, coding round-trip.
Principled or hacky: principled - the protocol now models what scrolling is (screen state vs. viewport presentation), and both renderer fixes remove whole failure classes rather than patching symptoms. The one bounded compromise: bursts larger than 2000 missed rows leave a gap in local scrollback (like a detached terminal), logged and chain-checked.
Residual risk / follow-up: a pre-existing shared-grid autofit negotiation loop (rows oscillating 53<->54 with zoom.viewport.noEffective retries) can keep replay barriers armed after attach; while it lasts, live-delta scrollback growth idles (the mechanism is fully unit- and producer-verified). That loop also churns today's verified mode and deserves its own issue/fix.
Localization audit: no user-facing strings changed (debug-only logging and protocol/rendering code).
🤖 Generated with Claude Code
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Make iOS terminal scrolling local and smooth with screen‑anchored render grids. Adds a scrollback depth picker (1k/4k/10k/20k, default 4k), raises the iOS limit to 16MB, hardens per‑-connection anchor negotiation, snapshots anchors once per flush, and pins a prebuilt GhosttyKit for the fork SHA to stabilize CI.
-
New Features
- Local primary‑screen scrolling; no scroll RPCs. Alt screen still forwards wheel input.
- Per‑connection anchor negotiation; Mac emits v1 (viewport) or v2 (screen) payloads (
terminal.render_grid.screen_anchor.v1). Anchor registry is a lock‑protected singleton. - Screen‑anchored deltas carry
scrolled_rows,history_rows, androw_space_revision; continuity is chain‑checked; misses trigger a full replay; scrolling deltas are never superseded. - Replays hydrate up to the user‑selected rows; Settings adds “Terminal Scrollback” (1k/4k/10k/20k, default 4k). Mac honors
anchor+max_scrollback_rowsand clamps to 20k. Verified read‑back uses the active area for screen‑anchored frames. iOS byte cap increased to 16MB. - Bounded, synchronous event fan‑out for multi‑anchor render grids; anchors are snapshotted once per flush; pre‑encoded fast path kept for both anchors.
-
Bug Fixes
- Fixed frozen iOS presents by rotating swap‑chain IOSurfaces in
ghostty. - Screen‑anchored full replays without scrollback repaint in place to preserve local history and avoid blink.
- Screen deltas that lack
delta_base_history_rowsnow fail closed and request a full replay. - Local scroll suppression now gates on a confirmed primary screen; unknown/stale screens forward to the Mac so alt‑screen wheel input isn’t dropped.
- Guarded empty
XCODE_AUTH_ARGSexpansion inios/scripts/reload.shto prevent bash 3.2 set ‑u crashes. - Pinned prebuilt
GhosttyKitarchive for submodulef11159ff4to fix CI/builder failures. - Fixed CI build by passing
anchorinVerifiedReplaySurfaceReadtest construction; also formed the owned‑userdata release callback as a literal closure for newer Swift toolchains. - Converged with main’s closure‑literal toolchain fix.
- Fixed frozen iOS presents by rotating swap‑chain IOSurfaces in
Summary by CodeRabbit
- New Features
- Added screen-anchored terminal rendering for smoother scrolling and more accurate history synchronization.
- Added a Terminal Scrollback setting with selectable limits from 1,000 to 20,000 rows.
- Improved replay and hydration for deep terminal history.
- Bug Fixes
- Prevented scrolling updates from being incorrectly replaced by ordinary screen refreshes.
- Improved recovery when terminal history continuity is interrupted.
- Documentation
- Updated terminal rendering and supported capability documentation.