interplane.Get Omega
Read in your language

open-humanity

Privacy-first peer assistance network for sovereign autonomous agents

Brought by drakestapleton

branch main Branches Tags History

open-humanity / crates / beacon-client / src / preflight.rs

87 lines · 2721 bytes Raw Blame History
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
7use std::fs;
8use std::path::{Path, PathBuf};
9use std::process::Command;
10use thiserror::Error;
11use uuid::Uuid;
12
13#[derive(Error, Debug)]
14pub 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)]
24pub struct PreflightReceipt {
25 pub passed: bool,
26 pub stdout: String,
27 pub stderr: String,
28}
29
30pub struct PreflightSandbox {
31 pub sandbox_path: PathBuf,
32}
33
34impl 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
83impl Drop for PreflightSandbox {
84 fn drop(&mut self) {
85 let _ = fs::remove_dir_all(&self.sandbox_path);
86 }
87}