Skip to content

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

fix(todo): convert argument errors to tool errors instead of fatal harness errors

Steven Enamakel15468b8078ed
diff --git a/crates/openhuman-core/src/agent/tools/todo.rs b/crates/openhuman-core/src/agent/tools/todo.rs
index aef5846d35..375cfa821f 100644
--- a/crates/openhuman-core/src/agent/tools/todo.rs
+++ b/crates/openhuman-core/src/agent/tools/todo.rs
@@ -43,9 +43,21 @@ impl ToolDispatch<(), crate::agent::tinyagents::host::OpenHumanRunContext> for T
parent: &RunContext<crate::agent::tinyagents::host::OpenHumanRunContext>,
) -> anyhow::Result<ToolResult> {
let context = ToolExecutionContext::from_run_context(parent, _call_id.clone());
- TodoTool::new()
+ // A dispatch `Err` is fatal to the whole run in the harness
+ // ("canonical execution errors remain fatal"), so nothing about a bad
+ // argument may escape as one: it goes back to the model as a tool
+ // error it can correct. A turn died this way when a model sent the
+ // retired `{"cards": …}` shape.
+ match TodoTool::new()
.execute_with_parent_context(arguments, parent.data.parent.clone(), Some(&context))
.await
+ {
+ Ok(result) => Ok(result),
+ Err(error) => {
+ tracing::warn!(%error, "[tool][todo] rejected call");
+ Ok(ToolResult::error(format!("todo failed: {error}")))
+ }
+ }
}
}
@@ -138,25 +150,20 @@ impl TodoTool {
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,
- Some(raw) => {
- let items: Vec<TodoItem> = serde_json::from_value(raw.clone())
- .map_err(|e| anyhow::anyhow!("invalid `todos`: {e}"))?;
- let mut cards = Vec::with_capacity(items.len());
- for item in items {
- let content = item.content.trim();
- if content.is_empty() {
- anyhow::bail!("every todo needs non-empty `content`");
- }
- let mut card = TaskBoardCard::new(content);
- card.status = match item.status.as_deref() {
- None => TaskCardStatus::Todo,
- Some(raw) => ops::parse_status(raw).map_err(anyhow::Error::msg)?,
- };
- cards.push(card);
- }
- ops::replace(&scope, cards).await
- }
+ None | Some(serde_json::Value::Null) => match args.as_object() {
+ // A write that used some other key (`cards`, `items`, …) is a
+ // shape error to report, not a request to read the list.
+ Some(map) if !map.is_empty() => Err(format!(
+ "unknown arguments {:?}: pass `todos` (the full list of \
+ {{content, status}}), or no arguments to read the list",
+ map.keys().collect::<Vec<_>>()
+ )),
+ _ => ops::list(&scope).await,
+ },
+ Some(raw) => match parse_items(raw) {
+ Ok(cards) => ops::replace(&scope, cards).await,
+ Err(error) => Err(error),
+ },
};
match result {
@@ -183,6 +190,27 @@ impl TodoTool {
}
}
+/// Turn the model's `todos` array into store cards; every problem is a
+/// message for the model, never a harness error.
+fn parse_items(raw: &serde_json::Value) -> Result<Vec<TaskBoardCard>, String> {
+ let items: Vec<TodoItem> =
+ serde_json::from_value(raw.clone()).map_err(|e| format!("invalid `todos`: {e}"))?;
+ let mut cards = Vec::with_capacity(items.len());
+ for item in items {
+ let content = item.content.trim();
+ if content.is_empty() {
+ return Err("every todo needs non-empty `content`".to_string());
+ }
+ let mut card = TaskBoardCard::new(content);
+ card.status = match item.status.as_deref() {
+ None => TaskCardStatus::Todo,
+ Some(raw) => ops::parse_status(raw)?,
+ };
+ cards.push(card);
+ }
+ Ok(cards)
+}
+
/// The three states the model is told about. Store states the list can no
/// longer produce (`ready`, `awaiting_approval`, `rejected`, `blocked`) fold
/// into the nearest one so an old thread still reads sensibly.
diff --git a/crates/openhuman-core/src/agent/tools/todo_tests.rs b/crates/openhuman-core/src/agent/tools/todo_tests.rs
index 7c926b9ae2..1a705d295c 100644
--- a/crates/openhuman-core/src/agent/tools/todo_tests.rs
+++ b/crates/openhuman-core/src/agent/tools/todo_tests.rs
@@ -77,20 +77,22 @@ async fn two_in_progress_items_are_rejected() {
reset_scratch().await;
}
+/// Bad input is a tool error the model can correct, never an `Err`: a
+/// dispatch `Err` is fatal to the whole run in the harness, and a turn died
+/// exactly that way when a model sent the retired `{"cards": …}` shape.
#[tokio::test]
-async fn empty_content_and_unknown_status_are_errors() {
+async fn bad_input_is_a_tool_error_not_a_harness_error() {
let tool = TodoTool::new();
- let err = tool
- .execute(json!({ "todos": [{ "content": " ", "status": "pending" }] }))
- .await
- .unwrap_err();
- assert!(err.to_string().contains("content"), "{err}");
-
- let err = tool
- .execute(json!({ "todos": [{ "content": "x", "status": "someday" }] }))
- .await
- .unwrap_err();
- assert!(err.to_string().contains("invalid status"), "{err}");
+ for (args, expect) in [
+ (json!({ "todos": [{ "content": " ", "status": "pending" }] }), "content"),
+ (json!({ "todos": [{ "content": "x", "status": "someday" }] }), "invalid status"),
+ (json!({ "todos": "not a list" }), "invalid `todos`"),
+ (json!({ "cards": [{ "content": "x", "status": "todo" }] }), "pass `todos`"),
+ ] {
+ let result = tool.execute(args.clone()).await.expect("never an Err: {args}");
+ assert!(result.is_error, "{args}");
+ assert!(result.output().contains(expect), "{args}: {}", result.output());
+ }
}
#[test]