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 result offload policy into helper

Steven Enamakelfb3a447c9397
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 071340d689..2f33327a30 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
@@ -425,23 +425,10 @@ async fn apply_subagent_result_policies(
if let Some(store) = store.as_ref() {
let threshold =
effective_offload_threshold(DEFAULT_OFFLOAD_THRESHOLD_BYTES, definition.max_result_chars);
- let applied = tinyagents_orchestration::subagent::ResultPolicy::new()
- .with_max_bytes(threshold)
- .with_overflow(tinyagents_orchestration::subagent::ResultOverflow::Artifact)
- .with_artifact_store(store.clone())
- .apply(task_id, &outcome.output, None)
- .await;
- if let Some(reference) = applied.artifact {
- if let Some(stored) = store.take(&reference.id) {
- outcome.output = stored.pointer;
- if !paths.contains(&stored.relative_path) {
- paths.push(stored.relative_path);
- }
- } else {
- tracing::warn!(
- task_id = %task_id,
- "[artifact] TinyAgents returned an unknown sub-agent artifact reference"
- );
+ if let Some(stored) = offload_result_by_policy(task_id, &outcome.output, threshold, store).await {
+ outcome.output = stored.pointer;
+ if !paths.contains(&stored.relative_path) {
+ paths.push(stored.relative_path);
}
}
} else if action_dir.is_none() {
@@ -481,6 +468,31 @@ async fn apply_subagent_result_policies(
);
}
+async fn offload_result_by_policy(
+ task_id: &str,
+ output: &str,
+ threshold_bytes: usize,
+ store: &Arc<SubagentArtifactStore>,
+) -> Option<StoredSubagentArtifact> {
+ 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;
+ let Some(reference) = applied.artifact else {
+ return None;
+ };
+ let stored = store.take(&reference.id);
+ if stored.is_none() {
+ tracing::warn!(
+ task_id = %task_id,
+ "[artifact] TinyAgents returned an unknown sub-agent artifact reference"
+ );
+ }
+ stored
+}
+
struct SubagentArtifactStore {
policy: Option<Arc<crate::security::SecurityPolicy>>,
action_dir: PathBuf,
@@ -551,6 +563,10 @@ impl tinyagents_orchestration::subagent::ArtifactStore for SubagentArtifactStore
}
}
+#[cfg(test)]
+#[path = "runner_tests.rs"]
+mod tests;
+
fn workspace_descriptor_for_subagent(
definition: &AgentDefinition,
options: &SubagentRunOptions,
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/runner_tests.rs b/crates/openhuman-core/src/agent/subagent_host/ops/runner_tests.rs
new file mode 100644
index 0000000000..49d5160dff
--- /dev/null
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/runner_tests.rs
@@ -0,0 +1,54 @@
+use super::*;
+
+#[tokio::test]
+async fn byte_policy_offloads_full_output_before_applying_the_character_cap() {
+ use crate::core::runtime::agent_scope::test_agent_context;
+ use crate::core::runtime::{CoreContext, DomainSet};
+ use crate::security::{SecurityPolicy, TrustedAccess, TrustedRoot};
+
+ let scratch = tempfile::tempdir().expect("artifact workspace");
+ let action_dir = scratch.path().join("action");
+ let workspace_dir = scratch.path().join("workspace");
+ std::fs::create_dir_all(&action_dir).expect("action directory");
+ std::fs::create_dir_all(&workspace_dir).expect("workspace directory");
+ let trusted_action_dir = action_dir
+ .canonicalize()
+ .expect("canonical action directory");
+ let policy = Arc::new(SecurityPolicy {
+ trusted_roots: vec![TrustedRoot {
+ path: trusted_action_dir.to_string_lossy().into_owned(),
+ access: TrustedAccess::ReadWrite,
+ }],
+ action_dir: action_dir.clone(),
+ workspace_dir,
+ ..SecurityPolicy::default()
+ });
+ let root = CoreContext::for_test(DomainSet::full(), None);
+ let scoped = test_agent_context(&root, "subagent-artifact-policy-test");
+ let output = "full sub-agent result that must survive the visible preview\n".repeat(16);
+ let store = Arc::new(SubagentArtifactStore::new(
+ Some(policy),
+ action_dir.clone(),
+ "worker".into(),
+ ));
+
+ let stored = CoreContext::scope(scoped, async {
+ offload_result_by_policy("task-policy-test", &output, 8, &store)
+ .await
+ .expect("TinyAgents byte policy invokes the host artifact store")
+ })
+ .await;
+
+ assert!(stored.relative_path.starts_with("outputs/"));
+ assert!(stored.pointer.contains(&stored.relative_path));
+ assert_ne!(stored.pointer, output);
+ let materialized = std::fs::read_to_string(action_dir.join(&stored.relative_path))
+ .expect("parent-readable artifact was materialized under its action root");
+ assert_eq!(materialized, output, "the full original output is retained");
+
+ let capped = tinyagents_orchestration::subagent::ResultPolicy::new()
+ .with_max_chars(512)
+ .apply("task-policy-test", &stored.pointer, None)
+ .await;
+ assert_eq!(capped.text, stored.pointer, "the existing pointer fits its cap");
+}