feat(tui): per-sub-agent context accounting in the TUI#3486
Merged
Conversation
…inspector Track the latest token-usage snapshot per agent (fed by TokenUsageEvents, sub-sessions included) in the shared session state. Surface it as a right-aligned context percentage on each agent's sidebar roster line and as a "Context: <tokens> of <limit> tokens (N%)" line in the agent-details dialog. Background agent tasks (run_background_agent) previously dropped all events; the runtime now forwards their TokenUsageEvents through a new out-of-band Runtime.OnBackgroundEvent hook that the App bridges into the TUI event stream, so background agents join per-agent accounting and session cost totals. Token-usage events routed to inactive tabs no longer move the current-agent marker. Covered by unit tests at each layer and two e2e TUI scenarios driving the full background-agent journey through scripted model providers. Closes #3438
dgageot
approved these changes
Jul 6, 2026
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.
Closes #3438
Surfaces each agent's context usage (% of its window) in the TUI, including background agents. No competitor exposes per-sub-agent context accounting.
Issue expectations vs implementation
provider/model(model text yields room, keeps left-truncation)Context: 12.8K of 128.0K tokens (10%)line; bare token count when the limit is unknown (harness models); omitted until the agent has runpkg/tools/builtin/agent)runCollectingforwards the background sub-session'sTokenUsageEvents through a new out-of-bandRuntime.OnBackgroundEventhook;App.Startbridges them into the TUI event streamData flow
Rendering (frames captured by the e2e harness)
Design notes
usageContextPercenthelper is the single place to recolor once it lands.Usage(ContextLength/ContextLimit) is used rather than the feat(tui): add/contextcommand with a categorized context-window breakdown #3432ContextBreakdown: the breakdown needs the live session object, which is not available for arbitrary sub-sessions.RemoteRuntime.OnBackgroundEventis a documented no-op (background tasks run server-side), same convention asOnToolsChanged.Testing
TestRunAgentForwardsTokenUsageOutOfBand: runCollecting forwards only TokenUsageEvents, tagged with the sub-session id and worker nameTestApp_Start_ForwardsBackgroundEvents: Start registers the hook and events reach the app streamagent_context_test.go: per-agent %, last-snapshot-wins, unknown limit, background usage not disturbing the main gauge, narrow-width layoutTestAgentDetailsDialog_ContextUsage: full form, no-limit form, omission casesTestBackgroundAgent_PerAgentContextInSidebarandTestBackgroundAgent_InspectorShowsWorkerContext: full journey from tool call to detached sub-session to sidebar/inspectorFull suite,
-raceon touched packages,golangci-lint, and the repo's custom linters all pass.