Skip to content

fix(parser/analytics): byte-correct JSON extraction + accurate savings metrics (#111 G7) - #116

Merged
thehoff merged 2 commits into
developfrom
fix/g7-audit-111
May 20, 2026
Merged

thehoff merged 2 commits into
developfrom
fix/g7-audit-111

Conversation

@thehoff

@thehoff thehoff commented May 20, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes the G7 findings from the independent Claude review (#111). Codex-reviewed across two passes — final: mergeable.

Findings fixed

Sev Fix
CRITICAL extract_json_object (parser/mod.rs) used a char index as a byte offset — panic / corrupt JSON on any non-ASCII input. Now tracks byte offsets via char_indices() + ch.len_utf8(); the CRLF line-offset reconstruction accounts for actual line-ending bytes. Byte-correct for all UTF-8 input; the in_string guard (a } inside a string value doesn't mis-terminate) is preserved.
IMPORTANT Session adoption metric undercounted — count_rtk_commands matched only legacy rtk , not contextcrawler . The current hook emits contextcrawler <cmd>, so adoption was understated for every user on the current binary. Now matches both.
IMPORTANT compute_totals reported an unweighted mean of per-period savings percentages. Now volume-weighted (saved / (saved + input + output)), consistent with set_rtk_from_month.
IMPORTANT Follow-up: the weighted formula reported a misleading 100% when no ccusage baseline was present. rtk_avg_savings_pct is now Option<f64> — None (JSON null) when there is no baseline to compare against.
IMPORTANT Branding — RTK → Contextcrawler in session_cmd.rs headers and learn/report.rs report labels + generated rules-file text.

Test plan

  • cargo test --workspace — 2350 lib + integration suites, 0 fail
  • New tests: multibyte/CRLF JSON extraction, contextcrawler -prefix adoption, weighted-pct, no-baseline → None
  • cargo build --release clean
  • Codex review ×2 — final: mergeable

Part of the #111 audit.

🤖 Generated with Claude Code

thehoff and others added 2 commits May 20, 2026 20:42
…; adoption + branding (#111 G7)

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…#111 G7 follow-up)

compute_totals derived rtk_avg_savings_pct = saved / (saved + cc_input +
cc_output). With no ccusage data the denominator collapses to saved alone,
so saved / saved reported a meaningless 100% savings. The savings_denom > 0
guard caught divide-by-zero but not this semantic mismatch.

Gate the metric on the ccusage baseline component being present and make the
field Option<f64> — None when there is no baseline to compare against. This
serialises to JSON null and matches the codebase's pervasive Option + "—"
missing-data pattern. The weighted formula (when ccusage IS present) is
unchanged; the divide-by-zero guard is kept.

Adds test_compute_totals_no_ccusage_baseline_not_100pct: RTK saved tokens
with zero ccusage input/output yields None, never 100%.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@thehoff
thehoff merged commit eb57111 into develop May 20, 2026
4 checks passed
@thehoff
thehoff deleted the fix/g7-audit-111 branch May 20, 2026 11:16
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.

1 participant