Skip to content

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

feat(session_host): wire artifact store into session host

Steven Enamakel166ebf00ad1c
diff --git a/crates/openhuman-core/src/agent/session_host/artifact_wiring.rs b/crates/openhuman-core/src/agent/session_host/artifact_wiring.rs
index ebb6142dd4..f00a25e401 100644
--- a/crates/openhuman-core/src/agent/session_host/artifact_wiring.rs
+++ b/crates/openhuman-core/src/agent/session_host/artifact_wiring.rs
@@ -1,16 +1,27 @@
//! Where the session host's tool-result artifact store comes from (#6408).
//!
//! Extracted from `runtime_session.rs` rather than inlined there: the choice of
-//! root is the whole correctness question for this feature, and it needs more
+//! storage is the whole correctness question for this feature, and it needs more
//! prose than a call site should carry.
+//!
+//! The bodies live on the acting tenant's scoped blob store (the same wiring the
+//! sub-agent artifact offload uses, [`artifact_blobs_for`]), so they are
+//! tenant-scoped and survive a restart. `file_read` opens the filesystem, so
+//! [`HostArtifactStore`] materialises a body into
+//! `<workspace>/artifacts/tool-results` on read; that directory is a cache the
+//! sweep below keeps bounded.
use std::path::{Path, PathBuf};
+use std::sync::Arc;
-use crate::agent::harness::tool_result_artifacts::new_tool_result_store;
-use tinyagents_harness::artifacts::tool_results::ToolResultArtifactStore;
+use crate::agent::harness::tool_result_artifacts::{new_tool_result_store, HostArtifactStore};
+use crate::agent::session_store::artifact_blobs_for;
+use crate::security::policy::tool_result_artifacts_dir;
+use tinystoragedrivers::{MemoryStorage, Scope, StorageBackend};
-/// How long another session's tool-result artifacts survive before a later
-/// session sweeps them.
+/// How long a tool-result artifact survives: the blob lifetime handed to
+/// drivers with blob TTL, and the age past which another session's
+/// materialised copies are swept.
///
/// **A conservative default chosen for safety, not a retention policy.** The
/// artifact store had no bound at all — nothing anywhere deletes these files —
@@ -30,59 +41,78 @@ use tinyagents_harness::artifacts::tool_results::ToolResultArtifactStore;
/// available for debugging a session after the fact.
const ARTIFACT_RETENTION: std::time::Duration = std::time::Duration::from_secs(24 * 60 * 60);
-/// Where artifacts are written: `<workspace_dir>/artifacts/tool-results`, the
-/// core's own state, never the agent's working directory.
-///
-/// The store used to be rooted at `action_dir` (or the per-turn workspace
-/// descriptor's root) and hand the model a path relative to it. That kept the
-/// pointer readable, but the action directory is very often a project the
-/// agent is editing: every oversized tool output became a stray
-/// `artifacts/tool-results/…` file in it, swept into `git add -A` and shipped
-/// in the diff. In a coding benchmark one task's whole patch was twelve of
-/// these files and a scratch test.
-///
-/// The store is now detached. It writes under the workspace and hands out the
-/// **absolute** path, which resolves the same way whatever the turn's action
-/// directory or workspace descriptor is, so the read path no longer has to be
-/// matched against the write path (the #6483 trap: `FsGate::scoped_to_workspace`
-/// moving relative resolution to the descriptor root). Reading it back is
-/// policy: `SecurityPolicy::from_config` grants this directory as a read-only
-/// trusted root, and `is_workspace_internal_path` already exempts it from the
-/// internal-state boundary.
-fn artifact_root(workspace_dir: &Path) -> PathBuf {
- // `ToolResultArtifactStore::detached` adds its own `tool-results` child
- // namespace so pruning stays within the store-owned directory. Pass the
- // workspace's `artifacts` directory here; the resulting store root is the
- // policy-granted `<workspace>/artifacts/tool-results` directory.
- workspace_dir.join("artifacts")
-}
-
/// Build the store the turn path hands to `TurnContextMiddleware`, sweeping
-/// other sessions' stale artifacts on the way.
+/// other sessions' stale materialised copies on the way.
+///
+/// With no blob store for the tenant (a SaaS task with no scope, a refused
+/// backend) the artifacts live in a private in-memory store for this process,
+/// so a large result is still stored and readable within the run.
///
/// The sweep is best-effort by design: a failed prune must never fail a turn,
/// because the worst it costs is disk, while failing the turn costs the user
/// their message.
-pub(super) fn build_artifact_store(
- workspace_dir: &Path,
- session_key: &str,
-) -> ToolResultArtifactStore {
- let store = new_tool_result_store(artifact_root(workspace_dir), session_key);
- prune(&store, "workspace");
- store
+pub(super) fn build_artifact_store(workspace_dir: &Path, session_key: &str) -> HostArtifactStore {
+ let blobs = artifact_blobs_for(workspace_dir).unwrap_or_else(|| {
+ tracing::warn!(
+ "[agent][tool-result-artifacts] no blob store for the acting tenant — artifacts stay in memory for this process"
+ );
+ let scope = Scope::new("local").expect("static scope");
+ Arc::clone(
+ MemoryStorage::new()
+ .for_scope(&scope)
+ .expect("memory scope")
+ .blobs(),
+ )
+ });
+ let root = tool_result_artifacts_dir(workspace_dir);
+ prune_materialized(&root, ARTIFACT_RETENTION);
+ new_tool_result_store(blobs, root, session_key).with_retention(ARTIFACT_RETENTION)
}
-fn prune(store: &ToolResultArtifactStore, which: &str) {
- match store.prune_stale_sessions(ARTIFACT_RETENTION) {
- Ok(0) => {}
- Ok(removed) => log::debug!(
- "[agent][tool-result-artifacts] pruned {removed} stale {which} artifact session dir(s) under {}",
- store.root().display()
- ),
- Err(error) => log::warn!(
- "[agent][tool-result-artifacts] {which} artifact prune failed (continuing): {error}"
- ),
+/// The newest modification time anywhere under `path`, itself included.
+fn newest_modified(path: &Path) -> Option<std::time::SystemTime> {
+ let mut newest = std::fs::symlink_metadata(path).ok()?.modified().ok()?;
+ if path.is_dir() {
+ for entry in std::fs::read_dir(path).ok()?.flatten() {
+ if let Some(modified) = newest_modified(&entry.path()) {
+ newest = newest.max(modified);
+ }
+ }
+ }
+ Some(newest)
+}
+
+/// Remove materialised session directories under `root` untouched for `ttl`.
+/// Returns how many went. The blobs themselves are the store's lifecycle.
+fn prune_materialized(root: &Path, ttl: std::time::Duration) -> usize {
+ let Ok(entries) = std::fs::read_dir(root) else {
+ return 0;
+ };
+ let Some(cutoff) = std::time::SystemTime::now().checked_sub(ttl) else {
+ return 0;
+ };
+ let mut removed = 0;
+ for entry in entries.flatten() {
+ let path = entry.path();
+ if !path.is_dir() || path.is_symlink() {
+ continue;
+ }
+ if newest_modified(&path).is_some_and(|newest| newest < cutoff) {
+ match std::fs::remove_dir_all(&path) {
+ Ok(()) => removed += 1,
+ Err(error) => tracing::warn!(
+ "[agent][tool-result-artifacts] materialised prune failed (continuing): {error}"
+ ),
+ }
+ }
+ }
+ if removed > 0 {
+ tracing::debug!(
+ "[agent][tool-result-artifacts] pruned {removed} stale materialised session dir(s) under {}",
+ root.display()
+ );
}
+ removed
}
#[cfg(test)]