Skip to content

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

refactor(agent): extract tool result artifact helpers into a module

Steven Enamakel938b519b34bd
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 6dcba7b193..4c54109863 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
@@ -12,12 +12,24 @@
//! one wrapper whose result *is* the wrapped tool's result;
//! * **the read limit**: the largest body `file_read` will open
//! ([`FileReadTool::MAX_FILE_SIZE_BYTES`]);
-//! * **where the files go**: `<workspace_dir>/artifacts/tool-results`
-//! ([`crate::security::policy::tool_result_artifacts_dir`]), a detached store
-//! outside the action directory, so a tool output is never written into the
-//! project the agent is working in.
+//! * **where the bodies live**: on the acting tenant's scoped blob store
+//! ([`crate::agent::session_store::artifact_blobs_for`]), under the
+//! `tool-results/<session>/<tool>/<call>.txt` key the pointer quotes. Nothing
+//! is written into the project the agent is working in;
+//! * **how `file_read` opens one**: `file_read` reads the filesystem, so a read
+//! of a `tool-results/…` key is first *materialised* from the blob store into
+//! `<workspace_dir>/artifacts/tool-results`
+//! ([`crate::security::policy::tool_result_artifacts_dir`], a read-only trusted
+//! root) and the call is pointed at that file ([`HostArtifactStore::resolve_read`]).
+//! The blob lookup runs on every read, so a key another tenant wrote is absent
+//! from this tenant's store and is never served, and a session that reloads
+//! finds its artifacts again because the blobs outlive the process.
+
+use std::path::{Path, PathBuf};
+use std::sync::Arc;
use serde_json::Value;
+use tinystoragedrivers::BlobStore;
use tinyagents_harness::artifacts::tool_results::{self, ArtifactRead, ToolResultArtifactStore};
use crate::agent::harness::artifact_offload::{SanitizingRedactor, READ_TOOL};
@@ -26,28 +38,121 @@ use tinytools_std::filesystem::FileReadTool;
/// Namespace of the artifact index in the run's store registry.
pub(crate) const TINYAGENTS_TOOL_RESULT_ARTIFACT_STORE: &str = "openhuman_tool_result_artifacts";
-/// A detached store for one session, keeping its files in `storage_dir` (see
-/// [`crate::security::policy::tool_result_artifacts_dir`]) and wired to
-/// OpenHuman's redactor, read tool and read limit.
+/// A session's artifact store plus the directory its bodies are materialised
+/// into for `file_read`. Derefs to the harness [`ToolResultArtifactStore`].
+#[derive(Clone, Debug)]
+pub(crate) struct HostArtifactStore {
+ store: ToolResultArtifactStore,
+ materialize_root: PathBuf,
+}
+
+impl std::ops::Deref for HostArtifactStore {
+ type Target = ToolResultArtifactStore;
+ fn deref(&self) -> &Self::Target {
+ &self.store
+ }
+}
+
+/// A store for one session over `blobs`, wired to OpenHuman's redactor, read
+/// tool and read limit. `materialize_root` is where `file_read` opens bodies
+/// (see [`crate::security::policy::tool_result_artifacts_dir`]).
pub(crate) fn new_tool_result_store(
- storage_dir: std::path::PathBuf,
+ blobs: Arc<dyn BlobStore>,
+ materialize_root: PathBuf,
session_key: impl Into<String>,
-) -> ToolResultArtifactStore {
- ToolResultArtifactStore::detached(
- storage_dir,
- session_key,
- std::sync::Arc::new(SanitizingRedactor),
- READ_TOOL,
- FileReadTool::MAX_FILE_SIZE_BYTES,
- )
+) -> HostArtifactStore {
+ HostArtifactStore {
+ store: ToolResultArtifactStore::new(
+ blobs,
+ session_key,
+ Arc::new(SanitizingRedactor),
+ READ_TOOL,
+ FileReadTool::MAX_FILE_SIZE_BYTES,
+ ),
+ materialize_root,
+ }
+}
+
+impl HostArtifactStore {
+ /// Apply a retention to every artifact written from here on.
+ pub(crate) fn with_retention(mut self, retention: std::time::Duration) -> Self {
+ self.store = self.store.with_retention(retention);
+ self
+ }
+
+ /// The directory bodies are materialised into.
+ pub(crate) fn materialize_root(&self) -> &Path {
+ &self.materialize_root
+ }
+
+ /// Write the stored body of `key` under the materialise root and return its
+ /// path, or `None` when this tenant's store has no such artifact. The key
+ /// was validated fail-closed by [`ToolResultArtifactStore::read`], so the
+ /// join cannot leave the root.
+ pub(crate) async fn materialize(&self, key: &str) -> anyhow::Result<Option<PathBuf>> {
+ let Some(body) = self.store.read(key).await? else {
+ return Ok(None);
+ };
+ let relative = relative_to_namespace(key);
+ let target = self.materialize_root.join(relative);
+ if let Some(parent) = target.parent() {
+ tokio::fs::create_dir_all(parent).await?;
+ }
+ tokio::fs::write(&target, body).await?;
+ Ok(Some(target))
+ }
+
+ /// Point a `file_read` of an artifact key at its materialised file, in
+ /// place. Leaves the arguments untouched when the artifact is absent
+ /// (never written, expired, purged or another tenant's).
+ pub(crate) async fn resolve_read(&self, args: &mut Value, read: &ArtifactRead) {
+ match self.materialize(&read.path).await {
+ Ok(Some(target)) => {
+ tracing::debug!(
+ key = %read.path,
+ "[tool-result-artifacts] materialised blob for file_read"
+ );
+ set_read_path(args, &target.to_string_lossy());
+ }
+ Ok(None) => tracing::debug!(
+ key = %read.path,
+ "[tool-result-artifacts] no such artifact in this tenant's store"
+ ),
+ Err(error) => tracing::warn!(
+ key = %read.path,
+ error = %error,
+ "[tool-result-artifacts] could not materialise artifact (continuing)"
+ ),
+ }
+ }
+}
+
+/// The part of a key below the `tool-results` namespace (either form).
+fn relative_to_namespace(key: &str) -> &str {
+ let key = key.trim_start_matches("./");
+ key.strip_prefix("artifacts/tool-results/")
+ .or_else(|| key.strip_prefix("tool-results/"))
+ .unwrap_or(key)
+}
+
+/// Set the `path` of the read call in `args`, following `use_skill`'s
+/// `{"tool", "args"}` wrapper to the call it runs.
+fn set_read_path(args: &mut Value, path: &str) {
+ let mut cursor = args;
+ while cursor.get("tool").is_some() && cursor.get("args").is_some_and(Value::is_object) {
+ cursor = &mut cursor["args"];
+ }
+ if let Some(object) = cursor.as_object_mut() {
+ object.insert("path".to_string(), Value::String(path.to_string()));
+ }
}
/// The artifact a tool call reads, if any — `file_read`, possibly wrapped in
/// `use_skill` (#6284).
///
-/// With a `store`, its own pointers are recognised (a detached store's are
-/// absolute); without one, only the relative `artifacts/tool-results/…` form a
-/// store-less turn can still meet in an older transcript.
+/// With a `store`, its own `tool-results/…` keys are recognised; without one,
+/// only the legacy relative `artifacts/tool-results/…` form a store-less turn
+/// can still meet in an older transcript.
pub(crate) fn artifact_read_target(
store: Option<&ToolResultArtifactStore>,
tool_name: &str,