Skip to content

fix(terminal): bound parse-deferred remote flow - #10012

Merged
OrcaWin merged 3 commits into
mainfrom
fix/terminal-flow-control-followup
Jul 23, 2026
Merged

OrcaWin merged 3 commits into
mainfrom
fix/terminal-flow-control-followup

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This is a direct-to-main follow-up to merged #9683 and commit b606117ee. It preserves the parsed-or-intentionally-discarded ACK contract and adds the bounded flow-control, cleanup, fairness, and performance work needed for production remote terminals.

  • Fan out one delivery credit across every scheduler write; ACK only after every child parses or intentionally discards.
  • Batch renderer ACKs at 192 KiB or 4 ms.
  • Adapt per-stream credit from 512 KiB to 2 MiB and aggregate credit from 2 MiB to 8 MiB.
  • Cap pending output at 256 KiB per stream and 32 active/pending streams per connection.
  • Drain pending streams round-robin so bulk output cannot starve interactive terminals.
  • Close/release ownership on malformed frames, renderer callbacks, transport writes, serialization failures, disposal, stale generations, resubscribe replacement, and reconnect races.

Receipt-time ACK is not used.

Deterministic reproduction

Paired renderer replay plus live-output tests hold delivery credit while xterm has not completed parsing, then exercise replay/live ordering, transformed frames, hidden panes, late callbacks, disposal, reconnection, and transport failure. The affected Windows 2 Setup PTY independently reproduced blank/stale replay, hidden queued input, and duplicated fragments while the underlying ConPTY remained writable.

Reliability coverage

Deterministic tests cover replay ordering, stale generations, malformed and transformed frames, queue eviction, hidden panes, pane disposal, recovery serialization failure, repeated same-slot subscribe, renderer delivery failure, ACK transport failure, late parser callbacks, reconnect, stream caps, bounded pending memory, and round-robin fairness.

Benchmark

Opt-in command:

ORCA_TERMINAL_PERF_BENCH=1 pnpm exec vitest run --config config/vitest.config.ts --disableConsoleIntercept src/main/runtime/rpc/terminal-multiplex-flow-control.bench.test.ts

RTT viewers aggregate MiB/s per-viewer MiB/s max in flight
1 ms 1 1828.6 1828.6 2 MiB
1 ms 4 7314.3 1828.6 8 MiB
1 ms 8 7641.8 955.2 8 MiB
20 ms 1 94.0 94.0 2 MiB
20 ms 4 375.9 94.0 8 MiB
20 ms 8 387.6 48.4 8 MiB
100 ms 1 18.8 18.8 2 MiB
100 ms 4 75.3 18.8 8 MiB
100 ms 8 77.6 9.7 8 MiB

Completion spread was 0 ms and aggregate in-flight memory stayed at or below 8 MiB. On the latest local run, measured @xterm/headless throughput for 1/4/8 viewers was 27.5/75.2/98.3 aggregate MiB/s; parser CPU was 81.4/196.7/324.2 ms; retained heap was 2,878/7,664/39,932 KiB for 4 MiB per viewer. The benchmark also reports protocol frame allocations, scheduler iterations, and xterm writes.

Screenshots

No UI change. This is a transport, renderer-delivery, and server scheduling contract.

Testing

  • Latest-main combined flow-control and resync suite: 11 files, 565/565 tests.
  • Earlier combined Windows 2 validation head 497138435: 23 files, 1,750/1,750 tests; fix(terminal): bound parse-deferred remote flow #10012 curated subset 535/535.
  • Typecheck.
  • Opt-in RTT/viewer/parser benchmark.
  • Windows terminal restart regression and Wayland input checks on the prior CI head.
  • Latest-head GitHub CI against main at c445f2654: verify, Linux/mac golden E2E, Wayland, Windows restart, and packaged crash survival all green.
  • Conflict audit against main at c445f2654 is clean.

AI Review Report

