use std::sync::Arc;
use crate::events::TodoItem;
use crate::session::SessionRegistry;
use crate::tools::{TodoError, TodoStore};
pub struct SessionTodoStore {
registry: Arc<SessionRegistry>,
session_id: String,
agent: String,
agent_id: String,
}
impl SessionTodoStore {
pub fn new(
registry: Arc<SessionRegistry>,
session_id: impl Into<String>,
agent: impl Into<String>,
agent_id: impl Into<String>,
) -> Self {
Self {
registry,
session_id: session_id.into(),
agent: agent.into(),
agent_id: agent_id.into(),
}
}
}
impl TodoStore for SessionTodoStore {
fn replace(&self, todos: Vec<TodoItem>) -> Result<(), TodoError> {
self.registry
.set_agent_todos(&self.session_id, &self.agent, &self.agent_id, todos)
.map_err(|e| TodoError::Unavailable(e.message))
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
use crate::binding::ProjectBinding;
use crate::events::TodoStatus;
use crate::tools::{TodoWriteTool, ToolExecutor};
fn session() -> (Arc<SessionRegistry>, String) {
let registry = Arc::new(SessionRegistry::new());
let created = registry.create("t".to_string(), None, ProjectBinding::None);
(registry, created.id)
}
#[tokio::test]
async fn tool_write_is_visible_through_get_agents() {
let (registry, id) = session();
let tool = TodoWriteTool::new(Arc::new(SessionTodoStore::new(
Arc::clone(®istry),
&id,
"pm",
"pm-1",
)));
let result = tool
.execute(json!({"todos": [
{"content": "add the --json flag", "status": "completed"},
{"content": "add a test", "status": "in_progress"},
{"content": "add a changelog line", "status": "pending"}
]}))
.await;
assert!(!result.is_error(), "{result:?}");
let roster = registry.get_agents(&id).expect("roster");
assert_eq!(roster.len(), 1);
assert_eq!(roster[0].agent_id, "pm-1");
let todos = &roster[0].todos;
assert_eq!(todos.len(), 3);
assert_eq!(todos[1].content, "add a test");
assert_eq!(todos[1].status, TodoStatus::InProgress);
}
#[tokio::test]
async fn a_refused_call_leaves_the_stored_list_unchanged() {
let (registry, id) = session();
let tool = TodoWriteTool::new(Arc::new(SessionTodoStore::new(
Arc::clone(®istry),
&id,
"pm",
"pm-1",
)));
tool.execute(json!({"todos": [{"content": "plan", "status": "in_progress"}]}))
.await;
let refused = tool
.execute(json!({"todos": [
{"content": "a", "status": "in_progress"},
{"content": "b", "status": "in_progress"}
]}))
.await;
assert!(refused.is_error());
let roster = registry.get_agents(&id).expect("roster");
assert_eq!(roster[0].todos.len(), 1);
assert_eq!(roster[0].todos[0].content, "plan");
}
#[tokio::test]
async fn a_vanished_session_is_reported_not_swallowed() {
let registry = Arc::new(SessionRegistry::new());
let tool = TodoWriteTool::new(Arc::new(SessionTodoStore::new(
registry, "gone", "pm", "pm-1",
)));
let result = tool
.execute(json!({"todos": [{"content": "a", "status": "pending"}]}))
.await;
assert!(result.is_error(), "{result:?}");
}
#[tokio::test]
async fn a_sub_agent_cannot_rewrite_the_pms_list() {
let (registry, id) = session();
let pm = TodoWriteTool::new(Arc::new(SessionTodoStore::new(
Arc::clone(®istry),
&id,
"pm",
"pm-1",
)));
let engineer = TodoWriteTool::new(Arc::new(SessionTodoStore::new(
Arc::clone(®istry),
&id,
"engineer",
"eng-1",
)));
pm.execute(json!({"todos": [{"content": "route the work", "status": "in_progress"}]}))
.await;
engineer
.execute(json!({"todos": [{"content": "write the code", "status": "in_progress"}]}))
.await;
let roster = registry.get_agents(&id).expect("roster");
let pm_row = roster
.iter()
.find(|a| a.agent_id == "pm-1")
.expect("pm row");
assert_eq!(pm_row.todos.len(), 1);
assert_eq!(pm_row.todos[0].content, "route the work");
let eng_row = roster
.iter()
.find(|a| a.agent_id == "eng-1")
.expect("engineer row");
assert_eq!(eng_row.todos[0].content, "write the code");
}
}