Skip to content

Pace terminal output to what the browser has rendered - #4993

Merged
ymichael merged 1 commit into
mainfrom
bb/in-app-terminal-feels-slow-reproduce-and-measure-thr_buadiv2kqr
Oct 6, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/in-app-terminal-feels-slow-reproduce-and-measure-thr_buadiv2kqr

Conversation

@ymichael

@ymichael ymichael commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Nothing between the PTY and xterm.js applied backpressure. The host daemon read a terminal's PTY as fast as the program wrote, and the server queued output for each browser socket until 32 MiB, then closed the socket with 1013 terminal-backpressure. The browser reconnected from its last sequence, found the daemon's 4 MiB ring had moved past it, and called terminal.reset() behind "Some terminal output was unavailable after reconnect".

Under sustained output (yes, a large cat or log, a chatty build) this happened about once a second:

  • The screen and scrollback were wiped repeatedly.
  • Keystrokes typed during the flood were echoed behind megabytes of queued output, or lost in the gaps.
  • A Ctrl+C sent just after a backpressure close could be lost outright (6 of 39 trials). Logpoints showed the frame never reached the server, so the program kept running.

Over bb Connect the effect spreads beyond the terminal. A flood filled the tunnel shared by everything on the server's handle, and the owner's own remote session stalled. During one, /health through the handle took 2.8 s, then 14 s, then returned 504s.

Separately, every keystroke echo waited for the daemon's 4 ms output batch timer. That was most of bb's 6 ms idle overhead compared with plain xterm.js over a local socket.

Prompted by Discord reports that the in-app terminal feels slow ("not up to par", "a bit slow").

What changed

  • Browser: the transport (packages/client-core) opts in with an outputAcks=1 query flag. ThreadTerminalView acknowledges each output chunk after xterm has parsed it and reports document visibility.
  • Server (TerminalOutputFlowControl in apps/server/src/services/terminals):
    • It paces each terminal by its fastest visible acknowledging client. It enables daemon flow control while such a client exists and forwards that client's progress.
    • Hidden tabs, clients that stop acknowledging for 3 s while behind, and clients that never opt in (older apps, the CLI) don't pace the terminal. They keep the existing per-socket backpressure.
    • An earlier "slowest client" policy let a slow second viewer on a remote link cut local throughput 3–6×, so the fastest visible client governs.
  • Daemon (TerminalManager):
    • It pauses node-pty once 1 MiB of output is unacknowledged and resumes at 512 KiB.
    • If the process exits while the PTY is paused, it resumes it so node-pty can drain the final output before its 200 ms socket destroy.
    • Pacing is released when the server connection drops, the terminal closes, or the server disables it.
    • The first output after an idle period is flushed on setImmediate rather than after the 4 ms batch delay. Streaming output still batches.
  • Wire:
    • The server→daemon protocol gains terminal.flow-control and terminal.ack, so HOST_DAEMON_PROTOCOL_VERSION goes 228 → 229.
    • The browser terminal socket gains the outputAcks query flag and the ack and visibility client messages. Older browser clients are unaffected.
  • Not changed: CLI, SDK, docs and settings. Scrollback size and replay limits are unchanged.

How you verified

Measurement setup

  • A pnpm start:worktree build driven by headless Chrome through dev-browser, with Metal WebGL at DPR 2 and an 87×66 terminal.
  • Timings were taken in the page from the keydown event to xterm's parse callback and the next animation frame. A temporary probe exposed the xterm instance; it is not part of this PR.
  • Baselines: plain xterm.js with the same addons over a local node-pty socket, and node-pty drained without rendering.
  • The machine was under heavy unrelated load, so runs were repeated and compared same-build where possible.

Before → after (medians)

Measure Before After
Idle keystroke → frame 6.7 ms 1.9 ms (plain xterm.js: 0.8 ms)
Typing into a program that floods output and echoes keys 18 of 45 keystrokes lost, 210–250 ms for the rest, 38–66 socket drops per run 0 lost, 29–31 ms, no drops
Ctrl+C on yes until output stops 145–2,204 ms, up to 24 MB still to draw 25–62 ms, under 0.6 MB
Ctrl+C lost during a flood 6 of 39 0 of 30
Simulated 80 ms round trip, typing during a flood 11 of 15 keystrokes lost 0 lost at 143 ms
Simulated 80 ms round trip, seq 1 3000000 21–31 s 4.5–4.7 s
  • Throughput for find, seq, a 30 MB cat, git log --stat, a coloured build log and yes | head is unchanged within load noise. A same-build A/B with pacing switched off gives the same timings.
  • A second, slower viewer of the same terminal no longer slows the first.

Real bb Connect

  • The dev server was shared through bb Connect. While a terminal flooded, the shared tunnel's /health stayed at its resting 0.12–0.31 s, compared with the 504s on main.
  • Typing into a flooding program lost 0 of 45 keystrokes at 145–212 ms. Ctrl+C settled in 121–260 ms.

