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
use std::io::IsTerminal;
use std::process::exit;

use clap::parser::ValueSource;
use clap::{CommandFactory, FromArgMatches};
use tracevault_cli::agent::Agent;
use tracevault_cli::cli::{run, Command, InitArgs};
use visdom_cli::agents::via_gateway::AgentCommand;
use visdom_cli::agents::McpScope;
use visdom_cli::billing::{self, Billing, BillingUse};
use visdom_cli::cli::{VisdomCli, VisdomCommand};
use visdom_cli::components::CredentialState;
use visdom_cli::doctor::git_toplevel;
use visdom_cli::doctor::{fix, print_attention, Environment, RealExecutor, Session};
use visdom_cli::finding::FixAction;
use visdom_cli::finding::{exit_code, render_json, render_text};
use visdom_cli::fixes::FixExecutor;
use visdom_cli::fixes::{policy_for, FixPolicy};
use visdom_cli::gateway::{ensure_key, key_store_path_in, revoke_kept, KeyStore};
use visdom_cli::instructions::Told;
use visdom_cli::interactive::InitScope;
use visdom_cli::mode::{
    configured_mode, ModeReason, TracingMode, TrafficPolicy, GATEWAY_SESSION_ENV, MODE_ENV,
};
use visdom_cli::subscription;
use visdom_cli::suite::PolicyState;
use visdom_cli::{config, interactive};
use visdom_cli::{doctor as checks, env_alias, instructions, launch};

fn main() {
    // Before the runtime starts its threads: this changes the environment.
    env_alias::apply();
    // What this binary installs for an agent or git to run calls `visdom`.
    tracevault_cli::program::run_as_visdom();
    let matches = VisdomCli::command().get_matches();
    let cli = VisdomCli::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
    // `init --agent` has a default, so only the matches know whether it was typed.
    let init_agent_typed = matches
        .subcommand_matches("init")
        .is_some_and(|init| init.value_source("agent") == Some(ValueSource::CommandLine));
    let runtime = match tokio::runtime::Runtime::new() {
        Ok(runtime) => runtime,
        Err(e) => {
            eprintln!("visdom: cannot start: {e}");
            exit(1);
        }
    };
    match cli.command {
        Some(VisdomCommand::Login(args)) => runtime.block_on(run(Command::Login(args))),
        Some(VisdomCommand::Logout) => {
            // First: a failed logout exits.
            forget_gateway_keys(&runtime);
            runtime.block_on(run(Command::Logout))
        }
        Some(VisdomCommand::Trace { command }) => runtime.block_on(run(command)),
        Some(VisdomCommand::Init { mut args }) => {
            // A repository, the whole machine or a folder of checkouts: said
            // with a flag, else asked. Whichever it is, the rest of the setup
            // (login, bazaar, the tracing mode, the gateway) is the same.
            let scope = init_scope(&args);
            let typed = init_agent_typed.then_some(args.agent).or(cli.agent);
            args.agent = init_agent(typed);
            let agent = args.agent;
            let launched = matches!(agent, Agent::ClaudeCode | Agent::Codex);
            if launched {
                init_login(&runtime, &args, cli.billing);
            }
            // A workspace has no hooks of its own: chosen from the question,
            // the machine-wide ones are installed with it.
            let also_machine =
                (scope.scope == InitScope::Workspace && scope.asked).then(|| machine_wide(&args));
            match scope.scope {
                InitScope::Repo => {}
                InitScope::Machine => args.global = true,
                InitScope::Workspace => init_workspace_args(&mut args),
            }
            // Exits on failure.
            runtime.block_on(run(Command::Init(args)));
            if let Some(machine) = also_machine {
                runtime.block_on(run(Command::Init(machine)));
            }
            if launched {
                init_bazaar(&runtime, agent, cli.billing, scope.scope);
                init_mode(&runtime, agent, cli.mode, cli.billing);
                init_gateway(&runtime, agent, cli.mode, cli.billing, scope.scope);
            }
        }
        Some(VisdomCommand::Doctor {
            fix,
            json,
            yes,
            agent,
            mode,
            billing,
        }) => doctor(
            &runtime,
            agent.or(cli.agent),
            mode.or(cli.mode),
            billing.or(cli.billing),
            fix,
            yes || cli.yes,
            json,
        ),
        Some(VisdomCommand::GatewayKey {
            console,
            project,
            billing,
        }) => gateway_key(&runtime, &console, &project, billing),
        Some(VisdomCommand::Env { agent }) => {
            env_script(&runtime, agent.or(cli.agent), cli.billing)
        }
        Some(VisdomCommand::Instructions { agent, mode }) => {
            let session = gather(
                &Environment::current(),
                agent.or(cli.agent),
                mode.or(cli.mode),
                cli.billing,
                &runtime,
            );
            // Through the gateway when that is the mode and it can be used:
            // what a launch from here would do.
            let through_gateway = matches!(session.gateway, Some(Ok(_)));
            if let Some(text) = instructions::compose(&session.facts(through_gateway)) {
                print!("{text}");
            }
        }
        None => check_and_launch(
            runtime,
            cli.agent,
            cli.mode,
            cli.billing,
            cli.yes,
            !cli.no_instructions,
            &cli.agent_args,
        ),
    }
}

fn now() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0)
}

/// Give the key to the agent that asked for it, which reads this command's
/// output: the key is that output, written as such and to nowhere else. A
/// reader that has gone away is a failure, not a panic.
fn hand_over(secret: &str) -> ! {
    use std::io::Write;
    let mut out = std::io::stdout().lock();
    let written = out
        .write_all(secret.as_bytes())
        .and_then(|()| out.write_all(b"\n"))
        .and_then(|()| out.flush());
    exit(if written.is_ok() { 0 } else { 1 })
}

/// `visdom gateway-key`: the key a launched agent asks for. The kept key is
/// printed without asking anybody; only a missing or expiring one costs a
/// request. Nothing but the key goes to stdout.
fn gateway_key(
    runtime: &tokio::runtime::Runtime,
    console: &str,
    project: &str,
    billing: Billing,
) -> ! {
    let env = Environment::current();
    let store = KeyStore::at(key_store_path_in(&env.config_root));
    let kept = checks::stored_account(&env.cwd)
        .and_then(|account| store.usable(console, project, &account, billing, now()));
    if let Some(key) = kept {
        hand_over(&key.secret);
    }
    let issued = checks::person_token(&env.cwd, runtime).and_then(|token| {
        runtime.block_on(ensure_key(
            &launch::key_http_client(),
            console,
            &token,
            project,
            billing,
            &store,
            now(),
            None,
        ))
    });
    match issued {
        Ok(ensured) => hand_over(&ensured.key.secret),
        Err(e) => {
            eprintln!("visdom: {e}");
            exit(1)
        }
    }
}

/// `visdom logout`: the gateway keys kept for this login go with it, revoked
/// at the console first while the login can still do that. A key that cannot
/// be revoked is said, and forgotten anyway.
fn forget_gateway_keys(runtime: &tokio::runtime::Runtime) {
    let env = Environment::current();
    let store = KeyStore::at(key_store_path_in(&env.config_root));
    // Held from the snapshot to the clearing: a key another `visdom` issues
    // meanwhile would otherwise be forgotten unrevoked. It waits, and keeps
    // its key after this logout.
    let _lock = match store.lock() {
        Ok(lock) => Some(lock),
        Err(e) => {
            eprintln!("visdom: warning: the AI Gateway key store could not be locked: {e}");
            None
        }
    };
    if !store.kept(now()).is_empty() {
        match checks::person_token(&env.cwd, runtime) {
            Ok(token) => {
                let http = launch::key_http_client();
                for warning in runtime.block_on(revoke_kept(&http, &store, &token, now())) {
                    eprintln!("visdom: warning: {warning}");
                }
            }
            Err(why) => eprintln!(
                "visdom: warning: the kept AI Gateway keys could not be revoked ({why}); they \
                 stay valid until they expire (revoke them in the AI Gateway console under My \
                 API key)"
            ),
        }
    }
    if let Err(e) = store.clear() {
        eprintln!("visdom: warning: the kept AI Gateway keys could not be removed: {e}");
    }
}

fn gather(
    env: &Environment,
    agent: Option<Agent>,
    mode: Option<TracingMode>,
    billing: Option<Billing>,
    runtime: &tokio::runtime::Runtime,
) -> Session {
    match Session::gather(env, agent, mode, billing, runtime) {
        Ok(session) => {
            for warning in &session.warnings {
                eprintln!("visdom: warning: {warning}");
            }
            session
        }
        Err(e) => {
            eprintln!("visdom: {e}");
            exit(2);
        }
    }
}

fn stdin_is_tty() -> bool {
    std::io::stdin().is_terminal()
}

fn doctor(
    runtime: &tokio::runtime::Runtime,
    agent: Option<Agent>,
    mode: Option<TracingMode>,
    billing: Option<Billing>,
    fix_flag: bool,
    yes: bool,
    json: bool,
) -> ! {
    let env = Environment::current();
    let mut session = gather(&env, agent, mode, billing, runtime);
    check_subscription(&mut session, runtime, true, &[]);
    let mut findings = session.collect();
    let policy = policy_for(fix_flag, yes, true, stdin_is_tty());
    if policy != FixPolicy::Never && findings.iter().any(|f| f.fix.is_some()) {
        // What is wrong, before asking to change it.
        print_attention(&findings);
        if fix(&findings, policy, &mut RealExecutor::new(json, policy)).applied {
            session = gather(&env, agent, mode, billing, runtime);
            check_subscription(&mut session, runtime, true, &[]);
            findings = session.collect();
        }
    }
    if json {
        println!("{}", render_json(&findings));
    } else {
        print!("{}", render_text(&findings));
    }
    exit(exit_code(&findings))
}

