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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
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
//! Shared model metadata types exchanged between Codex services and clients.
//!
//! These types are serialized across core, TUI, app-server, and SDK boundaries, so field defaults
//! are used to preserve compatibility when older payloads omit newly introduced attributes.

use std::fmt;
use std::str::FromStr;

use chrono::DateTime;
use chrono::Utc;
use schemars::JsonSchema;
use schemars::r#gen::SchemaGenerator;
use schemars::schema::InstanceType;
use schemars::schema::Metadata;
use schemars::schema::Schema;
use schemars::schema::SchemaObject;
use schemars::schema::StringValidation;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
use serde::de::DeserializeOwned;
use serde::de::Error;
use strum_macros::Display;
use strum_macros::EnumIter;
use tracing::warn;
use ts_rs::TS;

use crate::config_types::Personality;
use crate::config_types::ReasoningSummary;
use crate::config_types::SERVICE_TIER_DEFAULT_REQUEST_VALUE;
use crate::config_types::ServiceTier;
use crate::config_types::Verbosity;
use crate::protocol::MultiAgentVersion;

mod access_programs;
#[path = "openai_models/guardian.rs"]
mod guardian;
pub use guardian::GuardianModelPolicy;
pub use guardian::GuardianReviewMode;
pub use guardian::GuardianScope;

#[path = "openai_models/guardian_v2.rs"]
mod guardian_v2;
#[path = "openai_models/reasoning_effort.rs"]
mod reasoning_effort;

pub use access_programs::ModelAccessPrograms;
pub use guardian_v2::GuardianV2ModelConfig;
pub use guardian_v2::GuardianV2TranscriptModelConfig;

/// Backend model-catalog specialty identifying cybersecurity-focused models.
pub const MODEL_SPECIALTY_CYBER: &str = "cyber";
pub const SPEED_TIER_FAST: &str = "fast";

/// See https://platform.openai.com/docs/guides/reasoning?api-mode=responses#get-started-with-reasoning
#[derive(Debug, Default, Clone, PartialEq, Eq, TS, Hash)]
#[ts(type = "string")]
pub enum ReasoningEffort {
    None,
    Minimal,
    Low,
    #[default]
    Medium,
    High,
    XHigh,
    Max,
    Ultra,
    Persistent,
    /// A model-defined effort value that this client does not know yet.
    Custom(String),
}

impl ReasoningEffort {
    /// Returns the exact value used on the wire.
    pub fn as_str(&self) -> &str {
        match self {
            Self::None => "none",
            Self::Minimal => "minimal",
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
            Self::XHigh => "xhigh",
            Self::Max => "max",
            Self::Ultra => "ultra",
            Self::Persistent => "persistent",
            Self::Custom(effort) => effort,
        }
    }
}

impl fmt::Display for ReasoningEffort {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl JsonSchema for ReasoningEffort {
    fn schema_name() -> String {
        "ReasoningEffort".to_string()
    }

    fn json_schema(_generator: &mut SchemaGenerator) -> Schema {
        Schema::Object(SchemaObject {
            instance_type: Some(InstanceType::String.into()),
            metadata: Some(Box::new(Metadata {
                description: Some(
                    "A non-empty reasoning effort value advertised by the model.".to_string(),
                ),
                ..Default::default()
            })),
            string: Some(Box::new(StringValidation {
                min_length: Some(1),
                ..Default::default()
            })),
            ..Default::default()
        })
    }
}

impl Serialize for ReasoningEffort {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for ReasoningEffort {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let effort = String::deserialize(deserializer)?;
        effort.parse().map_err(D::Error::custom)
    }
}

impl FromStr for ReasoningEffort {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "none" => Ok(Self::None),
            "minimal" => Ok(Self::Minimal),
            "low" => Ok(Self::Low),
            "medium" => Ok(Self::Medium),
            "high" => Ok(Self::High),
            "xhigh" => Ok(Self::XHigh),
            "max" => Ok(Self::Max),
            "ultra" => Ok(Self::Ultra),
            "persistent" => Ok(Self::Persistent),
            "" => Err("reasoning_effort must not be empty".to_string()),
            effort => Ok(Self::Custom(effort.to_string())),
        }
    }
}

/// Canonical user-input modality tags advertised by a model.
#[derive(
    Debug,
    Serialize,
    Deserialize,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Display,
    JsonSchema,
    TS,
    EnumIter,
    Hash,
)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum InputModality {
    /// Plain text turns and tool payloads.
    Text,
    /// Image attachments included in user turns.
    Image,
    /// Audio attachments included in user turns.
    Audio,
}

/// Backward-compatible default when `input_modalities` is omitted on the wire.
///
/// Legacy payloads predate modality metadata, so we conservatively assume both text and images are
/// accepted unless a preset explicitly narrows support.
pub fn default_input_modalities() -> Vec<InputModality> {
    vec![InputModality::Text, InputModality::Image]
}

/// A reasoning effort option that can be surfaced for a model.
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq, Eq)]
pub struct ReasoningEffortPreset {
    /// Effort level that the model supports.
    pub effort: ReasoningEffort,
    /// Short human description shown next to the effort in UIs.
    pub description: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)]
pub struct ModelUpgrade {
    pub id: String,
    pub migration_config_key: String,
    pub model_link: Option<String>,
    pub upgrade_copy: Option<String>,
    pub migration_markdown: Option<String>,
    /// Informational time when the model associated with this upgrade is scheduled to retire.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_rfc3339_timestamp"
    )]
    #[schemars(with = "Option<String>")]
    #[ts(type = "string", optional)]
    pub retirement_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq, Eq)]
pub struct ModelAvailabilityNux {
    pub message: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq, Eq)]
pub struct ModelServiceTier {
    pub id: String,
    pub name: String,
    pub description: String,
}

/// Metadata describing a Codex-supported model.
#[derive(Debug, Clone, Deserialize, Serialize, TS, JsonSchema, PartialEq)]
pub struct ModelPreset {
    /// Stable identifier for the preset.
    pub id: String,
    /// Model slug (e.g., "gpt-5").
    pub model: String,
    /// Display name shown in UIs.
    pub display_name: String,
    /// Short human description shown in UIs.
    pub description: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model_specialty: Option<String>,
    /// Reasoning effort applied when none is explicitly chosen.
    pub default_reasoning_effort: ReasoningEffort,
    /// Supported reasoning effort options.
    pub supported_reasoning_efforts: Vec<ReasoningEffortPreset>,
    /// Deprecated catalog field, always false for new model presets.
    #[serde(default)]
    pub supports_personality: bool,
    /// Deprecated: use `service_tiers` instead.
    #[serde(default)]
    pub additional_speed_tiers: Vec<String>,
    /// Service tiers this model can run with.
    #[serde(default)]
    pub service_tiers: Vec<ModelServiceTier>,
    /// Catalog default service tier id for this model.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_service_tier: Option<String>,
    /// Caller-specific explicit access programs, when discovery resolved them.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub available_access_programs: Option<ModelAccessPrograms>,
    /// Whether this is the default model for new users.
    pub is_default: bool,
    /// recommended upgrade model
    pub upgrade: Option<ModelUpgrade>,
    /// Whether this preset should appear in the picker UI.
    pub show_in_picker: bool,
    /// Multi-agent backend selected when this model starts a new thread.
    #[serde(default, skip_serializing, skip_deserializing)]
    #[schemars(skip)]
    #[ts(skip)]
    pub multi_agent_version: Option<MultiAgentVersion>,
    /// Availability NUX shown when this preset becomes accessible to the user.
    pub availability_nux: Option<ModelAvailabilityNux>,
    /// whether this model is supported in the api
    pub supported_in_api: bool,
    /// Input modalities accepted when composing user turns for this preset.
    #[serde(default = "default_input_modalities")]
    pub input_modalities: Vec<InputModality>,
}

