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};
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, merge,
50    oracle, pipeline, probar, profile, prune, publish, pull, qa, qualify, quantize, rosetta,
51    rosetta::RosettaCommands, run, serve, showcase, tensors, tokenize, trace, tree, tui, validate,
52};
53#[cfg(feature = "training")]
54use commands::{finetune, gpu, train, tune};
55
56/// apr - APR Model Operations Tool
57///
58/// Inspect, debug, and manage .apr model files.
59/// Toyota Way: Genchi Genbutsu - Go and see the actual data.
60#[derive(Parser, Debug)]
61#[command(name = "apr")]
62#[command(author, version = concat!(env!("CARGO_PKG_VERSION"), " (", env!("APR_GIT_SHA"), ")"), about, long_about = None)]
63#[command(propagate_version = true)]
64pub struct Cli {
65    #[command(subcommand)]
66    pub command: Box<Commands>,
67
68    /// Output as JSON
69    #[arg(long, global = true)]
70    pub json: bool,
71
72    /// Verbose output
73    #[arg(short, long, global = true)]
74    pub verbose: bool,
75
76    /// Quiet mode (errors only)
77    #[arg(short, long, global = true)]
78    pub quiet: bool,
79
80    /// Disable network access (Sovereign AI compliance, Section 9)
81    #[arg(long, global = true)]
82    pub offline: bool,
83
84    /// Skip tensor contract validation (PMAT-237: use with diagnostic tooling)
85    #[arg(long, global = true)]
86    pub skip_contract: bool,
87}
88
89include!("commands_enum.rs");
90include!("model_ops_commands.rs");
91include!("extended_commands.rs");
92include!("tool_commands.rs");
93include!("data_commands.rs");
94#[cfg(feature = "training")]
95include!("train_commands.rs");
96include!("serve_commands.rs");
97include!("tokenize_commands.rs");
98include!("pipeline_commands.rs");
99include!("validate.rs");
100include!("dispatch_run.rs");
101include!("dispatch.rs");
102include!("dispatch_analysis.rs");
103include!("lib_07.rs");
104
105/// Full CLI entry point for `cargo install aprender`.
106///
107/// This function encapsulates the complete `apr` binary logic so that
108/// the `aprender` facade crate can produce the same binary via
109/// `cargo install aprender` (in addition to `cargo install apr-cli`).
110pub fn cli_main() -> std::process::ExitCode {
111    // GH-667: Reset SIGPIPE to default so piping to head/less doesn't panic.
112    #[cfg(unix)]
113    #[allow(unsafe_code)]
114    unsafe {
115        libc::signal(libc::SIGPIPE, libc::SIG_DFL);
116    }
117
118    // GH-646: Clear FPCR.FZ16 on aarch64 so f16 subnormals work.
119    #[cfg(target_arch = "aarch64")]
120    #[allow(unsafe_code)]
121    unsafe {
122        let fpcr: u64;
123        core::arch::asm!("mrs {}, fpcr", out(reg) fpcr);
124        if fpcr & (1 << 19) != 0 {
125            let new_fpcr = fpcr & !(1 << 19);
126            core::arch::asm!("msr fpcr, {}", in(reg) new_fpcr);
127        }
128    }
129
130    // GH-662: Respect NO_COLOR env var and non-TTY output.
131    let no_color = std::env::var("NO_COLOR").is_ok();
132    let is_tty = std::io::IsTerminal::is_terminal(&std::io::stdout());
133    if no_color || !is_tty {
134        colored::control::set_override(false);
135    }
136
137    let cli = Cli::parse();
138    match execute_command(&cli) {
139        Ok(()) => std::process::ExitCode::SUCCESS,
140        Err(e) => {
141            eprintln!("error: {e}");
142            e.exit_code()
143        }
144    }
145}