aien-harness
Autonomous agent verification and benchmark test harness
Brought by drakestapleton
aien-harness / src / main.rs
| 1 | use clap::{Parser, Subcommand}; |
| 2 | use serde_json::{json, Value}; |
| 3 | use spark_harness::{HarnessEngine, WorkflowState}; |
| 4 | use std::io::{self, BufRead, Write}; |
| 5 | use std::path::PathBuf; |
| 6 | |
| 7 | #[derive(Parser, Debug)] |
| 8 | #[command(name = "spark-harness", about = "Native Rust 5-Layer Change-Management Harness & MCP Server")] |
| 9 | struct Cli { |
| 10 | #[arg(long, default_value = "/home/drakestapleton/workspace/spark-harness-data")] |
| 11 | harness_path: PathBuf, |
| 12 | |
| 13 | #[command(subcommand)] |
| 14 | command: Option<Commands>, |
| 15 | } |
| 16 | |
| 17 | #[derive(Subcommand, Debug)] |
| 18 | enum Commands { |
| 19 | /// Run self-test across all 5 layers |
| 20 | Test, |
| 21 | /// Run as Stdio JSON-RPC MCP server |
| 22 | Mcp, |
| 23 | /// Validate JSON against a schema |
| 24 | Validate { |
| 25 | #[arg(long)] |
| 26 | schema: String, |
| 27 | #[arg(long)] |
| 28 | data: String, |
| 29 | }, |
| 30 | /// Evaluate content through a guardrail gate |
| 31 | Eval { |
| 32 | #[arg(long)] |
| 33 | gate: String, |
| 34 | #[arg(long)] |
| 35 | payload: String, |
| 36 | }, |
| 37 | } |
| 38 | |
| 39 | #[tokio::main] |
| 40 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 41 | let cli = Cli::parse(); |
| 42 | let engine = HarnessEngine::new(&cli.harness_path); |
| 43 | |
| 44 | match cli.command.unwrap_or(Commands::Test) { |
| 45 | Commands::Test => { |
| 46 | println!("=== Running spark-harness Native 5-Layer Verification Suite ==="); |
| 47 | |
| 48 | // 1. Slop Gate Eval Test |
| 49 | let clean_payload = json!({ "text": "Direct execution of native Rust binary on DGX Spark." }); |
| 50 | let slopped_payload = json!({ "text": "In today's fast-paced world, this is a game-changer to delve into." }); |
| 51 | |
| 52 | let res_clean = engine.run_eval("slop_gate", &clean_payload); |
| 53 | let res_slop = engine.run_eval("slop_gate", &slopped_payload); |
| 54 | |
| 55 | assert!(res_clean.passed, "Clean text should pass slop gate"); |
| 56 | assert!(!res_slop.passed, "Sloppy text must be rejected by slop gate"); |
| 57 | println!("Layer 2 (Slop Gate Eval): PASSED"); |
| 58 | |
| 59 | // 2. Evidence Requirement Eval Test |
| 60 | let no_evidence = json!({ "text": "Sample" }); |
| 61 | let has_evidence = json!({ "text": "Sample", "evidence": ["commit c9db3e2"] }); |
| 62 | assert!(!engine.run_eval("required_evidence", &no_evidence).passed); |
| 63 | assert!(engine.run_eval("required_evidence", &has_evidence).passed); |
| 64 | println!("Layer 2 (Required Evidence Eval): PASSED"); |
| 65 | |
| 66 | // 3. Workflow State Transition Test |
| 67 | let mut wf = WorkflowState { |
| 68 | id: "test-001".to_string(), |
| 69 | workflow_type: "engineering_fix".to_string(), |
| 70 | current_state: "reproduce".to_string(), |
| 71 | history: Vec::new(), |
| 72 | created_at: chrono::Utc::now().to_rfc3339(), |
| 73 | }; |
| 74 | |
| 75 | assert!(engine.advance_workflow(&mut wf, "regression_fail").is_ok()); |
| 76 | assert_eq!(wf.current_state, "regression_fail"); |
| 77 | assert!(engine.advance_workflow(&mut wf, "implement").is_ok()); |
| 78 | assert_eq!(wf.current_state, "implement"); |
| 79 | assert!(engine.advance_workflow(&mut wf, "done").is_err(), "Cannot jump directly to done"); |
| 80 | println!("Layer 4 (Deterministic Workflows): PASSED"); |
| 81 | |
| 82 | // 4. Failure Replay Record Test |
| 83 | let log_path = engine.record_failure("test-task-1", "RegressionTestFailed", &json!({"test": "assert_eq"}))?; |
| 84 | assert!(log_path.exists(), "Failure log should be created on disk"); |
| 85 | println!("Layer 5 (Failure Replay Recording): PASSED"); |
| 86 | |
| 87 | println!("\nAll 5-layer change management harness invariants verified in pure native Rust."); |
| 88 | } |
| 89 | |
| 90 | Commands::Validate { schema, data } => { |
| 91 | let parsed_data: Value = serde_json::from_str(&data)?; |
| 92 | match engine.validate_schema(&schema, &parsed_data) { |
| 93 | Ok(_) => println!("VALIDATION PASSED: Schema '{}'", schema), |
| 94 | Err(err) => { |
| 95 | eprintln!("VALIDATION FAILED: {}", err); |
| 96 | std::process::exit(1); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | Commands::Eval { gate, payload } => { |
| 102 | let parsed: Value = serde_json::from_str(&payload)?; |
| 103 | let result = engine.run_eval(&gate, &parsed); |
| 104 | println!("{}", serde_json::to_string_pretty(&result)?); |
| 105 | if !result.passed { |
| 106 | std::process::exit(1); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | Commands::Mcp => { |
| 111 | run_mcp_stdio_loop(engine).await?; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | Ok(()) |
| 116 | } |
| 117 | |
| 118 | async fn run_mcp_stdio_loop(engine: HarnessEngine) -> io::Result<()> { |
| 119 | let stdin = io::stdin(); |
| 120 | let mut stdout = io::stdout(); |
| 121 | |
| 122 | for line in stdin.lock().lines() { |
| 123 | let line = line?; |
| 124 | if line.trim().is_empty() { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | let req: Value = match serde_json::from_str(&line) { |
| 129 | Ok(v) => v, |
| 130 | Err(_) => continue, |
| 131 | }; |
| 132 | |
| 133 | let req_id = req.get("id").cloned().unwrap_or(Value::Null); |
| 134 | let method = req.get("method").and_then(|m| m.as_str()).unwrap_or(""); |
| 135 | |
| 136 | let resp = match method { |
| 137 | "initialize" => json!({ |
| 138 | "jsonrpc": "2.0", |
| 139 | "id": req_id, |
| 140 | "result": { |
| 141 | "protocolVersion": "2024-11-05", |
| 142 | "serverInfo": { |
| 143 | "name": "spark-harness-rs", |
| 144 | "version": "0.1.0" |
| 145 | }, |
| 146 | "capabilities": { |
| 147 | "tools": {} |
| 148 | } |
| 149 | } |
| 150 | }), |
| 151 | |
| 152 | "tools/list" => json!({ |
| 153 | "jsonrpc": "2.0", |
| 154 | "id": req_id, |
| 155 | "result": { |
| 156 | "tools": [ |
| 157 | { |
| 158 | "name": "harness_eval", |
| 159 | "description": "Run an artifact or text through change-management guardrail evals (slop_gate, required_evidence)", |
| 160 | "inputSchema": { |
| 161 | "type": "object", |
| 162 | "properties": { |
| 163 | "gate": { "type": "string" }, |
| 164 | "payload": { "type": "object" } |
| 165 | }, |
| 166 | "required": ["gate", "payload"] |
| 167 | } |
| 168 | }, |
| 169 | { |
| 170 | "name": "harness_validate", |
| 171 | "description": "Validate JSON object against registered harness schemas (engineering_fix, research_handoff, harness_change)", |
| 172 | "inputSchema": { |
| 173 | "type": "object", |
| 174 | "properties": { |
| 175 | "schema": { "type": "string" }, |
| 176 | "data": { "type": "object" } |
| 177 | }, |
| 178 | "required": ["schema", "data"] |
| 179 | } |
| 180 | } |
| 181 | ] |
| 182 | } |
| 183 | }), |
| 184 | |
| 185 | "tools/call" => { |
| 186 | let params = req.get("params").cloned().unwrap_or(json!({})); |
| 187 | let name = params.get("name").and_then(|n| n.as_str()).unwrap_or(""); |
| 188 | let args = params.get("arguments").cloned().unwrap_or(json!({})); |
| 189 | |
| 190 | match name { |
| 191 | "harness_eval" => { |
| 192 | let gate = args.get("gate").and_then(|g| g.as_str()).unwrap_or(""); |
| 193 | let payload = args.get("payload").cloned().unwrap_or(json!({})); |
| 194 | let eval_res = engine.run_eval(gate, &payload); |
| 195 | json!({ |
| 196 | "jsonrpc": "2.0", |
| 197 | "id": req_id, |
| 198 | "result": { |
| 199 | "content": [ |
| 200 | { "type": "text", "text": serde_json::to_string(&eval_res).unwrap() } |
| 201 | ] |
| 202 | } |
| 203 | }) |
| 204 | } |
| 205 | |
| 206 | "harness_validate" => { |
| 207 | let schema = args.get("schema").and_then(|s| s.as_str()).unwrap_or(""); |
| 208 | let data = args.get("data").cloned().unwrap_or(json!({})); |
| 209 | match engine.validate_schema(schema, &data) { |
| 210 | Ok(_) => json!({ |
| 211 | "jsonrpc": "2.0", |
| 212 | "id": req_id, |
| 213 | "result": { |
| 214 | "content": [{ "type": "text", "text": "Validation passed." }] |
| 215 | } |
| 216 | }), |
| 217 | Err(e) => json!({ |
| 218 | "jsonrpc": "2.0", |
| 219 | "id": req_id, |
| 220 | "result": { |
| 221 | "content": [{ "type": "text", "text": format!("Validation failed: {}", e) }], |
| 222 | "isError": true |
| 223 | } |
| 224 | }), |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | _ => json!({ |
| 229 | "jsonrpc": "2.0", |
| 230 | "id": req_id, |
| 231 | "error": { "code": -32601, "message": "Method or tool not found" } |
| 232 | }) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | _ => json!({ |
| 237 | "jsonrpc": "2.0", |
| 238 | "id": req_id, |
| 239 | "result": {} |
| 240 | }), |
| 241 | }; |
| 242 | |
| 243 | let out = serde_json::to_string(&resp).unwrap(); |
| 244 | writeln!(stdout, "{}", out)?; |
| 245 | stdout.flush()?; |
| 246 | } |
| 247 | |
| 248 | Ok(()) |
| 249 | } |