Skip to content

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

refactor(tokenjuice): use shared handle classification in repl tools

Steven Enamakel4f221166aae3
diff --git a/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs b/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs
index 9f59ea99a3..45c3cbdc4c 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs
@@ -6,6 +6,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use serde_json::Value;
+use tinyjuice_bus::handle;
use tinyjuice_bus::repl::{ReplLimits, ReplOp};
use tinyjuice_bus::wire::{QueryError, QueryRequest, QueryResponse, QueryTarget};
use tinytools::{PermissionLevel, Tool, ToolResult};
@@ -17,8 +18,6 @@ pub fn is_repl_tool(name: &str) -> bool {
REPL_TOOL_NAMES.contains(&name)
}
-/// Longest handle accepted. Real handles are 32 hex characters.
-const MAX_HANDLE_LEN: usize = 64;
/// Largest persisted tool-result artifact read through the handle slot: the
/// same limit `file_read` applies, which is also the most the artifact store
@@ -156,40 +155,6 @@ pub(crate) fn repl_tools_with_artifacts(
.collect()
}
-/// Accept a bare handle, or the `⟦tj:<handle>⟧` marker form.
-fn normalize_handle(raw: &str) -> Option<&str> {
- let handle = raw
- .trim()
- .trim_start_matches("⟦tj:")
- .trim_end_matches('⟧')
- .trim();
- let valid = !handle.is_empty()
- && handle.len() <= MAX_HANDLE_LEN
- && handle.chars().all(|c| c.is_ascii_alphanumeric());
- valid.then_some(handle)
-}
-
-/// What the model passed in the `handle` slot.
-enum HandleArg {
- /// A CCR handle (bare or marker form), normalized.
- Handle(String),
- /// Something path-shaped: possibly a persisted tool-result artifact.
- Path(String),
- /// Neither: a tool call id, prose, an over-long token.
- Invalid,
-}
-
-fn classify_handle_arg(raw: &str) -> HandleArg {
- let trimmed = raw.trim();
- if trimmed.contains('/') || trimmed.contains('\\') || trimmed.ends_with(".txt") {
- return HandleArg::Path(trimmed.to_string());
- }
- match normalize_handle(trimmed) {
- Some(handle) => HandleArg::Handle(handle.to_string()),
- None => HandleArg::Invalid,
- }
-}
-
fn not_a_handle_message() -> &'static str {
"juice: that is not a juice handle. A handle is the 32-character hex token named in a \
stored-output footer (or its `⟦tj:<handle>⟧` marker); a tool call id such as `call_…` \
@@ -365,10 +330,10 @@ impl Tool for ModuleReplTool {
let Some(raw) = args.get("handle").and_then(Value::as_str) else {
return Ok(ToolResult::error("missing required argument: handle"));
};
- let handle = match classify_handle_arg(raw) {
- HandleArg::Handle(handle) => handle,
- HandleArg::Path(path) => return self.execute_on_artifact(path, args).await,
- HandleArg::Invalid => {
+ let handle = match handle::classify(raw) {
+ handle::HandleArg::Handle(handle) => handle,
+ handle::HandleArg::Path(path) => return self.execute_on_artifact(path, args).await,
+ handle::HandleArg::Invalid => {
log::debug!("[tokenjuice][repl] {} rejected a non-handle", self.name);
return Ok(ToolResult::error(not_a_handle_message()));
}
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 e36b58ea84..f719d29186 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/repl_tools_tests.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/repl_tools_tests.rs
@@ -117,12 +117,18 @@ async fn an_unavailable_module_is_a_tool_error() {
assert!(result.output().contains("module unavailable"));
}
-#[test]
-fn handle_normalization_only_accepts_alphanumeric_tokens() {
- assert_eq!(normalize_handle(" abc123 "), Some("abc123"));
- assert_eq!(normalize_handle("⟦tj:abc123⟧"), Some("abc123"));
- assert_eq!(normalize_handle("../x"), None);
- assert_eq!(normalize_handle(&"f".repeat(MAX_HANDLE_LEN + 1)), None);
+#[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