Skip to content

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

test(tokenjuice): cover artifact handle resolution in repl tools

Steven Enamakel9964f04aa54e
diff --git a/crates/openhuman-core/src/inference/tokenjuice/repl_tools_tests.rs b/crates/openhuman-core/src/inference/tokenjuice/repl_tools_tests.rs
index 088dc80963..495ea8704b 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/repl_tools_tests.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/repl_tools_tests.rs
@@ -248,3 +248,153 @@ fn repl_tools_share_the_harness_tool_trait() {
);
assert_eq!(raw.len(), registry.len());
}
+
+/// A workspace whose tool-results dir holds one persisted shell output, the
+/// way `ToolResultArtifactStore::detached` writes it.
+fn artifacts_fixture() -> (tempfile::TempDir, std::path::PathBuf, std::path::PathBuf) {
+ let tmp = tempfile::TempDir::new().unwrap();
+ let dir = crate::security::policy::tool_result_artifacts_dir(tmp.path());
+ let file = dir.join("sess1").join("shell").join("call_abc123.txt");
+ std::fs::create_dir_all(file.parent().unwrap()).unwrap();
+ std::fs::write(&file, log_body()).unwrap();
+ (tmp, dir, file)
+}
+
+fn tools_with_artifacts(dir: std::path::PathBuf) -> Vec<Box<dyn Tool>> {
+ repl_tools_with_artifacts(
+ Arc::new(MemorySource(Arc::new(MemoryCcrStore::default()))),
+ ReplLimits::default(),
+ Some(dir),
+ )
+}
+
+#[tokio::test]
+async fn an_artifact_path_in_the_handle_slot_is_read_from_the_tool_results_dir() {
+ // Production: the model copies `artifact_path` from the oversized-output
+ // envelope into `handle` and got "invalid handle".
+ let (_tmp, dir, file) = artifacts_fixture();
+ let tools = tools_with_artifacts(dir);
+
+ let found = tools[tool(&tools, "juice_find")]
+ .execute(json!({ "handle": file.to_string_lossy(), "query": "needle" }))
+ .await
+ .unwrap();
+ assert!(!found.is_error, "{}", result_text(&found));
+ assert!(result_text(&found).contains("needle in the middle"));
+
+ let summary = tools[tool(&tools, "juice_summarize")]
+ .execute(json!({ "handle": file.to_string_lossy() }))
+ .await
+ .unwrap();
+ assert!(!summary.is_error, "{}", result_text(&summary));
+
+ // The legacy relative pointer form resolves against the same dir.
+ let relative = format!(
+ "artifacts/tool-results/{}",
+ file.strip_prefix(dir_of(&file, 3)).unwrap().to_string_lossy()
+ );
+ let by_relative = tools[tool(&tools, "juice_find")]
+ .execute(json!({ "handle": relative, "query": "needle" }))
+ .await
+ .unwrap();
+ assert!(!by_relative.is_error, "{}", result_text(&by_relative));
+}
+
+/// `path` with its last `levels` components removed.
+fn dir_of(path: &std::path::Path, levels: usize) -> std::path::PathBuf {
+ let mut p = path.to_path_buf();
+ for _ in 0..levels {
+ p.pop();
+ }
+ p
+}
+
+#[tokio::test]
+async fn an_artifact_path_outside_the_tool_results_dir_is_refused() {
+ let (tmp, dir, _file) = artifacts_fixture();
+ let secret = tmp.path().join("secret.txt");
+ std::fs::write(&secret, "needle secret").unwrap();
+ let tools = tools_with_artifacts(dir.clone());
+ let find = &tools[tool(&tools, "juice_find")];
+
+ let traversal = dir.join("sess1").join("..").join("..").join("..").join("secret.txt");
+ for bad in [
+ secret.to_string_lossy().into_owned(),
+ traversal.to_string_lossy().into_owned(),
+ "artifacts/tool-results/../../secret.txt".to_string(),
+ "/etc/passwd".to_string(),
+ ] {
+ let res = find
+ .execute(json!({ "handle": bad, "query": "needle" }))
+ .await
+ .unwrap();
+ assert!(res.is_error, "{bad} must be refused");
+ assert!(
+ !result_text(&res).contains("needle secret"),
+ "{bad} leaked content"
+ );
+ assert!(result_text(&res).contains("file_read"), "{}", result_text(&res));
+ }
+}
+
+#[cfg(unix)]
+#[tokio::test]
+async fn a_symlink_inside_the_tool_results_dir_cannot_escape_it() {
+ let (tmp, dir, _file) = artifacts_fixture();
+ let secret = tmp.path().join("secret.txt");
+ std::fs::write(&secret, "needle secret").unwrap();
+ let link = dir.join("sess1").join("shell").join("call_link.txt");
+ std::os::unix::fs::symlink(&secret, &link).unwrap();
+ let tools = tools_with_artifacts(dir);
+ let res = tools[tool(&tools, "juice_find")]
+ .execute(json!({ "handle": link.to_string_lossy(), "query": "needle" }))
+ .await
+ .unwrap();
+ assert!(res.is_error);
+ assert!(!result_text(&res).contains("needle secret"));
+}
+
+#[test]
+fn an_oversized_artifact_is_refused_before_it_is_read() {
+ let (_tmp, dir, file) = artifacts_fixture();
+ let err = read_tool_result_artifact(&dir, &file.to_string_lossy(), 16).unwrap_err();
+ assert!(err.contains("file_read"), "{err}");
+ let ok = read_tool_result_artifact(&dir, &file.to_string_lossy(), 10 * 1024 * 1024).unwrap();
+ assert!(ok.contains("needle in the middle"));
+}
+
+#[tokio::test]
+async fn a_call_id_or_other_non_handle_gets_an_actionable_message() {
+ let (_tmp, dir, _file) = artifacts_fixture();
+ for tools in [tools_with_artifacts(dir), repl_tools_with(
+ Arc::new(MemorySource(Arc::new(MemoryCcrStore::default()))),
+ ReplLimits::default(),
+ )] {
+ let find = &tools[tool(&tools, "juice_find")];
+ for bad in ["call_abc123", "toolu_01XyZ", "a b"] {
+ let res = find
+ .execute(json!({ "handle": bad, "query": "x" }))
+ .await
+ .unwrap();
+ assert!(res.is_error, "{bad} must be rejected");
+ let msg = result_text(&res);
+ assert!(msg.contains("32"), "names the handle shape: {msg}");
+ assert!(msg.contains("file_read"), "points at file_read: {msg}");
+ }
+ }
+}
+
+#[tokio::test]
+async fn an_artifact_path_without_a_configured_dir_points_at_file_read() {
+ let (_tmp, _dir, file) = artifacts_fixture();
+ let tools = repl_tools_with(
+ Arc::new(MemorySource(Arc::new(MemoryCcrStore::default()))),
+ ReplLimits::default(),
+ );
+ let res = tools[tool(&tools, "juice_find")]
+ .execute(json!({ "handle": file.to_string_lossy(), "query": "needle" }))
+ .await
+ .unwrap();
+ assert!(res.is_error);
+ assert!(result_text(&res).contains("file_read"));
+}