Skip to content

Commit 29d792b

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/opencode-since-until-adapter
Resolves the workspace split from #1428, which moved the files this branch touches into separate crates: - `date_utils` now lives in `ccusage-core`. The window helpers added here (`date_within_range`, `date_range_bounds_ms`) are `pub` rather than `pub(crate)`, because the opencode adapter that calls them is its own crate now; `parse_compact_date` and `start_of_day_ms` stay private. `parse_digits` keeps main's private, overflow-checked version. - `filter_loaded_entries_by_date` moved from the claude adapter to `ccusage-adapter-common`. Main's copy still inlined the date predicate, so it now calls the shared `date_within_range` instead — the point of collapsing the three copies in the first place. - `opencode::has_data` follows the adapter crate's new `pub` surface.
2 parents 2e1af2d + 60f1984 commit 29d792b

238 files changed

Lines changed: 4187 additions & 2194 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.agents/skills/agent-sources/SKILL.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ Read only the relevant adapter README before changing parser behavior, token
3939
mappings, data directory detection, fallback models, or agent-specific CLI
4040
flags:
4141

42-
- Claude Code: `rust/crates/ccusage/src/adapter/claude/README.md`
43-
- Codex: `rust/crates/ccusage/src/adapter/codex/README.md`
44-
- OpenCode: `rust/crates/ccusage/src/adapter/opencode/README.md`
45-
- Amp: `rust/crates/ccusage/src/adapter/amp/README.md`
46-
- pi-agent: `rust/crates/ccusage/src/adapter/pi/README.md`
42+
- Claude Code: `rust/adapters/claude/src/README.md`
43+
- Codex: `rust/adapters/codex/src/README.md`
44+
- OpenCode: `rust/adapters/opencode/src/README.md`
45+
- Amp: `rust/adapters/amp/src/README.md`
46+
- pi-agent: `rust/adapters/pi/src/README.md`
4747

4848
## Implementation Notes
4949

‎.agents/skills/rust/SKILL.md‎

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,21 @@ globs: 'rust/**/*.rs,rust/**/*.toml,rust/**/build.rs'
1010

1111
# ccusage Rust
1212

13-
Use this skill for the native Rust CLI under `rust/crates/ccusage` and `rust/crates/ccusage-terminal`.
13+
Use this skill for the native Rust CLI: `rust/adapters/<agent>` holds one crate per
14+
usage source, `rust/crates` everything that is not tied to a single source. Every crate has a
15+
`README.md` stating what it owns and which Crane artifact layer it lands in; read
16+
the one for the crate you are about to touch, because that layer determines how
17+
much a change to it costs:
18+
19+
- `ccusage` - the binary; thin dispatch only.
20+
- `ccusage-core` - pricing, cost, report shaping, config, dates, progress.
21+
- `ccusage-cli` - the plain argument types; `ccusage-cli-parser` - the parser,
22+
help renderer, and embedded help JSON, which only the binary depends on.
23+
- `ccusage-adapter-common` - shared file discovery, parallel reads, and the shared
24+
agent table; `adapters/<agent>` - one crate per source;
25+
`ccusage-adapter-all` - the unified report.
26+
- `ccusage-terminal` - table and color primitives; `ccusage-test-support` -
27+
fixtures and environment guards.
1428

1529
## Source Parity
1630

@@ -19,8 +33,8 @@ the user explicitly scopes a behavior change. Before implementing or refactoring
1933
an agent, inspect the current Rust adapter and the agent source reference docs:
2034

2135
```sh
22-
fd . rust/crates/ccusage/src/adapter/<agent>
23-
sed -n '1,220p' rust/crates/ccusage/src/adapter/<agent>/README.md
36+
fd . rust/adapters/<agent>
37+
sed -n '1,220p' rust/adapters/<agent>/src/README.md
2438
```
2539

2640
When porting behavior from the historical TypeScript implementation, first find
@@ -35,17 +49,42 @@ Preserve report semantics, JSON fields, table columns, progress/spinner text, ag
3549
Do not keep growing `main.rs` or single large adapter files. Use these
3650
responsibility boundaries where practical:
3751

