Skip to content

[Lens viz] Cost-by-component + recoverable-waste overlay (#211) - #224

Merged
anilmurty merged 1 commit into
mainfrom
feat/211-recoverable-waste-overlay
Jun 23, 2026
Merged

anilmurty merged 1 commit into
mainfrom
feat/211-recoverable-waste-overlay

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

The differentiated Lens viz (milestone #3): split the window's spend into the four token components and overlay each optimize analyzer's attributed recoverable waste — the cost-WASTE view a generic activity dashboard can't produce, because it depends on our analyzers' attributed estimates. Data is shaped server-side; the UI renders from the response and consumes the framing block (never re-derives), uses vendored uPlot (offline intact), and respects plan-tier framing.

Verification (screenshot)

Delivered to the reviewer to attach here (no binaries committed to the repo, per the hard rule). It shows: the left Cost bar stacked by the 4 token components; the right Recoverable (est.) bar stacked by analyzer; the honest caption ("…a heuristic estimate to review before acting, not a realized cost reduction"); and the per-analyzer list — Downsize → whole-call, Cache → input, Reuse → whole-call, each tagged estimated recoverable with its caveat verbatim. (Reuse appears straight from the findings dict — registry-driven.)

Summary

  • New GET /api/v1/cost/components — component cost split (input / output / cache-read / cache-write), priced per (provider, model) via the pricing table (exact, not apportioned from aggregate cost_usd) + a registry-driven per-analyzer recoverable list.
  • New ComponentWasteChart (uPlot stacked bars) on the Optimize screen: measured Cost vs Recoverable (est.) as two separate columns, plus a per-analyzer recoverable list.

Honesty discipline (Rule 14 — the reason #211 was split out)

  • Recoverable is a separate bar, never subtracted from measured cost, and the word "saved" never appears on this surface — the caption says "a heuristic estimate to review before acting, not a realized cost reduction"; figures carry the "estimated recoverable" tag.
  • Each analyzer keeps its own caveat + estimate_basis verbatim (downsize's MODEL_DOWNGRADE_CAVEAT, reuse's basis, etc.).
  • Every figure routes through the framing helpers (fmtFramedSavings for recoverable — same as the existing recoverable band — fmtFramedDollar for measured cost), so subscription/local see token-share, and the bars themselves switch to token volume.

Registry-driven

_collect_recoverable iterates the typed downgrade slot plus every finding carrying the #111 estimated_recoverable_usd contract field — a new analyzer surfaces with no code change. Component attribution defaults to "call" (whole-call) for analyzers that can't be honestly pinned to one token component (downsize / script / reuse); cache + trim → input. The test proves it by asserting reuse (a findings-dict analyzer, not a typed slot) appears automatically.

Tests / Verification

  • tests/integration/test_cost_charts.py (+4): per-component pricing + exact token volumes; registry-driven recoverable (reuse present) with the downsize caveat verbatim and "saved" absent from the payload; subscription framing suppresses dollars; empty-window safe.
  • tests/unit/test_lens_ui_regression.py (+5): chart/helpers present (uPlot.paths.bars, no new lib); built from resp.recoverable (no hard-coded analyzer list); honesty caption + "saved" absent from the [Lens viz] Cost-by-component + recoverable-waste overlay (differentiator) #211 card; recoverable routes through fmtFramedSavings, cost total through fmtFramedDollar.
  • test_ui_offline.py green; extracted app module passes node --check.
  • Full pytest tests/unit tests/integration → 823 passed; ruff check tokenjam/ + mypy tokenjam/ clean (116 files).

Notes

Closes #211

🤖 Generated with Claude Code

The differentiated Lens viz (#211, milestone #3): split the window's spend into
the four token components and overlay each optimize analyzer's attributed
recoverable waste — the cost-WASTE view a generic activity dashboard can't show
because it depends on our analyzers' attributed estimates.

New GET /api/v1/cost/components computes, server-side:
- the component cost split (input / output / cache-read / cache-write), priced
  per (provider, model) via the pricing table so it's exact, not apportioned
  from the aggregate cost_usd; and
- a registry-driven per-analyzer recoverable list — it iterates the typed
  downgrade slot plus every wave-2 finding carrying the #111 recoverable
  contract field, so a new analyzer appears automatically with no code change.
  Each entry keeps the analyzer's own caveat + estimate_basis verbatim and the
  component its savings act on ("call" = whole-call model swap / call
  elimination, for analyzers that can't be honestly pinned to one component).

The UI adds a ComponentWasteChart (vendored uPlot stacked bars, offline intact):
two columns — measured "Cost" stacked by component and "Recoverable (est.)"
stacked by analyzer. They are SEPARATE bars; recoverable is never subtracted
from cost and never called "saved" (Critical Rule 14) — the caption reads
"a heuristic estimate to review before acting, not a realized cost reduction".
A per-analyzer list under the chart surfaces each estimate behind the
"estimated recoverable" tag with its caveat. Every figure routes through the
framing block (fmtFramedSavings / fmtFramedDollar) so subscription/local users
see token-share, not raw dollars, and the bars themselves switch to token
volume. The endpoint fetch is best-effort — a failure never blanks Optimize.

Tests: endpoint tests (httpx + InMemoryBackend) pin the per-component pricing,
the registry-driven recoverable (reuse appears from the findings dict), the
downsize caveat verbatim, "saved" absent from the payload, and subscription
framing; static-grep UI regressions assert the chart/helpers, the registry-
driven build (no hard-coded analyzer list), the honesty caption + "saved"
absent from the surface, and the framing routing; test_ui_offline stays green
and the app module passes node --check.

Closes #211

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@anilmurty anilmurty left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: ✅ APPROVE — ready to merge

The differentiator, and the honesty discipline that got it carved out of Wave 1 is handled exactly right and pinned by tests. CI all 4 green (823), MERGEABLE, scope clean (cost.py + index.html + 2 test files), no committed binaries (screenshot delivered out-of-band — good).

Honesty discipline (Rule 14) — exemplary

  • "saved" never appears — asserted absent in BOTH the payload ("saved" not in json.dumps(d)) and the UI card.
  • Recoverable is never subtracted from cost — rendered as a separate column ("Cost by component" vs "Estimated recoverable"), so it can't read as a realized reduction.
  • Caption is exactly the right frame: "estimated recoverable waste … a heuristic estimate to review before acting, not a realized cost reduction" (test-pinned).
  • Routes through framing — fmtFramedSavings / fmtFramedDollar, subscription/local → token-share, bars switch to tokens.
  • Each analyzer's caveat/estimate_basis preserved verbatim (on the per-analyzer detail).

Design

  • Registry-driven via the #111 contract — iterates estimated_recoverable_usd/tokens on every finding, so a new analyzer appears automatically (matches the recoverable-savings contract).
  • Component cost priced exact per (provider, model), not apportioned from aggregate cost_usd — and an honest "call" (whole-call) attribution default where a component can't be pinned (cache/trim → input). That's the right call: don't fake per-component precision.

Tests

test_cost_charts.py pins per-component pricing, registry-drivenness, the downsize caveat verbatim, "saved" absent, subscription framing; UI static-grep pins the chart/helpers + honesty caption + framing routing. test_ui_offline green, node --check clean.

Ready to merge. Completes the Lens-viz Wave-1 (#211/#212/#213) and clears the last beachhead gate.

@anilmurty
anilmurty merged commit 1571bbd into main Jun 23, 2026
4 checks passed
@anilmurty
anilmurty deleted the feat/211-recoverable-waste-overlay branch July 18, 2026 16:18
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.

[Lens viz] Cost-by-component + recoverable-waste overlay (differentiator)

1 participant