Skip to content

fix(tui): include sub-agent costs in session cost - #58

Merged
jkyberneees merged 1 commit into
mainfrom
fix/subagent-session-cost
Sep 1, 2026
Merged

fix(tui): include sub-agent costs in session cost#58
jkyberneees merged 1 commit into
mainfrom
fix/subagent-session-cost

Conversation

@jkyberneees

Copy link
Copy Markdown
Contributor

What

Sub-agent LLM spend was missing from every session-cost surface: the header, the /stats cost row, and the cockpit cost-cap row all computed costUSD(sessCtxTok, sessOutTok, prices) — main-loop tokens only. Each finished sub-agent's engine-reported cost_usd (wire v2 P6) decorated its card in the transcript but never reached the session figure.

Fix

  • Bank each finished task's final cost per task_id (Model.subCosts), upserted from finished subagent_state frames — replayed frames overwrite instead of double-counting.
  • Banking happens before the card-attach attempt, so stray/resumed/late frames still count — spent is spent.
  • Add subCostTotal() to all three surfaces (header, /stats, cockpit cap row — the cap judges total spend).
  • Reset with the other session telemetry on session swap (resume) and /clear — no cross-session leakage.
  • Absent/zero cost records nothing: unavailable is never $0.

The framed result envelope stays display-only (it carries no task_id, so it cannot upsert safely); state frames are the single source of truth.

Tests (RED-first)

6 new tests in internal/tui/subagent_cost_test.go; the 4 behavioral tests were confirmed failing before the fix:

  • TestSubagentCostAddsToHeader — header = main + sub ($0.016 + $0.0175 = $0.0335)
  • TestSubagentCostReplayIsIdempotent — replayed finish frames never double-count
  • TestStatsCardIncludesSubagentCost — /stats cost row carries sub-agent spend
  • TestCockpitCapRowIncludesSubagentCost — cap row compares total spend against the cap
  • TestSubagentNoCostReportsNothing — guard: absent cost never distorts the figure
  • TestClearResetsSubagentCost — guard: /clear wipes banked costs

Verification

make fmt · make vet clean · make lint 0 issues · make test (go test -race ./...) all green.

Session spend (header, /stats, cockpit cap row) counted main-loop
tokens only: each finished sub-agent's engine-reported cost_usd
decorated its card but never reached the session figure. Bank the
final cost per task id from finished subagent_state frames - upsert,
so replayed frames never double-count - add the sum to all three
surfaces, and reset it with the other session telemetry on session
swap and clear. Absent cost stays absent: unavailable is never $0.
@jkyberneees
jkyberneees merged commit 2495b36 into main Sep 1, 2026
7 checks passed
@jkyberneees
jkyberneees deleted the fix/subagent-session-cost branch September 1, 2026 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant