Repository navigation
Skip unwatched terminal output lookups and build RPC result paths only on failure - #7240
Merged
ymichael merged 2 commits intoNov 9, 2026
Conversation
…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
marked this pull request as ready for review
November 9, 2026 17:48
ymichael
deleted the
bb/server-o2-terminal-api-cost-and-plugin-main-thre-thr_aemxuhadwf
branch
November 9, 2026 17:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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) andPOST /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 itsdata.dband 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 exitedterminal_sessionsrows (the snapshot has 25,542, down from 186k).Reproducing the plugin's pattern against a real server and daemon (15 concurrent create, then poll
getandoutputwithtailBytes=900000, then force close, with 300 KB of output per terminal) found two real per-call costs in core:terminal.outputchunk from the daemon read theterminal_sessionsrow 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 advancedforwardedNextSeqByTerminalIdfor 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.normalizeRpcJsonResultbuilt a path string for every node of every plugin RPC result. It shows up in the stall stacks ashost-policy.tsvisit(about 1 s of sampled stall time). On the usage plugin's realdashboardresult (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: forterminal.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.detachBrowserTerminalforgets 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:normalizeRpcJsonResultkeeps a stack of path segments and builds the path only when it reports a problem. It usesObject.keys/Reflect.getinstead ofObject.entriesandObject.getOwnPropertySymbolsinstead ofReflect.ownKeys().some(...). Messages, sparse-array handling and the cycle/DAG rules are unchanged.scripts/bump-plugin-sdk.mjs --patch), becausedist/internal/host-policy.jsanddist/testing/index.jschanged.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 withsinceSeq: 3(expected0); with the fix it passes. Its second case ("a browser watched and then left") fails withsinceSeq: 2without the detach change.Live repro (real server and daemon, a WebSocket client attaching like a browser,
kill -STOP/-CONTon the daemon): a viewer opening a terminal withsinceSeq=0during the disconnect started attick 3/tick 6on main and on the first commit, and starts atline 0with 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):
terminal_sessionslookups by idgetTerminalByIdfromhandleDaemonTerminalMessagein the CPU profileWall and CPU times in that repro were not usable: the load average was 35 to 60 from other fix threads.
normalizeRpcJsonResulton 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.stringifyof 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 withpublic-terminalsandplugin-wire.node packages/plugin-sdk/scripts/check-npm-version-guard.mjs: passes after the bump.🤖 Generated with Claude Code