visdom-cli 1.2.0

Visdom developer CLI: checks a project's Visdom wiring and launches the coding agent
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
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
//! Whether a Claude Code session paid by the developer's own Claude
//! subscription (`billing = subscription`) can work through this deployment's
//! AI Gateway, checked before it is started and at the end of `visdom init`:
//!
//! 1. the gateway takes subscription traffic (`gateway.subscription`),
//! 2. Claude Code is signed in with a claude.ai subscription
//!    (`gateway.claude_login`),
//! 3. the model alias is one the gateway takes a subscription call on
//!    (`gateway.subscription_model`),
//! 4. a subscription key is issued (or kept) and the gateway accepts it
//!    (`gateway.subscription_key`),
//! 5. nothing in Claude Code's settings takes the session over from that
//!    login (`gateway.claude_settings`).

use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

use serde::Deserialize;

use crate::finding::{Finding, Severity};
use crate::gateway::{OpenProject, SubscriptionTraffic};

const COMPONENT: &str = "gateway";

/// What the developer is told when the platform does not take subscription
/// traffic: printed word for word (the product owner's wording).
pub const NOT_SUPPORTED: &str = "Your platform configuration doesn't support subscription traffic \
                                 through visdom gateway. Contact your platform administrator or \
                                 use Claude API key or agent hooks";

/// The commands behind the two ways out [`NOT_SUPPORTED`] names.
pub const ALTERNATIVES: &str = "Claude API key (the project pays): visdom --billing project init; \
                                agent hooks: visdom --mode hooks init";

/// Whether `why` is [`NOT_SUPPORTED`], which is then printed on its own.
pub fn is_not_supported(why: &str) -> bool {
    why.starts_with(NOT_SUPPORTED)
}

/// `gateway.subscription`: whether the gateway takes subscription traffic.
/// Denied, or not said (a gateway from before the setting), is
/// [`NOT_SUPPORTED`] on the first line.
pub fn traffic_finding(traffic: SubscriptionTraffic) -> Finding {
    const ID: &str = "gateway.subscription";
    let why = match traffic {
        SubscriptionTraffic::Allowed => {
            return Finding::new(
                COMPONENT,
                ID,
                Severity::Ok,
                "the AI Gateway takes calls paid by a Claude subscription",
            )
        }
        SubscriptionTraffic::Denied => "the AI Gateway is set to deny subscription traffic",
        SubscriptionTraffic::Unpublished => {
            "the AI Gateway does not say that it takes subscription traffic (a version from \
             before it could)"
        }
    };
    Finding::new(
        COMPONENT,
        ID,
        Severity::Error,
        format!("{NOT_SUPPORTED}\n      ({why}) {ALTERNATIVES}"),
    )
}

/// How Claude Code is signed in, as `claude auth status` says.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ClaudeLogin {
    /// A claude.ai login, with its plan when Claude Code names it.
    Subscription {
        plan: Option<String>,
    },
    SignedOut,
    /// Signed in another way (an Anthropic Console key, a cloud provider).
    Other {
        method: String,
    },
    /// Could not be told; the text says why.
    Unknown(String),
}

/// Read what `claude auth status` printed. A claude.ai login stored beside
/// a key that takes precedence elsewhere (a key helper in the developer's
/// settings, which `visdom` sets aside) still counts as the login.
pub fn read_auth_status(stdout: &str) -> ClaudeLogin {
    #[derive(Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct Status {
        #[serde(default)]
        logged_in: bool,
        #[serde(default)]
        auth_method: Option<String>,
        #[serde(default)]
        subscription_type: Option<String>,
        #[serde(default)]
        stored_claude_ai_login: bool,
    }
    let Ok(status) = serde_json::from_str::<Status>(stdout.trim()) else {
        return ClaudeLogin::Unknown(
            "`claude auth status` did not answer in JSON (a Claude Code too old for it?)"
                .to_string(),
        );
    };
    let method = status.auth_method.unwrap_or_default();
    let plan = status
        .subscription_type
        .clone()
        .filter(|p| !p.trim().is_empty());
    // A claude.ai account without a paid plan has no subscription to pay
    // with. (A plan Claude Code does not name is taken as one: it names it
    // for every claude.ai login it can use.)
    if status.logged_in
        && method == "claude.ai"
        && plan
            .as_deref()
            .is_some_and(|p| p.eq_ignore_ascii_case("free"))
    {
        return ClaudeLogin::Other {
            method: "a claude.ai account without a paid plan".to_string(),
        };
    }
    // `oauth_token`: a subscription token from `claude setup-token` in
    // `CLAUDE_CODE_OAUTH_TOKEN` (Claude Code 2.1.332, tried by hand), which
    // the launch leaves alone.
    let subscription_method = method == "claude.ai" || method == "oauth_token";
    if status.logged_in && (subscription_method || status.stored_claude_ai_login) {
        return ClaudeLogin::Subscription {
            plan: status.subscription_type.filter(|p| !p.trim().is_empty()),
        };
    }
    if !status.logged_in || method.is_empty() || method == "none" {
        return ClaudeLogin::SignedOut;
    }
    ClaudeLogin::Other { method }
}

/// How long `claude auth status` may take.
const AUTH_STATUS_TIMEOUT: Duration = Duration::from_secs(10);

/// Ask Claude Code (`program`) how it is signed in, in the environment a
/// session `visdom` starts would have: without the keys and provider
/// switches the launch removes, which would otherwise be reported instead of
/// the login.
pub fn claude_login(program: &str, agent_args: &[String], private_dir: &Path) -> ClaudeLogin {
    let (settings, callers) = probe_settings(agent_args);
    let text = serde_json::Value::Object(settings).to_string();
    // The caller's own settings may hold a token (`CLAUDE_CODE_OAUTH_TOKEN`
    // in their `env`): in a file only the user can read, as the launch does,
    // not on a command line.
    let (value, file) = if callers {
        match crate::agents::via_gateway::write_private_file(private_dir, &text) {
            Ok(file) => (file.to_string_lossy().into_owned(), Some(file)),
            Err(why) => return ClaudeLogin::Unknown(why),
        }
    } else {
        (text, None)
    };
    let login = ask_auth_status(program, &value);
    if let Some(file) = file {
        let _ = std::fs::remove_file(file);
    }
    login
}

