visdom-cli 1.0.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
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::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, KeyStore};
use visdom_cli::mode::{
    configured_mode, ModeReason, TracingMode, TrafficPolicy, GATEWAY_SESSION_ENV, MODE_ENV,
};
use visdom_cli::suite::PolicyState;
use visdom_cli::{config, interactive};
use visdom_cli::{doctor as checks, env_alias, launch};

fn main() {
    // Before the runtime starts its threads: this changes the environment.
    env_alias::apply();
    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.block_on(run(Command::Logout))
        }
        Some(VisdomCommand::Trace { command }) => runtime.block_on(run(command)),
        Some(VisdomCommand::Init { mut args }) => {
            // `--global` and `--workspace` set up no single repository: they
            // are `tracevault init` as it is.
            let per_repo = !args.global && !args.workspace;
            if per_repo {
                let typed = init_agent_typed.then_some(args.agent).or(cli.agent);
                args.agent = init_agent(typed);
                if matches!(args.agent, Agent::ClaudeCode | Agent::Codex) {
                    init_login(&runtime, &args);
                }
            }
            let agent = args.agent;
            // Exits on failure.
            runtime.block_on(run(Command::Init(args)));
            if per_repo && matches!(agent, Agent::ClaudeCode | Agent::Codex) {
                init_bazaar(&runtime, agent);
                init_mode(&runtime, agent, cli.mode);
            }
        }
        Some(VisdomCommand::Doctor {
            fix,
            json,
            yes,
            agent,
            mode,
        }) => doctor(
            &runtime,
            agent.or(cli.agent),
            mode.or(cli.mode),
            fix,
            yes || cli.yes,
            json,
        ),
        Some(VisdomCommand::GatewayKey { console, project }) => {
            gateway_key(&runtime, &console, &project)
        }
        None => check_and_launch(runtime, cli.agent, cli.mode, cli.yes, &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) -> ! {
    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, 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,
            &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.
fn forget_gateway_keys() {
    let env = Environment::current();
    if let Err(e) = KeyStore::at(key_store_path_in(&env.config_root)).clear() {
        eprintln!("visdom: warning: the kept AI Gateway keys could not be removed: {e}");
    }
}

fn gather(
    env: &Environment,
    agent: Option<Agent>,
    mode: Option<TracingMode>,
    runtime: &tokio::runtime::Runtime,
) -> Session {
    match Session::gather(env, agent, mode, 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>,
    fix_flag: bool,
    yes: bool,
    json: bool,
) -> ! {
    let env = Environment::current();
    let mut session = gather(&env, agent, mode, runtime);
    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, runtime);
            findings = session.collect();
        }
    }
    if json {
        println!("{}", render_json(&findings));
    } else {
        print!("{}", render_text(&findings));
    }
    exit(exit_code(&findings))
}

/// 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 = git_toplevel(&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);
        }
    }
}

/// 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) {
    if !interactive::is_interactive() {
        return;
    }
    let env = Environment::current();
    let session = gather(&env, Some(args.agent), None, 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) {
    let env = Environment::current();
    let mut session = gather(&env, Some(agent), None, runtime);
    // 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, runtime);
        // 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>) {
    let env = Environment::current();
    let session = gather(&env, Some(agent), flag, 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(_) | ModeReason::Chosen => 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 = git_toplevel(&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;
    };
    match interactive::save_tracing_mode(&session.user_config_path, mode) {
        Ok(()) => eprintln!("Saved to {}", session.user_config_path.display()),
        Err(e) => eprintln!("visdom: warning: tracing mode not saved: {e}"),
    }
}

/// 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],
) -> std::process::Command {
    let hooks = |session: &Session| {
        let mut cmd = session.adapter.launch(agent_args);
        // 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
    };
    if session.mode.mode != TracingMode::Gateway {
        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(|(gateway, notes)| {
            session
                .adapter
                .launch_via_gateway(agent_args, &gateway, &|name| std::env::var(name).ok())
                .map(|cmd| (cmd, 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 matches!(
                session.mode.reason,
                ModeReason::Policy(TrafficPolicy::Enforced)
            ) {
                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)
        }
    }
}

/// 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>,
    yes: bool,
    agent_args: &[String],
) -> ! {
    let env = Environment::current();
    let mut session = gather(&env, agent, mode, &runtime);
    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, &runtime);
        // 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");
    }
    // In the directory visdom was run in, like starting the agent by hand.
    let cmd = agent_command(&session, &runtime, agent_args);
    // The agent takes over this process: no runtime threads may outlive it.
    drop(runtime);
    launch::exec_agent(cmd, session.ctx.agent, &session.user_config_path)
}