/// Check that a session on the developer's Claude subscription can work here
/// (nothing for any other session). `doctor` also tries the kept key; a
/// launch gets or tries its key when it prepares the gateway. `agent_args`
/// are the options the agent is started with.
fn check_subscription(
    session: &mut Session,
    runtime: &tokio::runtime::Runtime,
    doctor: bool,
    agent_args: &[String],
) {
    if !session.on_subscription() {
        return;
    }
    let model = session.subscription_model();
    let key = doctor.then(|| session.kept_subscription_key(runtime, now()));
    session.check_subscription(model.as_deref(), agent_args, key.as_ref());
}

/// The agent `visdom init` sets up: the one typed, else the one configured
/// (`VISDOM_AGENT`, repository config, user config), else — with somebody to
/// ask — the one they pick, else Claude Code.
fn init_agent(typed: Option<Agent>) -> Agent {
    if let Some(agent) = typed {
        return agent;
    }
    let env = Environment::current();
    let user = config::load_user_config_in(&env.config_root).unwrap_or_default();
    let repo = checks::settings_dir(&env.cwd)
        .and_then(|dir| config::load_repo_config_in(&dir).ok())
        .unwrap_or_default();
    match config::configured_agent(env.agent.as_deref(), &repo, &user) {
        Ok(Some(agent)) => agent,
        Ok(None) if interactive::is_interactive() => interactive::ask_agent(),
        Ok(None) => Agent::ClaudeCode,
        Err(e) => {
            eprintln!("visdom: {e}");
            exit(2);
        }
    }
}

/// What `init` is to set up, and whether somebody was asked.
struct ScopeChoice {
    scope: InitScope,
    asked: bool,
}

/// The scope of this `init`: `--global` or `--workspace` when given, the
/// repository when an option only a repository has is given or there is
/// nobody to ask, else the answer to the question — whose default is the
/// repository inside a checkout and the machine outside one.
fn init_scope(args: &InitArgs) -> ScopeChoice {
    let said = |scope| ScopeChoice {
        scope,
        asked: false,
    };
    if args.global {
        return said(InitScope::Machine);
    }
    if args.workspace {
        return said(InitScope::Workspace);
    }
    let repo_only = args.claude_settings.is_some() || args.no_gitignore;
    if repo_only || !interactive::is_interactive() {
        return said(InitScope::Repo);
    }
    let default = match git_toplevel(&Environment::current().cwd) {
        Some(_) => InitScope::Repo,
        None => InitScope::Machine,
    };
    match interactive::ask_scope(default) {
        Some(scope) => ScopeChoice { scope, asked: true },
        None => exit(1),
    }
}

/// `args` as a `--workspace` init: the project it records to is asked for
/// when not given. Options a workspace does not take stop here, said as they
/// would be on the command line.
fn init_workspace_args(args: &mut InitArgs) {
    let refused = [
        ("--claude-settings", args.claude_settings.is_some()),
        ("--no-gitignore", args.no_gitignore),
        ("--no-user-context", args.no_user_context),
        ("--user-context", args.user_context.is_some()),
        ("--claude-telemetry", args.claude_telemetry),
        ("--no-claude-telemetry", args.no_claude_telemetry),
    ];
    if let Some((option, _)) = refused.iter().find(|(_, given)| *given) {
        eprintln!("visdom: {option} cannot be used for a workspace");
        exit(2);
    }
    if args.project.is_none() {
        args.project = interactive::ask_text("Project the workspace's sessions are recorded to:");
    }
    if args.project.is_none() {
        eprintln!("visdom: a workspace needs a project: pass --workspace --project <name>");
        exit(2);
    }
    args.workspace = true;
}

/// `args` as the `--global` init that goes with a workspace.
fn machine_wide(args: &InitArgs) -> InitArgs {
    let mut machine = args.clone();
    machine.global = true;
    machine.workspace = false;
    machine.project = None;
    // Of a repository only.
    machine.server_url = None;
    machine
}

/// Before `init`, with somebody to ask: when not logged in, ask for the
/// tracing server (unless `--server-url` or an earlier setup names it) and log
/// in, instead of failing with "log in first". Without a terminal nothing
/// happens here and `init` reports what is missing.
fn init_login(runtime: &tokio::runtime::Runtime, args: &InitArgs, billing: Option<Billing>) {
    if !interactive::is_interactive() {
        return;
    }
    let env = Environment::current();
    let session = gather(&env, Some(args.agent), None, billing, runtime);
    if session.ctx.credential != CredentialState::Missing {
        return;
    }
    let known = args.server_url.clone().or(session.ctx.server_url);
    let Some(server_url) = known.or_else(|| {
        eprintln!("You are not logged in to Visdom.");
        interactive::ask_url("Tracing server URL:")
    }) else {
        // Nothing given: `init` says what is needed.
        return;
    };
    let login = FixAction::Login { server_url };
    if let Err(e) = RealExecutor::new(false, FixPolicy::Ask).execute(&login) {
        eprintln!("visdom: {e}");
        exit(1);
    }
}

