Skip to content

Typed text no longer shows up late when another terminal is printing a lot - #51089

Merged
brennanb2025 merged 19 commits into
mainfrom
brennanb2025/terminal-per-session-backpressure
Nov 28, 2026
Merged

brennanb2025 merged 19 commits into
mainfrom
brennanb2025/terminal-per-session-backpressure

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When one terminal prints a huge amount of output, another terminal you're typing in could freeze. Your keystrokes then appeared about a minute later. Now each terminal is slowed down only by its own output, so a noisy terminal can't hold up the one you're typing in.

Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​651 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​651
Prod 14 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​523 $\color{#cf222e}{\Huge{\mathbf{−}}}$​102 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​421

What Changed

  • Before: the terminal host paused a terminal's output only once all terminals together had about 4 MB waiting to reach the app. Below that, a flooding terminal kept piling output into the shared queue. A quiet interactive terminal (an agent TUI, a shell prompt) then waited behind up to 4 MB of someone else's output. When the app was busy, that drained slowly enough to hit the 60 s stall watchdog. Typed text echoed only after that, because a TUI blocked on writing its screen stops reading input.
  • After:
    • Each terminal pauses on its own backlog (256 KB) and resumes when it is back under 128 KB. A flooding terminal can therefore never put more than one window of output ahead of another terminal.
    • The 4 MB total is kept only as a last resort: while it is exceeded, every terminal's thresholds are halved.
    • If you type into a terminal whose output is stalled, its stall watchdog shortens from 60 s to 2 s.
    • When typed input reaches a paused terminal whose own output was delivered in the last 10 s, the terminal is resumed within 2 s so the program can read the keys. This never drops output and never moves the 60 s deadline.
    • The 60 s watchdog drops a paused terminal's queued output only when none of that terminal's own output was delivered in the last 10 s. Completions of other terminals' output, empty refill frames, and a disconnected client's writes don't count as progress.
    • When a detach or teardown releases a pause, the next update pauses the terminal afresh.
    • One terminal-output-paused event goes to daemon.log when an output pause lasts 2 s or more or a watchdog fires. It gives the session id, pause length, reason and backlog sizes, never content, and at most one per 10 s. The same applies to app.log for the app-to-renderer pause.

Why

The global threshold made one terminal's flow control depend on its siblings. Pausing each terminal on its own unsent output is the standard model for terminal hosts. It bounds the wait behind a flooding neighbour to one window, rather than to the whole shared cap.

Linked Issue

Internal report: typed text in a terminal appeared ~60 s late (intermittent, macOS).

Visual Proof

N/A: there is no UI change. The echo-latency measurements are in Testing.

Testing

  • daemon-stream-backpressure.test.ts: a flooding session pauses on its own backlog and its quiet sibling is never paused. It fails on the previous code.

  • pnpm check:changed passes.

  • Repro (Linux, 32 cores):

    • Setup: about 1,900 workspaces. One terminal floods about 600 MB/s while a TUI in a split redraws 3 KB every 100 ms and echoes keys. CPU load is 64 busy loops (load average about 65), and the app's main process is periodically stopped to imitate a busy app.
    • Keys are sent through CDP. Echo = send → char visible in the terminal buffer.
    main process build keys echo p50 echo p95 echo max keys over 10 s
    stopped 0.3 s of every 0.5 s before 300 195 ms 364 ms 996 ms 0
    stopped 0.3 s of every 0.5 s after 300 213 ms 359 ms 409 ms 0
    stopped 4 s of every 4.1 s before 36 16.4 s 32.8 s 37.0 s 28
    stopped 4 s of every 4.1 s after 36 133 ms 16.3 s 16.4 s 4
    • Keys typed into the flooding program itself: max 1,143 ms before, 628 ms after.
    • No flood: 5 ms p50 in both builds.
    • Throughput for a single 400 MB dump: after/before = 0.991, which is within noise.
    • Flooded output checked for gaps after Ctrl+C: none in any run after the change.
  • Watchdog and logging (same rig):

    • Main stopped for 75 s: the 60 s watchdog fired in all 4 reps. daemon.log got terminal-output-paused with reason: "timeout watchdog, consumer stalled" and pausedMs ≈ 60000, and the app recovered with keys echoing in ≤21 ms. Dropping output after a real 60 s stall is unchanged from before.
    • Main stopped 4 s of every 4.1 s for 150 s: only drained pauses (max 4 s), with 0 gaps across 24M flooded lines.
    • Typing into a paused terminal: reason: "typed input" 2 s after the keys arrived, and no output dropped.
    • Idle: no log lines. All log lines are at least 10 s apart.
  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Every added test fails without this change and avoids the test anti-patterns

Performance

  • Hot path: terminal output. refresh() already ran per write and per write-completion over the sessions with a backlog; it now runs its pause and resume checks every time, not only under global pressure. That is the same O(sessions-with-backlog) loop, with no new allocation beyond the per-session pause timestamp.
  • Throughput: a single flooding terminal is now paused at 256 KB of unsent output and resumed at 128 KB, instead of running free until 4 MB. That window is above the socket's 128 KB write gate, so a lone flood still keeps the socket full.
  • Memory bound (terminal host): about 256 KB plus one PTY read per terminal that is actively flooding, and half that once the total passes 4 MB. Before, there was no per-terminal bound below 4 MB in total.
  • App process: unchanged. Each terminal's renderer delivery is still capped by the existing 256K-char flow control.

Risk

Daemon output backpressure: state per (session id, producer incarnation)

The incarnation is the Session.incarnationId assigned at spawn. A respawn under the same id gets a new one.

Backlog means the session's own undroppable queued output plus queued metadata and in-flight writes on its owning connection. Background (droppable) output is excluded, so backgrounding a Paused session can drop it to the resume mark.

Producer column describes this mechanism only. The existing client-origin pause is independent, so Flowing or Keep-tail can still coexist with a client pause until the client resumes or its 5 s failsafe fires.

State Meaning Producer
Flowing no record reads
Paused entered when own backlog ≥ 256 KB (64 KB under global pressure); stays until a listed exit; watchdog armed. A tighter mark under pressure does not trim bytes already held paused
Unpausable terminating, exited, or no viewer attached (detached); never asked to pause as is
Keep-tail consumer stalled 60 s; old queued output may be dropped. An independent flag (see invariant below) reads
From Event To
Flowing backlog ≥ pause mark, and the pause is accepted Paused
Flowing background (droppable) output above the mark, no global pressure Flowing (it has its own keep-tail budget)
Flowing pause answered terminating, exited or detached Unpausable
Paused backlog ≤ resume mark (128 KB, or 32 KB under pressure) Flowing
Paused typed input, then 2 s, and this session's own output was delivered in the last 10 s Flowing (re-paused at the next refresh if still over)
Paused typed input, then 2 s, and no own delivery Paused (60 s deadline unchanged)
Paused 60 s, and own delivery in the last 10 s Flowing
Paused 60 s, and no own delivery in the last 10 s Keep-tail
Paused detach releases the pause (released) Flowing
Paused termination begins (terminating), or exit/teardown (exited) Unpausable
Unpausable a client attaches (released) Flowing (paused at the next refresh if still over)
Unpausable termination fails: the kill signal or every force-kill attempt is refused (released) Flowing
Unpausable a detach during termination (released) Flowing, then asked once and answered terminating again
Paused, Unpausable released Flowing; pause, unpausable and credit records dropped (Keep-tail, if set, stays)
Unpausable, Keep-tail all outstanding output for the id leaves (every queued and in-flight entry, droppable or not, on any connection) Flowing (record dropped)
any output enqueued for the id while the host has no session for it (a late tail) unchanged
any new PTY output enqueued for the id while the host reports a different incarnation Flowing; every record (pause, unpausable, keep-tail, credit) dropped
any its client disconnects that client's delivery credits dropped
any every client cleared Flowing; every record dropped

released rows clear the pause state only. If Keep-tail is set, it stays, and the next pause waits for the drain.

Keep-tail invariant: Keep-tail is independent of the pause states and is cleared only by a full drain, a respawn (incarnation change) or clearing every client. released does not clear it: the stalled consumer may still be there, and only a drain proves it isn't. It can coexist with Unpausable (a stall, then termination or exit): the output stays droppable while pauses are skipped, and the drain clears both.

Delivery credit is set only by a successful write that carries this session's own output, completed while it is Paused and its owning client is current. It is cleared on leaving Paused. Credit from a late completion of an earlier incarnation's queued output counts toward the new pause, because the stream is first-in, first-out per id.

Not counted as delivery: empty refill frames, control frames and failed writes.

Incarnation lookups happen once when a record is created, and when a session with a record gets new output enqueued. Retries and unrelated output make none.

Worst case held in the daemon: about 256 KB plus one PTY read per flooding session in Paused (64 KB under global pressure), for producers that support pausing. Keep-tail reads on and is bounded by the existing keep-tail budget instead. Producers without pause support already write through above the existing total cap; that is unchanged here.

Termination that gave up: when every force-kill attempt is refused, the id stays reserved as terminating (a respawn still waits) and only its producer may be paused again. When the first kill signal itself is refused, termination is reset entirely, as before this change. A force-kill that was sent but whose exit has not arrived stays Unpausable until the exit: the killed child can't add output, so its backlog can only drain. A child that never exits after SIGKILL is a pre-existing case tracked separately.

Level: Medium. This changes when terminal output is paused for every local and daemon-hosted terminal.

  • Riskiest part: throughput for very fast output (cat of a large file) now paces against a 256 KB per-terminal window rather than 4 MB. If that window were too small, a large dump would print more slowly. It would not lose output.
  • Terminals now pause below the old 4 MB total. A terminal that floods alone can now be paused, and so dropped at 60 s if its own output stops moving entirely. Before, it was paused only once all terminals together had 4 MB waiting. For a consumer that has really stopped reading, the outcome is the same as before: dropped at 60 s.
  • Fairness while the socket stays full. A paused terminal whose output is held behind steady small sibling writes gets no delivery credit of its own. If that lasts 60 s it is dropped. That needs a nearly stalled consumer; batcher fair share is tracked as a follow-up.
  • The 2 s typed-input resume never drops output.
  • Back out: a plain revert.

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • 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)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@brennanb2025
brennanb2025 force-pushed the brennanb2025/terminal-per-session-backpressure branch 2 times, most recently from 64836db to 30929ec Compare November 27, 2026 22:08
@brennanb2025
brennanb2025 marked this pull request as ready for review November 28, 2026 00:46
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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

