Skip to content

Commit 5795951

Browse files
committed
fix(pricing): break exact claim ties by source key within a declared id
Two catalogs declaring the same provider id are one provider to generation, which breaks exact-strength ties by `(sourceProviderId, sourceModelId)` - and with the ids colliding, by the source model key. The live loader kept whichever catalog arrived first instead, so a same-strength duplicate could resolve to a different rate online than in the snapshot. The claim slot now records the declared id and source key it was claimed under, and an exactly equal claim yields to a smaller source key from the same declared id. Differing declared ids were already settled by arrival order, which matches generation's ascending walk. Reported by pullfrog on #1541. Also narrows the new `cache_create_explicit` field to `pub(crate)`: nothing outside the crate reads it, and the hawk gate fails the build for exactly that.
1 parent 263c22f commit 5795951

1 file changed

Lines changed: 68 additions & 8 deletions

File tree

‎rust/crates/ccusage-core/src/pricing.rs‎

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub struct Pricing {
6565
pub cache_read_explicit: bool,
6666
/// Whether `cache_create` came from published data rather than the derived
6767
/// `input * 1.25` default, so provider-fact patches know what they may fix.
68-
pub cache_create_explicit: bool,
68+
pub(crate) cache_create_explicit: bool,
6969
pub input_above_200k: Option<f64>,
7070
pub output_above_200k: Option<f64>,
7171
pub(crate) cache_create_above_200k: Option<f64>,
@@ -316,6 +316,12 @@ struct ModelsDevClaim {
316316
struct ModelsDevClaimSlot {
317317
claim: ModelsDevClaim,
318318
stored_id: String,
319+
/// The declared id of the catalog that claimed it, and the source key it
320+
/// claimed under: generation breaks exact-strength ties by
321+
/// `(sourceProviderId, sourceModelId)`, and two catalogs can declare the
322+
/// same provider id.
323+
provider_id: String,
324+
source_key: String,
319325
}
320326

321327
/// Trust tiers, matching the generator's.
@@ -769,15 +775,23 @@ impl PricingMap {
769775
let trust = rules.rank(&provider_id);
770776
// A live catalog is the raw upstream shape, so none of
771777
// generation's verdicts are recorded in it.
772-
self.load_models_dev_models(provider.models, trust, true, &mut claims)
778+
self.load_models_dev_models(
779+
provider.models,
780+
&provider_id,
781+
trust,
782+
true,
783+
&mut claims,
784+
)
773785
})
774786
.sum()
775787
}
776788
ModelsDevJson::Models(models) => {
777789
let mut claims = FxHashMap::default();
778790
// A flat map is the generated snapshot, which carries its
779791
// verdicts as fields rather than leaving them to be rederived.
780-
self.load_models_dev_models(models, MODELS_DEV_TRUST_OWNER, false, &mut claims)
792+
// The flat snapshot has one implicit source, so any constant id
793+
// works: ties inside it are settled by the source keys.
794+
self.load_models_dev_models(models, "", MODELS_DEV_TRUST_OWNER, false, &mut claims)
781795
}
782796
})
783797
}
@@ -794,6 +808,7 @@ impl PricingMap {
794808
fn load_models_dev_models(
795809
&mut self,
796810
models: FxHashMap<String, ModelsDevModel>,
811+
provider_id: &str,
797812
trust: u8,
798813
derive_exact_only: bool,
799814
claims: &mut FxHashMap<String, ModelsDevClaimSlot>,
@@ -822,12 +837,13 @@ impl PricingMap {
822837
derive_exact_only
823838
&& rules.is_exact_only(&model_key, declared_id.unwrap_or(&model_key))
824839
});
840+
let source_key = model_key;
825841
// An empty declared id falls back to the source key, exactly as the
826842
// generator's `selectModelsDevPricingKey` does: keeping "" would
827843
// store the model under a name no lookup ever asks for.
828844
let model_id = match model.id.filter(|id| !id.is_empty()) {
829845
Some(id) => id,
830-
None => model_key,
846+
None => source_key.clone(),
831847
};
832848
// Dotted, dashed and case spellings name one model, so they contend
833849
// for one slot; kept apart, the fuzzy lookup ties between a tiered
@@ -861,10 +877,19 @@ impl PricingMap {
861877
has_cache_write: cost.cache_write.is_some(),
862878
has_context_limit: context_limit.is_some(),
863879
};
864-
if claimed
865-
.as_ref()
866-
.is_some_and(|claimed| claimed.claim >= claim)
867-
{
880+
let replaces_equal_claim = |previous: &ModelsDevClaimSlot| {
881+
// Generation compares equal-strength candidates by
882+
// `(sourceProviderId, sourceModelId)`, smaller first. Providers
883+
// load here in ascending declared-id order, so a differing
884+
// declared id is already settled by arrival; only a collision
885+
// falls through to the source keys.
886+
previous.claim == claim
887+
&& previous.provider_id == provider_id
888+
&& source_key < previous.source_key
889+
};
890+
if claimed.as_ref().is_some_and(|claimed| {
891+
claimed.claim > claim || (claimed.claim == claim && !replaces_equal_claim(claimed))
892+
}) {
868893
continue;
869894
}
870895
// A replacement under a different spelling supersedes the losing
@@ -929,6 +954,8 @@ impl PricingMap {
929954
ModelsDevClaimSlot {
930955
claim,
931956
stored_id: model_id,
957+
provider_id: provider_id.to_string(),
958+
source_key,
932959
},
933960
)
934961
.is_none()
@@ -3245,6 +3272,39 @@ mod tests {
32453272
assert!((entry.input * 1e6 - 1.0).abs() < 1e-9);
32463273
}
32473274

3275+
#[test]
3276+
fn live_models_dev_pricing_breaks_exact_ties_by_source_key_across_same_id_catalogs() {
3277+
// Two catalogs declaring the same provider id are one provider to
3278+
// generation, which breaks exact-strength ties by the source model key.
3279+
// The smaller source key lives in the catalog with the LARGER map key
3280+
// here, so keeping whichever arrived first would pick the other rate.
3281+
let json = r#"{
3282+
"aaa-key": {
3283+
"id": "some-gateway",
3284+
"models": {
3285+
"zz-spelling": {
3286+
"id": "some-reseller-only-model",
3287+
"cost": { "input": 9, "output": 9 }
3288+
}
3289+
}
3290+
},
3291+
"zzz-key": {
3292+
"id": "some-gateway",
3293+
"models": {
3294+
"aa-spelling": {
3295+
"id": "some-reseller-only-model",
3296+
"cost": { "input": 1, "output": 2 }
3297+
}
3298+
}
3299+
}
3300+
}"#;
3301+
let mut pricing = PricingMap::default();
3302+
3303+
assert_eq!(pricing.load_models_dev_json_missing(json), Some(1));
3304+
let entry = pricing.find_exact("some-reseller-only-model").unwrap();
3305+
assert!((entry.input * 1e6 - 1.0).abs() < 1e-9);
3306+
}
3307+
32483308
#[test]
32493309
fn live_models_dev_pricing_skips_flat_fee_catalogs() {
32503310
// `kimi-for-coding` is a subscription plan, so it publishes zero token

0 commit comments

Comments
 (0)