Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/swarm-wire/src/builder.rs
iris 1a31a4cd5a swarm: set an agent's config repo avatar from its icon
The agent module exposes the PNG nix renders from the agent's icon as the
read-only option services.hyperhive.agent.iconPng, which the forge and matrix
avatar syncs now share. When the builder reads an icon it builds that one
attribute with nix build and sends the file, base64, next to the SVG in the
meta build reply, so the builder runs no renderer of its own.

After recording the icon, the controller sets the avatar of
agent-configs/<agent> from the PNG as the forge admin, removes the avatar when
the icon is removed, and leaves it alone when the builder sent no PNG. A marker
per repo holds the image's fingerprint, so a build whose icon did not change
makes no forge call; a forge that refuses the avatar does not fail the build.
An old builder sends no PNG, so it changes nothing.
2026-11-23 10:11:16 +01:00

466 lines
17 KiB
Rust

//! The swarm builder's requests: the subjects swarm-controller sends a build,
//! an archetype evaluation, a meta-flake lock and an agent build from the meta
//! flake on, and what travels each way.
//!
//! Three crates name these: the controller sends the requests, the builder
//! answers them, and the auth-callout responder grants the subjects to each.
use std::time::Duration;
use hive_types::NixBuilds;
use serde::{Deserialize, Serialize};
/// The subject every build goes to. The controller is its one publisher and
/// the builder its one subscriber; the builder answers on the request's reply
/// subject.
pub const SUBJECT: &str = "$SWARM.builder.build";
/// The queue group the builder subscribes in, so a second instance shares the
/// requests instead of running each one twice.
pub const QUEUE_GROUP: &str = "builder";
/// The longest one build may take before the builder stops it and answers
/// with a timeout.
pub const BUILD_DEADLINE: Duration = Duration::from_hours(1);
/// How long the controller waits for the reply, and the queue lets the
/// builder answer: the build at its deadline, plus a minute the builder spends
/// on a request's wait for the build slot, the forge token read before the
/// build and the reply's trip back.
pub const REPLY_DEADLINE: Duration = Duration::from_mins(61);
/// One build: an agent's config repo at one commit, on the builder's own
/// agent base.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BuildAgent {
/// The agent, which is also its config repo's name.
pub agent: String,
/// The commit to build.
pub rev: String,
/// The PR the commit is the head of; its `refs/pull/<pr>/head` is the ref
/// the builder fetches the commit through.
pub pr: u64,
}
/// The builder's answer to one [`BuildAgent`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "kebab-case")]
pub enum Reply {
/// The config built; `out_path` is the system closure's store path.
Built { out_path: String },
/// The build did not finish: the last lines `nix` printed, or why the
/// builder could not start it.
Failed { error_tail: String },
}
/// The subject an archetype evaluation goes to: the controller asks, the
/// builder evaluates the `meta` of every `hyperhiveArchetypes` entry of its own
/// hyperhive source and answers on the request's reply subject.
pub const ARCHETYPES_SUBJECT: &str = "$SWARM.builder.archetypes";
/// The longest one archetype evaluation may take.
pub const ARCHETYPES_DEADLINE: Duration = Duration::from_mins(5);
/// How long the controller waits for an evaluation's reply: the evaluation at
/// its deadline, plus a minute for its timeout reply's trip back.
pub const ARCHETYPES_REPLY_DEADLINE: Duration = Duration::from_mins(6);
/// One archetype evaluation. No field yet: the builder evaluates the hyperhive
/// source it was deployed with.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct EvalArchetypes {}
/// The builder's answer to one [`EvalArchetypes`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "kebab-case")]
pub enum ArchetypesReply {
/// `source` is the flake evaluated; `archetypes` maps each archetype's
/// name to its `meta`, as `nix eval --json` printed it and unchecked.
Evaluated {
source: String,
archetypes: serde_json::Map<String, serde_json::Value>,
},
/// The evaluation did not finish: the last lines `nix` printed, or why the
/// builder could not start it.
Failed { error_tail: String },
}
/// The subject a swarm meta-flake lock goes to, with the same one publisher and
/// one subscriber as [`SUBJECT`].
pub const LOCK_SUBJECT: &str = "$SWARM.builder.lock-meta";
/// The longest one lock may take. A lock fetches every input's source and
/// builds nothing.
pub const LOCK_DEADLINE: Duration = Duration::from_mins(10);
/// How long the controller waits for a lock's reply: the lock at its deadline,
/// plus a minute for the rest.
pub const LOCK_REPLY_DEADLINE: Duration = Duration::from_mins(11);
/// One lock: the swarm meta flake's `flake.nix`, locked on its own in a scratch
/// directory. The builder writes nothing anywhere else and commits nothing; the
/// controller commits the returned lock beside the flake.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LockMeta {
/// The rendered `flake.nix`.
pub flake_nix: String,
}
/// The builder's answer to one [`LockMeta`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "kebab-case")]
pub enum LockReply {
/// The flake locked; `flake_lock` is the `flake.lock` `nix` wrote.
Locked { flake_lock: String },
/// The lock did not finish: the last lines `nix` printed, or why the
/// builder could not start it.
Failed { error_tail: String },
}
/// The subject an agent build from the swarm meta flake goes to, with the same
/// one publisher and one subscriber as [`SUBJECT`]. A subject of its own, so a
/// builder that predates the request has no subscriber and the controller gets
/// no responders at once.
pub const META_SUBJECT: &str = "$SWARM.builder.build-meta";
/// The swarm meta flake's repo on the forge, as `<org>/<repo>`.
pub const META_REPO_PATH: &str = "internal/meta";
/// One agent built from the swarm meta flake: the toplevel of
/// `nixosConfigurations.<agent>`, with every input at the flake's own lock.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BuildFromMeta {
/// The agent, which is also its name in the meta flake.
pub agent: String,
/// The `internal/meta` commit to build, a full 40-hex id.
pub meta_rev: String,
}
/// What an agent's `services.hyperhive.agent.icon` came to in its evaluated
/// config.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "kebab-case")]
pub enum IconRead {
/// The option names an SVG file; `svg` is its text, and `png` is the
/// builder's rendering of it, base64, for the forge, whose avatars are
/// bitmaps. `png` is absent when the builder could not render it, and from a
/// builder that predates it; neither removes an avatar.
Set {
svg: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
png: Option<String>,
},
/// The option is not set: the agent has no icon, and an icon it had is
/// removed.
Unset,
}
/// The builder's answer to one [`BuildFromMeta`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "kebab-case")]
pub enum FromMetaReply {
/// The toplevel built; `out_path` is its store path in the builder host's
/// store and `system` the platform the builder host builds for. `cached`
/// is whether the builder pushed the closure to the binary cache; a failed
/// push leaves the build green with `cached` false. A builder that predates
/// the field answers without it, which reads as false.
Built {
out_path: String,
system: String,
#[serde(default)]
cached: bool,
/// Whether the agent's evaluated config sets
/// `services.hyperhive.agent.user.privateUsers`. Absent (`None`) when
/// the builder could not read it: its eval failed or timed out. A
/// builder that predates the field answers without it too.
#[serde(default, skip_serializing_if = "Option::is_none")]
private_users: Option<bool>,
/// The agent's `services.hyperhive.agent.nixBuilds`. Absent (`None`)
/// when the builder could not read it, as for `private_users`.
#[serde(default, skip_serializing_if = "Option::is_none")]
nix_builds: Option<NixBuilds>,
/// The agent's `services.hyperhive.agent.icon`. Absent (`None`) when
/// the builder could not read it, as for `private_users`: the
/// controller then leaves the agent's icon as it is.
#[serde(default, skip_serializing_if = "Option::is_none")]
icon: Option<IconRead>,
},
/// The build did not succeed: the last lines `nix` printed, or why it
/// stopped (it timed out, or could not start). The controller holds the
/// deploy back and builds again on its next round.
Failed { error_tail: String },
/// The builder did not get to run `nix`: the build slot stayed busy or the
/// forge token could not be read. The controller holds the deploy back and
/// builds again on its next round.
Unavailable { reason: String },
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
#[test]
fn the_build_request_serializes_as_pinned() {
let req = BuildAgent {
agent: "atlas".to_owned(),
rev: "abc".to_owned(),
pr: 7,
};
let value = serde_json::to_value(&req).expect("encodes");
assert_eq!(value, json!({"agent": "atlas", "rev": "abc", "pr": 7}));
assert_eq!(
serde_json::from_value::<BuildAgent>(value).expect("decodes"),
req
);
}
#[test]
fn the_reply_serializes_as_pinned() {
for (reply, wire) in [
(
Reply::Built {
out_path: "/nix/store/x".to_owned(),
},
json!({"state": "built", "out_path": "/nix/store/x"}),
),
(
Reply::Failed {
error_tail: "e".to_owned(),
},
json!({"state": "failed", "error_tail": "e"}),
),
] {
assert_eq!(serde_json::to_value(&reply).expect("encodes"), wire);
assert_eq!(
serde_json::from_value::<Reply>(wire).expect("decodes"),
reply
);
}
}
#[test]
fn the_archetypes_request_and_reply_serialize_as_pinned() {
let req = EvalArchetypes::default();
let value = serde_json::to_value(&req).expect("encodes");
assert_eq!(value, json!({}));
assert_eq!(
serde_json::from_value::<EvalArchetypes>(value).expect("decodes"),
req
);
let mut archetypes = serde_json::Map::new();
archetypes.insert("coder".to_owned(), json!({"description": "d"}));
for (reply, wire) in [
(
ArchetypesReply::Evaluated {
source: "/nix/store/h".to_owned(),
archetypes,
},
json!({
"state": "evaluated",
"source": "/nix/store/h",
"archetypes": {"coder": {"description": "d"}},
}),
),
(
ArchetypesReply::Failed {
error_tail: "e".to_owned(),
},
json!({"state": "failed", "error_tail": "e"}),
),
] {
assert_eq!(serde_json::to_value(&reply).expect("encodes"), wire);
assert_eq!(
serde_json::from_value::<ArchetypesReply>(wire).expect("decodes"),
reply
);
}
}
#[test]
fn the_meta_build_request_and_reply_serialize_as_pinned() {
let req = BuildFromMeta {
agent: "atlas".to_owned(),
meta_rev: "abc".to_owned(),
};
let value = serde_json::to_value(&req).expect("encodes");
assert_eq!(value, json!({"agent": "atlas", "meta_rev": "abc"}));
assert_eq!(
serde_json::from_value::<BuildFromMeta>(value).expect("decodes"),
req
);
for (reply, wire) in [
(
FromMetaReply::Built {
out_path: "/nix/store/x".to_owned(),
system: "x86_64-linux".to_owned(),
cached: true,
private_users: Some(true),
nix_builds: Some(NixBuilds::Disabled),
icon: Some(IconRead::Set {
svg: "<svg/>".to_owned(),
png: None,
}),
},
json!({
"state": "built",
"out_path": "/nix/store/x",
"system": "x86_64-linux",
"cached": true,
"private_users": true,
"nix_builds": "none",
"icon": {"state": "set", "svg": "<svg/>"},
}),
),
(
FromMetaReply::Built {
out_path: "/nix/store/x".to_owned(),
system: "x86_64-linux".to_owned(),
cached: false,
private_users: None,
nix_builds: None,
icon: Some(IconRead::Unset),
},
json!({
"state": "built",
"out_path": "/nix/store/x",
"system": "x86_64-linux",
"cached": false,
"icon": {"state": "unset"},
}),
),
(
FromMetaReply::Failed {
error_tail: "e".to_owned(),
},
json!({"state": "failed", "error_tail": "e"}),
),
(
FromMetaReply::Unavailable {
reason: "r".to_owned(),
},
json!({"state": "unavailable", "reason": "r"}),
),
] {
assert_eq!(serde_json::to_value(&reply).expect("encodes"), wire);
assert_eq!(
serde_json::from_value::<FromMetaReply>(wire).expect("decodes"),
reply
);
}
}
#[test]
fn a_built_reply_from_a_builder_without_the_fields_reads_as_not_cached_and_unknown() {
let old = json!({"state": "built", "out_path": "/nix/store/x", "system": "s"});
assert_eq!(
serde_json::from_value::<FromMetaReply>(old).expect("decodes"),
FromMetaReply::Built {
out_path: "/nix/store/x".to_owned(),
system: "s".to_owned(),
cached: false,
private_users: None,
nix_builds: None,
icon: None,
}
);
}
/// A value the builder could not read is left out of the reply, not sent as
/// `null`: a controller that predates the option still decodes it.
#[test]
fn an_unread_option_is_left_out_of_the_built_reply() {
// The shape a reader before the option had: a bool that defaults to false.
#[derive(serde::Deserialize)]
struct Before {
#[serde(default)]
private_users: bool,
}
let reply = FromMetaReply::Built {
out_path: "/nix/store/x".to_owned(),
system: "s".to_owned(),
cached: false,
private_users: None,
nix_builds: Some(NixBuilds::Host),
icon: None,
};
let wire = serde_json::to_value(&reply).expect("encodes");
let fields = wire.as_object().expect("an object");
assert!(!fields.contains_key("private_users"), "{wire}");
assert_eq!(fields["nix_builds"], "host");
assert!(!fields.contains_key("icon"), "{wire}");
assert!(
!serde_json::from_value::<Before>(wire.clone())
.expect("decodes")
.private_users
);
assert_eq!(
serde_json::from_value::<FromMetaReply>(wire).expect("decodes"),
reply
);
}
#[test]
fn the_subjects_are_pinned() {
assert_eq!(SUBJECT, "$SWARM.builder.build");
assert_eq!(ARCHETYPES_SUBJECT, "$SWARM.builder.archetypes");
assert_eq!(LOCK_SUBJECT, "$SWARM.builder.lock-meta");
assert_eq!(META_SUBJECT, "$SWARM.builder.build-meta");
assert_eq!(QUEUE_GROUP, "builder");
}
#[test]
fn the_reply_deadline_outlasts_the_build() {
assert!(REPLY_DEADLINE > BUILD_DEADLINE);
}
#[test]
fn the_archetypes_deadline_is_five_minutes() {
assert_eq!(ARCHETYPES_DEADLINE, Duration::from_mins(5));
}
#[test]
fn the_archetypes_reply_deadline_outlasts_the_evaluation() {
assert!(ARCHETYPES_REPLY_DEADLINE > ARCHETYPES_DEADLINE);
}
#[test]
fn the_lock_reply_deadline_outlasts_the_lock() {
assert!(LOCK_REPLY_DEADLINE > LOCK_DEADLINE);
}
#[test]
fn the_lock_request_and_reply_serialize_as_pinned() {
let req = LockMeta {
flake_nix: "{ }".to_owned(),
};
let value = serde_json::to_value(&req).expect("encodes");
assert_eq!(value, json!({"flake_nix": "{ }"}));
assert_eq!(
serde_json::from_value::<LockMeta>(value).expect("decodes"),
req
);
for (reply, wire) in [
(
LockReply::Locked {
flake_lock: "{}".to_owned(),
},
json!({"state": "locked", "flake_lock": "{}"}),
),
(
LockReply::Failed {
error_tail: "e".to_owned(),
},
json!({"state": "failed", "error_tail": "e"}),
),
] {
assert_eq!(serde_json::to_value(&reply).expect("encodes"), wire);
assert_eq!(
serde_json::from_value::<LockReply>(wire).expect("decodes"),
reply
);
}
}
}