spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / isolation / rollback.rs
| 1 | use std::path::{Path, PathBuf}; |
| 2 | use std::process::Command; |
| 3 | |
| 4 | #[derive(Debug, Clone)] |
| 5 | pub struct RollbackCheckpoint { |
| 6 | pub repo_root: PathBuf, |
| 7 | pub cycle_id: String, |
| 8 | pub base_commit_sha: String, |
| 9 | pub worktree_dir: PathBuf, |
| 10 | pub ref_name: String, |
| 11 | } |
| 12 | |
| 13 | impl RollbackCheckpoint { |
| 14 | pub fn create(repo_root: &Path, cycle_id: &str) -> Result<Self, String> { |
| 15 | let head_out = Command::new("git") |
| 16 | .arg("-C") |
| 17 | .arg(repo_root) |
| 18 | .args(["rev-parse", "HEAD"]) |
| 19 | .output() |
| 20 | .map_err(|e| format!("Failed to resolve HEAD in {:?}: {}", repo_root, e))?; |
| 21 | |
| 22 | if !head_out.status.success() { |
| 23 | return Err(format!( |
| 24 | "git rev-parse HEAD failed: {}", |
| 25 | String::from_utf8_lossy(&head_out.stderr) |
| 26 | )); |
| 27 | } |
| 28 | |
| 29 | let base_commit_sha = String::from_utf8_lossy(&head_out.stdout).trim().to_string(); |
| 30 | let worktree_dir = PathBuf::from(format!("/tmp/spark-rsi-worktree-{}", cycle_id)); |
| 31 | let ref_name = format!("refs/rsi/checkpoints/{}", cycle_id); |
| 32 | |
| 33 | if worktree_dir.exists() { |
| 34 | let _ = Command::new("git") |
| 35 | .arg("-C") |
| 36 | .arg(repo_root) |
| 37 | .args([ |
| 38 | "worktree", |
| 39 | "remove", |
| 40 | "--force", |
| 41 | worktree_dir.to_str().unwrap(), |
| 42 | ]) |
| 43 | .output(); |
| 44 | let _ = std::fs::remove_dir_all(&worktree_dir); |
| 45 | } |
| 46 | |
| 47 | // Use detached HEAD to avoid branch namespace pollution |
| 48 | let wt_out = Command::new("git") |
| 49 | .arg("-C") |
| 50 | .arg(repo_root) |
| 51 | .args([ |
| 52 | "worktree", |
| 53 | "add", |
| 54 | "--detach", |
| 55 | worktree_dir.to_str().unwrap(), |
| 56 | &base_commit_sha, |
| 57 | ]) |
| 58 | .output() |
| 59 | .map_err(|e| format!("Failed to spawn git worktree add: {}", e))?; |
| 60 | |
| 61 | if !wt_out.status.success() { |
| 62 | return Err(format!( |
| 63 | "Failed to create detached git worktree: {}", |
| 64 | String::from_utf8_lossy(&wt_out.stderr) |
| 65 | )); |
| 66 | } |
| 67 | |
| 68 | let _ = Command::new("git") |
| 69 | .arg("-C") |
| 70 | .arg(repo_root) |
| 71 | .args(["update-ref", &ref_name, &base_commit_sha]) |
| 72 | .output(); |
| 73 | |
| 74 | Ok(Self { |
| 75 | repo_root: repo_root.to_path_buf(), |
| 76 | cycle_id: cycle_id.to_string(), |
| 77 | base_commit_sha, |
| 78 | worktree_dir, |
| 79 | ref_name, |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | pub fn teardown(&self) -> Result<(), String> { |
| 84 | let _ = Command::new("git") |
| 85 | .arg("-C") |
| 86 | .arg(&self.repo_root) |
| 87 | .args([ |
| 88 | "worktree", |
| 89 | "remove", |
| 90 | "--force", |
| 91 | self.worktree_dir.to_str().unwrap(), |
| 92 | ]) |
| 93 | .output(); |
| 94 | |
| 95 | let _ = Command::new("git") |
| 96 | .arg("-C") |
| 97 | .arg(&self.repo_root) |
| 98 | .args(["update-ref", "-d", &self.ref_name]) |
| 99 | .output(); |
| 100 | |
| 101 | if self.worktree_dir.exists() { |
| 102 | let _ = std::fs::remove_dir_all(&self.worktree_dir); |
| 103 | } |
| 104 | |
| 105 | Ok(()) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | #[cfg(test)] |
| 110 | mod tests { |
| 111 | use super::*; |
| 112 | |
| 113 | #[test] |
| 114 | fn test_rollback_checkpoint_lifecycle() { |
| 115 | let tmp = tempfile::tempdir().unwrap(); |
| 116 | let repo_path = tmp.path(); |
| 117 | |
| 118 | // Initialize mock git repo |
| 119 | Command::new("git") |
| 120 | .arg("init") |
| 121 | .current_dir(repo_path) |
| 122 | .output() |
| 123 | .unwrap(); |
| 124 | Command::new("git") |
| 125 | .args(["config", "user.name", "Test"]) |
| 126 | .current_dir(repo_path) |
| 127 | .output() |
| 128 | .unwrap(); |
| 129 | Command::new("git") |
| 130 | .args(["config", "user.email", "test@test.local"]) |
| 131 | .current_dir(repo_path) |
| 132 | .output() |
| 133 | .unwrap(); |
| 134 | |
| 135 | std::fs::write(repo_path.join("test.txt"), "genesis").unwrap(); |
| 136 | Command::new("git") |
| 137 | .args(["add", "."]) |
| 138 | .current_dir(repo_path) |
| 139 | .output() |
| 140 | .unwrap(); |
| 141 | Command::new("git") |
| 142 | .args(["commit", "-m", "initial"]) |
| 143 | .current_dir(repo_path) |
| 144 | .output() |
| 145 | .unwrap(); |
| 146 | |
| 147 | let cycle_id = format!("test-{}", uuid::Uuid::new_v4().simple()); |
| 148 | let cp = RollbackCheckpoint::create(repo_path, &cycle_id).unwrap(); |
| 149 | |
| 150 | assert!(cp.worktree_dir.exists()); |
| 151 | assert_eq!(cp.cycle_id, cycle_id); |
| 152 | assert!(!cp.base_commit_sha.is_empty()); |
| 153 | |
| 154 | // Verify git branch list does NOT contain any rsi/candidate branch |
| 155 | let branches = Command::new("git") |
| 156 | .arg("-C") |
| 157 | .arg(repo_path) |
| 158 | .arg("branch") |
| 159 | .output() |
| 160 | .unwrap(); |
| 161 | let branch_str = String::from_utf8_lossy(&branches.stdout); |
| 162 | assert!( |
| 163 | !branch_str.contains("rsi/candidate"), |
| 164 | "Detached worktree should not create a named branch: {}", |
| 165 | branch_str |
| 166 | ); |
| 167 | |
| 168 | // Teardown |
| 169 | cp.teardown().unwrap(); |
| 170 | assert!(!cp.worktree_dir.exists()); |
| 171 | } |
| 172 | } |