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_ratifier_branch_only.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_ratifier_branch_only.rs` 370 lines ยท 12813 bytes Raw Blame History 1 //! The ratifier must never commit to the target's default branch. It builds a 2 //! review branch and a PR-ready patch, and runs the protected-file checks on 3 //! the REAL git diff. Each test below is a bypass attempt or a realistic fault. 4 5 use spark_rsi::diff_gate::default_ratify_roots; 6 use spark_rsi::models::{ImprovementProposal, InvariantReport, ProposalKind}; 7 use spark_rsi::Ratifier; 8 use std::path::Path; 9 use std::process::Command; 10 11 const DEAD_CORTEX: &str = "http://127.0.0.1:9"; 12 13 fn git(repo: &Path, args: &[&str]) -> String { 14 let out = Command::new("git") 15 .arg("-C") 16 .arg(repo) 17 .args(args) 18 .output() 19 .expect("git runs"); 20 assert!( 21 out.status.success(), 22 "git {:?} failed: {}", 23 args, 24 String::from_utf8_lossy(&out.stderr) 25 ); 26 String::from_utf8_lossy(&out.stdout).trim().to_string() 27 } 28 29 fn git_ok(repo: &Path, args: &[&str]) -> bool { 30 Command::new("git") 31 .arg("-C") 32 .arg(repo) 33 .args(args) 34 .output() 35 .map(|o| o.status.success()) 36 .unwrap_or(false) 37 } 38 39 /// A target repository on `main` with a README, a docs file, and a Cargo.toml. 40 fn make_repo() -> tempfile::TempDir { 41 let tmp = tempfile::tempdir().unwrap(); 42 let repo = tmp.path(); 43 git(repo, &["init", "-q", "-b", "main"]); 44 git(repo, &["config", "user.name", "Test"]); 45 git(repo, &["config", "user.email", "test@example.invalid"]); 46 std::fs::write(repo.join("README.md"), "Fast \u{2014} and small.\n").unwrap(); 47 std::fs::create_dir_all(repo.join("docs")).unwrap(); 48 std::fs::write(repo.join("docs/guide.md"), "guide\n").unwrap(); 49 std::fs::write(repo.join("Cargo.toml"), "[package]\nname = \"t\"\n").unwrap(); 50 git(repo, &["add", "."]); 51 git(repo, &["commit", "-q", "-m", "initial"]); 52 tmp 53 } 54 55 fn proposal(id: &str, target: &str, content: &str) -> ImprovementProposal { 56 ImprovementProposal { 57 id: id.to_string(), 58 title: "test proposal".to_string(), 59 description: "test".to_string(), 60 target_file: target.to_string(), 61 proposed_patch: content.to_string(), 62 kind: ProposalKind::UnslopSanitization, 63 created_at: "now".to_string(), 64 sandbox_path: None, 65 operator_signature: None, 66 } 67 } 68 69 fn passed() -> InvariantReport { 70 InvariantReport { 71 passed: true, 72 unslop_clean: true, 73 em_dash_detected: 0, 74 en_dash_detected: 0, 75 forbidden_buzzwords_detected: vec![], 76 antithesis_tropes_detected: vec![], 77 zero_disk_secrets_clean: true, 78 secret_leaks: vec![], 79 compilation_passed: true, 80 compilation_error: None, 81 tests_passed: true, 82 test_output_summary: None, 83 notes: vec![], 84 } 85 } 86 87 struct Snapshot { 88 head: String, 89 main: String, 90 status: String, 91 branches: String, 92 readme: Vec, 93 } 94 95 fn snapshot(repo: &Path) -> Snapshot { 96 Snapshot { 97 head: git(repo, &["rev-parse", "HEAD"]), 98 main: git(repo, &["rev-parse", "refs/heads/main"]), 99 status: git(repo, &["status", "--porcelain", "--untracked-files=all"]), 100 branches: git(repo, &["for-each-ref", "--format=%(refname) %(objectname)"]), 101 readme: std::fs::read(repo.join("README.md")).unwrap_or_default(), 102 } 103 } 104 105 fn assert_untouched(repo: &Path, before: &Snapshot) { 106 let after = snapshot(repo); 107 assert_eq!(before.head, after.head, "HEAD moved"); 108 assert_eq!(before.main, after.main, "default branch moved"); 109 assert_eq!(before.status, after.status, "working tree or index changed"); 110 assert_eq!(before.readme, after.readme, "working tree file changed"); 111 } 112 113 async fn ratify( 114 repo: &Path, 115 patch_dir: &Path, 116 p: &ImprovementProposal, 117 roots: &[String], 118 ) -> spark_rsi::models::RatificationRecord { 119 Ratifier::ratify_proposal( 120 p, 121 &passed(), 122 repo, 123 roots, 124 patch_dir, 125 Some("ledger-hash"), 126 DEAD_CORTEX, 127 "test", 128 ) 129 .await 130 .expect("ratifier returns a record") 131 } 132 133 #[tokio::test] 134 async fn ratifier_makes_review_branch_and_patch_never_default_branch() { 135 let tmp = make_repo(); 136 let repo = tmp.path(); 137 let patches = tempfile::tempdir().unwrap(); 138 let before = snapshot(repo); 139 140 let p = proposal("prop-good1", "README.md", "Fast, and small.\n"); 141 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 142 143 assert_eq!(rec.status, "ProposedForReview", "{}", rec.message); 144 assert_untouched(repo, &before); 145 146 let branch = rec.branch.clone().unwrap(); 147 assert_eq!(branch, "rsi/prop-good1"); 148 let tip = git(repo, &["rev-parse", &format!("refs/heads/{}", branch)]); 149 assert_eq!(Some(tip.clone()), rec.commit_hash); 150 assert_eq!(git(repo, &["rev-parse", &format!("{}^", tip)]), before.main); 151 assert_eq!( 152 git(repo, &["diff", "--name-only", "main", &tip]), 153 "README.md" 154 ); 155 156 let patch = std::fs::read_to_string(rec.patch_path.unwrap()).unwrap(); 157 assert!(patch.contains("Subject: [PATCH] rsi: test proposal (prop-good1)")); 158 assert!(patch.contains("+Fast, and small.")); 159 // The patch applies cleanly to the untouched default branch. 160 let check = tempfile::NamedTempFile::new().unwrap(); 161 std::fs::write(check.path(), &patch).unwrap(); 162 assert!(git_ok( 163 repo, 164 &["apply", "--check", check.path().to_str().unwrap()] 165 )); 166 } 167 168 #[tokio::test] 169 async fn dotdot_target_cannot_reach_protected_file() { 170 let tmp = make_repo(); 171 let repo = tmp.path(); 172 let patches = tempfile::tempdir().unwrap(); 173 let before = snapshot(repo); 174 for target in [ 175 "docs/../Cargo.toml", 176 "../outside.txt", 177 "/etc/passwd", 178 "docs\\..\\Cargo.toml", 179 ".git/hooks/post-commit", 180 "docs/./../.git/config", 181 ] { 182 let p = proposal("prop-dotdot", target, "pwned\n"); 183 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 184 assert_eq!(rec.status, "Rejected", "target {:?} was accepted", target); 185 assert!(rec.branch.is_none()); 186 } 187 assert_untouched(repo, &before); 188 assert_eq!( 189 before.branches, 190 snapshot(repo).branches, 191 "a branch was created" 192 ); 193 } 194 195 #[tokio::test] 196 async fn protected_file_refused_even_when_listed_as_root() { 197 let tmp = make_repo(); 198 let repo = tmp.path(); 199 let patches = tempfile::tempdir().unwrap(); 200 let before = snapshot(repo); 201 // A misconfigured root list cannot open a root-of-trust file. 202 let roots = vec!["Cargo.toml".to_string(), ".github".to_string()]; 203 for target in ["Cargo.toml", ".github/workflows/ci.yml"] { 204 let p = proposal("prop-prot", target, "[package]\nname = \"evil\"\n"); 205 let rec = ratify(repo, patches.path(), &p, &roots).await; 206 assert_eq!(rec.status, "Rejected", "{}", target); 207 assert!(rec.message.contains("protected"), "{}", rec.message); 208 } 209 assert_untouched(repo, &before); 210 } 211 212 #[tokio::test] 213 async fn target_outside_declared_roots_refused() { 214 let tmp = make_repo(); 215 let repo = tmp.path(); 216 let patches = tempfile::tempdir().unwrap(); 217 let before = snapshot(repo); 218 for target in ["scripts/install.sh", "docsx/a.md", "README.md.orig"] { 219 let p = proposal("prop-out", target, "echo hi\n"); 220 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 221 assert_eq!(rec.status, "Rejected", "{}", target); 222 assert!( 223 rec.message.contains("outside the declared roots"), 224 "{}", 225 rec.message 226 ); 227 } 228 assert_untouched(repo, &before); 229 } 230 231 #[cfg(unix)] 232 #[tokio::test] 233 async fn symlink_in_base_tree_refused() { 234 let tmp = make_repo(); 235 let repo = tmp.path(); 236 let patches = tempfile::tempdir().unwrap(); 237 // docs/link -> ../Cargo.toml, and docs/sub -> ../.github (symlinked directory) 238 std::os::unix::fs::symlink("../Cargo.toml", repo.join("docs/link")).unwrap(); 239 std::fs::create_dir_all(repo.join(".github/workflows")).unwrap(); 240 std::fs::write(repo.join(".github/workflows/ci.yml"), "on: push\n").unwrap(); 241 std::os::unix::fs::symlink("../.github", repo.join("docs/sub")).unwrap(); 242 git(repo, &["add", "."]); 243 git(repo, &["commit", "-q", "-m", "links"]); 244 let before = snapshot(repo); 245 246 for target in ["docs/link", "docs/sub/workflows/ci.yml"] { 247 let p = proposal("prop-link", target, "evil\n"); 248 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 249 assert_eq!(rec.status, "Rejected", "{}", target); 250 assert!(rec.message.contains("symlink"), "{}", rec.message); 251 } 252 assert_untouched(repo, &before); 253 assert_eq!( 254 std::fs::read_to_string(repo.join("Cargo.toml")).unwrap(), 255 "[package]\nname = \"t\"\n" 256 ); 257 } 258 259 #[tokio::test] 260 async fn secret_in_real_diff_refused() { 261 let tmp = make_repo(); 262 let repo = tmp.path(); 263 let patches = tempfile::tempdir().unwrap(); 264 let before = snapshot(repo); 265 let key = ["AKIA", "ABCDEFGHIJKLMNOP"].join(""); 266 let p = proposal( 267 "prop-secret", 268 "docs/guide.md", 269 &format!("guide\nkey={}\n", key), 270 ); 271 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 272 assert_eq!(rec.status, "Rejected"); 273 assert!(rec.message.contains("secret"), "{}", rec.message); 274 assert_untouched(repo, &before); 275 } 276 277 #[tokio::test] 278 async fn network_primitive_in_real_diff_refused() { 279 let tmp = make_repo(); 280 let repo = tmp.path(); 281 let patches = tempfile::tempdir().unwrap(); 282 let p = proposal( 283 "prop-net", 284 "src/lib.rs", 285 "pub fn f() { let _ = std::net::TcpStream::connect(\"1.2.3.4:80\"); }\n", 286 ); 287 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 288 assert_eq!(rec.status, "Rejected"); 289 assert!(rec.message.contains("network"), "{}", rec.message); 290 } 291 292 #[tokio::test] 293 async fn unchanged_content_is_not_a_proposal() { 294 let tmp = make_repo(); 295 let repo = tmp.path(); 296 let patches = tempfile::tempdir().unwrap(); 297 let p = proposal("prop-same", "docs/guide.md", "guide\n"); 298 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 299 assert_eq!(rec.status, "Rejected"); 300 assert!(rec.message.contains("empty"), "{}", rec.message); 301 } 302 303 #[tokio::test] 304 async fn current_branch_named_like_review_branch_is_never_written() { 305 let tmp = make_repo(); 306 let repo = tmp.path(); 307 let patches = tempfile::tempdir().unwrap(); 308 // The checked-out (default) branch has the review branch's name. 309 git(repo, &["checkout", "-q", "-b", "rsi/prop-clash"]); 310 let tip = git(repo, &["rev-parse", "HEAD"]); 311 let p = proposal("prop-clash", "README.md", "changed\n"); 312 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 313 assert_eq!(rec.status, "Rejected", "{}", rec.message); 314 assert!(rec.message.contains("default branch"), "{}", rec.message); 315 assert_eq!(git(repo, &["rev-parse", "refs/heads/rsi/prop-clash"]), tip); 316 } 317 318 #[tokio::test] 319 async fn existing_review_branch_is_not_moved() { 320 let tmp = make_repo(); 321 let repo = tmp.path(); 322 let patches = tempfile::tempdir().unwrap(); 323 let main = git(repo, &["rev-parse", "main"]); 324 git(repo, &["branch", "rsi/prop-dup", &main]); 325 let p = proposal("prop-dup", "README.md", "changed\n"); 326 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 327 assert_eq!(rec.status, "Rejected", "{}", rec.message); 328 assert_eq!(git(repo, &["rev-parse", "refs/heads/rsi/prop-dup"]), main); 329 } 330 331 #[tokio::test] 332 async fn hostile_proposal_id_cannot_name_a_branch() { 333 let tmp = make_repo(); 334 let repo = tmp.path(); 335 let patches = tempfile::tempdir().unwrap(); 336 let before = snapshot(repo); 337 for id in ["../main", "x/../../main", "a b", ""] { 338 let p = proposal(id, "README.md", "changed\n"); 339 let rec = ratify(repo, patches.path(), &p, &default_ratify_roots()).await; 340 assert_eq!(rec.status, "Rejected", "id {:?}", id); 341 } 342 assert_untouched(repo, &before); 343 assert_eq!(before.branches, snapshot(repo).branches); 344 } 345 346 #[tokio::test] 347 async fn failed_invariants_never_touch_git() { 348 let tmp = make_repo(); 349 let repo = tmp.path(); 350 let patches = tempfile::tempdir().unwrap(); 351 let before = snapshot(repo); 352 let mut inv = passed(); 353 inv.passed = false; 354 let p = proposal("prop-inv", "README.md", "changed\n"); 355 let rec = Ratifier::ratify_proposal( 356 &p, 357 &inv, 358 repo, 359 &default_ratify_roots(), 360 patches.path(), 361 None, 362 DEAD_CORTEX, 363 "test", 364 ) 365 .await 366 .unwrap(); 367 assert_eq!(rec.status, "Rejected"); 368 assert_untouched(repo, &before); 369 assert_eq!(before.branches, snapshot(repo).branches); 370 }