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 / spark-harness / src / main.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 | fn default_harness_path() -> PathBuf { |
| 17 | if std::path::Path::new("schemas").is_dir() && std::path::Path::new("evals.json").is_file() { |
| 18 | return PathBuf::from("."); |
| 19 | } |
| 20 | let home = std::env::var("HOME") |
| 21 | .or_else(|_| std::env::var("USERPROFILE")) |
| 22 | .unwrap_or_else(|_| ".".to_string()); |
| 23 | PathBuf::from(home).join("workspace/spark-harness-data") |
| 24 | } |
| 25 | |
| 26 | use clap::{Parser, Subcommand}; |
| 27 | use serde_json::{json, Value}; |
| 28 | use spark_harness::completion::CompletionSubmission; |
| 29 | use spark_harness::{HarnessEngine, WorkflowState}; |
| 30 | use std::io::{self, BufRead, Write}; |
| 31 | use std::path::PathBuf; |
| 32 | |
| 33 | #[derive(Parser, Debug)] |
| 34 | #[command( |
| 35 | name = "spark-harness", |
| 36 | about = "Native Rust 5-Layer Change-Management Harness & MCP Server" |
| 37 | )] |
| 38 | struct Cli { |
| 39 | #[arg(long, default_value_os_t = default_harness_path())] |
| 40 | harness_path: PathBuf, |
| 41 | |
| 42 | #[command(subcommand)] |
| 43 | command: Option<Commands>, |
| 44 | } |
| 45 | |
| 46 | #[derive(Subcommand, Debug)] |
| 47 | enum Commands { |
| 48 | /// Run self-test across all 5 layers |
| 49 | Test, |
| 50 | /// Run as Stdio JSON-RPC MCP server |
| 51 | Mcp, |
| 52 | /// Validate JSON against a schema |
| 53 | Validate { |
| 54 | #[arg(long)] |
| 55 | schema: String, |
| 56 | #[arg(long)] |
| 57 | data: String, |
| 58 | }, |
| 59 | /// Evaluate content through a guardrail gate |
| 60 | Eval { |
| 61 | #[arg(long)] |
| 62 | gate: String, |
| 63 | #[arg(long)] |
| 64 | payload: String, |
| 65 | }, |
| 66 | } |
| 67 | |
| 68 | #[tokio::main] |
| 69 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 70 | let cli = Cli::parse(); |
| 71 | let engine = HarnessEngine::new(&cli.harness_path); |
| 72 | |
| 73 | match cli.command.unwrap_or(Commands::Test) { |
| 74 | Commands::Test => { |
| 75 | println!("=== Running spark-harness Native 5-Layer Verification Suite ==="); |
| 76 | |
| 77 | // 1. Slop Gate Eval Test |
| 78 | let clean_payload = |
| 79 | json!({ "text": "Direct execution of native Rust binary on DGX Spark." }); |
| 80 | let slopped_payload = json!({ "text": "In today's fast-paced world, this is a game-changer to delve into." }); |
| 81 | |
| 82 | let res_clean = engine.run_eval("slop_gate", &clean_payload); |
| 83 | let res_slop = engine.run_eval("slop_gate", &slopped_payload); |
| 84 | |
| 85 | assert!(res_clean.passed, "Clean text should pass slop gate"); |
| 86 | assert!( |
| 87 | !res_slop.passed, |
| 88 | "Sloppy text must be rejected by slop gate" |
| 89 | ); |
| 90 | println!("Layer 2 (Slop Gate Eval): PASSED"); |
| 91 | |
| 92 | // 2. Evidence Requirement Eval Test |
| 93 | let no_evidence = json!({ "text": "Sample" }); |
| 94 | let has_evidence = json!({ "text": "Sample", "evidence": ["commit c9db3e2"] }); |
| 95 | assert!(!engine.run_eval("required_evidence", &no_evidence).passed); |
| 96 | assert!(engine.run_eval("required_evidence", &has_evidence).passed); |
| 97 | println!("Layer 2 (Required Evidence Eval): PASSED"); |
| 98 | |
| 99 | // 3. Workflow State Transition Test |
| 100 | let mut wf = WorkflowState { |
| 101 | id: "test-001".to_string(), |
| 102 | workflow_type: "engineering_fix".to_string(), |
| 103 | current_state: "reproduce".to_string(), |
| 104 | history: Vec::new(), |
| 105 | created_at: chrono::Utc::now().to_rfc3339(), |
| 106 | }; |
| 107 | |
| 108 | assert!(engine.advance_workflow(&mut wf, "regression_fail").is_ok()); |
| 109 | assert_eq!(wf.current_state, "regression_fail"); |
| 110 | assert!(engine.advance_workflow(&mut wf, "implement").is_ok()); |
| 111 | assert_eq!(wf.current_state, "implement"); |
| 112 | assert!( |
| 113 | engine.advance_workflow(&mut wf, "done").is_err(), |
| 114 | "Cannot jump directly to done" |
| 115 | ); |
| 116 | assert!(engine.advance_workflow(&mut wf, "retest").is_ok()); |
| 117 | assert!(engine.advance_workflow(&mut wf, "review").is_ok()); |
| 118 | assert!( |
| 119 | engine.advance_workflow(&mut wf, "done").is_err(), |
| 120 | "done is reachable only through verified completion" |
| 121 | ); |
| 122 | assert_eq!(wf.current_state, "review"); |
| 123 | println!("Layer 4 (Deterministic Workflows): PASSED"); |
| 124 | |
| 125 | // 3b. Verified completion into a throwaway store. |
| 126 | let mut gated = engine.clone(); |
| 127 | gated.completion_store = std::env::temp_dir() |
| 128 | .join(format!("spark-harness-selftest-{}", uuid::Uuid::new_v4())); |
| 129 | let sub = CompletionSubmission { |
| 130 | task_id: "selftest-task".to_string(), |
| 131 | generation: 1, |
| 132 | source_commit: "0".repeat(40), |
| 133 | artifact: json!({ |
| 134 | "bug": "Parser rejects valid token sequence", |
| 135 | "reproduction_steps": ["Run parser test"], |
| 136 | "failure_evidence": "thread panicked at line 42", |
| 137 | "regression_test": "tests::test_parser", |
| 138 | "diff_summary": "Fixed token parser logic", |
| 139 | "retest_evidence": "All tests passing cleanly" |
| 140 | }), |
| 141 | }; |
| 142 | let outcome = gated.complete_verified_workflow(&mut wf, &sub); |
| 143 | let _ = std::fs::remove_dir_all(&gated.completion_store); |
| 144 | let record = outcome?; |
| 145 | assert_eq!(wf.current_state, "done"); |
| 146 | assert_eq!(record.receipt_id.len(), 64); |
| 147 | println!("Layer 4b (Verified Completion Gate): PASSED"); |
| 148 | |
| 149 | // 4. Failure Replay Record Test |
| 150 | let log_path = engine.record_failure( |
| 151 | "test-task-1", |
| 152 | "RegressionTestFailed", |
| 153 | &json!({"test": "assert_eq"}), |
| 154 | )?; |
| 155 | assert!(log_path.exists(), "Failure log should be created on disk"); |
| 156 | println!("Layer 5 (Failure Replay Recording): PASSED"); |
| 157 | |
| 158 | println!( |
| 159 | "\nAll 5-layer change management harness invariants verified in pure native Rust." |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | Commands::Validate { schema, data } => { |
| 164 | let parsed_data: Value = serde_json::from_str(&data)?; |
| 165 | match engine.validate_schema(&schema, &parsed_data) { |
| 166 | Ok(_) => println!("VALIDATION PASSED: Schema '{}'", schema), |
| 167 | Err(err) => { |
| 168 | eprintln!("VALIDATION FAILED: {}", err); |
| 169 | std::process::exit(1); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | Commands::Eval { gate, payload } => { |
| 175 | let parsed: Value = serde_json::from_str(&payload)?; |
| 176 | let result = engine.run_eval(&gate, &parsed); |
| 177 | println!("{}", serde_json::to_string_pretty(&result)?); |
| 178 | if !result.passed { |
| 179 | std::process::exit(1); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | Commands::Mcp => { |
| 184 | run_mcp_stdio_loop(engine).await?; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | Ok(()) |
| 189 | } |
| 190 | |
| 191 | async fn run_mcp_stdio_loop(engine: HarnessEngine) -> io::Result<()> { |
| 192 | let stdin = io::stdin(); |
| 193 | let mut stdout = io::stdout(); |
| 194 | |
| 195 | for line in stdin.lock().lines() { |
| 196 | let line = line?; |
| 197 | if line.trim().is_empty() { |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | let req: Value = match serde_json::from_str(&line) { |
| 202 | Ok(v) => v, |
| 203 | Err(_) => continue, |
| 204 | }; |
| 205 | |
| 206 | let req_id = req.get("id").cloned().unwrap_or(Value::Null); |
| 207 | let method = req.get("method").and_then(|m| m.as_str()).unwrap_or(""); |
| 208 | |
| 209 | let resp = match method { |
| 210 | "initialize" => json!({ |
| 211 | "jsonrpc": "2.0", |
| 212 | "id": req_id, |
| 213 | "result": { |
| 214 | "protocolVersion": "2024-11-05", |
| 215 | "serverInfo": { |
| 216 | "name": "spark-harness-rs", |
| 217 | "version": "0.1.0" |
| 218 | }, |
| 219 | "capabilities": { |
| 220 | "tools": {} |
| 221 | } |
| 222 | } |
| 223 | }), |
| 224 | |
| 225 | "tools/list" => json!({ |
| 226 | "jsonrpc": "2.0", |
| 227 | "id": req_id, |
| 228 | "result": { |
| 229 | "tools": [ |
| 230 | { |
| 231 | "name": "harness_eval", |
| 232 | "description": "Run an artifact or text through change-management guardrail evals (slop_gate, required_evidence)", |
| 233 | "inputSchema": { |
| 234 | "type": "object", |
| 235 | "properties": { |
| 236 | "gate": { "type": "string" }, |
| 237 | "payload": { "type": "object" } |
| 238 | }, |
| 239 | "required": ["gate", "payload"] |
| 240 | } |
| 241 | }, |
| 242 | { |
| 243 | "name": "harness_validate", |
| 244 | "description": "Validate JSON object against registered harness schemas (engineering_fix, research_handoff, harness_change)", |
| 245 | "inputSchema": { |
| 246 | "type": "object", |
| 247 | "properties": { |
| 248 | "schema": { "type": "string" }, |
| 249 | "data": { "type": "object" } |
| 250 | }, |
| 251 | "required": ["schema", "data"] |
| 252 | } |
| 253 | }, |
| 254 | { |
| 255 | "name": "harness_complete", |
| 256 | "description": "Verified completion: runs the workflow's required schema and completion evals on the artifact, stores a harness-minted EvidenceReceiptV1, and only then moves the workflow to done. Refusals are JSON-RPC errors. done does not mean committed.", |
| 257 | "inputSchema": { |
| 258 | "type": "object", |
| 259 | "properties": { |
| 260 | "workflow": { |
| 261 | "type": "object", |
| 262 | "properties": { |
| 263 | "id": { "type": "string" }, |
| 264 | "workflow_type": { "type": "string" }, |
| 265 | "current_state": { "type": "string" }, |
| 266 | "history": { "type": "array", "items": { "type": "string" } }, |
| 267 | "created_at": { "type": "string" } |
| 268 | }, |
| 269 | "required": ["id", "workflow_type", "current_state", "history", "created_at"] |
| 270 | }, |
| 271 | "submission": { |
| 272 | "type": "object", |
| 273 | "properties": { |
| 274 | "task_id": { "type": "string" }, |
| 275 | "generation": { "type": "integer", "minimum": 0 }, |
| 276 | "source_commit": { "type": "string" }, |
| 277 | "artifact": { "type": "object" } |
| 278 | }, |
| 279 | "required": ["task_id", "generation", "source_commit", "artifact"], |
| 280 | "additionalProperties": false |
| 281 | } |
| 282 | }, |
| 283 | "required": ["workflow", "submission"], |
| 284 | "additionalProperties": false |
| 285 | } |
| 286 | } |
| 287 | ] |
| 288 | } |
| 289 | }), |
| 290 | |
| 291 | "tools/call" => { |
| 292 | let params = req.get("params").cloned().unwrap_or(json!({})); |
| 293 | let name = params.get("name").and_then(|n| n.as_str()).unwrap_or(""); |
| 294 | let args = params.get("arguments").cloned().unwrap_or(json!({})); |
| 295 | |
| 296 | match name { |
| 297 | "harness_eval" => { |
| 298 | let gate = args.get("gate").and_then(|g| g.as_str()).unwrap_or(""); |
| 299 | let payload = args.get("payload").cloned().unwrap_or(json!({})); |
| 300 | let eval_res = engine.run_eval(gate, &payload); |
| 301 | json!({ |
| 302 | "jsonrpc": "2.0", |
| 303 | "id": req_id, |
| 304 | "result": { |
| 305 | "content": [ |
| 306 | { "type": "text", "text": serde_json::to_string(&eval_res).unwrap() } |
| 307 | ] |
| 308 | } |
| 309 | }) |
| 310 | } |
| 311 | |
| 312 | "harness_validate" => { |
| 313 | let schema = args.get("schema").and_then(|s| s.as_str()).unwrap_or(""); |
| 314 | let data = args.get("data").cloned().unwrap_or(json!({})); |
| 315 | match engine.validate_schema(schema, &data) { |
| 316 | Ok(_) => json!({ |
| 317 | "jsonrpc": "2.0", |
| 318 | "id": req_id, |
| 319 | "result": { |
| 320 | "content": [{ "type": "text", "text": "Validation passed." }] |
| 321 | } |
| 322 | }), |
| 323 | Err(e) => json!({ |
| 324 | "jsonrpc": "2.0", |
| 325 | "id": req_id, |
| 326 | "result": { |
| 327 | "content": [{ "type": "text", "text": format!("Validation failed: {}", e) }], |
| 328 | "isError": true |
| 329 | } |
| 330 | }), |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | "harness_complete" => match engine.handle_complete_request(&args) { |
| 335 | Ok(done) => json!({ |
| 336 | "jsonrpc": "2.0", |
| 337 | "id": req_id, |
| 338 | "result": { |
| 339 | "content": [ |
| 340 | { "type": "text", "text": done.to_string() } |
| 341 | ] |
| 342 | } |
| 343 | }), |
| 344 | Err(e) => json!({ |
| 345 | "jsonrpc": "2.0", |
| 346 | "id": req_id, |
| 347 | "error": e.rpc_error() |
| 348 | }), |
| 349 | }, |
| 350 | |
| 351 | _ => json!({ |
| 352 | "jsonrpc": "2.0", |
| 353 | "id": req_id, |
| 354 | "error": { "code": -32601, "message": "Method or tool not found" } |
| 355 | }), |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | _ => json!({ |
| 360 | "jsonrpc": "2.0", |
| 361 | "id": req_id, |
| 362 | "result": {} |
| 363 | }), |
| 364 | }; |
| 365 | |
| 366 | let out = serde_json::to_string(&resp).unwrap(); |
| 367 | writeln!(stdout, "{}", out)?; |
| 368 | stdout.flush()?; |
| 369 | } |
| 370 | |
| 371 | Ok(()) |
| 372 | } |