use anyhow::{Context, Result, bail};
use clap::{Parser, Subcommand};
use std::{env, path::PathBuf, process::ExitCode};
mod ashroot;
mod build;
mod cargo;
mod compat;
mod preflight;
mod profile;
mod project;
mod receipt;
mod rehearse;
mod skills;
mod takt;
mod wire;
const TOOLCHAIN: &str = "nightly-2026-10-15";
#[derive(Parser)]
#[command(version, about)]
struct Cli {
#[command(subcommand)]
command: Operation,
}
#[derive(Subcommand)]
enum Operation {
New {
path: PathBuf,
#[arg(long, default_value = "quickstart")]
example: project::Example,
#[arg(long, value_enum)]
agent: Option<project::Agent>,
},
Build(build::Options),
Lock {
#[arg(long, default_value = "Cargo.toml")]
manifest_path: PathBuf,
#[arg(long)]
offline: bool,
},
Docs {
package: String,
guide: Option<PathBuf>,
#[arg(long, default_value = "Cargo.toml")]
manifest_path: PathBuf,
},
Skills {
#[command(subcommand)]
command: SkillOperation,
},
Upgrade {
#[command(subcommand)]
command: UpgradeOperation,
},
Verify { manifest: PathBuf },
Takt(takt::Options),
Wire {
#[command(subcommand)]
command: WireOperation,
},
Profile {
#[command(subcommand)]
command: profile::ProfileCommand,
},
#[command(trailing_var_arg = true)]
Tool {
#[arg(required = true, allow_hyphen_values = true)]
args: Vec<String>,
},
}
#[derive(Subcommand)]
enum SkillOperation {
Install {
name: String,
#[arg(long, value_enum)]
agent: project::Agent,
#[arg(long, default_value = "Cargo.toml")]
manifest_path: PathBuf,
},
Sync {
#[arg(long, value_enum)]
agent: project::Agent,
#[arg(long, default_value = "Cargo.toml")]
manifest_path: PathBuf,
#[arg(short, long)]
package: Option<String>,
#[arg(short = 'F', long)]
features: Vec<String>,
#[arg(long)]
all_features: bool,
#[arg(long)]
no_default_features: bool,
#[arg(long, default_value = build::WASM64, value_parser = [build::WASM64, build::WASM32])]
wasm_target: String,
},
}
#[derive(Subcommand)]
enum UpgradeOperation {
Preflight {
#[arg(long)]
baseline: PathBuf,
#[arg(long)]
candidate: PathBuf,
},
Rehearse(rehearse::Options),
}
#[derive(Subcommand)]
enum WireOperation {
Manifest { build_manifest: PathBuf },
}
fn run(operation: Operation) -> Result<ExitCode> {
match operation {
Operation::New {
path,
example,
agent,
} => {
project::new(&path, TOOLCHAIN, example)?;
if let Some(agent) = agent {
skills::sync(&path.join("Cargo.toml"), None, &[], build::WASM64, agent)
.context("project created. To retry, run `cdk-rsx skills sync` there with the same `--agent` value")?;
}
}
Operation::Build(options) => build::build(options)?,
Operation::Lock {
manifest_path,
offline,
} => cargo::refresh_lock(&manifest_path, offline)?,
Operation::Docs {
package,
guide,
manifest_path,
} => {
let metadata = cargo::project_metadata(&manifest_path)?;
let path = project::docs(&metadata, &package, guide.as_deref())?;
println!("{}", path.display());
}
Operation::Skills {
command:
SkillOperation::Install {
name,
agent,
manifest_path,
},
} => {
let manifest = manifest_path.canonicalize()?;
let metadata = cargo::project_metadata(&manifest)?;
let root = manifest.parent().unwrap();
let path = project::install_skill(&metadata, root, &name, agent)?;
println!("{}", path.display());
}
Operation::Upgrade {
command:
UpgradeOperation::Preflight {
baseline,
candidate,
},
} => {
let result = match preflight::preflight(&baseline, &candidate) {
Ok(result) => result,
Err(error) => {
eprintln!("cdk-rsx: {error:#}");
return Ok(ExitCode::from(2));
}
};
println!("{}", serde_json::to_string_pretty(&result)?);
return Ok(if result["status"] == "compiler-policy-match" {
ExitCode::SUCCESS
} else {
ExitCode::FAILURE
});
}
Operation::Upgrade {
command: UpgradeOperation::Rehearse(options),
} => rehearse::run(options)?,
Operation::Skills {
command:
SkillOperation::Sync {
agent,
manifest_path,
package,
features,
all_features,
no_default_features,
wasm_target,
},
} => {
let flags = cargo::feature_flags(features, all_features, no_default_features);
skills::sync(
&manifest_path,
package.as_deref(),
&flags,
&wasm_target,
agent,
)?;
}
Operation::Verify { manifest } => {
println!(
"{}",
serde_json::to_string_pretty(&preflight::verify(&manifest)?)?
);
}
Operation::Tool { args } => cdk_rsx_wasm::command(&args)?,
Operation::Takt(options) => takt::run(options)?,
Operation::Wire {
command: WireOperation::Manifest { build_manifest },
} => {
println!(
"{}",
serde_json::to_string_pretty(&wire::manifest(&build_manifest)?)?
);
}
Operation::Profile { command } => command.run()?,
}
Ok(ExitCode::SUCCESS)
}
fn entrypoint() -> Result<ExitCode> {
let executable = env::current_exe()?;
if executable
.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| name.ends_with(".wasm-tool"))
{
cdk_rsx_wasm::command(&env::args().skip(1).collect::<Vec<_>>())?;
return Ok(ExitCode::SUCCESS);
}
if !cfg!(unix) {
bail!("CDK RSX canister builds require a Linux or macOS host");
}
run(Cli::parse().command)
}
fn main() -> ExitCode {
match entrypoint() {
Ok(code) => code,
Err(error) => {
eprintln!("cdk-rsx: {error:#}");
ExitCode::FAILURE
}
}
}