/// Visibility of a model in the picker or APIs.
#[derive(
    Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, TS, JsonSchema, EnumIter, Display,
)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum ModelVisibility {
    List,
    Hide,
    None,
}

/// Shell execution capability for a model.
#[derive(
    Debug,
    Serialize,
    Deserialize,
    Clone,
    Copy,
    PartialEq,
    Eq,
    TS,
    JsonSchema,
    EnumIter,
    Display,
    Hash,
)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum ConfigShellToolType {
    #[serde(alias = "default", alias = "local", alias = "shell_command")]
    UnifiedExec,
    Disabled,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, TS, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ApplyPatchToolType {
    Freeform,
}

#[derive(
    Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, TS, JsonSchema, Default,
)]
#[serde(rename_all = "snake_case")]
pub enum WebSearchToolType {
    #[default]
    Text,
    TextAndImage,
}

/// Server-provided truncation policy metadata for a model.
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, TS, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TruncationMode {
    Bytes,
    Tokens,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, TS, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ToolMode {
    Direct,
    CodeMode,
    CodeModeOnly,
}

fn deserialize_optional_model_selector<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
where
    D: Deserializer<'de>,
    T: DeserializeOwned,
{
    let Some(value) = Option::<String>::deserialize(deserializer)? else {
        return Ok(None);
    };
    Ok(serde_json::from_value(serde_json::Value::String(value)).ok())
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, TS, JsonSchema)]
pub struct TruncationPolicyConfig {
    pub mode: TruncationMode,
    pub limit: i64,
}

impl TruncationPolicyConfig {
    pub const fn bytes(limit: i64) -> Self {
        Self {
            mode: TruncationMode::Bytes,
            limit,
        }
    }

    pub const fn tokens(limit: i64) -> Self {
        Self {
            mode: TruncationMode::Tokens,
            limit,
        }
    }
}

/// Semantic version triple encoded as an array in JSON (e.g. [0, 62, 0]).
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, TS, JsonSchema)]
pub struct ClientVersion(pub i32, pub i32, pub i32);

const fn default_effective_context_window_percent() -> i64 {
    95
}

const fn default_true() -> bool {
    true
}

#[allow(clippy::trivially_copy_pass_by_ref)]
const fn is_true(value: &bool) -> bool {
    *value
}

/// Model metadata returned by the Codex backend `/models` endpoint.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ModelInfo {
    /// Model-owned approval coverage. Absent preserves legacy settings; an empty map disables
    /// ordinary Guardian review. Keys are computer_use, shell, file_changes, mcp, network,
    /// and permissions. This does not override mandatory safety or administrator requirements.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub guardian: Option<GuardianModelPolicy>,
    pub slug: String,
    pub display_name: String,
    pub description: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_reasoning_level: Option<ReasoningEffort>,
    pub supported_reasoning_levels: Vec<ReasoningEffortPreset>,
    pub shell_type: ConfigShellToolType,
    pub visibility: ModelVisibility,
    pub supported_in_api: bool,
    pub priority: i32,
    #[serde(default)]
    pub additional_speed_tiers: Vec<String>,
    #[serde(default)]
    pub service_tiers: Vec<ModelServiceTier>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_service_tier: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub available_access_programs: Option<ModelAccessPrograms>,
    pub availability_nux: Option<ModelAvailabilityNux>,
    pub upgrade: Option<ModelInfoUpgrade>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model_messages: Option<ModelMessages>,
    #[serde(default)]
    pub include_skills_usage_instructions: bool,
    #[serde(default)]
    pub include_plugin_usage_instructions: bool,
    #[serde(default = "default_true")]
    pub include_apps_usage_instructions: bool,
    /// Whether the model accepts the Responses API `reasoning.summary` parameter.
    #[serde(default = "default_true", skip_serializing_if = "is_true")]
    pub supports_reasoning_summary_parameter: bool,
    #[serde(default)]
    pub default_reasoning_summary: ReasoningSummary,
    pub support_verbosity: bool,
    pub default_verbosity: Option<Verbosity>,
    pub apply_patch_tool_type: Option<ApplyPatchToolType>,
    #[serde(default)]
    pub web_search_tool_type: WebSearchToolType,
    pub truncation_policy: TruncationPolicyConfig,
    #[serde(default)]
    pub supports_image_detail_original: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub context_window: Option<i64>,
    /// Maximum context window allowed for config overrides.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_context_window: Option<i64>,
    /// Token threshold for automatic compaction. When omitted, core derives it
    /// from `context_window` (90%). When provided, core clamps it to 90% of the
    /// context window when available.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub auto_compact_token_limit: Option<i64>,
    /// Opaque identifier for compaction-compatible model configurations.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub comp_hash: Option<String>,
    /// Percentage of the context window considered usable for inputs, after
    /// reserving headroom for system prompts, tool overhead, and model output.
    #[serde(default = "default_effective_context_window_percent")]
    pub effective_context_window_percent: i64,
    pub experimental_supported_tools: Vec<String>,
    /// Input modalities accepted by the backend for this model.
    #[serde(default = "default_input_modalities")]
    pub input_modalities: Vec<InputModality>,
    /// Internal-only marker set by core when a model slug resolved to fallback metadata.
    #[serde(default, skip_serializing, skip_deserializing)]
    #[schemars(skip)]
    #[ts(skip)]
    pub used_fallback_model_metadata: bool,
    #[serde(default)]
    pub supports_search_tool: bool,
    /// Whether experimental context management may be activated at session startup.
    #[serde(default)]
    pub supports_experimental_context: bool,
    #[serde(default)]
    pub use_responses_lite: bool,
    #[serde(default)]
    pub node_repl_auto_review_required: bool,
    #[serde(default)]
    pub node_repl_disabled: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub auto_review_model_override: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model_specialty: Option<String>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_model_selector"
    )]
    pub tool_mode: Option<ToolMode>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_model_selector"
    )]
    pub multi_agent_version: Option<MultiAgentVersion>,
    /// Reasoning effort used for multi-agent work when the user selects Ultra.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub multi_agent_reasoning_effort: Option<ReasoningEffort>,
}

impl ModelInfo {
    pub fn resolved_context_window(&self) -> Option<i64> {
        self.context_window.or(self.max_context_window)
    }

    /// Context available to inference after reserving this model's configured headroom.
    pub fn usable_context_window(&self) -> Option<i64> {
        self.resolved_context_window().map(|context_window| {
            context_window.saturating_mul(self.effective_context_window_percent) / 100
        })
    }