/// The second half of `visdom init`: set up bazaar for the agent. Asking for
/// `init` is asking for this, so the registration is shown and applied, not
/// offered. What init cannot do (an unknown bazaar URL, an entry it will not
/// replace) is reported; a registration that fails makes init fail.
fn init_bazaar(
    runtime: &tokio::runtime::Runtime,
    agent: Agent,
    billing: Option<Billing>,
    scope: InitScope,
) {
    let env = Environment::current();
    // For a repository the registration is that project directory's; a
    // machine or a folder of checkouts has sessions in many directories.
    let mcp_scope = match scope {
        InitScope::Repo => McpScope::Local,
        InitScope::Machine | InitScope::Workspace => McpScope::User,
    };
    let mut session = gather(&env, Some(agent), None, billing, runtime);
    session.mcp_scope = mcp_scope;
    // Nobody knows the bazaar URL: ask, and remember the answer for next time.
    if matches!(session.bazaar_url, Ok(None)) && interactive::is_interactive() {
        if let Some(url) = interactive::ask_url("Bazaar MCP URL (Enter to skip):") {
            match interactive::save_bazaar_url(&session.user_config_path, &url) {
                Ok(()) => eprintln!("Saved to {}", session.user_config_path.display()),
                Err(e) => eprintln!("visdom: warning: bazaar URL not saved: {e}"),
            }
            session.bazaar_url = Ok(Some(url));
        }
    }
    let findings = session.collect();
    let bazaar: Vec<_> = findings
        .iter()
        .filter(|f| f.component == "bazaar")
        .cloned()
        .collect();
    let policy = FixPolicy::All;
    let summary = fix(&bazaar, policy, &mut RealExecutor::new(false, policy));
    let findings = if summary.applied {
        let mut after = gather(&env, Some(agent), None, billing, runtime);
        after.mcp_scope = mcp_scope;
        // The URL just typed counts even if it could not be saved.
        if matches!(after.bazaar_url, Ok(None)) {
            after.bazaar_url = session.bazaar_url.clone();
        }
        after.collect()
    } else {
        findings
    };
    print_attention(&findings);
    if summary.failed {
        exit(1);
    }
    if findings
        .iter()
        .any(|f| f.id == "bazaar.mcp" && f.severity == visdom_cli::finding::Severity::Ok)
    {
        println!("Bazaar MCP registered for {}", agent.label());
    }
}

/// The last part of `visdom init`: how the developer's sessions are traced.
/// Asked only when it is theirs to choose — nothing chosen yet, a gateway to
/// choose, and no workspace policy deciding it — and remembered in their
/// config. A policy that decides is said instead.
fn init_mode(
    runtime: &tokio::runtime::Runtime,
    agent: Agent,
    flag: Option<TracingMode>,
    billing: Option<Billing>,
) {
    let env = Environment::current();
    let session = gather(&env, Some(agent), flag, billing, runtime);
    match session.mode.reason {
        ModeReason::Policy(TrafficPolicy::Enforced) => {
            println!(
                "This workspace requires the AI Gateway: `visdom` starts the agent through it."
            );
            return;
        }
        ModeReason::Policy(_) => return,
        ModeReason::Chosen => {
            // `visdom --mode … init` is a choice to keep, not one for this
            // run only: there is no agent to start here.
            if let Some(mode) = flag {
                save_mode(&session, mode);
                // Kept in the user config, which the environment and the
                // repository's config come before: a mode they name is the
                // one the next plain `visdom` runs in, not this one.
                let user = config::load_user_config_in(&env.config_root).unwrap_or_default();
                let repo = checks_repo_config(&env);
                let next = configured_mode(
                    None,
                    env.mode.as_deref(),
                    repo.tracing_mode,
                    user.tracing_mode,
                );
                if !matches!(next, Ok(Some(next)) if next == mode) {
                    let said_by = match env.mode {
                        Some(_) => format!("{MODE_ENV} in the environment"),
                        None => "tracing_mode in the repository's .visdom/config.toml".to_string(),
                    };
                    let other = match next {
                        Ok(Some(next)) => format!("`{}`", next.name()),
                        _ => "something else".to_string(),
                    };
                    eprintln!(
                        "visdom: `{mode}` was saved to your config, but {said_by} says {other} \
                         and comes first: a plain `visdom` here would not start in {mode} mode. \
                         Change it there, or pass --mode {mode} each time.",
                        mode = mode.name()
                    );
                    exit(1);
                }
            }
            return;
        }
        ModeReason::Default => {}
    }
    let user = config::load_user_config_in(&env.config_root).unwrap_or_default();
    let gateway_known = env.gateway_url.is_some()
        || user.gateway.url.is_some()
        || session.suite.ai_gateway_url.is_some();
    let free_choice = matches!(
        session.policy,
        PolicyState::Fresh(TrafficPolicy::PerAgent)
            | PolicyState::Remembered(TrafficPolicy::PerAgent, _)
    );
    if !gateway_known || !free_choice || !interactive::is_interactive() {
        return;
    }
    // Asked again here only to rule out a choice made outside the user config.
    let repo = checks::settings_dir(&env.cwd)
        .and_then(|dir| config::load_repo_config_in(&dir).ok())
        .unwrap_or_default();
    if !matches!(
        configured_mode(
            flag,
            env.mode.as_deref(),
            repo.tracing_mode,
            user.tracing_mode
        ),
        Ok(None)
    ) {
        return;
    }
    let Some(mode) = interactive::ask_mode() else {
        return;
    };
    save_mode(&session, mode);
}

