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