Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(todos): replace thread-scoped stores with session-scoped stores

Steven Enamakel1ce239edb431
diff --git a/crates/openhuman-core/src/agent/todos/ops.rs b/crates/openhuman-core/src/agent/todos/ops.rs
index b4f5d09729..22859aa3bd 100644
--- a/crates/openhuman-core/src/agent/todos/ops.rs
+++ b/crates/openhuman-core/src/agent/todos/ops.rs
@@ -1,20 +1,15 @@
//! OpenHuman host adapter over [`tinyagents_graph::todos`].
//!
-//! A todo list is scoped to one conversation thread ([`TodoScope::Thread`])
-//! or, when a tool runs with no thread at all, to a process-global scratch
-//! list ([`TodoScope::Scratch`]). The store, normalisation and rendering are
-//! TinyAgents'; this file only picks the store for a scope and reshapes the
-//! snapshot for OpenHuman callers. The whole-list `replace` is the only
-//! write the `todo` tool needs; `clear` is for tests and cleanup.
-
-use std::path::PathBuf;
-use std::sync::Arc;
+//! A todo list is scoped to one agent session ([`TodoScope::Session`]) or,
+//! when a tool runs with no session at all, to a scratch list
+//! ([`TodoScope::Scratch`]). Both live in the one in-process store; the
+//! normalisation and rendering are TinyAgents'. The whole-list `replace` is
+//! the only write the `todo` tool needs; `clear` is for tests and cleanup.
use serde::{Deserialize, Serialize};
use tinyagents_graph::todos::store as todos;
-use tinyagents_harness::store::Store;
-use crate::agent::tinyagents::todos::{scratch_todos_store, todos_store, SCRATCH_THREAD_ID};
+use crate::agent::tinyagents::todos::{session_todos_store, SCRATCH_SESSION_ID};
use crate::agent::todos::types::normalize_cards_for_wire;
pub use crate::agent::todos::types::{TaskBoardCard, TaskCardStatus};
@@ -23,42 +18,33 @@ pub use tinyagents_graph::todos::{parse_status, render_markdown};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TodosSnapshot {
- pub thread_id: Option<String>,
+ pub session_id: Option<String>,
pub cards: Vec<TaskBoardCard>,
pub markdown: String,
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TodoScope {
- Thread {
- workspace_dir: PathBuf,
- thread_id: String,
- },
+ Session { id: String },
Scratch,
}
impl TodoScope {
- pub fn thread_id(&self) -> Option<&str> {
+ pub fn session_id(&self) -> Option<&str> {
match self {
- Self::Thread { thread_id, .. } => Some(thread_id),
+ Self::Session { id } => Some(id),
Self::Scratch => None,
}
}
-}
-fn target(scope: &TodoScope) -> (Arc<dyn Store>, &str) {
- match scope {
- TodoScope::Thread {
- workspace_dir,
- thread_id,
- } => (todos_store(workspace_dir), thread_id),
- TodoScope::Scratch => (scratch_todos_store(), SCRATCH_THREAD_ID),
+ fn key(&self) -> &str {
+ self.session_id().unwrap_or(SCRATCH_SESSION_ID)
}
}
fn snapshot(scope: &TodoScope, value: tinyagents_graph::todos::TodosSnapshot) -> TodosSnapshot {
TodosSnapshot {
- thread_id: scope.thread_id().map(str::to_owned),
+ session_id: scope.session_id().map(str::to_owned),
cards: value.cards,
markdown: value.markdown,
}
@@ -74,18 +60,18 @@ fn finish(
}
pub async fn replace(scope: &TodoScope, cards: Vec<TaskBoardCard>) -> Result<TodosSnapshot, String> {
- let (store, thread_id) = target(scope);
- finish(scope, todos::replace(&store, thread_id, cards).await)
+ let store = session_todos_store();
+ finish(scope, todos::replace(&store, scope.key(), cards).await)
}
pub async fn clear(scope: &TodoScope) -> Result<TodosSnapshot, String> {
- let (store, thread_id) = target(scope);
- finish(scope, todos::clear(&store, thread_id).await)
+ let store = session_todos_store();
+ finish(scope, todos::clear(&store, scope.key()).await)
}
pub async fn list(scope: &TodoScope) -> Result<TodosSnapshot, String> {
- let (store, thread_id) = target(scope);
- todos::list(&store, thread_id)
+ let store = session_todos_store();
+ todos::list(&store, scope.key())
.await
.map(|value| snapshot(scope, value))
.map_err(|error| error.to_string())
diff --git a/crates/openhuman-core/src/agent/tools/todo.rs b/crates/openhuman-core/src/agent/tools/todo.rs
index 2b3f7f6f41..aef5846d35 100644
--- a/crates/openhuman-core/src/agent/tools/todo.rs
+++ b/crates/openhuman-core/src/agent/tools/todo.rs
@@ -3,10 +3,10 @@
//! One call writes the whole list: `{"todos": [{"content", "status"}]}`.
//! There is no per-card CRUD, no approval gate, no evidence, no plan; the
//! list is a progress checklist the model rewrites as it works. It is scoped
-//! to the conversation thread the turn runs in and persists across turns of
-//! that thread via [`crate::agent::todos::ops`]; without a thread (a bare
-//! `execute` in a test) it falls back to a process-global scratch list.
-//! Calling with no `todos` returns the current list.
+//! to the agent session the turn runs in (in memory, for the life of the
+//! process) via [`crate::agent::todos::ops`]; without a session (a bare
+//! `execute` in a test) it falls back to a scratch list. Calling with no
+//! `todos` returns the current list.
use crate::agent::harness::fork_context::ParentExecutionContext;
use crate::agent::todos::ops::{self, TodoScope};
@@ -135,7 +135,7 @@ impl TodoTool {
tool_context: Option<&dyn ToolRunContext>,
) -> anyhow::Result<ToolResult> {
let scope = current_scope(parent.as_ref(), tool_context);
- tracing::debug!(thread_id = ?scope.thread_id(), "[tool][todo] dispatch");
+ tracing::debug!(session_id = ?scope.session_id(), "[tool][todo] dispatch");
let result = match args.get("todos") {
None | Some(serde_json::Value::Null) => ops::list(&scope).await,
@@ -172,7 +172,7 @@ impl TodoTool {
})
.collect();
let payload = json!({
- "threadId": snap.thread_id,
+ "sessionId": snap.session_id,
"todos": todos,
"markdown": snap.markdown,
});
@@ -197,23 +197,26 @@ fn wire_status(status: TaskCardStatus) -> &'static str {
}
}
-/// Every agent, the orchestrator included, binds to the conversation thread it
-/// runs in. The orchestrator used to be routed to one app-wide
-/// `orchestrator-tasks` board instead; nothing rendered it, so the list the
-/// model kept was invisible to the thread the user was looking at.
+/// The list belongs to the agent session the tool runs in: the orchestrator's
+/// session for a chat thread, a sub-agent's own session for its run. The
+/// orchestrator used to be routed to one app-wide `orchestrator-tasks` board
+/// instead; nothing rendered it, so the list the model kept was invisible to
+/// the thread the user was looking at. The parent context names the session;
+/// a tool that is only handed a thread id (older callers, tests) keys on that.
fn current_scope(
parent: Option<&ParentExecutionContext>,
tool_context: Option<&dyn ToolRunContext>,
) -> TodoScope {
- let Some(parent) = parent else {
- return TodoScope::Scratch;
- };
- let Some(thread_id) = tool_context.and_then(ToolRunContext::thread_id) else {
- return TodoScope::Scratch;
- };
- TodoScope::Thread {
- workspace_dir: parent.workspace_dir.clone(),
- thread_id: thread_id.to_owned(),
+ if let Some(parent) = parent {
+ return TodoScope::Session {
+ id: parent.session_id.clone(),
+ };
+ }
+ match tool_context.and_then(ToolRunContext::thread_id) {
+ Some(thread_id) => TodoScope::Session {
+ id: thread_id.to_owned(),
+ },
+ None => TodoScope::Scratch,
}
}
diff --git a/crates/openhuman-core/src/agent/tools/todo_tests.rs b/crates/openhuman-core/src/agent/tools/todo_tests.rs
index 484ef2700c..7c926b9ae2 100644
--- a/crates/openhuman-core/src/agent/tools/todo_tests.rs
+++ b/crates/openhuman-core/src/agent/tools/todo_tests.rs
@@ -113,11 +113,11 @@ fn schema_is_the_claude_shape() {
);
}
-/// The orchestrator's list is the conversation thread's list. It used to be
-/// routed to one app-wide `orchestrator-tasks` board that nothing rendered,
-/// so the items the model wrote never showed up in the thread the user was in.
+/// The orchestrator's list is its session's list. It used to be routed to one
+/// app-wide `orchestrator-tasks` board that nothing rendered, so the items the
+/// model wrote never showed up in the thread the user was in.
#[test]
-fn every_agent_binds_to_the_live_thread() {
+fn every_agent_binds_to_its_own_session() {
struct ThreadContext(&'static str);
impl ToolRunContext for ThreadContext {
fn thread_id(&self) -> Option<&str> {
@@ -143,7 +143,7 @@ fn every_agent_binds_to_the_live_thread() {
agent_config: crate::config::AgentConfig::default(),
workflows: Arc::new(Vec::new()),
memory_context: Arc::new(None),
- session_id: "parent-session".into(),
+ session_id: "orchestrator_thread-live".into(),
channel: "test".into(),
connected_integrations: Vec::new(),
tool_call_format: crate::agent::prompts::ToolCallFormat::Native,
@@ -152,14 +152,33 @@ fn every_agent_binds_to_the_live_thread() {
on_progress: None,
run_queue: None,
};
- let context = ThreadContext("thread-live");
assert_eq!(
- current_scope(Some(&parent), Some(&context)).thread_id(),
- Some("thread-live")
+ current_scope(Some(&parent), Some(&ThreadContext("thread-live"))).session_id(),
+ Some("orchestrator_thread-live"),
+ "the parent's session wins over the thread id"
);
- assert!(
- matches!(current_scope(Some(&parent), None), TodoScope::Scratch),
- "without a thread there is no list to persist to"
+ assert_eq!(
+ current_scope(None, Some(&ThreadContext("thread-live"))).session_id(),
+ Some("thread-live"),
+ "a thread-only caller keys on the thread"
);
+ assert_eq!(current_scope(None, None), TodoScope::Scratch);
+}
+
+#[tokio::test]
+async fn sessions_do_not_see_each_other_and_a_list_survives_across_turns() {
+ let a = TodoScope::Session { id: "sess-a".into() };
+ let b = TodoScope::Session { id: "sess-b".into() };
+ crate::agent::todos::ops::clear(&a).await.unwrap();
+ crate::agent::todos::ops::clear(&b).await.unwrap();
+
+ let mut card = TaskBoardCard::new("only in a");
+ card.status = TaskCardStatus::InProgress;
+ crate::agent::todos::ops::replace(&a, vec![card]).await.unwrap();
+
+ let a_again = crate::agent::todos::ops::list(&a).await.unwrap();
+ assert_eq!(a_again.cards.len(), 1, "a later turn of the same session reads it back");
+ assert_eq!(a_again.session_id.as_deref(), Some("sess-a"));
+ assert!(crate::agent::todos::ops::list(&b).await.unwrap().cards.is_empty());
}