/// The repository's own visdom config for the directory `visdom` runs in.
fn checks_repo_config(env: &Environment) -> config::RepoConfig {
    checks::settings_dir(&env.cwd)
        .and_then(|dir| config::load_repo_config_in(&dir).ok())
        .unwrap_or_default()
}

fn save_mode(session: &Session, mode: TracingMode) {
    match interactive::save_tracing_mode(&session.user_config_path, mode) {
        Ok(()) => eprintln!(
            "Saved tracing_mode = \"{}\" to {}",
            mode.name(),
            session.user_config_path.display()
        ),
        Err(e) => {
            // The next `visdom` would not know the choice: not a setup done.
            eprintln!("visdom: the tracing mode could not be saved: {e}");
            eprintln!("  Set it yourself: export {MODE_ENV}={}", mode.name());
            exit(1);
        }
    }
}

/// Write a gateway setting init has settled to the config at `path`. One
/// that cannot be written ends init: the next `visdom` would not find it,
/// and the setup would only look done.
fn save_gateway(path: &std::path::Path, key: &str, value: &str) {
    match interactive::save_gateway_setting(path, key, value) {
        Ok(()) => eprintln!(
            "Saved {key} = \"{value}\" under [gateway] to {}",
            path.display()
        ),
        Err(e) => {
            eprintln!("visdom: the AI Gateway is not set up: {key} could not be saved: {e}");
            eprintln!(
                "  Set it yourself: {}",
                launch::gateway_setting_env(key).unwrap_or(key)
            );
            exit(1);
        }
    }
}