Daemon stream flow control now uses per-session backlog thresholds and handles producer stalls based on typed input, socket-write progress, or timeout. Producer pause APIs return whether a pause succeeded. Stream setup creates and returns session attachments, and long pauses are reported. PTY flow control records pause durations and rate-limits long-pause logs.

Merge Risk: Low · previous review 9400b

Typed input should no longer be held up by another terminal's flood, but a few edge cases around stall recovery and typed-input resume timing remain open. None drops output. The exited-session performance test also does not currently fail if its fix is reverted. These are worth fixing soon, and the change is reasonably safe to merge with owner awareness.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the user-visible problem the PR fixes: typed text appearing late while another terminal prints heavily. The changeset implements per-terminal backpressure limits in the terminal host,…
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.
Description check Passed The description follows the repository template and fills in every required section. It has an ELI5, a detailed What Changed section with before and after behavior, a Why section, a Linked Issue entry…
Full details: Title check

Explanation

The title states the user-visible problem the PR fixes: typed text appearing late while another terminal prints heavily. The changeset implements per-terminal backpressure limits in the terminal host, which addresses that cause. The title names the symptom rather than the mechanism, which is acceptable for a title.

Full details: Description check

Explanation

The description follows the repository template and fills in every required section. It has an ELI5, a detailed What Changed section with before and after behavior, a Why section, a Linked Issue entry, and Visual Proof marked N/A with a reason. It also covers Testing with reproduction data, a Performance section with a concrete one-line-plus-detail answer, a Risk section with level, riskiest parts, and a back-out plan, and a completed checklist. The Linked Issue entry is an internal report rather than a tracked issue link, and the template's required 'Fixes #' line is not filled in. The AI Disclosure section is left blank, which is acceptable only if the author is a StablyAI team member. The Notes and Checklist items are checked off. The description is detailed and on topic, so it passes, with a minor note about the linked issue.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@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: 2

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e8fc1887-b2da-41d7-8932-d8180a243fa0
Commits