38-
- `adapter/<agent>/mod.rs` - public adapter surface and command wiring.
39-
- `adapter/<agent>/paths.rs` - environment variables, defaults, and path discovery.
40-
- `adapter/<agent>/parser.rs` - raw record parsing and token/model mapping.
41-
- `adapter/<agent>/loader.rs` - file walking, SQLite reads, dedupe, and date filtering entry points.
42-
- `adapter/<agent>/report.rs` - JSON/table row shaping when agent-specific.
43-
- shared modules stay in `types.rs`, `summary.rs`, `output.rs`, `pricing.rs`, `progress.rs`, and `date_utils.rs`.
52+
- `adapters/<agent>/src/lib.rs` - public adapter surface and command wiring.
53+
- `adapters/<agent>/src/paths.rs` - environment variables, defaults, and path discovery.
54+
- `adapters/<agent>/src/parser.rs` - raw record parsing and token/model mapping.
55+
- `adapters/<agent>/src/loader.rs` - file walking, SQLite reads, dedupe, and date filtering entry points.
56+
- `adapters/<agent>/src/report.rs` - JSON/table row shaping when agent-specific.
57+
- shared modules stay in `ccusage-core` (`types.rs`, `summary.rs`, `output.rs`, `pricing.rs`, `progress.rs`, `date_utils.rs`) or in `ccusage-adapter-common` when they are about reading files or rendering the shared agent table.
58+
- do not add a dependency from one adapter to another; move the shared part into `ccusage-adapter-common` instead.
4459

45-
Keep public `pub(crate)` surfaces narrow. Prefer moving tests with the code they exercise instead of leaving all Rust tests in `main.rs`.
60+
Prefer moving tests with the code they exercise instead of leaving all Rust tests in `main.rs`.
4661

4762
When splitting large Rust modules or removing duplication, use the `reduce-similarities` skill, which runs `similarity-rs` for `.rs` files.
4863

