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::{config, interactive};
use visdom_cli::{env_alias, launch};
fn main() {
env_alias::apply();
let matches = VisdomCli::command().get_matches();
let cli = VisdomCli::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
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) => runtime.block_on(run(Command::Logout)),
Some(VisdomCommand::Trace { command }) => runtime.block_on(run(command)),
Some(VisdomCommand::Init { mut args }) => {
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;
runtime.block_on(run(Command::Init(args)));
if per_repo && matches!(agent, Agent::ClaudeCode | Agent::Codex) {
init_bazaar(&runtime, agent);
}
}
Some(VisdomCommand::Doctor {
fix,
json,
yes,
agent,
}) => doctor(&runtime, agent.or(cli.agent), fix, yes || cli.yes, json),
None => check_and_launch(runtime, cli.agent, cli.yes, &cli.agent_args),
}
}
fn gather(env: &Environment, agent: Option<Agent>, runtime: &tokio::runtime::Runtime) -> Session {
match Session::gather(env, agent, 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>,
fix_flag: bool,
yes: bool,
json: bool,
) -> ! {
let env = Environment::current();
let mut session = gather(&env, agent, 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()) {
print_attention(&findings);
if fix(&findings, policy, &mut RealExecutor::new(json, policy)).applied {
session = gather(&env, agent, runtime);
findings = session.collect();
}
}
if json {
println!("{}", render_json(&findings));
} else {
print!("{}", render_text(&findings));
}
exit(exit_code(&findings))
}
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);
}
}
}
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), 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 {
return;
};
let login = FixAction::Login { server_url };
if let Err(e) = RealExecutor::new(false, FixPolicy::Ask).execute(&login) {
eprintln!("visdom: {e}");
exit(1);
}
}
fn init_bazaar(runtime: &tokio::runtime::Runtime, agent: Agent) {
let env = Environment::current();
let mut session = gather(&env, Some(agent), runtime);
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), runtime);
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());
}
}
fn check_and_launch(
runtime: tokio::runtime::Runtime,
agent: Option<Agent>,
yes: bool,
agent_args: &[String],
) -> ! {
let env = Environment::current();
let mut session = gather(&env, agent, &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, &runtime);
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");
}
drop(runtime);
let cmd = session.adapter.launch(agent_args);
launch::exec_agent(cmd, session.ctx.agent, &session.user_config_path)
}