Skip to content

feat(dash): count tool deferral as its own lever on the Savings page (#888) - #891

Merged
amiddavid merged 6 commits into
mainfrom
feat/savings-defer-lever
Nov 4, 2026
Merged

amiddavid merged 6 commits into
mainfrom
feat/savings-defer-lever

Conversation

@amiddavid

@amiddavid amiddavid commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Behavior change (read first)

  • Who: accounts on the ultrahigh preset (tool deferral on). Other accounts have no deferral rows, so nothing changes for them.
  • Old: the Savings headline total, the waterfall and the daily chart left out tool deferral. The Tools page showed it alone.
  • New: deferral's net saving joins the per-thread money view (threadMoney, owner's yes 2026-11-04) and joins total_saved_usd, /api/stats, the lean stats read, SavingsTotals, the Savings page (new "Tool deferral" card) and the daily chart (own series). The /chart plugin lever list gets a "Tool deferral" row.
  • Turn off: stop deferring (leave ultrahigh). The figure is then 0 and drops out of every view. There is no separate switch.
  • The owner (a human developer) asked for this on 2026-11-04.

Summary

What changed

  • One SQL expression deferNetSum (SUM(r.defer_net_usd)). The Tools panel, Overview, OverviewLean, SavingsTotals and the daily Series all use it. There is no second copy of the sum.
  • One formula savingsArithmetic gets a deferNet addend. Overview, lean and SavingsTotals all use that formula.
  • Waterfall: new step defer_net before total_saved.
  • Series: Bucket.defer_net_usd, left out of the JSON when 0. An account with no deferral has no deferral series.
  • UI: savedParts gets defer. The Savings page shows a "Tool deferral" card only when the figure is not 0. The daily chart draws a second bar and a key only on days that deferred.
  • Plugin: cg.levers gets a "Tool deferral" row, so the /chart lever list still adds up to the headline.
  • Per-thread money: cacheThreadsQuery sums the same defer_net_usd column. threadMoney adds it to saved and baseline.
  • No double count: deferral runs after the declaration filter. Its definitions are in the top-level tools array, outside tokens_before. filtered_decl_tokens (the filter credit in declcredit.go) counts only what the filter removed. The two columns are disjoint. The self-removal credit stays outside the total.

Data dependence

None new. The change reads defer_net_usd, a number already stored on each request row (metadata only). At each level: off or metadata only, the figure is available. Everything: no change. No row in docs/data-dependence.md changes.

How it was tested

Run on the shared box (box 2):

  • go vet ./dash/ ./proxy/ and go build ./...: clean.
  • CGO_ENABLED=1 go test -race -timeout 30m ./dash/...: ok (601 s, at head 4a1bf19).
  • New test TestThreadMoneyIncludesDeferNet: a thread with deferral shows exactly that much more in saved and baseline. Mutation (pass 0): test fails. Restored.
  • go test ./dash -run 'TestDeferNet|TestGoldenEndpoints|TestToolDefer': pass.
  • New test TestDeferNetIsItsOwnLeverInTheTotal: a row with defer_net_usd 0.004 raises the total by exactly 0.004 in Overview, OverviewLean and SavingsTotals. It appears as the defer_net waterfall step and as a daily series. An account with no deferral has no series.
  • Golden files stats*.json updated (one new field and one new waterfall step, all 0 in the fixture).
  • node --test dash/savings.test.mjs: pass (new test: parts add up to the server total).
  • pytest in context-guru-plugin: 87 passed.
  • Mutation: removed deferNet from the total line in savingsArithmetic. TestDeferNetIsItsOwnLeverInTheTotal failed for all three readers. Restored.
  • An earlier race run failed TestSettingsFormSpeaksTheSameFieldNamesAsTheServer: my card id 'defer' matched a config field name. Renamed to tooldefer. Fixed in the second commit.

Technical notes

  • The ./dash/... race run is near Go's default 10 minute limit on a loaded box. I ran it with -timeout 30m.

🤖 Generated with Claude Code

The Savings total and the daily chart now include tool deferral's NET
saving (the sum of requests.defer_net_usd, the same figure as /stats
tool_defer and the Tools panel). It is one more addend of the total, a
waterfall step "defer_net", a Savings card, and a daily series.

One SQL expression (deferNetSum) feeds the Tools panel, the totals and
the series. No other lever holds these dollars: deferral runs after the
declaration filter and its definitions are outside tokens_before.

Closes #888

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

Assisted-By: Claude Opus 5.5
Signed-off-by: DAVID AMID <[email protected]>
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 2 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 34967a851dfe blocking

Verdict: blocking