Reviewing files that changed from the base of the PR and between 1ae6828 and 30929ec.

Files ignored due to path filters: 1

  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing: 10

  • src/main/daemon/server/daemon-server.ts
  • src/main/daemon/server/daemon-session-streams.ts
  • src/main/daemon/sessions/session-producer-pause.ts
  • src/main/daemon/sessions/session.ts
  • src/main/daemon/terminal-host/terminal-host.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.test.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.ts
  • src/main/daemon/transport/stream/daemon-stream-data-batcher-options.ts
  • src/main/daemon/transport/stream/daemon-stream-data-batcher.ts
  • src/main/ipc/pty/delivery/pty-producer-flow-control.ts

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

@brennanb2025
brennanb2025 force-pushed the brennanb2025/terminal-per-session-backpressure branch from 30929ec to 3caa124 Compare November 28, 2026 00:55
… progress counts only real write completions
@brennanb2025
brennanb2025 force-pushed the brennanb2025/terminal-per-session-backpressure branch from 3caa124 to 9dcb130 Compare November 28, 2026 00:55
…s draining; failed writes aren't progress; watchdog log lines are never rate-limited away

@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

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2162ea38-3413-4154-9ece-19f615c38a10
Commits

Reviewing files that changed from the base of the PR and between 30929ec and 9dcb130.

