Skip to content

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

feat(agent): persist oversized tool results as artifacts

Steven Enamakel5feef00014b1
diff --git a/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod.rs b/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod.rs
index 4c54109863..78f0d4f1fa 100644
--- a/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod.rs
+++ b/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod.rs
@@ -147,6 +147,19 @@ fn set_read_path(args: &mut Value, path: &str) {
}
}
+/// A store over a private in-memory blob store, materialising under
+/// `materialize_root`: the tests' stand-in for a tenant's scoped storage.
+#[cfg(test)]
+pub(crate) fn in_memory_tool_result_store(
+ materialize_root: PathBuf,
+ session_key: impl Into<String>,
+) -> HostArtifactStore {
+ let scope = tinystoragedrivers::Scope::new("local").expect("scope");
+ let blobs = tinystoragedrivers::MemoryStorage::new();
+ let blobs = tinystoragedrivers::StorageBackend::for_scope(&blobs, &scope).expect("scope");
+ new_tool_result_store(Arc::clone(blobs.blobs()), materialize_root, session_key)
+}
+
/// The artifact a tool call reads, if any — `file_read`, possibly wrapped in
/// `use_skill` (#6284).
///
diff --git a/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod_tests.rs b/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod_tests.rs
index f3a7ad058e..4828497bc5 100644
--- a/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod_tests.rs
+++ b/crates/openhuman-core/src/agent/harness/tool_result_artifacts/mod_tests.rs
@@ -6,25 +6,21 @@ use tinyagents_harness::artifacts::tool_results::apply_per_result_persistence;
use tinytools::Tool;
/// End to end through OpenHuman's wiring: the real `sanitize_text` redacts the
-/// stored body and the preview, the file lands under the workspace rather than
-/// the project the agent is editing, and the real `file_read` opens it at the
-/// absolute path the envelope names, under the strictest policy shape
-/// (enabled, `workspace_only`, action dir outside the workspace).
+/// stored body and the preview, the body lands on the blob store (nothing in
+/// the project the agent is editing), and the real `file_read` opens it through
+/// the materialised file under the strictest policy shape (enabled,
+/// `workspace_only`, action dir outside the workspace).
#[tokio::test]
-async fn threshold_persists_outside_the_project_and_reads_back() {
+async fn threshold_persists_to_blobs_and_file_read_serves_the_key() {
let tmp = tempfile::tempdir().unwrap();
let workspace = tmp.path().join("workspace");
let project = tmp.path().join("project");
std::fs::create_dir_all(&workspace).unwrap();
std::fs::create_dir_all(&project).unwrap();
- // `detached` adds its own `tool-results` namespace; give it the artifacts
- // directory so the final path matches the policy-granted root.
- let store = new_tool_result_store(workspace.join("artifacts"), "session/one");
- let raw = format!(
- "{} {}",
- "x".repeat(4096),
- "Bearer credential-marker-redact-me-123456"
- );
+ let root = crate::security::policy::tool_result_artifacts_dir(&workspace);
+ let store = in_memory_tool_result_store(root.clone(), "session/one");
+ let secret = "Bearer credential-marker-redact-me-123456";
+ let raw = format!("{} {}", "x".repeat(4096), secret);
let (out, outcome) = apply_per_result_persistence(
raw.clone(),
@@ -36,22 +32,21 @@ async fn threshold_persists_outside_the_project_and_reads_back() {
)
.await;
- let expected = workspace.join("artifacts/tool-results/session_one/shell/call-1.txt");
- let pointer = expected.to_string_lossy().into_owned();
+ let key = "tool-results/session_one/shell/call-1.txt";
assert!(outcome.persisted);
- assert!(
- out.contains(&format!("artifact_path: {pointer}\n")),
- "{out}"
- );
- assert!(out.contains("original_bytes:"));
+ assert!(out.contains(&format!("artifact_path: {key}\n")), "{out}");
assert!(out.contains("[preview]"));
- assert!(!out.contains("Bearer credential-marker-redact-me-123456"));
- assert!(expected.is_file());
- assert_eq!(
- std::fs::read_dir(&project).unwrap().count(),
- 0,
- "nothing may be written into the project"
- );
+ assert!(!out.contains(secret));
+ assert!(!root.exists(), "nothing is on disk until a read asks for it");
+ assert_eq!(std::fs::read_dir(&project).unwrap().count(), 0);
+
+ // The model's `file_read {"path": key}` is recognised and pointed at the
+ // materialised copy of the blob.
+ let mut args = json!({"path": key});
+ let read = artifact_read_target(Some(&store), "file_read", &args).expect("artifact read");
+ store.resolve_read(&mut args, &read).await;
+ let pointer = args["path"].as_str().unwrap().to_string();
+ assert_eq!(std::path::Path::new(&pointer), root.join("session_one/shell/call-1.txt"));
let cfg = crate::config::AutonomyConfig {
enabled: true,
@@ -60,12 +55,68 @@ async fn threshold_persists_outside_the_project_and_reads_back() {
};
let policy = Arc::new(SecurityPolicy::from_config(&cfg, &workspace, &project));
let reader = FileReadTool::new(policy);
- let read = reader.execute(json!({"path": pointer})).await.unwrap();
+ let read = reader.execute(args).await.unwrap();
assert!(!read.is_error, "{}", read.output());
assert!(read.output().contains("xxxx"));
- assert!(!read
- .output()
- .contains("Bearer credential-marker-redact-me-123456"));
+ assert!(!read.output().contains(secret));
+}
+
+/// A `use_skill`-wrapped read is rewritten at the call it wraps.
+#[tokio::test]
+async fn a_wrapped_read_is_pointed_at_the_materialised_file() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = in_memory_tool_result_store(tmp.path().join("tool-results"), "s");
+ let (_, outcome) = apply_per_result_persistence(
+ "y".repeat(4096),
+ None,
+ Some(&store),
+ "shell",
+ Some("c"),
+ 512,
+ )
+ .await;
+ let key = outcome.artifact_path.unwrap();
+ let mut args =
+ json!({"skill": "files", "tool": "file_read", "args": {"path": key, "offset": 3}});
+ let read = artifact_read_target(Some(&store), "use_skill", &args).unwrap();
+ store.resolve_read(&mut args, &read).await;
+ assert_ne!(args["args"]["path"], json!(key));
+ assert_eq!(args["args"]["offset"], json!(3));
+ assert!(std::path::Path::new(args["args"]["path"].as_str().unwrap()).is_file());
+}
+
+/// A blob store is the tenant's: an artifact another tenant stored is absent
+/// from this tenant's store, so its key is never served or materialised.
+#[tokio::test]
+async fn artifacts_are_tenant_scoped() {
+ use tinystoragedrivers::{MemoryStorage, Scope, StorageBackend};
+ let tmp = tempfile::tempdir().unwrap();
+ let backend = MemoryStorage::new();
+ let scoped = |name: &str| {
+ let scope = Scope::new(name).unwrap();
+ Arc::clone(backend.for_scope(&scope).unwrap().blobs())
+ };
+ let store_a = new_tool_result_store(scoped("tenant-a"), tmp.path().join("a"), "s");
+ let store_b = new_tool_result_store(scoped("tenant-b"), tmp.path().join("b"), "s");
+
+ let (_, outcome) = apply_per_result_persistence(
+ "secret ".repeat(1000),
+ None,
+ Some(&store_a),
+ "shell",
+ Some("c"),
+ 512,
+ )
+ .await;
+ let key = outcome.artifact_path.unwrap();
+
+ assert!(store_a.read(&key).await.unwrap().is_some());
+ assert!(store_b.read(&key).await.unwrap().is_none());
+ let mut args = json!({"path": key});
+ let read = artifact_read_target(Some(&store_b), "file_read", &args).unwrap();
+ store_b.resolve_read(&mut args, &read).await;
+ assert_eq!(args["path"], json!(key), "left unresolved for tenant b");
+ assert!(!tmp.path().join("b").exists());
}
/// The vocabulary passed to the crate is OpenHuman's: `file_read` reads,
@@ -83,12 +134,12 @@ fn read_targets_use_openhumans_tool_names() {
assert!(artifact_read_target(None, "glob", &json!({"path": path})).is_none());
}
-/// With the session's store, its absolute pointers page like the relative ones
-/// did, wrapped in `use_skill` or not.
+/// With the session's store, its `tool-results/` keys are recognised, wrapped
+/// in `use_skill` or not.
#[test]
-fn read_targets_recognise_the_detached_stores_absolute_pointers() {
+fn read_targets_recognise_the_stores_blob_keys() {
let tmp = tempfile::tempdir().unwrap();
- let store = new_tool_result_store(tmp.path().join("tool-results"), "s");
+ let store = in_memory_tool_result_store(tmp.path().join("tool-results"), "s");
let pointer = store.path_for_read_tool("shell", Some("c"));
assert!(artifact_read_target(Some(&store), "file_read", &json!({"path": pointer})).is_some());
assert!(artifact_read_target(
@@ -97,8 +148,12 @@ fn read_targets_recognise_the_detached_stores_absolute_pointers() {
&json!({"skill": "files", "tool": "file_read", "args": {"path": pointer, "offset": 3}})
)
.is_some_and(|read| read.offset == 3));
- // Without the store there is nothing to recognise an absolute path against.
- assert!(artifact_read_target(None, "file_read", &json!({"path": pointer})).is_none());
+ assert!(artifact_read_target(
+ Some(&store),
+ "file_read",
+ &json!({"path": "tool-results-backup/s/x.txt"})
+ )
+ .is_none());
}
/// A continuation names `file_read` and the offset, in the wire format the
diff --git a/crates/openhuman-core/src/agent/session_host/artifact_wiring_tests.rs b/crates/openhuman-core/src/agent/session_host/artifact_wiring_tests.rs
index 2b2cc868e3..8dded6549c 100644
--- a/crates/openhuman-core/src/agent/session_host/artifact_wiring_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/artifact_wiring_tests.rs
@@ -1,5 +1,7 @@
use super::*;
use crate::security::policy::tool_result_artifacts_dir;
+use serde_json::json;
+use tinyagents_harness::artifacts::tool_results::{apply_per_result_persistence, ArtifactRead};
const DAY: std::time::Duration = std::time::Duration::from_secs(24 * 60 * 60);
@@ -24,7 +26,7 @@ fn open_directory(path: &Path) -> std::fs::File {
std::fs::File::open(path).unwrap()
}
-fn stale_session(root: &Path, session: &str) -> PathBuf {
+fn stale_session(root: &Path, session: &str) -> std::path::PathBuf {
let dir = root.join(session).join("shell");
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("call.txt");
@@ -40,21 +42,52 @@ fn stale_session(root: &Path, session: &str) -> PathBuf {
root.join(session)
}
-/// The store writes under the workspace, never the action directory, whether
-/// with absolute pointers independent of the turn's working directory.
-#[test]
-fn the_store_is_rooted_in_the_workspace_not_the_action_dir() {
+/// The store materialises under the workspace, never the action directory, and
+/// hands the model a blob key rather than a path.
+#[tokio::test]
+async fn the_store_materialises_in_the_workspace_not_the_action_dir() {
let tmp = tempfile::tempdir().unwrap();
let workspace = tmp.path().join("workspace");
let store = build_artifact_store(&workspace, "s");
- assert!(store.is_detached());
- assert_eq!(store.root(), tool_result_artifacts_dir(&workspace));
+ assert_eq!(
+ store.materialize_root(),
+ tool_result_artifacts_dir(&workspace)
+ );
let pointer = store.path_for_read_tool("shell", Some("c"));
- assert!(Path::new(&pointer).starts_with(workspace.join("artifacts/tool-results")));
+ assert_eq!(pointer, "tool-results/s/shell/c.txt");
}
-/// Workspace retention never deletes files from the agent's project after
-/// retiring the old action-directory migration sweep.
+/// A large result is stored on the blob store, its pointer opens through the
+/// materialised file, and a rebuilt store (a session reload) still reads it.
+#[tokio::test]
+async fn a_stored_result_survives_a_session_reload() {
+ let tmp = tempfile::tempdir().unwrap();
+ let workspace = tmp.path().join("workspace");
+ let first = build_artifact_store(&workspace, "thread-1");
+ let raw = "z".repeat(4096);
+ let (out, outcome) =
+ apply_per_result_persistence(raw, None, Some(&first), "shell", Some("call-1"), 1024).await;
+ assert!(outcome.persisted, "{out}");
+ let key = outcome.artifact_path.expect("pointer");
+ assert!(key.starts_with("tool-results/thread-1/shell/"), "{key}");
+
+ let reloaded = build_artifact_store(&workspace, "thread-1");
+ let mut args = json!({"path": key});
+ reloaded
+ .resolve_read(
+ &mut args,
+ &ArtifactRead {
+ path: key.clone(),
+ offset: 0,
+ },
+ )
+ .await;
+ let path = args["path"].as_str().unwrap();
+ assert!(Path::new(path).starts_with(tool_result_artifacts_dir(&workspace)));
+ assert!(std::fs::read_to_string(path).unwrap().contains("zzzz"));
+}
+
+/// Retention sweeps stale materialised copies and nothing in the project.
#[test]
fn workspace_retention_leaves_legacy_project_artifacts_alone() {
let tmp = tempfile::tempdir().unwrap();
@@ -65,8 +98,13 @@ fn workspace_retention_leaves_legacy_project_artifacts_alone() {
let fresh = legacy_root.join("recent/shell");
std::fs::create_dir_all(&fresh).unwrap();
std::fs::write(fresh.join("call.txt"), "recent").unwrap();
- let detached_stale = stale_session(&tool_result_artifacts_dir(&workspace), "older");
+ let materialised_stale = stale_session(&tool_result_artifacts_dir(&workspace), "older");
+ let rt = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .unwrap();
+ let _guard = rt.enter();
build_artifact_store(&workspace, "current");
assert!(
@@ -74,5 +112,8 @@ fn workspace_retention_leaves_legacy_project_artifacts_alone() {
"project files are outside workspace retention"
);
assert!(fresh.join("call.txt").exists(), "a fresh one is kept");
- assert!(!detached_stale.exists(), "the workspace store is swept too");
+ assert!(
+ !materialised_stale.exists(),
+ "the workspace cache is swept too"
+ );
}
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_adapter_tests.rs b/crates/openhuman-core/src/agent/session_host/runtime_adapter_tests.rs
index df29c34eb6..1376d07dc3 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_adapter_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_adapter_tests.rs
@@ -614,14 +614,13 @@ async fn production_turn_path_wires_an_artifact_store_outside_the_project() {
"the production turn path must wire an artifact store, or oversized \
tool results are truncated with their tail discarded (#6408)",
);
- assert!(store.is_detached(), "the pointer must be absolute");
assert_eq!(
- store.root(),
+ store.materialize_root(),
crate::security::policy::tool_result_artifacts_dir(workspace.path()),
"the store must live under the workspace, not in the project the agent edits"
);
- assert!(!store.root().starts_with(action_dir.path()));
- assert!(!store.root().starts_with(turn_root.path()));
+ assert!(!store.materialize_root().starts_with(action_dir.path()));
+ assert!(!store.materialize_root().starts_with(turn_root.path()));
}
/// Without a descriptor the answer is the same: the workspace, not
@@ -657,7 +656,7 @@ async fn artifact_store_stays_in_the_workspace_without_a_descriptor() {
.as_ref()
.expect("artifact store");
assert_eq!(
- store.root(),
+ store.materialize_root(),
crate::security::policy::tool_result_artifacts_dir(workspace.path())
);
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_tests.rs
index acf56f0727..2a3bcd260e 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output_tests.rs
@@ -15,7 +15,7 @@ async fn same_tool_calls_persist_artifacts_under_distinct_call_ids() {
budget_bytes: 8,
payload_summarizer: None,
artifact_store: Some(
- crate::agent::harness::tool_result_artifacts::new_tool_result_store(
+ crate::agent::harness::tool_result_artifacts::in_memory_tool_result_store(
temp.path().to_path_buf(),
"identity-session",
),
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_artifact_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_artifact_tests.rs
index b5f3280002..b989aa6ec1 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_artifact_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_artifact_tests.rs
@@ -13,7 +13,7 @@ fn artifact_mw(
budget_bytes: 1_000,
payload_summarizer: summarizer,
artifact_store: Some(
- crate::agent::harness::tool_result_artifacts::new_tool_result_store(
+ crate::agent::harness::tool_result_artifacts::in_memory_tool_result_store(
storage_dir.to_path_buf(),
"session",
),