64+
## Visibility
65+
66+
Inside this workspace `pub` is only for what another crate actually uses.
67+
Everything else is `pub(crate)`, including items that other modules in the same
68+
crate reach through a module chain.
69+
70+
`just hawk` runs [hawk](https://github.com/astral-sh/hawk) over the workspace and
71+
reports `pub` items nothing outside their crate needs. Run it after adding a
72+
crate and after widening a visibility. `rust/hawk.toml` lists the shipped entry
73+
points; anything not reachable from them can be narrowed. Adding `--fix` to the
74+
underlying `cargo hawk check` applies the narrowing.
75+
76+
hawk is not in nixpkgs, so the dev shell does not provide it. Install a prebuilt
77+
release first:
78+
79+
```sh
80+
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/astral-sh/hawk/releases/latest/download/cargo-hawk-installer.sh | sh
81+
```
82+
83+
It uses `rustc_private` and only runs on the toolchain it was built against,
84+
which is why `rust-toolchain.toml` pins 1.97.1. It is experimental - its README
85+
says it is "not intended for public consumption" - and no CI check runs it, so
86+
read its findings as suggestions and confirm each one before narrowing.
87+
4988
## Pricing Embedding
5089

5190
TypeScript uses build/macro-time pricing snapshots. Rust should not rely on a manually edited `claude-pricing.json` as the only embedded source.
@@ -57,7 +96,11 @@ When changing pricing:
5796
- For Nix builds, pass the locked LiteLLM `model_prices_and_context_window.json`
5897
to `build.rs` through `CCUSAGE_PRICING_JSON_PATH`.
5998
- For non-Nix Cargo builds, have `build.rs` read the same `litellm` revision from
60-
`flake.lock` and fetch that pinned raw JSON at build time.
99+
`flake.lock` and fetch that pinned raw JSON at build time. That download lives
100+
behind the off-by-default `fetch-litellm-pricing` feature, because its rustls
101+
stack is the most expensive build-dependency in the workspace: the dev shell
102+
and every Nix package set `CCUSAGE_PRICING_JSON_PATH` instead, and only the
103+
Windows release build enables the feature.
61104
- Do not check generated LiteLLM pricing snapshots into the repository.
62105
- Keep pricing JSON filtering and compacting in `build.rs` so runtime code loads
63106
the generated build-time snapshot first, then built-in model overrides, then

‎.github/actions/build-linux-native-package/action.yaml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ runs:
2121
- uses: ./.github/actions/setup-nix-cache
2222
with:
2323
cache-scope: ${{ inputs.cache-scope }}
24+
gc-max-store-size-linux: 4G
2425

2526
- name: Build Linux static binary
2627
shell: bash
@@ -33,6 +34,13 @@ runs:
3334
nix build .#ccusage-static --print-build-logs
3435
fi
3536
37+
- name: Root current Crane artifacts for cache save
38+
shell: bash
39+
run: |
40+
system="$(nix eval --impure --raw --expr builtins.currentSystem)"
41+
nix build ".#packages.$system.ccusage-static.passthru.workspaceArtifacts.cacheRoot" \
42+
--out-link "$RUNNER_TEMP/ccusage-cargo-artifacts"
43+
3644
- name: Verify Linux binary is static
3745
shell: bash
3846
env:

‎.github/actions/build-windows-native-package/action.yaml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ runs:
1616

1717
- name: Build Cargo native binary
1818
shell: bash
19-
run: cargo build --manifest-path rust/Cargo.toml --release --bin ccusage
19+
# Nix cannot target Windows, so this is the one build that downloads the
20+
# LiteLLM pricing snapshot instead of reading it from the pinned input.
21+
run: cargo build --manifest-path rust/Cargo.toml --release --bin ccusage --features fetch-litellm-pricing
2022

2123
- name: Stage native package
2224
shell: bash

‎.github/actions/setup-nix-cache/action.yaml‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ name: Setup Nix with action cache
22
description: >
33
Install Nix and restore/save the store with cache-nix-action, shared by Linux
44
and macOS. cache-nix-action only caches non-substitutable paths (the crane
5-
deps, not the cache.nixos.org toolchain), so the cache stays small (~900MB).
6-
No gc-max-store-size: these runners are ephemeral (restore -> build -> save),
7-
so the store never accumulates across runs the way a sticky disk did. Trimming
8-
before save would only delete the unrooted crane deps and force a recompile, so
9-
we let cache-nix-action save the whole non-substitutable set as-is and rely on
10-
Blacksmith to evict stale entries.
5+
deps, not the cache.nixos.org toolchain). Linux jobs root the current Crane
6+
artifacts after building; garbage collection then removes superseded
7+
generations before saving so restored caches do not grow on every commit.
8+
Blacksmith evicts stale entries, so no purge step is needed.
119
inputs:
1210
cache-scope:
1311
description: >
@@ -16,15 +14,26 @@ inputs:
1614
each leg separate without leaning on runner.arch. Leave empty for
1715
single-leg jobs, which github.job alone already distinguishes.
1816
default: ''
17+
gc-max-store-size-linux:
18+
description: >
19+
Optional Linux store size limit applied before cache save. Callers must
20+
root every artifact they want the garbage collector to retain.
21+
default: ''
1922
runs:
2023
using: composite
2124
steps:
2225
- name: Install Nix
2326
uses: nixbuild/nix-quick-install-action@9f63be77f412a248c9d9a65a4c82cf066cdf8f0c # v35
2427
with:
2528
github_access_token: ${{ github.token }}
29+
# nix-quick-install-action writes this file verbatim, so Nix would
30+
# otherwise keep its documented default of max-jobs = 1 and build the
31+
# independent Crane artifact layers one at a time. cores stays at the
32+
# default 0 so the single dependency derivation, which compiles the whole
33+
# third-party graph, still gets every core.
2634
nix_conf: |
2735
access-tokens = github.com=${{ github.token }}
36+
max-jobs = auto
2837
2938
# github.workflow is included so workflows that share a job id (e.g.
3039
# build-native-packages in both ci.yaml and release.yaml) do not collide on
@@ -49,6 +58,7 @@ runs:
4958
restore-prefixes-first-match: |
5059
nix-${{ github.workflow }}-${{ github.job }}-${{ inputs.cache-scope }}-${{ hashFiles('flake.lock', 'flake.nix', 'default.nix', 'package.nix', 'nix/packages.nix', 'nix/static-package.nix', 'nix/darwin-x64-package.nix', 'rust-toolchain.toml', 'rust/Cargo.lock', 'rust/**/Cargo.toml') }}-
5160
nix-${{ github.workflow }}-${{ github.job }}-${{ inputs.cache-scope }}-
61+
gc-max-store-size-linux: ${{ inputs.gc-max-store-size-linux }}
5262

5363
- name: Fix single-user Nix store ownership
5464
if: runner.os != 'Windows'

‎.github/workflows/ci.yaml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,9 @@ jobs:
261261
# Packing only repackages the native binaries restored from the build
262262
# matrix below (no Rust build happens here), so the full `nix develop` dev
263263
# shell is unnecessary. The package checks bring Nu through their Nix
264-
# shebangs.
265-
- run: nix profile install --inputs-from . nixpkgs#pnpm nixpkgs#nodejs
264+
# shebangs; `publint` is the flake's own pinned build, invoked by every
265+
# package's prepack script.
266+
- run: nix profile install --inputs-from . nixpkgs#pnpm nixpkgs#nodejs .#publint
266267
- run: pnpm install --frozen-lockfile
267268
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
268269
with:

