fix(#3124): close AgentState — an unknown value is not a partial instruction

mara's call on the PR: "dont make the enum open, we will just add
entries later". The catch-all variant is gone, and with it the per-agent
inert path.

What changes is where version skew lands, not whether it is handled. An
unrecognised value used to be one agent this hive left alone; it is now a
decode failure for the whole declaration, so a hive running older code
converges *nothing* rather than obeying the agents it happened to
understand. That fails closed instead of dangerous, and it is the right
trade when both ends ship together — which is what "add entries later"
assumes.

The test moved with the property rather than being rewritten in place:
`an_unknown_state_fails_the_whole_declaration` lives in
swarm-queue-client, where the decode is, with a valid entry beside it as
the control. `hive-c0re` keeps a coverage check that every state this
build knows produces an action somewhere — asserting inertness there
would be asserting something the type system no longer lets me build.
This commit is contained in:
atlas 2026-09-01 14:05:30 +02:00
commit a4924aee4d
2 changed files with 41 additions and 58 deletions

View file

@ -15,8 +15,9 @@
//! converging "to exactly this set" would tear down every agent the swarm has
//! not adopted.
//!
//! **An unrecognised state is inert**, so a controller that learns a new state
//! does not take agents down on hives that have not been updated yet.
//! **An unknown state is not a partial instruction.** [`AgentState`] is closed,
//! so a value this build cannot read fails the whole decode: the hive converges
//! nothing rather than obeying the agents it happened to understand.
//!
//! # What "diverged" is measured against
//!
@ -25,9 +26,8 @@
//! reconcile's work; a loop reading `is_running` would insert a start DAG
//! behind that reconcile's back on every boot.
//!
//! One consequence, deliberate: a declaration outranks a local stop. An agent
//! this hive is declared to keep `Up` comes back at the next boot, because the
//! controller owns that decision.
//! One consequence, deliberate: a declaration outranks a local stop — an agent
//! declared `Up` returns at the next boot, because the controller owns that.
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
@ -176,12 +176,6 @@ fn plan(
Converge::Start => plan.start.push(agent.clone()),
Converge::Stop => plan.stop.push(agent.clone()),
Converge::Nothing => {}
Converge::Inert => {
tracing::info!(
%agent, state = ?decl.state,
"wanted state: state not recognised by this build; leaving the agent alone"
);
}
}
}
plan
@ -197,8 +191,6 @@ enum Converge {
Stop,
/// The hive already agrees with the declaration.
Nothing,
/// A state this build does not recognise.
Inert,
}
/// The whole decision, pure: no queue, no store, no container. The three
@ -206,8 +198,6 @@ enum Converge {
/// the behaviour rather than a model of it.
fn decide(state: &AgentState, present: bool, intent: Option<Wanted>) -> Converge {
match state {
// First, and whatever else is true of the agent.
AgentState::Unrecognised(_) => Converge::Inert,
AgentState::Up if !present => Converge::Deploy,
AgentState::Up => {
if intent == Some(Wanted::Up) {
@ -237,10 +227,6 @@ mod tests {
use crate::power::Wanted;
use swarm_queue_client::wanted::{AgentState, AgentWanted, HiveWanted};
fn unknown() -> AgentState {
AgentState::Unrecognised("paused".to_owned())
}
fn declaration(agents: &[(&str, AgentState)]) -> HiveWanted {
HiveWanted {
agents: agents
@ -354,14 +340,20 @@ mod tests {
assert_eq!(decide(&AgentState::Offline, false, None), Converge::Nothing);
}
/// The version-skew arm: an unrecognised state is inert in every
/// combination, including the ones where a known state would act.
/// Version skew is handled one layer up, at the decode: `AgentState` is
/// closed, so an unknown value never reaches [`decide`] — it fails the
/// whole declaration in `swarm-queue-client`, which owns that test
/// (`an_unknown_state_fails_the_whole_declaration`). There is deliberately
/// no case for it here: a test asserting something unrepresentable would
/// pass forever without measuring anything.
#[test]
fn an_unrecognised_state_is_inert_everywhere() {
for present in [true, false] {
for intent in [None, Some(Wanted::Up), Some(Wanted::Offline)] {
assert_eq!(decide(&unknown(), present, intent), Converge::Inert);
}
fn every_state_this_build_knows_is_covered_above() {
for state in [AgentState::Up, AgentState::Offline] {
let seen = [true, false].iter().any(|present| {
decide(&state, *present, None) != Converge::Nothing
|| decide(&state, *present, Some(Wanted::Up)) != Converge::Nothing
});
assert!(seen, "{state:?} produces no action in any combination");
}
}
}