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
fix(middleware): handle missing tool output file read middleware
Steven Enamakel68f51a48c195
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware.rs b/crates/openhuman-core/src/agent/tinyagents/middleware.rsindex da4a1bf614..029773ecc9 100644--- a/crates/openhuman-core/src/agent/tinyagents/middleware.rs+++ b/crates/openhuman-core/src/agent/tinyagents/middleware.rs@@ -37,6 +37,7 @@ mod research_budget;mod tool_exposure;mod tool_outcome_capture;mod tool_output;+mod tool_output_file_read;mod tool_policy;mod turn_context;diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_file_read.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_file_read.rsnew file mode 100644index 0000000000..df171745cc--- /dev/null+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_file_read.rs@@ -0,0 +1,89 @@+//! Which tool calls only read files the agent can read again.+//!+//! [`super::tool_output::ToolOutputMiddleware`] skips TinyJuice's LLM summary+//! for these. A summary of source code is a paraphrase, and an agent about to+//! edit a file needs its exact text: on live coding runs every summarized+//! `cat` of a source file was followed by `juice_retrieve` of the whole+//! original or a fresh `sed -n` read of the file. The summary bought nothing+//! and cost a model call plus up to TinyJuice's 8-second summary timeout,+//! which it hit on most attempts.+//!+//! The test is deliberately narrow: a file-reading tool, or a shell command+//! made only of `cd` and plain reads (`cat`, `head`, `tail`, `nl`, `sed`+//! without `-i`, `echo`/`printf` separators), with no redirects, heredocs,+//! substitutions or other programs. Anything else (a build, a test run, a+//! search) keeps the normal pipeline: its output is not a file on disk.++use serde_json::Value;++/// Tools whose whole job is returning a file's contents.+const FILE_READ_TOOLS: &[&str] = &["file_read", "read_file", "read", "view_file"];++/// Shell tools, by the names OpenHuman and common harness adapters use.+const SHELL_TOOLS: &[&str] = &["shell", "bash", "exec", "run_command", "terminal"];++/// Programs that print a file (`sed` only without in-place editing).+const READERS: &[&str] = &["cat", "head", "tail", "nl", "sed"];++/// Programs allowed beside the readers: navigation and separators.+const NEUTRAL: &[&str] = &["cd", "echo", "printf", "true", "pwd"];++/// Whether this call only prints files the agent can re-read (see the module+/// docs). `use_skill` is followed into the tool it wraps.+pub(crate) fn is_file_read_call(tool_name: &str, args: &Value) -> bool {+ let (name, args) = if tool_name == "use_skill" {+ match (args.get("tool").and_then(Value::as_str), args.get("args")) {+ (Some(inner), Some(inner_args)) => (inner, inner_args),+ _ => return false,+ }+ } else {+ (tool_name, args)+ };+ if FILE_READ_TOOLS.contains(&name) {+ return true;+ }+ if !SHELL_TOOLS.contains(&name) {+ return false;+ }+ args.get("command")+ .or_else(|| args.get("cmd"))+ .and_then(Value::as_str)+ .is_some_and(is_file_read_command)+}++/// Whether a shell command line only prints files: every segment (split on+/// `&&`, `||`, `;`, `|` and newlines) runs a reader or a neutral program, at+/// least one runs a reader, and nothing redirects, substitutes or feeds a+/// heredoc.+pub(crate) fn is_file_read_command(command: &str) -> bool {+ if command.contains(['>', '<', '`']) || command.contains("$(") {+ return false;+ }+ let mut reads = false;+ for segment in command+ .split(['\n', ';', '|', '&'])+ .map(str::trim)+ .filter(|s| !s.is_empty())+ {+ let mut words = segment.split_whitespace();+ let Some(program) = words.next() else { continue };+ let program = program.rsplit('/').next().unwrap_or(program);+ if program == "sed"+ && segment+ .split_whitespace()+ .any(|w| w.starts_with("-i") || w == "--in-place")+ {+ return false;+ }+ if READERS.contains(&program) {+ reads = true;+ } else if !NEUTRAL.contains(&program) {+ return false;+ }+ }+ reads+}++#[cfg(test)]+#[path = "tool_output_file_read_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_file_read_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_file_read_tests.rsnew file mode 100644index 0000000000..2aaff7c951--- /dev/null+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_file_read_tests.rs@@ -0,0 +1,50 @@+use super::*;+use serde_json::json;++#[test]+fn plain_reads_of_files_are_file_reads() {+ for command in [+ "cat vm/vmExprFunction.go",+ "cd /app && cat vm/vmExprFunction.go",+ "cd /app && cat -n parser/lexer.go",+ "cd /app && sed -n '120,300p' vm/vmExprFunction.go",+ "cd /app && cat parser/Makefile && echo \"=== parser.go.y ===\" && cat parser/parser.go.y",+ "cd /app && sed -n '125,150p' ast/expr.go && echo \"=== vmExprFunction.go ===\" && cat vm/vmExprFunction.go",+ "head -50 README.md; tail -20 CHANGELOG.md",+ "cat -n src/lib.rs | sed -n '1,80p'",+ ] {+ assert!(is_file_read_command(command), "{command}");+ }+}++#[test]+fn anything_that_runs_writes_or_searches_is_not() {+ for command in [+ "go test ./vm/...",+ "cd /app && git diff",+ "grep -rn FuncExpr .",+ "cat foo.go > bar.go",+ "sed -i 's/a/b/' foo.go",+ "sed --in-place 's/a/b/' foo.go",+ "cat <<'EOF' > x.go\npackage x\nEOF",+ "cat $(git ls-files)",+ "cd /app && cat a.go && go build ./...",+ "echo hello",+ "cd /app",+ ] {+ assert!(!is_file_read_command(command), "{command}");+ }+}++#[test]+fn file_read_tools_and_wrapped_calls_are_recognised() {+ assert!(is_file_read_call("file_read", &json!({"path": "a.rs"})));+ assert!(is_file_read_call("shell", &json!({"command": "cat a.rs"})));+ assert!(!is_file_read_call("shell", &json!({"command": "cargo test"})));+ assert!(!is_file_read_call("web_fetch", &json!({"url": "https://example.com"})));+ assert!(is_file_read_call(+ "use_skill",+ &json!({"tool": "shell", "args": {"command": "cat a.rs"}})+ ));+ assert!(!is_file_read_call("use_skill", &json!({"tool": "shell"})));+}