Skip to content

Skip unwatched terminal output lookups and build RPC result paths only on failure - #7240

Merged
ymichael merged 2 commits into
mainfrom
bb/server-o2-terminal-api-cost-and-plugin-main-thre-thr_aemxuhadwf
Nov 9, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/server-o2-terminal-api-cost-and-plugin-main-thre-thr_aemxuhadwf

Conversation

@ymichael

@ymichael ymichael commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Production logs (Nov 6 08:00 to Nov 7 10:46) show slow terminal API calls: POST /api/v1/terminals (1,444 slow, median 160 ms), GET /terminals/:id/output (1,079, median 133 ms) and POST /terminals/:id/close (354, median 199 ms). Nearly all of them come from the usage plugin, which starts 10 to 15 terminals at once about every 12 minutes. 90% of these calls overlap an event-loop stall record (median max delay about 200 ms). During those bursts, the sampled stall stacks are mostly the usage plugin's own SQLite work on its data.db and its JS (about 15 of 30 sampled seconds), plus GC (6 s). Core's terminal code accounts for about 0.2 s. The daemon terminal handlers that the stall summary names (ws:daemon terminal.exited/replay/output) run for a median of 1 to 2 ms wall time, so that bucket is a labelling problem (O4), not slow work. The queries are indexed, and #7106 already prunes exited terminal_sessions rows (the snapshot has 25,542, down from 186k).

Reproducing the plugin's pattern against a real server and daemon (15 concurrent create, then poll get and output with tailBytes=900000, then force close, with 300 KB of output per terminal) found two real per-call costs in core:

  • Every terminal.output chunk from the daemon read the terminal_sessions row before checking whether any browser was attached. Headless terminals never have a browser attached, so each chunk cost a query for nothing. The handler also advanced forwardedNextSeqByTerminalId for output nobody received. Because of that, a browser that reattaches while the daemon is disconnected caught up from that sequence and missed the output in between.
  • normalizeRpcJsonResult built a path string for every node of every plugin RPC result. It shows up in the stall stacks as host-policy.ts visit (about 1 s of sampled stall time). On the usage plugin's real dashboard result (4,888 rows, 2.35 MB of JSON) it took 10 to 14 ms median, about 4× JSON.stringify.

What changed

  • apps/server/src/services/terminals/terminal-session-lifecycle.ts: for terminal.output, the handler still records flow-control output, then returns before the DB lookup when the terminal has no browser clients. Forwarding to attached browsers works as before.
  • Same file: detachBrowserTerminal forgets the forwarded sequence when the last browser leaves. Otherwise a browser that watched and left kept it at its last position, and a new browser attaching during a daemon disconnect caught up from there and missed the earlier output.
  • packages/plugin-sdk/src/internal/host-policy.ts: normalizeRpcJsonResult keeps a stack of path segments and builds the path only when it reports a problem. It uses Object.keys/Reflect.get instead of Object.entries and Object.getOwnPropertySymbols instead of Reflect.ownKeys().some(...). Messages, sparse-array handling and the cycle/DAG rules are unchanged.
  • Plugin SDK bumped to 0.6.134 (scripts/bump-plugin-sdk.mjs --patch), because dist/internal/host-policy.js and dist/testing/index.js changed.
  • No wire, CLI or docs changes.

The usage plugin's own main-thread cost (its SQLite transactions and its 335 ms dashboard query) is not changed here. A proposal for isolating plugin server work is in the coordinator's report.

How you verified

  • New test public-terminals.test.ts › "catches a reattached browser up from its own position after output no browser was watching". On origin/main it fails with sinceSeq: 3 (expected 0); with the fix it passes. Its second case ("a browser watched and then left") fails with sinceSeq: 2 without the detach change.

  • Live repro (real server and daemon, a WebSocket client attaching like a browser, kill -STOP/-CONT on the daemon): a viewer opening a terminal with sinceSeq=0 during the disconnect started at tick 3/tick 6 on main and on the first commit, and starts at line 0 with both changes.

  • New packages/plugin-sdk/src/__tests__/rpc-json-result.test.ts: covers the exact path messages for non-finite, undefined, class-instance, symbol-key and cyclic values, and checks that shared non-cyclic values and sparse arrays still copy.

  • Burst repro (real server and daemon in one process, 3 rounds of 15 terminals with 300 KB of output each):

    before after
    SQL statements executed 7,623 1,477
    terminal_sessions lookups by id 7,378 (one per output chunk + per API call) 1,236 (per API call only)
    getTerminalById from handleDaemonTerminalMessage in the CPU profile 485 ms 0

    Wall and CPU times in that repro were not usable: the load average was 35 to 60 from other fix threads.

  • normalizeRpcJsonResult on the real dashboard payload: median 9.9 to 14.5 ms (min 5.8) before, 2.4 to 2.6 ms (min 2.2) after, over 3 runs of 40 at load average about 55. JSON.stringify of the same value takes 2.5 to 3.8 ms.

  • pnpm exec turbo run typecheck --filter=@bb/server --filter=@get-bb/plugin-sdk --filter=@bb/domain: passes.

  • pnpm exec turbo run test --filter=@get-bb/plugin-sdk --filter=@bb/domain: passes (418 and 316 tests).

  • pnpm exec turbo run test --filter=@bb/server: 3,939 passed and 2 timed out (install-machine-script, timeline-in-turn-window, both 15 s timeouts at load about 40). Both files pass on rerun, along with public-terminals and plugin-wire.

  • node packages/plugin-sdk/scripts/check-npm-version-guard.mjs: passes after the bump.

AGENT GENERATED

🤖 Generated with Claude Code

…y on failure

Every daemon terminal.output chunk read its terminal_sessions row before
checking whether any browser was attached. Headless terminals, such as the
usage plugin's scans, never have one, so each chunk cost a query for nothing.
The handler also advanced the forwarded sequence for output nobody received,
so a browser reattaching during a daemon disconnect caught up from that
sequence and missed the output in between. The handler now returns before
the lookup when the terminal has no browser clients.

normalizeRpcJsonResult built a path string for every node of an RPC result
while copying it. It now keeps a segment stack and renders the path only
when it reports a problem.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The forwarded sequence outlived the browsers it described. A browser that
watched a terminal and then left kept it at its last position, so a new
browser attaching while the daemon was disconnected caught up from there
and missed the output before it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael marked this pull request as ready for review November 9, 2026 17:48
@ymichael
ymichael merged commit 86a21ca into main Nov 9, 2026
50 checks passed
@ymichael
ymichael deleted the bb/server-o2-terminal-api-cost-and-plugin-main-thre-thr_aemxuhadwf branch November 9, 2026 17:50
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