Skip to content

fix(dash): lean /api/stats fills replay_tokens and saved_gross - #992

Merged
amiddavid merged 2 commits into
mainfrom
fix/lean-replay-tokens
Nov 10, 2026
Merged

amiddavid merged 2 commits into
mainfrom
fix/lean-replay-tokens

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Behavior change (a fix of wrong output): screens that show saved tokens as rd / wr start to show the rd half.

Summary

The lean stats read never computed saved_gross or replay_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: overviewLeanWindow sets SavedGross = TokensBefore - TokensAfter and ReplayTokens = max(0, SavedGross - SavedUnique). This is the same as dash/overview.go:715-721.
  • New test 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

  • The new test fails without the fix: lean 0/400/0, full 1200/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

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 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.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_tokens or saved_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 example Since: 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 of dash/api.go stats.
  • Parity of the row population. The full read sums tokens_before, tokens_after, saved_unique under f.where(), which excludes ping rows (r.keepalive = 0, query.go:107-109). The lean read selects rows with withKeepAlive(f).where() but wraps each of the three sums in CASE WHEN r.keepalive = 0 (overview_lean.go:108, 111-113). Both paths sum the same agent-only rows, so SavedGross = TokensBefore - TokensAfter and the max(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. mkEvent stores SavedUnique = before - after straight into requests.saved_unique (store.go:665, verified in insertBatch'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) reach overviewLeanWindow.
  • Downstream effects of the new values. ApplyReplayLedger and ApplySummarizeLedger (applied to the lean read in api.go:1402-1414) touch only dollar fields, not ReplayTokens or SavedGross. waterfall() does not read them. OverviewLean has 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 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.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 example func (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 name saved_unique (already scanned before this PR) nor the new saved_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-86 sets SavedGross = TokensBefore - TokensAfter and ReplayTokens = max(0, SavedGross - SavedUnique). dash/overview.go:715-722 computes the same, with the same clamp. Identical.
  • Population parity. The full read's main aggregate sums r.tokens_before, r.tokens_after, r.saved_unique under f.where(), which excludes pings by default (query.go:107-109). The lean query scans a withKeepAlive scope but zeroes ping rows through CASE WHEN r.keepalive = 0 for every token column (overview_lean.go:110-113). Both totals cover the same agent-only rows. No HTTP caller can set WithKeepAlive (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, not SavedGross / SavedUnique / ReplayTokens. ApplyReplayLedger (replaycap.go:618) and ApplySummarizeLedger (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:1497 reads saved_unique and replay_tokens from the window dict, and both stats reads go out as lean=1 (statusline.py:495, :1587). Before this PR the lean path left replay_tokens at 0, so rd never rendered. The bug (#990) and the fix both check out.
  • The new test. Fixture: 3 turns, each 1000→600 tokens, SavedUnique zeroed on turns 2 and 3 (both event and component rows — store.go:652,698 persists both). Expected gross 1200, unique 400, replay 800. The math holds. The test compares lean against the full Overview() for a session filter and a since window, 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.md update is needed. No new query, so the lean path's cost bound is untouched. The one OverviewLean caller (dash/api.go:1393) is unchanged.

@amiddavid

Copy link
Copy Markdown
Collaborator Author

Comment-only fix after both approvals: the lean field lists now name saved_unique, saved_gross and replay_tokens (both reviewers noted it). Output of scripts/release/approval-still-valid.sh:

approved: 0cbd4d78459a (base 8c12384ee614) patch-id 1b702fd02118
head:     c3662e6769a8 (base 8c12384ee614) patch-id 3594f7c5778b
--- git range-diff
1:  0cbd4d78 = 1:  0cbd4d78 fix(dash): lean /api/stats fills replay_tokens and saved_gross
-:  -------- > 2:  c3662e67 docs(dash): name the saved token counts in the lean field lists
COMMENTS ONLY: 0cbd4d78459a..c3662e6769a8 changes only comment lines in .go and .sh files. The approval of 0cbd4d78459a still counts. Say so on the PR.

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.

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.

dash: lean /api/stats never fills replay_tokens, so rd is always missing

1 participant