/// The settings `auth status` is asked with, and whether they include the
/// caller's own: the caller's `--settings`, merged as the launch merges
/// them, under what the launch sets aside — the developer's key helper
/// (which Claude Code 2.1.332 would report as `authMethod: api_key_helper`,
/// tried by hand, instead of the login the session uses), keys and provider
/// switches.
pub fn probe_settings(agent_args: &[String]) -> (serde_json::Map<String, serde_json::Value>, bool) {
    let (given, _) = crate::agents::via_gateway::split_settings(agent_args);
    let mut settings = serde_json::Map::new();
    for value in &given {
        if let Ok(read) = crate::agents::via_gateway::read_settings(value) {
            settings.extend(read);
        }
    }
    let mut env = match settings.remove("env") {
        Some(serde_json::Value::Object(env)) => env,
        _ => serde_json::Map::new(),
    };
    for name in ["ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN"] {
        env.insert(name.to_string(), "".into());
    }
    for switch in crate::agents::via_gateway::CLAUDE_OTHER_PROVIDERS {
        env.insert(switch.to_string(), "0".into());
    }
    settings.insert("env".to_string(), env.into());
    settings.insert("apiKeyHelper".to_string(), "".into());
    (settings, !given.is_empty())
}

/// `program --settings <settings> auth status`, read.
fn ask_auth_status(program: &str, settings: &str) -> ClaudeLogin {
    let mut cmd = Command::new(program);
    cmd.arg("--settings")
        .arg(settings)
        .args(["auth", "status"])
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null());
    for name in ["ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN"]
        .into_iter()
        .chain(crate::agents::via_gateway::CLAUDE_OTHER_PROVIDERS)
    {
        cmd.env_remove(name);
    }
    let mut child = match cmd.spawn() {
        Ok(child) => child,
        Err(e) => {
            return ClaudeLogin::Unknown(format!("`{program} auth status` could not be run: {e}"))
        }
    };
    let started = Instant::now();
    loop {
        match child.try_wait() {
            Ok(Some(_)) => break,
            Ok(None) if started.elapsed() < AUTH_STATUS_TIMEOUT => {
                std::thread::sleep(Duration::from_millis(25))
            }
            _ => {
                let _ = child.kill();
                let _ = child.wait();
                return ClaudeLogin::Unknown(format!(
                    "`{program} auth status` did not answer within {} seconds",
                    AUTH_STATUS_TIMEOUT.as_secs()
                ));
            }
        }
    }
    let mut out = String::new();
    if let Some(mut stdout) = child.stdout.take() {
        use std::io::Read;
        let _ = stdout.read_to_string(&mut out);
    }
    // Signed out, it exits 1 and still prints its JSON.
    read_auth_status(&out)
}

/// `gateway.claude_login`: whether Claude Code has the claude.ai login the
/// subscription is paid through.
pub fn login_finding(login: &ClaudeLogin) -> Finding {
    const ID: &str = "gateway.claude_login";
    const SIGN_IN: &str = "start `claude` and run /login with your claude.ai account";
    match login {
        ClaudeLogin::Subscription { plan } => Finding::new(
            COMPONENT,
            ID,
            Severity::Ok,
            match plan {
                Some(plan) => {
                    format!("Claude Code is signed in with a claude.ai subscription ({plan})")
                }
                None => "Claude Code is signed in with a claude.ai subscription".to_string(),
            },
        ),
        ClaudeLogin::SignedOut => Finding::new(
            COMPONENT,
            ID,
            Severity::Error,
            format!(
                "Claude Code is not signed in, so there is no subscription to pay for its calls: \
                 {SIGN_IN}"
            ),
        ),
        ClaudeLogin::Other { method } => Finding::new(
            COMPONENT,
            ID,
            Severity::Error,
            format!(
                "Claude Code is signed in with {}, not a claude.ai subscription: {SIGN_IN}",
                match method.as_str() {
                    "api_key" => "an API key (Anthropic Console)".to_string(),
                    said if said.starts_with("a ") => said.to_string(),
                    other => format!("`{other}`"),
                }
            ),
        ),
        // Nothing shows a subscription to pay with: not a setup to accept.
        ClaudeLogin::Unknown(why) => Finding::new(
            COMPONENT,
            ID,
            Severity::Error,
            format!(
                "whether Claude Code is signed in with a claude.ai subscription could not be \
                 checked: {why}. Make sure Claude Code is installed and recent (`claude \
                 update`), then {SIGN_IN}"
            ),
        ),
    }
}

/// A model alias Claude Code will ask for, and who set it (`None`: the
/// alias `visdom` configured or picked).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ModelUse {
    pub model: Option<String>,
    pub source: Option<String>,
}

/// The `env` of every Claude Code settings layer that applies over the
/// environment a session is started in, the one that wins first: the
/// settings an organisation manages, the caller's own `--settings` (the
/// later one first), the project's `.claude/settings.local.json` and
/// `.claude/settings.json`, the user's `settings.json`. `visdom`'s launch
/// sets the model as environment, so any of these that names one wins.
pub struct ClaudeEnv {
    layers: Vec<(String, serde_json::Map<String, serde_json::Value>)>,
}

impl ClaudeEnv {
    pub fn read(
        managed: &Path,
        home: &Path,
        project_dir: Option<&Path>,
        agent_args: &[String],
    ) -> ClaudeEnv {
        let mut files: Vec<(String, Option<serde_json::Map<String, serde_json::Value>>)> =
            vec![(managed.display().to_string(), read_json(managed))];
        // Merged as the launch merges them: a later one wins key by key, so
        // its `env` replaces an earlier `env` as a whole.
        let (given, _) = crate::agents::via_gateway::split_settings(agent_args);
        if !given.is_empty() {
            let mut merged = serde_json::Map::new();
            for value in &given {
                if let Ok(settings) = crate::agents::via_gateway::read_settings(value) {
                    merged.extend(settings);
                }
            }
            files.push(("--settings given to Claude Code".to_string(), Some(merged)));
        }
        if let Some(dir) = project_dir {
            for name in [".claude/settings.local.json", ".claude/settings.json"] {
                let path = dir.join(name);
                files.push((path.display().to_string(), read_json(&path)));
            }
        }
        let user = claude_config_dir(home).join("settings.json");
        files.push((user.display().to_string(), read_json(&user)));
        let layers = files
            .into_iter()
            .filter_map(|(at, settings)| {
                let env = settings?.get("env")?.as_object()?.clone();
                Some((at, env))
            })
            .collect();
        ClaudeEnv { layers }
    }