/// The end of `visdom init` when sessions go through the AI Gateway (chosen
/// just now, earlier, or required by the workspace): leave it ready to use.
/// The project and the model alias are settled — asked for when there is a
/// choice and somebody to ask — and written to the user config, a personal
/// key is issued and tried on the LLM gateway, and what was set up is shown
/// with where each setting comes from. A gateway that cannot be made ready
/// makes init fail, with what to set.
fn init_gateway(
    runtime: &tokio::runtime::Runtime,
    agent: Agent,
    flag: Option<TracingMode>,
    billing_flag: Option<Billing>,
    scope: InitScope,
) {
    let env = Environment::current();
    // The gateway project of a folder of checkouts is that folder's, kept in
    // its own `.visdom/config.toml`; otherwise it is the developer's.
    // (A workspace may be a checkout's top level; the file is there then.)
    let workspace_dir = (scope == InitScope::Workspace)
        .then(|| git_toplevel(&env.cwd).unwrap_or_else(|| env.cwd.clone()));
    // Whether the folder names its project itself already: one inherited
    // from the developer's own config is not the folder's, and would follow
    // every later change of theirs.
    let workspace_names_project = workspace_dir.as_deref().is_some_and(|dir| {
        config::load_repo_config_in(dir)
            .is_ok_and(|folder| folder.gateway.project.is_some_and(|p| !p.trim().is_empty()))
    });
    let workspace_file = workspace_dir.as_deref().map(config::repo_config_path_in);
    let for_workspace = workspace_file.is_some();
    let project_file =
        &workspace_file.unwrap_or_else(|| config::user_config_path_in(&env.config_root));
    let mut session = gather(&env, Some(agent), flag, billing_flag, runtime);
    if let BillingUse::Rejected(why) = &session.billing_use {
        // Hooks chosen while the user config says a subscription pays (the
        // way out the platform's message names): who pays means nothing
        // for hooks, so init leaves a setup the next `visdom` starts, paid
        // by the project should the gateway be chosen again.
        let hooks_chosen =
            session.mode.mode == TracingMode::Hooks && session.mode.reason == ModeReason::Chosen;
        let in_user_config = matches!(session.billing.source, billing::BillingSource::User(_));
        if !(hooks_chosen && in_user_config && billing_flag.is_none()) {
            eprintln!("visdom: {why}");
            exit(2);
        }
        eprintln!(
            "billing = subscription is for sessions through the AI Gateway; with hooks tracing \
             the project's setting applies"
        );
        init_billing(&session, Some(Billing::Project), false);
        session = gather(&env, Some(agent), flag, billing_flag, runtime);
    }
    if session.mode.mode != TracingMode::Gateway {
        // A `--billing` given is kept, as in gateway mode: it is what the
        // gateway uses when it is chosen again.
        if billing_flag.is_some() {
            init_billing(&session, billing_flag, false);
        }
        return;
    }
    let asking = interactive::is_interactive();
    // Right after the mode: who pays for Claude Code's calls through it.
    // Asked for Claude Code only; a `--billing` given is kept for any agent
    // (for Codex only `project` gets this far).
    let ask_billing = asking && agent == Agent::ClaudeCode;
    if init_billing(&session, billing_flag, ask_billing) {
        session = gather(&env, Some(agent), flag, billing_flag, runtime);
    }
    // Who pays, as a config further up says it: a folder's new config file
    // would hide it, so it goes into that file together with the project,
    // whichever step writes it first.
    let inherited_billing = (for_workspace
        && session.billing.source == billing::BillingSource::Repo)
        .then_some(session.billing.billing);
    let save_project = |slug: &str| {
        save_gateway(project_file, "project", slug);
        if let Some(inherited) = inherited_billing {
            save_gateway(project_file, "billing", inherited.name());
        }
    };
    // Several projects: the choice is the developer's.
    if asking && session.gateway_sources.project.is_none() {
        let open = match (&session.console_url, &session.token) {
            (Some(console), Some(token)) => runtime
                .block_on(visdom_cli::gateway::open_projects(
                    &checks::http_client(),
                    console,
                    token,
                ))
                .unwrap_or_default(),
            _ => Vec::new(),
        };
        if open.len() > 1 {
            let names: Vec<String> = open
                .iter()
                .map(|p| format!("{} ({})", p.slug, p.name))
                .collect();
            if let Some(i) =
                interactive::ask_choice("Which AI Gateway project is this for?", &names)
            {
                save_project(&open[i].slug);
                session = gather(&env, Some(agent), flag, billing_flag, runtime);
            }
        }
    }
    // Paid by a subscription: first of all, whether the platform takes it.
    // A gateway that says nothing of it (an older one) does not.
    let on_subscription = session.on_subscription();
    if let (true, Some(Ok(plan))) = (on_subscription, &session.gateway) {
        if !plan.subscription_traffic.supported() {
            subscription_not_supported();
        }
    }
    let visdom = std::env::current_exe().unwrap_or_else(|_| "visdom".into());
    let mut prepared = match launch::prepare_gateway(&session, runtime, &visdom, now()) {
        Ok(prepared) => prepared,
        // The console refused the subscription key: the deployment denies it.
        Err(why) if subscription::is_not_supported(&why) => subscription_not_supported(),
        Err(why) => {
            eprintln!("visdom: the AI Gateway is not ready: {why}");
            eprintln!(
                "{}",
                launch::gateway_settings_hint(&session.user_config_path)
            );
            exit(1);
        }
    };
    let Some(Ok(plan)) = session.gateway.clone() else {
        return;
    };
    let plan = &plan;
    let mut sources = session.gateway_sources.clone();
    let in_user_config = |key: &str| {
        Some(format!(
            "{key} under [gateway] in {}",
            session.user_config_path.display()
        ))
    };
    if sources.project.is_none() || (for_workspace && !workspace_names_project) {
        // The only project open today: written down, so that being given
        // access to another one later does not leave the choice open.
        save_project(&plan.project.slug);
        sources.project = Some(format!(
            "project under [gateway] in {}",
            project_file.display()
        ));
        // A folder's new config file comes before one further up, which may
        // be where the gateway mode was chosen: said in the new file too, or
        // the next `visdom` here would start with hooks.
        let chosen_in_files = session.mode.reason == ModeReason::Chosen && flag.is_none();
        if for_workspace
            && chosen_in_files
            && gather(&env, Some(agent), None, billing_flag, runtime)
                .mode
                .mode
                != TracingMode::Gateway
        {
            if let Err(e) = interactive::save_tracing_mode(project_file, TracingMode::Gateway) {
                eprintln!("visdom: the tracing mode could not be saved: {e}");
                exit(1);
            }
            eprintln!(
                "Saved tracing_mode = \"gateway\" to {}",
                project_file.display()
            );
        }
    }
    let model_key = format!("{}_model", config::agent_config_key(agent));
    if sources.model.is_none() {
        // Nobody named a model: the one picked for the agent, else theirs to
        // pick. Written down either way, so it does not change by itself.
        // Paid by a subscription, only the aliases that take it are offered.
        let picked = prepared.launch.model.clone().or_else(|| {
            (asking && !prepared.pickable.is_empty())
                .then(|| {
                    interactive::ask_choice(
                        &format!(
                            "Which model alias should {} ask the AI Gateway for?",
                            agent.label()
                        ),
                        &prepared.pickable,
                    )
                })
                .flatten()
                .map(|i| prepared.pickable[i].clone())
        });
        if let Some(model) = picked {
            save_gateway(&session.user_config_path, &model_key, &model);
            sources.model = in_user_config(&model_key);
            prepared.launch.model = Some(model);
        }
    }
    if on_subscription {
        // The final check: everything the session on the subscription needs,
        // with the key just issued (or kept) and accepted.
        session.gateway_sources = sources.clone();
        session.check_subscription(
            prepared.launch.model.as_deref(),
            &[],
            Some(&subscription::KeyState::Accepted {
                expires_at: prepared.key_expires_at,
            }),
        );
        print_attention(&session.subscription);
        if session.subscription_blocked() {
            eprintln!(
                "visdom: the AI Gateway is not ready for {} on your Claude subscription: correct \
                 what is marked above and run `visdom init` again",
                agent.label()
            );
            exit(1);
        }
    }
    if let Err(why) = launch::usable_model(
        agent,
        prepared.launch.model.as_deref(),
        sources.model.as_deref(),
        &plan.project.slug,
        &prepared.available,
    ) {
        eprintln!("visdom: the AI Gateway is not ready: {why}");
        exit(1);
    }
    print!(
        "{}",
        launch::gateway_summary(
            agent,
            &session.mode,
            plan,
            &sources,
            prepared.launch.model.as_deref(),
            &session.billing,
            &session.user_config_path,
        )
    );
}

