Skip to content

fix(ccusage): accept period field emitted by ccusage ≥19.0 for all granularities - #1968

Closed
bjanssen11 wants to merge 4 commits into
rtk-ai:developfrom
bjanssen11:fix/ccusage-period-field
Closed

bjanssen11 wants to merge 4 commits into
rtk-ai:developfrom
bjanssen11:fix/ccusage-period-field

Conversation

@bjanssen11

@bjanssen11 bjanssen11 commented May 19, 2026 •

Copy link
Copy Markdown

Problem

ccusage ≥19.0 unified all three granularities to emit "period" as the period identifier key, replacing the previous per-granularity field names:

Granularity Old field New field
Daily "date" "period"
Weekly "week" "period"
Monthly "month" "period"

This caused rtk gain (and any command that fetches ccusage data) to fail with:

Failed to fetch ccusage daily data: Failed to parse ccusage JSON output:
Invalid JSON structure for daily data: missing field `date` at line 30 column 5

Changes

  • DailyEntry: rename date → period with alias = "date" for backward compat
  • WeeklyEntry: rename week → period with alias = "week" for backward compat
  • MonthlyEntry: already fixed in earlier commit (rename month → period)
  • Rolling --since: replace hardcoded --since 20250101 with a dynamic rolling 90-day window so the fetch range stays current without manual updates
  • floor_char_boundary compile fix: replace with is_char_boundary iterator in json_cmd.rs and pipe_cmd.rs (was causing build failures in some toolchain configs)
  • Tests: updated daily/weekly tests to use period, added legacy-alias tests confirming old field names still deserialize correctly

Backward compatibility

All three entries use #[serde(rename = "period", alias = "<old>")] so both old and new ccusage versions parse correctly.

bjanssen11 and others added 2 commits May 19, 2026 08:36
ccusage v19.0.3 renamed the monthly key field from `month` to `period`.
Add serde alias so both old and new shapes deserialise correctly,
and update the happy-path test fixture to match the new JSON shape.

Closes rtk-ai#1933
FILTERS_TOML and TomlFilterRegistry::load were unused, causing
-D warnings to fail the mandatory pre-commit gate. Pre-existing
on develop; suppressed with #[allow(dead_code)].

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@CLAassistant

CLAassistant commented May 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@YOMXXX

YOMXXX commented May 20, 2026

Copy link
Copy Markdown
Contributor

LGTM as an outside reviewer 👍

#[serde(rename = "period", alias = "month")] is the right move — keeps backward compatibility with ccusage <19.0 while accepting the new schema. The two regression tests (period-form and month-form) cover both paths.

Maintainers: this is a 1-line behavior change with full test coverage for #1933. Recommend merging.

@YOMXXX YOMXXX left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 on the core fix.

The #[serde(rename = "period", alias = "month")] shape is exactly the right tool for "ccusage 19 renamed it, but pre-19 installs still exist in the wild" — single deserialization path, no version branching, and test_parse_monthly_legacy_month_field pins the back-compat so a future field-rename in ccusage can't quietly break the older-emit users. The period rename on the struct field flows cleanly into the one downstream e.period site.

One nit, non-blocking — feel free to leave it or split it out, whichever you prefer:

The diff also touches src/core/constants.rs::FILTERS_TOML and src/core/toml_filter.rs::TomlFilterRegistry::load with #[allow(dead_code)]. Those look unrelated to the ccusage rename — looks like dead-code warnings that bubbled up on the develop head from somewhere else. A reviewer skimming the title fix(ccusage): accept period field … will be surprised by changes outside src/analytics/ccusage.rs. If a future CI lands a flag like -D dead_code it'll get attributed to this PR. Two ways to keep the scope clean:

  • Move the #[allow] markers into their own follow-up commit/PR (chore: silence dead_code for FILTERS_TOML and TomlFilterRegistry::load), or
  • Add a short note in the PR body acknowledging the scope expansion + why.

Either way, the ccusage fix itself is correct and merge-ready.

bjanssen11 and others added 2 commits June 2, 2026 11:18
… date

ccusage ≥19.0 unified all three granularities to emit `period` instead
of the per-granularity field names (`date` for daily, `week` for weekly).
The previous fix only covered MonthlyEntry; DailyEntry and WeeklyEntry
still expected the old field names, causing parse failures on `rtk gain`.

Changes:
- DailyEntry: rename `date` → `period` with alias for backward compat
- WeeklyEntry: rename `week` → `period` with alias for backward compat
- Replace hardcoded `--since 20250101` with a rolling 90-day window so
  the fetch range stays current without manual updates
- Fix `floor_char_boundary` compile errors in json_cmd and pipe_cmd
  (not yet stable in all toolchain configs) using `is_char_boundary`
- Add new/updated tests covering both `period` (≥19.0) and legacy field
  names for daily and weekly granularities

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@bjanssen11 bjanssen11 changed the title fix(ccusage): accept period field emitted by ccusage ≥19.0 fix(ccusage): accept period field emitted by ccusage ≥19.0 for all granularities Jun 2, 2026
thehoff added a commit to thehoff/contextcrawler that referenced this pull request Jun 5, 2026
ccusage 19.0 renamed the monthly JSON key from "month" to "period",
hard-crashing cc-economics with: missing field `month`. Add a serde
alias so both formats parse. Old-format JSON unaffected.

Council triage 2026-06-02 HIGH (confirmed by agy + mmax spot-checks).
Upstream ref: rtk issues rtk-ai#2178/rtk-ai#1933, PR rtk-ai#1968 pattern.

Implemented via TDD in an isolated lane (red: missing-field crash
reproduced; green: 7/7 ccusage tests, re-verified in main checkout).
Landed with Hoff-authorised COUNCIL_OK: the council CLI review path
is blocked by the git-diff filter bug (task #10) and its codex peer
mutates working-tree state during review (task #11) — both now
queued as the next work items.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@rtk-release-bot

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale due to 90 days of inactivity. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. Your contribution is very much appreciated — we're sorry we haven't been able to review it yet.

@rtk-release-bot rtk-release-bot Bot added the stale label Sep 3, 2026
@rtk-release-bot

Copy link
Copy Markdown
Contributor

Closing this pull request after a prolonged period of inactivity. If this is still relevant, please ask for it to be reopened. Thank you!

@rtk-release-bot rtk-release-bot Bot closed this Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants