vct-core 2.3.1

Vibe Coding Tracker core library - parse local AI coding assistant session data into CodeAnalysis results
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
use crate::utils::{
    find_pricing_cache_for_date_in, get_pricing_cache_path_in, list_pricing_cache_files_in,
};
use anyhow::{Context, Result};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// A threshold-based pricing tier.
///
/// When a request's total input context (input + cache_read + cache_creation)
/// exceeds `threshold_tokens`, these per-token prices replace the base prices
/// for ALL token types on the model. Matches the Anthropic / Google "above Nk
/// tokens" model where the entire request switches to a higher rate once the
/// prompt crosses a size threshold.
///
/// `cache_creation_input_token_cost_above_1hr` is the price for cache writes
/// with Anthropic's extended (1 hour) TTL. A value of `0.0` means the model
/// doesn't offer 1hr cached writes at this tier — callers should fall back to
/// the 5-minute (`cache_creation_input_token_cost`) price.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct ThresholdTier {
    /// Total input context (in tokens) above which this tier's prices take over.
    pub threshold_tokens: i64,
    /// Input price in USD per token at this tier.
    #[serde(default)]
    pub input_cost_per_token: f64,
    /// Output price in USD per token at this tier.
    #[serde(default)]
    pub output_cost_per_token: f64,
    /// Cache-read price in USD per token at this tier.
    #[serde(default)]
    pub cache_read_input_token_cost: f64,
    /// Cache-write (5-minute TTL) price in USD per token at this tier.
    #[serde(default)]
    pub cache_creation_input_token_cost: f64,
    /// Cache-write (1-hour TTL) price in USD per token; `0.0` falls back to the 5-minute rate.
    #[serde(default)]
    pub cache_creation_input_token_cost_above_1hr: f64,
}

/// A single range for range-based tiered pricing (Qwen / doubao style).
///
/// Matches when `input_tokens` falls in `[min_tokens, max_tokens)`. Unlike
/// `ThresholdTier`, each range is a fully independent price table — base
/// prices are not used as fallback.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct TierRange {
    /// Inclusive lower bound of the input-token range this row prices.
    pub min_tokens: i64,
    /// Exclusive upper bound of the input-token range this row prices.
    pub max_tokens: i64,
    /// Input price in USD per token within this range.
    #[serde(default)]
    pub input_cost_per_token: f64,
    /// Output price in USD per token within this range.
    #[serde(default)]
    pub output_cost_per_token: f64,
    /// Cache-read price in USD per token within this range.
    #[serde(default)]
    pub cache_read_input_token_cost: f64,
    /// Reasoning-token price in USD per token; `0.0` falls back to `output_cost_per_token`.
    #[serde(default)]
    pub output_cost_per_reasoning_token: f64,
}

/// Pricing data for a single AI model in USD per token.
///
/// Supports three strategies, checked in this order by `calculate_cost`:
/// 1. **Range-based** (`ranges` is `Some`): `input_tokens` selects a `TierRange`
///    and its prices are applied standalone. Used by Qwen / doubao families.
/// 2. **Threshold-based** (`tiers` is non-empty): the highest `ThresholdTier`
///    whose `threshold_tokens` is exceeded by total input context wins; all
///    token types switch to that tier's prices. Used by Claude Sonnet 4.x,
///    Gemini 2.5 Pro, Gemini 1.5 (128k), GPT-5.x (272k), etc.
/// 3. **Flat** (neither set): base prices apply to every request.
///
/// This struct is only ever held in memory — `tiers` / `ranges` are derived
/// from the raw `*_above_Nk_tokens` / `tiered_pricing` keys of LiteLLM by
/// `parse_litellm_entry`. Cache files store the raw LiteLLM cost fields
/// verbatim (see `filter_cost_fields`); reloading the cache runs
/// them back through `parse_litellm_entry` so the derived structures are
/// reconstructed freshly on every launch.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ModelPricing {
    /// Base input price in USD per token.
    #[serde(default)]
    pub input_cost_per_token: f64,
    /// Base output price in USD per token.
    #[serde(default)]
    pub output_cost_per_token: f64,
    /// Base cache-read price in USD per token.
    #[serde(default)]
    pub cache_read_input_token_cost: f64,
    /// Base cache-write (5-minute TTL) price in USD per token.
    #[serde(default)]
    pub cache_creation_input_token_cost: f64,

    /// Price per token for cache writes using Anthropic's extended (1 hour) TTL.
    /// `0.0` means the model doesn't support 1hr cached writes — callers fall
    /// back to `cache_creation_input_token_cost` (5-minute TTL price).
    #[serde(default)]
    pub cache_creation_input_token_cost_above_1hr: f64,

    /// Price for reasoning / thinking tokens emitted as part of the assistant
    /// response but billed separately from regular output tokens. Populated
    /// by Gemini 2.5 flash / flash-lite (`thoughts_tokens`), perplexity
    /// `sonar-deep-research`, and some qwen-turbo entries. `0.0` means the
    /// model doesn't split reasoning from output — callers fall back to
    /// `output_cost_per_token`.
    #[serde(default)]
    pub output_cost_per_reasoning_token: f64,

    /// Price in USD for one server-side web-search request
    /// (`server_tool_use.web_search_requests`), billed per query rather than
    /// per token. Derived by `parse_litellm_entry` from LiteLLM's nested
    /// `search_context_cost_per_query` object (Anthropic charges a flat
    /// $0.01 across its low/medium/high sizes). `0.0` means the model
    /// publishes no web-search price.
    #[serde(default)]
    pub web_search_cost_per_query: f64,

    /// Threshold-based tiers, sorted ascending by `threshold_tokens`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tiers: Vec<ThresholdTier>,

    /// Range-based pricing (mutually exclusive with `tiers` in practice).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ranges: Option<Vec<TierRange>>,
}

/// Extracts the numeric token count from a LiteLLM threshold suffix.
///
/// E.g. `"200k_tokens" → Some(200_000)`, `"1hr" → None`.
fn parse_threshold_suffix(suffix: &str) -> Option<i64> {
    let without_tokens = suffix.strip_suffix("_tokens")?;
    let num_part = without_tokens.strip_suffix('k')?;
    num_part.parse::<i64>().ok().map(|n| n * 1000)
}

/// Parses one LiteLLM `tiered_pricing` array element into a `TierRange`.
///
/// Returns `None` unless the element is an object with a two-element `range`
/// array, which is how non-token tiers (e.g. `max_results_range`) are skipped.
fn parse_tier_range(value: &serde_json::Value) -> Option<TierRange> {
    let obj = value.as_object()?;
    let range = obj.get("range")?.as_array()?;
    if range.len() != 2 {
        return None;
    }
    let min = range[0].as_f64()? as i64;
    let max = range[1].as_f64()? as i64;
    let f = |k: &str| obj.get(k).and_then(|v| v.as_f64()).unwrap_or(0.0);
    Some(TierRange {
        min_tokens: min,
        max_tokens: max,
        input_cost_per_token: f("input_cost_per_token"),
        output_cost_per_token: f("output_cost_per_token"),
        cache_read_input_token_cost: f("cache_read_input_token_cost"),
        output_cost_per_reasoning_token: f("output_cost_per_reasoning_token"),
    })
}

/// Converts one LiteLLM model entry into our normalized `ModelPricing`.
///
/// Extracts base prices, consolidates all `*_above_Nk_tokens` fields into
/// `ThresholdTier` rows keyed by the numeric threshold, and parses
/// `tiered_pricing` arrays into `TierRange` rows. `cache_creation_input_token_cost_above_1hr`
/// is captured as a separate 1-hour TTL price (base and per-tier). Unsupported
/// fields (batch / priority / audio / computer_use) are ignored.
pub fn parse_litellm_entry(value: &serde_json::Value) -> ModelPricing {
    let obj = match value.as_object() {
        Some(o) => o,
        None => return ModelPricing::default(),
    };

    let mut pricing = ModelPricing::default();
    let mut tier_input: HashMap<i64, f64> = HashMap::new();
    let mut tier_output: HashMap<i64, f64> = HashMap::new();
    let mut tier_cache_read: HashMap<i64, f64> = HashMap::new();
    let mut tier_cache_creation: HashMap<i64, f64> = HashMap::new();
    // 1-hour TTL variants: a threshold of 0 means the base (non-tiered) 1hr price.
    let mut tier_cache_creation_1hr: HashMap<i64, f64> = HashMap::new();

    for (key, raw_val) in obj {
        if key == "tiered_pricing" {
            if let Some(arr) = raw_val.as_array() {
                let ranges: Vec<TierRange> = arr.iter().filter_map(parse_tier_range).collect();
                if !ranges.is_empty() {
                    pricing.ranges = Some(ranges);
                }
            }
            continue;
        }

        if key == "search_context_cost_per_query" {
            // Nested object { search_context_size_low/medium/high }. Anthropic
            // bills a flat rate across sizes, so take the medium tier with the
            // low/high siblings as fallbacks (handled here, not via as_f64()).
            if let Some(sc) = raw_val.as_object() {
                let pick = |k: &str| sc.get(k).and_then(|v| v.as_f64());
                pricing.web_search_cost_per_query = pick("search_context_size_medium")
                    .or_else(|| pick("search_context_size_low"))
                    .or_else(|| pick("search_context_size_high"))
                    .unwrap_or(0.0);
            }
            continue;
        }

        let num_value = match raw_val.as_f64() {
            Some(n) => n,
            None => continue,
        };

        match key.as_str() {
            "input_cost_per_token" => pricing.input_cost_per_token = num_value,
            "output_cost_per_token" => pricing.output_cost_per_token = num_value,
            "cache_read_input_token_cost" => pricing.cache_read_input_token_cost = num_value,
            "cache_creation_input_token_cost" => {
                pricing.cache_creation_input_token_cost = num_value
            }
            "cache_creation_input_token_cost_above_1hr" => {
                // Base (non-tiered) 1hr TTL price.
                pricing.cache_creation_input_token_cost_above_1hr = num_value;
            }
            "output_cost_per_reasoning_token" => {
                pricing.output_cost_per_reasoning_token = num_value;
            }
            _ => {
                if let Some(suffix) = key.strip_prefix("input_cost_per_token_above_") {
                    if let Some(th) = parse_threshold_suffix(suffix) {
                        tier_input.insert(th, num_value);
                    }
                } else if let Some(suffix) = key.strip_prefix("output_cost_per_token_above_") {
                    if let Some(th) = parse_threshold_suffix(suffix) {
                        tier_output.insert(th, num_value);
                    }
                } else if let Some(suffix) = key.strip_prefix("cache_read_input_token_cost_above_")
                {
                    if let Some(th) = parse_threshold_suffix(suffix) {
                        tier_cache_read.insert(th, num_value);
                    }
                } else if let Some(suffix) =
                    key.strip_prefix("cache_creation_input_token_cost_above_")
                {
                    // Two possible shapes:
                    //   "200k_tokens"           → context-size tier at 200K
                    //   "1hr_above_200k_tokens" → 1hr TTL variant of the 200K tier
                    if let Some(inner) = suffix.strip_prefix("1hr_above_") {
                        if let Some(th) = parse_threshold_suffix(inner) {
                            tier_cache_creation_1hr.insert(th, num_value);
                        }
                    } else if !suffix.starts_with("1hr")
                        && let Some(th) = parse_threshold_suffix(suffix)
                    {
                        tier_cache_creation.insert(th, num_value);
                    }
                }
            }
        }
    }

    let mut thresholds: Vec<i64> = tier_input
        .keys()
        .chain(tier_output.keys())
        .chain(tier_cache_read.keys())
        .chain(tier_cache_creation.keys())
        .chain(tier_cache_creation_1hr.keys())
        .copied()
        .collect();
    thresholds.sort();
    thresholds.dedup();

    pricing.tiers = thresholds
        .into_iter()
        .map(|th| ThresholdTier {
            threshold_tokens: th,
            input_cost_per_token: *tier_input.get(&th).unwrap_or(&pricing.input_cost_per_token),
            output_cost_per_token: *tier_output
                .get(&th)
                .unwrap_or(&pricing.output_cost_per_token),
            cache_read_input_token_cost: *tier_cache_read
                .get(&th)
                .unwrap_or(&pricing.cache_read_input_token_cost),
            cache_creation_input_token_cost: *tier_cache_creation
                .get(&th)
                .unwrap_or(&pricing.cache_creation_input_token_cost),
            // Intentionally do NOT inherit base 1hr into the tier: if LiteLLM
            // doesn't publish a tier-specific 1hr price, leaving this at 0 lets
            // `calculate_cost` fall back to the tier's own 5m rate. Inheriting
            // base 1hr could produce a tier 1hr price BELOW the tier 5m price
            // (nonsensical) whenever the 200K tier substantially marks up the
            // 5m rate but the base 1hr stays at its unmarked level.
            cache_creation_input_token_cost_above_1hr: tier_cache_creation_1hr
                .get(&th)
                .copied()
                .unwrap_or(0.0),
        })
        .collect();

    // Range-based models: sort by min_tokens ascending so selection can assume
    // ordering (LiteLLM data is already sorted, but being explicit makes the
    // `calculate_cost` dispatch logic simpler to reason about).
    if let Some(ranges) = pricing.ranges.as_mut() {
        ranges.sort_by_key(|r| r.min_tokens);
    }

    pricing
}

/// Parses the full LiteLLM `model_prices_and_context_window.json` payload.
pub fn parse_litellm_pricing_map(raw: serde_json::Value) -> HashMap<String, ModelPricing> {
    let obj = match raw.as_object() {
        Some(o) => o,
        None => return HashMap::new(),
    };
    obj.iter()
        .filter(|(_, v)| v.is_object())
        .map(|(k, v)| (k.clone(), parse_litellm_entry(v)))
        .collect()
}

/// Copies every `cost`-related key from a LiteLLM model entry into a new
/// object, preserving values verbatim (including nested objects like
/// `search_context_cost_per_query`).
///
/// We keep *all* keys whose name contains `cost` rather than only the ones
/// the current `calculate_cost` knows how to consume. That way the on-disk
/// cache is a faithful, diff-able subset of the upstream LiteLLM JSON —
/// future calculation strategies (priority / flex / batch tiers, audio /
/// image modalities, reasoning-token splits, …) don't require re-fetching
/// or a schema migration to gain access to the numbers they need.
///
/// `tiered_pricing` is whitelisted explicitly even though the key name
/// doesn't contain `cost`: its array values are the **only** source of
/// range-based pricing (Qwen / doubao / dashscope), so dropping it would
/// silently zero out those models on every cache reload.
///
/// Returns `None` when the entry has no cost-related keys at all; such
/// models carry nothing we can price against and are skipped at the map
/// level.
pub fn filter_cost_fields(value: &Value) -> Option<Value> {
    let obj = value.as_object()?;
    let mut filtered = Map::with_capacity(obj.len());
    for (k, v) in obj {
        if k.contains("cost") || k == "tiered_pricing" {
            filtered.insert(k.clone(), v.clone());
        }
    }
    if filtered.is_empty() {
        None
    } else {
        Some(Value::Object(filtered))
    }
}

/// Builds the on-disk cache payload: a map from model name to its
/// cost-only subset (see `filter_cost_fields`). Non-object top-level
/// entries (e.g. LiteLLM's `sample_spec`, which is kept — it still has
/// cost keys) and entries with no cost keys are dropped here.
pub fn build_filtered_cost_json(raw: &Value) -> Value {
    let obj = match raw.as_object() {
        Some(o) => o,
        None => return Value::Object(Map::new()),
    };
    let mut filtered_map = Map::with_capacity(obj.len());
    for (model, entry) in obj {
        if let Some(filtered) = filter_cost_fields(entry) {
            filtered_map.insert(model.clone(), filtered);
        }
    }
    Value::Object(filtered_map)
}

pub(crate) fn pricing_cache_date() -> String {
    Utc::now().date_naive().format("%Y-%m-%d").to_string()
}

/// Removes outdated pricing cache files in `dir`, keeping only today's cache.
///
/// Best-effort: a failure to list or delete a file is logged or ignored rather
/// than propagated, since a stale cache file is harmless and rotates out daily.
pub fn cleanup_old_cache_in(dir: &Path) {
    let today = pricing_cache_date();

    for (filename, path) in list_pricing_cache_files_in(dir) {
        if !filename.contains(&today) {
            let _ = fs::remove_file(&path);
            log::debug!("Removed old cache file: {:?}", path);
        }
    }
}

/// Loads pricing data from today's cache file under an explicit cache dir.
///
/// The cache stores the raw LiteLLM cost-field subset (see
/// `build_filtered_cost_json`) rather than our derived `ModelPricing`
/// shape, so we re-run `parse_litellm_entry` here to rebuild `tiers`
/// and `ranges` on load.
///
/// Pre-Phase-2 versions serialised the derived `ModelPricing` struct
/// directly, carrying top-level `tiers` / `ranges` arrays instead of
/// the raw `*_above_Nk_tokens` / `tiered_pricing` keys.
/// `parse_litellm_entry` would silently drop those arrays (they aren't
/// cost-keyed scalars) and under-price every tier- or range-priced
/// model until the cache rotated the next day. We detect that shape
/// via `looks_like_legacy_pricing_cache` and return `Err` so
/// `fetch_model_pricing` falls through to a refetch, which overwrites
/// the stale cache with the new schema.
///
/// # Errors
///
/// Returns an error if no cache file exists for today, the file cannot be
/// read, its contents are not valid JSON, it contains no priced model, or the
/// file is in the pre-Phase-2 legacy schema (deliberately treated as an error
/// to force a refetch).
pub fn load_from_cache_in(dir: &Path) -> Result<HashMap<String, ModelPricing>> {
    let today = pricing_cache_date();
    let cache_path = find_pricing_cache_for_date_in(dir, &today)
        .ok_or_else(|| anyhow::anyhow!("No cache file found for today"))?;

    let content = fs::read_to_string(&cache_path).context("Failed to read cached pricing file")?;
    let raw: Value =
        serde_json::from_str(&content).context("Failed to parse cached pricing JSON")?;

    if !raw.is_object() {
        anyhow::bail!("cached pricing JSON must be an object");
    }

    if looks_like_legacy_pricing_cache(&raw) {
        log::warn!(
            "Detected pre-Phase-2 pricing cache format at {:?}; refetching to avoid silent tier/range data loss",
            cache_path
        );
        anyhow::bail!("legacy pricing cache format detected, forcing refetch");
    }

    let parsed = parse_litellm_pricing_map(raw);
    if !pricing_rates_are_valid(&parsed) {
        anyhow::bail!("cached pricing JSON contains a negative or non-finite price");
    }
    let pricing = normalize_pricing(parsed);
    if pricing.is_empty() {
        anyhow::bail!("cached pricing JSON has no priced models");
    }
    Ok(pricing)
}

/// Heuristic: does this cache file look like a pre-Phase-2 serialised
/// `ModelPricing` map?
///
/// The new schema (`build_filtered_cost_json` → `filter_cost_fields`)
/// only emits keys that either contain `cost` or equal `tiered_pricing`,
/// so it never produces top-level `tiers` / `ranges` arrays on an
/// entry. The old schema (`ModelPricing` via derived `Serialize`) did
/// emit them whenever a model carried tier or range data. Any entry
/// with such a key is a definitive signal of the old format.
fn looks_like_legacy_pricing_cache(raw: &Value) -> bool {
    let Some(obj) = raw.as_object() else {
        return false;
    };
    obj.values()
        .filter_map(|v| v.as_object())
        .any(|entry| entry.contains_key("tiers") || entry.contains_key("ranges"))
}

/// Saves a raw LiteLLM cost-field subset to today's cache file under an explicit
/// cache dir and cleans up old caches.
///
/// Callers should pass the output of `build_filtered_cost_json` so the
/// on-disk payload is a cost-only projection of the upstream LiteLLM JSON
/// — that keeps the cache file small, diff-able against upstream, and
/// forward-compatible with calculation strategies that aren't wired up
/// yet.
///
/// # Errors
///
/// Returns an error if the JSON cannot be serialized or atomically written.
/// Cleanup of old cache files runs only after a successful write and swallows
/// its own errors.
pub fn save_to_cache_in(dir: &Path, filtered_raw: &Value) -> Result<()> {
    let today = pricing_cache_date();
    let cache_path = get_pricing_cache_path_in(dir, &today);

    crate::utils::write_json_atomic_pretty(&cache_path, filtered_raw)
        .context("Failed to write pricing cache file")?;

    cleanup_old_cache_in(dir);
    Ok(())
}

/// Filters out models whose every pricing field is zero (unpriced / free) or
/// whose usable pricing fields contain an invalid rate.
///
/// A model is kept if **any** of the following yields a positive price:
/// - The base-level per-token costs.
/// - Any tier entry (`ThresholdTier`) with at least one positive field.
/// - Any range entry (`TierRange`) with at least one positive field.
///
/// Models are dropped when every strategy they publish is entirely zero or any
/// usable rate is negative or non-finite. This preserves synthetic models that
/// ship tier or range data without base prices while excluding invalid, free,
/// and placeholder entries from LiteLLM.
pub fn normalize_pricing(
    mut pricing: HashMap<String, ModelPricing>,
) -> HashMap<String, ModelPricing> {
    pricing.retain(|_name, p| {
        let has_base = p.input_cost_per_token > 0.0
            || p.output_cost_per_token > 0.0
            || p.cache_read_input_token_cost > 0.0
            || p.cache_creation_input_token_cost > 0.0
            || p.cache_creation_input_token_cost_above_1hr > 0.0
            || p.output_cost_per_reasoning_token > 0.0
            || p.web_search_cost_per_query > 0.0;
        let has_positive_tier = p.tiers.iter().any(|t| {
            t.input_cost_per_token > 0.0
                || t.output_cost_per_token > 0.0
                || t.cache_read_input_token_cost > 0.0
                || t.cache_creation_input_token_cost > 0.0
                || t.cache_creation_input_token_cost_above_1hr > 0.0
        });
        let has_positive_range = p
            .ranges
            .as_ref()
            .map(|rs| {
                rs.iter().any(|r| {
                    r.input_cost_per_token > 0.0
                        || r.output_cost_per_token > 0.0
                        || r.cache_read_input_token_cost > 0.0
                        || r.output_cost_per_reasoning_token > 0.0
                })
            })
            .unwrap_or(false);
        model_pricing_rates_are_valid(p) && (has_base || has_positive_tier || has_positive_range)
    });
    pricing
}

pub(crate) fn pricing_rates_are_valid(pricing: &HashMap<String, ModelPricing>) -> bool {
    pricing.values().all(model_pricing_rates_are_valid)
}

fn model_pricing_rates_are_valid(pricing: &ModelPricing) -> bool {
    let valid = |rate: f64| rate.is_finite() && rate >= 0.0;
    valid(pricing.input_cost_per_token)
        && valid(pricing.output_cost_per_token)
        && valid(pricing.cache_read_input_token_cost)
        && valid(pricing.cache_creation_input_token_cost)
        && valid(pricing.cache_creation_input_token_cost_above_1hr)
        && valid(pricing.output_cost_per_reasoning_token)
        && valid(pricing.web_search_cost_per_query)
        && pricing.tiers.iter().all(|tier| {
            valid(tier.input_cost_per_token)
                && valid(tier.output_cost_per_token)
                && valid(tier.cache_read_input_token_cost)
                && valid(tier.cache_creation_input_token_cost)
                && valid(tier.cache_creation_input_token_cost_above_1hr)
        })
        && pricing.ranges.as_ref().is_none_or(|ranges| {
            ranges.iter().all(|range| {
                valid(range.input_cost_per_token)
                    && valid(range.output_cost_per_token)
                    && valid(range.cache_read_input_token_cost)
                    && valid(range.output_cost_per_reasoning_token)
            })
        })
}

#[cfg(test)]
mod parser_tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn parses_flat_model_no_tiers() {
        // A typical Anthropic Opus entry — no above_Xk fields, no tiered_pricing.
        let raw = json!({
            "input_cost_per_token": 5e-6,
            "output_cost_per_token": 2.5e-5,
            "cache_read_input_token_cost": 5e-7,
            "cache_creation_input_token_cost": 6.25e-6,
            "cache_creation_input_token_cost_above_1hr": 1e-5,
            "max_input_tokens": 200000
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.input_cost_per_token, 5e-6);
        assert_eq!(p.output_cost_per_token, 2.5e-5);
        assert_eq!(p.cache_read_input_token_cost, 5e-7);
        assert_eq!(p.cache_creation_input_token_cost, 6.25e-6);
        // above_1hr is cache TTL, not a context-size tier — must NOT become a tier.
        assert!(p.tiers.is_empty());
        assert!(p.ranges.is_none());
    }

    #[test]
    fn parses_web_search_cost_per_query() {
        // LiteLLM ships web-search pricing as a nested object; the parser
        // collapses it to a per-query scalar (medium tier; Anthropic is a flat
        // $0.01 across sizes). The value is a non-numeric object, so it must be
        // handled before the `as_f64()` skip.
        let raw = json!({
            "input_cost_per_token": 5e-6,
            "output_cost_per_token": 2.5e-5,
            "search_context_cost_per_query": {
                "search_context_size_low": 0.01,
                "search_context_size_medium": 0.01,
                "search_context_size_high": 0.01
            }
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.web_search_cost_per_query, 0.01);
        assert_eq!(p.input_cost_per_token, 5e-6);
    }

    #[test]
    fn web_search_cost_absent_defaults_to_zero() {
        let raw = json!({ "input_cost_per_token": 5e-6 });
        assert_eq!(parse_litellm_entry(&raw).web_search_cost_per_query, 0.0);
    }

    #[test]
    fn parses_sonnet_like_with_200k_tier() {
        let raw = json!({
            "input_cost_per_token": 3e-6,
            "output_cost_per_token": 1.5e-5,
            "cache_read_input_token_cost": 3e-7,
            "cache_creation_input_token_cost": 3.75e-6,
            "input_cost_per_token_above_200k_tokens": 6e-6,
            "output_cost_per_token_above_200k_tokens": 2.25e-5,
            "cache_read_input_token_cost_above_200k_tokens": 6e-7,
            "cache_creation_input_token_cost_above_200k_tokens": 7.5e-6
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.tiers.len(), 1);
        let t = &p.tiers[0];
        assert_eq!(t.threshold_tokens, 200_000);
        assert_eq!(t.input_cost_per_token, 6e-6);
        assert_eq!(t.output_cost_per_token, 2.25e-5);
        assert_eq!(t.cache_read_input_token_cost, 6e-7);
        assert_eq!(t.cache_creation_input_token_cost, 7.5e-6);
    }

    #[test]
    fn parses_multiple_thresholds_sorted() {
        // Synthetic GPT-5.x-like entry with 272k tier.
        let raw = json!({
            "input_cost_per_token": 1e-6,
            "output_cost_per_token": 2e-6,
            "input_cost_per_token_above_272k_tokens": 4e-6,
            "output_cost_per_token_above_272k_tokens": 8e-6,
            "input_cost_per_token_above_128k_tokens": 2e-6,
            "output_cost_per_token_above_128k_tokens": 4e-6
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.tiers.len(), 2);
        // Must be sorted ascending by threshold.
        assert_eq!(p.tiers[0].threshold_tokens, 128_000);
        assert_eq!(p.tiers[1].threshold_tokens, 272_000);
        assert_eq!(p.tiers[0].input_cost_per_token, 2e-6);
        assert_eq!(p.tiers[1].input_cost_per_token, 4e-6);
    }

    #[test]
    fn missing_tier_fields_fall_back_to_base() {
        // Only input has a 200k override; output/cache should inherit base.
        let raw = json!({
            "input_cost_per_token": 1e-6,
            "output_cost_per_token": 2e-6,
            "cache_read_input_token_cost": 1e-7,
            "input_cost_per_token_above_200k_tokens": 2e-6
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.tiers.len(), 1);
        let t = &p.tiers[0];
        assert_eq!(t.input_cost_per_token, 2e-6);
        assert_eq!(t.output_cost_per_token, 2e-6); // from base
        assert_eq!(t.cache_read_input_token_cost, 1e-7); // from base
    }

    #[test]
    fn parses_tiered_pricing_ranges() {
        // Mimics dashscope/qwen3-coder-plus structure.
        let raw = json!({
            "tiered_pricing": [
                {
                    "range": [0, 32000],
                    "input_cost_per_token": 1e-6,
                    "output_cost_per_token": 5e-6,
                    "cache_read_input_token_cost": 1e-7
                },
                {
                    "range": [32000, 128000],
                    "input_cost_per_token": 1.8e-6,
                    "output_cost_per_token": 9e-6
                },
                {
                    "range": [256000, 1000000],
                    "input_cost_per_token": 6e-6,
                    "output_cost_per_token": 6e-5
                }
            ]
        });
        let p = parse_litellm_entry(&raw);
        let ranges = p.ranges.expect("ranges should be parsed");
        assert_eq!(ranges.len(), 3);
        assert_eq!(ranges[0].min_tokens, 0);
        assert_eq!(ranges[0].max_tokens, 32_000);
        assert_eq!(ranges[0].input_cost_per_token, 1e-6);
        assert_eq!(ranges[1].input_cost_per_token, 1.8e-6);
        assert_eq!(ranges[2].max_tokens, 1_000_000);
    }

    #[test]
    fn skips_non_token_tiered_pricing() {
        // exa_ai / firecrawl use max_results_range — not token-based. Skip.
        let raw = json!({
            "tiered_pricing": [
                {"max_results_range": [0, 25], "input_cost_per_query": 0.005}
            ]
        });
        let p = parse_litellm_entry(&raw);
        assert!(p.ranges.is_none());
    }

    #[test]
    fn parses_combined_1hr_plus_200k_tier() {
        // Claude 3.5 Sonnet-like: has both `_above_1hr` (base) and
        // `_above_1hr_above_200k_tokens` (tiered 1hr). Verify the tiered 1hr
        // price lands on the right tier entry, not inherited from base.
        let raw = json!({
            "input_cost_per_token": 3e-6,
            "output_cost_per_token": 1.5e-5,
            "cache_creation_input_token_cost": 3.75e-6,
            "cache_creation_input_token_cost_above_1hr": 7.5e-6,
            "input_cost_per_token_above_200k_tokens": 6e-6,
            "output_cost_per_token_above_200k_tokens": 2.25e-5,
            "cache_creation_input_token_cost_above_200k_tokens": 7.5e-6,
            "cache_creation_input_token_cost_above_1hr_above_200k_tokens": 1.5e-5
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.cache_creation_input_token_cost_above_1hr, 7.5e-6);
        assert_eq!(p.tiers.len(), 1);
        let t = &p.tiers[0];
        assert_eq!(t.threshold_tokens, 200_000);
        assert_eq!(t.cache_creation_input_token_cost, 7.5e-6);
        // The tiered 1hr price must be $15/M (from `_above_1hr_above_200k_tokens`),
        // NOT the base 1hr $7.5/M.
        assert_eq!(t.cache_creation_input_token_cost_above_1hr, 1.5e-5);
    }

    #[test]
    fn tier_1hr_left_zero_when_missing_so_calculate_cost_can_fall_back() {
        // If LiteLLM publishes a 200K tier but omits the 1hr-tiered field, the
        // parser must NOT inherit base 1hr (that could yield tier_1hr < tier_5m).
        let raw = json!({
            "input_cost_per_token": 3e-6,
            "cache_creation_input_token_cost": 3.75e-6,
            "cache_creation_input_token_cost_above_1hr": 6e-6,
            "cache_creation_input_token_cost_above_200k_tokens": 7.5e-6
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.cache_creation_input_token_cost_above_1hr, 6e-6);
        let t = &p.tiers[0];
        assert_eq!(t.cache_creation_input_token_cost, 7.5e-6);
        assert_eq!(t.cache_creation_input_token_cost_above_1hr, 0.0);
    }

    #[test]
    fn cache_reload_reconstructs_tiers_from_raw_keys() {
        // Cache files now store the raw LiteLLM cost-field subset rather
        // than our derived `ModelPricing` shape. Reloading must rebuild
        // `tiers` by re-running `parse_litellm_entry`, or a Sonnet-style
        // 200K tier would silently vanish and under-price large sessions.
        let raw = json!({
            "input_cost_per_token": 3e-6,
            "output_cost_per_token": 1.5e-5,
            "cache_read_input_token_cost": 3e-7,
            "cache_creation_input_token_cost": 3.75e-6,
            "input_cost_per_token_above_200k_tokens": 6e-6,
            "output_cost_per_token_above_200k_tokens": 2.25e-5,
            "cache_read_input_token_cost_above_200k_tokens": 6e-7,
            "cache_creation_input_token_cost_above_200k_tokens": 7.5e-6
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.tiers.len(), 1, "tier must be rebuilt on cache reload");
        assert_eq!(p.tiers[0].threshold_tokens, 200_000);
    }

    #[test]
    fn parses_output_cost_per_reasoning_token() {
        // Gemini 2.5 Flash and friends bill `thoughts_tokens` at a separate
        // per-token rate. Older `ModelPricing` dropped this field entirely;
        // the parser now preserves it as a base-level price.
        let raw = json!({
            "input_cost_per_token": 3e-7,
            "output_cost_per_token": 2.5e-6,
            "output_cost_per_reasoning_token": 2.5e-6
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.output_cost_per_reasoning_token, 2.5e-6);
    }

    #[test]
    fn filter_cost_fields_keeps_only_cost_keys() {
        let raw = json!({
            "input_cost_per_token": 3e-6,
            "output_cost_per_token": 1.5e-5,
            "cache_creation_input_token_cost_above_1hr": 6e-6,
            "max_input_tokens": 200_000,
            "supports_vision": true,
            "litellm_provider": "anthropic",
            "search_context_cost_per_query": {"search_context_size_high": 0.01}
        });
        let filtered = filter_cost_fields(&raw).expect("has cost keys");
        let obj = filtered.as_object().unwrap();
        assert!(obj.contains_key("input_cost_per_token"));
        assert!(obj.contains_key("output_cost_per_token"));
        assert!(obj.contains_key("cache_creation_input_token_cost_above_1hr"));
        assert!(
            obj.contains_key("search_context_cost_per_query"),
            "nested cost objects must survive the filter"
        );
        assert!(!obj.contains_key("max_input_tokens"));
        assert!(!obj.contains_key("supports_vision"));
        assert!(!obj.contains_key("litellm_provider"));
    }

    #[test]
    fn filter_cost_fields_returns_none_for_non_cost_entries() {
        // Some LiteLLM entries (e.g. retired / embedding-only models) have
        // no cost-related keys at all. They should be dropped from the
        // cache, not serialised as empty objects.
        let raw = json!({
            "max_input_tokens": 8192,
            "litellm_provider": "azure"
        });
        assert!(filter_cost_fields(&raw).is_none());
    }

    #[test]
    fn build_filtered_cost_json_skips_entries_without_cost_keys() {
        let raw = json!({
            "model-a": {
                "input_cost_per_token": 1e-6,
                "max_input_tokens": 8192
            },
            "model-b": {
                "max_input_tokens": 16384
            }
        });
        let filtered = build_filtered_cost_json(&raw);
        let obj = filtered.as_object().unwrap();
        assert!(obj.contains_key("model-a"));
        assert!(!obj.contains_key("model-b"));
        let a = obj["model-a"].as_object().unwrap();
        assert!(a.contains_key("input_cost_per_token"));
        assert!(!a.contains_key("max_input_tokens"));
    }

    #[test]
    fn ranges_are_sorted_by_min_tokens_after_parse() {
        // Feed intentionally-unsorted ranges; parser should sort ascending.
        let raw = json!({
            "tiered_pricing": [
                {"range": [128_000, 256_000], "input_cost_per_token": 3e-6},
                {"range": [0, 32_000],        "input_cost_per_token": 1e-6},
                {"range": [32_000, 128_000],  "input_cost_per_token": 2e-6}
            ]
        });
        let p = parse_litellm_entry(&raw);
        let ranges = p.ranges.expect("ranges");
        assert_eq!(ranges.len(), 3);
        assert_eq!(ranges[0].min_tokens, 0);
        assert_eq!(ranges[1].min_tokens, 32_000);
        assert_eq!(ranges[2].min_tokens, 128_000);
    }

    #[test]
    fn ignores_unknown_fields() {
        let raw = json!({
            "input_cost_per_token": 1e-6,
            "output_cost_per_token": 2e-6,
            "input_cost_per_token_priority": 5e-6,
            "input_cost_per_token_batches": 5e-7,
            "output_cost_per_reasoning_token": 3e-6,
            "supports_vision": true,
            "litellm_provider": "anthropic"
        });
        let p = parse_litellm_entry(&raw);
        assert_eq!(p.input_cost_per_token, 1e-6);
        assert_eq!(p.output_cost_per_token, 2e-6);
        assert!(p.tiers.is_empty());
        assert!(p.ranges.is_none());
    }

    #[test]
    fn filter_cost_fields_preserves_tiered_pricing() {
        // `tiered_pricing` is the only source of range-based pricing
        // (Qwen / doubao / dashscope). Its key name doesn't contain
        // "cost", so without an explicit whitelist the filter would
        // silently drop range data on every cache rotation.
        let raw = json!({
            "input_cost_per_token": 0.0,
            "tiered_pricing": [
                {
                    "range": [0, 32000],
                    "input_cost_per_token": 1e-6,
                    "output_cost_per_token": 5e-6
                },
                {
                    "range": [32000, 128000],
                    "input_cost_per_token": 1.8e-6,
                    "output_cost_per_token": 9e-6
                }
            ],
            "max_input_tokens": 1_000_000,
            "litellm_provider": "dashscope"
        });
        let filtered = filter_cost_fields(&raw).expect("has cost keys");
        let obj = filtered.as_object().unwrap();
        assert!(
            obj.contains_key("tiered_pricing"),
            "tiered_pricing must survive the filter — it carries range-based pricing data"
        );
        let ranges = obj["tiered_pricing"].as_array().expect("array preserved");
        assert_eq!(ranges.len(), 2);
        assert!(!obj.contains_key("max_input_tokens"));
        assert!(!obj.contains_key("litellm_provider"));
    }

    #[test]
    fn cache_roundtrip_preserves_range_priced_models() {
        // Full-pipeline regression: a range-priced model goes through
        // `build_filtered_cost_json` (what `save_to_cache` writes) and
        // back through `parse_litellm_pricing_map` (what
        // `load_from_cache` reads). `ranges` must survive end-to-end —
        // earlier iterations of the filter dropped `tiered_pricing` as
        // a non-cost key, zeroing every Qwen / doubao model.
        let upstream = json!({
            "qwen3-coder-plus": {
                "tiered_pricing": [
                    {
                        "range": [0, 32000],
                        "input_cost_per_token": 1e-6,
                        "output_cost_per_token": 5e-6
                    },
                    {
                        "range": [32000, 128000],
                        "input_cost_per_token": 1.8e-6,
                        "output_cost_per_token": 9e-6
                    }
                ],
                "max_input_tokens": 1_000_000,
                "litellm_provider": "dashscope"
            }
        });

        let filtered = build_filtered_cost_json(&upstream);
        let reloaded = parse_litellm_pricing_map(filtered);

        let p = reloaded
            .get("qwen3-coder-plus")
            .expect("model must survive roundtrip");
        let ranges = p.ranges.as_ref().expect("ranges must be rebuilt on reload");
        assert_eq!(ranges.len(), 2);
        assert_eq!(ranges[0].min_tokens, 0);
        assert_eq!(ranges[0].max_tokens, 32_000);
        assert_eq!(ranges[0].input_cost_per_token, 1e-6);
        assert_eq!(ranges[1].min_tokens, 32_000);
        assert_eq!(ranges[1].input_cost_per_token, 1.8e-6);
    }

    #[test]
    fn looks_like_legacy_pricing_cache_flags_tiers_array() {
        // Pre-Phase-2 cache format: serialised `ModelPricing` with a
        // top-level `tiers` array. The new format never emits this key
        // at the entry level (the filter drops it), so its presence
        // unambiguously signals the old shape.
        let legacy = json!({
            "claude-sonnet-4-6": {
                "input_cost_per_token": 3e-6,
                "output_cost_per_token": 1.5e-5,
                "tiers": [
                    {
                        "threshold_tokens": 200_000,
                        "input_cost_per_token": 6e-6,
                        "output_cost_per_token": 2.25e-5
                    }
                ]
            }
        });
        assert!(looks_like_legacy_pricing_cache(&legacy));
    }

    #[test]
    fn looks_like_legacy_pricing_cache_flags_ranges_field() {
        let legacy = json!({
            "qwen-plus": {
                "ranges": [
                    {
                        "min_tokens": 0,
                        "max_tokens": 32_000,
                        "input_cost_per_token": 1e-6
                    }
                ]
            }
        });
        assert!(looks_like_legacy_pricing_cache(&legacy));
    }

    #[test]
    fn looks_like_legacy_pricing_cache_accepts_new_format() {
        // New format keeps only cost-named keys plus `tiered_pricing`
        // and never emits top-level `tiers` or `ranges` arrays on an
        // entry, so a fresh cache file must not trip the detector.
        let new_format = json!({
            "claude-sonnet-4-6": {
                "input_cost_per_token": 3e-6,
                "output_cost_per_token": 1.5e-5,
                "input_cost_per_token_above_200k_tokens": 6e-6,
                "output_cost_per_token_above_200k_tokens": 2.25e-5
            },
            "qwen3-coder-plus": {
                "tiered_pricing": [
                    {
                        "range": [0, 32000],
                        "input_cost_per_token": 1e-6
                    }
                ]
            }
        });
        assert!(!looks_like_legacy_pricing_cache(&new_format));
    }
}

#[cfg(test)]
mod serialization_tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_model_pricing_default() {
        // Test ModelPricing default values
        let pricing = ModelPricing::default();

        assert_eq!(pricing.input_cost_per_token, 0.0);
        assert_eq!(pricing.output_cost_per_token, 0.0);
        assert_eq!(pricing.cache_read_input_token_cost, 0.0);
        assert_eq!(pricing.cache_creation_input_token_cost, 0.0);
        assert!(pricing.tiers.is_empty());
        assert!(pricing.ranges.is_none());
    }

    #[test]
    fn test_model_pricing_serialization() {
        // Test ModelPricing can be serialized and deserialized with a threshold tier
        let pricing = ModelPricing {
            input_cost_per_token: 0.000001,
            output_cost_per_token: 0.000002,
            cache_read_input_token_cost: 0.0000001,
            cache_creation_input_token_cost: 0.0000005,
            tiers: vec![ThresholdTier {
                threshold_tokens: 200_000,
                input_cost_per_token: 0.000002,
                output_cost_per_token: 0.000004,
                cache_read_input_token_cost: 0.0000002,
                cache_creation_input_token_cost: 0.000001,
                ..Default::default()
            }],
            ranges: None,
            ..Default::default()
        };

        let json = serde_json::to_string(&pricing).unwrap();
        let deserialized: ModelPricing = serde_json::from_str(&json).unwrap();

        assert_eq!(
            deserialized.input_cost_per_token,
            pricing.input_cost_per_token
        );
        assert_eq!(deserialized.tiers.len(), 1);
        assert_eq!(deserialized.tiers[0].threshold_tokens, 200_000);
        assert_eq!(deserialized.tiers[0].input_cost_per_token, 0.000002);
    }

    #[test]
    fn test_model_pricing_clone() {
        // Vec means ModelPricing is no longer Copy — explicit clone is required.
        let pricing1 = ModelPricing {
            input_cost_per_token: 0.000001,
            output_cost_per_token: 0.000002,
            ..Default::default()
        };

        let pricing2 = pricing1.clone();

        assert_eq!(pricing1.input_cost_per_token, pricing2.input_cost_per_token);
        assert_eq!(
            pricing1.output_cost_per_token,
            pricing2.output_cost_per_token
        );
    }

    #[test]
    fn test_model_pricing_debug() {
        // Test ModelPricing debug formatting
        let pricing = ModelPricing::default();
        let debug_str = format!("{:?}", pricing);

        assert!(debug_str.contains("ModelPricing"));
    }

    #[test]
    fn test_model_pricing_with_partial_data() {
        // Test deserializing with partial data (using #[serde(default)])
        let json = r#"{"input_cost_per_token": 0.000001}"#;
        let pricing: ModelPricing = serde_json::from_str(json).unwrap();

        assert_eq!(pricing.input_cost_per_token, 0.000001);
        assert_eq!(pricing.output_cost_per_token, 0.0); // Should use default
    }

    #[test]
    fn test_model_pricing_empty_json() {
        // Test deserializing empty JSON object
        let json = "{}";
        let pricing: ModelPricing = serde_json::from_str(json).unwrap();

        assert_eq!(pricing.input_cost_per_token, 0.0);
        assert_eq!(pricing.output_cost_per_token, 0.0);
    }

    #[test]
    fn test_model_pricing_hashmap_serialization() {
        // Test HashMap<String, ModelPricing> serialization
        let mut pricing_map = HashMap::new();
        pricing_map.insert(
            "gpt-4".to_string(),
            ModelPricing {
                input_cost_per_token: 0.000030,
                output_cost_per_token: 0.000060,
                ..Default::default()
            },
        );
        pricing_map.insert(
            "claude-3".to_string(),
            ModelPricing {
                input_cost_per_token: 0.000015,
                output_cost_per_token: 0.000075,
                ..Default::default()
            },
        );

        let json = serde_json::to_string(&pricing_map).unwrap();
        let deserialized: HashMap<String, ModelPricing> = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.len(), 2);
        assert!(deserialized.contains_key("gpt-4"));
        assert!(deserialized.contains_key("claude-3"));
    }

    #[test]
    fn test_model_pricing_all_fields() {
        // Verify base prices + tiers + ranges all survive round-trip serialization.
        let pricing = ModelPricing {
            input_cost_per_token: 1.0,
            output_cost_per_token: 2.0,
            cache_read_input_token_cost: 3.0,
            cache_creation_input_token_cost: 4.0,
            output_cost_per_reasoning_token: 11.0,
            web_search_cost_per_query: 0.01,
            tiers: vec![ThresholdTier {
                threshold_tokens: 200_000,
                input_cost_per_token: 5.0,
                output_cost_per_token: 6.0,
                cache_read_input_token_cost: 7.0,
                cache_creation_input_token_cost: 8.0,
                cache_creation_input_token_cost_above_1hr: 12.0,
            }],
            cache_creation_input_token_cost_above_1hr: 10.0,
            ranges: Some(vec![TierRange {
                min_tokens: 0,
                max_tokens: 32_000,
                input_cost_per_token: 0.1,
                output_cost_per_token: 0.2,
                cache_read_input_token_cost: 0.01,
                output_cost_per_reasoning_token: 0.5,
            }]),
        };

        let json = serde_json::to_string(&pricing).unwrap();
        let deserialized: ModelPricing = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.input_cost_per_token, 1.0);
        assert_eq!(deserialized.output_cost_per_token, 2.0);
        assert_eq!(deserialized.cache_read_input_token_cost, 3.0);
        assert_eq!(deserialized.cache_creation_input_token_cost, 4.0);
        assert_eq!(deserialized.output_cost_per_reasoning_token, 11.0);
        assert_eq!(deserialized.web_search_cost_per_query, 0.01);
        assert_eq!(deserialized.tiers.len(), 1);
        assert_eq!(deserialized.tiers[0].threshold_tokens, 200_000);
        assert_eq!(deserialized.tiers[0].input_cost_per_token, 5.0);
        assert_eq!(deserialized.tiers[0].output_cost_per_token, 6.0);
        assert_eq!(deserialized.tiers[0].cache_read_input_token_cost, 7.0);
        assert_eq!(deserialized.tiers[0].cache_creation_input_token_cost, 8.0);
        let ranges = deserialized.ranges.unwrap();
        assert_eq!(ranges.len(), 1);
        assert_eq!(ranges[0].max_tokens, 32_000);
        assert_eq!(ranges[0].output_cost_per_reasoning_token, 0.5);
    }

    #[test]
    fn test_model_pricing_zero_values() {
        // Test with all zero values
        let pricing = ModelPricing::default();
        let json = serde_json::to_string(&pricing).unwrap();
        let deserialized: ModelPricing = serde_json::from_str(&json).unwrap();

        // All should be zero
        assert_eq!(deserialized.input_cost_per_token, 0.0);
        assert_eq!(deserialized.output_cost_per_token, 0.0);
    }

    #[test]
    fn test_model_pricing_negative_values() {
        // Test that negative values are preserved (although not realistic)
        let pricing = ModelPricing {
            input_cost_per_token: -0.000001,
            output_cost_per_token: -0.000002,
            ..Default::default()
        };

        let json = serde_json::to_string(&pricing).unwrap();
        let deserialized: ModelPricing = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.input_cost_per_token, -0.000001);
        assert_eq!(deserialized.output_cost_per_token, -0.000002);
    }

    #[test]
    fn test_model_pricing_very_small_values() {
        // Test with very small values (scientific notation)
        let pricing = ModelPricing {
            input_cost_per_token: 1e-10,
            output_cost_per_token: 1e-15,
            ..Default::default()
        };

        let json = serde_json::to_string(&pricing).unwrap();
        let deserialized: ModelPricing = serde_json::from_str(&json).unwrap();

        assert!((deserialized.input_cost_per_token - 1e-10).abs() < 1e-20);
        assert!((deserialized.output_cost_per_token - 1e-15).abs() < 1e-25);
    }

    #[test]
    fn test_model_pricing_large_values() {
        // Test with large values
        let pricing = ModelPricing {
            input_cost_per_token: 1000000.0,
            output_cost_per_token: 9999999.99,
            ..Default::default()
        };

        let json = serde_json::to_string(&pricing).unwrap();
        let deserialized: ModelPricing = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.input_cost_per_token, 1000000.0);
        assert_eq!(deserialized.output_cost_per_token, 9999999.99);
    }
}