swarm: an agent's open todos, mirrored to the queue and served by the controller
This commit is contained in:
parent
c9604a6708
commit
b239500f88
2 changed files with 478 additions and 6 deletions
|
|
@ -38,6 +38,7 @@ mod subagent_role_watch;
|
|||
mod swarm_agent_commands;
|
||||
mod swarm_agent_icon;
|
||||
mod swarm_agent_state;
|
||||
mod swarm_agent_todos;
|
||||
mod swarm_agents_md;
|
||||
mod swarm_inference;
|
||||
mod swarm_input;
|
||||
|
|
@ -455,6 +456,8 @@ fn spawn_todo_socket(
|
|||
// swarm keeps for this agent.
|
||||
// Open-todo count for the OTEL gauge.
|
||||
tokio::spawn(todos_gauge::run(store.clone()));
|
||||
// The open todos, mirrored to the swarm for the swarm UI.
|
||||
swarm_agent_todos::spawn(store.clone());
|
||||
tokio::spawn(schedule_timer::run(store.clone(), todo_wake.clone()));
|
||||
// And so do the todos the swarm notification router queues for
|
||||
// this agent.
|
||||
|
|
|
|||
140
hive-agent/src/swarm_agent_todos.rs
Normal file
140
hive-agent/src/swarm_agent_todos.rs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
//! Publishing this agent's open todos into the swarm's `agent-todos` bucket,
|
||||
//! which the swarm UI shows on the agent's card (`swarm_wire::agent_todos`).
|
||||
//!
|
||||
//! **A mirror, read on a timer.** Todos arrive from a dozen producers, some of
|
||||
//! them out of process, so there is no one place to hook a change. [`run`]
|
||||
//! reads the open todos every [`TICK`] and publishes the list when it differs
|
||||
//! from the one last published, and once at start so an agent with no todos
|
||||
//! clears whatever an earlier run left. An unchanged list costs one sqlite
|
||||
//! read. Best-effort: [`crate::swarm_kv_put`] retries a failed write, and
|
||||
//! nothing here ever blocks the todo store or a turn. The agent's own queue
|
||||
//! credential is granted exactly this agent's key.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use swarm_wire::agent_todos::{
|
||||
self, MAX_LABEL_CHARS, MAX_SUMMARY_CHARS, MAX_TODOS, Todo as WireTodo, Todos as WireTodos,
|
||||
};
|
||||
|
||||
use crate::swarm_kv_put::{Failure, Put, put_retrying};
|
||||
use crate::todos::{Todo, Todos};
|
||||
|
||||
/// How often the open todos are read.
|
||||
const TICK: Duration = Duration::from_secs(5);
|
||||
|
||||
/// Start the publish task, if this agent has a label.
|
||||
pub fn spawn(store: Arc<Todos>) {
|
||||
let agent = crate::identity::label();
|
||||
if agent.is_empty() {
|
||||
tracing::warn!("this agent has no label; not publishing its todos upward");
|
||||
return;
|
||||
}
|
||||
tokio::spawn(run(agent, store));
|
||||
}
|
||||
|
||||
/// The list as the swarm gets it: newest first as the store lists them, cut
|
||||
/// to [`MAX_TODOS`], each text one line of bounded length.
|
||||
fn wire_list(todos: &[Todo]) -> WireTodos {
|
||||
WireTodos {
|
||||
todos: todos
|
||||
.iter()
|
||||
.take(MAX_TODOS)
|
||||
.map(|t| WireTodo {
|
||||
id: t.id,
|
||||
subsystem: agent_todos::clip(&t.subsystem, MAX_LABEL_CHARS),
|
||||
summary: agent_todos::clip(&t.summary, MAX_SUMMARY_CHARS),
|
||||
source: t
|
||||
.source
|
||||
.as_deref()
|
||||
.map(|s| agent_todos::clip(s, MAX_LABEL_CHARS))
|
||||
.filter(|s| !s.is_empty()),
|
||||
updated_unix: t.updated_at.timestamp(),
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(agent: String, store: Arc<Todos>) {
|
||||
let client = crate::swarm_queue::client().await;
|
||||
let subject = agent_todos::subject(&agent);
|
||||
let mut published: Option<WireTodos> = None;
|
||||
loop {
|
||||
match store.list(None) {
|
||||
Ok(open) => {
|
||||
let list = wire_list(&open);
|
||||
if published.as_ref() != Some(&list) {
|
||||
publish(&client, &subject, &list).await;
|
||||
// Also after a list that can never fit: the same list is
|
||||
// not tried again until it changes.
|
||||
published = Some(list);
|
||||
}
|
||||
}
|
||||
Err(e) => tracing::warn!(error = %e, "reading the open todos to publish failed"),
|
||||
}
|
||||
tokio::time::sleep(TICK).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Write `list`; a failure is logged, not returned.
|
||||
async fn publish(client: &async_nats::Client, subject: &str, list: &WireTodos) {
|
||||
let payload = match serde_json::to_vec(list) {
|
||||
Ok(payload) => payload,
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %e, "serialising the agent's todos failed; not publishing");
|
||||
return;
|
||||
}
|
||||
};
|
||||
match put_retrying(client, subject, Put::Set(&payload), "the agent's todos").await {
|
||||
Ok(()) => {
|
||||
tracing::debug!(
|
||||
subject,
|
||||
count = list.todos.len(),
|
||||
"published this agent's todos"
|
||||
);
|
||||
}
|
||||
Err(Failure::TooLarge(e)) => {
|
||||
tracing::warn!(error = %e, "the agent's todos are over the queue's payload limit; not publishing them");
|
||||
}
|
||||
// Only a `TooLarge` ends the retries.
|
||||
Err(Failure::Retry(e)) => {
|
||||
tracing::error!(error = e, "gave up publishing the agent's todos");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use hive_sh4re::wire_time;
|
||||
|
||||
fn todo(id: i64, summary: &str, source: Option<&str>) -> Todo {
|
||||
Todo {
|
||||
id,
|
||||
subsystem: "forge".to_owned(),
|
||||
subsystem_key: Some(format!("k{id}")),
|
||||
summary: summary.to_owned(),
|
||||
source: source.map(str::to_owned),
|
||||
updated_at: wire_time::from_secs(1_000 + id),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_todo_is_published_as_one_clipped_line_without_its_dedup_key() {
|
||||
let list = wire_list(&[todo(3, "two\nlines", Some(" "))]);
|
||||
assert_eq!(list.todos.len(), 1);
|
||||
assert_eq!(list.todos[0].summary, "two lines");
|
||||
assert_eq!(list.todos[0].source, None, "a blank source is left out");
|
||||
assert_eq!(list.todos[0].updated_unix, 1_003);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_list_is_cut_and_the_texts_are_bounded() {
|
||||
let many: Vec<Todo> = (0..i64::try_from(MAX_TODOS).expect("fits") + 30)
|
||||
.map(|i| todo(i, &"s ".repeat(500), None))
|
||||
.collect();
|
||||
let list = wire_list(&many);
|
||||
assert_eq!(list.todos.len(), MAX_TODOS);
|
||||
assert!(list.todos[0].summary.chars().count() <= MAX_SUMMARY_CHARS);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue