refactor(#1202): introduce HiveEnv + AgentPaths to reduce arg repetition
This commit is contained in:
parent
e7785b4948
commit
29c7f64bd3
8 changed files with 131 additions and 235 deletions
|
|
@ -6,6 +6,8 @@ use anyhow::{Context, Result, bail};
|
|||
use hive_sh4re::priv_proto::BindMount;
|
||||
use tokio::process::Command;
|
||||
|
||||
use crate::coordinator::{AgentPaths, HiveEnv};
|
||||
|
||||
/// 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.
|
||||
|
|
@ -221,23 +223,7 @@ async fn port_collision(self_name: &str) -> Option<String> {
|
|||
None
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments, clippy::implicit_hasher)]
|
||||
pub async fn spawn(
|
||||
name: &str,
|
||||
hyperhive_flake: &str,
|
||||
nixpkgs_flake: &str,
|
||||
nixpkgs_unstable_flake: &str,
|
||||
agent_dir: &Path,
|
||||
proposed_dir: &Path,
|
||||
applied_dir: &Path,
|
||||
claude_dir: &Path,
|
||||
notes_dir: &Path,
|
||||
dashboard_port: u16,
|
||||
operator_pronouns: &str,
|
||||
context_window_tokens: &std::collections::HashMap<String, u64>,
|
||||
cpu_quota: &str,
|
||||
memory_max: &str,
|
||||
) -> Result<()> {
|
||||
pub async fn spawn(name: &str, hive: &HiveEnv, paths: &AgentPaths) -> Result<()> {
|
||||
validate(name)?;
|
||||
if let Some(other) = port_collision(name).await {
|
||||
bail!(
|
||||
|
|
@ -245,28 +231,19 @@ pub async fn spawn(
|
|||
agent_web_port(name)
|
||||
);
|
||||
}
|
||||
setup_proposed(proposed_dir, name).await?;
|
||||
setup_applied(applied_dir, Some(proposed_dir), name).await?;
|
||||
ensure_claude_dir(claude_dir)?;
|
||||
ensure_state_dir(notes_dir)?;
|
||||
setup_proposed(&paths.proposed_dir, name).await?;
|
||||
setup_applied(&paths.applied_dir, Some(&paths.proposed_dir), name).await?;
|
||||
ensure_claude_dir(&paths.claude_dir)?;
|
||||
ensure_state_dir(&paths.notes_dir)?;
|
||||
// Meta flake gets the new agent's input + nixosConfiguration
|
||||
// before `nixos-container create` so the `--flake meta#<name>`
|
||||
// ref resolves.
|
||||
let agents = agents_after_spawn(name).await?;
|
||||
crate::meta::sync_agents(
|
||||
hyperhive_flake,
|
||||
nixpkgs_flake,
|
||||
nixpkgs_unstable_flake,
|
||||
dashboard_port,
|
||||
operator_pronouns,
|
||||
context_window_tokens,
|
||||
&agents,
|
||||
)
|
||||
.await?;
|
||||
crate::meta::sync_agents(hive, &agents).await?;
|
||||
let container = container_name(name);
|
||||
priv_run("create", name).await?;
|
||||
set_nspawn_flags(&container, agent_dir, claude_dir, notes_dir).await?;
|
||||
set_resource_limits(&container, cpu_quota, memory_max).await?;
|
||||
set_nspawn_flags(&container, &paths.agent_dir, &paths.claude_dir, &paths.notes_dir).await?;
|
||||
set_resource_limits(&container, &hive.agent_cpu_quota, &hive.agent_memory_max).await?;
|
||||
systemd_daemon_reload().await?;
|
||||
priv_run("start", name).await
|
||||
}
|
||||
|
|
@ -389,21 +366,10 @@ pub async fn destroy(name: &str) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments, clippy::implicit_hasher)]
|
||||
pub async fn rebuild(
|
||||
name: &str,
|
||||
hyperhive_flake: &str,
|
||||
nixpkgs_flake: &str,
|
||||
nixpkgs_unstable_flake: &str,
|
||||
agent_dir: &Path,
|
||||
applied_dir: &Path,
|
||||
claude_dir: &Path,
|
||||
notes_dir: &Path,
|
||||
dashboard_port: u16,
|
||||
operator_pronouns: &str,
|
||||
context_window_tokens: &std::collections::HashMap<String, u64>,
|
||||
cpu_quota: &str,
|
||||
memory_max: &str,
|
||||
hive: &HiveEnv,
|
||||
paths: &AgentPaths,
|
||||
on_step: &(dyn Fn(&str) + Send + Sync),
|
||||
) -> Result<()> {
|
||||
// Sync the meta flake (idempotent — no-op when the rendered
|
||||
|
|
@ -412,21 +378,12 @@ pub async fn rebuild(
|
|||
// got added directly via `nixos-container create` outside
|
||||
// hive-c0re).
|
||||
let agents = agents_for_meta(None).await?;
|
||||
crate::meta::sync_agents(
|
||||
hyperhive_flake,
|
||||
nixpkgs_flake,
|
||||
nixpkgs_unstable_flake,
|
||||
dashboard_port,
|
||||
operator_pronouns,
|
||||
context_window_tokens,
|
||||
&agents,
|
||||
)
|
||||
.await?;
|
||||
crate::meta::sync_agents(hive, &agents).await?;
|
||||
// Then bump just this agent's input — picks up whatever
|
||||
// `applied/<n>/main` currently points at (deployed/<latest>).
|
||||
// Commits the lock if it changed.
|
||||
crate::meta::lock_update_for_rebuild(name).await?;
|
||||
rebuild_no_meta(name, agent_dir, applied_dir, claude_dir, notes_dir, cpu_quota, memory_max, on_step).await
|
||||
rebuild_no_meta(name, hive, paths, on_step).await
|
||||
}
|
||||
|
||||
/// Container-level rebuild without touching the meta repo. Callers
|
||||
|
|
@ -441,12 +398,8 @@ pub async fn rebuild(
|
|||
/// is not needed.
|
||||
pub async fn rebuild_no_meta(
|
||||
name: &str,
|
||||
agent_dir: &Path,
|
||||
applied_dir: &Path,
|
||||
claude_dir: &Path,
|
||||
notes_dir: &Path,
|
||||
cpu_quota: &str,
|
||||
memory_max: &str,
|
||||
hive: &HiveEnv,
|
||||
paths: &AgentPaths,
|
||||
on_step: &(dyn Fn(&str) + Send + Sync),
|
||||
) -> Result<()> {
|
||||
validate(name)?;
|
||||
|
|
@ -456,17 +409,17 @@ pub async fn rebuild_no_meta(
|
|||
agent_web_port(name)
|
||||
);
|
||||
}
|
||||
setup_applied(applied_dir, None, name).await?;
|
||||
ensure_claude_dir(claude_dir)?;
|
||||
ensure_state_dir(notes_dir)?;
|
||||
setup_applied(&paths.applied_dir, None, name).await?;
|
||||
ensure_claude_dir(&paths.claude_dir)?;
|
||||
ensure_state_dir(&paths.notes_dir)?;
|
||||
let container = container_name(name);
|
||||
let flake_ref = format!("{}#{name}", crate::meta::meta_dir().display());
|
||||
if container_exists(name).await {
|
||||
// Rebuild strategy: stop-before-update + pre-build.
|
||||
// See `docs/coordinator.md::Container lifecycle`.
|
||||
let was_running = is_running(name).await;
|
||||
set_nspawn_flags(&container, agent_dir, claude_dir, notes_dir).await?;
|
||||
set_resource_limits(&container, cpu_quota, memory_max).await?;
|
||||
set_nspawn_flags(&container, &paths.agent_dir, &paths.claude_dir, &paths.notes_dir).await?;
|
||||
set_resource_limits(&container, &hive.agent_cpu_quota, &hive.agent_memory_max).await?;
|
||||
systemd_daemon_reload().await?;
|
||||
if was_running {
|
||||
on_step("nix build");
|
||||
|
|
@ -537,8 +490,8 @@ pub async fn rebuild_no_meta(
|
|||
// See `docs/coordinator.md::Spawn path`.
|
||||
on_step("nixos-container create");
|
||||
priv_run("create", name).await?;
|
||||
set_nspawn_flags(&container, agent_dir, claude_dir, notes_dir).await?;
|
||||
set_resource_limits(&container, cpu_quota, memory_max).await?;
|
||||
set_nspawn_flags(&container, &paths.agent_dir, &paths.claude_dir, &paths.notes_dir).await?;
|
||||
set_resource_limits(&container, &hive.agent_cpu_quota, &hive.agent_memory_max).await?;
|
||||
systemd_daemon_reload().await?;
|
||||
on_step("nixos-container start");
|
||||
priv_run("start", name).await
|
||||
|
|
|
|||
Loading…
Reference in a new issue