Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/spark-rsi-mv/blob/main/tests/test_cli_v2_receipts.rs # spark-rsi-mv Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `spark-rsi-mv / tests / test_cli_v2_receipts.rs` 256 lines ยท 8231 bytes Raw Blame History 1 //! The CLI path writes version 2 receipts (issue #35). Pre-registered rows: 2 //! 3 //! 1. `spark-rsi judge` with `--policy-file` and `--subject-path` prints a version 2 receipt bound 4 //! to that subject, policy and holdout set, and the promotion gate accepts it. 5 //! 2. The policy may come from `RSI_JUDGE_POLICY_FILE` instead: configured once, version 2 is the 6 //! default for every run. 7 //! 3. A malformed policy file is refused: non-zero exit, the error names the policy, and no 8 //! receipt is printed. It is never ignored in favour of a version 1 receipt. 9 //! 4. A policy without a subject is refused rather than downgraded to version 1. 10 //! 5. `cycle` and `daemon` refuse a malformed judge policy or judge public key at startup, 11 //! before any cycle runs. 12 13 use p256::ecdsa::SigningKey; 14 use sha2::{Digest, Sha256}; 15 use spark_rsi::actor::judge::HoldoutSuite; 16 use spark_rsi::evaluator::EvaluationReceipt; 17 use spark_rsi::promotion_gate::{check_v2_promotion, PromotionSubject}; 18 use std::fs; 19 use std::path::{Path, PathBuf}; 20 use std::process::{Command, Output}; 21 22 const README: &[u8] = b"A plain readme for the candidate.\n"; 23 24 fn key(seed: u8) -> SigningKey { 25 SigningKey::from_slice(&[seed; 32]).unwrap() 26 } 27 28 fn sha(bytes: &[u8]) -> String { 29 hex::encode(Sha256::digest(bytes)) 30 } 31 32 fn pub_hex(k: &SigningKey) -> String { 33 hex::encode(k.verifying_key().to_sec1_point(false).as_bytes()) 34 } 35 36 fn bin() -> &'static Path { 37 Path::new(env!("CARGO_BIN_EXE_spark-rsi")) 38 } 39 40 struct Trees { 41 _tmp: tempfile::TempDir, 42 root: PathBuf, 43 parent: PathBuf, 44 candidate: PathBuf, 45 holdouts: PathBuf, 46 } 47 48 fn trees() -> Trees { 49 let tmp = tempfile::tempdir().unwrap(); 50 let root = tmp.path().to_path_buf(); 51 let parent = root.join("parent"); 52 let candidate = root.join("candidate"); 53 for t in [&parent, &candidate] { 54 fs::create_dir_all(t.join("bin")).unwrap(); 55 fs::copy(bin(), t.join("bin/spark-rsi")).unwrap(); 56 } 57 fs::write(parent.join("README.md"), b"The old readme.\n").unwrap(); 58 fs::write(candidate.join("README.md"), README).unwrap(); 59 let holdouts = root.join("holdouts"); 60 for s in HoldoutSuite::builtin_suites() { 61 s.save_to_dir(&holdouts).unwrap(); 62 } 63 fs::create_dir_all(root.join("out")).unwrap(); 64 Trees { 65 _tmp: tmp, 66 root, 67 parent, 68 candidate, 69 holdouts, 70 } 71 } 72 73 /// Writes the operator policy pinning the trees' holdout set; returns its path and digest. 74 fn write_policy(t: &Trees) -> (PathBuf, String) { 75 let (_, holdout_digest) = HoldoutSuite::load_strict(&t.holdouts).unwrap(); 76 let text = serde_json::json!({ 77 "policy_id": "cli-v2-test", 78 "holdout_set_sha256": holdout_digest, 79 "min_holdout_pass_ratio": 1.0, 80 "require_admitted": true, 81 "allowed_targets": ["README.md"], 82 "protected_paths": ["holdouts", "policy", ".rsi"], 83 }) 84 .to_string(); 85 let path = t.root.join("policy.json"); 86 fs::write(&path, &text).unwrap(); 87 (path, sha(text.as_bytes())) 88 } 89 90 /// `spark-rsi judge` over the trees with the judge key; `extra` adds the version 2 arguments. 91 fn judge_cmd(t: &Trees) -> Command { 92 let mut cmd = Command::new(bin()); 93 cmd.env_remove("RSI_JUDGE_POLICY_FILE") 94 .env_remove("RSI_JUDGE_SIGNING_KEY") 95 .arg("judge") 96 .args(["--cycle-id", "cyc-cli", "--candidate-id", "cand"]) 97 .args(["--parent-id", "par"]) 98 .arg("--candidate-path") 99 .arg(&t.candidate) 100 .arg("--parent-path") 101 .arg(&t.parent) 102 .arg("--holdouts-dir") 103 .arg(&t.holdouts) 104 .arg("--output-dir") 105 .arg(t.root.join("out")) 106 .args(["--signing-key-hex", &hex::encode([7u8; 32])]) 107 .args(["--non-inferiority-margin-pct", "1000"]) 108 .args(["--executable", "bin/spark-rsi"]); 109 cmd 110 } 111 112 fn receipt_of(out: &Output) -> EvaluationReceipt { 113 assert!( 114 out.status.success(), 115 "judge failed: {}", 116 String::from_utf8_lossy(&out.stderr) 117 ); 118 serde_json::from_slice(&out.stdout).expect("stdout is one receipt") 119 } 120 121 fn assert_v2_for(r: &EvaluationReceipt, policy_path: &Path, policy_sha: &str) { 122 assert_eq!(r.format_version, 2, "CLI must write a version 2 receipt"); 123 let b = r.binding.as_ref().expect("version 2 binding"); 124 assert_eq!(b.subject_path, "README.md"); 125 assert_eq!(b.subject_sha256, sha(README)); 126 assert_eq!(b.policy_sha256, policy_sha); 127 assert!( 128 r.admitted, 129 "candidate should be admitted: {:?}", 130 r.layer_results 131 ); 132 check_v2_promotion( 133 r, 134 Some(&pub_hex(&key(7))), 135 key(9).verifying_key(), 136 Some(policy_path.to_str().unwrap()), 137 &PromotionSubject::same("par", "README.md", &sha(README)), 138 ) 139 .unwrap(); 140 } 141 142 #[test] 143 fn cli_judge_with_policy_file_writes_a_v2_receipt() { 144 let t = trees(); 145 let (policy, policy_sha) = write_policy(&t); 146 let out = judge_cmd(&t) 147 .arg("--policy-file") 148 .arg(&policy) 149 .args(["--subject-path", "README.md"]) 150 .output() 151 .unwrap(); 152 assert_v2_for(&receipt_of(&out), &policy, &policy_sha); 153 } 154 155 #[test] 156 fn cli_judge_takes_the_policy_from_the_environment() { 157 let t = trees(); 158 let (policy, policy_sha) = write_policy(&t); 159 let out = judge_cmd(&t) 160 .env("RSI_JUDGE_POLICY_FILE", &policy) 161 .args(["--subject-path", "README.md"]) 162 .output() 163 .unwrap(); 164 assert_v2_for(&receipt_of(&out), &policy, &policy_sha); 165 } 166 167 fn assert_refused(out: &Output, needle: &str) { 168 let stderr = String::from_utf8_lossy(&out.stderr); 169 assert!(!out.status.success(), "must be refused, stderr: {}", stderr); 170 assert!( 171 stderr.contains(needle), 172 "stderr must name {:?}, got: {}", 173 needle, 174 stderr 175 ); 176 assert!( 177 serde_json::from_slice::(&out.stdout).is_err(), 178 "no receipt may be printed when the policy is refused" 179 ); 180 } 181 182 #[test] 183 fn cli_judge_refuses_a_malformed_policy_file() { 184 let t = trees(); 185 let bad = t.root.join("bad-policy.json"); 186 fs::write(&bad, b"{ \"policy_id\": \"half").unwrap(); 187 let out = judge_cmd(&t) 188 .arg("--policy-file") 189 .arg(&bad) 190 .args(["--subject-path", "README.md"]) 191 .output() 192 .unwrap(); 193 assert_refused(&out, "policy"); 194 195 let out = judge_cmd(&t) 196 .env("RSI_JUDGE_POLICY_FILE", &bad) 197 .args(["--subject-path", "README.md"]) 198 .output() 199 .unwrap(); 200 assert_refused(&out, "policy"); 201 } 202 203 #[test] 204 fn cli_judge_refuses_a_policy_without_a_subject() { 205 let t = trees(); 206 let (policy, _) = write_policy(&t); 207 let out = judge_cmd(&t) 208 .arg("--policy-file") 209 .arg(&policy) 210 .output() 211 .unwrap(); 212 assert_refused(&out, "subject"); 213 let out = judge_cmd(&t) 214 .env("RSI_JUDGE_POLICY_FILE", &policy) 215 .output() 216 .unwrap(); 217 assert_refused(&out, "subject"); 218 } 219 220 #[test] 221 fn cycle_and_daemon_refuse_a_malformed_judge_policy_or_key_at_startup() { 222 let t = trees(); 223 let bad = t.root.join("bad-policy.json"); 224 fs::write(&bad, b"not json").unwrap(); 225 let (good, _) = write_policy(&t); 226 for sub in ["cycle", "daemon"] { 227 let run = |args: &[&std::ffi::OsStr]| { 228 Command::new(bin()) 229 .env_remove("RSI_JUDGE_POLICY_FILE") 230 .env_remove("RSI_JUDGE_PUBLIC_KEY") 231 .current_dir(&t.root) 232 .arg(sub) 233 .arg(&t.candidate) 234 .args(args) 235 .output() 236 .unwrap() 237 }; 238 let out = run(&["--judge-policy-file".as_ref(), bad.as_os_str()]); 239 assert_refused(&out, "policy"); 240 let out = run(&[ 241 "--judge-policy-file".as_ref(), 242 good.as_os_str(), 243 "--judge-public-key".as_ref(), 244 "zz-not-a-key".as_ref(), 245 ]); 246 assert_refused(&out, "judge public key"); 247 let out = Command::new(bin()) 248 .env("RSI_JUDGE_POLICY_FILE", &bad) 249 .current_dir(&t.root) 250 .arg(sub) 251 .arg(&t.candidate) 252 .output() 253 .unwrap(); 254 assert_refused(&out, "policy"); 255 } 256 }