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::McpScope;
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::instructions::Told;
use visdom_cli::interactive::InitScope;
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, instructions, launch};
fn main() {
env_alias::apply();
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());
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) => {
forget_gateway_keys();
runtime.block_on(run(Command::Logout))
}
Some(VisdomCommand::Trace { command }) => runtime.block_on(run(command)),
Some(VisdomCommand::Init { mut args }) => {
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);
}
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),
}
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, scope.scope);
init_mode(&runtime, agent, cli.mode);
init_gateway(&runtime, agent, cli.mode, scope.scope);
}
}
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)
}
Some(VisdomCommand::Env { agent }) => env_script(&runtime, agent.or(cli.agent)),
Some(VisdomCommand::Instructions { agent, mode }) => {
let session = gather(
&Environment::current(),
agent.or(cli.agent),
mode.or(cli.mode),
&runtime,
);
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.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)
}
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 })
}
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)
}
}
}
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()) {
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))
}
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);
}
}
}
struct ScopeChoice {
scope: InitScope,
asked: bool,
}
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),
}
}
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;
}
fn machine_wide(args: &InitArgs) -> InitArgs {
let mut machine = args.clone();
machine.global = true;
machine.workspace = false;
machine.project = None;
machine.server_url = None;
machine
}
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 {
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, scope: InitScope) {
let env = Environment::current();
let mcp_scope = match scope {
InitScope::Repo => McpScope::Local,
InitScope::Machine | InitScope::Workspace => McpScope::User,
};
let mut session = gather(&env, Some(agent), None, runtime);
session.mcp_scope = mcp_scope;
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);
after.mcp_scope = mcp_scope;
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 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(_) => return,
ModeReason::Chosen => {
if let Some(mode) = flag {
save_mode(&session, mode);
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;
}
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);
}
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) => {
eprintln!("visdom: the tracing mode could not be saved: {e}");
eprintln!(" Set it yourself: export {MODE_ENV}={}", mode.name());
exit(1);
}
}
}
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);
}
}
}
fn init_gateway(
runtime: &tokio::runtime::Runtime,
agent: Agent,
flag: Option<TracingMode>,
scope: InitScope,
) {
let env = Environment::current();
let workspace_dir = (scope == InitScope::Workspace)
.then(|| git_toplevel(&env.cwd).unwrap_or_else(|| env.cwd.clone()));
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, runtime);
if session.mode.mode != TracingMode::Gateway {
return;
}
let asking = interactive::is_interactive();
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_gateway(project_file, "project", &open[i].slug);
session = gather(&env, Some(agent), flag, runtime);
}
}
}
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,
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 else {
return;
};
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) {
save_gateway(project_file, "project", &plan.project.slug);
sources.project = Some(format!(
"project under [gateway] in {}",
project_file.display()
));
let chosen_in_files = session.mode.reason == ModeReason::Chosen && flag.is_none();
if for_workspace
&& chosen_in_files
&& gather(&env, Some(agent), None, 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() {
let picked = prepared.launch.model.clone().or_else(|| {
asking
.then(|| {
interactive::ask_choice(
&format!(
"Which model alias should {} ask the AI Gateway for?",
agent.label()
),
&prepared.available,
)
})
.flatten()
.map(|i| prepared.available[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 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.user_config_path,
)
);
}
fn env_script(runtime: &tokio::runtime::Runtime, agent: Option<Agent>) -> ! {
let env = Environment::current();
let session = gather(&env, agent, Some(TracingMode::Gateway), 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);
}
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(),
};
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)
}
}
}
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
}
}
}
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(&told_args(session, agent_args, false));
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(|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) => {
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)
}
}
}
fn check_and_launch(
runtime: tokio::runtime::Runtime,
agent: Option<Agent>,
mode: Option<TracingMode>,
yes: bool,
instructions: 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);
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");
}
session.agent_instructions &= instructions;
let cmd = agent_command(&session, &runtime, agent_args);
drop(runtime);
launch::exec_agent(cmd, session.ctx.agent, &session.user_config_path)
}