diff --git a/hive-agent/src/swarm_agent_commands.rs b/hive-agent/src/swarm_agent_commands.rs index 9ff8c9c14..fe8ad2193 100644 --- a/hive-agent/src/swarm_agent_commands.rs +++ b/hive-agent/src/swarm_agent_commands.rs @@ -14,8 +14,8 @@ //! with an empty description. //! //! **Once per change.** The list is published when the first turn reports it -//! and again whenever a later turn reports a different one, so an unchanged -//! list costs nothing. Best-effort: [`crate::swarm_kv_put`] retries a failed +//! and again whenever a later turn reports different names, so an unchanged +//! list costs nothing, not even the reads of the skill files. Best-effort: [`crate::swarm_kv_put`] retries a failed //! write, and the harness never waits on it. The agent's own queue credential //! is granted exactly this agent's key. @@ -46,23 +46,45 @@ pub fn spawn() { tokio::spawn(run(agent)); } -/// Hear about one stream event: an init event with a different skill list than +/// Hear about one stream event: an init event with different skill names than /// the last one makes the new list the one to publish. Every other event is -/// ignored. +/// ignored, and an init event with the same names as the last one costs no file +/// reads: a description changes only with a plugin update, which restarts claude. pub fn observe_init(event: &Value) { if !is_init(event) { return; } + let tx = latest(); + if init_names(event).is_some_and(|names| names_of(tx.borrow().as_ref()) == Some(names)) { + return; + } let Some(skills) = skills_from_init(event, &home_skills_dir()) else { return; }; - latest().send_if_modified(|current| { + offer(tx, skills); +} + +/// Make `skills` the list to publish, unless it already is. Whether it changed. +fn offer(tx: &watch::Sender>, skills: Skills) -> bool { + tx.send_if_modified(|current| { if current.as_ref() == Some(&skills) { return false; } *current = Some(skills); true - }); + }) +} + +/// The skill names of an init event, in its order, or `None` without a `skills` +/// array. +fn init_names(event: &Value) -> Option> { + let names = event.get("skills")?.as_array()?; + Some(names.iter().filter_map(Value::as_str).collect()) +} + +/// The names of a list, in order, or `None` before there is one. +fn names_of(skills: Option<&Skills>) -> Option> { + skills.map(|s| s.skills.iter().map(|s| s.name.as_str()).collect()) } async fn run(agent: String) { @@ -289,6 +311,40 @@ mod tests { assert_eq!(none, Some(Skills::default())); } + #[test] + fn a_list_is_offered_once_however_often_it_is_reported() { + let (tx, _rx) = watch::channel(None); + let one = Skills { + skills: vec![Skill { + name: "a".into(), + description: String::new(), + }], + }; + assert!(offer(&tx, one.clone())); + assert!(!offer(&tx, one.clone())); + let mut two = one.clone(); + two.skills[0].description = "now described".into(); + assert!(offer(&tx, two)); + } + + #[test] + fn the_names_decide_whether_a_list_needs_its_files_read() { + let event = json!({"type": "system", "subtype": "init", "skills": ["b", "a"]}); + let list = |names: &[&str]| Skills { + skills: names + .iter() + .map(|n| Skill { + name: (*n).into(), + description: String::new(), + }) + .collect(), + }; + assert_eq!(init_names(&event), names_of(Some(&list(&["b", "a"])))); + assert_ne!(init_names(&event), names_of(Some(&list(&["a", "b"])))); + assert_ne!(init_names(&event), names_of(None)); + assert_eq!(init_names(&json!({"type": "system"})), None); + } + #[test] fn a_long_description_is_cut_to_one_line() { let long = format!("description: {}", "word ".repeat(100));