use super::*;
use crate::events::TodoItem;
impl SessionRegistry {
pub fn set_agent_todos(
&self,
id: &str,
agent: &str,
agent_id: &str,
todos: Vec<TodoItem>,
) -> Result<(), RpcError> {
if agent_id.is_empty() {
return Err(RpcError::invalid_argument(
"a checklist needs a non-empty agent_id to attribute it to".to_string(),
));
}
{
let mut sessions = self.lock();
let entry = sessions
.get_mut(id)
.ok_or_else(|| RpcError::session_not_found(id))?;
match entry.agents.iter_mut().find(|a| a.agent_id == agent_id) {
Some(existing) => {
existing.name = agent.to_string();
existing.todos = todos.clone();
}
None => entry.agents.push(AgentRosterState {
agent_id: agent_id.to_string(),
name: agent.to_string(),
running: false,
todos: todos.clone(),
}),
}
}
self.record(
id,
Event::TodosChanged {
session_id: id.to_string(),
agent: agent.to_string(),
agent_id: agent_id.to_string(),
todos,
},
);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::events::TodoStatus;
fn items(pairs: &[(&str, TodoStatus)]) -> Vec<TodoItem> {
pairs
.iter()
.map(|(content, status)| TodoItem {
content: (*content).to_string(),
status: *status,
})
.collect()
}
fn registry_with_session() -> (SessionRegistry, String) {
let registry = SessionRegistry::new();
let session = registry.create("t".to_string(), None, crate::binding::ProjectBinding::None);
(registry, session.id)
}
#[test]
fn set_agent_todos_writes_the_roster_row() {
let (registry, id) = registry_with_session();
registry
.set_agent_todos(
&id,
"pm",
"pm-1",
items(&[("add the flag", TodoStatus::InProgress)]),
)
.expect("write");
let roster = registry.get_agents(&id).expect("roster");
assert_eq!(roster.len(), 1);
assert_eq!(roster[0].agent_id, "pm-1");
assert_eq!(roster[0].todos.len(), 1);
assert_eq!(roster[0].todos[0].content, "add the flag");
assert_eq!(roster[0].todos[0].status, TodoStatus::InProgress);
}
#[test]
fn set_agent_todos_replaces_the_whole_list() {
let (registry, id) = registry_with_session();
registry
.set_agent_todos(
&id,
"pm",
"pm-1",
items(&[("a", TodoStatus::Pending), ("b", TodoStatus::Pending)]),
)
.expect("first write");
registry
.set_agent_todos(&id, "pm", "pm-1", items(&[("a", TodoStatus::Completed)]))
.expect("second write");
let roster = registry.get_agents(&id).expect("roster");
assert_eq!(roster[0].todos.len(), 1, "append, not replace");
assert_eq!(roster[0].todos[0].status, TodoStatus::Completed);
}
#[test]
fn set_agent_todos_empty_list_clears() {
let (registry, id) = registry_with_session();
registry
.set_agent_todos(&id, "pm", "pm-1", items(&[("a", TodoStatus::Pending)]))
.expect("write");
registry
.set_agent_todos(&id, "pm", "pm-1", Vec::new())
.expect("clear");
let roster = registry.get_agents(&id).expect("roster");
assert!(roster[0].todos.is_empty());
}
#[test]
fn set_agent_todos_keeps_two_agents_lists_apart() {
let (registry, id) = registry_with_session();
registry
.set_agent_todos(
&id,
"pm",
"pm-1",
items(&[("route the work", TodoStatus::InProgress)]),
)
.expect("pm write");
registry
.set_agent_todos(
&id,
"engineer",
"eng-1",
items(&[("write the code", TodoStatus::Pending)]),
)
.expect("engineer write");
let roster = registry.get_agents(&id).expect("roster");
let pm = roster
.iter()
.find(|a| a.agent_id == "pm-1")
.expect("pm row");
let eng = roster
.iter()
.find(|a| a.agent_id == "eng-1")
.expect("engineer row");
assert_eq!(pm.todos.len(), 1);
assert_eq!(pm.todos[0].content, "route the work");
assert_eq!(eng.todos[0].content, "write the code");
}
#[test]
fn set_agent_todos_keeps_two_sessions_lists_apart() {
let registry = SessionRegistry::new();
let a = registry.create("a".to_string(), None, crate::binding::ProjectBinding::None);
let b = registry.create("b".to_string(), None, crate::binding::ProjectBinding::None);
registry
.set_agent_todos(&a.id, "pm", "pm-1", items(&[("in a", TodoStatus::Pending)]))
.expect("write a");
registry
.set_agent_todos(&b.id, "pm", "pm-1", items(&[("in b", TodoStatus::Pending)]))
.expect("write b");
assert_eq!(
registry.get_agents(&a.id).expect("roster a")[0].todos[0].content,
"in a"
);
assert_eq!(
registry.get_agents(&b.id).expect("roster b")[0].todos[0].content,
"in b"
);
}
#[test]
fn set_agent_todos_streams_a_change_event() {
let (registry, id) = registry_with_session();
registry
.set_agent_todos(
&id,
"pm",
"pm-1",
items(&[("add a test", TodoStatus::InProgress)]),
)
.expect("write");
let replay = registry.replay(&id).expect("replay");
let streamed = replay
.iter()
.find_map(|e| match &e.event {
Event::TodosChanged {
agent,
agent_id,
todos,
..
} => Some((agent.clone(), agent_id.clone(), todos.clone())),
_ => None,
})
.expect("a todos_changed event must be recorded");
assert_eq!(streamed.0, "pm");
assert_eq!(streamed.1, "pm-1");
assert_eq!(streamed.2[0].content, "add a test");
}
#[test]
fn set_agent_todos_leaves_running_state_alone() {
let (registry, id) = registry_with_session();
registry
.record_tool_started(&id, "pm", "pm-1", "bash", "c1", "ls")
.expect("tool started");
registry
.set_agent_todos(&id, "pm", "pm-1", items(&[("a", TodoStatus::Pending)]))
.expect("write");
let roster = registry.get_agents(&id).expect("roster");
assert_eq!(
roster[0].state, "running",
"a checklist write must not clear an in-flight tool call"
);
}
#[test]
fn set_agent_todos_unknown_session_errors() {
let registry = SessionRegistry::new();
let err = registry
.set_agent_todos("nope", "pm", "pm-1", Vec::new())
.expect_err("unknown session");
assert_eq!(err.code, -32007);
}
#[test]
fn set_agent_todos_empty_agent_id_errors() {
let (registry, id) = registry_with_session();
let err = registry
.set_agent_todos(&id, "pm", "", items(&[("a", TodoStatus::Pending)]))
.expect_err("empty agent_id");
assert!(
err.message.contains("agent_id"),
"not actionable: {}",
err.message
);
assert!(registry.get_agents(&id).expect("roster").is_empty());
}
}