Skip to content

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

test(tokenjuice): trim repl tools tests to the adapter case

Steven Enamakel9c8ebe1af086
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 077cacc8b0..a641b434ce 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/repl_tools_tests.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/repl_tools_tests.rs
@@ -1,30 +1,9 @@
use super::*;
-use serde_json::json;
use std::sync::Mutex;
use tinyjuice_bus::repl::ReplOutput;
+use tinytools::{Tool, ToolCallOptions};
-#[derive(Default)]
-struct RecordingSource(Mutex<Vec<QueryRequest>>);
-
-#[async_trait]
-impl QuerySource for RecordingSource {
- async fn query(&self, _: &DispatchCtx, request: QueryRequest) -> Result<QueryResponse, String> {
- self.0.lock().unwrap().push(request);
- Ok(Ok(ReplOutput::Text {
- text: "needle in the middle".into(),
- }))
- }
-}
-
-struct FailingSource;
-#[async_trait]
-impl QuerySource for FailingSource {
- async fn query(&self, _: &DispatchCtx, _: QueryRequest) -> Result<QueryResponse, String> {
- Err("module unavailable".into())
- }
-}
-
-/// Records the core of the context each query was handed.
+/// Records which core context the OpenHuman adapter forwards to module calls.
#[derive(Default)]
struct CoreRecordingSource(Mutex<Vec<Option<Arc<crate::core::runtime::CoreContext>>>>);
@@ -36,315 +15,7 @@ impl QuerySource for CoreRecordingSource {
}
}
-fn tool(tools: &[Box<dyn Tool>], name: &str) -> usize {
- tools.iter().position(|t| t.name() == name).unwrap()
-}
-fn result_text(result: &ToolResult) -> String {
- result.output()
-}
-fn log_body() -> String {
- "# Report\nrow\nERROR: needle in the middle\n".repeat(100)
-}
-
-#[test]
-fn declarations_preserve_names_schemas_and_read_only_caps() {
- let tools = repl_tools();
- assert_eq!(
- tools.iter().map(|t| t.name()).collect::<Vec<_>>(),
- tinyjuice_bus::tools::REPL_TOOLS
- );
- for (tool, declaration) in tools
- .iter()
- .zip(tinyjuice_bus::tools::repl_tool_declarations())
- {
- assert_eq!(tool.parameters_schema(), declaration.parameters);
- assert_eq!(tool.description(), declaration.description);
- assert_eq!(tool.permission_level(), PermissionLevel::ReadOnly);
- assert!(tool.is_concurrency_safe(&json!({})));
- assert!(!tool.external_effect());
- assert_eq!(tool.max_result_size_chars(), Some(16_000));
- }
-}
-
-#[tokio::test]
-async fn a_cached_handle_is_queried_without_retrieving_the_original() {
- let source = Arc::new(RecordingSource::default());
- let tools = repl_tools_with(source.clone(), ReplLimits::default());
- let result = tools[0]
- .execute(json!({"handle":"⟦tj:abc123⟧", "query":"needle"}))
- .await
- .unwrap();
- assert!(!result.is_error);
- let requests = source.0.lock().unwrap();
- assert_eq!(requests.len(), 1);
- assert_eq!(
- requests[0].target,
- QueryTarget::Handle {
- token: "abc123".into()
- }
- );
- assert!(matches!(&requests[0].op, ReplOp::Find {query, ..} if query == "needle"));
-}
-
-#[tokio::test]
-async fn authorized_artifacts_send_content_without_a_path() {
- let (_tmp, dir, file) = artifacts_fixture();
- let source = Arc::new(RecordingSource::default());
- let tools = repl_tools_with_artifacts(source.clone(), ReplLimits::default(), Some(dir));
- let result = tools[0]
- .execute(json!({"handle":file.to_string_lossy(), "query":"needle"}))
- .await
- .unwrap();
- assert!(!result.is_error);
- assert_eq!(
- source.0.lock().unwrap()[0].target,
- QueryTarget::Content {
- content: log_body()
- }
- );
-}
-
-#[tokio::test]
-async fn rejected_paths_and_invalid_operations_never_reach_the_module() {
- let source = Arc::new(RecordingSource::default());
- let tools = repl_tools_with(source.clone(), ReplLimits::default());
- for args in [
- json!({"handle":"../secret", "query":"x"}),
- json!({"handle":"abc123"}),
- json!({}),
- ] {
- assert!(tools[0].execute(args).await.unwrap().is_error);
- }
- assert!(source.0.lock().unwrap().is_empty());
-}
-
-#[tokio::test]
-async fn an_unavailable_module_is_a_tool_error() {
- let tools = repl_tools_with(Arc::new(FailingSource), ReplLimits::default());
- let result = tools[0]
- .execute(json!({"handle":"abc123", "query":"x"}))
- .await
- .unwrap();
- assert!(result.is_error);
- assert!(result.output().contains("module unavailable"));
-}
-
-#[tokio::test]
-async fn marker_and_bare_handles_reach_the_source_and_junk_does_not() {
- let source = Arc::new(RecordingSource::default());
- let tools = repl_tools_with(source.clone(), ReplLimits::default());
- for ok in ["abc123", " abc123 ", "\u{27e6}tj:abc123\u{27e7}"] {
- let r = tools[0]
- .execute(json!({"handle": ok, "query":"x"}))
- .await
- .unwrap();
- assert!(!r.output().contains("not a juice handle"), "{ok}");
- }
- for bad in ["call_abc", "../x", &"f".repeat(65)] {
- let r = tools[0]
- .execute(json!({"handle": bad, "query":"x"}))
- .await
- .unwrap();
- assert!(r.is_error, "{bad}");
- }
-}
-
-/// 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(RecordingSource::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(RecordingSource::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(RecordingSource::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"));
-}
-
-/// A workspace reached through a symlinked component (macOS `/var` ->
-/// `/private/var`) still accepts the canonical absolute `artifact_path` the
-/// store hands out, while the canonical containment check keeps refusing
-/// anything outside the tool-results dir.
-#[cfg(unix)]
-#[test]
-fn a_canonical_artifact_path_is_accepted_when_the_workspace_is_a_symlink() {
- let tmp = tempfile::TempDir::new().unwrap();
- let real = tmp.path().join("real");
- std::fs::create_dir_all(&real).unwrap();
- let alias = tmp.path().join("alias");
- std::os::unix::fs::symlink(&real, &alias).unwrap();
-
- let dir = crate::security::policy::tool_result_artifacts_dir(&alias);
- 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();
- let canonical_file = std::fs::canonicalize(&file).unwrap();
- assert!(
- !canonical_file.starts_with(&dir),
- "fixture must differ lexically"
- );
-
- let ok = read_tool_result_artifact(&dir, &canonical_file.to_string_lossy(), 1 << 20)
- .expect("canonical artifact path must be readable");
- assert!(ok.contains("needle in the middle"));
-
- let secret = real.join("secret.txt");
- std::fs::write(&secret, "needle secret").unwrap();
- let canonical_secret = std::fs::canonicalize(&secret).unwrap();
- let err =
- read_tool_result_artifact(&dir, &canonical_secret.to_string_lossy(), 1 << 20).unwrap_err();
- assert!(!err.contains("needle secret"), "{err}");
-}
-
-/// A query runs for the context of the run that called the tool, never for the
-/// scope of the task that polls it: two runs under two contexts do not cross,
-/// even with a third context ambient.
+/// Each tool call uses its own run context rather than a concurrent ambient one.
#[tokio::test]
async fn queries_run_for_the_tool_runs_context_not_the_ambient_one() {
use crate::agent::tinyagents::host::OpenHumanRunContext;
@@ -366,7 +37,7 @@ async fn queries_run_for_the_tool_runs_context_not_the_ambient_one() {
);
let source = Arc::new(CoreRecordingSource::default());
let tools = repl_tools_with(source.clone(), ReplLimits::default());
- let args = json!({"handle":"⟦tj:abc123⟧", "query":"needle"});
+ let args = serde_json::json!({"handle":"⟦tj:abc123⟧", "query":"needle"});
CoreContext::scope(ambient.clone(), async {
for core in [&a, &b] {