/// The platform does not take subscription traffic: said in the product
/// owner's words, with the commands for the two ways out, and init fails.
fn subscription_not_supported() -> ! {
    eprintln!("{}", subscription::NOT_SUPPORTED);
    eprintln!("  {}", subscription::ALTERNATIVES);
    exit(1)
}

/// Who pays at the next plain `visdom` here, as the files say now.
fn next_billing(env: &Environment) -> Result<billing::ResolvedBilling, String> {
    let user = config::load_user_config_in(&env.config_root).unwrap_or_default();
    let repo = checks_repo_config(env);
    billing::configured_billing(
        None,
        env.gateway_billing.as_deref(),
        repo.gateway.billing,
        user.gateway.billing,
        &config::user_config_path_in(&env.config_root)
            .display()
            .to_string(),
    )
}

/// Who pays for Claude Code's calls through the gateway, in `visdom init`:
/// `--billing` is saved to the user config without asking; with nothing
/// chosen anywhere and somebody to ask, the answer is. A saved value that the
/// environment or the repository's config overrules stops init, as for the
/// mode. `true` when something was saved.
fn init_billing(session: &Session, flag: Option<Billing>, asking: bool) -> bool {
    let billing = match flag {
        Some(billing) => billing,
        None if session.billing.source == billing::BillingSource::Default && asking => {
            match interactive::ask_billing() {
                Some(billing) => billing,
                None => return false,
            }
        }
        None => return false,
    };
    save_gateway(&session.user_config_path, "billing", billing.name());
    match next_billing(&Environment::current()) {
        Ok(next) if next.billing == billing => true,
        next => {
            let said_by = match next {
                Ok(next) => next.describe(),
                Err(e) => e,
            };
            eprintln!(
                "visdom: billing = \"{billing}\" was saved to your config, but {said_by} comes \
                 first: a plain `visdom` here would not use it. Change it there, or pass \
                 --billing {billing} each time."
            );
            exit(1);
        }
    }
}

/// `visdom env`: the shell commands for an agent started without `visdom`.
/// Only they go to stdout.
fn env_script(
    runtime: &tokio::runtime::Runtime,
    agent: Option<Agent>,
    billing_flag: Option<Billing>,
) -> ! {
    let env = Environment::current();
    // Asking for these is asking for the gateway, whatever the mode at rest.
    let session = gather(
        &env,
        agent,
        Some(TracingMode::Gateway),
        billing_flag,
        runtime,
    );
    if session.mode.mode != TracingMode::Gateway {
        eprintln!(
            "visdom: the workspace's AI traffic policy keeps sessions off the AI Gateway: \
             there is nothing to set"
        );
        exit(1);
    }
    stop_on_billing(&session);
    let visdom = std::env::current_exe().unwrap_or_else(|_| "visdom".into());
    let script = launch::prepare_gateway(&session, runtime, &visdom, now()).and_then(|prepared| {
        let project = match &session.gateway {
            Some(Ok(plan)) => plan.project.slug.clone(),
            _ => String::new(),
        };
        // Nothing checks the gateway again before an agent started by hand:
        // a setup that would ask for a model the project refuses is not one
        // to hand out.
        launch::usable_model(
            session.ctx.agent,
            prepared.launch.model.as_deref(),
            session.gateway_sources.model.as_deref(),
            &project,
            &prepared.available,
        )?;
        session.adapter.env_script(&prepared.launch, &project)
    });
    match script {
        Ok(script) => {
            print!("{script}");
            exit(0)
        }
        Err(why) => {
            eprintln!("visdom: the AI Gateway is not ready: {why}");
            eprintln!(
                "{}",
                launch::gateway_settings_hint(&session.user_config_path)
            );
            exit(1)
        }
    }
}

/// The agent's arguments with what it is told about Visdom's tools. Where
/// that is left to a configuration file of the agent's, the developer is told
/// which, once.
fn told_args(session: &Session, agent_args: &[String], through_gateway: bool) -> Vec<String> {
    static SAID: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
    match session.agent_args(agent_args, through_gateway) {
        Told::With(args) | Told::Nothing(args) => args,
        Told::LeftTo { file, args } => {
            if !SAID.swap(true, std::sync::atomic::Ordering::Relaxed) {
                eprintln!(
                    "visdom: {} sets {}'s developer instructions, and visdom does not replace \
                     them: the agent is not told about Visdom's tools. `visdom instructions` \
                     prints the text to add there.",
                    file.display(),
                    session.ctx.agent.label()
                );
            }
            args
        }
    }
}

