93 lines
3.5 KiB
Rust
93 lines
3.5 KiB
Rust
//! Runtime state + config shared between the host admin socket, the manager
|
|
//! socket, and the per-agent sockets: the broker, configured `agent_flake`,
|
|
//! and the map of registered agent sockets.
|
|
|
|
use std::collections::HashMap;
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
use crate::agent_server::{self, AgentSocket};
|
|
use crate::approvals::Approvals;
|
|
use crate::broker::Broker;
|
|
|
|
const AGENT_RUNTIME_ROOT: &str = "/run/hyperhive/agents";
|
|
const MANAGER_RUNTIME_ROOT: &str = "/run/hyperhive/manager";
|
|
/// Manager-editable per-agent config repos. Bind-mounted RW into the manager
|
|
/// container as `/agents/<name>/`. Hive-c0re only writes to these on first
|
|
/// spawn (initial commit); after that it's manager-only.
|
|
const AGENT_STATE_ROOT: &str = "/var/lib/hyperhive/agents";
|
|
/// Hive-c0re-only authoritative per-agent config repos. Containers build from
|
|
/// these. Manager has no filesystem access; the only way to update is via
|
|
/// `request_apply_commit` + user approval.
|
|
const APPLIED_STATE_ROOT: &str = "/var/lib/hyperhive/applied";
|
|
|
|
pub struct Coordinator {
|
|
pub broker: Arc<Broker>,
|
|
pub approvals: Arc<Approvals>,
|
|
/// URL of the hyperhive flake (no fragment). Inlined into per-agent
|
|
/// `flake.nix` files as `inputs.hyperhive.url`.
|
|
pub hyperhive_flake: String,
|
|
agents: Mutex<HashMap<String, AgentSocket>>,
|
|
}
|
|
|
|
impl Coordinator {
|
|
pub fn open(db_path: &Path, hyperhive_flake: String) -> Result<Self> {
|
|
let broker = Broker::open(db_path).context("open broker")?;
|
|
let approvals = Approvals::open(db_path).context("open approvals")?;
|
|
Ok(Self {
|
|
broker: Arc::new(broker),
|
|
approvals: Arc::new(approvals),
|
|
hyperhive_flake,
|
|
agents: Mutex::new(HashMap::new()),
|
|
})
|
|
}
|
|
|
|
pub fn register_agent(&self, name: &str) -> Result<PathBuf> {
|
|
// Idempotent: drop any existing listener so re-registration (e.g. on rebuild,
|
|
// or after a hive-c0re restart cleared /run/hyperhive) gets a fresh socket.
|
|
self.unregister_agent(name);
|
|
let agent_dir = Self::agent_dir(name);
|
|
std::fs::create_dir_all(&agent_dir)
|
|
.with_context(|| format!("create agent dir {}", agent_dir.display()))?;
|
|
let socket_path = Self::socket_path(name);
|
|
let socket = agent_server::start(name, &socket_path, self.broker.clone())?;
|
|
self.agents.lock().unwrap().insert(name.to_owned(), socket);
|
|
Ok(agent_dir)
|
|
}
|
|
|
|
pub fn unregister_agent(&self, name: &str) {
|
|
if let Some(socket) = self.agents.lock().unwrap().remove(name) {
|
|
socket.handle.abort();
|
|
let _ = std::fs::remove_file(&socket.path);
|
|
}
|
|
}
|
|
|
|
pub fn agent_dir(name: &str) -> PathBuf {
|
|
PathBuf::from(format!("{AGENT_RUNTIME_ROOT}/{name}"))
|
|
}
|
|
|
|
pub fn socket_path(name: &str) -> PathBuf {
|
|
Self::agent_dir(name).join("mcp.sock")
|
|
}
|
|
|
|
pub fn manager_dir() -> PathBuf {
|
|
PathBuf::from(MANAGER_RUNTIME_ROOT)
|
|
}
|
|
|
|
pub fn manager_socket_path() -> PathBuf {
|
|
Self::manager_dir().join("mcp.sock")
|
|
}
|
|
|
|
/// Manager-editable proposed config repo. Bind-mounted into the manager
|
|
/// container as `/agents/<name>/config/`.
|
|
pub fn agent_proposed_dir(name: &str) -> PathBuf {
|
|
PathBuf::from(format!("{AGENT_STATE_ROOT}/{name}/config"))
|
|
}
|
|
|
|
/// Authoritative applied config repo. Hive-c0re-only.
|
|
pub fn agent_applied_dir(name: &str) -> PathBuf {
|
|
PathBuf::from(format!("{APPLIED_STATE_ROOT}/{name}"))
|
|
}
|
|
}
|