    /// The value of `name` a session would see, and where it comes from:
    /// a settings layer, else `process` (the environment).
    pub fn get(
        &self,
        name: &str,
        process: &dyn Fn(&str) -> Option<String>,
    ) -> Option<(String, String)> {
        self.layers
            .iter()
            .find_map(|(at, env)| {
                env.get(name)
                    .and_then(|v| v.as_str())
                    .map(|v| (v.to_string(), format!("`env` in {at}")))
            })
            .or_else(|| process(name).map(|v| (v, IN_ENVIRONMENT.to_string())))
    }
}

/// The aliases a session started now would ask for: the main model (the
/// caller's `--model`, else `ANTHROPIC_MODEL` as the session sees it — a
/// settings layer, else the environment, which the launch keeps — else
/// `configured`) and the small one (`ANTHROPIC_DEFAULT_HAIKU_MODEL`, else
/// `configured`). `env` gives a variable's value and where it is set.
pub fn models_in_use(
    configured: Option<&str>,
    agent_args: &[String],
    env: &dyn Fn(&str) -> Option<(String, String)>,
) -> Vec<ModelUse> {
    // A blank variable in the environment is replaced by visdom's alias at
    // launch (as if unset). A blank one in a settings layer comes after the
    // launch's environment and leaves Claude Code its own default, which no
    // gateway alias is: a use with no model, reported where it is set.
    let named = |name: &str| {
        env(name).and_then(|(value, at)| {
            let source = Some(format!("{name} in {at}"));
            if !value.trim().is_empty() {
                Some(ModelUse {
                    model: Some(value),
                    source,
                })
            } else if at != IN_ENVIRONMENT {
                Some(ModelUse {
                    model: None,
                    source,
                })
            } else {
                None
            }
        })
    };
    let option = |name: &str| {
        let eq = format!("{name}=");
        agent_args
            .iter()
            .take_while(|a| *a != "--")
            .enumerate()
            .find_map(|(i, arg)| {
                if arg == name {
                    agent_args.get(i + 1).cloned()
                } else {
                    arg.strip_prefix(&eq).map(String::from)
                }
            })
            .filter(|v| !v.trim().is_empty())
    };
    let ours = || ModelUse {
        model: configured.map(String::from),
        source: None,
    };
    let main = match option("--model") {
        Some(model) => ModelUse {
            model: Some(model),
            source: Some("--model given to Claude Code".to_string()),
        },
        None => named("ANTHROPIC_MODEL").unwrap_or_else(ours),
    };
    let small = named("ANTHROPIC_DEFAULT_HAIKU_MODEL").unwrap_or_else(ours);
    // Used when the main model is overloaded; passed on as given.
    let fallback = option("--fallback-model").map(|model| ModelUse {
        model: Some(model),
        source: Some("--fallback-model given to Claude Code".to_string()),
    });
    let mut uses = Vec::new();
    // Background calls, the model aliases and subagents may each name
    // another model; the launch keeps whatever names one.
    let others = OTHER_MODEL_SETTINGS.iter().filter_map(|name| named(name));
    let all = [main, small]
        .into_iter()
        .chain(fallback)
        .chain(others)
        .flat_map(|used| resolve_selector(used, &named));
    for used in all {
        if !uses.contains(&used) {
            uses.push(used);
        }
    }
    uses
}

/// Where [`ClaudeEnv::get`] says the process environment is.
const IN_ENVIRONMENT: &str = "the environment";

/// A Claude Code model selector (`sonnet`, `opus`, `haiku`, with or without
/// `[1m]`, and `opusplan`, which uses Opus and Sonnet) stands for the model
/// its `ANTHROPIC_DEFAULT_*_MODEL` setting names; unset, for Claude Code's
/// own model of that family, which no gateway alias is. Anything else is
/// the model itself.
fn resolve_selector(used: ModelUse, named: &dyn Fn(&str) -> Option<ModelUse>) -> Vec<ModelUse> {
    let Some(model) = used.model.as_deref() else {
        return vec![used];
    };
    let bare = model
        .trim()
        .trim_end_matches("[1m]")
        .trim_end_matches("[1M]")
        .to_ascii_lowercase();
    let families: &[&str] = match bare.as_str() {
        "sonnet" => &["SONNET"],
        "opus" => &["OPUS"],
        "haiku" => &["HAIKU"],
        "opusplan" => &["OPUS", "SONNET"],
        _ => return vec![used],
    };
    let given = used
        .source
        .clone()
        .unwrap_or_else(|| "visdom's model setting".to_string());
    families
        .iter()
        .map(|family| {
            let setting = format!("ANTHROPIC_DEFAULT_{family}_MODEL");
            match named(&setting) {
                Some(target) => ModelUse {
                    source: Some(format!(
                        "{}, which `{model}` ({given}) stands for",
                        target.source.unwrap_or_else(|| setting.clone())
                    )),
                    model: target.model,
                },
                // Claude Code's own model: no alias of the gateway.
                None => ModelUse {
                    model: Some(model.to_string()),
                    source: Some(format!(
                        "{given}; `{model}` is Claude Code's own model unless {setting} names \
                         an alias"
                    )),
                },
            }
        })
        .collect()
}

/// Claude Code's other settings that name a model a session may ask for,
/// beside `ANTHROPIC_MODEL` and `ANTHROPIC_DEFAULT_HAIKU_MODEL`. The list is
/// Claude Code's: a setting it gains has to be added here.
pub const OTHER_MODEL_SETTINGS: [&str; 4] = [
    "ANTHROPIC_SMALL_FAST_MODEL",
    "ANTHROPIC_DEFAULT_SONNET_MODEL",
    "ANTHROPIC_DEFAULT_OPUS_MODEL",
    "CLAUDE_CODE_SUBAGENT_MODEL",
];

