Skip to main content

apr_cli/
lib.rs

1//! apr-cli library
2//!
3//! This library is the foundation for the apr CLI binary.
4//! Exports CLI structures for testing and reuse.
5
6// APR-MONO: Clippy pedantic allows for monorepo transition.
7// unwrap() eliminated (524 → expect()). Style lints from 20 merged crates
8// are suppressed at crate level. Will be incrementally addressed.
9#![allow(clippy::all, clippy::pedantic, clippy::disallowed_methods)]
10#![allow(
11    unreachable_code,
12    unused_variables,
13    unused_imports,
14    dead_code,
15    unused_assignments
16)]
17
18use clap::{Parser, Subcommand, ValueEnum};
19use std::path::{Path, PathBuf};
20
21// Contract assertions from YAML (pv codegen)
22#[macro_use]
23#[allow(unused_macros, clippy::duplicated_attributes)]
24mod generated_contracts;
25
26mod commands;
27pub mod error;
28mod output;
29pub mod pipe;
30
31pub use error::CliError;
32
33// Public re-exports for integration tests
34pub mod qa_types {
35    pub use crate::commands::qa::{GateResult, QaReport, SystemInfo};
36}
37
38// Public re-exports for downstream crates (whisper-apr proxies these)
39pub mod model_pull {
40    pub use crate::commands::pull::{list, run};
41}
42
43#[cfg(feature = "inference")]
44pub mod federation;
45
46// Commands are crate-private, used internally by execute_command
47use commands::{
48    bench, canary, canary::CanaryCommands, cbtop, chat, compare_hf, compile, convert, data, debug,
49    diagnose, diff, distill, eval, explain, export, flow, hex, import, inspect, lint, mcp, merge,
50    oracle, pipeline, probar, profile, prune, publish, pull, qa, qualify, quantize, rosetta,
51    rosetta::RosettaCommands, run, serve, showcase, stamp, tensors, tokenize, trace, tree, tui,
52    validate, validate_manifest,
53};
54#[cfg(feature = "training")]
55use commands::{finetune, gpu, train, tune};
56
57#[cfg(feature = "training")]
58pub use commands::pretrain::PretrainMode;
59
60/// apr - APR Model Operations Tool
61///
62/// Inspect, debug, and manage .apr model files.
63/// Toyota Way: Genchi Genbutsu - Go and see the actual data.
64#[derive(Parser, Debug)]
65#[command(name = "apr")]
66#[command(author, version = concat!(env!("CARGO_PKG_VERSION"), " (", env!("APR_GIT_SHA"), ")"), about, long_about = None)]
67#[command(propagate_version = true)]
68pub struct Cli {
69    #[command(subcommand)]
70    pub command: Box<Commands>,
71
72    /// Output as JSON
73    #[arg(long, global = true)]
74    pub json: bool,
75
76    /// Verbose output
77    #[arg(short, long, global = true)]
78    pub verbose: bool,
79
80    /// Quiet mode (errors only)
81    #[arg(short, long, global = true)]
82    pub quiet: bool,
83
84    /// Disable network access (Sovereign AI compliance, Section 9)
85    #[arg(long, global = true)]
86    pub offline: bool,
87
88    /// Skip tensor contract validation (PMAT-237: use with diagnostic tooling)
89    #[arg(long, global = true)]
90    pub skip_contract: bool,
91}
92
93include!("commands_enum.rs");
94include!("model_ops_commands.rs");
95include!("extended_commands.rs");
96include!("tool_commands.rs");
97include!("data_commands.rs");
98#[cfg(feature = "training")]
99include!("train_commands.rs");
100include!("serve_commands.rs");
101include!("tokenize_commands.rs");
102include!("pipeline_commands.rs");
103include!("validate.rs");
104include!("dispatch_run.rs");
105include!("dispatch.rs");
106include!("dispatch_analysis.rs");
107include!("lib_07.rs");
108
109/// Full CLI entry point for `cargo install aprender`.
110///
111/// This function encapsulates the complete `apr` binary logic so that
112/// the `aprender` facade crate can produce the same binary via
113/// `cargo install aprender` (in addition to `cargo install apr-cli`).
114pub fn cli_main() -> std::process::ExitCode {
115    // GH-667: Reset SIGPIPE to default so piping to head/less doesn't panic.
116    #[cfg(unix)]
117    #[allow(unsafe_code)]
118    unsafe {
119        libc::signal(libc::SIGPIPE, libc::SIG_DFL);
120    }
121
122    // GH-646: Clear FPCR.FZ16 on aarch64 so f16 subnormals work.
123    #[cfg(target_arch = "aarch64")]
124    #[allow(unsafe_code)]
125    unsafe {
126        let fpcr: u64;
127        core::arch::asm!("mrs {}, fpcr", out(reg) fpcr);
128        if fpcr & (1 << 19) != 0 {
129            let new_fpcr = fpcr & !(1 << 19);
130            core::arch::asm!("msr fpcr, {}", in(reg) new_fpcr);
131        }
132    }
133
134    // GH-662: Respect NO_COLOR env var and non-TTY output.
135    let no_color = std::env::var("NO_COLOR").is_ok();
136    let is_tty = std::io::IsTerminal::is_terminal(&std::io::stdout());
137    if no_color || !is_tty {
138        colored::control::set_override(false);
139    }
140
141    // FALSIFY-GPUTRAIN-007 / INV-GPUTRAIN-007 — `apr --version --json` MUST
142    // emit a machine-parseable object containing at least the three keys
143    // { cuda_feature, cuda_runtime_available, visible_devices }. Intercept
144    // this flag combo BEFORE clap's default `--version` handler exits the
145    // process with a plain string. Bound by `gputrain_007.rs` — see
146    // `AC_GPUTRAIN_007_REQUIRED_VERSION_JSON_KEYS`.
147    let raw: Vec<String> = std::env::args().collect();
148    if raw.iter().any(|a| a == "--version") && raw.iter().any(|a| a == "--json") {
149        emit_version_json();
150        return std::process::ExitCode::SUCCESS;
151    }
152
153    let cli = Cli::parse();
154    match execute_command(&cli) {
155        Ok(()) => std::process::ExitCode::SUCCESS,
156        Err(e) => {
157            eprintln!("error: {e}");
158            e.exit_code()
159        }
160    }
161}
162
163/// FALSIFY-GPUTRAIN-007 — emit `apr --version --json` output with the
164/// 3-key schema required by `AC_GPUTRAIN_007_REQUIRED_VERSION_JSON_KEYS`.
165///
166/// Schema:
167/// ```json
168/// {
169///   "name":    "apr",
170///   "version": "<semver>",
171///   "git_sha": "<commit>",
172///   "cuda_feature":           <bool>,   // was the binary built --features cuda?
173///   "cuda_runtime_available": <bool>,   // does cudaRuntimeGetVersion succeed?
174///   "visible_devices":        ["0", "1", ...]  // nvidia-smi -L indices, empty if no runtime
175/// }
176/// ```
177///
178/// Consumers (`entrenar::train::gputrain_007::verdict_from_version_json_keys`
179/// and `verdict_from_version_json_fields`) parse this and assert schema
180/// completeness + field invariants (`visible_devices.len() <= 16`, no
181/// `cuda_feature && !cuda_runtime_available` inconsistency).
182pub fn emit_version_json() {
183    let cuda_feature = cfg!(feature = "cuda");
184
185    // cuda_runtime_available: try nvidia-smi -L. Present-and-exits-0 ⇒ true.
186    // This matches how gputrain_003 queries nvidia-smi — keep the probe
187    // consistent with the residency check.
188    let cuda_runtime_available = std::process::Command::new("nvidia-smi")
189        .arg("-L")
190        .output()
191        .map(|o| o.status.success())
192        .unwrap_or(false);
193
194    // visible_devices: if the runtime is available, parse nvidia-smi -L
195    // output (one GPU per line, "GPU 0: ...", "GPU 1: ..."). Emit the
196    // index strings to match INV-GPUTRAIN-001 grammar (:0..:15).
197    let visible_devices: Vec<String> = if cuda_runtime_available {
198        std::process::Command::new("nvidia-smi")
199            .arg("-L")
200            .output()
201            .ok()
202            .and_then(|o| String::from_utf8(o.stdout).ok())
203            .map(|s| {
204                s.lines()
205                    .filter_map(|line| {
206                        // Expect "GPU <idx>: <name> (UUID: <uuid>)"
207                        line.strip_prefix("GPU ").and_then(|rest| {
208                            rest.split_once(':').map(|(idx, _)| idx.trim().to_string())
209                        })
210                    })
211                    .collect()
212            })
213            .unwrap_or_default()
214    } else {
215        Vec::new()
216    };
217
218    let body = serde_json::json!({
219        "name": "apr",
220        "version": env!("CARGO_PKG_VERSION"),
221        "git_sha": env!("APR_GIT_SHA"),
222        "cuda_feature": cuda_feature,
223        "cuda_runtime_available": cuda_runtime_available,
224        "visible_devices": visible_devices,
225    });
226
227    // Emit pretty-printed JSON on stdout so it's grep-friendly and
228    // round-trippable through `| jq .cuda_feature`.
229    println!(
230        "{}",
231        serde_json::to_string_pretty(&body).expect("build version json")
232    );
233}