hive-c0re: in-memory broker + per-agent sockets + coordinator state

This commit is contained in:
müde 2026-05-14 21:42:51 +02:00
parent 4545c08908
commit d79b5a39a1
6 changed files with 220 additions and 9 deletions

View file

@ -1,13 +1,15 @@
use std::path::Path;
use std::sync::Arc;
use anyhow::{Context, Result};
use hive_sh4re::{HostRequest, HostResponse};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::{UnixListener, UnixStream};
use crate::coordinator::Coordinator;
use crate::lifecycle;
pub async fn serve(socket: &Path, agent_flake: &str) -> Result<()> {
pub async fn serve(socket: &Path, agent_flake: &str, coord: Arc<Coordinator>) -> Result<()> {
if let Some(parent) = socket.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("create socket parent {}", parent.display()))?;
@ -23,15 +25,16 @@ pub async fn serve(socket: &Path, agent_flake: &str) -> Result<()> {
loop {
let (stream, _) = listener.accept().await.context("accept connection")?;
let agent_flake = agent_flake.to_owned();
let coord = coord.clone();
tokio::spawn(async move {
if let Err(e) = handle(stream, &agent_flake).await {
if let Err(e) = handle(stream, &agent_flake, coord).await {
tracing::warn!(error = ?e, "connection failed");
}
});
}
}
async fn handle(stream: UnixStream, agent_flake: &str) -> Result<()> {
async fn handle(stream: UnixStream, agent_flake: &str, coord: Arc<Coordinator>) -> Result<()> {
let (read, mut write) = stream.into_split();
let mut reader = BufReader::new(read);
let mut line = String::new();
@ -43,7 +46,7 @@ async fn handle(stream: UnixStream, agent_flake: &str) -> Result<()> {
return Ok(());
}
let resp = match serde_json::from_str::<HostRequest>(line.trim()) {
Ok(req) => dispatch(&req, agent_flake).await,
Ok(req) => dispatch(&req, agent_flake, &coord).await,
Err(e) => HostResponse::error(format!("parse error: {e}")),
};
let mut payload = serde_json::to_string(&resp)?;
@ -53,17 +56,23 @@ async fn handle(stream: UnixStream, agent_flake: &str) -> Result<()> {
}
}
async fn dispatch(req: &HostRequest, agent_flake: &str) -> HostResponse {
async fn dispatch(req: &HostRequest, agent_flake: &str, coord: &Coordinator) -> HostResponse {
let result: anyhow::Result<HostResponse> = async {
Ok(match req {
HostRequest::Spawn { name } => {
tracing::info!(%name, "spawn");
lifecycle::spawn(name, agent_flake).await?;
let agent_dir = coord.register_agent(name).await?;
if let Err(e) = lifecycle::spawn(name, agent_flake, &agent_dir).await {
// Roll back socket registration if container creation failed.
coord.unregister_agent(name);
return Err(e);
}
HostResponse::success()
}
HostRequest::Kill { name } => {
tracing::info!(%name, "kill");
lifecycle::kill(name).await?;
coord.unregister_agent(name);
HostResponse::success()
}
HostRequest::Rebuild { name } => {