Skip to content

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

refactor(subagent-host): extract sub-agent result policy into runner_artifacts

Steven Enamakele9f3972487f9
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/mod.rs b/crates/openhuman-core/src/agent/subagent_host/ops/mod.rs
index c0dcd6c2d4..69223e22d4 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/mod.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/mod.rs
@@ -26,6 +26,7 @@ mod pause_checkpoint;
pub(crate) use pause_checkpoint::is_safe_task_id;
mod prompt;
mod provider;
+mod runner_artifacts;
mod runner;
// The lifecycle adapter owns the public entry point. This is its host-only
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs b/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
index 3b3dd5f5bc..a604cfd2ea 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
@@ -6,12 +6,10 @@
//! calls and tool execution until the model returns without further tool calls
//! (or the iteration budget is exhausted).
-use std::collections::{HashMap, HashSet};
-use std::path::PathBuf;
-use std::sync::{Arc, Mutex};
+use std::collections::HashSet;
+use std::sync::Arc;
use std::time::Instant;
-use crate::agent::harness::artifact_offload::offload_run_result;
use crate::agent::harness::fork_context::ParentExecutionContext;
use crate::agent::prompts::render_subagent_system_prompt_with_format;
use crate::agent::prompts::{PromptContext, SubagentRenderOptions};
@@ -30,10 +28,6 @@ use tinyagents_definition::{
validate_tier_transition, AgentTier, IterationPolicy, PromptSource,
SandboxMode as AgentSandboxMode,
};
-use tinyagents_harness::artifacts::{
- effective_offload_threshold, extract_artifact_paths, note_artifact_handoff,
- DEFAULT_OFFLOAD_THRESHOLD_BYTES, HANDOFF_STAGE_RECORDED,
-};
use tinyagents_harness::context::{current_run_depth, with_run_depth, RunConfig, RunDepth};
use tinytools::{SandboxMode as TinyagentsSandboxMode, ToolSpec, WorkspaceDescriptor};
use tinytools_std::file_state::with_file_state_agent_id;
@@ -369,8 +363,13 @@ pub(crate) async fn run_subagent_direct(
// this host's authorized artifact store. Project its opaque reference
// into the product pointer before applying the independent character
// cap, preserving the existing full-output-first behavior.
- if let Some(error) =
- apply_subagent_result_policies(&mut outcome, definition, &options, &task_id).await
+ if let Some(error) = super::runner_artifacts::apply_subagent_result_policies(
+ &mut outcome,
+ definition,
+ &options,
+ &task_id,
+ )
+ .await
{
tracing::debug!(task_id = %task_id, error = %error, "[artifact] sub-agent result policy fell back to inline output");
}
@@ -391,198 +390,6 @@ pub(crate) async fn run_subagent_direct(
.await
}
-/// Host-owned result adaptation around TinyAgents' generic result policy.
-///
-/// TinyAgents decides whether the full result crosses the product byte
-/// threshold and applies the character cap. This adapter supplies authorized
-/// storage, resolves the opaque artifact reference into the product's existing
-/// pointer/abstract text, and records user-authored or stored paths.
-async fn apply_subagent_result_policies(
- outcome: &mut SubagentRunOutcome,
- definition: &AgentDefinition,
- options: &SubagentRunOptions,
- task_id: &str,
-) -> Option<String> {
- let mut paths = extract_artifact_paths(&outcome.output);
- let mut artifact_error = None;
-
- // A worktree-isolated worker offloads into its own checkout; everyone else
- // uses the live policy's action root, which is the same root a parent's
- // relative read resolves the returned path against.
- let policy = crate::security::live_policy::effective_in(&options.run_context.dispatch_ctx());
- let action_dir = options
- .worktree_action_dir
- .clone()
- .or_else(|| policy.as_ref().map(|p| p.action_dir.clone()));
-
- let can_store = !policy
- .as_ref()
- .is_some_and(|p| p.autonomy == crate::security::AutonomyLevel::ReadOnly);
- let threshold =
- effective_offload_threshold(DEFAULT_OFFLOAD_THRESHOLD_BYTES, definition.max_result_chars);
- let store = match (action_dir.as_ref(), can_store) {
- (Some(action_dir), true) => Some(Arc::new(SubagentArtifactStore::new(
- policy,
- action_dir.clone(),
- outcome.agent_id.clone(),
- threshold,
- ))),
- _ => None,
- };
-
- if let Some(store) = store.as_ref() {
- match offload_result_by_policy(task_id, &outcome.output, threshold, store).await {
- Ok(Some(stored)) => {
- outcome.output = stored.pointer;
- if !paths.contains(&stored.relative_path) {
- paths.push(stored.relative_path);
- }
- }
- Ok(None) => {}
- Err(error) => artifact_error = Some(error),
- }
- } else if action_dir.is_none() {
- tracing::debug!(
- task_id = %task_id,
- agent_id = %outcome.agent_id,
- worker_authored_paths = paths.len(),
- "[artifact] no resolvable action_dir — skipping offload (summarizer/truncation backstop applies)"
- );
- } else {
- tracing::debug!(
- task_id = %task_id,
- agent_id = %outcome.agent_id,
- "[artifact] readonly autonomy tier — skipping offload (summarizer/truncation backstop applies)"
- );
- }
-
- if let Some(cap) = definition.max_result_chars {
- outcome.output = tinyagents_orchestration::subagent::ResultPolicy::new()
- .with_max_chars(cap)
- .apply(task_id, &outcome.output, None)
- .await
- .text;
- }
-
- for path in extract_artifact_paths(&outcome.output) {
- if !paths.contains(&path) {
- paths.push(path);
- }
- }
- outcome.artifact_paths = paths;
- note_artifact_handoff(
- HANDOFF_STAGE_RECORDED,
- &outcome.agent_id,
- task_id,
- &outcome.artifact_paths,
- );
- artifact_error
-}
-
-async fn offload_result_by_policy(
- task_id: &str,
- output: &str,
- threshold_bytes: usize,
- store: &Arc<SubagentArtifactStore>,
-) -> Result<Option<StoredSubagentArtifact>, String> {
- let applied = tinyagents_orchestration::subagent::ResultPolicy::new()
- .with_max_bytes(threshold_bytes)
- .with_overflow(tinyagents_orchestration::subagent::ResultOverflow::Artifact)
- .with_artifact_store(store.clone())
- .apply(task_id, output, None)
- .await;
- if let Some(error) = applied.artifact_error {
- return Err(error);
- }
- let Some(reference) = applied.artifact else {
- return Ok(None);
- };
- let stored = store.take(&reference.id);
- stored.map(Some).ok_or_else(|| {
- tracing::warn!(
- task_id = %task_id,
- "[artifact] TinyAgents returned an unknown sub-agent artifact reference"
- );
- "TinyAgents returned an unknown OpenHuman artifact reference".to_owned()
- })
-}
-
-struct SubagentArtifactStore {
- policy: Option<Arc<crate::security::SecurityPolicy>>,
- action_dir: PathBuf,
- agent_id: String,
- threshold_bytes: usize,
- artifacts: Mutex<HashMap<String, StoredSubagentArtifact>>,
-}
-
-struct StoredSubagentArtifact {
- pointer: String,
- relative_path: String,
-}
-
-impl SubagentArtifactStore {
- fn new(
- policy: Option<Arc<crate::security::SecurityPolicy>>,
- action_dir: PathBuf,
- agent_id: String,
- threshold_bytes: usize,
- ) -> Self {
- Self {
- policy,
- action_dir,
- agent_id,
- threshold_bytes,
- artifacts: Mutex::new(HashMap::new()),
- }
- }
-
- fn take(&self, id: &str) -> Option<StoredSubagentArtifact> {
- self.artifacts
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
- .remove(id)
- }
-}
-
-#[async_trait::async_trait]
-impl tinyagents_orchestration::subagent::ArtifactStore for SubagentArtifactStore {
- async fn store(
- &self,
- task_id: &str,
- content: &str,
- ) -> Result<tinyagents_orchestration::subagent::ArtifactReference, String> {
- let (pointer, artifact) = offload_run_result(
- content.to_owned(),
- self.policy.clone(),
- self.action_dir.clone(),
- self.agent_id.clone(),
- task_id,
- self.threshold_bytes,
- )
- .await;
- let artifact = artifact.ok_or_else(|| "OpenHuman did not persist the result".to_owned())?;
- let id = uuid::Uuid::new_v4().to_string();
- self.artifacts
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
- .insert(
- id.clone(),
- StoredSubagentArtifact {
- pointer,
- relative_path: artifact.relative_path,
- },
- );
- Ok(tinyagents_orchestration::subagent::ArtifactReference {
- id,
- media_type: Some("text/plain".into()),
- metadata: Default::default(),
- })
- }
-}
-
-#[cfg(test)]
-#[path = "runner_tests.rs"]
-mod tests;
fn workspace_descriptor_for_subagent(
definition: &AgentDefinition,
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/runner_artifacts.rs b/crates/openhuman-core/src/agent/subagent_host/ops/runner_artifacts.rs
new file mode 100644
index 0000000000..a0a169b49e
--- /dev/null
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/runner_artifacts.rs
@@ -0,0 +1,208 @@
+//! OpenHuman's authorized artifact adapter for sub-agent results.
+//!
+//! TinyAgents owns the byte-overflow decision and opaque artifact reference.
+//! This module supplies OpenHuman's scoped storage, security/redaction policy,
+//! parent-readable materialization, path projection and product character cap.
+
+use std::collections::HashMap;
+use std::path::PathBuf;
+use std::sync::{Arc, Mutex};
+
+use crate::agent::harness::artifact_offload::offload_run_result;
+use crate::agent::subagent_host::types::{SubagentRunOptions, SubagentRunOutcome};
+use crate::agent_registry::host_definition::AgentDefinition;
+use tinyagents_harness::artifacts::{
+ effective_offload_threshold, extract_artifact_paths, note_artifact_handoff,
+ DEFAULT_OFFLOAD_THRESHOLD_BYTES, HANDOFF_STAGE_RECORDED,
+};
+
+/// Apply the shared result policy with OpenHuman's authorized artifact store.
+///
+/// Returns a soft persistence error, if any; callers retain the inline result
+/// and its product cap regardless of artifact-store failures.
+pub(super) async fn apply_subagent_result_policies(
+ outcome: &mut SubagentRunOutcome,
+ definition: &AgentDefinition,
+ options: &SubagentRunOptions,
+ task_id: &str,
+) -> Option<String> {
+ let mut paths = extract_artifact_paths(&outcome.output);
+ let mut artifact_error = None;
+
+ // A worktree-isolated worker offloads into its own checkout; everyone else
+ // uses the live policy's action root, which is the same root a parent's
+ // relative read resolves the returned path against.
+ let policy = crate::security::live_policy::effective_in(&options.run_context.dispatch_ctx());
+ let action_dir = options
+ .worktree_action_dir
+ .clone()
+ .or_else(|| policy.as_ref().map(|p| p.action_dir.clone()));
+
+ let can_store = !policy
+ .as_ref()
+ .is_some_and(|p| p.autonomy == crate::security::AutonomyLevel::ReadOnly);
+ let threshold =
+ effective_offload_threshold(DEFAULT_OFFLOAD_THRESHOLD_BYTES, definition.max_result_chars);
+ let store = match (action_dir.as_ref(), can_store) {
+ (Some(action_dir), true) => Some(Arc::new(SubagentArtifactStore::new(
+ policy,
+ action_dir.clone(),
+ outcome.agent_id.clone(),
+ threshold,
+ ))),
+ _ => None,
+ };
+
+ if let Some(store) = store.as_ref() {
+ match offload_result_by_policy(task_id, &outcome.output, threshold, store).await {
+ Ok(Some(stored)) => {
+ outcome.output = stored.pointer;
+ if !paths.contains(&stored.relative_path) {
+ paths.push(stored.relative_path);
+ }
+ }
+ Ok(None) => {}
+ Err(error) => artifact_error = Some(error),
+ }
+ } else if action_dir.is_none() {
+ tracing::debug!(
+ task_id = %task_id,
+ agent_id = %outcome.agent_id,
+ worker_authored_paths = paths.len(),
+ "[artifact] no resolvable action_dir — skipping offload (summarizer/truncation backstop applies)"
+ );
+ } else {
+ tracing::debug!(
+ task_id = %task_id,
+ agent_id = %outcome.agent_id,
+ "[artifact] readonly autonomy tier — skipping offload (summarizer/truncation backstop applies)"
+ );
+ }
+
+ if let Some(cap) = definition.max_result_chars {
+ outcome.output = tinyagents_orchestration::subagent::ResultPolicy::new()
+ .with_max_chars(cap)
+ .apply(task_id, &outcome.output, None)
+ .await
+ .text;
+ }
+
+ for path in extract_artifact_paths(&outcome.output) {
+ if !paths.contains(&path) {
+ paths.push(path);
+ }
+ }
+ outcome.artifact_paths = paths;
+ note_artifact_handoff(
+ HANDOFF_STAGE_RECORDED,
+ &outcome.agent_id,
+ task_id,
+ &outcome.artifact_paths,
+ );
+ artifact_error
+}
+
+async fn offload_result_by_policy(
+ task_id: &str,
+ output: &str,
+ threshold_bytes: usize,
+ store: &Arc<SubagentArtifactStore>,
+) -> Result<Option<StoredSubagentArtifact>, String> {
+ let applied = tinyagents_orchestration::subagent::ResultPolicy::new()
+ .with_max_bytes(threshold_bytes)
+ .with_overflow(tinyagents_orchestration::subagent::ResultOverflow::Artifact)
+ .with_artifact_store(store.clone())
+ .apply(task_id, output, None)
+ .await;
+ if let Some(error) = applied.artifact_error {
+ return Err(error);
+ }
+ let Some(reference) = applied.artifact else {
+ return Ok(None);
+ };
+ let stored = store.take(&reference.id);
+ stored.map(Some).ok_or_else(|| {
+ tracing::warn!(
+ task_id = %task_id,
+ "[artifact] TinyAgents returned an unknown sub-agent artifact reference"
+ );
+ "TinyAgents returned an unknown OpenHuman artifact reference".to_owned()
+ })
+}
+
+struct SubagentArtifactStore {
+ policy: Option<Arc<crate::security::SecurityPolicy>>,
+ action_dir: PathBuf,
+ agent_id: String,
+ threshold_bytes: usize,
+ artifacts: Mutex<HashMap<String, StoredSubagentArtifact>>,
+}
+
+struct StoredSubagentArtifact {
+ pointer: String,
+ relative_path: String,
+}
+
+impl SubagentArtifactStore {
+ fn new(
+ policy: Option<Arc<crate::security::SecurityPolicy>>,
+ action_dir: PathBuf,
+ agent_id: String,
+ threshold_bytes: usize,
+ ) -> Self {
+ Self {
+ policy,
+ action_dir,
+ agent_id,
+ threshold_bytes,
+ artifacts: Mutex::new(HashMap::new()),
+ }
+ }
+
+ fn take(&self, id: &str) -> Option<StoredSubagentArtifact> {
+ self.artifacts
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .remove(id)
+ }
+}
+
+#[async_trait::async_trait]
+impl tinyagents_orchestration::subagent::ArtifactStore for SubagentArtifactStore {
+ async fn store(
+ &self,
+ task_id: &str,
+ content: &str,
+ ) -> Result<tinyagents_orchestration::subagent::ArtifactReference, String> {
+ let (pointer, artifact) = offload_run_result(
+ content.to_owned(),
+ self.policy.clone(),
+ self.action_dir.clone(),
+ self.agent_id.clone(),
+ task_id,
+ self.threshold_bytes,
+ )
+ .await;
+ let artifact = artifact.ok_or_else(|| "OpenHuman did not persist the result".to_owned())?;
+ let id = uuid::Uuid::new_v4().to_string();
+ self.artifacts
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .insert(
+ id.clone(),
+ StoredSubagentArtifact {
+ pointer,
+ relative_path: artifact.relative_path,
+ },
+ );
+ Ok(tinyagents_orchestration::subagent::ArtifactReference {
+ id,
+ media_type: Some("text/plain".into()),
+ metadata: Default::default(),
+ })
+ }
+}
+
+#[cfg(test)]
+#[path = "runner_artifacts_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/runner_tests.rs b/crates/openhuman-core/src/agent/subagent_host/ops/runner_artifacts_tests.rs
similarity index 95%
rename from crates/openhuman-core/src/agent/subagent_host/ops/runner_tests.rs
rename to crates/openhuman-core/src/agent/subagent_host/ops/runner_artifacts_tests.rs
index 5c6419d5aa..0f479b0986 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/runner_tests.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/runner_artifacts_tests.rs
@@ -66,8 +66,13 @@ async fn byte_policy_projects_a_parent_readable_artifact_before_the_result_cap()
assert!(options.run_context.dispatch_ctx().tenant().is_ok());
assert!(crate::storage::sessions::artifact_blobs_for(&workspace_dir).is_some());
assert_eq!(
- apply_subagent_result_policies(&mut outcome, &definition, &options, "task-policy-test")
- .await,
+ apply_subagent_result_policies(
+ &mut outcome,
+ &definition,
+ &options,
+ "task-policy-test"
+ )
+ .await,
None,
"artifact storage should succeed"
);