spark-rsi
Resilient systems interface and fault-tolerant agent communication
Brought by drakestapleton
| 1 | use crate::models::{ImprovementProposal, ProposalKind}; |
| 2 | use std::fs; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | use uuid::Uuid; |
| 5 | |
| 6 | pub struct ProposalGenerator; |
| 7 | |
| 8 | impl ProposalGenerator { |
| 9 | pub fn create_proposal( |
| 10 | title: &str, |
| 11 | description: &str, |
| 12 | target_file: &str, |
| 13 | proposed_patch: &str, |
| 14 | kind: ProposalKind, |
| 15 | ) -> ImprovementProposal { |
| 16 | let id = format!("prop-{}", Uuid::new_v4().simple()); |
| 17 | ImprovementProposal { |
| 18 | id, |
| 19 | title: title.to_string(), |
| 20 | description: description.to_string(), |
| 21 | target_file: target_file.to_string(), |
| 22 | proposed_patch: proposed_patch.to_string(), |
| 23 | kind, |
| 24 | created_at: chrono::Utc::now().to_rfc3339(), |
| 25 | sandbox_path: None, |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /// Prepares an isolated sandbox worktree directory to evaluate the proposal safely. |
| 30 | pub fn stage_in_sandbox( |
| 31 | proposal: &mut ImprovementProposal, |
| 32 | repo_root: &Path, |
| 33 | sandbox_base: &Path, |
| 34 | ) -> Result<PathBuf, String> { |
| 35 | let sandbox_dir = sandbox_base.join(&proposal.id); |
| 36 | if sandbox_dir.exists() { |
| 37 | let _ = fs::remove_dir_all(&sandbox_dir); |
| 38 | } |
| 39 | fs::create_dir_all(&sandbox_dir) |
| 40 | .map_err(|e| format!("Failed to create sandbox dir: {}", e))?; |
| 41 | |
| 42 | // Copy source files excluding target and .git |
| 43 | copy_dir_recursive(repo_root, &sandbox_dir, 0)?; |
| 44 | |
| 45 | // Apply proposed patch to target file in sandbox |
| 46 | let target_in_sandbox = sandbox_dir.join(&proposal.target_file); |
| 47 | if let Some(parent) = target_in_sandbox.parent() { |
| 48 | let _ = fs::create_dir_all(parent); |
| 49 | } |
| 50 | |
| 51 | fs::write(&target_in_sandbox, &proposal.proposed_patch) |
| 52 | .map_err(|e| format!("Failed to write proposed patch in sandbox: {}", e))?; |
| 53 | |
| 54 | proposal.sandbox_path = Some(sandbox_dir.display().to_string()); |
| 55 | Ok(sandbox_dir) |
| 56 | } |
| 57 | |
| 58 | /// Automatically inspects a repository for unslop violations and proposes atomic sanitization. |
| 59 | pub fn scan_and_propose_unslop(repo_root: &Path) -> Option<ImprovementProposal> { |
| 60 | let candidate_files = ["README.md", "docs/PHILOSOPHY.md", "CONTRIBUTING.md"]; |
| 61 | for rel in &candidate_files { |
| 62 | let full_path = repo_root.join(rel); |
| 63 | if full_path.exists() { |
| 64 | if let Ok(content) = fs::read_to_string(&full_path) { |
| 65 | if content.contains('\u{2014}') || content.contains('\u{2013}') { |
| 66 | let cleaned = content |
| 67 | .replace('\u{2014}', ", ") |
| 68 | .replace('\u{2013}', "-"); |
| 69 | return Some(Self::create_proposal( |
| 70 | &format!("sanitize unslop punctuation in {}", rel), |
| 71 | "Remove em dashes and en dashes in accordance with unslop standard", |
| 72 | rel, |
| 73 | &cleaned, |
| 74 | ProposalKind::UnslopSanitization, |
| 75 | )); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | None |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | fn copy_dir_recursive(src: &Path, dst: &Path, depth: usize) -> Result<(), String> { |
| 85 | if depth > 8 { |
| 86 | return Ok(()); |
| 87 | } |
| 88 | if !dst.exists() { |
| 89 | fs::create_dir_all(dst).map_err(|e| format!("Failed to create dir {:?}: {}", dst, e))?; |
| 90 | } |
| 91 | let entries = fs::read_dir(src).map_err(|e| format!("Failed to read dir {:?}: {}", src, e))?; |
| 92 | for entry in entries.flatten() { |
| 93 | let path = entry.path(); |
| 94 | let file_name = entry.file_name(); |
| 95 | let name = file_name.to_str().unwrap_or_default(); |
| 96 | |
| 97 | if name == "target" || name == ".git" || name.starts_with(".") { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | let target_path = dst.join(&file_name); |
| 102 | if path.is_dir() { |
| 103 | copy_dir_recursive(&path, &target_path, depth + 1)?; |
| 104 | } else if path.is_file() { |
| 105 | let _ = fs::copy(&path, &target_path); |
| 106 | } |
| 107 | } |
| 108 | Ok(()) |
| 109 | } |
| 110 | |
| 111 | #[cfg(test)] |
| 112 | mod tests { |
| 113 | use super::*; |
| 114 | |
| 115 | #[test] |
| 116 | fn test_create_proposal() { |
| 117 | let prop = ProposalGenerator::create_proposal( |
| 118 | "add verified test", |
| 119 | "Improve verification test coverage", |
| 120 | "tests/verify.rs", |
| 121 | "// new test content", |
| 122 | ProposalKind::Refactor, |
| 123 | ); |
| 124 | assert!(prop.id.starts_with("prop-")); |
| 125 | assert_eq!(prop.kind, ProposalKind::Refactor); |
| 126 | } |
| 127 | } |