Findings

  1. dash/cachestate.go:282-298 — Per-thread savings omit tool deferral
    • What fails: /api/cachestate reports per-thread savings that exclude defer_net_usd, while every other dashboard endpoint includes it. A session with tool deferral shows inconsistent totals across endpoints.
    • Proof: cacheThreadsQuery() at line 282 builds the SQL that sums cost components per thread. It includes baseline_cost_usd, cost_usd, cg_llm_cost_usd, cachesplit_saved_usd, keep-alive savings, and manual ping costs (lines 289-295), but defer_net_usd is absent. Line 202 calls threadMoney(base, cost, cgllm, split, kaSv, ping, manualSaved, manualCost) with the scanned values. Line 310 then calls savingsArithmetic(cost, baseline, cgllm, split, kaSv, ping, manualSaved-manualCost, 0) with a hardcoded 0 for deferNet. Compare to overview.go:1101, overview_lean.go:76, and savings.go:57, which all pass o.DeferNetUSD or s.DeferNetUSD as the 8th parameter. The test at tooldeferpanel_test.go:88-180 verifies that deferral raises the total in Overview, OverviewLean, and SavingsTotals, but does not test /api/cachestate.
    • Fix: Add COALESCE(SUM(CASE WHEN + agent + THEN r.defer_net_usd ELSE 0 END),0) to the SELECT in cacheThreadsQuery() (after line 295). Scan it into a new deferNet float64 variable in ThreadsFor() (line 192). Pass deferNet to threadMoney() instead of the hardcoded 0 at line 310. Update threadMoney()'s signature to accept deferNet float64 as its 9th parameter and pass it to savingsArithmetic().

Non-blocking

None.

What I checked

  • Read CLAUDE.md and docs/design.md for repo rules
  • Read the full diff (all changed files)
  • Verified savingsArithmetic() signature change adds deferNet as 8th parameter
  • Verified all callers of savingsArithmetic() were updated except threadMoney()
  • Verified defer_net_usd is a per-request column (schema.go:223, DEFAULT 0)
  • Verified the test coverage includes Overview, OverviewLean, SavingsTotals, and Series, but not the cache state endpoint
  • Verified the comment at cachestate.go:303 says "Only savings written per request are in it" and defer_net_usd qualifies
  • Checked that no other queries or calculations were missed

@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 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 34967a851dfe blocking

Verdict: blocking

Findings

  1. dash/cachestate.go:310 — Per-thread savings exclude tool deferral
    • What fails: A session's thread money totals do not include deferral savings. The Overview shows session total with deferral, but Cache State's per-thread breakdown omits it. The figures disagree.
    • Proof: Line 310 calls savingsArithmetic(..., 0) with hardcoded 0 for deferNet. Line 290-305 cacheThreadsQuery sums baseline_cost_usd, cost_usd, cg_llm_cost_usd, cachesplit_saved_usd, keep-alive fields, and manual pings, but does not sum defer_net_usd. Every other caller of savingsArithmetic in this PR passes the actual deferral value: savings.go:58, overview.go:1103, overview_lean.go:77.
    • Fix: Add COALESCE(SUM(CASE WHEN + agent + THEN r.defer_net_usd ELSE 0 END),0) to cacheThreadsQuery select list. Add deferNet float64 to the scan variables at line 195. Pass deferNet instead of 0 to savingsArithmetic at line 310.

Non-blocking

  • The test TestDeferNetIsItsOwnLeverInTheTotal is thorough and covers Overview, OverviewLean, SavingsTotals, the waterfall, and the daily series. It does not cover threadMoney or the Cache State page, which is where the finding above lives.
  • The UI changes (app.js, style.css) correctly show deferral only when parts.defer !== 0, so accounts that never deferred see no deferral card or series. Clean.
  • The Python plugin script cg.py correctly adds the lever to the waterfall breakdown.

What I checked

  • Read CLAUDE.md and docs/design.md for repo rules
  • Read the full diff (all 16 files changed)
  • Verified savingsArithmetic signature change and all call sites
  • Verified deferNetSum SQL constant is used consistently across queries
  • Verified test coverage in tooldeferpanel_test.go and savings.test.mjs
  • Verified UI changes show deferral conditionally and add it to totals
  • Verified golden test files updated with new waterfall step
  • Checked that the new field is omitempty in JSON where appropriate (Bucket)
  • Verified the waterfall description explains why deferral is its own line

@amiddavid

Copy link
Copy Markdown
Collaborator Author

Round 1: both reviewers raised the same finding. Fixed in 4a1bf19.

  • Finding: threadMoney and cacheThreadsQuery left out defer_net_usd (savingsArithmetic(..., 0)).
  • Fix: the per-thread query now sums the same column as deferNetSum (shared deferNetCol, no second copy). threadMoney adds it to saved and to baseline. The owner agreed on 2026-11-04.
  • Test: TestThreadMoneyIncludesDeferNet. A thread with defer_net_usd 0.004 shows exactly 0.004 more in saved and baseline than a thread without. The thread without deferral is unchanged.
  • Mutation: I passed 0 to savingsArithmetic in threadMoney. The test failed. Restored.
  • Race: CGO_ENABLED=1 go test -race -timeout 30m ./dash/... is ok (601 s) on box 2, fresh sync.
  • Golden files: none change. The fixture has no deferral rows.

@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 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 4a1bf19cd7c3 approve

Verdict: approve

Findings

None. The round 1 finding is fixed.

Round 1 finding status

Finding 1 (dash/cachestate.go:310) — FIXED