    pub fn auto_compact_token_limit(&self) -> Option<i64> {
        let context_limit = self
            .resolved_context_window()
            .map(|context_window| (context_window * 9) / 10);
        let config_limit = self.auto_compact_token_limit;
        if let Some(context_limit) = context_limit {
            return Some(
                config_limit.map_or(context_limit, |limit| std::cmp::min(limit, context_limit)),
            );
        }
        config_limit
    }

    /// Returns the literal instruction template. The personality argument remains for older
    /// callers; the `None` opt-out is applied separately by models-manager.
    pub fn get_model_instructions(&self, _personality: Option<Personality>) -> String {
        if let Some(model_messages) = &self.model_messages
            && let Some(template) = &model_messages.instructions_template
        {
            template.clone()
        } else {
            warn!(
                model = %self.slug,
                "Model has no instruction template; returning empty instructions."
            );
            String::new()
        }
    }
}

/// A strongly-typed template for assembling model instructions and developer messages.
///
/// `instructions_template` is literal text. The deprecated `instructions_variables` field is
/// retained to decode catalogs produced before personality selection was removed.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ModelMessages {
    /// Additional developer instructions for persistent mode. Missing or null uses the built-in
    /// instructions; an empty string disables them.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub persistent_instructions: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tools: Option<ToolMessages>,
    pub instructions_template: Option<String>,
    pub instructions_variables: Option<ModelInstructionsVariables>,
    pub approvals: Option<ApprovalMessages>,
    pub collaboration_modes: Option<CollaborationModeMessages>,
    pub auto_review: Option<AutoReviewMessages>,
    pub permissions: Option<PermissionMessages>,
    pub multi_agent: Option<MultiAgentMessages>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub token_budget: Option<ModelTokenBudgetConfig>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub guardian_v2: Option<GuardianV2ModelConfig>,
    /// Replacement confirmation-policy documents forwarded in actor MCP request metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub confirmation_policies: Option<ConfirmationPolicies>,
}

/// Model-owned confirmation-policy Markdown, forwarded unchanged to actor tools.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ConfirmationPolicies {
    /// Replacement Markdown for the Browser Use confirmation-policy document.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub browser_use: Option<String>,
    /// Replacement Markdown for the native Computer Use confirmation policy.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub computer_use: Option<String>,
}

/// Model-owned messages for built-in tools.
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ToolMessages {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub send_user_message_async: Option<ToolMessage>,
}

