Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/swarm-wire/src/agent_todos.rs

113 lines
3.9 KiB
Rust

//! The open todos an agent holds, which the swarm UI shows on the agent's
//! card.
//!
//! **One key per agent**, `<agent>`, in the [`BUCKET`] KV bucket, holding
//! [`Todos`]. The agent's harness writes its own key with the agent's own
//! queue credential, which may publish [`subject`] and nothing else of
//! `JetStream`; `swarm-controller` creates the bucket
//! (`swarm_queue_client::agent_todos::open_or_create`) and reads the key to
//! answer `GET /api/agents/{name}/todos`. The writer publishes to the subject
//! directly instead of opening the bucket, as the agent icon does
//! (`crate::agent_icon`); a write before the bucket exists is refused and
//! tried again.
//!
//! The value is a mirror for display: the harness's own database stays the
//! source of truth, and each write replaces the whole list.
//!
//! What the agent publishes is **untrusted**: the controller cuts the list to
//! [`MAX_TODOS`] and each text to its limit before it serves them.
use serde::{Deserialize, Serialize};
/// The KV bucket the todo lists are published into, keyed by agent name.
pub const BUCKET: &str = "agent-todos";
/// The most todos one list carries; the rest are dropped.
pub const MAX_TODOS: usize = 200;
/// The longest summary, in characters; a longer one is cut.
pub const MAX_SUMMARY_CHARS: usize = 400;
/// The longest subsystem or source text, in characters; a longer one is cut.
pub const MAX_LABEL_CHARS: usize = 80;
/// One open todo.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Todo {
/// The harness's own id for it, the number `mark_todos_done` takes.
pub id: i64,
/// What produced it: `matrix`, `forge`, `bash`, `schedule`, …
pub subsystem: String,
/// What it says, as one line.
pub summary: String,
/// Where it came from, when the producer says (a room name, a task
/// label).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source: Option<String>,
/// When it was last updated, in seconds since the Unix epoch.
pub updated_unix: i64,
}
/// The value under an agent's key: its open todos, newest first.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Todos {
pub todos: Vec<Todo>,
}
/// The subject `agent`'s list is written on: `$KV.<bucket>.<agent>`, the
/// agent name alone.
#[must_use]
pub fn subject(agent: &str) -> String {
format!("$KV.{BUCKET}.{agent}")
}
/// `text` as one line of at most `max` characters: whitespace runs become
/// single spaces, and a longer text is cut with `…`.
#[must_use]
pub fn clip(text: &str, max: usize) -> String {
let one_line = text.split_whitespace().collect::<Vec<_>>().join(" ");
if one_line.chars().count() <= max {
return one_line;
}
let mut cut: String = one_line.chars().take(max.saturating_sub(1)).collect();
cut.push('…');
cut
}
#[cfg(test)]
mod tests {
use super::*;
/// One token after the bucket, the agent name alone: what the per-agent
/// grant `$KV.agent-todos.{agent}` expands to.
#[test]
fn the_published_subject_carries_the_agent_alone() {
assert_eq!(subject("iris"), "$KV.agent-todos.iris");
}
#[test]
fn a_text_is_one_line_and_cut() {
assert_eq!(clip("a\n b\tc", 10), "a b c");
let cut = clip(&"y".repeat(50), 10);
assert_eq!(cut.chars().count(), 10);
assert!(cut.ends_with('…'));
}
#[test]
fn the_wire_form_is_plain_json_and_omits_an_absent_source() {
let wire = serde_json::to_string(&Todos {
todos: vec![Todo {
id: 7,
subsystem: "forge".to_owned(),
summary: "review".to_owned(),
source: None,
updated_unix: 100,
}],
})
.expect("encodes");
assert_eq!(
wire,
r#"{"todos":[{"id":7,"subsystem":"forge","summary":"review","updated_unix":100}]}"#
);
}
}