Repository navigation
Keep the context breakdown and ring during a running turn - #8288
Merged
Merged
Conversation
The shared projection took the snapshot only from the newest context usage event. Mid-turn Claude events carry a total and no snapshot, so the card lost "Show details" at the first estimate after a send. Now the projection keeps the last snapshot of the same provider session. It drops the snapshot at the boundaries that already end the retained window: a reset, a measured aggregate, a session change, or a different window. The server reader and the event pruning already keep that row. The card and `bb thread context` show the total that the breakdown was measured at. The Claude bridge reads the breakdown after each result. If the next prompt opens a turn before the read ends, the read is stale and the bridge sent only the capacity, with `used: null` because the new turn had no request size yet. The projection reads `null` as a reset, so the ring disappeared until the first reply. The bridge now updates the capacity and sends no delta until a request size is known. Fixes get-bb#8233
A resumed Claude Code session (after the idle-session timeout, a daemon restart, or a settings-driven rebuild) reported its context usage as unknown, so the composer hid the ring until the first reply of the next turn. Resuming keeps the same conversation, so only new and forked sessions now reset the usage. A context compaction now forgets the last request size, so a context read that finishes after compaction no longer reports the pre-compaction size as the current usage. The claude-code resume and stop-interrupt parity lanes are re-recorded: each drops the resume's usage reset. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The "Measured at N tokens" line appeared and disappeared as the live total moved away from the last breakdown, shifting the card's layout during a turn. The breakdown now shows as measured, like any other stale-until-refreshed value. Co-Authored-By: Claude Opus 5.5 <[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
Issue #8233 reports two symptoms. Each has its own cause.
extractThreadContextWindowUsage(thread-context-window-usage.ts) 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.htmlusedTokens: null, which the projection reads as a context reset. The Claude bridge sentnullin three cases where usage was not actually unknown:used: state.latestRequestContextTokens ?? null(delta-translation.ts). A new turn has no request size yet, so that wasnull.sendSessionReset) sentused: 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.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 fix: retain auto-compact context window between estimates #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.tsshows 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 contextprints the measured total on theCaptured:line, for exampleCaptured: 2026-09-11T12:00:00.000Z at 80 tokens. JSON output is unchanged;snapshot.usedTokensalready holds the value.plugins/provider-claude-code:setClaudeReportedContextWindowstill records the capacity, but returns no delta while no request size is known. The next usage delta carries the new capacity.attachThreadSessiontakesorigin: "start" | "resume" | "fork". Only new and forked sessions report usage as unknown; resumes and in-place session rebuilds (replaceThreadSession) keep it.resumeandstop-interruptparity recordings are re-recorded; each drops the resume's usage reset.No wire field changed, so
HOST_DAEMON_PROTOCOL_VERSIONstays the same. Thesnapshotfield keeps its schema. It can now hold a breakdown measured at a lower total thanusedTokens; consumers can comparesnapshot.usedTokenswithusedTokens.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: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"public-thread-context.test.ts(server, SQLite +/api/v1/threads/:id/context)usagewithoutsnapshotafter an estimatebridge.test.ts: "keeps context usage when a stale context read finishes after the next prompt is sent"usedTokenssequence[450, 0, 0, null, 0]nullbridge.test.ts: "does not report the pre-compaction request size when a stale read finishes after compaction"bridge.test.ts: "$request reports context usage as unknown only for a new conversation" (start vs resume)nullthread-context.test.ts(CLI text output)After merging current
main(which changedattachThreadSessionfor fork boundaries),turbo run typecheck test lintpasses forbb-plugin-provider-claude-code,@bb/provider-parity,@bb/thread-viewand@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):bb thread contextmid-turnEstimated context: 26,088 / 1,000,000 tokensonlyCaptured: … at 24,592 tokens, and every categoryusageisnull) on desktop and phonenullbb thread compactFixes #8233