proof-cli 1.0.0

Command-line interface for creating, verifying, and evaluating Proof Engine proofs
Documentation
// Copyright 2026 Proof Engine Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
//! proof-cli binary: thin dispatcher over the library modules. All trust
//! decisions live in proof-verify / proof-policy; this file only routes
//! commands and maps outcomes to exit codes.

use proof_cli::{Cli, EXIT_ERR, EXIT_OK, USAGE};

fn main() {
    let args: Vec<String> = std::env::args().skip(1).collect();
    let code = run(&args);
    std::process::exit(code);
}

// PE-CLI-001: 0 PASS · 1 verdict · 2 usage/engine error (matrix-tested below).
fn run(args: &[String]) -> i32 {
    let cli = match Cli::parse(args) {
        Ok(c) => c,
        Err(e) => {
            eprintln!(
                "error: {e}

{USAGE}"
            );
            return EXIT_ERR;
        }
    };
    if cli.command == "help" || cli.has("help") {
        println!("{USAGE}");
        return EXIT_OK;
    }
    let result = match cli.command.as_str() {
        "create-event" => proof_cli::artifact::write_event(&cli).map(|_| EXIT_OK),
        "attest" => proof_cli::artifact::write_attestation(&cli).map(|_| EXIT_OK),
        "add-evidence" => proof_cli::artifact::write_evidence(&cli).map(|_| EXIT_OK),
        "relate" => proof_cli::artifact::write_relationship(&cli).map(|_| EXIT_OK),
        "revoke" => proof_cli::artifact::write_status_object(&cli, "revoke").map(|_| EXIT_OK),
        "supersede" => proof_cli::artifact::write_status_object(&cli, "supersede").map(|_| EXIT_OK),
        "build" => proof_cli::make::build(&cli).map(|_| EXIT_OK),
        "verify" => proof_cli::make::verify(&cli),
        "evaluate" => proof_cli::make::evaluate(&cli, false),
        "explain" => proof_cli::make::evaluate(&cli, true),
        "demo" => {
            let dir = cli.opt("out").unwrap_or_else(|| "demo/out".to_string());
            proof_cli::demo::run(&dir)
        }
        other => Err(format!("unknown command `{other}` (try `proof-cli help`)")),
    };
    match result {
        Ok(code) => code,
        // Usage and engine errors are exit 2; exit 1 is reserved for
        // FAIL/INDETERMINATE verdicts (returned as Ok(EXIT_FAIL) above).
        Err(e) => {
            eprintln!("error: {e}");
            EXIT_ERR
        }
    }
}

#[cfg(test)]
mod tests {
    use super::run;

    fn args(v: &[&str]) -> Vec<String> {
        v.iter().map(|s| s.to_string()).collect()
    }

    fn tmpdir(name: &str) -> String {
        let d =
            std::env::temp_dir().join(format!("proof-cli-main-test-{name}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&d);
        std::fs::create_dir_all(&d).unwrap();
        d.to_string_lossy().into_owned()
    }

    /// Exit-code contract (lib.rs, README, POLICY.md): 0 = PASS/valid,
    /// 1 = FAIL/INDETERMINATE verdict, 2 = usage or engine error.
    #[test]
    fn usage_errors_exit_2_not_1() {
        // Unknown command.
        assert_eq!(run(&args(&["bogus-cmd"])), proof_cli::EXIT_ERR);
        // Missing required flag.
        assert_eq!(
            run(&args(&["verify", "--proof", "p.json"])),
            proof_cli::EXIT_ERR
        );
        // Bad flag value type.
        assert_eq!(
            run(&args(&["verify", "--proof", "p.json", "--clock", "soon"])),
            proof_cli::EXIT_ERR
        );
        // Unreadable input file (engine error, not a verdict).
        assert_eq!(
            run(&args(&[
                "verify",
                "--proof",
                "/tmp/does-not-exist-proof-cli-test.json",
                "--clock",
                "1700000300"
            ])),
            proof_cli::EXIT_ERR
        );
        // Oversized input (H1 cap) is an engine error, not a verdict.
        let w = tmpdir("oversized");
        let big = format!("{w}/big.json");
        std::fs::write(
            &big,
            vec![b'x'; (proof_cli::MAX_INPUT_FILE_BYTES + 8) as usize],
        )
        .unwrap();
        assert_eq!(
            run(&args(&["verify", "--proof", &big, "--clock", "1700000300"])),
            proof_cli::EXIT_ERR
        );
    }

    #[test]
    fn help_exits_0() {
        assert_eq!(run(&args(&["help"])), proof_cli::EXIT_OK);
    }

    /// PE-CRYPTO-009 (FORMAT §3): ids carry one full SHA-256 digest; a
    /// truncated digest cannot name an object. Usage error (exit 2), and the
    /// failure names the digest rule, not a generic parse error.
    #[test]
    fn truncated_digest_refs_rejected_as_usage_error() {
        let full = "1111111111111111111111111111111111111111111111111111111111111111";
        let short = &full[..40];
        for flag in ["--payload-hex", "--digest-hex"] {
            let cmd = if flag == "--payload-hex" {
                vec![
                    "create-event".to_string(),
                    "--type".to_string(),
                    "x.y".to_string(),
                    "--subject".to_string(),
                    "s:1".to_string(),
                    "--effective-at".to_string(),
                    "1700000000".to_string(),
                    flag.to_string(),
                    short.to_string(),
                    "--out".to_string(),
                    "/tmp/proof-cli-must-not-write.json".to_string(),
                ]
            } else {
                vec![
                    "add-evidence".to_string(),
                    "--kind".to_string(),
                    "k".to_string(),
                    flag.to_string(),
                    short.to_string(),
                    "--out".to_string(),
                    "/tmp/proof-cli-must-not-write.json".to_string(),
                ]
            };
            let r = run(&cmd);
            assert_eq!(
                r,
                proof_cli::EXIT_ERR,
                "{flag}: truncated digest must be usage error"
            );
            assert!(
                !std::path::Path::new("/tmp/proof-cli-must-not-write.json").exists(),
                "no artifact may be written from a truncated digest"
            );
        }
    }
}