/// `gateway.subscription_model`: whether every alias of `uses` is one the
/// gateway takes a subscription call on, in `project`. `source` is where
/// `visdom`'s own alias was set; `config_key` is the setting that names it.
/// A gateway that does not publish which aliases qualify cannot be relied on
/// to keep a subscription call off another provider: an error too.
pub fn model_finding(
    uses: &[ModelUse],
    source: Option<&str>,
    project: &OpenProject,
    config_key: &str,
) -> Finding {
    const ID: &str = "gateway.subscription_model";
    let Some(eligible) = project.subscription_aliases() else {
        return Finding::new(
            COMPONENT,
            ID,
            Severity::Error,
            format!(
                "the AI Gateway does not say which model aliases of the project `{}` take calls \
                 paid by a Claude subscription (a version from before it could), so none can be \
                 relied on: ask your platform administrator to update it, or use --billing \
                 project",
                project.slug
            ),
        );
    };
    for used in uses {
        let finding = one_model_finding(
            used.model.as_deref(),
            used.source.as_deref().or(source),
            used.source.is_some(),
            &eligible,
            project,
            config_key,
        );
        if finding.severity != Severity::Ok {
            return finding;
        }
    }
    let names: Vec<String> = uses
        .iter()
        .filter_map(|u| u.model.as_deref())
        .map(|m| format!("`{m}`"))
        .collect();
    Finding::new(
        COMPONENT,
        ID,
        Severity::Ok,
        format!(
            "the model alias {} takes calls paid by a Claude subscription",
            names.join(" and ")
        ),
    )
}

/// [`model_finding`] for one alias. `outside` says it was set outside
/// visdom's config (the environment, the agent's options).
fn one_model_finding(
    model: Option<&str>,
    source: Option<&str>,
    outside: bool,
    eligible: &[String],
    project: &OpenProject,
    config_key: &str,
) -> Finding {
    const ID: &str = "gateway.subscription_model";
    let set = match (outside, source) {
        (true, Some(source)) => format!("change {source}"),
        _ => format!(
            "set {config_key} under [gateway] in your visdom config, or VISDOM_GATEWAY_MODEL"
        ),
    };
    if eligible.is_empty() {
        return Finding::new(
            COMPONENT,
            ID,
            Severity::Error,
            format!(
                "no model alias of the project `{}` takes calls paid by a Claude subscription \
                 (only aliases served by Anthropic directly do): ask a project operator for one, \
                 or use --billing project",
                project.slug
            ),
        );
    }
    let names = eligible.join(", ");
    match model {
        Some(model) if eligible.iter().any(|alias| alias == model) => Finding::new(
            COMPONENT,
            ID,
            Severity::Ok,
            format!("the model alias `{model}` takes calls paid by a Claude subscription"),
        ),
        Some(model) => Finding::new(
            COMPONENT,
            ID,
            Severity::Error,
            format!(
                "the model alias `{model}`{} does not take calls paid by a Claude subscription \
                 (only aliases served by Anthropic directly do; the gateway refuses it): {set} to \
                 one that does — {names}",
                source.map(|s| format!(" ({s})")).unwrap_or_default()
            ),
        ),
        None => Finding::new(
            COMPONENT,
            ID,
            Severity::Error,
            format!(
                "no model alias is set, and Claude Code's own default is not one the gateway \
                 takes a subscription call on: {set} to one that does — {names}"
            ),
        ),
    }
}

/// The aliases of `available` the gateway takes a subscription call on in
/// `project`; none when it does not say.
pub fn eligible_of(project: &OpenProject, available: &[String]) -> Vec<String> {
    let eligible = project.subscription_aliases().unwrap_or_default();
    available
        .iter()
        .filter(|alias| eligible.contains(alias))
        .cloned()
        .collect()
}

/// The day (UTC) Unix seconds `at` fall on.
fn day(at: i64) -> String {
    chrono::DateTime::from_timestamp(at, 0)
        .map(|t| t.format("%Y-%m-%d").to_string())
        .unwrap_or_else(|| at.to_string())
}

/// The state of the subscription key, as far as it was checked.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum KeyState {
    /// The gateway took it (`GET /v1/models`); it expires at these Unix
    /// seconds.
    Accepted { expires_at: i64 },
    /// None is kept yet, and none was issued here (`visdom doctor`).
    NoneKept,
    /// The kept one was refused; `visdom` issues another when it starts.
    KeptRefused,
    /// None could be had, or the gateway refused it; the text says why.
    Failed(String),
}

/// `gateway.subscription_key`, with the expiry date.
pub fn key_finding(state: &KeyState) -> Finding {
    const ID: &str = "gateway.subscription_key";
    match state {
        KeyState::Accepted { expires_at } => Finding::new(
            COMPONENT,
            ID,
            Severity::Ok,
            format!(
                "your subscription key is accepted by the AI Gateway; it expires on {} and \
                 `visdom` renews it a week before",
                day(*expires_at)
            ),
        ),
        KeyState::NoneKept => Finding::new(
            COMPONENT,
            ID,
            Severity::Info,
            "no subscription key is kept yet: `visdom` gets one when it starts Claude Code",
        ),
        KeyState::KeptRefused => Finding::new(
            COMPONENT,
            ID,
            Severity::Warn,
            "the AI Gateway no longer accepts your kept subscription key: `visdom` gets a new \
             one when it starts Claude Code",
        ),
        KeyState::Failed(why) => Finding::new(COMPONENT, ID, Severity::Error, why.clone()),
    }
}

/// Where Claude Code reads the settings an organisation manages, which come
/// after everything `visdom` passes. `VISDOM_CLAUDE_MANAGED_SETTINGS` names
/// another file (an unusual installation, and the tests).
pub fn managed_settings_path() -> PathBuf {
    if let Some(path) = std::env::var_os("VISDOM_CLAUDE_MANAGED_SETTINGS") {
        return PathBuf::from(path);
    }
    if cfg!(target_os = "macos") {
        PathBuf::from("/Library/Application Support/ClaudeCode/managed-settings.json")
    } else if cfg!(windows) {
        PathBuf::from(r"C:\Program Files\ClaudeCode\managed-settings.json")
    } else {
        PathBuf::from("/etc/claude-code/managed-settings.json")
    }
}

/// Claude Code's own settings directory: `CLAUDE_CONFIG_DIR`, else
/// `~/.claude`.
fn claude_config_dir(home: &Path) -> PathBuf {
    std::env::var_os("CLAUDE_CONFIG_DIR")
        .filter(|v| !v.is_empty())
        .map(PathBuf::from)
        .unwrap_or_else(|| home.join(".claude"))
}

fn read_json(path: &Path) -> Option<serde_json::Map<String, serde_json::Value>> {
    let text = std::fs::read_to_string(path).ok()?;
    match serde_json::from_str(&text) {
        Ok(serde_json::Value::Object(map)) => Some(map),
        _ => None,
    }
}