Tests

  • Daemon:
    • pauses one window ahead of acknowledged output and resumes on ack;
    • resumes a paused PTY whose process exited;
    • releases all pacing on disconnect;
    • sends the first output after idle without the batch delay.
  • Server:
    • flow-control policy: fastest visible client, hidden-only clients, stall exclusion and recovery, re-enable on a new daemon session;
    • an integration test from the browser ack to the daemon's terminal.ack.
  • Client: transport ack coalescing and re-reporting a hidden document after reconnect.
  • Contract tests updated for the new query flag and protocol version.

Commands run

  • pnpm exec turbo run typecheck --filter='...[origin/main]': 78 tasks. bb-plugin-bb--prompt-library passed after a pnpm install of the rebased lockfile.
  • pnpm exec turbo run test lint for @bb/host-daemon-contract, @bb/server-contract, @bb/client-core, @bb/host-daemon, @bb/server and @bb/app.
    • One unrelated builtin-plugins.test.ts hot-reload case timed out once under load and passed on rerun (28/28).
    • App lint: 0 errors; the warnings are existing ones.

🤖 Generated with Claude Code

AGENT GENERATED

## What was wrong

Nothing between the PTY and xterm.js applied backpressure. The daemon
read the PTY as fast as the program wrote, and the server queued output
for each browser socket until it passed 32 MiB, then closed the socket
with 1013 terminal-backpressure. The browser reconnected from its last
sequence, found the 4 MiB daemon ring had moved on, and reset the
terminal behind "Some terminal output was unavailable after reconnect".

Under sustained output (yes, a large cat or log, a chatty build) this
dropped and reset the terminal about once a second, wiping the screen
and scrollback. Keystrokes typed during the flood were echoed behind
megabytes of queued output or lost in the gaps. Input could be lost
outright: a Ctrl+C sent just after a backpressure close never reached
the server (6 of 39 trials) and the program kept running.

Every keystroke also waited for the daemon's 4 ms output batch timer.

## What changed

- The browser transport opts into output acknowledgement
  (outputAcks=1). The terminal view acknowledges each chunk once xterm
  has parsed it, and reports document visibility.
- The server paces each terminal by its fastest visible acknowledging
  client. It enables daemon flow control while one exists and forwards
  that client's progress. Hidden clients, clients that stop
  acknowledging for 3 s while behind, and clients that never opt in
  (older apps, the CLI) do not pace the terminal and keep today's
  per-socket backpressure.
- The daemon pauses node-pty once 1 MiB of output is unacknowledged and
  resumes at 512 KiB. A paused PTY whose process has exited is resumed
  so node-pty can drain its final output before its 200 ms socket
  destroy. Pacing is released when the server connection drops, the
  terminal closes, or the server disables it.
- The first output after an idle period is flushed on setImmediate
  instead of after the 4 ms batch delay; streaming output still
  batches.

The server/daemon wire gains terminal.flow-control and terminal.ack, so
HOST_DAEMON_PROTOCOL_VERSION is 229. The browser terminal socket gains
the outputAcks query flag and ack and visibility client messages.

## How you verified

Headless Chrome (dev-browser, Metal WebGL, DPR 2) against a
pnpm start:worktree build, an 87x66 terminal, timings taken inside the
page from the keydown event to xterm's parse callback and the next
animation frame. Medians, before -> after:

- Idle keystroke to frame: 6.7 ms -> 1.9 ms (plain xterm.js with the
  same addons over a local socket: 0.8 ms).
- Typing into a program that floods output and echoes keys: 18 of 45
  keystrokes lost and 210-250 ms for the rest, 38-66 socket drops per
  run -> 0 lost, 29-31 ms, no drops.
- Ctrl+C on yes until output stops: 145-2204 ms with up to 24 MB still
  to draw -> 25-62 ms with under 0.6 MB.
- Ctrl+C lost during a flood: 6 of 39 trials -> 0 of 30.
- Throughput for find, seq, a 30 MB cat, git log, a coloured build log
  and yes | head is unchanged within machine-load noise. A same-build
  A/B with pacing switched off gives the same timings.
- Through a proxy adding 80 ms RTT: keystrokes during a flood
  11 of 15 lost -> 0 lost at 143 ms; seq 3M 21-31 s -> 4.5-4.7 s.
- A second, slower viewer of the same terminal no longer slows the
  first.
- Through real bb Connect (a share of the dev server): a terminal flood
  stalled every request on the shared tunnel, including the owner's own
  remote session (/health 2.8 s, 14 s, then 504s). After, /health stays
  at its resting 0.12-0.31 s during a flood. Typing into a flooding
  program loses 0 of 45 keystrokes at 145-212 ms, and Ctrl+C settles in
  121-260 ms.

Turbo typecheck for every dependent of the changed contracts (77
tasks); tests for host-daemon-contract, server-contract, client-core,
host-daemon, server and app; app lint (existing warnings only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 8abfcd7 into main Oct 6, 2026
41 checks passed
@ymichael
ymichael deleted the bb/in-app-terminal-feels-slow-reproduce-and-measure-thr_buadiv2kqr branch October 6, 2026 21:01
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