move the builder and claude login wire contracts into swarm-wire
This commit is contained in:
parent
f99e502634
commit
0c3d128de1
23 changed files with 224 additions and 81 deletions
178
swarm-wire/src/builder.rs
Normal file
178
swarm-wire/src/builder.rs
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
//! The swarm builder's requests: the subjects swarm-controller sends a build
|
||||
//! and an archetype evaluation 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 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, and how long the controller
|
||||
/// waits for its reply.
|
||||
pub const ARCHETYPES_DEADLINE: Duration = Duration::from_mins(2);
|
||||
|
||||
/// 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 },
|
||||
}
|
||||
|
||||
#[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_subjects_are_pinned() {
|
||||
assert_eq!(SUBJECT, "$SWARM.builder.build");
|
||||
assert_eq!(ARCHETYPES_SUBJECT, "$SWARM.builder.archetypes");
|
||||
assert_eq!(QUEUE_GROUP, "builder");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_reply_deadline_outlasts_the_build() {
|
||||
assert!(REPLY_DEADLINE > BUILD_DEADLINE);
|
||||
}
|
||||
}
|
||||
142
swarm-wire/src/claude_login.rs
Normal file
142
swarm-wire/src/claude_login.rs
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
//! The claude login minter's requests: the subject swarm-controller sends an
|
||||
//! operator's login steps on, and what travels each way.
|
||||
//!
|
||||
//! Three crates name these: the controller sends the requests, the minter
|
||||
//! answers them, and the auth-callout responder grants the one subject to
|
||||
//! each.
|
||||
//!
|
||||
//! A login is two requests for one agent:
|
||||
//! [`Request::Start`](crate::claude_login::Request::Start) runs
|
||||
//! `claude auth login` and answers with the URL it printed, and
|
||||
//! [`Request::Code`](crate::claude_login::Request::Code) hands it the code the
|
||||
//! operator pasted and answers once the credentials are in the agent's store
|
||||
//! leaf. The running login lives in the minter's memory between the two, so
|
||||
//! the swarm runs one minter.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// The subject every request goes to. The controller is its one publisher
|
||||
/// and the minter its one subscriber; the minter answers on the request's
|
||||
/// reply subject.
|
||||
pub const SUBJECT: &str = "$SWARM.claude-login.request";
|
||||
|
||||
/// The longest the minter waits for `claude` to print the login URL, and
|
||||
/// then for it to finish once the code is in.
|
||||
pub const STEP_DEADLINE: Duration = Duration::from_mins(1);
|
||||
|
||||
/// How long the controller waits for a reply, and the queue lets the minter
|
||||
/// answer: a step at its deadline, plus the store write.
|
||||
pub const REPLY_DEADLINE: Duration = Duration::from_secs(90);
|
||||
|
||||
/// How long a started login waits for its code before the minter stops it
|
||||
/// and deletes its scratch directory.
|
||||
pub const SESSION_DEADLINE: Duration = Duration::from_mins(15);
|
||||
|
||||
/// One step of an operator's login of one agent.
|
||||
///
|
||||
/// No `Debug` derive: [`Request::Code`] carries the one-time code.
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "step", rename_all = "kebab-case")]
|
||||
pub enum Request {
|
||||
/// Start a login for `agent`, replacing one already running for it.
|
||||
Start {
|
||||
agent: String,
|
||||
/// The swarm UI user, as the gateway named them.
|
||||
operator: String,
|
||||
},
|
||||
/// Hand the running login for `agent` the code the operator pasted.
|
||||
Code { agent: String, code: String },
|
||||
/// Stop the running login for `agent`, if any.
|
||||
Cancel { agent: String },
|
||||
}
|
||||
|
||||
/// The minter's answer to one [`Request`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "state", rename_all = "kebab-case")]
|
||||
pub enum Reply {
|
||||
/// The login is running; the operator opens `url` and pastes the code it
|
||||
/// shows back.
|
||||
Started { url: String },
|
||||
/// The credentials are stored in the agent's leaf, as its newest
|
||||
/// version.
|
||||
Stored,
|
||||
/// The running login, if there was one, is stopped.
|
||||
Cancelled,
|
||||
/// The step failed: why, with the last lines `claude` printed where it
|
||||
/// printed any.
|
||||
Failed { error: String },
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_request_serializes_as_pinned() {
|
||||
for (req, wire) in [
|
||||
(
|
||||
Request::Start {
|
||||
agent: "atlas".to_owned(),
|
||||
operator: "mara".to_owned(),
|
||||
},
|
||||
json!({"step": "start", "agent": "atlas", "operator": "mara"}),
|
||||
),
|
||||
(
|
||||
Request::Code {
|
||||
agent: "atlas".to_owned(),
|
||||
code: "c".to_owned(),
|
||||
},
|
||||
json!({"step": "code", "agent": "atlas", "code": "c"}),
|
||||
),
|
||||
(
|
||||
Request::Cancel {
|
||||
agent: "atlas".to_owned(),
|
||||
},
|
||||
json!({"step": "cancel", "agent": "atlas"}),
|
||||
),
|
||||
] {
|
||||
assert_eq!(serde_json::to_value(&req).expect("encodes"), wire);
|
||||
assert!(serde_json::from_value::<Request>(wire).expect("decodes") == req);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_reply_serializes_as_pinned() {
|
||||
for (reply, wire) in [
|
||||
(
|
||||
Reply::Started {
|
||||
url: "https://u".to_owned(),
|
||||
},
|
||||
json!({"state": "started", "url": "https://u"}),
|
||||
),
|
||||
(Reply::Stored, json!({"state": "stored"})),
|
||||
(Reply::Cancelled, json!({"state": "cancelled"})),
|
||||
(
|
||||
Reply::Failed {
|
||||
error: "e".to_owned(),
|
||||
},
|
||||
json!({"state": "failed", "error": "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_subject_is_pinned() {
|
||||
assert_eq!(SUBJECT, "$SWARM.claude-login.request");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_reply_deadline_outlasts_a_step() {
|
||||
assert!(REPLY_DEADLINE > STEP_DEADLINE);
|
||||
}
|
||||
}
|
||||
9
swarm-wire/src/lib.rs
Normal file
9
swarm-wire/src/lib.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
//! The request and reply shapes swarm services exchange over the queue: the
|
||||
//! subjects, their deadlines and the serde types that travel each way.
|
||||
//!
|
||||
//! Serde only, with no queue client and no secret-store client. A crate that
|
||||
//! names a contract (the sender, the answerer, the auth-callout responder that
|
||||
//! grants the subject) depends on this one without linking either.
|
||||
|
||||
pub mod builder;
|
||||
pub mod claude_login;
|
||||
Loading…
Reference in a new issue