fn is_set(value: Option<&serde_json::Value>) -> bool {
    value
        .and_then(|v| v.as_str())
        .is_some_and(|v| !v.trim().is_empty())
}

/// What in the managed settings `settings` takes a subscription session
/// over: they come after `visdom`'s own, so it cannot set them aside.
pub fn managed_takeovers(settings: &serde_json::Map<String, serde_json::Value>) -> Vec<String> {
    let mut found = Vec::new();
    if is_set(settings.get("apiKeyHelper")) {
        found.push("`apiKeyHelper` (its key is sent instead of your subscription)".to_string());
    }
    if let Some(method) = settings.get("forceLoginMethod").and_then(|v| v.as_str()) {
        if method != "claudeai" {
            found.push(format!(
                "`forceLoginMethod = {method}` (a claude.ai login is not allowed)"
            ));
        }
    }
    let env = settings.get("env").and_then(|v| v.as_object());
    // An empty key is no key; an empty address or header list still
    // replaces visdom's (no address is Anthropic's own; no headers, no key).
    for (name, what, even_empty) in [
        (
            "ANTHROPIC_API_KEY",
            "its key is sent instead of your subscription",
            false,
        ),
        (
            "ANTHROPIC_AUTH_TOKEN",
            "its token is sent instead of your subscription",
            false,
        ),
        (
            "ANTHROPIC_BASE_URL",
            "the session is sent past the gateway",
            true,
        ),
        (
            "ANTHROPIC_CUSTOM_HEADERS",
            "they replace the headers that carry your subscription key",
            true,
        ),
    ] {
        let value = env.and_then(|env| env.get(name));
        if is_set(value) || (even_empty && value.is_some()) {
            found.push(format!("`{name}` in `env` ({what})"));
        }
    }
    // A switch to another provider's API outranks the gateway's address;
    // visdom turns them off ("0"), but managed settings come after it.
    for switch in crate::agents::via_gateway::CLAUDE_OTHER_PROVIDERS {
        let on = env
            .and_then(|env| env.get(switch))
            .map(|v| match v {
                serde_json::Value::String(s) => {
                    !matches!(s.trim().to_ascii_lowercase().as_str(), "" | "0" | "false")
                }
                serde_json::Value::Bool(b) => *b,
                serde_json::Value::Number(n) => n.as_f64() != Some(0.0),
                _ => false,
            })
            .unwrap_or(false);
        if on {
            found.push(format!(
                "`{switch}` in `env` (the session is sent to another provider, past the gateway)"
            ));
        }
    }
    found
}

