Skip to content

Commit 85ebca3

Browse files
authored
perf(rust): move pricing snapshots into a leaf crate (ccusage#1922)
* perf(rust): move pricing snapshots into a leaf crate Most commits on main are the scheduled "chore(pricing)" snapshot updates, and each one invalidated the foundation and adapters Crane layers: ccusage-core embedded the models.dev JSON through its build script, and CCUSAGE_PRICING_JSON_PATH (the litellm flake input) was passed to every layer. A models.dev or LiteLLM bump therefore rebuilt ccusage-core, every adapter, the config-schema generator, and the clippy, hawk and test bases. The snapshot build script and the committed models.dev files now live in ccusage-pricing-snapshots, a dependency-free crate only the binary links. ccusage-core keeps the loaders and receives the deflated bytes through pricing::set_embedded_snapshots, installed in main() next to the existing set_json_fetcher. Tests reach the snapshots through a test-only `test-snapshots` feature on ccusage-core, enabled from [dev-dependencies], which Cargo leaves out of `cargo build`, so neither the layers nor the shipped binary compile it. The artifact layers no longer receive CCUSAGE_PRICING_JSON_PATH or the snapshot JSON, so their derivation paths stay the same across both kinds of snapshot update. The embedded bytes are unchanged: the moved build script produces byte-identical deflated snapshots. antigravity's dev-dependency on the removed fetch-litellm-pricing feature of ccusage-core is dropped; the binary's feature now forwards to the new crate. * docs(skills): point pricing embedding at ccusage-pricing-snapshots The rust skill and the testing reference still described the snapshots as part of ccusage-core. Record where they live now, why snapshot data must stay out of ccusage-core, and that a crate whose tests load pricing enables ccusage-core's test-snapshots feature. * docs(ccusage): list ccusage-pricing-snapshots as a dependency
1 parent 7a063a0 commit 85ebca3

38 files changed

Lines changed: 241 additions & 70 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ https://github.com/astral-sh/hawk
6565
## Pricing Embedding
6666

6767
Two snapshots ship inside the binary, both fed by pinned flake inputs and loaded
68-
by `rust/crates/ccusage-core/src/pricing.rs`;
69-
`rust/crates/ccusage-core/README.md` and its `build.rs` cover the build-time half.
68+
by `rust/crates/ccusage-core/src/pricing.rs`. The build-time half lives in
69+
`rust/crates/ccusage-pricing-snapshots` (its `README.md` and `build.rs`), a leaf
70+
crate only the binary depends on: the binary hands its bytes to
71+
`pricing::set_embedded_snapshots` at startup, so the scheduled snapshot
72+
refreshes rebuild that crate and the binary, not `ccusage-core` and every
73+
adapter. Keep snapshot data out of `ccusage-core` for that reason. Tests get the
74+
snapshots through `ccusage-core`'s `test-snapshots` feature, which a crate
75+
enables from its `[dev-dependencies]`.
7076

7177
LiteLLM is the primary table. It is compacted into `OUT_DIR` at build time and
7278
never committed: Nix builds and the dev shell hand `build.rs` the locked snapshot
@@ -77,7 +83,7 @@ expensive build-dependency in the workspace. `just update-litellm-pricing`
7783
re-locks the input and validates.
7884

7985
models.dev is the committed offline fallback:
80-
`rust/crates/ccusage-core/src/models-dev-pricing.json` and
86+
`rust/crates/ccusage-pricing-snapshots/src/models-dev-pricing.json` and
8187
`rust/adapters/codex/src/codex-auto-review-fallbacks.json`, both regenerated by
8288
`just gen-models-dev-pricing` (`just update-models-dev-pricing` bumps the pinned
8389
input first). `rust/crates/ccusage-core/src/fast-multiplier-overrides.json` sits

‎.agents/skills/testing/references/rust.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ doubles as documentation, not for broad CLI behavior.
4545

4646
## Pricing And Model Names
4747

48-
Pricing resolves against two embedded snapshots: the LiteLLM one generated into
49-
`OUT_DIR` at build time, and the committed `models-dev-pricing.json`.
48+
Pricing resolves against two embedded snapshots, both compiled by
49+
`ccusage-pricing-snapshots`: the LiteLLM one generated into `OUT_DIR` at build time,
50+
and the committed `models-dev-pricing.json`. A crate whose tests load pricing enables
51+
`ccusage-core = { workspace = true, features = ["test-snapshots"] }` in its
52+
`[dev-dependencies]`; without it, loading pricing panics because only the binary
53+
installs the snapshots.
5054
`PricingMap::load_embedded()` reads only those and touches no network, so tests use it;
5155
`load_with_overrides(offline: false, ..)` is the path that fetches. Lookup is not plain
5256
equality — alias and separator handling means the tests in

‎.github/scripts/update-models-dev-lock.nu‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
use ./pricing-lock.nu report
55

66
const SNAPSHOTS = [
7-
'rust/crates/ccusage-core/src/models-dev-pricing.json'
8-
'rust/crates/ccusage-core/src/models-dev-catalog-rules.json'
7+
'rust/crates/ccusage-pricing-snapshots/src/models-dev-pricing.json'
8+
'rust/crates/ccusage-pricing-snapshots/src/models-dev-catalog-rules.json'
99
'rust/adapters/codex/src/codex-auto-review-fallbacks.json'
1010
]
1111

‎justfile‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ update-litellm-pricing:
8888

8989
# Regenerate committed models.dev snapshots from the pinned input
9090
gen-models-dev-pricing:
91-
snapshots="$(nix build .#models-dev-pricing --no-link --print-out-paths)" && cp "$snapshots/models-dev-pricing.json" rust/crates/ccusage-core/src/models-dev-pricing.json && cp "$snapshots/models-dev-catalog-rules.json" rust/crates/ccusage-core/src/models-dev-catalog-rules.json && cp "$snapshots/codex-auto-review-fallbacks.json" rust/adapters/codex/src/codex-auto-review-fallbacks.json
92-
chmod u+w rust/crates/ccusage-core/src/models-dev-pricing.json
93-
chmod u+w rust/crates/ccusage-core/src/models-dev-catalog-rules.json
91+
snapshots="$(nix build .#models-dev-pricing --no-link --print-out-paths)" && cp "$snapshots/models-dev-pricing.json" rust/crates/ccusage-pricing-snapshots/src/models-dev-pricing.json && cp "$snapshots/models-dev-catalog-rules.json" rust/crates/ccusage-pricing-snapshots/src/models-dev-catalog-rules.json && cp "$snapshots/codex-auto-review-fallbacks.json" rust/adapters/codex/src/codex-auto-review-fallbacks.json
92+
chmod u+w rust/crates/ccusage-pricing-snapshots/src/models-dev-pricing.json
93+
chmod u+w rust/crates/ccusage-pricing-snapshots/src/models-dev-catalog-rules.json
9494
chmod u+w rust/adapters/codex/src/codex-auto-review-fallbacks.json
95-
nix fmt rust/crates/ccusage-core/src/models-dev-pricing.json
96-
nix fmt rust/crates/ccusage-core/src/models-dev-catalog-rules.json
95+
nix fmt rust/crates/ccusage-pricing-snapshots/src/models-dev-pricing.json
96+
nix fmt rust/crates/ccusage-pricing-snapshots/src/models-dev-catalog-rules.json
9797
nix fmt rust/adapters/codex/src/codex-auto-review-fallbacks.json
9898

9999
# Update the pinned models.dev input, regenerate its pricing snapshot, and validate

‎nix/cargo-artifacts.nix‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ let
3131
"ccusage-terminal"
3232
"ccusage-test-support"
3333
];
34+
# The pricing snapshots are compiled into their own crate, which only the final
35+
# binary builds, so a routine snapshot refresh leaves every layer here cached.
36+
# Its sources still go into each layer because ccusage-core dev-depends on it
37+
# and Cargo resolves the whole workspace; the snapshot data itself does not.
38+
snapshotCrates = [ "ccusage-pricing-snapshots" ];
3439
agentNames = [
3540
"amp"
3641
"antigravity"
@@ -70,8 +75,6 @@ let
7075
lib.optionals (lib.elem "ccusage-core" names) [
7176
(rustRoot + /crates/ccusage-core/src/fast-multiplier-overrides.json)
7277
(rustRoot + /crates/ccusage-core/src/flex-multiplier-overrides.json)
73-
(rustRoot + /crates/ccusage-core/src/models-dev-pricing.json)
74-
(rustRoot + /crates/ccusage-core/src/models-dev-catalog-rules.json)
7578
]
7679
++ lib.optionals (lib.elem "ccusage-adapter-codex" names) [
7780
(rustRoot + /adapters/codex/src/codex-auto-review-fallbacks.json)
@@ -95,6 +98,8 @@ let
9598
+ lib.optionalString (cargoTargetArgs != "") " ${cargoTargetArgs}";
9699
artifactCommonArgs =
97100
builtins.removeAttrs commonArgs [
101+
# Only the snapshot crate's build script reads it, and no layer builds that.
102+
"CCUSAGE_PRICING_JSON_PATH"
98103
"CCUSAGE_VERSION"
99104
"cargoExtraArgs"
100105
"src"
@@ -125,13 +130,13 @@ let
125130
name = "ccusage-foundation";
126131
inherit cargoArtifacts;
127132
packages = foundationCrates;
128-
sources = foundationCrates;
133+
sources = foundationCrates ++ snapshotCrates;
129134
};
130135
adapters = mkArtifacts {
131136
name = "ccusage-adapters";
132137
cargoArtifacts = foundation;
133138
packages = adapterCrates;
134-
sources = foundationCrates ++ adapterCrates;
139+
sources = foundationCrates ++ snapshotCrates ++ adapterCrates;
135140
};
136141
cacheRoot = pkgs.linkFarm "ccusage-cargo-artifact-cache-root" [
137142
{

‎nix/rust-src.nix‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Source subset the Rust build, tests, and clippy actually read at compile time,
22
# so those derivations only rebuild when something they depend on changes — not
33
# on every docs, TypeScript, or workflow edit elsewhere in the repo:
4-
# * ccusage-core/build.rs reads ../../../flake.lock
4+
# * ccusage-pricing-snapshots/build.rs reads ../../../flake.lock
55
# * config_schema.rs tests include_str! ../../../../ccusage.example.json
66
# * main.rs tests include_str! ../../../../package.json
77
{ lib, root }:

‎nix/treefmt.nix‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ in
119119
# comment too if they are spelled out here. Only typos is excluded,
120120
# because the JSON formatter is what keeps these files reviewable.
121121
settings.formatter.typos.excludes = [
122-
"rust/crates/ccusage-core/src/models-dev-pricing.json"
123-
"rust/crates/ccusage-core/src/models-dev-catalog-rules.json"
122+
"rust/crates/ccusage-pricing-snapshots/src/models-dev-pricing.json"
123+
"rust/crates/ccusage-pricing-snapshots/src/models-dev-catalog-rules.json"
124124
"rust/adapters/codex/src/codex-auto-review-fallbacks.json"
125125
];
126126

‎rust/Cargo.lock‎

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎rust/Cargo.toml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ ccusage-cli = { path = "crates/ccusage-cli" }
4040
ccusage-cli-parser = { path = "crates/ccusage-cli-parser" }
4141
ccusage-config = { path = "crates/ccusage-config" }
4242
ccusage-core = { path = "crates/ccusage-core" }
43+
ccusage-pricing-snapshots = { path = "crates/ccusage-pricing-snapshots" }
4344
ccusage-terminal = { path = "crates/ccusage-terminal" }
4445
ccusage-test-support = { path = "crates/ccusage-test-support" }
4546
assert_fs = "1.1.4"
@@ -52,8 +53,8 @@ jiff = { version = "0.2.38", default-features = false, features = [
5253
"tzdb-bundle-platform",
5354
] }
5455
memchr = "2.8.3"
55-
# Inflate-only consumer of the deflated pricing snapshots build.rs embeds; the
56-
# compression side is a build-dependency of ccusage-core.
56+
# Inflate-only consumer of the deflated pricing snapshots ccusage-pricing-snapshots
57+
# embeds; the compression side is a build-dependency of that crate.
5758
miniz_oxide = { version = "0.9.1", default-features = false, features = ["with-alloc"] }
5859
mimalloc = "0.1.52"
5960
minreq = { version = "3.0.0", default-features = false, features = ["https-rustls"] }

‎rust/adapters/antigravity/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ serde_json.workspace = true
1212
sqlite.workspace = true
1313

1414
[dev-dependencies]
15-
ccusage-core = { workspace = true, features = ["fetch-litellm-pricing"] }
15+
ccusage-core = { workspace = true, features = ["test-snapshots"] }
1616
ccusage-test-support.workspace = true
1717
insta.workspace = true

0 commit comments

Comments
 (0)