Skip to content

fix(gain): preserve signed net savings across reports - #4436

Open
yeahjack wants to merge 17 commits into
rtk-ai:developfrom
yeahjack:fix/gain-signed-net-savings
Open

yeahjack wants to merge 17 commits into
rtk-ai:developfrom
yeahjack:fix/gain-signed-net-savings

Conversation

@yeahjack

@yeahjack yeahjack commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #3702.

Tracker::record already stores negative savings on current develop, but gain's readers disagree: the headline clamps each row, while command/day/period/history readers clamp their aggregates. A saving of 100 → 50 followed by an expansion of 100 → 200 consequently reports +50 in the headline and 0 elsewhere instead of the real net −50 (−25%).

This change keeps signed token deltas throughout the reporting path:

  • Use i64 for saved-token fields in history, summary, command/day groups, and daily/weekly/monthly statistics; retain existing unsigned input/output counts
  • Preserve negative values in text, JSON/CSV, period totals, and the quota estimate
  • Remove the hardcoded history minus prefix, so losses no longer render as --62% (0)
  • Render loss bars explicitly, with a shared zero axis for graphs containing negative days
  • List the ten largest net-regressing command groups independently of the top-ten savers, respecting project scope and deterministic ordering
  • Carry signed savings through cc-economics and reuse the monthly tracking rate consistently

Zero-input records retain their signed token delta with a finite 0% rate. Existing SQLite INTEGER columns already support signed values: no schema migration, version bump, or historical rewrite is needed. Rows that older versions already stored with clamped zero savings remain unchanged; this limitation is documented.

Scope and related work

This branch starts directly from develop at c356374, independently of #4435. It does not alter filters, command output, hooks, baseline/window accounting, or token-estimate caps. Intentionally expanded diagnostic output remains intact.

The history-sign correction overlaps the narrow display fix in #2818. #3943's estimate-cap/baseline work remains separate; subsequent migrations should preserve signed deltas rather than reinstate a zero floor.

Verification

  • Reproduced the original mixed-record failure before implementing the fix (+50 instead of −50)
  • Added 14 focused unit/integration tests, including mixed net-positive/net-negative/cancelling records, zero input, empty/positive-only data, temporal totals, JSON/CSV, quota, graph direction, signed formatter boundaries, project filtering, regression ordering beyond the top ten, legacy-database compatibility, and cc-economics
  • cargo fmt --all -- --check
  • cargo clippy --locked --all-targets
  • cargo test --locked --all: 4,127 passed, 0 failed, 8 pre-existing ignored
  • Focused integration suite also passes with hostile inherited RTK/Git environment variables
  • cargo build --locked --release and isolated real-command smoke check
  • git diff --check and repository test-presence check
  • Independent review; fixed its finding that a new bar-format test needed ANSI normalization for color-enabled terminals

CI run 37140636125 passed all 10 jobs on f94cd9785bbb37d5d9eeec34ca3d182727f41287, including Linux/macOS/Windows tests, benchmark, lint and security gates. CLA passed; the conditional target-branch check was skipped. This PR is ready for review.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added analytics bug Something isn't working tests-included labels Oct 3, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes gain analytics so negative net savings (regressions) are preserved as signed values throughout history, summary, command/day/period, JSON/CSV export, and quota estimates instead of being clamped to zero or hidden in some views. Adds a new 'Regressions' section listing top net-losing commands, fixes loss bar rendering in graphs, and updates cc-economics to carry signed savings consistently, with no schema migration required.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #3702


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@yeahjack
yeahjack marked this pull request as ready for review October 4, 2026 20:22
@yeahjack

Copy link
Copy Markdown
Contributor Author

@TaKO8Ki Could you review #4435 and this PR when you have a chance? They cover gain table controls and signed-savings reporting. Both are synced with develop and their latest CI is green. Thanks!

@CLAassistant

CLAassistant commented Oct 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@yeahjack
yeahjack force-pushed the fix/gain-signed-net-savings branch from ed6252c to 5ee3b9c Compare October 19, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

analytics bug Something isn't working tests-included

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk gain cannot represent a regression: saturating_sub floors negative savings to zero

2 participants