Review compared the implementation with landed #9683, current reconnect/output scheduling, and the newer #10089 resync hardening now on main. Repeated internal-review-until-clean passes covered exactly-once credit settlement, fanout, nested delivery, timer/disposal ownership, transport throws, unavailable or failed recovery snapshots, malformed/transformed frames, bounded memory, stream caps, replay trimming, retry/watchdog composition, and fairness. The protocol and renderer paths are platform-neutral; this PR adds no shortcuts or labels, uses no platform-specific path or shell assumptions, and preserves Electron/SSH/native/WSL transport boundaries. Focused tests ran on macOS and Windows, while latest-head CI covers Linux/Wayland and Windows terminal restart/crash survival. The final CodeRabbit pass is clean and all GitHub review threads are resolved.

Security Audit

Frame validation remains fail-closed. Malformed framing closes the connection so server-side debt is released; invalid transformed payloads are credited only after intentional discard. Per-stream, aggregate, pending-byte, and stream-count bounds prevent a slow or malicious viewer from creating unbounded memory growth. No receipt-time ACK bypass exists.

Notes

Related remote-runtime recovery and Active Server isolation: #10011. Both PRs target main directly; the split keeps throughput/backpressure risk independent from host/topology migration risk.

@OrcaWin
OrcaWin force-pushed the fix/terminal-flow-control-followup branch from 7862da3 to 6ae5a1c Compare July 22, 2026 23:34
@OrcaWin
OrcaWin changed the base branch from main to fix/node-types-stat-nonnull July 22, 2026 23:34
@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Updates terminal multiplex flow control with shared limits, adaptive per-stream and aggregate ACK windows, round-robin pending-output draining, transport-failure handling, and stream-slot enforcement. Renderer delivery credits now settle after all scheduler claims complete. Remote terminal multiplexing batches acknowledgements and handles malformed frames, unknown streams, disposal, parser failures, and transport errors. New tests cover these behaviors, alongside parsed-credit simulation and benchmark evidence updates.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and accurately summarizes the main change: bounding parse-deferred terminal flow control.
Description check ✅ Passed The description includes all required sections and covers the implementation, testing, review, security, and notes.
✨ Finishing Touches
📝 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/renderer/src/runtime/remote-runtime-terminal-multiplexer.ts (1)

525-541: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Guard non-ack terminal output delivery with the same catch branch.

For !stream.acknowledgeOutput clients, deliverOutput() directly calls stream.callbacks.onData(...) without a try/catch, while the ack path wraps the same callback in failConnection. Uncaught onData errors here should be routed the same way for robustness and unsubscribe handling.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bd5f011a-f97d-4714-ac02-1323e9863f63

📥 Commits

Reviewing files that changed from the base of the PR and between 01bcc57 and 7862da3c8b5abb3c8474ea9e234699802c027b76.

📒 Files selected for processing (16)
  • config/reliability-gates.jsonc
  • src/main/runtime/rpc/methods/terminal.ts
  • src/main/runtime/rpc/terminal-multiplex-flow-control.bench.test.ts
  • src/main/runtime/rpc/terminal-multiplex-round-robin.test.ts
  • src/main/runtime/rpc/terminal-multiplex-round-robin.ts
  • src/main/runtime/rpc/terminal-multiplex.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/components/terminal-pane/terminal-pty-ack-gate.test.ts
  • src/renderer/src/components/terminal-pane/terminal-pty-ack-gate.ts
  • src/renderer/src/lib/pane-manager/terminal-delivery-credit.test.ts
  • src/renderer/src/lib/pane-manager/terminal-delivery-credit.ts
  • src/renderer/src/runtime/remote-runtime-terminal-multiplexer.ts
  • src/renderer/src/runtime/remote-runtime-terminal-parse-backpressure.test.ts
  • src/renderer/src/runtime/runtime-terminal-stream.test.ts
  • src/shared/terminal-multiplex-flow-control.ts
💤 Files with no reviewable changes (1)
  • src/renderer/src/components/terminal-pane/terminal-pty-ack-gate.ts

@OrcaWin
OrcaWin changed the base branch from fix/node-types-stat-nonnull to main July 22, 2026 23:56
@OrcaWin
OrcaWin force-pushed the fix/terminal-flow-control-followup branch 5 times, most recently from ede6096 to 5042ce7 Compare July 23, 2026 02:37
@OrcaWin

