Repository navigation
Show provider context breakdowns for Claude Code - #3540
Merged
Merged
Conversation
ymichael
force-pushed
the
bb/support-this-in-bb-thr_er5gp74huw
branch
from
September 11, 2026 21:38
2a38451 to
56b8bbc
Compare
This was referenced Oct 30, 2026
ymichael
added a commit
that referenced
this pull request
Dec 1, 2026
## Human comments ## What was wrong Issue #8233 reports two symptoms. Each has its own cause. 1. "Show details" disappears while a turn runs. `extractThreadContextWindowUsage` ([thread-context-window-usage.ts](https://github.com/get-bb/bb/blob/b0ddaad731565d7e4de68d824120d802fc78f7d5/packages/thread-view/src/thread-context-window-usage.ts#L104-L141)) took the snapshot only from the newest usage event, and kept it only when its total equalled that event's total. Claude Code measures a breakdown only after a result, a session start or a compaction. Mid-turn events carry an estimated total and no snapshot, so the first estimate after a send removed the breakdown, and with it the card's "Show details" button. The SLOP COP report shows this in the projection and in the rendered card: https://get-bb.github.io/reports/issues/8233.html 2. The ring disappears right after a send. The composer hides the ring whenever the newest usage event has `usedTokens: null`, which the projection reads as a context reset. The Claude bridge sent `null` in three cases where usage was not actually unknown: - **Stale breakdown read.** After each result, the bridge reads the breakdown in an isolated forked session (up to 5 s). If the next prompt opens a turn before that read ends, the read is stale, and the bridge sent only the capacity, as `used: state.latestRequestContextTokens ?? null` ([delta-translation.ts](https://github.com/get-bb/bb/blob/b0ddaad731565d7e4de68d824120d802fc78f7d5/plugins/provider-claude-code/src/delta-translation.ts#L1550-L1563)). A new turn has no request size yet, so that was `null`. - **Session resume.** Every session attach (`sendSessionReset`) sent `used: null`, including resumes after the idle-session timeout, a daemon restart or a settings-driven rebuild. A resume keeps the same conversation, so its usage is still valid. - **Compaction.** After a compaction, the bridge still held the pre-compaction request size, so a stale read that finished after compaction would report it as current usage. ## What changed - `packages/thread-view`: the projection keeps the last snapshot of the same provider session through later estimates. It drops the snapshot at the boundaries that already end the retained auto-compact window from #4690: a reset (`usedTokens: null`), a measured (non-estimated) aggregate, a provider session change, or a different context window. A context clear still starts a new range on the server. The server reader (`getLatestContextWindowBoundary`) and the usage pruning already keep that boundary row, so no query or pruning change is needed. `public-thread-context.test.ts` shows this through SQLite and the HTTP API. The card shows the kept breakdown as-is, like any other value that is stale until refreshed; there is no extra label, so the card's layout does not shift as the live total moves. - `apps/cli`: `bb thread context` prints the measured total on the `Captured:` line, for example `Captured: 2026-09-11T12:00:00.000Z at 80 tokens`. JSON output is unchanged; `snapshot.usedTokens` already holds the value. - `plugins/provider-claude-code`: - `setClaudeReportedContextWindow` still records the capacity, but returns no delta while no request size is known. The next usage delta carries the new capacity. - `attachThreadSession` takes `origin: "start" | "resume" | "fork"`. Only new and forked sessions report usage as unknown; resumes and in-place session rebuilds (`replaceThreadSession`) keep it. - A compaction boundary clears the last request size. - The claude-code `resume` and `stop-interrupt` parity recordings are re-recorded; each drops the resume's usage reset. No wire field changed, so `HOST_DAEMON_PROTOCOL_VERSION` stays the same. The `snapshot` field keeps its schema. It can now hold a breakdown measured at a lower total than `usedTokens`; consumers can compare `snapshot.usedTokens` with `usedTokens`. ### Decision for a maintainer #3540 chose "New aggregate-only events do not retain an older breakdown". This PR changes that choice, because #4690 later kept the snapshot's window through the same estimates, and the server and pruning already keep the snapshot row for it. This PR uses the same boundaries for the breakdown. ## How you verified Each new or changed test fails on `main` (b0ddaad) and passes with this change: | Test | Before | After | |---|---|---| | `thread-context-window-usage.test.ts`: "keeps the last measured snapshot through later estimates of the same session", "drops the snapshot after a measured aggregate or a window change", "does not attach a snapshot measured for another window" | snapshot missing | pass | | `public-thread-context.test.ts` (server, SQLite + `/api/v1/threads/:id/context`) | `usage` without `snapshot` after an estimate | pass | | `bridge.test.ts`: "keeps context usage when a stale context read finishes after the next prompt is sent" | `usedTokens` sequence `[450, 0, 0, null, 0]` | pass, no `null` | | `bridge.test.ts`: "does not report the pre-compaction request size when a stale read finishes after compaction" | reports the pre-compaction size | pass | | `bridge.test.ts`: "$request reports context usage as unknown only for a new conversation" (start vs resume) | resume reports `null` | pass | | `thread-context.test.ts` (CLI text output) | no measured total | pass | After merging current `main` (which changed `attachThreadSession` for fork boundaries), `turbo run typecheck test lint` passes for `bb-plugin-provider-claude-code`, `@bb/provider-parity`, `@bb/thread-view` and `@bb/cli`. Live (`pnpm start:worktree`, real Claude Code Haiku 5.5 turns, the same thread and data dir on main and on this branch, the context route polled every 100 ms, the ring watched in a headless browser at 1280×800 and 390×844): | Scenario | main | This branch | |---|---|---| | Hover the ring after the first tool call of a multi-step turn | Card shows only the total; no "Show details" | "Show details" stays and lists the breakdown last measured at 24,592 tokens while the total grows to 25,150 | | `bb thread context` mid-turn | `Estimated context: 26,088 / 1,000,000 tokens` only | Total, `Captured: … at 24,592 tokens`, and every category | | First send after a session resume (server restart) | Ring disappears for about 1.1 s (`usage` is `null`) on desktop and phone | Ring stays; usage is never `null` | | Queued follow-up and `bb thread compact` | No gap observed (the races are timing-dependent) | No gap; compaction reports the post-compaction size | Fixes #8233 > AGENT GENERATED --------- Co-authored-by: Michael Yong <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
scowalt
pushed a commit
to scowalt/bb
that referenced
this pull request
Dec 1, 2026
## Human comments ## What was wrong Issue get-bb#8233 reports two symptoms. Each has its own cause. 1. "Show details" disappears while a turn runs. `extractThreadContextWindowUsage` ([thread-context-window-usage.ts](https://github.com/get-bb/bb/blob/a8c32288cf24894fbe85cb3a9ca0469c7a997ed6/packages/thread-view/src/thread-context-window-usage.ts#L104-L141)) took the snapshot only from the newest usage event, and kept it only when its total equalled that event's total. Claude Code measures a breakdown only after a result, a session start or a compaction. Mid-turn events carry an estimated total and no snapshot, so the first estimate after a send removed the breakdown, and with it the card's "Show details" button. The SLOP COP report shows this in the projection and in the rendered card: https://get-bb.github.io/reports/issues/8233.html 2. The ring disappears right after a send. The composer hides the ring whenever the newest usage event has `usedTokens: null`, which the projection reads as a context reset. The Claude bridge sent `null` in three cases where usage was not actually unknown: - **Stale breakdown read.** After each result, the bridge reads the breakdown in an isolated forked session (up to 5 s). If the next prompt opens a turn before that read ends, the read is stale, and the bridge sent only the capacity, as `used: state.latestRequestContextTokens ?? null` ([delta-translation.ts](https://github.com/get-bb/bb/blob/a8c32288cf24894fbe85cb3a9ca0469c7a997ed6/plugins/provider-claude-code/src/delta-translation.ts#L1550-L1563)). A new turn has no request size yet, so that was `null`. - **Session resume.** Every session attach (`sendSessionReset`) sent `used: null`, including resumes after the idle-session timeout, a daemon restart or a settings-driven rebuild. A resume keeps the same conversation, so its usage is still valid. - **Compaction.** After a compaction, the bridge still held the pre-compaction request size, so a stale read that finished after compaction would report it as current usage. ## What changed - `packages/thread-view`: the projection keeps the last snapshot of the same provider session through later estimates. It drops the snapshot at the boundaries that already end the retained auto-compact window from get-bb#4690: a reset (`usedTokens: null`), a measured (non-estimated) aggregate, a provider session change, or a different context window. A context clear still starts a new range on the server. The server reader (`getLatestContextWindowBoundary`) and the usage pruning already keep that boundary row, so no query or pruning change is needed. `public-thread-context.test.ts` shows this through SQLite and the HTTP API. The card shows the kept breakdown as-is, like any other value that is stale until refreshed; there is no extra label, so the card's layout does not shift as the live total moves. - `apps/cli`: `bb thread context` prints the measured total on the `Captured:` line, for example `Captured: 2026-09-11T12:00:00.000Z at 80 tokens`. JSON output is unchanged; `snapshot.usedTokens` already holds the value. - `plugins/provider-claude-code`: - `setClaudeReportedContextWindow` still records the capacity, but returns no delta while no request size is known. The next usage delta carries the new capacity. - `attachThreadSession` takes `origin: "start" | "resume" | "fork"`. Only new and forked sessions report usage as unknown; resumes and in-place session rebuilds (`replaceThreadSession`) keep it. - A compaction boundary clears the last request size. - The claude-code `resume` and `stop-interrupt` parity recordings are re-recorded; each drops the resume's usage reset. No wire field changed, so `HOST_DAEMON_PROTOCOL_VERSION` stays the same. The `snapshot` field keeps its schema. It can now hold a breakdown measured at a lower total than `usedTokens`; consumers can compare `snapshot.usedTokens` with `usedTokens`. ### Decision for a maintainer get-bb#3540 chose "New aggregate-only events do not retain an older breakdown". This PR changes that choice, because get-bb#4690 later kept the snapshot's window through the same estimates, and the server and pruning already keep the snapshot row for it. This PR uses the same boundaries for the breakdown. ## How you verified Each new or changed test fails on `main` (a8c3228) and passes with this change: | Test | Before | After | |---|---|---| | `thread-context-window-usage.test.ts`: "keeps the last measured snapshot through later estimates of the same session", "drops the snapshot after a measured aggregate or a window change", "does not attach a snapshot measured for another window" | snapshot missing | pass | | `public-thread-context.test.ts` (server, SQLite + `/api/v1/threads/:id/context`) | `usage` without `snapshot` after an estimate | pass | | `bridge.test.ts`: "keeps context usage when a stale context read finishes after the next prompt is sent" | `usedTokens` sequence `[450, 0, 0, null, 0]` | pass, no `null` | | `bridge.test.ts`: "does not report the pre-compaction request size when a stale read finishes after compaction" | reports the pre-compaction size | pass | | `bridge.test.ts`: "$request reports context usage as unknown only for a new conversation" (start vs resume) | resume reports `null` | pass | | `thread-context.test.ts` (CLI text output) | no measured total | pass | After merging current `main` (which changed `attachThreadSession` for fork boundaries), `turbo run typecheck test lint` passes for `bb-plugin-provider-claude-code`, `@bb/provider-parity`, `@bb/thread-view` and `@bb/cli`. Live (`pnpm start:worktree`, real Claude Code Haiku 5.5 turns, the same thread and data dir on main and on this branch, the context route polled every 100 ms, the ring watched in a headless browser at 1280×800 and 390×844): | Scenario | main | This branch | |---|---|---| | Hover the ring after the first tool call of a multi-step turn | Card shows only the total; no "Show details" | "Show details" stays and lists the breakdown last measured at 24,592 tokens while the total grows to 25,150 | | `bb thread context` mid-turn | `Estimated context: 26,088 / 1,000,000 tokens` only | Total, `Captured: … at 24,592 tokens`, and every category | | First send after a session resume (server restart) | Ring disappears for about 1.1 s (`usage` is `null`) on desktop and phone | Ring stays; usage is never `null` | | Queued follow-up and `bb thread compact` | No gap observed (the races are timing-dependent) | No gap; compaction reports the post-compaction size | Fixes get-bb#8233 > AGENT GENERATED --------- Co-authored-by: Michael Yong <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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
The context window menu showed only total usage, so users could not tell whether messages, instructions, skills, or tools were consuming the window. Claude Code exposes this breakdown, but bb did not collect or carry it through to the UI.
What changed
ContextSnapshot: capture/session/model metadata, token totals, and provider-defined categories/entries. Category names are not hard-coded in the contract; only accounting kinds (used,free,reserved,deferred) are standardized. Claude-specific translation stays in the provider bridge.GET /api/v1/threads/:id/context,sdk.threads.context(), andbb thread context [id] --self --json, plus guide/configuration documentation. No database migration.HOST_DAEMON_PROTOCOL_VERSIONincreases from 203 to 204 for the wire change; plugin SDK increases from 0.4.84 to 0.4.85 for the new exports.How you verified
Updated Claude replay recordings and pinned event counts for context invalidation; all 56 provider parity tests pass. Documented the new exports in the authoring reference and verified its completeness check.
Relevant Turbo tests/typechecks passed for the provider bridge, Claude normalization and stale-read/timeouts, event projection, SQLite API persistence, CLI output, SDK types, contracts, and settings compatibility. The UI automatically shows details when a snapshot is available and removes them when only aggregate usage remains.
A real Claude turn produced matching API, CLI, and UI totals/categories. Context clear removed usage. Compaction invalidation is covered by a controlled bridge test; the live manual compaction attempt was declined because the test thread had too few messages.
Checked the single Storybook route on desktop and mobile Chromium, including the narrower card, animated expansion, stable toggle position, and scrolling. Removed the experiment flag, settings control, and documentation; the breakdown is available by default when supported.