Watch
0
0
Fork
You've already forked hyperhive
0

drop the applied repo: the hive keeps no copy of an agent's config, only the deploy markers. Refs #5913

This commit is contained in:
damocles 2026-11-01 17:19:41 +01:00
commit 862495530c

View file

@ -1,5 +1,5 @@
//! Startup convergence. Three phases, all idempotent and unguarded:
//! harness files, the applied repos, the agent-set files, plus the
//! harness files, the leftover applied repos, the agent-set files, plus the
//! removal of the legacy meta repo's leftovers. They re-run every
//! boot on purpose — each one is a no-op once its state is already
//! correct.
@ -23,29 +23,21 @@ use std::path::Path;
use std::time::Duration;
use anyhow::{Context, Result};
use tokio::process::Command;
use crate::coordinator::Coordinator;
use crate::lifecycle::{self, AGENT_PREFIX};
const KILL_SWITCH: &str = "HIVE_SKIP_META_MIGRATION";
/// Per-shellout timeout for the blocking startup convergence. `run` is
/// Timeout for the shellout in the blocking startup convergence. `run` is
/// awaited *before* the daemon starts serving (main.rs), so any child
/// process that wedges here freezes the whole daemon — admin socket +
/// dashboard included — with no diagnostics: a git shellout was observed
/// blocked for 86min under a concurrent `nixos-rebuild`. Every shellout
/// runs under a timeout that kills the child on elapse, so a stuck phase
/// degrades to a logged warning instead of a hung boot.
/// blocked for 86min under a concurrent `nixos-rebuild`. The shellout
/// runs under a timeout, so a stuck phase degrades to a logged warning
/// instead of a hung boot.
const GIT_TIMEOUT: Duration = Duration::from_mins(2);
/// Substring that identifies the *current* agent flake boilerplate.
/// Bumped whenever the template changes so the startup migration
/// re-renders existing agents onto the new shape. Today the marker
/// is the `flakeInputs` module-arg forwarding line — older templates
/// (raw `import ./agent.nix`) get rewritten on next hive-c0re start.
const MODULE_FLAKE_MARKER: &str = "_module.args.flakeInputs";
pub async fn run() -> Result<()> {
if std::env::var(KILL_SWITCH).is_ok() {
tracing::info!("migration: {KILL_SWITCH} set — skipping");
@ -80,26 +72,16 @@ pub async fn run() -> Result<()> {
Err(e) => tracing::warn!(error = ?e, "migration: removing the legacy meta .git failed"),
}
// Phase 1 + 2: per-agent applied repo.
tracing::debug!("migration: phase 1+2 (applied repo)");
// An archetype agent has no applied repo, and seeding one here would hand
// it a config it is not built from.
let archetypes = crate::archetypes::read_at(&crate::archetypes::path()).unwrap_or_else(|e| {
tracing::warn!(error = ?e, "migration: stored archetypes unreadable; treating every agent as custom");
crate::archetypes::Stored::new()
});
for name in &names {
if archetypes.contains_key(name.as_str()) {
continue;
}
tracing::debug!(%name, "migration: applied");
if let Err(e) = migrate_applied_repo(name.as_str()).await {
tracing::warn!(%name, error = ?e, "migration: applied repo rewrite failed");
}
// Phase 1: the applied repos an earlier hive kept per agent.
tracing::debug!("migration: phase 1 (leftover applied repos)");
match remove_applied_repos(&crate::paths::applied_root()) {
Ok(0) => {}
Ok(n) => tracing::info!(count = n, "migration: removed leftover applied repos"),
Err(e) => tracing::warn!(error = ?e, "migration: removing the applied repos failed"),
}
// Phase 3: the files that follow the agent set.
tracing::debug!("migration: phase 3 (agent_set refresh)");
// Phase 2: the files that follow the agent set.
tracing::debug!("migration: phase 2 (agent_set refresh)");
// `refresh` takes the agents it is given as the whole set, so an empty
// stand-in for an unreadable list would drop every agent from the topology.
let agents = match lifecycle::agents_for_meta_listing().await {
@ -185,89 +167,27 @@ async fn enumerate_agents() -> Result<Vec<hive_types::Ident>> {
.collect())
}
async fn migrate_applied_repo(name: &str) -> Result<()> {
migrate_applied_dir(name, &crate::paths::applied_dir(name)).await
}
/// Rewrites the applied repo at `dir` to the module-only flake. A missing
/// `flake.nix` counts as "marker absent"; any other read error fails the
/// migration so the agent's file is never overwritten unread.
async fn migrate_applied_dir(name: &str, dir: &Path) -> Result<()> {
if !dir.join(".git").exists() {
return Ok(());
}
let flake_path = dir.join("flake.nix");
let cur = match std::fs::read_to_string(&flake_path) {
Ok(cur) => cur,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(e) => return Err(e).with_context(|| format!("read {}", flake_path.display())),
/// Remove the per-agent `applied/<name>` repos an earlier hive kept in `root`,
/// destroyed agents' included. The forge's `agent-configs/<name>` is the only
/// copy of a config now, so nothing reads them. The dot-files beside them (the
/// deploy markers) stay. Returns how many it removed.
fn remove_applied_repos(root: &Path) -> Result<usize> {
let entries = match std::fs::read_dir(root) {
Ok(entries) => entries,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(0),
Err(e) => return Err(e).with_context(|| format!("reading {}", root.display())),
};
if cur.contains(MODULE_FLAKE_MARKER) {
return Ok(());
let mut removed = 0;
for entry in entries {
let entry = entry.with_context(|| format!("reading {}", root.display()))?;
if !entry.file_type()?.is_dir() {
continue;
}
let path = entry.path();
std::fs::remove_dir_all(&path).with_context(|| format!("removing {}", path.display()))?;
removed += 1;
}
let want = lifecycle::initial_flake_nix();
std::fs::write(&flake_path, want).with_context(|| format!("write {}", flake_path.display()))?;
raw_git(
dir,
&[
"-c",
"user.name=c0re",
"-c",
"user.email=c0re@hyperhive.local",
"add",
"flake.nix",
],
)
.await?;
raw_git(
dir,
&[
"-c",
"user.name=c0re",
"-c",
"user.email=c0re@hyperhive.local",
"commit",
"-m",
"migration: module-only flake",
],
)
.await?;
// Relocate deployed/0 to the migration commit so
// setup_applied's existence check passes.
raw_git(dir, &["tag", "-f", "deployed/0", "HEAD"]).await?;
tracing::info!(%name, "migration: applied repo migrated to module-only flake");
Ok(())
}
/// Run a command to completion under a timeout, capturing its output. On
/// timeout the child is killed (`kill_on_drop`) and an error is returned,
/// so a wedged shellout can never freeze startup migration. `what` is a
/// human label surfaced in the timeout error + `with_context`.
async fn output_with_timeout(
mut cmd: Command,
timeout: Duration,
what: &str,
) -> Result<std::process::Output> {
cmd.kill_on_drop(true);
match tokio::time::timeout(timeout, cmd.output()).await {
Ok(r) => r.with_context(|| format!("run {what}")),
Err(_) => anyhow::bail!("{what} timed out after {timeout:?} (child killed)"),
}
}
async fn raw_git(dir: &Path, args: &[&str]) -> Result<()> {
let mut cmd = lifecycle::git_command();
cmd.current_dir(dir).args(args);
let label = format!("git {} in {}", args.join(" "), dir.display());
let out = output_with_timeout(cmd, GIT_TIMEOUT, &label).await?;
if !out.status.success() {
anyhow::bail!(
"git {} failed: {}",
args.join(" "),
String::from_utf8_lossy(&out.stderr).trim()
);
}
Ok(())
Ok(removed)
}
#[cfg(test)]
@ -312,17 +232,29 @@ mod tests {
assert!(!remove_core_token(dir.path()).expect("second"), "no-op");
}
#[tokio::test]
async fn unreadable_flake_is_left_untouched() {
let dir = tempfile::tempdir().expect("tempdir");
std::fs::create_dir(dir.path().join(".git")).expect("mkdir .git");
let flake = dir.path().join("flake.nix");
let bytes = [0xff_u8, 0xfe, b'x'];
std::fs::write(&flake, bytes).expect("write flake");
#[test]
fn applied_repos_go_and_the_markers_beside_them_stay() {
let root = tempfile::tempdir().expect("tempdir");
for name in ["alice", "bob"] {
std::fs::create_dir_all(root.path().join(name).join(".git")).expect("mkdir repo");
std::fs::write(root.path().join(name).join("agent.nix"), "{}").expect("write config");
std::fs::write(root.path().join(format!(".{name}.config-rev")), "abc").expect("marker");
}
let res = migrate_applied_dir("t", dir.path()).await;
assert!(res.is_err(), "non-UTF-8 flake.nix must fail the migration");
assert_eq!(std::fs::read(&flake).expect("read back"), bytes);
assert_eq!(remove_applied_repos(root.path()).expect("first"), 2);
assert!(!root.path().join("alice").exists());
assert!(
root.path().join(".alice.config-rev").exists(),
"marker stays"
);
assert_eq!(
remove_applied_repos(root.path()).expect("second"),
0,
"no-op"
);
assert_eq!(
remove_applied_repos(&root.path().join("missing")).expect("absent root"),
0
);
}
}