interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / rust / crates / interplane-conformance / src / bin / interplane-corpus.rs
| 1 | //! Corpus tools for the INTERPLANE conformance corpus. |
| 2 | //! |
| 3 | //! interplane-corpus [--root <repo>] schemas check every spec/schemas/*.json against the 2020-12 meta-schema |
| 4 | //! interplane-corpus [--root <repo>] validate validate the conformance and dialect fixtures |
| 5 | //! interplane-corpus [--root <repo>] trust-digest --check recompute and compare with conformance/TRUST-DIGEST.txt |
| 6 | //! interplane-corpus [--root <repo>] trust-digest --write rewrite conformance/TRUST-DIGEST.txt |
| 7 | //! interplane-corpus [--root <repo>] subset --subset <adapter> print the adapter's subset, one fixture per line |
| 8 | //! interplane-corpus [--root <repo>] subset --plans <adapter> print the translated plans of the subset as JSON |
| 9 | //! |
| 10 | //! Without --root the repository is the nearest directory, from the current one upward, that holds |
| 11 | //! conformance/adapter-translation.json. Exit 0 on success, 1 on a failed check, 2 on a usage error. |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | use std::process::ExitCode; |
| 14 | |
| 15 | use interplane_conformance::ojson::{dumps, OJson, Style}; |
| 16 | use interplane_conformance::subset::Corpus; |
| 17 | use interplane_conformance::{fixture_check, trust_digest}; |
| 18 | |
| 19 | const USAGE: &str = "usage: interplane-corpus [--root <repo>] schemas | validate | trust-digest --check|--write | subset --subset|--plans <adapter>"; |
| 20 | |
| 21 | fn find_root() -> Option<PathBuf> { |
| 22 | let mut d = std::env::current_dir().ok()?; |
| 23 | loop { |
| 24 | if d.join("conformance/adapter-translation.json").is_file() { |
| 25 | return Some(d); |
| 26 | } |
| 27 | if !d.pop() { |
| 28 | return None; |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn usage() -> ExitCode { |
| 34 | eprintln!("{USAGE}"); |
| 35 | ExitCode::from(2) |
| 36 | } |
| 37 | |
| 38 | fn fail(e: String) -> ExitCode { |
| 39 | eprintln!("error: {e}"); |
| 40 | ExitCode::from(1) |
| 41 | } |
| 42 | |
| 43 | fn main() -> ExitCode { |
| 44 | let mut args: Vec<String> = std::env::args().skip(1).collect(); |
| 45 | let root = if args.first().map(String::as_str) == Some("--root") { |
| 46 | if args.len() < 2 { |
| 47 | return usage(); |
| 48 | } |
| 49 | let r = PathBuf::from(args.remove(1)); |
| 50 | args.remove(0); |
| 51 | r |
| 52 | } else { |
| 53 | match find_root() { |
| 54 | Some(r) => r, |
| 55 | None => { |
| 56 | eprintln!("no repository found above the current directory; pass --root"); |
| 57 | return ExitCode::from(2); |
| 58 | } |
| 59 | } |
| 60 | }; |
| 61 | let args: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 62 | match args.as_slice() { |
| 63 | ["schemas"] => match fixture_check::self_check_schemas(&root) { |
| 64 | Ok(lines) => { |
| 65 | for l in lines { |
| 66 | println!("{l}"); |
| 67 | } |
| 68 | ExitCode::SUCCESS |
| 69 | } |
| 70 | Err(e) => fail(e), |
| 71 | }, |
| 72 | ["validate"] => match fixture_check::run(&root) { |
| 73 | Ok(r) => { |
| 74 | print!("{}", r.text()); |
| 75 | if r.passed() { |
| 76 | ExitCode::SUCCESS |
| 77 | } else { |
| 78 | ExitCode::from(1) |
| 79 | } |
| 80 | } |
| 81 | Err(e) => fail(e), |
| 82 | }, |
| 83 | ["trust-digest", mode @ ("--check" | "--write")] => trust(&root, mode), |
| 84 | ["subset", mode @ ("--subset" | "--plans"), adapter] => subset(&root, mode, adapter), |
| 85 | _ => usage(), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fn trust(root: &Path, mode: &str) -> ExitCode { |
| 90 | let want = match trust_digest::compute(root) { |
| 91 | Ok(w) => w, |
| 92 | Err(e) => return fail(e), |
| 93 | }; |
| 94 | let out = root.join(trust_digest::OUT); |
| 95 | if mode == "--write" { |
| 96 | if let Err(e) = std::fs::write(&out, &want) { |
| 97 | return fail(format!("{}: {e}", out.display())); |
| 98 | } |
| 99 | println!("wrote {}", out.display()); |
| 100 | return ExitCode::SUCCESS; |
| 101 | } |
| 102 | if std::fs::read_to_string(&out).ok().as_deref() != Some(want.as_str()) { |
| 103 | println!("TRUST-DIGEST.txt is stale or missing; expected:\n{want}"); |
| 104 | return ExitCode::from(1); |
| 105 | } |
| 106 | println!("TRUST-DIGEST.txt matches"); |
| 107 | ExitCode::SUCCESS |
| 108 | } |
| 109 | |
| 110 | fn subset(root: &Path, mode: &str, adapter: &str) -> ExitCode { |
| 111 | let corpus = Corpus::new(root); |
| 112 | let table = match corpus.load_table() { |
| 113 | Ok(t) => t, |
| 114 | Err(e) => return fail(e), |
| 115 | }; |
| 116 | if !table.get("adapters").has(adapter) { |
| 117 | eprintln!("{adapter} is not in conformance/adapter-translation.json"); |
| 118 | return usage(); |
| 119 | } |
| 120 | if mode == "--subset" { |
| 121 | match corpus.subset(adapter, &table) { |
| 122 | Ok(names) => println!("{}", names.join("\n")), |
| 123 | Err(e) => return fail(e), |
| 124 | } |
| 125 | } else { |
| 126 | match corpus.plans(adapter, &table) { |
| 127 | Ok(plans) => print!("{}", dumps(&OJson::Arr(plans), Style::SORTED)), |
| 128 | Err(e) => return fail(e), |
| 129 | } |
| 130 | } |
| 131 | ExitCode::SUCCESS |
| 132 | } |