open-humanity
Privacy-first peer assistance network for sovereign autonomous agents
Brought by drakestapleton
open-humanity / crates / beacon-client / src / preflight.rs
| 1 | //! Autonomous Sandboxed Preflight Verification. |
| 2 | //! |
| 3 | //! Applies candidate resolution patches inside an ephemeral, isolated |
| 4 | //! directory (/tmp/beacon-verify-<uuid>), executing compiler verification |
| 5 | //! before any changes touch active workspaces. |
| 6 | |
| 7 | use std::fs; |
| 8 | use std::path::{Path, PathBuf}; |
| 9 | use std::process::Command; |
| 10 | use thiserror::Error; |
| 11 | use uuid::Uuid; |
| 12 | |
| 13 | #[derive(Error, Debug)] |
| 14 | pub enum PreflightError { |
| 15 | #[error("IO error during preflight setup: {0}")] |
| 16 | Io(#[from] std::io::Error), |
| 17 | #[error("Verification command failed with status {status}: {stderr}")] |
| 18 | VerificationFailed { status: i32, stderr: String }, |
| 19 | #[error("Failed to execute verification command: {0}")] |
| 20 | Execution(String), |
| 21 | } |
| 22 | |
| 23 | #[derive(Debug)] |
| 24 | pub struct PreflightReceipt { |
| 25 | pub passed: bool, |
| 26 | pub stdout: String, |
| 27 | pub stderr: String, |
| 28 | } |
| 29 | |
| 30 | pub struct PreflightSandbox { |
| 31 | pub sandbox_path: PathBuf, |
| 32 | } |
| 33 | |
| 34 | impl PreflightSandbox { |
| 35 | pub fn new() -> Result<Self, PreflightError> { |
| 36 | let id = Uuid::now_v7(); |
| 37 | let sandbox_path = std::env::temp_dir().join(format!("beacon-verify-{}", id)); |
| 38 | fs::create_dir_all(&sandbox_path)?; |
| 39 | Ok(Self { sandbox_path }) |
| 40 | } |
| 41 | |
| 42 | pub fn path(&self) -> &Path { |
| 43 | &self.sandbox_path |
| 44 | } |
| 45 | |
| 46 | pub fn write_file(&self, relative_path: &str, content: &str) -> Result<PathBuf, PreflightError> { |
| 47 | let target = self.sandbox_path.join(relative_path); |
| 48 | if let Some(parent) = target.parent() { |
| 49 | fs::create_dir_all(parent)?; |
| 50 | } |
| 51 | fs::write(&target, content)?; |
| 52 | Ok(target) |
| 53 | } |
| 54 | |
| 55 | pub fn run_check(&self, program: &str, args: &[&str]) -> Result<PreflightReceipt, PreflightError> { |
| 56 | // The check judges text from the network: the lasting identity seed of this node |
| 57 | // (open-humanity reads it from OPEN_HUMANITY_IDENTITY) never reaches it. |
| 58 | let output = Command::new(program) |
| 59 | .args(args) |
| 60 | .env_remove("OPEN_HUMANITY_IDENTITY") |
| 61 | .current_dir(&self.sandbox_path) |
| 62 | .output() |
| 63 | .map_err(|e| PreflightError::Execution(e.to_string()))?; |
| 64 | |
| 65 | let stdout = String::from_utf8_lossy(&output.stdout).to_string(); |
| 66 | let stderr = String::from_utf8_lossy(&output.stderr).to_string(); |
| 67 | |
| 68 | if output.status.success() { |
| 69 | Ok(PreflightReceipt { |
| 70 | passed: true, |
| 71 | stdout, |
| 72 | stderr, |
| 73 | }) |
| 74 | } else { |
| 75 | Err(PreflightError::VerificationFailed { |
| 76 | status: output.status.code().unwrap_or(-1), |
| 77 | stderr, |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl Drop for PreflightSandbox { |
| 84 | fn drop(&mut self) { |
| 85 | let _ = fs::remove_dir_all(&self.sandbox_path); |
| 86 | } |
| 87 | } |