Skip to content

Keep terminals responsive under agent output load - #7176

Closed
brennanb2025 wants to merge 1 commit into
mainfrom
brennanb2025/7017
Closed

brennanb2025 wants to merge 1 commit into
mainfrom
brennanb2025/7017

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Summary

Keeps the renderer responsive while agents stream terminal output, by pacing how fast the output scheduler feeds xterm:

  • HIGH_PRIORITY_MAX_WRITES_PER_DRAIN 16 → 2 (at most ~32 KB handed to xterm per drain tick, down from ~256 KB)
  • HIGH_PRIORITY_DRAIN_INTERVAL_MS 1 → 4 (matches Chromium's clamped nested-timer floor, cuts timer churn)
  • New DRAIN_TIME_BUDGET_MS (8 ms): a drain yields after the current write once the tick's wall-clock budget is spent. This has a real production trigger — after user input, xterm's write() parses synchronously inside the call (_didUserInput fast path, up to its 12 ms slice), so a slow first write no longer drags a second synchronous parse into the same tick.

Why

Under multi-agent output load the scheduler could feed xterm up to 256 KB/ms. xterm parses on the renderer main thread in ~12 ms slices, so sustained output kept the thread near-100% busy and starved input/paint — the "terminal gets laggy, then the whole app" escalation in #7017's Windows/WSL report. Keystroke echo is unaffected: latency-sensitive foreground writes still bypass the queue.

Might fix #7175 — "Not responding" with >3 parallel sessions on Windows is consistent with this renderer-saturation mechanism, since all sessions share one renderer and output load scales with session count. Note it does not address memory-driven crashes (#7175's crash on 32 GB could also be OOM), nor main-process load from many projects.

Related: #7017

Testing

  • pane-terminal-output-scheduler.test.ts: 45/45, including two new tests (drain yields when writes spend the frame budget; Date.now fallback when performance is unavailable)
  • Consumer suites (terminal-shutdown-layout-capture, use-terminal-pane-global-effects, use-terminal-scroll-visibility-memory): 31/31
  • typecheck:web clean
  • Local test:e2e:terminal-perf run failed for environmental reasons (harness fixture repos.add fails before any test body; reproduced identically with main's scheduler files) — relying on CI for the perf gates (typing latency, redraw freeze, output scheduler, artificial OpenCode load)

Follow-ups (pre-existing, not addressed here)

  • The foreground/high-priority backlog is unbounded, and PTY ACKs are sent on receipt, so bulk visible output faster than the drain rate accumulates in renderer strings. Before this change the excess landed in xterm's write buffer instead, which throws at 50 MB pending and gets treated as a disposed terminal (silent backlog drop) — the failure mode shifts from data loss toward memory growth. Worth capping foreground backlogs with the existing warning + snapshot-recovery machinery, or ACKing after drain.
  • The visible pane shares the per-drain write budget round-robin with backlogged hidden panes (no active-terminal-first ordering anymore), so heavy multi-agent load adds some visible TUI paint latency. CI's typing-latency and redraw-freeze specs are the gate to watch.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Duplicate of #7139 (same commit) - filed before spotting the existing PR. The might-fix note for #7175 has been added to #7139.

@brennanb2025
brennanb2025 deleted the brennanb2025/7017 branch July 3, 2026 02:31
@coderabbitai

coderabbitai Bot commented Jul 3, 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

Run ID: 5217e615-9057-4ea9-85c0-5bb3e2a9092d

📥 Commits

Reviewing files that changed from the base of the PR and between 087ad66 and 15a1519.

📒 Files selected for processing (2)
  • src/renderer/src/lib/pane-manager/pane-terminal-output-scheduler.test.ts
  • src/renderer/src/lib/pane-manager/pane-terminal-output-scheduler.ts

📝 Walkthrough

Walkthrough

This change modifies terminal output drain scheduling behavior. The high-priority drain interval was increased from 1ms to 4ms, the maximum writes per drain reduced from 16 to 2, and a new 8ms time budget (DRAIN_TIME_BUDGET_MS) was introduced. A getDrainNow() helper using performance.now() with a Date.now() fallback tracks elapsed drain time, causing the drain loop to yield once writes have started and the budget is reached. Corresponding test assertions were updated and two new tests added covering budget-triggered yielding and the Date.now fallback path.

Changes

Area Change
pane-terminal-output-scheduler.ts Adjusted drain interval/write-count constants; added time budget, getDrainNow helper, and loop throttling logic
pane-terminal-output-scheduler.test.ts Updated existing test expectations; added tests for budget-based yielding and Date.now fallback

Sequence Diagram(s)

Not applicable — changes are confined to internal scheduling logic and test assertions without a multi-component interaction flow worth diagramming beyond the internal control flow shown in the review stack.

Estimated code review effort: 🎯 3 (Moderate) | ⏱️ ~20 minutes

Related PRs: None identified from the provided context.

Suggested labels: performance, scheduler, tests

Suggested reviewers: Not enough information available to recommend specific reviewers.

Poem
A rabbit watched the terminal spool,
Timing writes by a budgeted rule,
Eight milliseconds, then a pause so light,
Backlogs drain in measured flight,
Date.now steps in when performance won't play —
Hop by hop, the output finds its way.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers summary, rationale, testing, and follow-ups, but omits required sections like Screenshots, AI Review Report, Security Audit, and Notes. Add the missing template sections: Screenshots/No visual change, AI Review Report, Security Audit, and Notes, including the required cross-platform review details.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: keeping terminal output responsive under agent load.
Linked Issues check ✅ Passed The changes address the linked issue's renderer-saturation path by pacing xterm output, matching the primary objective for #7175.
Out of Scope Changes check ✅ Passed The diff stays focused on the scheduler and its tests, with no unrelated feature or infrastructure changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

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] Windows: 'Not responding' and crash when running more than 3 sessions in parallel (32GB RAM)

2 participants