/// `gateway.claude_settings`: nothing in Claude Code's settings takes the
/// session over from the claude.ai login. Settings an organisation manages
/// that would are an error; a key helper in the developer's own settings is
/// set aside in sessions `visdom` starts, but not in a Claude Code started
/// without it (`source <(visdom env)` cannot), so it is a warning.
/// `project_dir` is the checkout whose `.claude/` settings apply.
pub fn settings_finding(managed: &Path, home: &Path, project_dir: Option<&Path>) -> Finding {
    const ID: &str = "gateway.claude_settings";
    if let Some(settings) = read_json(managed) {
        let found = managed_takeovers(&settings);
        if !found.is_empty() {
            return Finding::new(
                COMPONENT,
                ID,
                Severity::Error,
                format!(
                    "the Claude Code settings your organisation manages ({}) set {}: they come \
                     after visdom's, so a session on your subscription cannot work. Ask whoever \
                     manages them, or use --billing project",
                    managed.display(),
                    found.join(", ")
                ),
            );
        }
    }
    let mut own = vec![claude_config_dir(home).join("settings.json")];
    if let Some(dir) = project_dir {
        own.push(dir.join(".claude/settings.json"));
        own.push(dir.join(".claude/settings.local.json"));
    }
    let helpers: Vec<String> = own
        .iter()
        .filter(|path| read_json(path).is_some_and(|s| is_set(s.get("apiKeyHelper"))))
        .map(|path| path.display().to_string())
        .collect();
    if !helpers.is_empty() {
        return Finding::new(
            COMPONENT,
            ID,
            Severity::Warn,
            format!(
                "`apiKeyHelper` in {}: set aside in sessions `visdom` starts, but a Claude Code \
                 started without it (after `source <(visdom env)`) sends that key instead of \
                 your subscription",
                helpers.join(", ")
            ),
        );
    }
    Finding::new(
        COMPONENT,
        ID,
        Severity::Ok,
        "nothing in Claude Code's settings takes the session over from your claude.ai login",
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::gateway::ProjectModel;

    #[test]
    fn the_platforms_message_is_the_product_owners_word_for_word() {
        assert_eq!(
            NOT_SUPPORTED,
            "Your platform configuration doesn't support subscription traffic through visdom \
             gateway. Contact your platform administrator or use Claude API key or agent hooks"
        );
        for traffic in [
            SubscriptionTraffic::Denied,
            SubscriptionTraffic::Unpublished,
        ] {
            let finding = traffic_finding(traffic);
            assert_eq!(finding.severity, Severity::Error);
            assert_eq!(finding.message.lines().next(), Some(NOT_SUPPORTED));
            assert!(finding.message.contains("--billing project"), "{finding:?}");
            assert!(finding.message.contains("--mode hooks"), "{finding:?}");
            assert!(is_not_supported(&finding.message));
        }
        assert_eq!(
            traffic_finding(SubscriptionTraffic::Allowed).severity,
            Severity::Ok
        );
    }

    #[test]
    fn auth_status_tells_a_subscription_from_anything_else() {
        let sub = r#"{"loggedIn":true,"authMethod":"claude.ai","subscriptionType":"team"}"#;
        assert_eq!(
            read_auth_status(sub),
            ClaudeLogin::Subscription {
                plan: Some("team".to_string())
            }
        );
        // A claude.ai account without a paid plan has nothing to pay with.
        let free = r#"{"loggedIn":true,"authMethod":"claude.ai","subscriptionType":"free"}"#;
        let login = read_auth_status(free);
        assert!(matches!(login, ClaudeLogin::Other { .. }), "{login:?}");
        let finding = login_finding(&login);
        assert_eq!(finding.severity, Severity::Error);
        assert!(
            finding.message.contains("without a paid plan"),
            "{finding:?}"
        );
        // A key helper outranks the stored login, which visdom keeps.
        let helper = r#"{"loggedIn":true,"authMethod":"api_key","storedClaudeAiLogin":true}"#;
        assert!(matches!(
            read_auth_status(helper),
            ClaudeLogin::Subscription { .. }
        ));
        let console = r#"{"loggedIn":true,"authMethod":"api_key","apiKeySource":"/login"}"#;
        assert_eq!(
            read_auth_status(console),
            ClaudeLogin::Other {
                method: "api_key".to_string()
            }
        );
        let out = r#"{"loggedIn":false,"authMethod":"none"}"#;
        assert_eq!(read_auth_status(out), ClaudeLogin::SignedOut);
        assert!(matches!(
            read_auth_status("Usage: claude [options]"),
            ClaudeLogin::Unknown(_)
        ));
    }

    #[test]
    fn a_login_that_is_not_a_subscription_names_the_fix() {
        let finding = login_finding(&ClaudeLogin::SignedOut);
        assert_eq!(finding.severity, Severity::Error);
        assert!(finding.message.contains("/login"), "{finding:?}");
        let finding = login_finding(&ClaudeLogin::Other {
            method: "api_key".to_string(),
        });
        assert_eq!(finding.severity, Severity::Error);
        assert!(finding.message.contains("Anthropic Console"), "{finding:?}");
        assert!(finding.message.contains("/login"), "{finding:?}");
        // Nothing shows a subscription: not accepted either.
        let finding = login_finding(&ClaudeLogin::Unknown("x".to_string()));
        assert_eq!(finding.severity, Severity::Error);
        assert!(finding.message.contains("claude update"), "{finding:?}");
    }

    fn project(models: Option<&[(&str, bool)]>) -> OpenProject {
        OpenProject {
            id: "p-1".to_string(),
            slug: "alpha".to_string(),
            name: "Alpha".to_string(),
            models: models.map(|models| {
                models
                    .iter()
                    .map(|(alias, subscription)| ProjectModel {
                        alias: alias.to_string(),
                        subscription: *subscription,
                    })
                    .collect()
            }),
        }
    }

    #[test]
    fn the_alias_must_take_subscription_calls_and_the_ones_that_do_are_named() {
        let alpha = project(Some(&[
            ("gpt-general", false),
            ("claude-sonnet", true),
            ("claude-opus", true),
        ]));
        let ours = |model: Option<&str>| {
            vec![ModelUse {
                model: model.map(String::from),
                source: None,
            }]
        };
        let ok = model_finding(
            &ours(Some("claude-sonnet")),
            None,
            &alpha,
            "claude_code_model",
        );
        assert_eq!(
            (ok.id, ok.severity),
            ("gateway.subscription_model", Severity::Ok)
        );
        let bad = model_finding(
            &ours(Some("gpt-general")),
            Some("VISDOM_GATEWAY_MODEL in the environment"),
            &alpha,
            "claude_code_model",
        );
        assert_eq!(bad.severity, Severity::Error);
        assert!(
            bad.message.contains("claude-sonnet, claude-opus"),
            "{bad:?}"
        );
        assert!(bad
            .message
            .contains("VISDOM_GATEWAY_MODEL in the environment"));
        assert!(bad.message.contains("claude_code_model"), "{bad:?}");
        let none = model_finding(&ours(None), None, &alpha, "claude_code_model");
        assert_eq!(none.severity, Severity::Error);
        assert!(none.message.contains("claude-sonnet"), "{none:?}");

        let nothing = model_finding(
            &ours(Some("gpt-general")),
            None,
            &project(Some(&[("gpt-general", false)])),
            "claude_code_model",
        );
        assert_eq!(nothing.severity, Severity::Error);
        assert!(nothing.message.contains("--billing project"), "{nothing:?}");

        // Without the list nothing can be relied on.
        let silent = model_finding(&ours(Some("x")), None, &project(None), "claude_code_model");
        assert_eq!(silent.severity, Severity::Error);
        assert!(silent.message.contains("--billing project"), "{silent:?}");

        // The small model set in the environment is checked too, and its
        // source is the one to change.
        let env = |name: &str| {
            (name == "ANTHROPIC_DEFAULT_HAIKU_MODEL")
                .then(|| ("gpt-general".to_string(), "the environment".to_string()))
        };
        let uses = models_in_use(Some("claude-sonnet"), &[], &env);
        assert_eq!(uses.len(), 2, "{uses:?}");
        let small = model_finding(&uses, None, &alpha, "claude_code_model");
        assert_eq!(small.severity, Severity::Error);
        assert!(
            small
                .message
                .contains("change ANTHROPIC_DEFAULT_HAIKU_MODEL in the environment"),
            "{small:?}"
        );
    }

    #[test]
    fn the_models_in_use_are_the_agents_options_then_the_environment_then_visdoms() {
        let none = |_: &str| None;
        assert_eq!(
            models_in_use(Some("claude-sonnet"), &[], &none),
            [ModelUse {
                model: Some("claude-sonnet".to_string()),
                source: None
            }]
        );
        let env = |name: &str| {
            (name == "ANTHROPIC_MODEL")
                .then(|| ("claude-opus".to_string(), "the environment".to_string()))
        };
        let uses = models_in_use(Some("claude-sonnet"), &[], &env);
        assert_eq!(uses[0].model.as_deref(), Some("claude-opus"));
        assert_eq!(uses[1].model.as_deref(), Some("claude-sonnet"));
        let args = ["--model=gpt-general".to_string(), "-p".to_string()];
        let uses = models_in_use(Some("claude-sonnet"), &args, &env);
        assert_eq!(uses[0].model.as_deref(), Some("gpt-general"));
        assert_eq!(
            uses[0].source.as_deref(),
            Some("--model given to Claude Code")
        );
        let prompt = ["--".to_string(), "--model".to_string(), "x".to_string()];
        assert_eq!(
            models_in_use(Some("claude-sonnet"), &prompt, &none)[0]
                .model
                .as_deref(),
            Some("claude-sonnet"),
            "after `--` it is the prompt's"
        );
    }

    fn uses_with(
        args: &[&str],
        vars: &'static [(&'static str, &'static str, &'static str)],
    ) -> Vec<ModelUse> {
        let args: Vec<String> = args.iter().map(|a| a.to_string()).collect();
        let env = move |name: &str| {
            vars.iter()
                .find(|(n, ..)| *n == name)
                .map(|(_, v, at)| (v.to_string(), at.to_string()))
        };
        models_in_use(Some("claude-sonnet"), &args, &env)
    }

    #[test]
    fn claude_codes_selectors_stand_for_their_default_model_settings() {
        // `sonnet` is what ANTHROPIC_DEFAULT_SONNET_MODEL names.
        let uses = uses_with(
            &["--model", "sonnet"],
            &[(
                "ANTHROPIC_DEFAULT_SONNET_MODEL",
                "claude-sonnet",
                "the environment",
            )],
        );
        assert!(
            uses.iter()
                .all(|u| u.model.as_deref() == Some("claude-sonnet")),
            "{uses:?}"
        );
        let source = uses[0].source.as_deref().unwrap();
        assert!(
            source.contains("ANTHROPIC_DEFAULT_SONNET_MODEL"),
            "{source}"
        );
        assert!(source.contains("`sonnet`"), "{source}");
        // Unset, it is Claude Code's own model, named as such.
        let uses = uses_with(&["--model=opus[1m]"], &[]);
        assert_eq!(uses[0].model.as_deref(), Some("opus[1m]"));
        assert!(uses[0]
            .source
            .as_deref()
            .unwrap()
            .contains("ANTHROPIC_DEFAULT_OPUS_MODEL"));
        // `opusplan` uses both.
        let uses = uses_with(
            &[],
            &[
                ("ANTHROPIC_MODEL", "opusplan", "the environment"),
                (
                    "ANTHROPIC_DEFAULT_OPUS_MODEL",
                    "claude-opus",
                    "the environment",
                ),
                (
                    "ANTHROPIC_DEFAULT_SONNET_MODEL",
                    "claude-sonnet",
                    "the environment",
                ),
            ],
        );
        let models: Vec<_> = uses.iter().filter_map(|u| u.model.as_deref()).collect();
        assert!(models.contains(&"claude-opus") && models.contains(&"claude-sonnet"));
        assert!(!models.contains(&"opusplan"), "{models:?}");
    }

    #[test]
    fn the_fallback_model_is_checked_too() {
        let uses = uses_with(&["-p", "hi", "--fallback-model", "gpt-general"], &[]);
        let fallback = uses
            .iter()
            .find(|u| u.model.as_deref() == Some("gpt-general"))
            .unwrap();
        assert_eq!(
            fallback.source.as_deref(),
            Some("--fallback-model given to Claude Code")
        );
        let alpha = project(Some(&[("claude-sonnet", true), ("gpt-general", false)]));
        let finding = model_finding(&uses, None, &alpha, "claude_code_model");
        assert_eq!(finding.severity, Severity::Error);
        assert!(finding.message.contains("--fallback-model"), "{finding:?}");
    }

    #[test]
    fn a_blank_model_in_a_settings_layer_is_claude_codes_own_default() {
        // In the environment the launch replaces it with the alias.
        let uses = uses_with(&[], &[("ANTHROPIC_MODEL", " ", "the environment")]);
        assert_eq!(uses[0].model.as_deref(), Some("claude-sonnet"));
        // In a settings file it stays blank: reported where it is set.
        let uses = uses_with(
            &[],
            &[(
                "ANTHROPIC_MODEL",
                "",
                "`env` in /home/u/.claude/settings.json",
            )],
        );
        assert_eq!(uses[0].model, None);
        let alpha = project(Some(&[("claude-sonnet", true)]));
        let finding = model_finding(&uses, None, &alpha, "claude_code_model");
        assert_eq!(finding.severity, Severity::Error);
        assert!(
            finding
                .message
                .contains("change ANTHROPIC_MODEL in `env` in /home/u/.claude/settings.json"),
            "{finding:?}"
        );
    }

    #[test]
    fn the_login_probe_keeps_the_callers_settings_under_the_launchs() {
        let args = [
            "--settings".to_string(),
            r#"{"env":{"CLAUDE_CODE_OAUTH_TOKEN":"sk-ant-oat01-x","ANTHROPIC_API_KEY":"sk"},"apiKeyHelper":"mine"}"#
                .to_string(),
        ];
        let (settings, callers) = probe_settings(&args);
        assert!(callers);
        let env = settings["env"].as_object().unwrap();
        assert_eq!(env["CLAUDE_CODE_OAUTH_TOKEN"], "sk-ant-oat01-x");
        assert_eq!(env["ANTHROPIC_API_KEY"], "");
        assert_eq!(env["CLAUDE_CODE_USE_BEDROCK"], "0");
        assert_eq!(settings["apiKeyHelper"], "");
        let (_, callers) = probe_settings(&[]);
        assert!(!callers);
        // A `CLAUDE_CODE_OAUTH_TOKEN` login is a subscription login.
        let token = r#"{"loggedIn":true,"authMethod":"oauth_token","apiProvider":"firstParty"}"#;
        assert_eq!(
            read_auth_status(token),
            ClaudeLogin::Subscription { plan: None }
        );
    }

    #[test]
    fn a_model_in_claude_codes_settings_wins_over_the_environment() {
        let dir = tempfile::TempDir::new().unwrap();
        let home = dir.path().join("home");
        let repo = dir.path().join("repo");
        let managed = dir.path().join("managed-settings.json");
        std::fs::create_dir_all(home.join(".claude")).unwrap();
        std::fs::create_dir_all(repo.join(".claude")).unwrap();
        std::fs::write(
            home.join(".claude/settings.json"),
            r#"{"env":{"ANTHROPIC_MODEL":"from-user","ANTHROPIC_DEFAULT_HAIKU_MODEL":"small-user"}}"#,
        )
        .unwrap();
        std::fs::write(
            repo.join(".claude/settings.json"),
            r#"{"env":{"ANTHROPIC_MODEL":"from-project"}}"#,
        )
        .unwrap();
        let process = |_: &str| Some("from-process".to_string());
        let env = ClaudeEnv::read(&managed, &home, Some(&repo), &[]);
        let (model, at) = env.get("ANTHROPIC_MODEL", &process).unwrap();
        assert_eq!(model, "from-project");
        assert!(at.contains(".claude/settings.json"), "{at}");
        assert_eq!(
            env.get("ANTHROPIC_DEFAULT_HAIKU_MODEL", &process)
                .unwrap()
                .0,
            "small-user"
        );
        assert_eq!(
            env.get("OTHER", &process).unwrap(),
            ("from-process".to_string(), "the environment".to_string())
        );
        let args = [
            "--settings".to_string(),
            r#"{"env":{"ANTHROPIC_MODEL":"first"}}"#.to_string(),
            "--settings".to_string(),
            r#"{"env":{"ANTHROPIC_MODEL":"from-flag"}}"#.to_string(),
        ];
        let env = ClaudeEnv::read(&managed, &home, Some(&repo), &args);
        let (model, at) = env.get("ANTHROPIC_MODEL", &process).unwrap();
        assert_eq!(
            (model.as_str(), at.as_str()),
            ("from-flag", "`env` in --settings given to Claude Code")
        );
        // As the launch merges them, a later `env` replaces an earlier one as
        // a whole: the project's model shows through.
        let replaced = [
            "--settings".to_string(),
            r#"{"env":{"ANTHROPIC_MODEL":"first"}}"#.to_string(),
            "--settings".to_string(),
            r#"{"env":{"OTHER":"1"}}"#.to_string(),
        ];
        let env = ClaudeEnv::read(&managed, &home, Some(&repo), &replaced);
        assert_eq!(
            env.get("ANTHROPIC_MODEL", &process).unwrap().0,
            "from-project"
        );
        // Other model settings are checked when they name one.
        let named = |name: &str| {
            (name == "CLAUDE_CODE_SUBAGENT_MODEL")
                .then(|| ("gpt-general".to_string(), "the environment".to_string()))
        };
        let uses = models_in_use(Some("claude-sonnet"), &[], &named);
        assert_eq!(uses.len(), 2, "{uses:?}");
        assert_eq!(uses[1].model.as_deref(), Some("gpt-general"));
        std::fs::write(&managed, r#"{"env":{"ANTHROPIC_MODEL":"managed"}}"#).unwrap();
        let env = ClaudeEnv::read(&managed, &home, Some(&repo), &args);
        assert_eq!(env.get("ANTHROPIC_MODEL", &process).unwrap().0, "managed");
    }

    #[test]
    fn only_eligible_aliases_are_offered() {
        let available = ["gpt-general".to_string(), "claude-sonnet".to_string()];
        let alpha = project(Some(&[("claude-sonnet", true), ("gpt-general", false)]));
        assert_eq!(eligible_of(&alpha, &available), ["claude-sonnet"]);
        assert!(eligible_of(&project(None), &available).is_empty());
    }

    #[test]
    fn the_key_is_reported_with_its_expiry_date() {
        let at = chrono::DateTime::parse_from_rfc3339("2026-12-27T10:00:00Z")
            .unwrap()
            .timestamp();
        let finding = key_finding(&KeyState::Accepted { expires_at: at });
        assert_eq!(
            (finding.id, finding.severity),
            ("gateway.subscription_key", Severity::Ok)
        );
        assert!(finding.message.contains("2026-12-27"), "{finding:?}");
        assert_eq!(key_finding(&KeyState::NoneKept).severity, Severity::Info);
        assert_eq!(key_finding(&KeyState::KeptRefused).severity, Severity::Warn);
        let failed = key_finding(&KeyState::Failed(NOT_SUPPORTED.to_string()));
        assert_eq!(failed.severity, Severity::Error);
    }

    #[test]
    fn managed_settings_that_take_the_session_over_are_named() {
        let settings = |text: &str| match serde_json::from_str(text).unwrap() {
            serde_json::Value::Object(map) => map,
            _ => unreachable!(),
        };
        assert!(managed_takeovers(&settings(r#"{"forceLoginMethod":"claudeai"}"#)).is_empty());
        assert!(managed_takeovers(&settings(r#"{"apiKeyHelper":""}"#)).is_empty());
        let found = managed_takeovers(&settings(
            r#"{"apiKeyHelper":"get-key","forceLoginMethod":"console",
                "env":{"ANTHROPIC_API_KEY":"sk","ANTHROPIC_BASE_URL":"https://x","OTHER":"1"}}"#,
        ));
        assert_eq!(found.len(), 4, "{found:?}");
        assert!(found[1].contains("forceLoginMethod = console"), "{found:?}");
        // An empty key is none, but empty headers still replace visdom's.
        let found = managed_takeovers(&settings(
            r#"{"env":{"ANTHROPIC_API_KEY":"","ANTHROPIC_CUSTOM_HEADERS":""}}"#,
        ));
        assert_eq!(found.len(), 1, "{found:?}");
        assert!(found[0].contains("ANTHROPIC_CUSTOM_HEADERS"), "{found:?}");
        // A provider switch that is on; one that is off is fine.
        let found = managed_takeovers(&settings(
            r#"{"env":{"CLAUDE_CODE_USE_BEDROCK":"1","CLAUDE_CODE_USE_VERTEX":"0","CLAUDE_CODE_USE_FOUNDRY":false}}"#,
        ));
        assert_eq!(found.len(), 1, "{found:?}");
        assert!(found[0].contains("CLAUDE_CODE_USE_BEDROCK"), "{found:?}");
    }

    #[test]
    fn settings_are_an_error_when_managed_and_a_warning_when_the_developers() {
        let dir = tempfile::TempDir::new().unwrap();
        let home = dir.path().join("home");
        let repo = dir.path().join("repo");
        let managed = dir.path().join("managed-settings.json");
        std::fs::create_dir_all(home.join(".claude")).unwrap();
        std::fs::create_dir_all(repo.join(".claude")).unwrap();
        let clean = settings_finding(&managed, &home, Some(&repo));
        assert_eq!(
            (clean.id, clean.severity),
            ("gateway.claude_settings", Severity::Ok)
        );
        std::fs::write(
            repo.join(".claude/settings.local.json"),
            r#"{"apiKeyHelper":"echo sk"}"#,
        )
        .unwrap();
        let own = settings_finding(&managed, &home, Some(&repo));
        assert_eq!(own.severity, Severity::Warn);
        assert!(own.message.contains("settings.local.json"), "{own:?}");
        std::fs::write(&managed, r#"{"forceLoginMethod":"console"}"#).unwrap();
        let theirs = settings_finding(&managed, &home, Some(&repo));
        assert_eq!(theirs.severity, Severity::Error);
        assert!(theirs.message.contains("--billing project"), "{theirs:?}");
    }
}