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-proof / src / workspace.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 | //! Split a cargo workspace into one job per member crate. Each job's inputs are |
| 17 | //! the crate, every local crate it depends on (transitively, dev-dependencies |
| 18 | //! included), and the root manifest and lockfile, so a stamp goes stale exactly |
| 19 | //! when something the crate's tests can see changes. |
| 20 | |
| 21 | use serde_json::Value; |
| 22 | use std::collections::{BTreeMap, BTreeSet}; |
| 23 | use std::io; |
| 24 | use std::path::{Path, PathBuf}; |
| 25 | use std::process::Command; |
| 26 | |
| 27 | pub struct Member { |
| 28 | pub name: String, |
| 29 | pub inputs: Vec<PathBuf>, |
| 30 | pub gpu: bool, |
| 31 | } |
| 32 | |
| 33 | fn rel(root: &Path, p: &Path) -> PathBuf { |
| 34 | p.strip_prefix(root) |
| 35 | .map(Path::to_path_buf) |
| 36 | .unwrap_or_else(|_| p.to_path_buf()) |
| 37 | } |
| 38 | |
| 39 | /// Returns the workspace root and its members, sorted by name. |
| 40 | pub fn members(start: &Path) -> io::Result<(PathBuf, Vec<Member>)> { |
| 41 | let out = Command::new("cargo") |
| 42 | .args(["metadata", "--format-version", "1", "--no-deps"]) |
| 43 | .current_dir(start) |
| 44 | .output()?; |
| 45 | if !out.status.success() { |
| 46 | return Err(io::Error::other( |
| 47 | String::from_utf8_lossy(&out.stderr).into_owned(), |
| 48 | )); |
| 49 | } |
| 50 | let meta: Value = serde_json::from_slice(&out.stdout)?; |
| 51 | parse(&meta) |
| 52 | } |
| 53 | |
| 54 | pub fn parse(meta: &Value) -> io::Result<(PathBuf, Vec<Member>)> { |
| 55 | let root = PathBuf::from( |
| 56 | meta["workspace_root"] |
| 57 | .as_str() |
| 58 | .ok_or_else(|| io::Error::other("no workspace_root"))?, |
| 59 | ); |
| 60 | let ids: BTreeSet<&str> = meta["workspace_members"] |
| 61 | .as_array() |
| 62 | .map(|a| a.iter().filter_map(Value::as_str).collect()) |
| 63 | .unwrap_or_default(); |
| 64 | |
| 65 | // crate dir (relative) -> (name, local dependency dirs, gpu flag) |
| 66 | let mut crates: BTreeMap<PathBuf, (String, Vec<PathBuf>, bool)> = BTreeMap::new(); |
| 67 | for pkg in meta["packages"].as_array().into_iter().flatten() { |
| 68 | if !pkg["id"].as_str().is_some_and(|id| ids.contains(id)) { |
| 69 | continue; |
| 70 | } |
| 71 | let manifest = PathBuf::from(pkg["manifest_path"].as_str().unwrap_or_default()); |
| 72 | let dir = rel(&root, manifest.parent().unwrap_or(&root)); |
| 73 | let deps = pkg["dependencies"] |
| 74 | .as_array() |
| 75 | .into_iter() |
| 76 | .flatten() |
| 77 | .filter_map(|d| d["path"].as_str()) |
| 78 | .map(|p| rel(&root, Path::new(p))) |
| 79 | .collect(); |
| 80 | let gpu = pkg["metadata"]["aien-proof"]["gpu"] |
| 81 | .as_bool() |
| 82 | .unwrap_or(false); |
| 83 | crates.insert( |
| 84 | dir, |
| 85 | ( |
| 86 | pkg["name"].as_str().unwrap_or_default().to_string(), |
| 87 | deps, |
| 88 | gpu, |
| 89 | ), |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | let mut shared = vec![PathBuf::from("Cargo.toml"), PathBuf::from("Cargo.lock")]; |
| 94 | if root.join(".cargo").is_dir() { |
| 95 | shared.push(PathBuf::from(".cargo")); |
| 96 | } |
| 97 | shared.retain(|p| root.join(p).exists()); |
| 98 | |
| 99 | let mut members = Vec::new(); |
| 100 | for (dir, (name, _, gpu)) in &crates { |
| 101 | let mut seen = BTreeSet::new(); |
| 102 | let mut stack = vec![dir.clone()]; |
| 103 | while let Some(d) = stack.pop() { |
| 104 | if !seen.insert(d.clone()) { |
| 105 | continue; |
| 106 | } |
| 107 | if let Some((_, deps, _)) = crates.get(&d) { |
| 108 | stack.extend(deps.iter().cloned()); |
| 109 | } |
| 110 | } |
| 111 | let mut inputs: Vec<PathBuf> = seen.into_iter().collect(); |
| 112 | inputs.extend(shared.iter().cloned()); |
| 113 | members.push(Member { |
| 114 | name: name.clone(), |
| 115 | inputs, |
| 116 | gpu: *gpu, |
| 117 | }); |
| 118 | } |
| 119 | members.sort_by(|a, b| a.name.cmp(&b.name)); |
| 120 | Ok((root, members)) |
| 121 | } |
| 122 | |
| 123 | #[cfg(test)] |
| 124 | mod tests { |
| 125 | use super::*; |
| 126 | use serde_json::json; |
| 127 | |
| 128 | #[test] |
| 129 | fn inputs_follow_local_dependencies_transitively() { |
| 130 | let meta = json!({ |
| 131 | "workspace_root": "/ws", |
| 132 | "workspace_members": ["a", "b", "c"], |
| 133 | "packages": [ |
| 134 | {"id": "a", "name": "a", "manifest_path": "/ws/crates/a/Cargo.toml", |
| 135 | "dependencies": [{"name": "b", "path": "/ws/crates/b"}, {"name": "serde"}], "metadata": null}, |
| 136 | {"id": "b", "name": "b", "manifest_path": "/ws/crates/b/Cargo.toml", |
| 137 | "dependencies": [{"name": "c", "path": "/ws/crates/c"}], "metadata": null}, |
| 138 | {"id": "c", "name": "c", "manifest_path": "/ws/crates/c/Cargo.toml", |
| 139 | "dependencies": [], "metadata": {"aien-proof": {"gpu": true}}} |
| 140 | ] |
| 141 | }); |
| 142 | let (_, members) = parse(&meta).unwrap(); |
| 143 | let a = members.iter().find(|m| m.name == "a").unwrap(); |
| 144 | let dirs: Vec<_> = a |
| 145 | .inputs |
| 146 | .iter() |
| 147 | .map(|p| p.to_string_lossy().into_owned()) |
| 148 | .collect(); |
| 149 | assert!(dirs.contains(&"crates/a".into())); |
| 150 | assert!(dirs.contains(&"crates/b".into())); |
| 151 | assert!(dirs.contains(&"crates/c".into())); |
| 152 | let c = members.iter().find(|m| m.name == "c").unwrap(); |
| 153 | assert!(c.gpu); |
| 154 | assert_eq!( |
| 155 | c.inputs.iter().filter(|p| p.starts_with("crates")).count(), |
| 156 | 1 |
| 157 | ); |
| 158 | } |
| 159 | } |