The fix in commit 4a1bf19 is correct and complete:

  1. cacheThreadsQuery now sums defer_net_usd at line 296: COALESCE(SUM(CASE WHEN + agent + THEN + deferNetCol + ELSE 0 END),0)
  2. ThreadsFor scans the new column into deferNet float64 at line 196
  3. threadMoney signature updated to accept deferNet as 9th parameter at line 311
  4. threadMoney passes deferNet to savingsArithmetic at line 312 (no longer hardcoded 0)
  5. threadMoney adds deferNet to the baseline at line 313: return saved, baseline + split + kaSv + manualSaved + deferNet
  6. Comment at line 307 updated to explain that deferral is included
  7. New test TestThreadMoneyIncludesDeferNet at line 186 verifies the fix: a thread with defer_net_usd 0.004 shows exactly 0.004 more in both SavedUSD and BaselineUSD

The author also introduced deferNetCol constant in tooldeferpanel.go:23 to avoid duplicating the column name between deferNetSum and cacheThreadsQuery. Clean.

Non-blocking

None.

What I checked

  • Last review: round 1, 34967a8, 2026-11-04T11:48:19Z
  • New commits read: 1 (4a1bf19 "feat(dash): count tool deferral in the per-thread money view")
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
  • New items since my last review:
    • Commit 4a1bf19: read full diff and surrounding code
    • Comment by amiddavid at 2026-11-04T11:49:47Z: confirms fix, test, and mutation testing
    • Reviewer 2's round 1 review: same finding as mine, no additional issues
  • Verified the fix addresses all parts of the finding
  • Verified the new test covers the fixed code path
  • Verified no new issues introduced by the fix
  • Checked that deferNetCol constant is used correctly in both places

@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 (bob, user of @amiddavid) IBM Bob Shell 1.0.3 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 4a1bf19cd7c3 approve

Verdict: approve

Findings

None. My round 1 finding is fixed.

Round 1 finding status

Finding 1 (dash/cachestate.go:282-298) — FIXED

The fix in commit 4a1bf19 addresses the finding completely:

  • Added deferNetCol constant to share the column name with deferNetSum
  • Added the deferral sum to cacheThreadsQuery SELECT (line 297)
  • Added deferNet float64 variable and scanned it (lines 193, 196)
  • Passed deferNet to threadMoney (line 203)
  • Updated threadMoney signature to accept deferNet as 9th parameter (line 313)
  • Passed deferNet to savingsArithmetic and added it to baseline (lines 314-315)
  • Updated the comment to explain deferral is included (lines 307-308)
  • Added test TestThreadMoneyIncludesDeferNet that verifies a thread with deferral shows exactly 0.004 more in saved and baseline than one without

The author's comment confirms mutation testing (passing 0 failed the test) and race testing passed.

Non-blocking

None.

What I checked

  • Last review: round 1, 34967a8, 2026-11-04T11:48:15Z
  • New commits read: 1 (4a1bf19 "feat(dash): count tool deferral in the per-thread money view")
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
  • New items since my last review:
    • Commit 4a1bf19: read full diff and surrounding code in all 3 changed files
    • Author's comment at 2026-11-04T11:49:47Z: confirms the fix, mutation test, and race test results
    • Reviewer 1's review at 2026-11-04T11:48:19Z: raised the same finding independently
  • Verified the fix matches my proposed solution exactly
  • Verified the new test covers the finding: creates two sessions (one with deferral, one without) and checks the difference is exactly 0.004 in both saved and baseline
  • Verified deferNetCol is used consistently (shared with deferNetSum)
  • Verified the comment update explains why deferral raises y (baseline)
  • Checked that no other code paths were affected by the signature change

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

Copy link
Copy Markdown
Collaborator Author

CI lint failed on gofmt for dash/tooldeferpanel.go. Fixed in d74d10c. The commit is formatting only: it adds spaces around the two + in the SQL string join.

-		SUM(r.defer_gross_usd), `+deferNetSum+`,
+		SUM(r.defer_gross_usd), ` + deferNetSum + `,

On box 2, gofmt -l . now lists nothing and go vet ./... is clean. No code changed.

@amiddavid

Copy link
Copy Markdown
Collaborator Author

New commits: a merge of origin/main (includes #889) and 6d56f79, which vendors cg.py into the Codex copy.

@amiddavid
amiddavid merged commit 4090759 into main Nov 4, 2026
5 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 4, 2026
amiddavid added a commit that referenced this pull request Nov 4, 2026
…0.2.11) (#921)

Claude Code plugin 0.6.6 -> 0.6.7:
- #889: the preset picker restarts the proxy after a yes, and the status shows a pending preset.
- #891: insights and the status show tool deferral as its own saving lever.
- #877, #885: save-more tools (keep, unkeep, defer, undefer, search on/off, revert).
- #882: ultrahigh is marked experimental in the picker.

Codex plugin 0.2.10 -> 0.2.11:
- #889, #891: the same cg.py changes (vendored copy).
- #882: ultrahigh is marked experimental in the picker.


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.

feat(dash): count tool deferral as its own lever on the Savings page

1 participant