hive-agent: read a skill's file only when the skill names change
This commit is contained in:
parent
8cc99020a8
commit
d54b59df7e
1 changed files with 62 additions and 6 deletions
|
|
@ -14,8 +14,8 @@
|
||||||
//! with an empty description.
|
//! with an empty description.
|
||||||
//!
|
//!
|
||||||
//! **Once per change.** The list is published when the first turn reports it
|
//! **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
|
//! and again whenever a later turn reports different names, so an unchanged
|
||||||
//! list costs nothing. Best-effort: [`crate::swarm_kv_put`] retries a failed
|
//! 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
|
//! write, and the harness never waits on it. The agent's own queue credential
|
||||||
//! is granted exactly this agent's key.
|
//! is granted exactly this agent's key.
|
||||||
|
|
||||||
|
|
@ -46,23 +46,45 @@ pub fn spawn() {
|
||||||
tokio::spawn(run(agent));
|
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
|
/// 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) {
|
pub fn observe_init(event: &Value) {
|
||||||
if !is_init(event) {
|
if !is_init(event) {
|
||||||
return;
|
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 {
|
let Some(skills) = skills_from_init(event, &home_skills_dir()) else {
|
||||||
return;
|
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<Option<Skills>>, skills: Skills) -> bool {
|
||||||
|
tx.send_if_modified(|current| {
|
||||||
if current.as_ref() == Some(&skills) {
|
if current.as_ref() == Some(&skills) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
*current = Some(skills);
|
*current = Some(skills);
|
||||||
true
|
true
|
||||||
});
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The skill names of an init event, in its order, or `None` without a `skills`
|
||||||
|
/// array.
|
||||||
|
fn init_names(event: &Value) -> Option<Vec<&str>> {
|
||||||
|
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<Vec<&str>> {
|
||||||
|
skills.map(|s| s.skills.iter().map(|s| s.name.as_str()).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run(agent: String) {
|
async fn run(agent: String) {
|
||||||
|
|
@ -289,6 +311,40 @@ mod tests {
|
||||||
assert_eq!(none, Some(Skills::default()));
|
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]
|
#[test]
|
||||||
fn a_long_description_is_cut_to_one_line() {
|
fn a_long_description_is_cut_to_one_line() {
|
||||||
let long = format!("description: {}", "word ".repeat(100));
|
let long = format!("description: {}", "word ".repeat(100));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue