use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use super::HiveError;
use super::convert::ConvertApi;
pub const MAX_SPLIT_CHILDREN: usize = 50;
pub fn eligible(goal: &Value, worker_id: &str) -> bool {
["conductor_workers", "delegates"].iter().any(|list| {
goal["swarm"][list]
.as_array()
.is_some_and(|ids| ids.iter().any(|id| id.as_str() == Some(worker_id)))
})
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Lease {
pub epoch: u64,
pub expires_at: u64,
pub holder: Holder,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Holder {
pub worker_id: String,
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Child {
pub title: String,
pub text: String,
pub acceptance: Vec<String>,
pub kind: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "action", rename_all = "snake_case", deny_unknown_fields)]
pub enum Action {
Split {
parent: String,
children: Vec<Child>,
},
Assign {
goal: String,
assignee: String,
},
Synthesize {
parent: String,
title: String,
},
Block {
goal: String,
reason: String,
},
Unblock {
goal: String,
},
Drop {
goal: String,
reason: String,
},
Reopen {
goal: String,
reason: String,
},
Edit {
goal: String,
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
acceptance: Option<Vec<String>>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Refusal {
NotConductor,
WorkerNotOwned,
Busy { holder: Option<String> },
StaleEpoch,
Other { status: u16, code: String },
}
impl Refusal {
pub fn code(&self) -> &str {
match self {
Self::NotConductor => "not_conductor",
Self::WorkerNotOwned => "worker_not_owned",
Self::Busy { .. } => "conductor_busy",
Self::StaleEpoch => "stale_epoch",
Self::Other { code, .. } => code,
}
}
fn of(status: u16, body: &Value) -> Self {
let e = &body["error"];
let reason = e["reason"].as_str().unwrap_or_default();
let code = e["code"].as_str().unwrap_or_default();
match (status, code, reason) {
(403, _, "not_conductor") => Self::NotConductor,
(403, _, "worker_not_owned") => Self::WorkerNotOwned,
(409, _, "conductor_busy") => Self::Busy {
holder: e["holder"]["worker_id"].as_str().map(str::to_string),
},
(409, "stale_epoch", _) => Self::StaleEpoch,
_ => Self::Other {
status,
code: if reason.is_empty() { code } else { reason }.to_string(),
},
}
}
}
impl From<Refusal> for HiveError {
fn from(r: Refusal) -> Self {
HiveError::Refused(format!("conductor: {}", r.code()))
}
}
pub struct Conductor<'a> {
pub api: &'a dyn ConvertApi,
pub swarm_id: &'a str,
pub worker_id: &'a str,
pub eligible: bool,
}
fn ok_or(status: u16, body: Value) -> Result<Value, Result<Refusal, HiveError>> {
if (200..300).contains(&status) {
Ok(body)
} else {
Err(Ok(Refusal::of(status, &body)))
}
}
pub type Answer<T> = Result<T, Result<Refusal, HiveError>>;
impl Conductor<'_> {
fn gate(&self) -> Answer<()> {
if self.eligible {
Ok(())
} else {
Err(Ok(Refusal::NotConductor))
}
}
fn base(&self) -> String {
format!("/swarms/{}/conductor", self.swarm_id)
}
fn lease_from(body: Value) -> Answer<Lease> {
serde_json::from_value(body)
.map_err(|e| Err(HiveError::Protocol(format!("conductor lease: {e}"))))
}
pub fn acquire(&self) -> Answer<Lease> {
self.gate()?;
let (status, body) = self
.api
.answer(
"POST",
&format!("{}/lease", self.base()),
Some(&json!({ "worker_id": self.worker_id })),
)
.map_err(Err)?;
Self::lease_from(ok_or(status, body)?)
}
pub fn renew(&self, epoch: u64) -> Answer<Lease> {
self.gate()?;
let (status, body) = self
.api
.answer(
"POST",
&format!("{}/lease/renew", self.base()),
Some(&json!({ "worker_id": self.worker_id, "epoch": epoch })),
)
.map_err(Err)?;
Self::lease_from(ok_or(status, body)?)
}
pub fn release(&self, epoch: u64) -> Answer<()> {
self.gate()?;
let (status, body) = self
.api
.answer(
"DELETE",
&format!(
"{}/lease?epoch={epoch}&worker_id={}",
self.base(),
self.worker_id
),
None,
)
.map_err(Err)?;
ok_or(status, body).map(|_| ())
}
pub fn action_body(&self, epoch: u64, action: &Action) -> Result<Value, HiveError> {
if let Action::Split { children, .. } = action
&& !(1..=MAX_SPLIT_CHILDREN).contains(&children.len())
{
return Err(HiveError::Config(format!(
"a split has 1 to {MAX_SPLIT_CHILDREN} children"
)));
}
let mut body = serde_json::to_value(action).map_err(|e| HiveError::Local(e.to_string()))?;
body["worker_id"] = self.worker_id.into();
body["epoch"] = epoch.into();
Ok(body)
}
pub fn act(&self, epoch: u64, action: &Action) -> Answer<Value> {
self.gate()?;
let body = self.action_body(epoch, action).map_err(Err)?;
let (status, answer) = self
.api
.answer("POST", &format!("{}/actions", self.base()), Some(&body))
.map_err(Err)?;
ok_or(status, answer)
}
}
pub fn cli(
session: &crate::core::hive::auth::Session,
args: &[String],
) -> Result<String, HiveError> {
let usage = || {
HiveError::Config(
"usage: lean-ctx hive swarm plan <swarm> --worker <wkr_…> lease | renew <epoch> | release <epoch> | act <epoch> --action '<json>'"
.into(),
)
};
let mut positional = Vec::new();
let (mut worker, mut action) = (None, None);
let mut it = args.iter();
while let Some(a) = it.next() {
match a.as_str() {
"--worker" => worker = it.next().cloned(),
"--action" => action = it.next().cloned(),
_ => positional.push(a.clone()),
}
}
let (Some(swarm), Some(worker)) = (positional.first(), worker) else {
return Err(usage());
};
let epoch = |i: usize| -> Result<u64, HiveError> {
positional
.get(i)
.and_then(|e| e.parse().ok())
.ok_or_else(usage)
};
let goal =
session.with_client(|c| c.get(&format!("/swarms/{swarm}/goal?worker_id={worker}")))?;
let c = Conductor {
api: session,
swarm_id: swarm,
worker_id: &worker,
eligible: eligible(&goal, &worker),
};
let flat = |r: Result<Refusal, HiveError>| match r {
Ok(refusal) => HiveError::from(refusal),
Err(e) => e,
};
match positional.get(1).map(String::as_str) {
Some("lease") => {
let l = c.acquire().map_err(flat)?;
Ok(format!(
"Conductor lease epoch {} until {} (renew within 5 min)",
l.epoch, l.expires_at
))
}
Some("renew") => {
let l = c.renew(epoch(2)?).map_err(flat)?;
Ok(format!(
"Conductor lease epoch {} until {}",
l.epoch, l.expires_at
))
}
Some("release") => {
c.release(epoch(2)?).map_err(flat)?;
Ok("Conductor lease released".into())
}
Some("act") => {
let action: Action = serde_json::from_str(action.as_deref().ok_or_else(usage)?)
.map_err(|e| HiveError::Config(format!("--action: {e}")))?;
let answer = c.act(epoch(2)?, &action).map_err(flat)?;
Ok(format!(
"Done: {} goal node(s) changed, swarm log seq(s) {}",
answer["nodes"].as_array().map_or(0, Vec::len),
answer["seqs"]
))
}
_ => Err(usage()),
}
}
#[cfg(test)]
#[path = "conductor_tests.rs"]
mod tests;