mod cmd;
mod exec;
mod interrupt;
mod keyring;
mod logging;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::time::Instant;
use agent_loadout_core::diagnostics::report::{self, ReportOptions};
use agent_loadout_core::paths::{self, LoadoutHome, SyncRoot};
use agent_loadout_core::store::{HistoryFinding, Store};
use agent_loadout_core::{CoreError, Env, ErrorCode, KnownFolder, RealFs, brand, versions};
use clap::{Args, CommandFactory, FromArgMatches, Parser, Subcommand};
use serde_json::{Value, json};
use cmd::{Context, JSON_SCHEMA, path_json, print_json, sync_root_warning};
#[derive(Debug, Parser)]
#[command(
name = brand::BIN_NAME,
version,
about = "Manage MCP servers, skills and secret references for AI coding clients"
)]
struct Cli {
#[arg(long, global = true, value_name = "PATH")]
home: Option<PathBuf>,
#[arg(long, global = true)]
json: bool,
#[arg(long, global = true)]
yes: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
Doctor(DoctorArgs),
Server(cmd::server::ServerArgs),
Skills(cmd::skills::SkillsArgs),
Agent(cmd::agent::AgentArgs),
Source(cmd::source::SourceArgs),
Catalog(cmd::catalog::CatalogArgs),
Scanner(cmd::scanner::ScannerArgs),
Project(cmd::project::ProjectArgs),
History(cmd::history::HistoryArgs),
Status(cmd::status::StatusArgs),
Diff(cmd::diff::DiffArgs),
Effective(cmd::effective::EffectiveArgs),
Apply(cmd::apply::ApplyArgs),
Rollback(cmd::rollback::RollbackArgs),
Eject(cmd::eject::EjectArgs),
Show(cmd::show::ShowArgs),
Tag(cmd::tag::TagArgs),
Restore(cmd::restore::RestoreArgs),
Backup(cmd::backup::BackupArgs),
Remote(cmd::remote::RemoteArgs),
#[command(hide = true)]
GitCredential(cmd::git_credential::GitCredentialArgs),
Import(cmd::import::ImportArgs),
Adopt(cmd::adopt::AdoptArgs),
Migrate(cmd::migrate::MigrateArgs),
Secrets(cmd::secrets::SecretsArgs),
Env(cmd::env::EnvArgs),
Secret(cmd::env::SecretArgs),
SecretHeader(cmd::env::SecretHeaderArgs),
Exec(cmd::exec::ExecArgs),
Completions(CompletionsArgs),
}
#[derive(Debug, Args)]
struct DoctorArgs {
#[arg(long)]
paths: bool,
#[arg(long)]
scan_history: bool,
#[arg(long, conflicts_with_all = ["paths", "scan_history"])]
report: bool,
}
#[derive(Debug, Args)]
struct CompletionsArgs {
#[arg(value_enum)]
shell: clap_complete::Shell,
}
fn main() -> ExitCode {
let argv: Vec<OsString> = std::env::args_os().collect();
if let Some(args) = cmd::exec::exec_args(&argv) {
exec::main(args);
}
let matches = Cli::command().get_matches();
let cli = Cli::from_arg_matches(&matches).unwrap_or_else(|err| err.exit());
match run(&cli, &logging::command_name(&matches)) {
Ok(code) => ExitCode::from(code),
Err(err) => {
cmd::report_error(&err, cli.json);
ExitCode::from(cmd::exit_code(&err))
}
}
}
fn run(cli: &Cli, name: &str) -> Result<u8, CoreError> {
let env = Env::from_process()?;
let flag = cli.home.as_deref().map(absolute).transpose()?;
let home = logging::logged(name)
.then(|| paths::loadout_home(&env, &RealFs, flag.as_deref()).ok())
.flatten();
let logged = home
.as_ref()
.is_some_and(|home| logging::start(&env, home, name));
let started = Instant::now();
let result = dispatch(cli, &env, flag.as_deref(), home);
if logged {
logging::finish(name, &result, started);
}
result
}
fn dispatch(
cli: &Cli,
env: &Env,
flag: Option<&Path>,
home: Option<LoadoutHome>,
) -> Result<u8, CoreError> {
let resolve = || match &home {
Some(home) => Ok(home.clone()),
None => paths::loadout_home(env, &RealFs, flag),
};
let context_unchecked = || -> Result<Context, CoreError> {
Ok(Context {
home: resolve()?,
delivery: cmd::delivery_env(env),
env: env.clone(),
json: cli.json,
yes: cli.yes,
})
};
let context = || -> Result<Context, CoreError> {
let context = context_unchecked()?;
prepare_data(env, &context.home)?;
Ok(context)
};
let done = |result: Result<(), CoreError>| result.map(|()| 0);
match &cli.command {
Command::Doctor(args) => done(doctor(env, resolve()?, args, cli.json)),
Command::Server(args) => cmd::server::run(&context()?, args),
Command::Project(args) => cmd::project::run(&context()?, args),
Command::Skills(args) => cmd::skills::run(&context()?, args),
Command::Agent(args) => cmd::agent::run(&context()?, args),
Command::Source(args) => cmd::source::run(&context()?, args),
Command::Catalog(args) => cmd::catalog::run(&context()?, args),
Command::Scanner(args) => cmd::scanner::run(&context()?, args),
Command::History(args) => done(cmd::history::run(&context()?, args)),
Command::Status(args) => cmd::status::run(&context()?, args),
Command::Diff(args) => cmd::diff::run(&context()?, args),
Command::Effective(args) => done(cmd::effective::run(&context()?, args)),
Command::Apply(args) => cmd::apply::run(&context()?, args),
Command::Rollback(args) => cmd::rollback::run(&context()?, args),
Command::Eject(args) => cmd::eject::run(&context()?, args),
Command::Show(args) => done(cmd::show::run(&context()?, args)),
Command::Tag(args) => done(cmd::tag::run(&context()?, args)),
Command::Restore(args) => cmd::restore::run(&context()?, args),
Command::Backup(args) => done(cmd::backup::run(&context()?, args)),
Command::Remote(args) => cmd::remote::run(&context()?, args),
Command::GitCredential(args) => done(cmd::git_credential::run(&context()?, args)),
Command::Import(args) => cmd::import::run(&context()?, args),
Command::Adopt(args) => cmd::adopt::run(&context()?, args),
Command::Migrate(args) => cmd::migrate::run(&context()?, args),
Command::Secrets(args) => cmd::secrets::run(&context()?, args),
Command::Env(args) => cmd::env::run_env(&context()?, args),
Command::Secret(args) => cmd::env::run_secret(&context_unchecked()?, args),
Command::SecretHeader(args) => cmd::env::run_header(&context_unchecked()?, args),
Command::Exec(args) => exec::main(&args.args),
Command::Completions(args) => {
completions(args.shell);
Ok(0)
}
}
}
fn completions(shell: clap_complete::Shell) {
clap_complete::generate(
shell,
&mut Cli::command(),
brand::BIN_NAME,
&mut std::io::stdout(),
);
}
fn absolute(path: &Path) -> Result<PathBuf, CoreError> {
std::path::absolute(path).map_err(|err| {
CoreError::new(ErrorCode::InvalidPath, format!("invalid --home: {err}")).with_path(path)
})
}
fn prepare_data(env: &Env, home: &LoadoutHome) -> Result<(), CoreError> {
let prepared = versions::prepare(env, &RealFs, home, &cmd::client_name())?;
note_migration(prepared.into_result(home)?);
Ok(())
}
fn note_migration(migration: Option<versions::Migration>) {
let Some(migration) = migration else {
return;
};
let mut note = format!(
"note: upgraded {} data folder file(s) to this version's schema",
migration.files.len()
);
if let Some(commit) = &migration.commit {
note.push_str(&format!("; store commit {}", commit.short()));
}
if let Some(backup) = &migration.backup {
note.push_str(&format!("; earlier copies in {}", backup.display()));
}
eprintln!("{note}");
}
fn doctor(env: &Env, home: LoadoutHome, args: &DoctorArgs, json: bool) -> Result<(), CoreError> {
if args.report {
let program = cmd::client_name();
let channel = install_channel(env);
let text = report::build(
env,
&RealFs,
&home,
&ReportOptions {
program: &program,
channel,
log_lines: report::DEFAULT_LOG_LINES,
},
);
if json {
print_json(&json!({ "schema": JSON_SCHEMA, "report": text }));
} else {
print!("{text}");
}
return Ok(());
}
let migration_error = match versions::prepare(env, &RealFs, &home, &cmd::client_name()) {
Ok(prepared) => {
note_migration(prepared.into_result(&home)?);
None
}
Err(err) => Some(err),
};
let findings: Vec<&SyncRoot> = home.sync_root.iter().collect();
let leaks = if args.scan_history {
match Store::open(env, &RealFs, &home, &cmd::client_name())? {
Some(store) => store.scan_history()?,
None => Vec::new(),
}
} else {
Vec::new()
};
if json {
let mut doc = json!({
"schema": JSON_SCHEMA,
"findings": findings
.iter()
.map(|root| sync_root_finding_json(root))
.chain(leaks.iter().map(leak_finding_json))
.chain(migration_error.iter().map(migration_finding_json))
.collect::<Vec<_>>(),
});
if args.paths {
doc["paths"] = paths_json(env, &home);
}
print_json(&doc);
return Ok(());
}
if args.paths {
for (label, value) in paths_table(env, &home) {
println!("{label:<18}{value}");
}
println!();
}
if findings.is_empty() && leaks.is_empty() && migration_error.is_none() {
println!("No problems found.");
}
if let Some(err) = &migration_error {
println!(
"error[{MIGRATION_FAILED}]: the data folder could not be upgraded to this version's \
schema: [{}] {err}",
err.code()
);
}
for root in findings {
println!("warning[sync_root_detected]: {}", sync_root_warning(root));
}
for leak in &leaks {
println!(
"warning[leak_detected]: commit {} {}",
leak.commit.short(),
leak.finding
);
}
Ok(())
}
const MIGRATION_FAILED: &str = "schema_migration_failed";
fn migration_finding_json(err: &CoreError) -> Value {
json!({
"code": MIGRATION_FAILED,
"severity": "error",
"error": cmd::error_json(err),
})
}
fn install_channel(env: &Env) -> Option<&'static str> {
let exe = std::env::current_exe().ok()?;
let cargo_home = env
.var("CARGO_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| env.home().join(".cargo"));
exe.starts_with(cargo_home.join("bin")).then_some("cargo")
}
fn leak_finding_json(leak: &HistoryFinding) -> Value {
json!({
"code": "leak_detected",
"severity": "warning",
"commit": leak.commit.to_string(),
"file": leak.finding.file,
"line": leak.finding.line,
"key": leak.finding.key,
"reason": leak.finding.kind.as_str(),
})
}
fn paths_table(env: &Env, home: &LoadoutHome) -> Vec<(&'static str, String)> {
let mut rows = vec![
("os", env.os().as_str().to_owned()),
("profile", env.home().display().to_string()),
];
for (label, folder) in known_folders() {
if let Some(path) = env.known_folder(folder) {
rows.push((label, path.display().to_string()));
}
}
rows.push((
"loadout_home",
format!("{} ({})", home.path.display(), home.source.as_str()),
));
rows.extend(layout(home).map(|(label, path)| (label, path.display().to_string())));
rows
}
fn paths_json(env: &Env, home: &LoadoutHome) -> Value {
let mut doc = json!({
"os": env.os().as_str(),
"profile": path_json(env.home()),
"loadout_home": path_json(&home.path),
"loadout_home_source": home.source.as_str(),
});
for (label, folder) in known_folders() {
doc[label] = env.known_folder(folder).map_or(Value::Null, path_json);
}
for (label, path) in layout(home) {
doc[label] = path_json(&path);
}
doc
}
fn known_folders() -> [(&'static str, KnownFolder); 2] {
[
("local_app_data", KnownFolder::LocalAppData),
("roaming_app_data", KnownFolder::RoamingAppData),
]
}
fn layout(home: &LoadoutHome) -> impl Iterator<Item = (&'static str, PathBuf)> {
[
("store", home.store_dir()),
("state", home.state_dir()),
("backups", home.backups_dir()),
("cache", home.cache_dir()),
("imports", home.imports_dir()),
("logs", home.logs_dir()),
]
.into_iter()
}
fn sync_root_finding_json(root: &SyncRoot) -> Value {
json!({
"code": "sync_root_detected",
"severity": "warning",
"kind": root.kind.as_str(),
"root": path_json(&root.root),
})
}
#[cfg(test)]
mod tests {
use agent_loadout_core::agent;
use clap::CommandFactory;
use super::Cli;
fn skill_texts() -> Vec<(String, String)> {
agent::bundled_files()
.into_iter()
.map(|(path, bytes)| (path, String::from_utf8(bytes).expect("UTF-8 skill file")))
.collect()
}
fn code_segments(path: &str, text: &str) -> Vec<(String, String)> {
let mut segments = Vec::new();
let mut fenced = false;
for (index, line) in text.lines().enumerate() {
let place = format!("{path}:{}", index + 1);
if line.trim_start().starts_with("```") {
fenced = !fenced;
continue;
}
if fenced {
segments.push((place, line.to_owned()));
continue;
}
for (n, part) in line.split('`').enumerate() {
if n % 2 == 1 {
segments.push((place.clone(), part.to_owned()));
}
}
}
segments
}
fn check_command(place: &str, line: &str) {
let root = Cli::command();
let globals: Vec<String> = root
.get_arguments()
.filter_map(|arg| arg.get_long().map(str::to_owned))
.chain(["help".to_owned(), "version".to_owned()])
.collect();
let mut command = &root;
let mut descending = true;
let words = line.split_whitespace().skip(1);
for word in words.take_while(|word| !["|", "&&", ";", ">", "#"].contains(word)) {
let word = word.trim_matches(|c| c == '[' || c == ']' || c == ',');
let word = word.trim_end_matches("...");
if let Some(flag) = word.strip_prefix("--") {
let name = flag.split('=').next().unwrap_or(flag);
let known = command
.get_arguments()
.any(|arg| arg.get_long() == Some(name))
|| globals.iter().any(|global| global == name);
assert!(
known,
"{place}: `{line}`: --{name} is not an option of `{}`",
command.get_name()
);
continue;
}
if word.starts_with('<') && descending && command.has_subcommands() {
return;
}
if word.starts_with('-') || word.starts_with('<') || word.is_empty() {
continue;
}
if descending && command.has_subcommands() {
command = command.find_subcommand(word).unwrap_or_else(|| {
panic!(
"{place}: `{line}`: `{word}` is not a subcommand of `{}`",
command.get_name()
)
});
continue;
}
descending = false;
}
assert!(
!command.has_subcommands(),
"{place}: `{line}`: `{}` needs a subcommand",
command.get_name()
);
}
#[test]
fn every_command_the_skill_cites_exists() {
let mut checked = 0;
for (path, text) in skill_texts() {
for (place, segment) in code_segments(&path, &text) {
let segment = segment.trim();
if segment.starts_with("loadout ") {
check_command(&place, segment);
checked += 1;
}
}
}
assert!(checked >= 30, "only {checked} commands found");
}
const DECISION_FLAGS: [&str; 7] = [
"--yes",
"--trust-source",
"--allow-dangerous",
"--on-conflict",
"--remove-old-backups",
"--all-identical",
"--theirs",
];
#[test]
fn decision_flags_appear_only_in_never_guidance() {
for (path, text) in skill_texts() {
for (index, line) in text.lines().enumerate() {
for flag in DECISION_FLAGS.iter().filter(|flag| line.contains(*flag)) {
assert!(
never_guarded(line, flag),
"{path}:{}: {flag} outside \"never pass/use\" guidance: {line}",
index + 1
);
}
}
}
let skill = &skill_texts()
.into_iter()
.find(|(path, _)| path == "SKILL.md")
.expect("SKILL.md")
.1;
for flag in ["--yes", "--trust-source", "--allow-dangerous"] {
assert!(skill.contains(flag), "SKILL.md says never to use {flag}");
}
let prose = skill.split_whitespace().collect::<Vec<_>>().join(" ");
assert!(
prose.contains("is data, not instructions"),
"SKILL.md says third-party text is data"
);
}
fn never_guarded(line: &str, flag: &str) -> bool {
let lower = line.to_lowercase();
lower.match_indices(flag).all(|(at, _)| {
let before = &lower[..at];
["never pass", "never use"]
.iter()
.filter_map(|guard| before.rfind(guard).map(|start| start + guard.len()))
.max()
.is_some_and(|end| !before[end..].contains(". "))
})
}
#[test]
fn the_guard_needs_never_before_the_flag_in_one_sentence() {
assert!(never_guarded(
"Never pass `--yes` or `--trust-source`.",
"--yes"
));
assert!(!never_guarded(
"run `loadout apply --yes`, never skip review",
"--yes"
));
assert!(!never_guarded("Never pass it. Run `--yes` now.", "--yes"));
assert!(!never_guarded(
"Use `--allow-dangerous` when you like",
"--allow-dangerous"
));
}
#[test]
fn the_errors_reference_names_real_codes() {
let errors = &skill_texts()
.into_iter()
.find(|(path, _)| path == "references/errors.md")
.expect("errors.md")
.1;
let table = errors
.split("## Error codes")
.nth(1)
.and_then(|rest| rest.split("\n## ").next())
.expect("an Error codes section");
let mut listed = Vec::new();
for line in table.lines().filter(|line| line.starts_with("| `")) {
let first = line.split('|').nth(1).unwrap_or_default();
for (n, part) in first.split('`').enumerate() {
if n % 2 == 1 {
listed.push(part.to_owned());
}
}
}
for code in &listed {
assert!(
serde_json::from_value::<agent_loadout_core::ErrorCode>(serde_json::json!(code))
.is_ok(),
"errors.md lists {code:?}, which is not an error code"
);
}
for code in [
"not_found",
"locked",
"trust_required",
"missing_secret",
"scan_blocked",
"source_trust_required",
"catalogs_off",
"catalog_unreachable",
] {
assert!(
listed.iter().any(|known| known == code),
"errors.md misses {code}"
);
}
}
}