Skip to content

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

fix(files): page long file reads

Steven Enamakelc329f8439aed
diff --git a/crates/openhuman-core/src/tools/impl/filesystem/file_read.rs b/crates/openhuman-core/src/tools/impl/filesystem/file_read.rs
index 2aa3c8c264..f606eb27bf 100644
--- a/crates/openhuman-core/src/tools/impl/filesystem/file_read.rs
+++ b/crates/openhuman-core/src/tools/impl/filesystem/file_read.rs
@@ -7,6 +7,11 @@ use std::sync::Arc;
use tinytools::ToolRunContext;
const MAX_FILE_SIZE_BYTES: u64 = 10 * 1024 * 1024;
+/// Keep a page below the harness's 16 KiB per-result ceiling so the continuation
+/// marker survives the result middleware. Returning the whole remainder and
+/// relying on that middleware to truncate it loses the next offset and makes a
+/// model repeat the same read forever.
+const MAX_PAGE_BYTES: usize = 12 * 1024;
/// Read file contents with path sandboxing
pub struct FileReadTool {
@@ -33,7 +38,8 @@ impl Tool for FileReadTool {
"Read the contents of a file in your working directory (the action sandbox). \
Relative paths resolve against that directory; paths outside it are blocked. \
To read a file written by `shell`, confirm its location with `pwd` and use the \
- same relative path."
+ same relative path. Long files are returned one page at a time; continue with \
+ the exact `offset` reported at the end of the page."
}
fn parameters_schema(&self) -> serde_json::Value {
@@ -148,9 +154,6 @@ impl FileReadTool {
}
},
};
- if offset == 0 {
- return Ok(ToolResult::success(contents));
- }
if offset > contents.len() {
return Ok(ToolResult::error(format!(
"offset {offset} is past the end of the file ({} bytes)",
@@ -165,7 +168,24 @@ impl FileReadTool {
"offset {offset} falls inside a multi-byte character; continue from the offset a paged read reports"
)));
}
- Ok(ToolResult::success(contents[offset..].to_string()))
+ let mut end = offset.saturating_add(MAX_PAGE_BYTES).min(contents.len());
+ while end > offset && !contents.is_char_boundary(end) {
+ end -= 1;
+ }
+
+ if end == contents.len() {
+ return Ok(ToolResult::success(contents[offset..end].to_string()));
+ }
+
+ let mut page = contents[offset..end].to_string();
+ let path_json = serde_json::to_string(path)
+ .unwrap_or_else(|_| "\"<unrenderable path>\"".to_string());
+ page.push_str(&format!(
+ "\n\n[file_read page: bytes {offset}..{end} of {}; continue with \
+ file_read {{\"path\":{path_json},\"offset\":{end}}}]",
+ contents.len()
+ ));
+ Ok(ToolResult::success(page))
}
Err(e) => Ok(ToolResult::error(format!("Failed to read file: {e}"))),
}
diff --git a/crates/openhuman-core/src/tools/impl/filesystem/file_read_tests.rs b/crates/openhuman-core/src/tools/impl/filesystem/file_read_tests.rs
index 6efc482de2..bb8ed8f5f8 100644
--- a/crates/openhuman-core/src/tools/impl/filesystem/file_read_tests.rs
+++ b/crates/openhuman-core/src/tools/impl/filesystem/file_read_tests.rs
@@ -324,6 +324,46 @@ async fn file_read_offset_continues_from_the_given_byte() {
);
}
+#[tokio::test]
+async fn file_read_pages_long_files_and_reports_a_working_continuation() {
+ let dir = tempfile::tempdir().unwrap();
+ let contents = format!("{}éTAIL", "x".repeat(MAX_PAGE_BYTES - 1));
+ tokio::fs::write(dir.path().join("long.txt"), &contents)
+ .await
+ .unwrap();
+ let tool = FileReadTool::new(test_security(dir.path().to_path_buf()));
+
+ let first = tool.execute(json!({"path": "long.txt"})).await.unwrap();
+ assert!(!first.is_error, "{}", first.output());
+ assert!(
+ first.output().contains("continue with file_read"),
+ "a truncated page must tell the caller exactly how to continue: {}",
+ first.output()
+ );
+ assert!(
+ first.output().len() < 16 * 1024,
+ "the page and marker must survive the harness's per-result budget"
+ );
+ let marker = first
+ .output()
+ .split("\"offset\":")
+ .nth(1)
+ .and_then(|tail| tail.split('}').next())
+ .and_then(|value| value.parse::<usize>().ok())
+ .expect("continuation offset in marker");
+ assert!(contents.is_char_boundary(marker));
+
+ let second = tool
+ .execute(json!({"path": "long.txt", "offset": marker}))
+ .await
+ .unwrap();
+ assert!(!second.is_error, "{}", second.output());
+ assert_eq!(
+ format!("{}{}", &first.output()[..marker], second.output()),
+ contents
+ );
+}
+
#[tokio::test]
async fn file_read_rejects_an_offset_inside_a_multibyte_character() {
let dir = tempfile::tempdir().unwrap();