Files selected for processing: 5

  • src/main/daemon/sessions/session-producer-pause.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.test.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.ts
  • src/main/ipc/pty/delivery/pty-producer-flow-control.ts
  • src/shared/terminal/pty/flow-control/long-pause-log-limiter.ts

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

…e once; count a session's own deliveries as its progress
…s afresh; refill frames and dead connections aren't delivery

@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

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3a3685ac-825d-45cb-b444-827743a05127
Commits

Reviewing files that changed from the base of the PR and between 9dcb130 and cc2d53f.

Files selected for processing: 3

  • src/main/daemon/sessions/session-producer-pause.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.ts
  • src/shared/terminal/pty/flow-control/long-pause-log-limiter.ts

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

…ing a connection drops only its own delivery credit

@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

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e4abc1e0-8fde-44ea-84e1-72783d4033f6
Commits

Reviewing files that changed from the base of the PR and between cc2d53f and 7629170.

Files selected for processing: 6

  • src/main/daemon/sessions/session-producer-pause.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.performance.test.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.test.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.ts
  • src/main/daemon/transport/stream/daemon-stream-data-batcher-options.ts
  • src/main/daemon/transport/stream/daemon-stream-data-batcher.ts

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

@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

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 540d8a9a-a27e-4e9d-90b3-526968ff1803
Commits

Reviewing files that changed from the base of the PR and between 7629170 and 9400bef.

Files selected for processing: 7

  • src/main/daemon/sessions/session-producer-pause.ts
  • src/main/daemon/sessions/session.ts
  • src/main/daemon/terminal-host/terminal-host.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.performance.test.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.test.ts
  • src/main/daemon/transport/stream/daemon-stream-backpressure.ts
  • src/main/daemon/transport/stream/daemon-stream-data-batcher-options.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

…check incarnation when a recorded id gets new output so a respawn starts clean
… drops every record; refused force-kill lets the producer pause again; control frames earn no delivery credit

- a pause request with no viewer answers 'detached' and the id is recorded Unpausable; the first attach reports 'released'
- incarnation looked up once when a record is created, never on retries
- 'released' drops pause, unpausable, keep-tail and credit, like an incarnation change
- clearing every client drops keep-tail records with their incarnation
- moving credit between clients drops the empty per-client index
…credit; only a keep-tail change rescans backlogs on release
…ns; a late tail without an incarnation is not a respawn; control-credit check becomes a perf contract on both flush paths
@brennanb2025

Copy link
Copy Markdown
Contributor Author

QA evidence: head a1c7b06, Linux, headless app with about 1,900 workspaces, a flooding terminal next to an interactive one.

Scenario Result
Consumer fully stalled for 75 s (2 reps) Pass. The flooding terminal's pause was released at 60 s (timeout watchdog, consumer stalled in daemon.log), and output and echo recovered within about 3 s of the stall ending. Echo p50 was 31 ms and 5 ms.
Slow but draining consumer, with typing (2 reps, plus a tighter cycle) Pass. No stalled release, all 40 keys arrived in order, and 0 stream gaps on the brief's cycle. On the tighter cycle a typed key resumed a paused terminal (typed input).
Quiet control, 30 s Pass. No pause lines. Echo p50 6 ms, max 98 ms.
Window reload during a flood (3 reps) Output resumed within about 40 ms. The pause re-applied (the flooding process blocked on write about two thirds of the time). No stalled release, and the daemon's memory stayed flat over 60 s. Lines were lost across the reload restore, the same as on the merge base. That loss is in the app's snapshot restore path, not this change.

@brennanb2025
brennanb2025 merged commit 93623b5 into main Nov 28, 2026
72 checks passed
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