Skip to content

feat(plugin): show the band's subscription saving from the tokens fields - #1018

Merged
amiddavid merged 2 commits into
mainfrom
feat/band-tokens
Nov 10, 2026
Merged

amiddavid merged 2 commits into
mainfrom
feat/band-tokens

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

On a Claude subscription, the band now shows the session's saving from the tokens fields of /api/stats (#999). It shows one figure per tier and never adds tiers into one number.

  • Before: 5h 34% · this session saved 1.2M rd / 85.0k wr (compaction only).
  • After: 5h 34% · this session saved 85.0k wr · cost 40.0k rd (every feature).

Part 2 of #998, for the band. Refs #998.

What users see changes. Who: Claude Code users on a subscription, in the band's second row. Old: compaction's rd / wr estimate. New: the accountant's tokens for every feature, saved and cost, by tier. To go back: revert this PR. A proxy without tokens keeps the old figure.

What changed

  • cg.py: new tokens_text. status_extras uses it for window_saved.
    • Tiers show in the order wr (5-minute write), wr1h (1-hour write), rd (read), in (fresh input).
    • A tier at 0 is left out. cost … follows only when a tier was paid.
    • Nothing in any tier: no figure, the row shows 5h 34% only.
    • The read is the same: lean=1, this session, since the window's start, the same cache file and TTL.
    • The band does not show tokens_unmeasured_requests.
    • Fallback: an answer with no tokens (a proxy before feat(dash): store the accountant's tier tokens and serve them from /api/stats #999) keeps the old saved 1.2M rd / 85.0k wr.
  • The Codex copy of cg.py is identical (TestCGScriptsAreVendoredIntoCodex).
  • statusline.py does not change. The status line keeps its rd / wr.
  • The "next message" price (next message 90.0k rd · 90.0k wr if cold) does not change.
  • docs/plugin/statusline.md: the band example, and a new section "The band's tokens on a subscription".

Data dependence

Metadata only: token counts by tier. The same at each storage level (off, metadata only, everything). No change to docs/data-dependence.md.

How it was tested

  • Go, on the eval box: go test -race ./context-guru-plugin/... passes (378 s). go vet is clean.
  • Plugin: claude plugin test context-guru-plugin: 98 pass, 1 fail. The failure is no data: the band is left to the engine ("no implementation for clock.now"). It fails the same way on origin/main without this change.

Open questions

🤖 Generated with Claude Code

On a subscription the band now reads `tokens.saved` and `tokens.caused`
from the lean `/api/stats` read (#999), for this session in the 5-hour
window. It shows one figure per tier, never a sum:
`this session saved 85.0k wr · cost 40.0k rd`.

- Every feature counts, not only compaction.
- A tier at 0 is left out. `cost` shows only when a tier was paid.
- A proxy without `tokens` keeps the old `rd / wr` figure.
- The Codex copy of cg.py stays identical.

Refs #998

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 e4a663922879 approve

Verdict: approve

Findings

None.

Non-blocking

  • context-guru-plugin/cg_cachestate_test.go:100 — the helper puts window_asked in the doc, but no test asserts it equals cachestateWindowStart. It only prints inside a failure message. The since and session params are still guarded indirectly: a wrong param changes the stub's answer, so window_saved fails. A direct assert would fail faster and name the cause.
  • context-guru-plugin/scripts/cg.py:254 — the comment says the tier order is "the order every screen shows them". docs/dashboard.md (Savings in tokens) lists the tiers as input, cache_read, cache_write_5m, cache_write_1h. The band's order (wr, wr1h, rd, in) differs from that list. Say "the order the band shows them" instead.
  • docs/plugin/statusline.md (new section) — cost does not include the keep-alive pings' own billed tokens. On a subscription those reads also draw from the 5-hour limit. docs/dashboard.md says this one link away ("a ping is not agent traffic"). One sentence here would save the reader the hop.
  • A proxy at #999 or later, whose rows for this session are all unmeasured (rows from before the accountant, or rows it could not price), now shows no figure where it showed the compaction estimate. The PR and the docs disclose this. Deliberate, not a defect.

What I checked

  • The full diff: both cg.py copies, cg_cachestate_test.go, register.test.tsx, docs/plugin/statusline.md. Both cg.py copies are byte-identical (diff), and TestCGScriptsAreVendoredIntoCodex covers cg.py.
  • The read the band makes is real: /api/stats?lean=1&since=<window>&session=<sid> goes to the lean branch (dash/api.go:1362), and overviewLeanWindow puts the tokens totals and the unmeasured count on the answer (dash/overview_lean.go:92, dash/acctokens.go:238). The JSON tier names (input, cache_read, cache_write_5m, cache_write_1h under saved / caused) match TOKEN_TIERS exactly.
  • tokens_text logic: I ran it locally (Python) against all six cases the Go tests assert, with _human copied from statusline.py:337. All six outputs match, including tier order, the v >= 1 filter, the bool guard, and the None returns.
  • The fallback: an answer with no tokens (proxy before #999) keeps sl._saved_tokens_text; mine of None (proxy down, nothing cached) also yields no field. Both paths end in if saved:, so the field is absent, never wrong.
  • The statusline helpers the code touches exist and keep their names: _human, _saved_tokens_text, _bg_json, WINDOW_TTL_SECONDS. The Go test is the guard the repo uses for these renames, and it covers the new path.
  • The UI side: model.ts passes window_saved through unchanged; register.tsx:204 prefixes this session; statusRow's width ladder drops limit.saved when the longer string does not fit, so the row degrades to 5h 34%, never overflows. register.test.tsx asserts the new row.
  • The test refactor: each case gets a fresh XDG_CACHE_HOME (t.TempDir() per call) and a fresh port, so no cache or stub leaks between cases. The stub's three branches guard the query params indirectly.
  • Docs: the ../dashboard.md#savings-in-tokens anchor exists; the section's rules match the code (tier at 0 left out, cost only when paid, nothing in any tier means 5h 34% only, pre-#999 fallback).
  • Process rules: the PR names the user-visible change at the top, with the old and new behavior and the revert path. Data dependence is metadata only (token counts by tier), stated in the PR; no docs/data-dependence.md row is needed. No Go component, proxy path, or fail-open path changes; statusline.py is untouched, as the PR says.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 e4a663922879 approve

Verdict: approve

Findings

None.

Non-blocking

  • context-guru-plugin/hooks/register.tsx:6 — the file's header comment still shows the old figure. Band row 2 is described as `5h 34% · saved 1.2M rd / 85.0k wr`. After this PR the band draws saved 85.0k wr · cost 40.0k rd. The PR updated docs/plugin/statusline.md but not this comment. Fix: one line, next touch.
  • Upgrade edge, documented, so intended: the figure can drop right after a proxy upgrade. A post-#999 proxy always answers tokens (Go struct, never omitted), so has_tokens is true even when every row in the 5-hour window predates the measure (acc_priced = 0, token columns 0). The band then shows nothing, or less than the compaction-only figure it showed before the upgrade, until new rows accumulate. docs/plugin/statusline.md states this rule ("count as nothing here"). No code change asked; noting it so the merge decision owns it knowingly.
  • context-guru-plugin/cg_cachestate_test.go:100 — window_asked is recorded but never asserted. The helper puts the since the window read asked for into doc["window_asked"], and the first test names it only inside its failure message. The since value is still guarded indirectly (a wrong or missing since routes the stub to another branch and fails the string assertion). A direct doc["window_asked"] != cachestateWindowStart check would make the intent explicit.

What I checked

  • Read CLAUDE.md, docs/design.md, and the full diff of the one commit (e4a66392) against base 7fd26dcd.
  • The two cg.py copies are byte-identical (git show + diff): context-guru-plugin/scripts/cg.py and codex-marketplace/plugins/context-guru/scripts/cg.py. cg_test.go:601 keeps them identical.
  • The wire contract is real. The plugin reads /api/stats?lean=1&since=<ms>&session=<sid>. dash/overview_lean.go answers that shape from one indexed scan and applies leanAgg.read() → Overview.Tokens (json:"tokens", dash/overview.go:440), with saved/caused totals by the four tiers and tokens_unmeasured_requests, bounded by both the session and since filters. Tier key names (input, cache_read, cache_write_5m, cache_write_1h) match TierTokens in dash/acctokens.go:22.
  • The formatting matches the tests. Ran tokens_text standalone against every case in TestCGCachestateWindowTokensByCase plus malformed inputs (None, missing tokens, saved: {}, caused: null): outputs equal the wanted strings, and malformed input yields None, never a raise. sl._human gives 85.0k, 2.0k, 1.2M, 5, 600 as the tests expect.
  • The fallback is correct. sl._saved_tokens_text on {"saved_unique":85000,"replay_tokens":1200000} gives saved 1.2M rd / 85.0k wr, the pre-#999 figure the test wants. has_tokens is false exactly when the proxy served no tokens object; a post-#999 proxy always serves one.
  • The band renders the string verbatim (hooks/model.ts:303 → hooks/register.tsx:204), no parsing, so the format change cannot break the width logic; windowSaved is in the redraw signature. With nothing in any tier the row degrades to 5h 34% only, as the docs claim.
  • The test harness is sound. runCGCode sets CLAUDE_CODE_SESSION_ID=session-A, so the window read carries the session; the stub's three branches make both the session scoping and the since param fail the test if cg.py drops either. _bg_json does one inline GET on a cache miss, so the first invocation sees the stub's answer (fresh XDG_CACHE_HOME per case, no cross-case cache carryover).
  • Docs. The new docs/plugin/statusline.md section matches the code: tier order and labels, zero-tier omission, cost only when paid, pre-#999 fallback, unmeasured rows counting as nothing. The ../dashboard.md#savings-in-tokens anchor exists (docs/dashboard.md:411). The status line's own compaction-only figure (statusline.md line 25) is unchanged on purpose and the new section says so.
  • Repo rules. Display-only change, no request path, no storage: no data-dependence change (the #998 row in docs/data-dependence.md already covers the token figures), no fail-open surface touched, /stats field names untouched.
  • Live tests were not run (not approved). Python was parsed and the new function exercised locally with plain python3; no Go build or test was run.

- The test now checks that the window read asks for the window's start.
- The tier-order comment in cg.py says "the band", not "every screen".
- The register.tsx header shows the new band figure.
- docs/plugin/statusline.md says that `cost` leaves out the keep-alive pings.

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Round-1 notes, fixed in this push:

  • Both reviewers, cg_cachestate_test.go: the test now asserts that window_asked equals the window's start.
  • Reviewer 1, cg.py: the tier-order comment now says "the order the band shows them".
  • Reviewer 1, docs/plugin/statusline.md: a new line says that cost leaves out the keep-alive pings' own tokens.
  • Reviewer 2, register.tsx header: it now shows 5h 34% · this session saved 85.0k wr · cost 40.0k rd.
  • Both reviewers, the drop after an upgrade (unmeasured rows count as nothing): kept as designed and documented. The owner decides at merge.

Box: all 8 TestCGCachestate* and TestCGScriptsAreVendoredIntoCodex pass with -race.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 85ea890810c4 approve

Verdict: approve

Findings

None.

Round-1 notes, re-checked

  • window_asked never asserted — fixed. cg_cachestate_test.go:35 now asserts doc["window_asked"] == fmt.Sprint(cachestateWindowStart). I traced the values: the helper passes --window-start-ms 1799996000000, cg.py parses it with type=int (cg.py:1661) and puts it in the query's since, and the stub records that string back. The assert also fails when the window read never happens (windowAsked stays ""), so it guards the read itself, not only its value. fmt is already imported.
  • Tier-order comment — fixed. Both cg.py copies now say "the order the band shows them". The two copies are still byte-identical (diff), so TestCGScriptsAreVendoredIntoCodex holds.
  • Pings missing from the docs — fixed. docs/plugin/statusline.md adds one line: cost leaves out the keep-alive pings' own billed tokens. The wording matches the code (dash/acctokens.go: a ping is not agent traffic; its tokens live in keepalive_ping_tokens, not tokens.caused).
  • Unmeasured rows show no figure — kept as designed. The author kept it and the owner decides at merge. I called it deliberate in round 1 and the PR plus docs disclose it. I accept.

Reviewer 2's round-1 notes (register.tsx header, upgrade edge, window_asked) — I checked each myself in round 1 or in this commit's diff. The header comment now shows the real row (this session saved 85.0k wr · cost 40.0k rd), which matches what register.test.tsx asserts. No disagreement.

Non-blocking

None new.

What I checked

  • Last review: round 1, e4a663922879b6a03deb50c0db00dc1e0a37146b, 2026-11-10T08:42:07Z.
  • New commits read: 1 — 85ea8908 "fix(mod): address the #1018 round-1 notes".
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three .review-in-*.txt files; the inline file is empty).
  • New items since my last review: my round-1 review and reviewer 2's round-1 review (both at the old sha, both approve, read in full); the author's fix comment at 08:44:12 (lists the fixes, checked each against the diff); the fix commit itself (reviewed below).
  • The full diff e4a6639..85ea8908: 5 files, 7 insertions, 3 deletions. Every hunk read. The changes are the three fixes above plus the register.tsx header comment. No logic changes: tokens_text, status_extras, the lean read path, and statusline.py are untouched.
  • The two cg.py copies are byte-identical after the change (diff).
  • The commit carries the DCO sign-off and Assisted-By: attribution, per the repo rules.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 85ea890810c4 approve

Verdict: approve

Findings

None.

Earlier findings, round 1

Mine (all non-blocking):

  1. register.tsx:6 header showed the old figure — fixed. The header now reads `5h 34% · this session saved 85.0k wr · cost 40.0k rd`, which matches what statusRow draws for a subscription (register.tsx:204 prefixes this session ).
  2. Upgrade edge: unmeasured rows count as nothing, so the figure can drop right after a proxy upgrade — kept as designed, per the author's comment. The rule stays documented in docs/plugin/statusline.md ("count as nothing here"). I agree: the alternative would mix the accountant's measured tokens with the old compaction estimate in one string.
  3. window_asked recorded but never asserted — fixed. cg_cachestate_test.go:35 now asserts doc["window_asked"] == fmt.Sprint(cachestateWindowStart). Checked the types: the stub records q.Get("since") (a string), fmt.Sprint of the constant gives the same decimal string, and fmt is already imported. The assertion fails with a clear message when the window read drops or changes since.

Reviewer 1's notes (I checked each myself, not just the author's reply):

  • window_asked assertion — same fix as mine, item 3 above. Verified.
  • cg.py:254 "every screen" vs "the band" — fixed in both copies; docs/dashboard.md indeed lists the tiers in a different order (input first), so "the band" is the correct scope. Verified.
  • cost leaves out the keep-alive pings — fixed. docs/plugin/statusline.md:213 now says so. Checked the claim against the code: the lean read returns no ping tokens at all (leanAgg.read(), dash/acctokens.go:286), and the full read keeps them beside tokens (keepalive_ping_tokens), never inside tokens.caused. The added clause "on a subscription they also count toward the 5-hour limit" is right: a ping is a real provider request under the account.
  • Unmeasured-rows drop — same as my item 2. Kept as designed.

Non-blocking

None.

What I checked

  • Last review: round 1, e4a663922879b6a03deb50c0db00dc1e0a37146b, 2026-11-10T08:42:36Z.
  • New commits read: 1 — 85ea890 "fix(mod): address the #1018 round-1 notes".
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from .review-in-reviews.txt, .review-in-comments.txt, .review-in-inline.txt — the inline file is empty).
  • New items since my last review:
    • Commit 85ea890: read the full diff (git diff e4a66392..HEAD) and the changed files around each hunk. Five files, 7 insertions, 3 deletions. Every hunk is a round-1 fix; no new behavior, no new surface.
    • Author comment (2026-11-10T08:44:12Z): lists the fixes and the one kept-as-designed item, and reports the Go tests pass with -race. I verified each code claim myself above. The -race result I cannot re-run (live tests not approved); the changed lines are a string comparison and comments, nothing that new concurrency could break.
  • The two cg.py copies are still byte-identical after the fix commit (git show + diff); the comment change landed in both.
  • The new assertion cannot pass vacuously: if the window branch of the stub is never hit, windowAsked stays "" and the assertion fails; if since is mangled, the string compare fails.
  • The docs line added to statusline.md matches the served data (checked against dash/acctokens.go and docs/dashboard.md "Savings in tokens").

@amiddavid
amiddavid merged commit d058b33 into main Nov 10, 2026
12 of 13 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 10, 2026
amiddavid added a commit that referenced this pull request Nov 10, 2026
main was red since d058b33 (#1018):
TestCGCachestateCarriesTheStatusLinesExtras and
TestCGCachestateWindowTokensByCase failed with window_saved empty.

Cause: cg.py defined tokens_text twice. #1018 added
tokens_text(stats, human) at line 259; #989 (merged just before it)
added tokens_text(st) at line 572, which shadowed it. status_extras
then called the wrong one with 2 arguments; it swallows the
TypeError, so window_saved stayed empty. Each PR was green alone:
#1018's branch never had #989, so the collision appeared only after
both merged (a semantic merge conflict).

Fix: rename #1018's helper to band_tokens_text, with its one call and
one doc mention, in both copies of cg.py (the Codex copy too). No
test changes.

Behavior change: none intended; it restores the band's window saving
that #1018 added (owner-approved).
Data dependence: none.

Reviews: 2 offload reviews (GLM), round 1, both approve at 753bcc4.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
amiddavid added a commit that referenced this pull request Nov 11, 2026
…0.2.12, Pi 0.1.1)

Claude Code plugin 0.6.7 -> 0.7.2:
- #981, #989, #1018, #1035: subscription band and savings show saved tokens.
- #1038: insights drop the finding that credits a user's own removal.
- #1039, #1040: dash and keep-alive fixes that touch the plugin files.

Codex plugin 0.2.11 -> 0.2.12:
- #1051: read the cache strategies and presets from the proxy.
- The vendored cg.py gets the same plugin changes.

Pi package 0.1.0 -> 0.1.1:
- #1010: chain the proxy in front of an existing base URL.
- #1011: check the proxy at session start and restart it once.
- #1049, #1058: write the cache strategy by name; review fixes.

OpenCode plugin (#483): new in v0.7.2, no version file, not changed.
Bob plugin 0.1.0: no change since v0.7.1, not changed.

Signed-off-by: DAVID AMID <[email protected]>
Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
amiddavid added a commit that referenced this pull request Nov 11, 2026
Bump the plugin versions for v0.7.2.

- Claude Code plugin 0.6.7 -> 0.7.2: #981, #989, #1018, #1035 (savings
  in tokens on a subscription), #1038, #1039, #1040, #1055 (cache
  strategies read from the proxy).
- Codex plugin 0.2.11 -> 0.2.12: #1051 (cache strategies and presets
  read from the proxy), plus the vendored cg.py changes.
- Pi package 0.1.0 -> 0.1.1: #1010, #1011, #1049, #1058, and the #1040
  README line.
- OpenCode plugin: new in v0.7.2 (#483), no version file. Its first
  start now downloads proxy v0.7.2 (was v0.7.1). Nothing compares
  against PINNED_VERSION; the floor stays "v0.7.1 or later". The README
  lines, the install guide and 3 tests follow.
- Bob plugin: 0.1.0, no change since v0.7.1.

Behavior change: the OpenCode plugin's first download is proxy v0.7.2,
not v0.7.1. Owner's yes, 2026-11-11, in the integrator session: "option1
it is"; the version bumps: "I approve the version bumps for the
plugins".

Data dependence: none.

Reviews: 3 offload review rounds (GLM; round 3 pre-approved by the
owner). Both reviewers approve at 3cf6176, which is the PR head.

Signed-off-by: DAVID AMID <[email protected]>
Assisted-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant