iOS: preserve terminal input ordering under fast typing

Merged
AZ
4mo ago
+403-42

Fixes https://github.com/manaflow-ai/cmux/issues/6082 (fast typing on iOS lands out of order, e.g. "aziz" → "aizz").

Root cause: the iOS terminal's per-keystroke path lost ordering after the surface delegate. GhosttySurfaceCoordinator.didProduceInput spawned one unstructured Task per keystroke, and submitTerminalRawInput(data:surfaceID:) sent each directly through sendRemoteTerminalInput, bypassing the FIFO MobileTerminalInputSendBuffer that the sibling text path already used for exactly this reason. Concurrent keystroke tasks then crossed reentrant actors (MobileTerminalLaneCoordinator → MobileIrohTerminalLane → QUIC stream write) or the multi-await RPC sendRequest, so two keystrokes could reach the wire in either order. The Mac applies input strictly in arrival order (lane router reads one QUIC stream serially; v2MobileTerminalInput is synchronous per request), so the reorder was purely client-side.

Fix: every terminal raw-input producer (typing, backspace, accessory escape sequences, chat pane, composer submit, release-gate probes) now enqueues synchronously on the main actor into the one rawTerminalInputBuffer, and the single serial drain loop is the only sender: next chunk goes out only after the previous send completes, so wire order always equals keystroke order on both the iroh lane and the RPC fallback. The delegate call is now a synchronous enqueue (the per-keystroke Task is gone), and the drain dequeues with a 16 KiB cap split at Unicode-scalar boundaries so coalescing can never produce a frame the iroh lane rejects.

Commit 1 adds the regression test only (red): with the first terminal.input response held, four racing keystroke submissions previously produced 4 concurrent in-flight requests and unordered text. Commit 2 adds the fix (green): max one in-flight request, concatenated wire text exactly aziz. Splitter edge cases (multi-byte scalar straddling the cap, order across splits) are covered in CmuxMobileShellModel tests, which this PR also adds to the test-ios CI gate.


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.


Summary by cubic

Preserves terminal input ordering on iOS by sending all raw input through a single FIFO buffer with a serial drain. Fixes #6082 where fast typing could arrive out of order.

  • Bug Fixes
    • All producers enqueue synchronously to rawTerminalInputBuffer; a single drain sends the next chunk only after the previous completes, keeping FIFO order over iroh and RPC.
    • Added a 16 KiB send cap and Unicode-scalar-safe splitting in MobileTerminalInputSendBuffer.nextBatch(maximumByteCount:).
    • Replaced per-keystroke tasks with synchronous sendTerminalRawInput(...) in CmuxMobileShellUI (Ghostty surface and chat composer).
    • Added ordering regression tests in CmuxMobileShell and buffer-splitting tests in CmuxMobileShellModel; CI now runs CmuxMobileShellModel tests.

Summary by CodeRabbit

  • Bug Fixes
    • Improved terminal raw input ordering during rapid, concurrent typing.
    • Prevented input submissions from stalling during buffer drains and connection lifecycle resets (sign-out/reconnect/pairing).
    • Ensured large multilingual inputs are split safely at Unicode boundaries with capped batch sizing.
  • Tests
    • Added ordering and routing test coverage for terminal raw input behavior, including queue/drain synchronization.
    • Expanded buffer tests for UTF-8/capped chunking edge cases.
  • CI
    • Extended the iOS test gate to run additional SwiftPM tests for the MobileShellModel package.