/// Model-owned messages for a built-in tool.
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ToolMessage {
    /// Missing or null uses the built-in description; an empty string leaves the description
    /// empty without disabling the tool.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// Model-owned defaults for the context-window token-budget feature.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ModelTokenBudgetConfig {
    #[serde(default)]
    pub enabled: bool,
    #[serde(default)]
    pub use_history_notes_extension: bool,
    pub reminder_threshold_tokens: i64,
    pub reminder_message_template: String,
    pub guidance_message: String,
    pub auto_compact_fallback_prompt: String,
    pub auto_compact_fallback_buffer_tokens: i64,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ApprovalMessages {
    pub on_request: Option<String>,
    pub on_request_auto_review: Option<String>,
    pub never: Option<String>,
    pub unless_trusted: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct CollaborationModeMessages {
    pub default: Option<String>,
    pub plan: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct AutoReviewMessages {
    pub policy: Option<String>,
    pub policy_template: Option<String>,
    /// Extra developer policy for `node_repl` and `cua_repl` reviews.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub node_repl_policy: Option<String>,
    pub rejection_instructions: Option<String>,
    pub timeout_instructions: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct PermissionMessages {
    pub danger_full_access: Option<String>,
    pub workspace_write: Option<String>,
    pub read_only: Option<String>,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct MultiAgentMessages {
    pub role: Option<MultiAgentRoleMessages>,
    pub mode: Option<MultiAgentModeMessages>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct MultiAgentRoleMessages {
    pub root: Option<String>,
    pub subagent: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct MultiAgentModeMessages {
    pub explicit: Option<String>,
    /// Ultra-only mode instructions. Missing or null uses the built-in proactive hint;
    /// an empty string suppresses the mode message. `hint_text` takes precedence.
    pub proactive: Option<String>,
    pub hint_text: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ModelInstructionsVariables {
    pub personality_default: Option<String>,
    pub personality_friendly: Option<String>,
    pub personality_pragmatic: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ModelInfoUpgrade {
    pub model: String,
    pub migration_markdown: String,
    /// Informational time when the model associated with this upgrade is scheduled to retire.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_rfc3339_timestamp"
    )]
    #[schemars(with = "Option<String>")]
    #[ts(type = "string", optional)]
    pub retirement_at: Option<DateTime<Utc>>,
}

fn deserialize_optional_rfc3339_timestamp<'de, D>(
    deserializer: D,
) -> Result<Option<DateTime<Utc>>, D::Error>
where
    D: Deserializer<'de>,
{
    let value = Option::<serde_json::Value>::deserialize(deserializer)?;
    Ok(value
        .as_ref()
        .and_then(serde_json::Value::as_str)
        .and_then(|value| DateTime::parse_from_rfc3339(value).ok())
        .map(|value| value.with_timezone(&Utc)))
}

impl From<&ModelUpgrade> for ModelInfoUpgrade {
    fn from(upgrade: &ModelUpgrade) -> Self {
        ModelInfoUpgrade {
            model: upgrade.id.clone(),
            migration_markdown: upgrade.migration_markdown.clone().unwrap_or_default(),
            retirement_at: upgrade.retirement_at,
        }
    }
}

/// Response wrapper for `/models`.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema, Default)]
pub struct ModelsResponse {
    #[serde(
        serialize_with = "serialize_model_infos_with_legacy_base",
        deserialize_with = "deserialize_model_infos_with_legacy_base"
    )]
    pub models: Vec<ModelInfo>,
}

#[derive(Serialize)]
struct ModelInfoWithLegacyBaseInstructionsRef<'a> {
    #[serde(flatten)]
    model: &'a ModelInfo,
    base_instructions: String,
}

#[derive(Deserialize)]
struct ModelInfoWithLegacyBaseInstructions {
    #[serde(default)]
    base_instructions: Option<String>,
    #[serde(flatten)]
    model: ModelInfo,
}

/// Serializes catalog models with the deprecated top-level instruction field required by older
/// clients.
#[doc(hidden)]
pub fn serialize_model_infos_with_legacy_base<S>(
    models: &[ModelInfo],
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    models
        .iter()
        .map(|model| ModelInfoWithLegacyBaseInstructionsRef {
            model,
            base_instructions: model.get_model_instructions(/*personality*/ None),
        })
        .collect::<Vec<_>>()
        .serialize(serializer)
}

/// Deserializes catalog models while promoting the legacy top-level instruction field into Model
/// Messages V2 when no canonical instruction template is present.
#[doc(hidden)]
pub fn deserialize_model_infos_with_legacy_base<'de, D>(
    deserializer: D,
) -> Result<Vec<ModelInfo>, D::Error>
where
    D: Deserializer<'de>,
{
    let models = Vec::<ModelInfoWithLegacyBaseInstructions>::deserialize(deserializer)?;
    models
        .into_iter()
        .map(|legacy_model| {
            let ModelInfoWithLegacyBaseInstructions {
                base_instructions,
                mut model,
            } = legacy_model;
            if let Some(base_instructions) = base_instructions
                && model
                    .model_messages
                    .as_ref()
                    .and_then(|messages| messages.instructions_template.as_ref())
                    .is_none()
            {
                let messages = model.model_messages.get_or_insert(ModelMessages {
                    persistent_instructions: None,
                    tools: None,
                    instructions_template: None,
                    instructions_variables: None,
                    approvals: None,
                    collaboration_modes: None,
                    auto_review: None,
                    permissions: None,
                    multi_agent: None,
                    token_budget: None,
                    confirmation_policies: None,
                    guardian_v2: None,
                });
                messages.instructions_template = Some(base_instructions);
            }
            if model
                .model_messages
                .as_ref()
                .and_then(|messages| messages.instructions_template.as_ref())
                .is_none()
            {
                let model_slug = &model.slug;
                return Err(D::Error::custom(format!(
                    "model `{model_slug}` is missing both `base_instructions` and \
                     `model_messages.instructions_template`"
                )));
            }
            Ok(model)
        })
        .collect()
}

// convert ModelInfo to ModelPreset
impl From<ModelInfo> for ModelPreset {
    fn from(info: ModelInfo) -> Self {
        ModelPreset {
            id: info.slug.clone(),
            model: info.slug.clone(),
            display_name: info.display_name,
            description: info.description.unwrap_or_default(),
            model_specialty: info.model_specialty,
            default_reasoning_effort: info
                .default_reasoning_level
                .unwrap_or(ReasoningEffort::None),
            supported_reasoning_efforts: info.supported_reasoning_levels.clone(),
            supports_personality: false,
            additional_speed_tiers: info.additional_speed_tiers,
            service_tiers: info.service_tiers,
            default_service_tier: info.default_service_tier,
            available_access_programs: info.available_access_programs,
            is_default: false, // default is the highest priority available model
            upgrade: info.upgrade.as_ref().map(|upgrade| ModelUpgrade {
                id: upgrade.model.clone(),
                migration_config_key: info.slug.clone(),
                // todo(aibrahim): add the model link here.
                model_link: None,
                upgrade_copy: None,
                migration_markdown: Some(upgrade.migration_markdown.clone()),
                retirement_at: upgrade.retirement_at,
            }),
            show_in_picker: info.visibility == ModelVisibility::List,
            multi_agent_version: info.multi_agent_version,
            availability_nux: info.availability_nux,
            supported_in_api: info.supported_in_api,
            input_modalities: info.input_modalities,
        }
    }
}

impl ModelPreset {
    pub fn supports_fast_mode(&self) -> bool {
        self.service_tiers
            .iter()
            .any(|tier| tier.id == ServiceTier::Fast.request_value())
            || self
                .additional_speed_tiers
                .iter()
                .any(|tier| tier == SPEED_TIER_FAST)
    }
}

impl ModelInfo {
    pub fn supports_service_tier(&self, service_tier: &str) -> bool {
        self.service_tiers
            .iter()
            .any(|tier| tier.id == service_tier)
    }

    pub fn service_tier_for_request(&self, service_tier: Option<String>) -> Option<String> {
        service_tier.filter(|service_tier| {
            service_tier != SERVICE_TIER_DEFAULT_REQUEST_VALUE
                && self.supports_service_tier(service_tier)
        })
    }
}

impl ModelPreset {
    /// Filter models based on authentication mode.
    ///
    /// In ChatGPT mode, all models are visible. Otherwise, only API-supported models are shown.
    pub fn filter_by_auth(models: Vec<ModelPreset>, chatgpt_mode: bool) -> Vec<ModelPreset> {
        models
            .into_iter()
            .filter(|model| chatgpt_mode || model.supported_in_api)
            .collect()
    }

    /// Recompute the single default preset using picker visibility.
    ///
    /// The first picker-visible model wins; if none are picker-visible, the first model wins.
    pub fn mark_default_by_picker_visibility(models: &mut [ModelPreset]) {
        for preset in models.iter_mut() {
            preset.is_default = false;
        }
        if let Some(default) = models.iter_mut().find(|preset| preset.show_in_picker) {
            default.is_default = true;
        } else if let Some(default) = models.first_mut() {
            default.is_default = true;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use serde_json::from_str;
    use serde_json::to_string;

    #[test]
    fn legacy_shell_model_metadata_deserializes_as_unified_exec() {
        for legacy_shell_type in ["default", "local", "shell_command"] {
            assert_eq!(
                from_str::<ConfigShellToolType>(&format!("\"{legacy_shell_type}\""))
                    .expect("legacy shell type"),
                ConfigShellToolType::UnifiedExec
            );
        }
        assert_eq!(
            to_string(&ConfigShellToolType::UnifiedExec).expect("serialize unified shell type"),
            "\"unified_exec\""
        );
    }

    pub(super) fn test_model(spec: Option<ModelMessages>) -> ModelInfo {
        ModelInfo {
            slug: "test-model".to_string(),
            display_name: "Test Model".to_string(),
            description: None,
            default_reasoning_level: None,
            supported_reasoning_levels: vec![],
            shell_type: ConfigShellToolType::UnifiedExec,
            visibility: ModelVisibility::List,
            supported_in_api: true,
            priority: 1,
            additional_speed_tiers: Vec::new(),
            service_tiers: Vec::new(),
            default_service_tier: None,
            available_access_programs: None,
            availability_nux: None,
            upgrade: None,
            model_messages: spec,
            include_skills_usage_instructions: false,
            include_plugin_usage_instructions: false,
            include_apps_usage_instructions: false,
            supports_reasoning_summary_parameter: true,
            default_reasoning_summary: ReasoningSummary::Auto,
            support_verbosity: false,
            default_verbosity: None,
            apply_patch_tool_type: None,
            web_search_tool_type: WebSearchToolType::Text,
            truncation_policy: TruncationPolicyConfig::bytes(/*limit*/ 10_000),
            supports_image_detail_original: false,
            context_window: None,
            max_context_window: None,
            auto_compact_token_limit: None,
            comp_hash: None,
            effective_context_window_percent: 95,
            experimental_supported_tools: vec![],
            input_modalities: default_input_modalities(),
            used_fallback_model_metadata: false,
            supports_search_tool: false,
            supports_experimental_context: false,
            use_responses_lite: false,
            guardian: None,
            node_repl_auto_review_required: false,
            node_repl_disabled: false,
            auto_review_model_override: None,
            model_specialty: None,
            tool_mode: None,
            multi_agent_version: None,
            multi_agent_reasoning_effort: None,
        }
    }
    #[test]
    fn model_messages_deserialize_without_optional_sections() {
        let messages: ModelMessages = from_str(
            r#"{"instructions_template":null,"instructions_variables":null,"persistent_instructions":null}"#,
        )
        .expect("model messages should deserialize");

        assert_eq!(
            messages,
            ModelMessages {
                persistent_instructions: None,
                tools: None,
                instructions_template: None,
                instructions_variables: None,
                approvals: None,
                collaboration_modes: None,
                auto_review: None,
                permissions: None,
                multi_agent: None,
                token_budget: None,
                confirmation_policies: None,
                guardian_v2: None,
            }
        );
    }

    #[test]
    fn send_user_message_async_description_preserves_missing_null_and_empty_values() {
        for (value, expected) in [
            (serde_json::json!({}), None),
            (serde_json::json!({"tools": null}), None),
            (serde_json::json!({"tools": {}}), Some(None)),
            (
                serde_json::json!({"tools": {"send_user_message_async": null}}),
                Some(None),
            ),
            (
                serde_json::json!({"tools": {"send_user_message_async": {}}}),
                Some(Some(ToolMessage::default())),
            ),
            (
                serde_json::json!({"tools": {"send_user_message_async": {"description": null}}}),
                Some(Some(ToolMessage::default())),
            ),
            (
                serde_json::json!({"tools": {"send_user_message_async": {"description": ""}}}),
                Some(Some(ToolMessage {
                    description: Some(String::new()),
                })),
            ),
            (
                serde_json::json!({"tools": {"send_user_message_async": {"description": "Catalog description"}}}),
                Some(Some(ToolMessage {
                    description: Some("Catalog description".to_string()),
                })),
            ),
        ] {
            let messages: ModelMessages =
                serde_json::from_value(value).expect("model messages should deserialize");
            let serialized =
                serde_json::to_value(&messages).expect("model messages should serialize");
            assert_eq!(
                (
                    messages.tools,
                    serialized
                        .get("tools")
                        .map(|tools| tools.get("send_user_message_async").cloned()),
                ),
                (
                    expected.as_ref().map(|tool| ToolMessages {
                        send_user_message_async: tool.clone(),
                    }),
                    expected.map(|tool| tool.map(|tool| match tool.description {
                        Some(description) => serde_json::json!({"description": description}),
                        None => serde_json::json!({}),
                    })),
                )
            );
        }
    }

    #[test]
    fn approval_messages_preserve_missing_and_empty_values() {
        let messages: ModelMessages = from_str(
            r#"{
                "instructions_template": null,
                "instructions_variables": null,
                "approvals": {
                    "on_request": "",
                    "never": ""
                }
            }"#,
        )
        .expect("approval messages should deserialize");

        assert_eq!(
            messages.approvals,
            Some(ApprovalMessages {
                on_request: Some(String::new()),
                on_request_auto_review: None,
                never: Some(String::new()),
                unless_trusted: None,
            })
        );
    }

    #[test]
    fn auto_review_messages_preserve_missing_and_empty_values() {
        let missing_template: ModelMessages = from_str(
            r#"{
                "instructions_template": null,
                "instructions_variables": null,
                "auto_review": {
                    "policy": "policy"
                }
            }"#,
        )
        .expect("auto-review messages should deserialize without a policy template");
        let empty_template: ModelMessages = from_str(
            r#"{
                "instructions_template": null,
                "instructions_variables": null,
                "auto_review": {
                    "policy": "policy",
                    "policy_template": "",
                    "node_repl_policy": "",
                    "rejection_instructions": "",
                    "timeout_instructions": ""
                }
            }"#,
        )
        .expect("auto-review messages should deserialize with an empty policy template");

        assert_eq!(
            missing_template.auto_review,
            Some(AutoReviewMessages {
                policy: Some("policy".to_string()),
                policy_template: None,
                node_repl_policy: None,
                rejection_instructions: None,
                timeout_instructions: None,
            })
        );
        assert_eq!(
            empty_template.auto_review,
            Some(AutoReviewMessages {
                policy: Some("policy".to_string()),
                policy_template: Some(String::new()),
                node_repl_policy: Some(String::new()),
                rejection_instructions: Some(String::new()),
                timeout_instructions: Some(String::new()),
            })
        );
    }

    #[test]
    fn permission_messages_preserve_missing_and_empty_values() {
        let messages: ModelMessages = from_str(
            r#"{
                "instructions_template": null,
                "instructions_variables": null,
                "permissions": {
                    "workspace_write": ""
                }
            }"#,
        )
        .expect("permission messages should deserialize");

        assert_eq!(
            messages.permissions,
            Some(PermissionMessages {
                danger_full_access: None,
                workspace_write: Some(String::new()),
                read_only: None,
            })
        );
    }

    #[test]
    fn multi_agent_messages_preserve_missing_and_empty_values() {
        let messages: ModelMessages = from_str(
            r#"{"instructions_template":null,"instructions_variables":null,"multi_agent":{"role":{"root":"","subagent":"subagent base"},"mode":{"explicit":"explicit mode","proactive":"","hint_text":""}}}"#,
        )
        .expect("multi-agent messages should deserialize");

        assert_eq!(
            messages.multi_agent,
            Some(MultiAgentMessages {
                role: Some(MultiAgentRoleMessages {
                    root: Some(String::new()),
                    subagent: Some("subagent base".to_string()),
                }),
                mode: Some(MultiAgentModeMessages {
                    explicit: Some("explicit mode".to_string()),
                    proactive: Some(String::new()),
                    hint_text: Some(String::new()),
                }),
            })
        );
    }

    #[test]
    fn collaboration_mode_messages_preserve_missing_and_empty_values() {
        let messages: ModelMessages = from_str(
            r#"{
                "instructions_template": null,
                "instructions_variables": null,
                "collaboration_modes": {
                    "default": ""
                }
            }"#,
        )
        .expect("collaboration mode messages should deserialize");

        assert_eq!(
            messages,
            ModelMessages {
                persistent_instructions: None,
                tools: None,
                instructions_template: None,
                instructions_variables: None,
                approvals: None,
                collaboration_modes: Some(CollaborationModeMessages {
                    default: Some(String::new()),
                    plan: None,
                }),
                auto_review: None,
                permissions: None,
                multi_agent: None,
                token_budget: None,
                confirmation_policies: None,
                guardian_v2: None,
            }
        );
    }

    #[test]
    fn reasoning_effort_accepts_known_and_custom_values() {
        let custom = ReasoningEffort::Custom("future".to_string());
        let deserialized = from_str::<ReasoningEffort>(r#""future""#)
            .expect("custom reasoning effort should deserialize");
        let serialized = to_string(&custom).expect("custom reasoning effort should serialize");
        let serialized_max = to_string(&ReasoningEffort::Max).expect("Max should serialize");
        let serialized_ultra = to_string(&ReasoningEffort::Ultra).expect("Ultra should serialize");
        let serialized_persistent =
            to_string(&ReasoningEffort::Persistent).expect("Persistent should serialize");

        assert_eq!(
            (
                "high".parse(),
                "max".parse(),
                "ultra".parse(),
                "persistent".parse(),
                "future".parse(),
                deserialized,
                serialized,
                serialized_max,
                serialized_ultra,
                serialized_persistent,
                custom.to_string(),
            ),
            (
                Ok(ReasoningEffort::High),
                Ok(ReasoningEffort::Max),
                Ok(ReasoningEffort::Ultra),
                Ok(ReasoningEffort::Persistent),
                Ok(custom.clone()),
                custom,
                r#""future""#.to_string(),
                r#""max""#.to_string(),
                r#""ultra""#.to_string(),
                r#""persistent""#.to_string(),
                "future".to_string(),
            )
        );
    }

    #[test]
    fn reasoning_effort_rejects_empty_values() {
        assert_eq!(
            "".parse::<ReasoningEffort>(),
            Err("reasoning_effort must not be empty".to_string())
        );
    }

    #[test]
    fn reasoning_effort_json_schema_is_an_open_string() {
        let mut effort_generator = SchemaGenerator::default();

        assert_eq!(
            ReasoningEffort::json_schema(&mut effort_generator),
            Schema::Object(SchemaObject {
                instance_type: Some(InstanceType::String.into()),
                metadata: Some(Box::new(Metadata {
                    description: Some(
                        "A non-empty reasoning effort value advertised by the model.".to_string(),
                    ),
                    ..Default::default()
                })),
                string: Some(Box::new(StringValidation {
                    min_length: Some(1),
                    ..Default::default()
                })),
                ..Default::default()
            })
        );
    }

    #[test]
    fn get_model_instructions_ignores_legacy_personality_variables() {
        let model = test_model(Some(ModelMessages {
            persistent_instructions: None,
            tools: None,
            instructions_template: Some("Hello {{ personality }}".to_string()),
            instructions_variables: Some(ModelInstructionsVariables {
                personality_default: Some("default".to_string()),
                personality_friendly: Some("friendly".to_string()),
                personality_pragmatic: Some("pragmatic".to_string()),
            }),
            approvals: None,
            collaboration_modes: None,
            auto_review: None,
            permissions: None,
            multi_agent: None,
            token_budget: None,
            confirmation_policies: None,
            guardian_v2: None,
        }));

        let instructions = "Hello {{ personality }}";
        for personality in [
            Some(Personality::Friendly),
            Some(Personality::Pragmatic),
            Some(Personality::None),
            None,
        ] {
            assert_eq!(model.get_model_instructions(personality), instructions);
        }
    }

    #[test]
    fn get_model_instructions_is_empty_when_template_is_missing() {
        let model = test_model(Some(ModelMessages {
            persistent_instructions: None,
            tools: None,
            instructions_template: None,
            instructions_variables: Some(ModelInstructionsVariables {
                personality_default: None,
                personality_friendly: None,
                personality_pragmatic: None,
            }),
            approvals: None,
            collaboration_modes: None,
            auto_review: None,
            permissions: None,
            multi_agent: None,
            token_budget: None,
            confirmation_policies: None,
            guardian_v2: None,
        }));

        let instructions = model.get_model_instructions(Some(Personality::Friendly));

        assert_eq!(instructions, "");
    }

    #[test]
    fn get_model_instructions_is_empty_when_model_messages_is_missing() {
        let model = test_model(/*spec*/ None);

        assert_eq!(
            model.get_model_instructions(Some(Personality::Friendly)),
            ""
        );
    }

    #[test]
    fn models_response_promotes_legacy_base_instructions() {
        let mut value = serde_json::to_value(ModelsResponse {
            models: vec![test_model(/*spec*/ None)],
        })
        .expect("serialize models response");
        value["models"][0]["base_instructions"] = serde_json::json!("legacy instructions");

        let response: ModelsResponse =
            serde_json::from_value(value).expect("deserialize legacy models response");
        let model = &response.models[0];

        assert_eq!(
            model.model_messages,
            Some(ModelMessages {
                persistent_instructions: None,
                tools: None,
                instructions_template: Some("legacy instructions".to_string()),
                instructions_variables: None,
                approvals: None,
                collaboration_modes: None,
                auto_review: None,
                permissions: None,
                multi_agent: None,
                token_budget: None,
                confirmation_policies: None,
                guardian_v2: None,
            })
        );
        assert_eq!(
            model.get_model_instructions(/*personality*/ None),
            "legacy instructions"
        );
        let serialized = serde_json::to_value(response).expect("serialize canonical response");
        assert_eq!(
            serialized["models"][0]["base_instructions"],
            "legacy instructions"
        );
    }

    #[test]
    fn models_response_rejects_model_without_instruction_source() {
        let mut value = serde_json::to_value(ModelsResponse {
            models: vec![test_model(/*spec*/ None)],
        })
        .expect("serialize models response");
        value["models"][0]
            .as_object_mut()
            .expect("model should serialize as an object")
            .remove("base_instructions")
            .expect("serialized model should include legacy base instructions");

        let error = serde_json::from_value::<ModelsResponse>(value)
            .expect_err("model without instructions should be rejected");

        assert_eq!(
            error.to_string(),
            "model `test-model` is missing both `base_instructions` and \
             `model_messages.instructions_template`"
        );
    }

    #[test]
    fn models_response_serializes_literal_legacy_base_instructions() {
        let response = ModelsResponse {
            models: vec![test_model(Some(ModelMessages {
                persistent_instructions: None,
                tools: None,
                instructions_template: Some("before {{ personality }} after".to_string()),
                instructions_variables: Some(ModelInstructionsVariables {
                    personality_default: Some("default".to_string()),
                    personality_friendly: Some("friendly".to_string()),
                    personality_pragmatic: Some("pragmatic".to_string()),
                }),
                approvals: None,
                collaboration_modes: None,
                auto_review: None,
                permissions: None,
                multi_agent: None,
                token_budget: None,
                confirmation_policies: None,
                guardian_v2: None,
            }))],
        };

        let serialized = serde_json::to_value(response).expect("serialize models response");

        assert_eq!(
            serialized["models"][0]["base_instructions"],
            "before {{ personality }} after"
        );
    }

    #[test]
    fn models_response_prefers_template_and_preserves_message_siblings() {
        let messages = ModelMessages {
            persistent_instructions: Some("Persistent catalog instructions".to_string()),
            tools: Some(ToolMessages {
                send_user_message_async: Some(ToolMessage {
                    description: Some("Catalog description".to_string()),
                }),
            }),
            instructions_template: None,
            instructions_variables: None,
            approvals: Some(ApprovalMessages {
                on_request: Some("approval".to_string()),
                on_request_auto_review: None,
                never: Some("never approval".to_string()),
                unless_trusted: Some("unless-trusted approval".to_string()),
            }),
            collaboration_modes: Some(CollaborationModeMessages {
                default: Some("default collaboration".to_string()),
                plan: Some("plan collaboration".to_string()),
            }),
            auto_review: Some(AutoReviewMessages {
                policy: Some("policy".to_string()),
                policy_template: None,
                node_repl_policy: None,
                rejection_instructions: Some("rejection instructions".to_string()),
                timeout_instructions: Some("timeout instructions".to_string()),
            }),
            permissions: Some(PermissionMessages {
                danger_full_access: None,
                workspace_write: Some("workspace".to_string()),
                read_only: None,
            }),
            multi_agent: None,
            token_budget: None,
            confirmation_policies: Some(ConfirmationPolicies {
                browser_use: Some(
                    "# Browser confirmations\n\nKeep {{literal_markdown}}.\n".to_string(),
                ),
                computer_use: Some(
                    "  # Native confirmations\r\n\nKeep ${native_markdown}.\n".to_string(),
                ),
            }),
            guardian_v2: Some(GuardianV2ModelConfig {
                classifier_instructions: Some("Guardian classification".to_string()),
                review_threshold_basis_points: Some(7_500),
                reasoning_effort: Some(ReasoningEffort::Minimal),
                transcript: Some(GuardianV2TranscriptModelConfig {
                    sources: Some(vec!["reasoning".to_string()]),
                    max_tool_entry_tokens: Some(500),
                    ..Default::default()
                }),
                ..Default::default()
            }),
        };
        let mut value = serde_json::to_value(ModelsResponse {
            models: vec![test_model(Some(messages.clone()))],
        })
        .expect("serialize models response");
        assert_eq!(
            value["models"][0]["model_messages"]["confirmation_policies"],
            serde_json::json!({
                "browser_use": "# Browser confirmations\n\nKeep {{literal_markdown}}.\n",
                "computer_use": "  # Native confirmations\r\n\nKeep ${native_markdown}.\n",
            })
        );
        value["models"][0]["base_instructions"] = serde_json::json!("legacy instructions");

        let response: ModelsResponse =
            serde_json::from_value(value).expect("deserialize legacy models response");
        let mut expected_messages = messages;
        expected_messages.instructions_template = Some("legacy instructions".to_string());
        assert_eq!(response.models[0].model_messages, Some(expected_messages));

        let canonical_messages = ModelMessages {
            persistent_instructions: Some(String::new()),
            tools: Some(ToolMessages {
                send_user_message_async: Some(ToolMessage {
                    description: Some(String::new()),
                }),
            }),
            instructions_template: Some("canonical instructions".to_string()),
            instructions_variables: None,
            approvals: None,
            collaboration_modes: None,
            auto_review: None,
            permissions: None,
            multi_agent: None,
            token_budget: None,
            confirmation_policies: None,
            guardian_v2: None,
        };
        let mut value = serde_json::to_value(ModelsResponse {
            models: vec![test_model(Some(canonical_messages.clone()))],
        })
        .expect("serialize models response");
        value["models"][0]["base_instructions"] = serde_json::json!("legacy instructions");

        let response: ModelsResponse =
            serde_json::from_value(value).expect("deserialize mixed models response");
        assert_eq!(response.models[0].model_messages, Some(canonical_messages));
    }

    #[test]
    fn model_info_defaults_availability_nux_to_none_when_omitted() {
        let model: ModelInfo = serde_json::from_value(serde_json::json!({
            "slug": "test-model",
            "display_name": "Test Model",
            "description": null,
            "supported_reasoning_levels": [],
            "shell_type": "unified_exec",
            "visibility": "list",
            "supported_in_api": true,
            "priority": 1,
            "upgrade": null,
            "model_messages": null,
            "default_reasoning_summary": "auto",
            "support_verbosity": false,
            "default_verbosity": null,
            "apply_patch_tool_type": null,
            "truncation_policy": {
                "mode": "bytes",
                "limit": 10000
            },
            "supports_image_detail_original": false,
            "context_window": null,
            "auto_compact_token_limit": null,
            "effective_context_window_percent": 95,
            "experimental_supported_tools": []
        }))
        .expect("deserialize model info");

        assert_eq!(model.availability_nux, None);
        assert_eq!(
            model.input_modalities,
            vec![InputModality::Text, InputModality::Image]
        );
        assert!(!model.include_skills_usage_instructions);
        assert!(!model.include_plugin_usage_instructions);
        assert!(model.include_apps_usage_instructions);
        assert!(model.supports_reasoning_summary_parameter);
        assert!(!model.supports_image_detail_original);
        assert_eq!(model.web_search_tool_type, WebSearchToolType::Text);
        assert!(!model.supports_search_tool);
        assert!(!model.supports_experimental_context);
        assert!(!model.use_responses_lite);
        assert!(!model.node_repl_auto_review_required);
        assert!(!model.node_repl_disabled);
        assert_eq!(model.comp_hash, None);
        assert_eq!(model.auto_review_model_override, None);
        assert_eq!(model.tool_mode, None);
        assert_eq!(model.multi_agent_reasoning_effort, None);
    }

    #[test]
    fn model_info_deserializes_multi_agent_reasoning_effort() {
        let mut value =
            serde_json::to_value(test_model(/*spec*/ None)).expect("serialize test model");
        value
            .as_object_mut()
            .expect("model info should be an object")
            .insert(
                "multi_agent_reasoning_effort".to_string(),
                serde_json::Value::String("high".to_string()),
            );

        let model = serde_json::from_value::<ModelInfo>(value)
            .expect("deserialize multi-agent reasoning effort");

        assert_eq!(
            model.multi_agent_reasoning_effort,
            Some(ReasoningEffort::High)
        );
    }

    #[test]
    fn model_info_deserializes_optional_upgrade_retirement_at() {
        let base = serde_json::to_value(test_model(/*spec*/ None))
            .expect("serialize test model without retirement time");

        let mut absent = base.clone();
        absent
            .as_object_mut()
            .expect("model info should be an object")
            .insert(
                "upgrade".to_string(),
                serde_json::json!({
                    "model": "replacement-model",
                    "migration_markdown": "Use the replacement model."
                }),
            );
        let absent = serde_json::from_value::<ModelInfo>(absent)
            .expect("deserialize model info without upgrade retirement time");
        assert_eq!(
            absent
                .upgrade
                .as_ref()
                .and_then(|upgrade| upgrade.retirement_at.as_ref())
                .map(DateTime::timestamp),
            None
        );

        let mut null = base.clone();
        null.as_object_mut()
            .expect("model info should be an object")
            .insert(
                "upgrade".to_string(),
                serde_json::json!({
                    "model": "replacement-model",
                    "migration_markdown": "Use the replacement model.",
                    "retirement_at": null
                }),
            );
        let null = serde_json::from_value::<ModelInfo>(null)
            .expect("deserialize model info with null upgrade retirement time");
        assert_eq!(
            null.upgrade
                .as_ref()
                .and_then(|upgrade| upgrade.retirement_at.as_ref())
                .map(DateTime::timestamp),
            None
        );

        let mut populated = base;
        populated
            .as_object_mut()
            .expect("model info should be an object")
            .insert(
                "upgrade".to_string(),
                serde_json::json!({
                    "model": "replacement-model",
                    "migration_markdown": "Use the replacement model.",
                    "retirement_at": "2030-01-01T00:00:00Z"
                }),
            );
        let populated = serde_json::from_value::<ModelInfo>(populated)
            .expect("deserialize model info with upgrade retirement time");
        assert_eq!(
            populated
                .upgrade
                .as_ref()
                .and_then(|upgrade| upgrade.retirement_at.as_ref())
                .map(DateTime::timestamp),
            Some(1_893_456_000)
        );

        let mut malformed = serde_json::to_value(test_model(/*spec*/ None))
            .expect("serialize test model for malformed retirement time");
        malformed
            .as_object_mut()
            .expect("model info should be an object")
            .insert(
                "upgrade".to_string(),
                serde_json::json!({
                    "model": "replacement-model",
                    "migration_markdown": "Use the replacement model.",
                    "retirement_at": "not-a-timestamp"
                }),
            );
        let malformed = serde_json::from_value::<ModelInfo>(malformed)
            .expect("tolerate malformed upgrade retirement time");
        assert_eq!(
            malformed
                .upgrade
                .as_ref()
                .and_then(|upgrade| upgrade.retirement_at.as_ref())
                .map(DateTime::timestamp),
            None
        );
    }

    #[test]
    fn model_info_preserves_explicit_apps_guidance_opt_out() {
        let value = serde_json::to_value(test_model(/*spec*/ None))
            .expect("serialize model info with explicit apps guidance opt-out");
        assert_eq!(value["include_apps_usage_instructions"], false);

        let model = serde_json::from_value::<ModelInfo>(value).expect("deserialize model info");
        assert!(!model.include_apps_usage_instructions);
    }

    #[test]
    fn model_info_deserializes_known_tool_mode() {
        let mut value =
            serde_json::to_value(test_model(/*spec*/ None)).expect("serialize test model");
        let object = value
            .as_object_mut()
            .expect("model info should be an object");
        object.insert(
            "tool_mode".to_string(),
            serde_json::Value::String("code_mode_only".to_string()),
        );
        let model = serde_json::from_value::<ModelInfo>(value).expect("deserialize model info");

        assert_eq!(model.tool_mode, Some(ToolMode::CodeModeOnly));
    }

    #[test]
    fn model_info_treats_unknown_tool_mode_as_omitted() {
        let mut value =
            serde_json::to_value(test_model(/*spec*/ None)).expect("serialize test model");
        let object = value
            .as_object_mut()
            .expect("model info should be an object");
        object.insert(
            "tool_mode".to_string(),
            serde_json::Value::String("future_tool_mode".to_string()),
        );
        let model = serde_json::from_value::<ModelInfo>(value).expect("deserialize model info");

        assert_eq!(model.tool_mode, None);
        let serialized = serde_json::to_value(model).expect("serialize model info");
        let object = serialized
            .as_object()
            .expect("model info should be an object");
        assert!(!object.contains_key("tool_mode"));
    }

    #[test]
    fn model_info_treats_unknown_multi_agent_version_as_omitted() {
        let mut value =
            serde_json::to_value(test_model(/*spec*/ None)).expect("serialize test model");
        let object = value
            .as_object_mut()
            .expect("model info should be an object");
        object.insert(
            "multi_agent_version".to_string(),
            serde_json::Value::String("future_multi_agent_version".to_string()),
        );
        let model = serde_json::from_value::<ModelInfo>(value).expect("deserialize model info");

        assert_eq!(model.multi_agent_version, None);
    }

    #[test]
    fn resolved_context_window_prefers_context_window() {
        let model = ModelInfo {
            context_window: Some(273_000),
            max_context_window: Some(400_000),
            ..test_model(/*spec*/ None)
        };

        assert_eq!(model.resolved_context_window(), Some(273_000));
    }

    #[test]
    fn resolved_context_window_falls_back_to_max_context_window() {
        let model = ModelInfo {
            context_window: None,
            max_context_window: Some(400_000),
            ..test_model(/*spec*/ None)
        };

        assert_eq!(model.resolved_context_window(), Some(400_000));
        assert_eq!(model.usable_context_window(), Some(380_000));
        assert_eq!(model.auto_compact_token_limit(), Some(360_000));
    }

    #[test]
    fn model_context_window_limits_preserve_their_distinct_meanings() {
        let model = ModelInfo {
            context_window: Some(272_000),
            max_context_window: Some(400_000),
            auto_compact_token_limit: Some(250_000),
            effective_context_window_percent: 95,
            ..test_model(/*spec*/ None)
        };

        assert_eq!(
            (
                model.resolved_context_window(),
                model.usable_context_window(),
                model.auto_compact_token_limit(),
            ),
            (Some(272_000), Some(258_400), Some(244_800))
        );
    }

    #[test]
    fn model_preset_preserves_availability_nux() {
        let preset = ModelPreset::from(ModelInfo {
            availability_nux: Some(ModelAvailabilityNux {
                message: "Try Spark.".to_string(),
            }),
            additional_speed_tiers: vec![SPEED_TIER_FAST.to_string()],
            default_service_tier: Some(ServiceTier::Fast.request_value().to_string()),
            service_tiers: Vec::new(),
            ..test_model(/*spec*/ None)
        });

        assert_eq!(
            preset.availability_nux,
            Some(ModelAvailabilityNux {
                message: "Try Spark.".to_string(),
            })
        );
        assert!(preset.supports_fast_mode());
        assert_eq!(
            preset.default_service_tier,
            Some(ServiceTier::Fast.request_value().to_string())
        );
    }

    #[test]
    fn model_access_programs_preserve_explicit_selection_availability() {
        use crate::turn_input::CyberAccessProgram;

        for (metadata, expected) in [
            (None, None),
            (Some(serde_json::Value::Null), None),
            (
                Some(serde_json::json!({ "cyber": [] })),
                Some(ModelAccessPrograms { cyber: vec![] }),
            ),
            (
                Some(serde_json::json!({
                    "cyber": ["standard", "daybreak_blue", "future_program", "daybreak_red"]
                })),
                Some(ModelAccessPrograms {
                    cyber: vec![
                        CyberAccessProgram::Standard,
                        CyberAccessProgram::DaybreakBlue,
                        CyberAccessProgram::DaybreakRed,
                    ],
                }),
            ),
        ] {
            let mut value = serde_json::to_value(test_model(/*spec*/ None)).unwrap();
            if let Some(metadata) = metadata {
                value["available_access_programs"] = metadata;
            }
            let model: ModelInfo = serde_json::from_value(value).unwrap();
            assert_eq!(model.available_access_programs, expected);
            assert_eq!(ModelPreset::from(model).available_access_programs, expected);
        }
    }

    #[test]
    fn model_preset_supports_fast_mode_from_service_tiers() {
        let preset = ModelPreset::from(ModelInfo {
            service_tiers: vec![ModelServiceTier {
                id: ServiceTier::Fast.request_value().to_string(),
                name: "Fast".to_string(),
                description: "Priority processing.".to_string(),
            }],
            ..test_model(/*spec*/ None)
        });

        assert!(preset.supports_fast_mode());
    }

    #[test]
    fn service_tier_for_request_omits_explicit_default_tier() {
        let model = ModelInfo {
            default_service_tier: Some(ServiceTier::Fast.request_value().to_string()),
            service_tiers: vec![ModelServiceTier {
                id: ServiceTier::Fast.request_value().to_string(),
                name: "Fast".to_string(),
                description: "Priority processing.".to_string(),
            }],
            ..test_model(/*spec*/ None)
        };

        assert_eq!(
            model.service_tier_for_request(Some(SERVICE_TIER_DEFAULT_REQUEST_VALUE.to_string())),
            None
        );
    }

    #[test]
    fn service_tier_for_request_filters_unsupported_tiers() {
        let model = ModelInfo {
            default_service_tier: Some(ServiceTier::Fast.request_value().to_string()),
            service_tiers: vec![ModelServiceTier {
                id: ServiceTier::Fast.request_value().to_string(),
                name: "Fast".to_string(),
                description: "Priority processing.".to_string(),
            }],
            ..test_model(/*spec*/ None)
        };

        assert_eq!(
            model.service_tier_for_request(Some(ServiceTier::Fast.request_value().to_string())),
            Some(ServiceTier::Fast.request_value().to_string())
        );
        assert_eq!(
            model.service_tier_for_request(Some("unsupported".to_string())),
            None
        );
        assert_eq!(model.service_tier_for_request(/*service_tier*/ None), None);
    }
}