aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / tools / aien-test / src / bin / aien-audit-inputs.rs
| 1 | //! aien-audit-inputs: run one gate under strace and list every file it read |
| 2 | //! that its manifest does not declare (see `aien_test::audit`). |
| 3 | //! |
| 4 | //! ```text |
| 5 | //! aien-audit-inputs [--root DIR] --target-dir DIR --work DIR GATE_FILE |
| 6 | //! aien-audit-inputs [--root DIR] --target-dir DIR --work DIR --trace FILE GATE_FILE |
| 7 | //! ``` |
| 8 | //! The first form runs `run.exec` with the manifest's args from the |
| 9 | //! repository root, with `CARGO_TARGET_DIR=--target-dir` (use a cold one) and |
| 10 | //! `TMPDIR=<work>/tmp`, and keeps the trace in `<work>/strace.txt`. The second |
| 11 | //! judges a trace made earlier with the same layout. Exit 0: inputs complete; |
| 12 | //! 1: something read is not declared; 2: usage or setup error. |
| 13 | use aien_test::audit::{self, Places}; |
| 14 | use aien_test::manifest; |
| 15 | use std::path::PathBuf; |
| 16 | use std::process::{exit, Command}; |
| 17 | |
| 18 | fn main() { |
| 19 | let mut root = PathBuf::from("."); |
| 20 | let (mut target, mut work, mut trace, mut gate): ( |
| 21 | Option<PathBuf>, |
| 22 | Option<PathBuf>, |
| 23 | Option<PathBuf>, |
| 24 | Option<PathBuf>, |
| 25 | ) = (None, None, None, None); |
| 26 | let mut siblings: Vec<PathBuf> = Vec::new(); |
| 27 | let mut as_rel: Option<String> = None; |
| 28 | let mut it = std::env::args().skip(1); |
| 29 | while let Some(a) = it.next() { |
| 30 | let mut val = || { |
| 31 | it.next() |
| 32 | .map(PathBuf::from) |
| 33 | .unwrap_or_else(|| usage(&format!("{a} needs a value"))) |
| 34 | }; |
| 35 | match a.as_str() { |
| 36 | "--root" => root = val(), |
| 37 | "--target-dir" => target = Some(val()), |
| 38 | "--work" => work = Some(val()), |
| 39 | "--trace" => trace = Some(val()), |
| 40 | "--sibling" => siblings.push(audit::canon(&val())), |
| 41 | "--manifest-rel" => as_rel = Some(val().to_string_lossy().into_owned()), |
| 42 | s if s.starts_with('-') => usage(&format!("unknown flag {s}")), |
| 43 | _ => gate = Some(PathBuf::from(&a)), |
| 44 | } |
| 45 | } |
| 46 | let (Some(target), Some(work), Some(gate)) = (target, work, gate) else { |
| 47 | usage("missing --target-dir, --work or GATE_FILE") |
| 48 | }; |
| 49 | let root = audit::canon(&root); |
| 50 | let gate_path = if gate.is_absolute() { |
| 51 | gate.clone() |
| 52 | } else { |
| 53 | root.join(&gate) |
| 54 | }; |
| 55 | let bytes = std::fs::read(&gate_path) |
| 56 | .unwrap_or_else(|e| fail(&format!("cannot read {}: {e}", gate_path.display()))); |
| 57 | let m = manifest::parse(&bytes).unwrap_or_else(|e| fail(&format!("bad manifest: {e:?}"))); |
| 58 | // `--manifest-rel REL`: judge an earlier trace with a manifest kept |
| 59 | // elsewhere (a later edit of the same gate), named as the repository path REL. |
| 60 | let manifest_rel = match &as_rel { |
| 61 | Some(r) => r.clone(), |
| 62 | None => audit::canon(&gate_path) |
| 63 | .strip_prefix(&root) |
| 64 | .map(|p| p.to_string_lossy().into_owned()) |
| 65 | .unwrap_or_else(|_| fail("the gate file is not inside --root")), |
| 66 | }; |
| 67 | // The tree at HEAD: what is not in it was made by the run. A dirty tree |
| 68 | // is judged anyway (its own files count as made by the run), with a warning. |
| 69 | let ls = Command::new("git") |
| 70 | .args(["ls-files", "-z"]) |
| 71 | .current_dir(&root) |
| 72 | .output() |
| 73 | .unwrap_or_else(|e| fail(&format!("git ls-files: {e}"))); |
| 74 | let tracked: std::collections::BTreeSet<String> = String::from_utf8_lossy(&ls.stdout) |
| 75 | .split('\0') |
| 76 | .filter(|s| !s.is_empty()) |
| 77 | .map(str::to_string) |
| 78 | .collect(); |
| 79 | let dirty = Command::new("git") |
| 80 | .args(["status", "--porcelain"]) |
| 81 | .current_dir(&root) |
| 82 | .output() |
| 83 | .map(|o| !o.stdout.is_empty()) |
| 84 | .unwrap_or(true); |
| 85 | if dirty { |
| 86 | println!("WARNING the tree has uncommitted changes; audit a clean checkout for the record"); |
| 87 | } |
| 88 | let tmpdir = work.join("tmp"); |
| 89 | std::fs::create_dir_all(&tmpdir) |
| 90 | .unwrap_or_else(|e| fail(&format!("cannot make {}: {e}", tmpdir.display()))); |
| 91 | std::fs::create_dir_all(&target) |
| 92 | .unwrap_or_else(|e| fail(&format!("cannot make {}: {e}", target.display()))); |
| 93 | let trace_path = match trace { |
| 94 | Some(t) => t, |
| 95 | None => { |
| 96 | let t = work.join("strace.txt"); |
| 97 | let status = Command::new("strace") |
| 98 | .args([ |
| 99 | "-f", |
| 100 | "-qq", |
| 101 | "-Y", |
| 102 | "-y", |
| 103 | "-e", |
| 104 | "trace=open,openat,openat2,execve", |
| 105 | "-e", |
| 106 | "status=successful", |
| 107 | "-o", |
| 108 | ]) |
| 109 | .arg(&t) |
| 110 | .arg(root.join(&m.exec)) |
| 111 | .args(&m.args) |
| 112 | .current_dir(&root) |
| 113 | .env("TMPDIR", &tmpdir) |
| 114 | .env("CARGO_TARGET_DIR", &target) |
| 115 | .status() |
| 116 | .unwrap_or_else(|e| fail(&format!("cannot start strace: {e}"))); |
| 117 | println!( |
| 118 | "GATE_EXIT {}", |
| 119 | status |
| 120 | .code() |
| 121 | .map(|c| c.to_string()) |
| 122 | .unwrap_or_else(|| "signal".into()) |
| 123 | ); |
| 124 | t |
| 125 | } |
| 126 | }; |
| 127 | let text = std::fs::read_to_string(&trace_path) |
| 128 | .unwrap_or_else(|e| fail(&format!("cannot read {}: {e}", trace_path.display()))); |
| 129 | let places = Places { |
| 130 | root, |
| 131 | manifest_rel, |
| 132 | target_dir: audit::canon(&target), |
| 133 | tmpdir: audit::canon(&tmpdir), |
| 134 | home: std::env::var_os("HOME") |
| 135 | .map(PathBuf::from) |
| 136 | .unwrap_or_default(), |
| 137 | tracked: Some(tracked), |
| 138 | siblings, |
| 139 | }; |
| 140 | let report = audit::judge(&m, &places, &audit::parse_strace(&text)); |
| 141 | let out = audit::render(&m, &report); |
| 142 | let _ = std::fs::write(work.join(format!("audit-{}.txt", m.gate)), &out); |
| 143 | // Every repository file the run read, declared or not: the closure to |
| 144 | // narrow `inputs` to. |
| 145 | let reads: Vec<&str> = report |
| 146 | .covered |
| 147 | .iter() |
| 148 | .map(String::as_str) |
| 149 | .chain(report.undeclared.keys().map(String::as_str)) |
| 150 | .collect(); |
| 151 | let mut reads = reads; |
| 152 | reads.sort(); |
| 153 | let _ = std::fs::write( |
| 154 | work.join(format!("reads-{}.txt", m.gate)), |
| 155 | reads.join("\n") + "\n", |
| 156 | ); |
| 157 | print!("{out}"); |
| 158 | exit(if report.ok(&m) { 0 } else { 1 }); |
| 159 | } |
| 160 | |
| 161 | fn usage(msg: &str) -> ! { |
| 162 | eprintln!("aien-audit-inputs: {msg}\nusage: aien-audit-inputs [--root DIR] --target-dir DIR --work DIR [--trace FILE] [--sibling DIR]... [--manifest-rel REL] GATE_FILE"); |
| 163 | exit(2) |
| 164 | } |
| 165 | |
| 166 | fn fail(msg: &str) -> ! { |
| 167 | eprintln!("aien-audit-inputs: {msg}"); |
| 168 | exit(2) |
| 169 | } |