Repository navigation
fix(dash): lean /api/stats fills replay_tokens and saved_gross - #992
Merged
Merged
Conversation
The lean read with a session or a time window never set SavedGross or ReplayTokens, so it always answered 0. The status line, the mod band and the status screens then showed only `wr`, never `rd`. overviewLeanWindow now computes both as the full Overview() does, from the columns it already scans. Closes #990 Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
amiddavid
commented
Nov 9, 2026
amiddavid
left a comment
Collaborator
Author
There was a problem hiding this comment.
| 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.319 | offload-review skill, @amiddavid's session | fresh | 1 of 2 | 1 of 2 | 0cbd4d78459a |
approve |
Verdict: approve
Findings
None. The fix is correct and the test proves it.
Non-blocking
- dash/overview_lean.go:9-23 and dash/api.go:1361-1367 — the field lists are now stale. Both doc comments enumerate what a lean read answers, and neither names
replay_tokensorsaved_gross. This PR makes both fields part of the lean answer. A later reader auditing lean's contract from those comments will miss them. - dash/overview_lean.go:83-86 — the arithmetic is a copy of dash/overview.go:715-722. Two copies of the same three lines can drift. A small shared helper (for example
o.finishTokenSavings()) would keep them one code path. - dash/overview_lean_test.go:434 — the two filters select the same three rows.
{Session: "s-replay"}and{Since: DayMs}both match all events (ts = DayMs+1..3). A window that excludes a turn (for exampleSince: DayMs+2) would also pin the window scoping of the new fields.
What I checked
- Read the full diff (2 files, +45 lines) and the code around each change:
overviewLeanWindow,leanWindowMainQuery,Overview,Filter.where,withKeepAlive,insertBatch, and the lean branch ofdash/api.gostats. - Parity of the row population. The full read sums
tokens_before,tokens_after,saved_uniqueunderf.where(), which excludes ping rows (r.keepalive = 0, query.go:107-109). The lean read selects rows withwithKeepAlive(f).where()but wraps each of the three sums inCASE WHEN r.keepalive = 0(overview_lean.go:108, 111-113). Both paths sum the same agent-only rows, soSavedGross = TokensBefore - TokensAfterand themax(0, SavedGross - SavedUnique)clamp give the same numbers as the full path (overview.go:715-722). The new code is the same arithmetic on the same columns. - The regression test.
mkEventstoresSavedUnique = before - afterstraight intorequests.saved_unique(store.go:665, verified ininsertBatch's INSERT). The fixture gives gross 1200 (3 turns x 400), unique 400, replay 800 — the asserted constants match. Without lines 83-86 the lean read returns 0/400/0 and the test fails, so it covers the fix. Both filter shapes (session, time window) reachoverviewLeanWindow. - Downstream effects of the new values.
ApplyReplayLedgerandApplySummarizeLedger(applied to the lean read in api.go:1402-1414) touch only dollar fields, notReplayTokensorSavedGross.waterfall()does not read them.OverviewLeanhas one caller (api.go:1393). No consumer recomputes anything from these fields inside this repo. - Process rules. The PR names the behavior change at the top (who, old, new, how to turn it off), and states data dependence (metadata only, no change) — matching issue #990, which the PR closes. No data-dependence table row is needed: no feature's data dependence changed. No fail-open path is touched: this is a read-side computation from already-scanned columns.
- Did not run tests (live tests not approved). Did not read the other review or any PR comment; the PR description and issue #990 only.
amiddavid
commented
Nov 9, 2026
amiddavid
left a comment
Collaborator
Author
There was a problem hiding this comment.
| 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.319 | offload-review skill, @amiddavid's session | fresh | 2 of 2 | 1 of 2 | 0cbd4d78459a |
approve |
Verdict: approve
Findings
None. The fix is correct and complete for what it claims.
Non-blocking
- dash/overview_lean.go:81-86 — the replay arithmetic is now written twice. The same four lines exist in
dash/overview.go:715-722. The comment says "the same arithmetic", but nothing enforces that. If the full read's formula changes, the lean copy can drift silently. A shared helper (for examplefunc (o *Overview) fillReplayTokens()) would pin one definition. Not a defect today: I compared both line by line and they match. - dash/overview_lean.go:13-18 — the doc comment's field list is now stale. It enumerates what a lean read must answer (
total_saved_usd,keepalive_net_usd, ..., the 7 waterfall keys). It does not namesaved_unique(already scanned before this PR) nor the newsaved_gross/replay_tokens. The next reader auditing lean's field set will find the list incomplete. One line would fix it.
What I checked
- Arithmetic parity with the full read.
overview_lean.go:83-86setsSavedGross = TokensBefore - TokensAfterandReplayTokens = max(0, SavedGross - SavedUnique).dash/overview.go:715-722computes the same, with the same clamp. Identical. - Population parity. The full read's main aggregate sums
r.tokens_before,r.tokens_after,r.saved_uniqueunderf.where(), which excludes pings by default (query.go:107-109). The lean query scans awithKeepAlivescope but zeroes ping rows throughCASE WHEN r.keepalive = 0for every token column (overview_lean.go:110-113). Both totals cover the same agent-only rows. No HTTP caller can setWithKeepAlive(no route exposes it), so the two reads cannot disagree through that flag. - No side effects on other lean outputs.
waterfall()(overview.go:1311) uses only the USD fields, notSavedGross/SavedUnique/ReplayTokens.ApplyReplayLedger(replaycap.go:618) andApplySummarizeLedger(summarizeattr.go:151) adjust only USD fields. Filling the two token fields changes exactly those two JSON fields and nothing else on the lean path. - The consumer claim.
context-guru-plugin/scripts/statusline.py:1497readssaved_uniqueandreplay_tokensfrom the window dict, and both stats reads go out aslean=1(statusline.py:495,:1587). Before this PR the lean path leftreplay_tokensat 0, sordnever rendered. The bug (#990) and the fix both check out. - The new test. Fixture: 3 turns, each 1000→600 tokens,
SavedUniquezeroed on turns 2 and 3 (both event and component rows —store.go:652,698persists both). Expected gross 1200, unique 400, replay 800. The math holds. The test compares lean against the fullOverview()for a session filter and asincewindow, and pins the full read to the constants too, so a regression on either side fails it. - Repo rules. The PR names the behavior change at the top (who, old, new, how to turn it off) per CLAUDE.md. Data dependence is metadata only (token counts from existing columns) and unchanged; no
docs/data-dependence.mdupdate is needed. No new query, so the lean path's cost bound is untouched. The oneOverviewLeancaller (dash/api.go:1393) is unchanged.
Assisted-By: Claude Opus 5.5 <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
Collaborator
Author
|
Comment-only fix after both approvals: the lean field lists now name Not done here, as follow-ups: a shared helper for the replay arithmetic (both reviewers), and a test window that excludes a turn (reviewer 1). Both change code, so they would need a new round. |
This was referenced Nov 10, 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.
Behavior change (a fix of wrong output): screens that show saved tokens as
rd / wrstart to show therdhalf./api/stats?lean=1with a session or a time window: the status line, the mod band, and the subscription screens in feat(dash): on a Claude subscription show saved tokens, with the 5-hour share as an estimate #988 and feat(plugin): status and insights savings show saved tokens on a subscription #989.replay_tokensandsaved_grosswere always 0, sordnever showed:saved 593 wr.saved 1.2M rd / 593 wr.Summary
The lean stats read never computed
saved_grossorreplay_tokens. It now computes them as the full read does, from the columns it already scans. No new query.Closes #990
What changed
dash/overview_lean.go:overviewLeanWindowsetsSavedGross = TokensBefore - TokensAfterandReplayTokens = max(0, SavedGross - SavedUnique). This is the same asdash/overview.go:715-721.TestOverviewLeanCountsReplayTokensAsTheFullReadDoes: 3 turns of one session, 400 unique tokens removed, 800 replay. It compares lean and full for a session filter and for a time window.How it was tested
0/400/0, full1200/400/800(gross/unique/replay). It passes with the fix.go test -race ./dash/...on the eval box: ok (two runs,^Test[A-H]279 s and^Test[^A-H]348 s). One run of the whole package hit Go's default 10-minute timeout on the loaded box, with no failed test.Data dependence
Metadata only: token counts. No change.
🤖 Generated with Claude Code