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 / crates / aien-drive / tests / equivalence.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Equivalence with the removed Python `scripts/aien_drive.py` (issue #179). |
| 17 | //! |
| 18 | //! `golden_from_python_original.json` was produced by running the original |
| 19 | //! Python `extract_tool_calls` and `dispatch_tool` (git history of |
| 20 | //! aien-sovereign-core before #182) on exactly these inputs. Results are |
| 21 | //! compared as parsed JSON (key order is not significant to the model). |
| 22 | //! The one allowed difference: error TEXT from the OS (Python prints |
| 23 | //! "[Errno 2] ...", Rust prints "No such file or directory (os error 2)"), |
| 24 | //! so for errors that come from the OS only the `error` key is compared. |
| 25 | |
| 26 | use aien_drive::{dispatch_tool, extract_tool_calls, CommandPolicy, Config}; |
| 27 | use serde_json::Value; |
| 28 | use std::time::Duration; |
| 29 | |
| 30 | fn cfg_at(root: &std::path::Path) -> Config { |
| 31 | Config { |
| 32 | endpoint: String::new(), |
| 33 | model: String::new(), |
| 34 | cortex_endpoint: "http://127.0.0.1:1".into(), |
| 35 | cortex_space: "aien-sources".into(), |
| 36 | request_timeout: Duration::from_secs(5), |
| 37 | command: CommandPolicy { |
| 38 | // The golden `run_command` cwd is this directory. It has to be |
| 39 | // the command root, or the root check rejects the call before |
| 40 | // the shell-metacharacter check runs. |
| 41 | root: root.to_path_buf(), |
| 42 | ..CommandPolicy::default() |
| 43 | }, |
| 44 | cortex_token: None, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn golden() -> Value { |
| 49 | serde_json::from_str(include_str!("golden_from_python_original.json")).unwrap() |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn extract_matches_python() { |
| 54 | let g = golden(); |
| 55 | let cases = g["extract"].as_array().unwrap(); |
| 56 | assert!(cases.len() >= 6); |
| 57 | for c in cases { |
| 58 | let text = c["text"].as_str().unwrap(); |
| 59 | let got = extract_tool_calls(text); |
| 60 | // Bare `{name, arguments}` objects are not a tool-call channel. |
| 61 | if text.starts_with("bare ") { |
| 62 | assert!( |
| 63 | got.is_empty(), |
| 64 | "bare JSON must not be executed: {got:?} from {text}" |
| 65 | ); |
| 66 | continue; |
| 67 | } |
| 68 | assert_eq!(Value::Array(got), c["calls"], "text: {text}"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn prepare(tmp: &std::path::Path) { |
| 73 | std::fs::create_dir_all(tmp.join("ls")).unwrap(); |
| 74 | std::fs::write(tmp.join("f.txt"), "one\ntwo\nthree\nfour\n").unwrap(); |
| 75 | std::fs::write(tmp.join("e.txt"), "aa bb aa\n").unwrap(); |
| 76 | for n in ["b", "a", "c"] { |
| 77 | std::fs::write(tmp.join("ls").join(n), "x").unwrap(); |
| 78 | } |
| 79 | std::fs::write(tmp.join("g.txt"), "needle here\nother\n").unwrap(); |
| 80 | } |
| 81 | |
| 82 | fn run_dispatch_cases(mutate_expected: impl Fn(&mut Value)) -> Vec<String> { |
| 83 | static N: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); |
| 84 | let n = N.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 85 | let tmp = std::env::temp_dir().join(format!("aien-drive-eq-{}-{n}", std::process::id())); |
| 86 | let _ = std::fs::remove_dir_all(&tmp); |
| 87 | prepare(&tmp); |
| 88 | let t = tmp.to_str().unwrap(); |
| 89 | let g = golden(); |
| 90 | let mut failures = vec![]; |
| 91 | for c in g["dispatch"].as_array().unwrap() { |
| 92 | let name = c["name"].as_str().unwrap(); |
| 93 | let args: Value = serde_json::from_str(&c["args"].to_string().replace("{TMP}", t)).unwrap(); |
| 94 | let got = dispatch_tool(&cfg_at(&tmp), name, &args); |
| 95 | let got: Value = serde_json::from_str(&got.to_string().replace(t, "{TMP}")).unwrap(); |
| 96 | // The Python golden ran these strings through a shell (`echo out; ...`, |
| 97 | // `$(seq ...)`). The cwd is `tmp`, which is the command root, so the |
| 98 | // rejection has to be the metacharacter check and not the root check. |
| 99 | // The historical shell result stays in the fixture. |
| 100 | if name == "run_command" { |
| 101 | let err = got.get("error").and_then(Value::as_str).unwrap_or(""); |
| 102 | if !err.contains("metacharacter") { |
| 103 | failures.push(format!( |
| 104 | "{name}: expected shell metacharacter rejection inside the command root, got {got}" |
| 105 | )); |
| 106 | } |
| 107 | continue; |
| 108 | } |
| 109 | let mut want = c["result"].clone(); |
| 110 | mutate_expected(&mut want); |
| 111 | let os_error = want["error"] |
| 112 | .as_str() |
| 113 | .is_some_and(|e| e.starts_with("[Errno")); |
| 114 | let ok = if os_error { |
| 115 | got.get("error").is_some() |
| 116 | } else { |
| 117 | got == want |
| 118 | }; |
| 119 | if !ok { |
| 120 | failures.push(format!("{name}: got {got} want {want}")); |
| 121 | } |
| 122 | } |
| 123 | let _ = std::fs::remove_dir_all(&tmp); |
| 124 | failures |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn dispatch_matches_python() { |
| 129 | let f = run_dispatch_cases(|_| {}); |
| 130 | assert!(f.is_empty(), "{f:#?}"); |
| 131 | } |
| 132 | |
| 133 | /// Negative control: corrupt one expected value and the comparison must fail. |
| 134 | #[test] |
| 135 | fn comparison_detects_a_difference() { |
| 136 | let f = run_dispatch_cases(|w| { |
| 137 | if w.get("milestone_done").is_some() { |
| 138 | w["milestone_done"] = Value::Bool(false); |
| 139 | } |
| 140 | }); |
| 141 | assert_eq!(f.len(), 1, "{f:#?}"); |
| 142 | } |