feat(tui): add /context command with a categorized context-window breakdown#3458
Merged
Conversation
…akdown Add a /context slash command that opens a dialog (consistent with the /cost dialog) showing the estimated composition of the context window, broken down by category: system prompt, tool definitions, prompt files, conversation messages, tool results, and compaction summary. The breakdown is computed in pkg/runtime from Session.GetMessages output using the usage-calibrated compaction estimator, plus tool schema and add_prompt_files estimates. Percentages are relative to the resolved context limit (same source as the sidebar gauge); the dialog renders a stacked usage bar, per-category rows with item counts, free space, and an explicit estimates disclaimer. Numbers can be copied as plain text. The computation runs in a tea.Cmd goroutine so lazily-started toolsets never block the UI, and remote runtimes report the feature as unavailable via an optional capability interface. Closes #3432
rumpl
approved these changes
Jul 3, 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 #3432
Summary
Adds a
/contextslash command that opens a dialog (consistent with the existing/costdialog) showing the estimated context-window composition, broken down by category, with a stacked usage bar, per-category rows, free space, and a copy-to-clipboard action.Bar segments and row markers use per-category theme colors; the free space remainder is muted.
Issue expectations
/contextcommandpkg/tui/commands/commands.go(session.context): palette, slash parser, completion; compatible with--disable-commandsmessages.ShowContextDialogMsginpkg/tui/messages/toggle.go, next to the siblingShow*DialogMsgtypes (the issue sketchedsession.go;toggle.gois where the existing dialog messages live)pkg/tui/dialog/context.go, modeled oncost.go: scrollview,ccopies a plain-text version,Esc/Enter/qclosespkg/runtimefromSession.GetMessagespkg/runtime/context_breakdown.go:ContextBreakdown/ContextCategorytypes,(*LocalRuntime).ContextBreakdown(ctx, sess)compaction.EstimateMessageTokens, labeled as suchcompaction.NewSliceEstimator(same estimator the proactive compaction trigger uses); explicit disclaimer line in the dialogcontextLimitForAgentModel(same source as the sidebar gauge, including compaction-model capping); "context limit unknown" fallback for harness agents and uncatalogued modelstoolcommon/tokencount.goFormatTokenCounthelperFlow
Design decisions
ContextBreakdownEvent". An on-demand method was implemented instead: the data is fresh at dialog-open time and there is a single consumer today. feat(tui): per-sub-agent context accounting in the TUI #3438 can callLocalRuntime.ContextBreakdowndirectly; an event can be layered on later if feat(runtime): cross-agent compaction orchestration (explicit, user-triggered) #3439 needs one.pkg/app(same pattern asagentConfigProvider): remote runtimes show an info toast ("Context breakdown is not supported with remote runtimes").tea.Cmdgoroutine (unlike/tools, which blocks the Update loop) because tool listing may lazily start MCP toolsets.session.SummaryMessageContent()andSession.LastSummary()helpers classify the summary message by exact content, so a user message that merely starts with "Session Summary: " stays in the conversation bucket (pinned by a test). The two former string literals insession.gonow share the same helper.Testing
pkg/runtime(8 tests)pkg/tui/dialog(9 tests)pkg/sessionLastSummary,SummaryMessageContent/GetMessagesexact-content contractpkg/tui/commands/contextparse testgo build ./...,golangci-lint run ./...(0 issues),go run ./lint .(no offenses), full test suite green except pre-existing environment-dependent SSRF/network tests that also fail on a clean checkout ofmain.