//! Spawns and supervises the warm Node/Bun worker process, and relays //! `Hook` requests to it. //! //! The worker speaks a SEPARATE, simpler internal protocol on its own //! socket (no `protocolVersion` — this process always spawns a //! version-matched worker, so there's nothing to negotiate) — this module //! is the only thing that talks to it, and it's the one place that //! translates between that internal protocol and the client-facing //! [`fpai_ipc::ServerMessage`] envelope (which DOES carry `protocolVersion`, //! since that one crosses a real compatibility boundary against whatever //! `failproofai` CLI version happens to be installed). use fpai_ipc::framing::{read_message, write_message}; use serde::Deserialize; use serde_json::json; use std::io; use std::os::unix::net::UnixStream; use std::os::unix::process::CommandExt; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Stdio}; use std::sync::Mutex; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, Instant}; #[derive(Debug)] pub enum WorkerError { Io(io::Error), /// The worker never created its socket within the startup deadline. StartupTimedOut, /// A response arrived but wasn't a well-formed hookResult/error. BadResponse(String), } impl std::fmt::Display for WorkerError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { WorkerError::Io(e) => write!(f, "worker io error: {e}"), WorkerError::StartupTimedOut => write!(f, "worker did not start in time"), WorkerError::BadResponse(s) => write!(f, "unexpected worker response: {s}"), } } } impl std::error::Error for WorkerError {} #[derive(Debug)] pub struct HookOutcome { pub exit_code: i32, pub stdout: String, pub stderr: String, pub evaluation: Option, } #[derive(Debug, Clone, Deserialize)] #[serde(rename_all = "camelCase")] pub struct PolicyEvaluation { pub decision: String, pub policy_name: Option, #[serde(default)] pub policy_names: Vec, pub reason: Option, #[serde(default)] pub matched_policies: Vec, pub duration_ms: u64, pub tool_name: Option, } /// How to launch the worker process. `FAILPROOFAI_WORKER_CMD` (dev/test /// override — a full shell command string, run via `sh -c`) always takes /// precedence; otherwise falls back to `node