OrcaWin commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@OrcaWin
OrcaWin force-pushed the fix/terminal-flow-control-followup branch from 5042ce7 to a6978be Compare July 23, 2026 03:02
@OrcaWin

OrcaWin commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@OrcaWin
OrcaWin force-pushed the fix/terminal-flow-control-followup branch from a6978be to dd58794 Compare July 23, 2026 05:18
@OrcaWin

OrcaWin commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/renderer/src/runtime/remote-runtime-terminal-multiplexer.ts (1)

575-591: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Unhandled exception path for non-desktop (acknowledgeOutput === false) streams.

When stream.acknowledgeOutput is true, deliverOutput() is now wrapped in try/catch and any throw fails the connection gracefully. The sibling branch for !stream.acknowledgeOutput (mobile clients, since acknowledgeOutput: args.client.type === 'desktop' at line 254) calls deliverOutput() unguarded — if stream.callbacks.onData throws, the exception escapes handleBinary entirely, up through the onBinary callback passed to window.api.runtimeEnvironments.subscribe(...), with no cleanup (no failConnection, no unsubscribe, no onError). This leaves the connection/stream state inconsistent for mobile clients specifically, and is untested (all provided tests use client: { type: 'desktop' }).

🐛 Proposed fix to align both branches
       if (!stream.acknowledgeOutput) {
-        deliverOutput()
+        try {
+          deliverOutput()
+        } catch (error) {
+          this.failConnection(
+            error instanceof Error ? error : new Error('Remote terminal output delivery failed.')
+          )
+        }
         return
       }

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7008a335-529c-42af-b703-43a7e6ab4474

📥 Commits

Reviewing files that changed from the base of the PR and between a6978be and dd58794.

📒 Files selected for processing (16)
  • config/reliability-gates.jsonc
  • src/main/runtime/rpc/methods/terminal.ts
  • src/main/runtime/rpc/terminal-multiplex-flow-control.bench.test.ts
  • src/main/runtime/rpc/terminal-multiplex-round-robin.test.ts
  • src/main/runtime/rpc/terminal-multiplex-round-robin.ts
  • src/main/runtime/rpc/terminal-multiplex.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/components/terminal-pane/terminal-pty-ack-gate.test.ts
  • src/renderer/src/components/terminal-pane/terminal-pty-ack-gate.ts
  • src/renderer/src/lib/pane-manager/terminal-delivery-credit.test.ts
  • src/renderer/src/lib/pane-manager/terminal-delivery-credit.ts
  • src/renderer/src/runtime/remote-runtime-terminal-multiplexer.ts
  • src/renderer/src/runtime/remote-runtime-terminal-parse-backpressure.test.ts
  • src/renderer/src/runtime/runtime-terminal-stream.test.ts
  • src/shared/terminal-multiplex-flow-control.ts
💤 Files with no reviewable changes (1)
  • src/renderer/src/components/terminal-pane/terminal-pty-ack-gate.ts
🚧 Files skipped from review as they are similar to previous changes (13)
  • src/shared/terminal-multiplex-flow-control.ts
  • src/main/runtime/rpc/terminal-multiplex-round-robin.test.ts
  • src/main/runtime/rpc/terminal-multiplex-round-robin.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/components/terminal-pane/terminal-pty-ack-gate.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/lib/pane-manager/terminal-delivery-credit.test.ts
  • src/renderer/src/lib/pane-manager/terminal-delivery-credit.ts
  • src/main/runtime/rpc/terminal-multiplex-flow-control.bench.test.ts
  • src/renderer/src/runtime/runtime-terminal-stream.test.ts
  • config/reliability-gates.jsonc
  • src/main/runtime/rpc/methods/terminal.ts
  • src/main/runtime/rpc/terminal-multiplex.test.ts

@OrcaWin
OrcaWin merged commit 72a2d7b into main Jul 23, 2026
6 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
Credits remote terminal output only after parse or intentional discard, with bounded adaptive windows, fair draining, recovery cleanup, and RTT/parser benchmarks.
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.

1 participant