‎.github/workflows/release.yaml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,9 @@ jobs:
134134
node-version: lts/*
135135
package-manager-cache: false
136136
# node comes from setup-node above (needed for the npm registry auth);
137-
# Nushell package checks bring Nu through their Nix shebangs.
138-
- run: nix profile install --inputs-from . nixpkgs#pnpm
137+
# Nushell package checks bring Nu through their Nix shebangs. `publint` is
138+
# the flake's own pinned build, invoked by every package's prepack script.
139+
- run: nix profile install --inputs-from . nixpkgs#pnpm .#publint
139140
- run: pnpm install --frozen-lockfile
140141
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
141142
with:

‎.github/workflows/update-pricing.yaml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ jobs:
9898
run: |
9999
# Only the compacted snapshot affects the build; if a models.dev bump
100100
# leaves embedded pricing unchanged, drop the lock-only churn.
101-
if git diff --quiet -- rust/crates/ccusage/src/models-dev-pricing.json; then
102-
echo "models.dev pricing snapshot is unchanged; skipping PR."
103-
git checkout -- flake.lock rust/crates/ccusage/src/models-dev-pricing.json
101+
if git diff --quiet -- rust/crates/ccusage-core/src/models-dev-pricing.json rust/crates/ccusage-adapter-codex/src/codex-auto-review-fallbacks.json; then
102+
echo "models.dev pricing snapshots are unchanged; skipping PR."
103+
git checkout -- flake.lock rust/crates/ccusage-core/src/models-dev-pricing.json rust/crates/ccusage-adapter-codex/src/codex-auto-review-fallbacks.json
104104
fi
105105
- name: Validate updated pricing snapshot
106106
run: |
@@ -124,7 +124,7 @@ jobs:
124124
125125
branch="automation/models-dev-pricing"
126126
git checkout -B "$branch"
127-
git add flake.lock rust/crates/ccusage/src/models-dev-pricing.json
127+
git add flake.lock rust/crates/ccusage-core/src/models-dev-pricing.json rust/crates/ccusage-adapter-codex/src/codex-auto-review-fallbacks.json
128128
git commit -m "chore(pricing): update models.dev snapshot"
129129
git push --force origin "$branch"
130130

‎AGENTS.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ Check the nearest package-specific `AGENTS.md` before editing package code:
4141
- Runtime libraries for bundled packages belong in `devDependencies` unless explicitly requested otherwise.
4242
- When initializing this repository environment, run `direnv allow` so the pinned Nix dev shell is activated.
4343
- Prefer tools provided by the Nix dev shell before falling back to ad hoc installs: `rg`, `fd`, `fzf`, `delta`, `dust`, `jq`, `gh`, `hyperfine`, `similarity`, `ast-grep`, `typos`, and `typos-lsp`. When a missing tool would be useful for repeated agent work in this repository, add it to `flake.nix`.
44-
- The production CLI is Rust-first under `rust/crates/ccusage`. Put new runtime behavior there unless the work is specifically about npm packaging, generated schemas, docs tooling, or benchmark scripts.
45-
- For Rust code, keep modules small, keep `pub(crate)` surfaces narrow, prefer fixture-backed parser/loader tests, and run cargo checks through the `just` recipes when possible.
44+
- The production CLI is Rust-first under `rust/crates` and `rust/adapters`. Put source-specific
45+
runtime behavior in `rust/adapters/<agent>` and shared behavior in
46+
`ccusage-core` or `ccusage-adapter-common` unless the work is specifically about
47+
npm packaging, generated schemas, docs tooling, or benchmark scripts.
48+
- For Rust code, keep modules small, use `pub` only for what another crate uses and `pub(crate)` for everything else (`just hawk` reports the difference), prefer fixture-backed parser/loader tests, and run cargo checks through the `just` recipes when possible.
4649
- TypeScript rules still apply to `.ts`, `.tsx`, `.js`, and `.jsx` package/tooling files. Use `typescript` there, especially `satisfies` and `as const satisfies` for typed literals.
4750
- For TypeScript package code, use `logger.ts` instead of `console.log`, use `.ts` extensions for local imports, and avoid dynamic imports.
4851
- After code changes, run `just fmt` when formatting may apply. Rely on git hooks and CI for covered checks; run `just typecheck` or `just test` manually when the change touches behavior, types, package code, or when hooks/CI do not cover the edited files. `just` is the single entry point for repo tasks (`just --list`); recipes route to the pnpm workspace or the Nix flake.

‎apps/ccusage/justfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ensure-native-binary:
2222
scripts/ensure-native-binary.nu
2323

2424
generate-schema:
25-
cargo run --quiet --manifest-path ../../rust/Cargo.toml --bin generate-config-schema -- config-schema.json
25+
cargo run --quiet --manifest-path ../../rust/Cargo.toml -p ccusage-config --bin generate-config-schema -- config-schema.json
2626
oxfmt --write config-schema.json
2727
cp config-schema.json ../../docs/public/config-schema.json
2828

0 commit comments

Comments
 (0)