Skip to content

fix(mobile): pipeline live terminal input instead of awaiting each RPC (STA-3199) - #15708

Closed
brennanb2025 wants to merge 3 commits into
mainfrom
brennanb2025/sta-3199-mobile-input-pipeline
Closed

brennanb2025 wants to merge 3 commits into
mainfrom
brennanb2025/sta-3199-mobile-input-pipeline

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​500 $\color{#cf222e}{\Huge{\mathbf{−}}}$​2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​498
Prod 11 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​313 $\color{#cf222e}{\Huge{\mathbf{−}}}$​50 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​263

ELI5

Phone typing into a remote terminal used to wait for the desktop to reply to every keystroke before sending the next one. On a slow link that made letters appear in chunks and keep arriving after you stopped. Live keys now ride the same one-way terminal stream desktop already uses, so typing is no longer gated on a round trip per letter.

What Changed

  • Live mirror deltas and accessory raw bytes write existing TerminalStreamOpcode.Input frames (opcode 7) on the mobile terminal.subscribe binary stream and return after the socket write.
  • LAN, Relay, and Iroh share RpcClient.sendTerminalInput. Ordered writes and pending-byte coalescing stay; the queue no longer waits on RPC responses.
  • If the host never advertised a numeric streamId (JSON-only subscribe / lease-only / not yet subscribed), live input still uses terminal.send.
  • A failed stream write does not fall back to terminal.send (avoids duplicate or stale replay after disconnect).

Why

Mobile live input was stop-and-wait: queueTerminalLiveMirrorSend awaited rpc.sendRequest('terminal.send'), and sendRequest only resolves when the correlated response arrives. Even with coalescing, a burst was first-key RTT plus remainder RTT, which is the “one visible step every ~0.5s, then drain after you stop” report. Desktop already sends Input frames fire-and-forget on the multiplex stream; the host already accepts those frames on terminal.subscribe. This matches that path instead of inventing a debounce or dropping keys.

Linked Issue

Fixes #12071

https://linear.app/stably/issue/STA-3199

Visual Proof

N/A — no UI layout or styling change. The change is send-path latency: live keystrokes no longer wait for an RPC response before the next write.

Testing

  • Acceptance test written first against unfixed sender: later writes stayed empty while the first terminal.send was held (expected '' to be 'abc').
  • After the stream-write path: same test passes; overlapping 'h'+'ello world' concatenates in order with nothing dropped; old-host fallback still coalesces 'bc' onto the second terminal.send.
  • Neutered only the sendTerminalInput branch (kept the tests): first test went red again (expected '' to be 'abc'), then restored.
  • pnpm vitest run --config vitest.config.ts on live-input, rpc-client, relay, logical-client, accessory, pending-flush, and commit tests — 107 passed in the full related set; focused re-run after test-file split still green.
  • pnpm typecheck in mobile/
  • oxlint on changed files (no max-lines disable)

Before / after (deterministic)

Hold the first RPC response, then queue a, b, c:

Writes issued before first RPC response terminal.send calls
Before none 1 (a), remainder blocked
After a then bc (join abc) 0

At 300–500 ms RTT that is the difference between “step every RTT, keep draining after stop” and “all live bytes leave immediately; only echo waits on RTT”.

Remote wire compatibility

Reuse of existing opcode 7 (Input), not a new opcode, not a host-publish change. Clients send Input only after a numeric streamId from the subscribe handshake, which only binary-stream hosts emit. Older JSON-only hosts never see Input frames and keep terminal.send. Old clients against new hosts are unchanged.

Notes

Cross-platform: no keyboard/path changes. SSH/folder: input still goes to the execution host via existing terminal.send / stream Input handlers. Compose-box submit and native-chat answer paths still use terminal.send (one-shot, not live typing).

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • Focused tests, typecheck, and oxlint pass locally

Live keystrokes were stop-and-wait on terminal.send, so bursts still
paid RTT between the first write and the coalesced tail. Reuse the
existing subscribe Input frames so writes return after the socket send,
matching desktop multiplex input, and fall back to terminal.send when
the host has not advertised a binary stream id.

Fixes #12071
STA-3199 moved the live-mirror send out of the session route, so the route's
TERMINAL_INPUT_SEND_OPTIONS count drops 4 -> 3. Rather than just lowering the
number, assert the new module carries the option too, and that the pipelined
stream path - which never reaches terminal.send - gets its now-or-never
guarantee from the connection gate instead.
@coderabbitai

coderabbitai Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fdb5c2d0-42d8-4e52-b7a9-68df894ca0d1

📥 Commits

Reviewing files that changed from the base of the PR and between 5ad4aba and b26c2fd.

📒 Files selected for processing (2)
  • mobile/src/transport/rpc-client-encrypted-binary-send.ts
  • mobile/src/transport/rpc-client-terminal-input.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • mobile/src/transport/rpc-client-terminal-input.test.ts
  • mobile/src/transport/rpc-client-encrypted-binary-send.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The change adds terminal input stream framing with opcode 7 and UTF-8 encoding. It tracks terminal stream IDs and sends encrypted binary input frames through RPC, relay, and logical clients. Stream mappings are cleared during removal, replay, and session cleanup. Live terminal input and accessory input attempt streaming first, then use the existing terminal.send RPC when no stream is available. Tests cover framing, encryption, connection states, lifecycle cleanup, ordering, failures, and fallback behavior.

Merge Risk: 🔵 Low · up to b26c2

Live terminal input now uses the binary stream when available, but a stale connection may reject keystrokes without automatically recovering, temporarily interrupting typing until the connection is refreshed. This bounded risk warrants owner awareness or follow-up, while the change remains mergeable.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 34 functions across 20 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: removing per-RPC waiting from mobile live terminal input.
Description check ✅ Passed The description covers the required change, rationale, issue, testing, compatibility, visual proof, and checklist items.
Linked Issues check ✅ Passed The implementation addresses issue #12071 with ordered stream input, fallback compatibility, coalescing, disconnect safety, and related regression tests.
Out of Scope Changes check ✅ Passed The transport, stream, RPC, accessory, and test changes directly support the live terminal input pipeline objective.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5d503bef-7399-407a-9152-a5c8322b987a

📥 Commits

Reviewing files that changed from the base of the PR and between 2c24025 and 3af90c0.

📒 Files selected for processing (19)
  • mobile/app/h/[hostId]/session/[worktreeId].tsx
  • mobile/src/terminal/send-live-terminal-input.test.ts
  • mobile/src/terminal/send-live-terminal-input.ts
  • mobile/src/terminal/terminal-live-accessory-raw-send.test.ts
  • mobile/src/terminal/terminal-live-accessory-raw-send.ts
  • mobile/src/transport/e2ee.ts
  • mobile/src/transport/mobile-relay-rpc-session.test.ts
  • mobile/src/transport/mobile-relay-rpc-session.ts
  • mobile/src/transport/mobile-relay-rpc-streams.test.ts
  • mobile/src/transport/mobile-relay-rpc-streams.ts
  • mobile/src/transport/rpc-client-encrypted-binary-send.test.ts
  • mobile/src/transport/rpc-client-encrypted-binary-send.ts
  • mobile/src/transport/rpc-client-terminal-input-send.test.ts
  • mobile/src/transport/rpc-client-terminal-input-send.ts
  • mobile/src/transport/rpc-client-terminal-input.test.ts
  • mobile/src/transport/rpc-client.ts
  • mobile/src/transport/stable-logical-rpc-client.test.ts
  • mobile/src/transport/stable-logical-rpc-client.ts
  • mobile/src/transport/terminal-stream-protocol.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 1 remains after this review.

Comment on lines +49 to +50
if (!socket || socket.readyState !== WebSocket.OPEN || !sharedKey) {
return false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Recover when the binary socket is stale.

Lines 49-50 return false without invoking onWriteError when the socket is no longer open. If React Native drops onclose, live input returns failed but does not start reconnect recovery. The live-input caller suppresses its terminal.send fallback for failed, so later input is dropped until another recovery path runs.

Route a non-open socket through the current-socket error callback. Add a regression test that changes readyState without calling onclose.

Proposed fix
-  if (!socket || socket.readyState !== WebSocket.OPEN || !sharedKey) {
+  if (!socket || !sharedKey) {
     return false
   }
+  if (socket.readyState !== WebSocket.OPEN) {
+    onWriteError(socket)
+    return false
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!socket || socket.readyState !== WebSocket.OPEN || !sharedKey) {
return false
if (!socket || !sharedKey) {
return false
}
if (socket.readyState !== WebSocket.OPEN) {
onWriteError(socket)
return false
}

…path

React Doctor flags both on changed lines: TerminalInputStreamRegistry is used
only as a type, and Array<T> is forbidden in favour of T[].
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Close this older input-stream patch while #12071 keeps tracking slow phone typing. The fix needs to use the current connection and stream ownership.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Mobile terminal input serializes every live delta behind a full RPC round trip

1 participant