Skip to content

Commit f268798

Browse files
fix(cli): apply source-specific config in unified reports (ccusage#1959)
* fix(cli): carry per-source options on unified report args A unified report loads every source, and the config can address one source at a time through `<source>.defaults`, `<source>.commands.<report>` and `commands.<source>:<report>`. Give `AgentCommandArgs` a place for the options each source resolves to, keyed by agent name, so the binary can fill it and the unified loader can read it. Nothing fills it yet, so behavior is unchanged. Refs ccusage#1957 * fix(config): expose a source's view of the config for unified reports `option_maps` reads `<agent>.defaults`, `<agent>.commands.<report>` and `commands.<agent>:<report>` only when the parsed command names an agent, so a unified report never sees them. Add `ConfigContext::for_source`, which returns the config as `ccusage <agent> <report>` reads it, and `None` when the file does not address that agent. The binary uses it to resolve the options each source of a unified report loads with. Refs ccusage#1957 * fix(cli): resolve source config sections for unified reports `ccusage daily` and the other unified reports handed every source the same shared options, so `<source>.defaults`, `<source>.commands.<report>` and `commands.<source>:<report>` only reached the focused `ccusage <source> <report>`. The docs describe those sections as applying to unified reports too, and the TypeScript CLI merged one option set per agent before loading. The binary now resolves one `SourceArgs` per source the config addresses: the same command line parsed against that source's view of the config, so flags keep precedence over config as they do for the focused command, and `--last` and the timezone check apply per source. The Codex speed and the pi and OpenClaw paths come from the same view. A bare `ccusage` is resolved into the unified daily command in the binary before the sources are attached, so `main` dispatches on a `Command` rather than on an optional one. Refs ccusage#1957 * fix(cli): load each unified source with its own options Hand the per-source options resolved by the binary to the unified loader: each source loads with its own shared options (date window, cost mode, timezone, debug), Codex uses the configured speed, and pi and OpenClaw read their configured paths, so `ccusage daily` sees a source the way `ccusage <source> daily` does. A source the config does not address keeps the unified options, and the pricing table the sources share is still built from the unified options. Closes ccusage#1957 * docs(config): say where unified reports read pricing options from Unified reports now apply source sections, as the config guide already says, but the pricing table the sources share is built from the unified options. Tell readers to keep `offline` and `pricingOverrides` under `defaults` or `commands.<report>` when they should reach every source. Refs ccusage#1957 * test(cli): cover unified source config across report modes Verify source defaults, report sections, colon command sections, and command-line precedence for unified reports. Co-authored-by: viniciusdsandrade <[email protected]> * chore: format imported source configuration changes Apply the repository formatter to the imported config and unified report tests. Co-authored-by: viniciusdsandrade <[email protected]> * fix(cli): honor source pricing and validate custom pi stores Cache pricing by resolved source options so offline mode and overrides reach each loader without leaking rates between sources. Keep named pi stores on unified options and reject overlaps with the configured pi path. Cover Fast Codex pricing with and without date bounds, independent source rates, OpenClaw paths, pi collisions, and named-store behavior with fixtures. Co-authored-by: viniciusdsandrade <[email protected]> * docs(config): clarify unified source pricing and section options Describe source pricing overrides, timezone grouping, and the invoked-command settings used by additional report sections. Co-authored-by: viniciusdsandrade <[email protected]> * test(cli): isolate unified report fixtures from host data Keep source directories and XDG paths inside each fixture so local tests cannot scan or count usage from the host environment. Co-authored-by: viniciusdsandrade <[email protected]> * docs: clarify unified source configuration contracts List the public source options, document colon-form config precedence, and state the override-free base requirement for pricing copies. Co-authored-by: viniciusdsandrade <[email protected]> * fix(cli): preserve unified source configuration semantics Use unified week starts for source windows and name the source in config errors. Carry Claude project filters and skip unused shared pricing maps. Cover source-aware sections and space-form config, and clarify named pi store settings. Keep existing report failure and section behavior. Co-authored-by: viniciusdsandrade <[email protected]> * fix(cli): apply global agent defaults to unified sources Seed Codex speed and Pi/OpenClaw paths from global config before applying source-specific sections. Preserve shared CLI options and avoid creating entries for unrelated sources. Cover default and named reports, global report options, and precedence. Co-authored-by: viniciusdsandrade <[email protected]> --------- Co-authored-by: Vinícius DS Andrade <[email protected]>
1 parent ae3e07d commit f268798

15 files changed

Lines changed: 1827 additions & 145 deletions

File tree

‎docs/guide/all-reports.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ ccusage daily --by-agent --json
3232

3333
`--sections` accepts `daily`, `weekly`, `monthly`, and `session`. The invoked report section is always included, and table output prints each requested section as a separate table. `--by-agent` adds an `agents` array to daily, weekly, and monthly JSON rows; session rows are already source-specific.
3434

35+
Every section uses the options resolved for the invoked command. For example, `ccusage daily --sections weekly,monthly` applies the `daily` configuration to all three sections; each source's configured date window and timezone are applied while its data is loaded.
36+
3537
## How Unified Views Work
3638

3739
ccusage detects local usage files from Claude Code, Codex, OpenCode, Amp, Droid, Codebuff, Hermes Agent, pi-agent, Goose, OpenClaw, Kilo, Kimi, Qwen, GitHub Copilot CLI, Gemini CLI, Antigravity, Grok Build CLI, and ZCode. The same daily, weekly, monthly, and session views can run in two modes:

‎docs/guide/config-files.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,20 @@ ccusage antigravity daily
287287
ccusage zcode daily
288288
```
289289

290-
Source-specific settings are also applied when running unified reports such as `ccusage daily`. In that case, each source receives its own merged options before data is loaded.
290+
Source-specific settings are also applied when running unified reports such as `ccusage daily`. Each source uses the same merged options as its focused command: global `defaults`, `commands.<report>`, `commands."<source>:<report>"`, `<source>.defaults`, and `<source>.commands.<report>`, with command-line flags taking precedence for every source. Set `offline` and `pricingOverrides` globally to share them across sources, or in a source section to specialize them. Set `claude.commands.daily.project` to filter Claude rows in unified daily reports and their sections. Unified weekly reports use the unified command's `startOfWeek` for every source and for `--last` calculation; source-level `startOfWeek` values are ignored. Each source groups dates with its resolved timezone before rows are combined, so sources with different timezones can place the same event on adjacent calendar dates.
291291

292292
Use `pi.stores` for additional pi-format session stores, such as tools or forks that write sessions outside `~/.pi/agent/sessions`. Named stores are additive to the default `pi` agent in unified reports, use their own agent name in JSON and tables, and prefix models with `[name]` followed by a space. Store names must match `^[a-z][a-z0-9_-]{0,31}$`, must be unique, and cannot use a built-in agent name. Each store path can be one sessions directory or a comma-separated list; `~` is expanded for store paths, nonexistent paths are treated as empty, and resolved paths that overlap the default `pi` store or another named store — including one path nested inside another — are rejected. They do not create focused commands such as `ccusage omp daily`; `PI_AGENT_DIR`, `--pi-path`, and `pi.defaults.piPath` still affect only the default `pi` agent.
293293

294-
For a namespaced command, options are applied in this order:
294+
Named stores use the unified report's options and pricing overrides; `pi.defaults` and `pi.commands` apply only to the default `pi` source.
295+
296+
For a source-focused command, or each source within a unified report, options are applied in this order:
295297

296298
1. `defaults`
297299
2. `commands.<report>`
298-
3. `<source>.defaults`
299-
4. `<source>.commands.<report>`
300-
5. Command-line arguments
300+
3. `commands."<source>:<report>"`
301+
4. `<source>.defaults`
302+
5. `<source>.commands.<report>`
303+
6. Command-line arguments
301304

302305
## Command-Specific Options
303306

‎rust/crates/ccusage-adapter-all/src/lib.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ use crate::{
3838
pub fn run(args: AgentCommandArgs) -> Result<()> {
3939
let kind = args.kind;
4040
let shared = args.shared;
41+
let sources = args.sources;
4142
let include_agents = args.by_agent;
4243
let start_of_week = args.start_of_week;
4344
if let Some(sections) = args.sections {
4445
let sections = requested_sections(kind, sections);
45-
let result = loader::load_sections(&sections, &shared, start_of_week)?;
46+
let result = loader::load_sections(&sections, &shared, &sources, start_of_week)?;
4647
if wants_json(&shared) {
4748
return report::print_sections_report_json(
4849
&result.sections,
@@ -62,7 +63,7 @@ pub fn run(args: AgentCommandArgs) -> Result<()> {
6263
}
6364
return Ok(());
6465
}
65-
let result = loader::load_rows(kind, &shared, start_of_week)?;
66+
let result = loader::load_rows(kind, &shared, &sources, start_of_week)?;
6667
if wants_json(&shared) {
6768
let output = report::report_json_with_agents(&result.rows, kind, include_agents);
6869
return print_json_or_jq(output, shared.jq.as_deref(), shared.no_cost);

0 commit comments

Comments
 (0)