Watch
0
0
Fork
You've already forked hyperhive
0

reconcile: start a stopped harness inside a running container

hive-priv gains AgentHarness { agent_name, action: probe | start }, which
runs systemctl -M h-<name> against the fixed unit hive-agent.service.
run_reconcile and the 1-min tick read the unit for a running wanted-up
agent and start it when it is inactive or failed. A hive-priv without the
op reads as unknown state and leaves reconcile container-only.
This commit is contained in:
atlas 2026-11-19 01:42:18 +01:00 • committed by mara
commit 36515c289d
2 changed files with 517 additions and 32 deletions

View file

@ -15,7 +15,8 @@ use hive_jobq::NodeId;
use super::model::NodeKind;
use crate::coordinator::{AgentPaths, Coordinator, HiveEnv};
use crate::power::{ReconcileAction, reconcile_action};
use crate::lifecycle::HarnessState;
use crate::power::{ReconcileAction, Wanted, reconcile_action};
use crate::stats::sweep_health::{self, SweepHealth};
/// Max time `Drain` waits for the harness to run its stop-checkpoint
@ -40,6 +41,10 @@ const PAUSE_ACK_TIMEOUT: std::time::Duration = GRACEFUL_STOP_TIMEOUT;
pub(super) trait Host: Sync {
/// Whether the agent's container unit is active.
async fn is_running(&self, name: &str) -> bool;
/// The state of the agent's harness unit inside its running container.
async fn harness_state(&self, name: &str) -> HarnessState;
/// Queue a start of the agent's harness unit inside its running container.
async fn start_harness(&self, name: &str) -> Result<()>;
/// Stop the agent's container.
async fn kill(&self, name: &str) -> Result<()>;
/// The record the swarm's builder wrote for the agent; `None` when it wrote
@ -106,6 +111,14 @@ impl Host for RealHost {
crate::lifecycle::is_running(name).await
}
async fn harness_state(&self, name: &str) -> HarnessState {
crate::lifecycle::harness_state(name).await
}
async fn start_harness(&self, name: &str) -> Result<()> {
crate::lifecycle::start_harness(name).await
}
async fn kill(&self, name: &str) -> Result<()> {
crate::lifecycle::kill(name).await
}
@ -846,12 +859,35 @@ async fn run_reconcile(
agent: name.to_owned(),
}),
ReconcileAction::Noop => {
if running && wanted == Wanted::Up {
restart_stopped_harness(host, name).await;
}
tracing::debug!(%name, wanted = <&str>::from(wanted), running, "reconcile: noop");
None
}
})
}
/// Start the harness unit of a wanted-up agent whose container is running but
/// whose harness is stopped or failed. Nothing else would: the unit restarts
/// only on failure, and a graceful stop exits cleanly.
///
/// Runs inside the `Reconcile` node, which holds the agent's lease, so no
/// other node of the agent is mid-stop; the periodic tick queues a `Reconcile`
/// only for an agent no live node targets. A paused agent keeps its harness
/// active (the pause marker parks the turn loop), so it never reads as
/// stopped. A start that fails is logged, not failed: the node belongs to a
/// DAG that may report a successful rebuild, and the tick tries again.
async fn restart_stopped_harness(host: &impl Host, name: &str) {
if host.harness_state(name).await != HarnessState::Stopped {
return;
}
tracing::info!(%name, "harness stopped inside running container; starting it");
if let Err(e) = host.start_harness(name).await {
tracing::warn!(%name, error = ?e, "starting the stopped harness failed");
}
}
/// Mechanical container start — the sub-step a `Reconcile` planner fans
/// out when it observes `wanted = Up` and the container down.
async fn run_start(host: &impl Host, coord: &Arc<Coordinator>, name: &str) -> Result<()> {

View file

@ -18,6 +18,7 @@ use super::{
};
use crate::coordinator::{AgentPaths, Coordinator, HiveEnv};
use crate::job_queue::model::NodeKind;
use crate::lifecycle::HarnessState;
use crate::power::Wanted;
use crate::stats::sweep_health::SweepHealth;
use crate::warnings::snapshot;
@ -30,6 +31,8 @@ const CLOSURE: &str = "/nix/store/0123456789abcdfghijklmnpqrsvwxyz-nixos-system-
#[derive(Default)]
struct FakeHost {
running: bool,
/// The harness unit inside the container is stopped.
harness_stopped: bool,
/// What the swarm's builder recorded for the agent; `None` reads as no
/// record.
built: Mutex<Option<AgentClosure>>,
@ -66,6 +69,12 @@ impl FakeHost {
}
}
/// The harness unit is stopped inside the running container.
fn harness_down(mut self) -> Self {
self.harness_stopped = true;
self
}
/// The builder recorded the closure for the agent, pushed or not.
fn built(self, cached: bool) -> Self {
*self.built.lock().unwrap() = Some(closure(cached));
@ -126,6 +135,19 @@ impl Host for FakeHost {
self.running
}
async fn harness_state(&self, name: &str) -> HarnessState {
self.record(format!("harness_state {name}"));
if self.harness_stopped {
HarnessState::Stopped
} else {
HarnessState::Present
}
}
async fn start_harness(&self, name: &str) -> anyhow::Result<()> {
self.step("start_harness", format!("start_harness {name}"))
}
async fn kill(&self, name: &str) -> anyhow::Result<()> {
self.record(format!("kill {name}"));
if self.fails("kill") {
@ -683,6 +705,67 @@ async fn reconcile_is_a_noop_when_state_matches_intent() {
assert!(sub.is_none());
}
/// The harness unit is down inside a running container and the agent is
/// wanted up: reconcile starts it and plans no container work.
#[tokio::test]
async fn reconcile_starts_a_stopped_harness_of_a_wanted_up_running_agent() {
let (_dir, coord) = coordinator();
coord.power.set("alice", Wanted::Up);
let host = FakeHost::up().harness_down();
let sub = run_reconcile(&host, &coord, "alice").await.expect("ok");
assert!(sub.is_none(), "the container is left alone");
assert_eq!(
host.calls(),
[
"is_running alice",
"harness_state alice",
"start_harness alice"
]
);
}
/// Each case that must not start the harness, against the control above.
#[tokio::test]
async fn reconcile_leaves_the_harness_alone_unless_the_agent_is_wanted_up_and_running() {
let (_dir, coord) = coordinator();
// A running harness (a paused agent keeps its harness active too).
coord.power.set("alice", Wanted::Up);
let host = FakeHost::up();
run_reconcile(&host, &coord, "alice").await.expect("ok");
assert!(!host.calls().contains(&"start_harness alice".to_owned()));
// Wanted offline: the container is stopped instead, the harness untouched.
coord.power.set("alice", Wanted::Offline);
let host = FakeHost::up().harness_down();
let sub = run_reconcile(&host, &coord, "alice").await.expect("ok");
assert!(matches!(sub, Some(NodeKind::Stop { .. })));
assert_eq!(host.calls(), ["is_running alice"]);
// Wanted offline and already down: nothing to do, the harness is not read.
coord.power.set("alice", Wanted::Offline);
let host = FakeHost::down().harness_down();
let sub = run_reconcile(&host, &coord, "alice").await.expect("ok");
assert!(sub.is_none());
assert_eq!(host.calls(), ["is_running alice"]);
// Container down: it is started, and the harness is not read.
coord.power.set("alice", Wanted::Up);
let host = FakeHost::down().harness_down();
let sub = run_reconcile(&host, &coord, "alice").await.expect("ok");
assert!(matches!(sub, Some(NodeKind::Start { .. })));
assert_eq!(host.calls(), ["is_running alice"]);
}
/// A failed harness start is logged; the reconcile node still succeeds.
#[tokio::test]
async fn a_failed_harness_start_does_not_fail_the_reconcile() {
let (_dir, coord) = coordinator();
coord.power.set("alice", Wanted::Up);
let host = FakeHost::up().harness_down().failing("start_harness");
run_reconcile(&host, &coord, "alice").await.expect("ok");
assert!(host.calls().contains(&"start_harness alice".to_owned()));
}
#[tokio::test]
async fn reconcile_seeds_a_missing_intent_from_the_observed_state() {
let (_dir, coord) = coordinator();