Skip to content

feat(tui): per-sub-agent context accounting in the TUI#3486

Merged
dgageot merged 1 commit into
mainfrom
feat/tui-per-agent-context
Jul 6, 2026
Merged

feat(tui): per-sub-agent context accounting in the TUI#3486
dgageot merged 1 commit into
mainfrom
feat/tui-per-agent-context

Conversation

@Sayt-0

@Sayt-0 Sayt-0 commented Jul 6, 2026

Copy link
Copy Markdown
Member

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

Expectation Implementation
Per-agent / per-sub-session context % in the sidebar agent list Each roster entry's line 2 now carries the agent's latest known context percentage, right-aligned next to provider/model (model text yields room, keeps left-truncation)
Same in the agent-details dialog New Context: 12.8K of 128.0K tokens (10%) line; bare token count when the limit is unknown (harness models); omitted until the agent has run
Include background agents (pkg/tools/builtin/agent) runCollecting forwards the background sub-session's TokenUsageEvents through a new out-of-band Runtime.OnBackgroundEvent hook; App.Start bridges them into the TUI event stream

Data flow

TokenUsageEvent (session id + agent name + Usage incl. ContextLimit)
 ├─ interactive stream (parent / transfer_task / run_skill) ──────► a.events
 └─ background task (runCollecting, events otherwise dropped)
      └─ emitBackgroundEvent ──► OnBackgroundEvent ──────────────► a.events   [new]

chatPage.handleTokenUsage ──► sidebar.SetTokenUsage
 ├─ sessionUsage[sessionID]          (existing: main gauge, cost totals)
 └─ SessionState.SetAgentUsage()     [new: per-agent snapshot, last write wins]
      ├─ sidebar roster line 2 ("N%")
      └─ agent-details dialog ("Context: ...")

Rendering (frames captured by the e2e harness)

 Agents ───────────────────────────────
 ▶ root                              ^1
   test/fake-root                   10%

   worker                            ^2
   test/fake-worker                 45%
 │  Model: test/fake-worker                       │
 │  Context: 450 of 1.0K tokens (45%)             │

Design notes

  • Roster granularity is per agent (last snapshot wins). Concurrent background tasks of the same agent alternate its displayed %; per-session accounting stays intact underneath (cost totals, sub-session count). If per-task rows seem more appropriate, this can be extended later.
  • The percentage renders as plain muted text. feat(tui): add warning/compacting states to the context-usage gauge #3434 (warning/compacting gauge states) is not implemented yet; the shared usageContextPercent helper is the single place to recolor once it lands.
  • Provider-reported Usage (ContextLength/ContextLimit) is used rather than the feat(tui): add /context command with a categorized context-window breakdown #3432 ContextBreakdown: the breakdown needs the live session object, which is not available for arbitrary sub-sessions.
  • RemoteRuntime.OnBackgroundEvent is a documented no-op (background tasks run server-side), same convention as OnToolsChanged.
  • Token-usage events routed to inactive tabs no longer move the current-agent marker: a background task's usage can arrive while the tab is idle and previously would have pinned the marker to the worker.
  • Per-agent snapshots are in-memory; after a session restore only the current agent's % is re-seeded (by the startup TokenUsageEvent) until other agents run again.

Testing

Layer Test
Runtime TestRunAgentForwardsTokenUsageOutOfBand: runCollecting forwards only TokenUsageEvents, tagged with the sub-session id and worker name
App TestApp_Start_ForwardsBackgroundEvents: Start registers the hook and events reach the app stream
Sidebar agent_context_test.go: per-agent %, last-snapshot-wins, unknown limit, background usage not disturbing the main gauge, narrow-width layout
SessionState set/get, empty-name guard, replacement
Dialog TestAgentDetailsDialog_ContextUsage: full form, no-limit form, omission cases
e2e (real TUI, real runtime, scripted providers) TestBackgroundAgent_PerAgentContextInSidebar and TestBackgroundAgent_InspectorShowsWorkerContext: full journey from tool call to detached sub-session to sidebar/inspector

Full suite, -race on touched packages, golangci-lint, and the repo's custom linters all pass.

…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
@Sayt-0 Sayt-0 requested a review from a team as a code owner July 6, 2026 11:42
@Sayt-0 Sayt-0 added area/agent For work that has to do with the general agent loop/agentic features of the app area/tui For features/issues/fixes related to the TUI kind/feat PR adds a new feature (maps to feat:). Use on PRs only. labels Jul 6, 2026
@dgageot dgageot merged commit cfe64ae into main Jul 6, 2026
18 checks passed
@dgageot dgageot deleted the feat/tui-per-agent-context branch July 6, 2026 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent For work that has to do with the general agent loop/agentic features of the app area/tui For features/issues/fixes related to the TUI kind/feat PR adds a new feature (maps to feat:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(tui): per-sub-agent context accounting in the TUI

2 participants