naming: h-<name> for agents, hm1nd for manager (11-char limit)

This commit is contained in:
müde 2026-05-14 21:59:01 +02:00
parent 7ce0f0022f
commit 326da5a7bf
2 changed files with 29 additions and 8 deletions

View file

@ -5,8 +5,13 @@ use std::path::Path;
use anyhow::{Context, Result, bail};
use tokio::process::Command;
pub const AGENT_PREFIX: &str = "hive-agent-";
pub const HIVE_PREFIX: &str = "hive-";
/// Sub-agent container prefix. `nixos-container` caps the total container name
/// at 11 chars (it gets encoded into network interface names), so the agent
/// name itself can be at most `MAX_AGENT_NAME` chars.
pub const AGENT_PREFIX: &str = "h-";
pub const MAX_AGENT_NAME: usize = 9;
/// Container name of the manager (a separate slot from sub-agents).
pub const MANAGER_NAME: &str = "hm1nd";
/// Mount point of the per-agent runtime directory inside the container.
pub const CONTAINER_RUNTIME_MOUNT: &str = "/run/hive";
@ -15,7 +20,21 @@ pub fn container_name(name: &str) -> String {
format!("{AGENT_PREFIX}{name}")
}
fn validate(name: &str) -> Result<()> {
if name.is_empty() {
bail!("agent name must not be empty");
}
if name.len() > MAX_AGENT_NAME {
bail!(
"agent name '{name}' is too long ({} chars); max {MAX_AGENT_NAME}",
name.len()
);
}
Ok(())
}
pub async fn spawn(name: &str, agent_flake: &str, agent_dir: &Path) -> Result<()> {
validate(name)?;
let container = container_name(name);
run(&["create", &container, "--flake", agent_flake]).await?;
write_nspawn_override(&container, agent_dir)?;
@ -39,11 +58,13 @@ fn write_nspawn_override(container: &str, agent_dir: &Path) -> Result<()> {
}
pub async fn kill(name: &str) -> Result<()> {
validate(name)?;
let container = container_name(name);
run(&["stop", &container]).await
}
pub async fn rebuild(name: &str, agent_flake: &str) -> Result<()> {
validate(name)?;
let container = container_name(name);
run(&["update", &container, "--flake", agent_flake]).await
}
@ -64,7 +85,7 @@ pub async fn list() -> Result<Vec<String>> {
Ok(String::from_utf8_lossy(&out.stdout)
.lines()
.map(str::trim)
.filter(|line| line.starts_with(HIVE_PREFIX))
.filter(|line| line.starts_with(AGENT_PREFIX) || *line == MANAGER_NAME)
.map(str::to_owned)
.collect())
}