The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(agent-harness): add end-to-end tests for todo list and thread goal tools
Steven Enamakelf44742217730
diff --git a/tests/agent_harness_e2e.rs b/tests/agent_harness_e2e.rsindex e4df817362..7cfde67809 100644--- a/tests/agent_harness_e2e.rs+++ b/tests/agent_harness_e2e.rs@@ -4365,3 +4365,476 @@ mod tool_policy_boundary_placement {);}}++// ─── Harness work state: the session todo list and the thread goal ──────────+//+// Two harness-level primitives the orchestrator drives with its own tools and+// the chat pane renders read-only above the composer:+//+// - `todo` — the session checklist. One call writes the whole list+// (`{"todos": [{content, status}]}`), Claude Code / Codex style. Scoped to+// the thread's orchestrator session, alive for the process.+// - `goal_set` / `goal_get` / `goal_complete` — the thread's durable+// objective, persisted in the crate `graph.goals` store.+//+// Both answer with a JSON payload. The frontend reads the newest one off the+// tool timeline (`app/src/features/conversations/utils/harnessState.ts`), so+// these tests pin the two contracts the UI depends on: the payload shape on+// the live `tool_result` socket event, and the same payload persisted in the+// turn-state snapshot a reloaded thread rehydrates from.++/// Every `tool_result` frame the turn emitted, in order, plus the terminal+/// event. Collected on one connection so no frame is lost between waits.+async fn collect_turn_tool_results(+ rx: &mut tokio::sync::mpsc::UnboundedReceiver<Value>,+ timeout: Duration,+) -> (Value, Vec<Value>) {+ let deadline = tokio::time::Instant::now() + timeout;+ let mut results = Vec::new();+ loop {+ let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());+ match tokio::time::timeout(remaining, rx.recv()).await {+ Ok(Some(v)) => match v.get("event").and_then(Value::as_str) {+ Some("tool_result") => results.push(v),+ Some("chat_done") | Some("chat_error") => return (v, results),+ _ => {}+ },+ Ok(None) => panic!("SSE channel closed waiting for terminal event"),+ Err(_) => panic!(+ "timed out waiting for terminal web-chat event; tool results so far: {results:?}"+ ),+ }+ }+}++/// The JSON payload a named tool answered with on the live socket.+fn tool_result_payload(results: &[Value], tool_name: &str) -> Value {+ let frame = results+ .iter()+ .find(|frame| frame.get("tool_name").and_then(Value::as_str) == Some(tool_name))+ .unwrap_or_else(|| panic!("no tool_result frame for `{tool_name}` in {results:?}"));+ assert_eq!(+ frame.get("success"),+ Some(&json!(true)),+ "`{tool_name}` should succeed: {frame}"+ );+ let output = frame+ .get("output")+ .and_then(Value::as_str)+ .unwrap_or_else(|| panic!("`{tool_name}` result carries no output: {frame}"));+ serde_json::from_str(output)+ .unwrap_or_else(|e| panic!("`{tool_name}` output is not JSON ({e}): {output}"))+}++/// The statuses of a `todo` payload's items, in list order.+fn todo_statuses(payload: &Value) -> Vec<String> {+ payload["todos"]+ .as_array()+ .unwrap_or_else(|| panic!("todo payload has no `todos` array: {payload}"))+ .iter()+ .map(|item| item["status"].as_str().unwrap_or("").to_string())+ .collect()+}++/// A `todo` write: `steps` in order, the first `completed` of them done, the+/// next one in progress, the rest pending — the shape an agent writes as it+/// works down a list one item at a time.+fn todo_write(steps: &[&str], completed: usize) -> Value {+ let todos: Vec<Value> = steps+ .iter()+ .enumerate()+ .map(|(i, content)| {+ let status = if i < completed {+ "completed"+ } else if i == completed {+ "in_progress"+ } else {+ "pending"+ };+ json!({ "content": content, "status": status })+ })+ .collect();+ tool_call_completion("todo", json!({ "todos": todos }))+}++/// The persisted turn-state snapshots for a thread, newest first, through+/// the RPC the reloaded pane uses.+async fn turn_state_history(rpc_base: &str, id: i64, thread_id: &str) -> Vec<Value> {+ let resp = post_json_rpc(+ rpc_base,+ id,+ "openhuman.threads_turn_state_history",+ json!({ "thread_id": thread_id }),+ )+ .await;+ assert_no_jsonrpc_error(&resp, "threads_turn_state_history")+ .get("data")+ .and_then(|d| d.get("turnStates"))+ .and_then(Value::as_array)+ .cloned()+ .unwrap_or_else(|| panic!("turn_state_history has no turnStates: {resp}"))+}++/// Persisted tool rows named `tool_name`, oldest first, across every turn of+/// the thread — what a cold-booted pane rehydrates its timeline from.+fn persisted_tool_rows(turns: &[Value], tool_name: &str) -> Vec<Value> {+ turns+ .iter()+ .rev()+ .flat_map(|turn| {+ turn["toolTimeline"]+ .as_array()+ .cloned()+ .unwrap_or_default()+ .into_iter()+ .filter(|row| row["name"].as_str() == Some(tool_name))+ })+ .collect()+}++const FIVE_STEPS: [&str; 5] = [+ "Read the request",+ "Draft the outline",+ "Write the sections",+ "Review for accuracy",+ "Send the summary",+];++/// A five-item todo list worked one item per turn: the agent writes all five+/// up front, then rewrites the list each turn moving one more item to+/// `completed`. Every write reaches the pane on the live socket and survives+/// in the persisted turn state; the last write is all five completed.+#[test]+fn todo_list_ticks_off_five_items_across_turns() {+ run_on_agent_stack(+ "todo_list_ticks_off_five_items_across_turns",+ todo_list_ticks_off_five_items_across_turns_inner,+ );+}++async fn todo_list_ticks_off_five_items_across_turns_inner() {+ let _lock = env_lock();+ // Turn 1 writes the plan (item 1 in progress). Turns 2-6 each complete+ // one more item; the final write has every item completed.+ let mut script = vec![+ todo_write(&FIVE_STEPS, 0),+ text_completion("Plan written; starting on the first step."),+ ];+ for completed in 1..=FIVE_STEPS.len() {+ script.push(todo_write(&FIVE_STEPS, completed));+ script.push(text_completion(&format!("Step {completed} done.")));+ }+ reset_script(script);+ let stack = boot_stack().await;++ let mut events = spawn_sse_collector(format!(+ "{}/events?client_id=harness-todo-five",+ stack.rpc_base+ ));++ // Turn 1: the whole plan lands, first item in progress.+ send_web_chat(+ &stack.rpc_base,+ 600,+ "harness-todo-five",+ "thread-todo-five",+ "Summarise the report in five steps and work through them.",+ )+ .await;+ let (terminal, results) = collect_turn_tool_results(&mut events, Duration::from_secs(60)).await;+ assert_eq!(+ terminal.get("event").and_then(Value::as_str),+ Some("chat_done"),+ "turn 1: {terminal}"+ );+ let first = tool_result_payload(&results, "todo");+ assert_eq!(+ todo_statuses(&first),+ vec!["in_progress", "pending", "pending", "pending", "pending"],+ "turn 1 payload: {first}"+ );+ let contents: Vec<&str> = first["todos"]+ .as_array()+ .unwrap()+ .iter()+ .map(|item| item["content"].as_str().unwrap())+ .collect();+ assert_eq!(contents, FIVE_STEPS.to_vec());+ assert!(+ first["markdown"]+ .as_str()+ .unwrap()+ .starts_with("- [~] Read the request\n- [ ] Draft the outline"),+ "markdown mirrors the list: {first}"+ );+ assert!(+ first["sessionId"].as_str().is_some_and(|s| !s.is_empty()),+ "the list is bound to the orchestrator's session: {first}"+ );++ // Turns 2-6: one more item completed each turn.+ for completed in 1..=FIVE_STEPS.len() {+ send_web_chat(+ &stack.rpc_base,+ 600 + completed as i64,+ "harness-todo-five",+ "thread-todo-five",+ "continue",+ )+ .await;+ let (terminal, results) =+ collect_turn_tool_results(&mut events, Duration::from_secs(60)).await;+ assert_eq!(+ terminal.get("event").and_then(Value::as_str),+ Some("chat_done"),+ "turn {}: {terminal}",+ completed + 1+ );+ let payload = tool_result_payload(&results, "todo");+ let expected: Vec<String> = FIVE_STEPS+ .iter()+ .enumerate()+ .map(|(i, _)| {+ if i < completed {+ "completed"+ } else if i == completed {+ "in_progress"+ } else {+ "pending"+ }+ .to_string()+ })+ .collect();+ assert_eq!(todo_statuses(&payload), expected, "after {completed} done: {payload}");+ }++ // The model was told what it wrote back: the tool message in the next+ // upstream request is the same payload the socket carried.+ let requests = with_captured(|c| c.clone());+ let echoed = tool_result_text(&requests, "todo").expect("todo result echoed to the model");+ assert!(+ echoed.contains("\"todos\""),+ "the model sees the JSON list, not a bare acknowledgement: {echoed}"+ );++ // What a reloaded pane sees: every write persisted in the turn state,+ // the newest with all five completed.+ let turns = turn_state_history(&stack.rpc_base, 650, "thread-todo-five").await;+ assert_eq!(turns.len(), FIVE_STEPS.len() + 1, "one snapshot per turn");+ let rows = persisted_tool_rows(&turns, "todo");+ assert_eq!(rows.len(), FIVE_STEPS.len() + 1, "one todo row per turn: {rows:?}");+ for row in &rows {+ assert_eq!(row["status"].as_str(), Some("success"), "{row}");+ assert!(row["output"].as_str().is_some(), "persisted row keeps the output: {row}");+ }+ let last: Value = serde_json::from_str(rows.last().unwrap()["output"].as_str().unwrap())+ .expect("persisted output is the JSON payload");+ assert_eq!(+ todo_statuses(&last),+ vec!["completed"; FIVE_STEPS.len()],+ "final persisted list: {last}"+ );++ stack.shutdown();+}++/// Two `in_progress` items break the single-focus invariant: the store+/// refuses the write, the agent gets a tool error it can correct, and the+/// list it wrote before is untouched.+#[test]+fn todo_list_rejects_two_items_in_progress() {+ run_on_agent_stack(+ "todo_list_rejects_two_items_in_progress",+ todo_list_rejects_two_items_in_progress_inner,+ );+}++async fn todo_list_rejects_two_items_in_progress_inner() {+ let _lock = env_lock();+ reset_script(vec![+ todo_write(&["Only step", "Next step"], 0),+ tool_call_completion(+ "todo",+ json!({ "todos": [+ { "content": "Only step", "status": "in_progress" },+ { "content": "Next step", "status": "in_progress" }+ ] }),+ ),+ // A read (no `todos`) shows the earlier write survived the rejection.+ tool_call_completion("todo", json!({})),+ text_completion("Kept one thing in progress."),+ ]);+ let stack = boot_stack().await;++ let mut events = spawn_sse_collector(format!(+ "{}/events?client_id=harness-todo-invariant",+ stack.rpc_base+ ));+ send_web_chat(+ &stack.rpc_base,+ 700,+ "harness-todo-invariant",+ "thread-todo-invariant",+ "track two things at once",+ )+ .await;+ let (terminal, results) = collect_turn_tool_results(&mut events, Duration::from_secs(60)).await;+ assert_eq!(+ terminal.get("event").and_then(Value::as_str),+ Some("chat_done"),+ "{terminal}"+ );++ let todo_frames: Vec<&Value> = results+ .iter()+ .filter(|frame| frame.get("tool_name").and_then(Value::as_str) == Some("todo"))+ .collect();+ assert_eq!(todo_frames.len(), 3, "write, rejected write, read: {results:?}");+ assert_eq!(todo_frames[0]["success"], json!(true), "{}", todo_frames[0]);+ assert_eq!(+ todo_frames[1]["success"],+ json!(false),+ "two in_progress must be rejected: {}",+ todo_frames[1]+ );+ assert!(+ todo_frames[1]["output"]+ .as_str()+ .unwrap_or("")+ .contains("in_progress"),+ "the error names the invariant: {}",+ todo_frames[1]+ );+ let read: Value = serde_json::from_str(todo_frames[2]["output"].as_str().unwrap()).unwrap();+ assert_eq!(+ todo_statuses(&read),+ vec!["in_progress", "pending"],+ "the rejected write left the list untouched: {read}"+ );++ stack.shutdown();+}++/// The thread goal across turns: `goal_set` records the objective with a+/// budget and answers with the structured goal; a later turn's `goal_get`+/// reads the same goal back (it persisted); `goal_complete` closes it. Each+/// payload is what the goal banner renders, live and after reload.+#[test]+fn thread_goal_is_set_read_back_and_completed_across_turns() {+ run_on_agent_stack(+ "thread_goal_is_set_read_back_and_completed_across_turns",+ thread_goal_is_set_read_back_and_completed_across_turns_inner,+ );+}++async fn thread_goal_is_set_read_back_and_completed_across_turns_inner() {+ let _lock = env_lock();+ reset_script(vec![+ tool_call_completion(+ "goal_set",+ json!({ "objective": "Ship the v2 release notes", "token_budget": 50000 }),+ ),+ text_completion("Goal recorded."),+ tool_call_completion("goal_get", json!({})),+ text_completion("Still on it."),+ tool_call_completion("goal_complete", json!({})),+ text_completion("Release notes shipped."),+ ]);+ let stack = boot_stack().await;++ let mut events = spawn_sse_collector(format!(+ "{}/events?client_id=harness-goal",+ stack.rpc_base+ ));++ // Turn 1: goal_set.+ send_web_chat(+ &stack.rpc_base,+ 800,+ "harness-goal",+ "thread-goal",+ "Ship the v2 release notes.",+ )+ .await;+ let (terminal, results) = collect_turn_tool_results(&mut events, Duration::from_secs(60)).await;+ assert_eq!(terminal["event"].as_str(), Some("chat_done"), "{terminal}");+ let set = tool_result_payload(&results, "goal_set");+ assert_eq!(set["goal"]["objective"], "Ship the v2 release notes", "{set}");+ assert_eq!(set["goal"]["status"], "active", "{set}");+ assert_eq!(set["goal"]["tokenBudget"], 50000, "{set}");+ assert_eq!(set["goal"]["threadId"], "thread-goal", "bound to the chat thread: {set}");+ let goal_id = set["goal"]["goalId"]+ .as_str()+ .filter(|id| !id.is_empty())+ .expect("goal_set mints a goal id")+ .to_string();+ assert!(+ set["text"].as_str().unwrap().starts_with("Goal set."),+ "the model-facing text: {set}"+ );++ // Turn 2: goal_get reads the persisted goal back — same id, still active,+ // and the first turn's usage has been charged against the budget.+ send_web_chat(&stack.rpc_base, 801, "harness-goal", "thread-goal", "status?").await;+ let (terminal, results) = collect_turn_tool_results(&mut events, Duration::from_secs(60)).await;+ assert_eq!(terminal["event"].as_str(), Some("chat_done"), "{terminal}");+ let got = tool_result_payload(&results, "goal_get");+ assert_eq!(got["goal"]["goalId"], goal_id, "same goal across turns: {got}");+ assert_eq!(got["goal"]["status"], "active", "{got}");+ assert!(+ got["goal"]["tokensUsed"].as_u64().unwrap_or(0) > 0,+ "turn 1's tokens were accounted against the goal: {got}"+ );++ // Turn 3: goal_complete.+ send_web_chat(&stack.rpc_base, 802, "harness-goal", "thread-goal", "done?").await;+ let (terminal, results) = collect_turn_tool_results(&mut events, Duration::from_secs(60)).await;+ assert_eq!(terminal["event"].as_str(), Some("chat_done"), "{terminal}");+ let done = tool_result_payload(&results, "goal_complete");+ assert_eq!(done["goal"]["goalId"], goal_id, "{done}");+ assert_eq!(done["goal"]["status"], "complete", "{done}");+ assert!(+ done["text"].as_str().unwrap().starts_with("Goal marked complete."),+ "{done}"+ );++ // The persisted turn state carries every goal payload for a reload: the+ // newest row is the completion.+ let turns = turn_state_history(&stack.rpc_base, 850, "thread-goal").await;+ assert_eq!(turns.len(), 3, "one snapshot per turn");+ let names: Vec<String> = ["goal_set", "goal_get", "goal_complete"]+ .iter()+ .flat_map(|name| persisted_tool_rows(&turns, name))+ .map(|row| row["name"].as_str().unwrap().to_string())+ .collect();+ assert_eq!(names, vec!["goal_set", "goal_get", "goal_complete"]);+ let persisted_done: Value = serde_json::from_str(+ persisted_tool_rows(&turns, "goal_complete")[0]["output"]+ .as_str()+ .expect("persisted goal_complete keeps its output"),+ )+ .unwrap();+ assert_eq!(persisted_done["goal"]["status"], "complete", "{persisted_done}");++ // A thread that never set a goal reads back none — the payload the pane+ // treats as "no banner".+ reset_script(vec![+ tool_call_completion("goal_get", json!({})),+ text_completion("No goal here."),+ ]);+ send_web_chat(+ &stack.rpc_base,+ 803,+ "harness-goal",+ "thread-goal-none",+ "any goal?",+ )+ .await;+ let (terminal, results) = collect_turn_tool_results(&mut events, Duration::from_secs(60)).await;+ assert_eq!(terminal["event"].as_str(), Some("chat_done"), "{terminal}");+ let none = tool_result_payload(&results, "goal_get");+ assert!(none["goal"].is_null(), "{none}");+ assert_eq!(none["text"], "no goal set for this thread", "{none}");++ stack.shutdown();+}