Skip to content

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

feat(tokenjuice): accept tool-result artifact paths in the handle slot

Steven Enamakelad9259679975
diff --git a/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs b/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs
index 47f2e0d0e1..4178576e4f 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/repl_tools.rs
@@ -15,9 +15,18 @@
//! the stock tool answers. The ops, argument parsing, size caps and
//! read-only/concurrency flags are TinyJuice's, unchanged.
//!
-//! Read-only, no side effects, no path or network access. Nothing here logs
-//! the handle's content or the query.
+//! Models also pass the `artifact_path` from a `[tool_result_preview]`
+//! envelope (an oversized output persisted under
+//! `<workspace>/artifacts/tool-results/`) where the handle goes. Such a path is
+//! read from that one directory — nowhere else, no traversal, no symlink out,
+//! and no larger than `file_read` would open — and queried the same way.
+//! Anything else that is not a handle (a tool call id such as `call_…`) gets a
+//! message saying what a handle looks like.
+//!
+//! Read-only, no side effects, no network access. Nothing here logs the
+//! handle's content or the query.
+use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use async_trait::async_trait;
@@ -36,6 +45,17 @@ pub fn is_repl_tool(name: &str) -> bool {
/// 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
+/// ever writes.
+const MAX_ARTIFACT_BYTES: u64 = tinytools_std::filesystem::FileReadTool::MAX_FILE_SIZE_BYTES;
+
+/// The relative pointer form older stores handed out (`artifacts/tool-results/…`).
+const RELATIVE_ARTIFACT_PREFIX: &str = "artifacts/tool-results/";
+
+/// Token the one-entry store files an artifact's content under.
+const ARTIFACT_TOKEN: &str = "artifact";
+
/// Where the original behind a handle comes from.
#[async_trait]
pub(crate) trait OriginalSource: Send + Sync {
@@ -77,9 +97,13 @@ struct ModuleReplTool {
cap: Option<usize>,
source: Arc<dyn OriginalSource>,
limits: ReplLimits,
+ /// `<workspace>/artifacts/tool-results`, when known: the one directory an
+ /// `artifact_path` passed as the handle may be read from.
+ artifacts_dir: Option<PathBuf>,
}
-/// The three REPL tools, reading through the TinyJuice module.
+/// The three REPL tools, reading through the TinyJuice module. Without a
+/// workspace they cannot resolve an `artifact_path`; see [`repl_tools_for`].
pub fn repl_tools() -> Vec<Box<dyn Tool>> {
repl_tools_with(Arc::new(ModuleSource), ReplLimits::default())
}
@@ -96,12 +120,26 @@ pub fn repl_tools_for(config: &crate::config::Config) -> Vec<Box<dyn Tool>> {
"[tokenjuice][repl] registering {}",
REPL_TOOL_NAMES.join(", ")
);
- repl_tools()
+ repl_tools_with_artifacts(
+ Arc::new(ModuleSource),
+ ReplLimits::default(),
+ Some(crate::security::policy::tool_result_artifacts_dir(
+ &config.workspace_dir,
+ )),
+ )
}
pub(crate) fn repl_tools_with(
source: Arc<dyn OriginalSource>,
limits: ReplLimits,
+) -> Vec<Box<dyn Tool>> {
+ repl_tools_with_artifacts(source, limits, None)
+}
+
+pub(crate) fn repl_tools_with_artifacts(
+ source: Arc<dyn OriginalSource>,
+ limits: ReplLimits,
+ artifacts_dir: Option<PathBuf>,
) -> Vec<Box<dyn Tool>> {
// The declarations come from TinyJuice; the probe store is never read.
let probe: Arc<dyn CcrStore> = Arc::new(OneEntryStore {
@@ -118,6 +156,7 @@ pub(crate) fn repl_tools_with(
cap: inner.max_result_size_chars(),
source: Arc::clone(&source),
limits,
+ artifacts_dir: artifacts_dir.clone(),
}) as Box<dyn Tool>
})
.collect()
@@ -136,6 +175,94 @@ fn normalize_handle(raw: &str) -> Option<&str> {
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_…` \
+ is not one. For a `[tool_result_preview]` envelope, pass its `artifact_path` as the \
+ handle, or read that file with file_read."
+}
+
+fn artifact_refused(reason: &str) -> String {
+ format!(
+ "juice: {reason}. Only an `artifact_path` under the workspace's \
+ artifacts/tool-results directory can stand in for a handle; read other files with \
+ file_read."
+ )
+}
+
+/// Read a persisted tool-result artifact named by `raw`, which must resolve
+/// inside `dir` (the absolute pointer a detached store hands out, or the
+/// legacy `artifacts/tool-results/…` relative form). Refuses `..`, a symlink
+/// or anything else that resolves outside `dir`, a non-file, and a file larger
+/// than `max_bytes`. Errors never quote the file's content.
+pub(crate) fn read_tool_result_artifact(
+ dir: &Path,
+ raw: &str,
+ max_bytes: u64,
+) -> Result<String, String> {
+ let raw = raw.trim();
+ let path = Path::new(raw);
+ if path.components().any(|c| matches!(c, Component::ParentDir)) {
+ return Err(artifact_refused("that path contains `..`"));
+ }
+ let resolved = if path.is_absolute() {
+ path.to_path_buf()
+ } else {
+ let forward = raw.replace('\\', "/");
+ match forward
+ .trim_start_matches("./")
+ .strip_prefix(RELATIVE_ARTIFACT_PREFIX)
+ {
+ Some(rest) if !rest.is_empty() => dir.join(rest),
+ _ => return Err(artifact_refused("that path is not a tool-result artifact")),
+ }
+ };
+ if !resolved.starts_with(dir) {
+ return Err(artifact_refused("that path is not a tool-result artifact"));
+ }
+ let not_found = |_| artifact_refused("that artifact no longer exists");
+ let canonical_dir = std::fs::canonicalize(dir).map_err(not_found)?;
+ let canonical = std::fs::canonicalize(&resolved).map_err(not_found)?;
+ if !canonical.starts_with(&canonical_dir) {
+ return Err(artifact_refused(
+ "that path resolves outside the tool-results directory",
+ ));
+ }
+ let meta = std::fs::metadata(&canonical).map_err(not_found)?;
+ if !meta.is_file() {
+ return Err(artifact_refused("that path is not a file"));
+ }
+ if meta.len() > max_bytes {
+ return Err(artifact_refused(&format!(
+ "that artifact is {} bytes, over the {max_bytes}-byte limit",
+ meta.len()
+ )));
+ }
+ let bytes = std::fs::read(&canonical).map_err(not_found)?;
+ Ok(String::from_utf8_lossy(&bytes).into_owned())
+}
+
fn miss_message() -> &'static str {
"juice: that handle is no longer stored (evicted, or from an earlier session). \
Do NOT re-run the same tool call to regenerate it: the result would be stored \
@@ -143,6 +270,56 @@ fn miss_message() -> &'static str {
narrower arguments so the result is small enough to keep in full."
}
+impl ModuleReplTool {
+ /// Run TinyJuice's own tool over `content`, filed under `token`.
+ async fn run_stock(
+ &self,
+ token: String,
+ content: String,
+ mut args: Value,
+ ) -> anyhow::Result<ToolResult> {
+ if let Some(object) = args.as_object_mut() {
+ object.insert("handle".into(), Value::String(token.clone()));
+ }
+ let store: Arc<dyn CcrStore> = Arc::new(OneEntryStore { token, content });
+ let Some(tool) = tinyjuice::repl::tools::repl_tools(store, self.limits)
+ .into_iter()
+ .find(|tool| tool.name() == self.name)
+ else {
+ return Ok(ToolResult::error("juice: unknown repl tool"));
+ };
+ tool.execute(args).await
+ }
+
+ /// The handle slot held a path: query the persisted artifact it names.
+ async fn execute_on_artifact(&self, path: String, args: Value) -> anyhow::Result<ToolResult> {
+ let Some(dir) = self.artifacts_dir.clone() else {
+ log::debug!("[tokenjuice][repl] {} artifact path with no workspace", self.name);
+ return Ok(ToolResult::error(not_a_handle_message()));
+ };
+ let read = tokio::task::spawn_blocking(move || {
+ read_tool_result_artifact(&dir, &path, MAX_ARTIFACT_BYTES)
+ })
+ .await
+ .map_err(|e| anyhow::anyhow!("juice: artifact read task failed: {e}"))?;
+ match read {
+ Ok(content) => {
+ log::debug!(
+ "[tokenjuice][repl] {} artifact bytes={}",
+ self.name,
+ content.len()
+ );
+ self.run_stock(ARTIFACT_TOKEN.to_string(), content, args)
+ .await
+ }
+ Err(message) => {
+ log::debug!("[tokenjuice][repl] {} artifact refused", self.name);
+ Ok(ToolResult::error(message))
+ }
+ }
+ }
+}
+
#[async_trait]
impl Tool for ModuleReplTool {
fn name(&self) -> &str {
@@ -161,10 +338,13 @@ 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 Some(handle) = normalize_handle(raw).map(str::to_string) else {
- return Ok(ToolResult::error(
- "invalid handle: pass the handle from the stored-output footer",
- ));
+ let handle = match classify_handle_arg(raw) {
+ HandleArg::Handle(handle) => handle,
+ HandleArg::Path(path) => return self.execute_on_artifact(path, args).await,
+ HandleArg::Invalid => {
+ log::debug!("[tokenjuice][repl] {} rejected a non-handle", self.name);
+ return Ok(ToolResult::error(not_a_handle_message()));
+ }
};
let content = match self.source.original(&handle).await {
Ok(Some(content)) => content,
@@ -185,17 +365,7 @@ impl Tool for ModuleReplTool {
if let Some(object) = args.as_object_mut() {
object.insert("handle".into(), Value::String(handle.clone()));
}
- let store: Arc<dyn CcrStore> = Arc::new(OneEntryStore {
- token: handle,
- content,
- });
- let Some(tool) = tinyjuice::repl::tools::repl_tools(store, self.limits)
- .into_iter()
- .find(|tool| tool.name() == self.name)
- else {
- return Ok(ToolResult::error("juice: unknown repl tool"));
- };
- tool.execute(args).await
+ self.run_stock(handle, content, args).await
}
fn permission_level(&self) -> PermissionLevel {