/// The agent's command for this session: through the AI Gateway when that is
/// the mode and the gateway is ready. A gateway that is not ready stops the
/// launch only when the workspace requires it; a developer who merely chose
/// it gets their agent, traced by hooks.
fn agent_command(
    session: &Session,
    runtime: &tokio::runtime::Runtime,
    agent_args: &[String],
) -> AgentCommand {
    let hooks = |session: &Session| -> AgentCommand {
        let mut cmd = session
            .adapter
            .launch(&told_args(session, agent_args, false));
        // Said outright, so nothing left in the environment (this may be an
        // agent's own shell, inside a gateway session) can switch the hooks'
        // capture off for a session that does not use the gateway.
        cmd.env(MODE_ENV, TracingMode::Hooks.name());
        cmd.env_remove(GATEWAY_SESSION_ENV);
        cmd.into()
    };
    if session.mode.mode != TracingMode::Gateway {
        return hooks(session);
    }
    stop_on_billing(session);
    let required = matches!(
        session.mode.reason,
        ModeReason::Policy(TrafficPolicy::Enforced)
    );
    if session.subscription_blocked() {
        // Why was said with the findings above.
        if required {
            eprintln!(
                "visdom: the workspace requires the AI Gateway, which cannot take this session \
                 on your Claude subscription, so the agent is not started"
            );
            exit(1);
        }
        eprintln!(
            "visdom: the AI Gateway cannot take this session on your Claude subscription: \
             starting the agent with hooks tracing instead"
        );
        return hooks(session);
    }
    let visdom = std::env::current_exe().unwrap_or_else(|_| "visdom".into());
    let prepared = launch::prepare_gateway(session, runtime, &visdom, now()).and_then(|prepared| {
        session
            .adapter
            .launch_via_gateway(
                &told_args(session, agent_args, true),
                &prepared.launch,
                &|name| std::env::var(name).ok(),
            )
            .map(|cmd| (cmd, prepared.notes))
    });
    match prepared {
        Ok((cmd, notes)) => {
            for note in notes {
                eprintln!("visdom: warning: {note}");
            }
            cmd
        }
        Err(why) => {
            // A gateway that could not be planned was already reported with
            // the findings; say here only what was not.
            if matches!(session.gateway, Some(Ok(_))) {
                eprintln!("visdom: {why}");
            }
            if required {
                eprintln!(
                    "visdom: the workspace requires the AI Gateway, so the agent is not started"
                );
                exit(1);
            }
            eprintln!("visdom: starting the agent with hooks tracing instead");
            hooks(session)
        }
    }
}

/// Stop before an agent is started through the gateway in a way the billing
/// does not allow: a combination that cannot work (exit 2, like a mode that
/// names none).
fn stop_on_billing(session: &Session) {
    if let BillingUse::Rejected(why) = &session.billing_use {
        eprintln!("visdom: {why}");
        exit(2);
    }
}

/// Plain `visdom`: check, offer the fixes, start the agent. Findings never
/// stop the agent from starting; only a gateway the workspace requires and
/// that cannot be used does.
fn check_and_launch(
    runtime: tokio::runtime::Runtime,
    agent: Option<Agent>,
    mode: Option<TracingMode>,
    billing: Option<Billing>,
    yes: bool,
    instructions: bool,
    agent_args: &[String],
) -> ! {
    let env = Environment::current();
    let mut session = gather(&env, agent, mode, billing, &runtime);
    check_subscription(&mut session, &runtime, false, agent_args);
    let findings = session.collect();
    print_attention(&findings);
    let policy = policy_for(false, yes, false, stdin_is_tty());
    if fix(&findings, policy, &mut RealExecutor::new(false, policy)).applied {
        session = gather(&env, agent, mode, billing, &runtime);
        check_subscription(&mut session, &runtime, false, agent_args);
        // What is still open, once, above the agent.
        print_attention(&session.collect());
    } else if policy == FixPolicy::Never && findings.iter().any(|f| f.fix.is_some()) {
        eprintln!("visdom: run `visdom doctor --fix` (or `visdom --yes`) to apply the fixes");
    }
    // After the fixes: what was just set up is what the agent is told about.
    session.agent_instructions &= instructions;
    // A billing that cannot be used here (a subscription outside the
    // gateway, or for Codex) is a setting to correct, in hooks mode too. Why
    // was said with the findings above.
    if matches!(session.billing_use, BillingUse::Rejected(_)) {
        eprintln!("visdom: the agent is not started: correct the billing (gateway.billing above)");
        exit(2);
    }
    // In the directory visdom was run in, like starting the agent by hand.
    let AgentCommand { cmd, private_file } = agent_command(&session, &runtime, agent_args);
    // The agent takes over this process (or runs as its only child): no
    // runtime threads may outlive it.
    drop(runtime);
    match private_file {
        // Settings holding a key: removed once the agent has exited.
        Some(file) => {
            launch::run_agent_then_remove(cmd, file, session.ctx.agent, &session.user_config_path)
        }
        None => launch::exec_agent(cmd, session.ctx.agent, &session.user_config_path),
    }
}