Skip to content

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

fix(agent): keep result handoff from replacing artifact reads (#6284)

M3gA-Mindd0bd92f61263
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/turn_context.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/turn_context.rs
index 4a1d38068f..895bb84a8b 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/turn_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/turn_context.rs
@@ -323,11 +323,7 @@ impl TurnContextMiddleware {
// budget can shrink it below the 50k-token handoff threshold and defeat the
// drill-in.
if let Some(handoff) = self.handoff {
- harness.push_middleware(Arc::new(HandoffMiddleware {
- cache: handoff.cache,
- agent_id: handoff.agent_id,
- task_id: handoff.task_id,
- }));
+ harness.push_middleware(Arc::new(HandoffMiddleware::new(handoff)));
}
}
}
@@ -339,10 +335,29 @@ impl TurnContextMiddleware {
/// `SubagentToolSource` ran on every tool result (via `apply_handoff`), which the
/// agent_graph rewrite dropped. Errors and `extract_from_result`'s own output
/// pass through unchanged (handled inside `apply_handoff`).
+///
+/// A read of a persisted tool-result artifact also passes through: this hook
+/// runs before `ToolOutputMiddleware`'s, so stashing the read here would hand
+/// the artifact pager a short `extract_from_result` pointer instead of the
+/// bytes the model asked for (#6284).
pub(crate) struct HandoffMiddleware {
cache: Arc<crate::agent::harness::subagent_runner::ResultHandoffCache>,
agent_id: String,
task_id: String,
+ /// Call ids of artifact reads, recorded in `before_tool` (where the
+ /// arguments are visible) and consumed in `after_tool`.
+ artifact_reads: std::sync::Mutex<std::collections::HashSet<String>>,
+}
+
+impl HandoffMiddleware {
+ pub(crate) fn new(config: HandoffConfig) -> Self {
+ Self {
+ cache: config.cache,
+ agent_id: config.agent_id,
+ task_id: config.task_id,
+ artifact_reads: Default::default(),
+ }
+ }
}
#[async_trait]
@@ -351,12 +366,45 @@ impl Middleware<()> for HandoffMiddleware {
"result_handoff"
}
+ async fn before_tool(
+ &self,
+ _ctx: &mut RunContext<()>,
+ _state: &(),
+ call: &mut tinyinference::tool::ToolCall,
+ ) -> TaResult<()> {
+ if crate::agent::harness::tool_result_artifacts::artifact_read_target(
+ &call.name,
+ &call.arguments,
+ )
+ .is_some()
+ {
+ if let Ok(mut reads) = self.artifact_reads.lock() {
+ reads.insert(call.id.clone());
+ }
+ }
+ Ok(())
+ }
+
async fn after_tool(
&self,
_ctx: &mut RunContext<()>,
_state: &(),
result: &mut TaToolResult,
) -> TaResult<()> {
+ let artifact_read = self
+ .artifact_reads
+ .lock()
+ .map(|mut reads| reads.remove(&result.call_id))
+ .unwrap_or(false);
+ if artifact_read {
+ tracing::debug!(
+ tool = %result.name,
+ call_id = %result.call_id,
+ task_id = %self.task_id,
+ "[tinyagents::mw] artifact read: skipping result handoff so the artifact pager sees the bytes"
+ );
+ return Ok(());
+ }
result.content = crate::agent::harness::subagent_runner::apply_handoff(
&self.cache,
&result.name,
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_artifact_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_artifact_tests.rs
index d906ce9765..0731ebe369 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_artifact_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_artifact_tests.rs
@@ -128,3 +128,40 @@ async fn a_raw_result_file_read_cannot_open_is_stored_as_the_processed_copy() {
"a raw body over file_read's limit would be unreadable, so the processed copy is stored"
);
}
+
+/// #6284 review: `HandoffMiddleware` runs before the artifact pager, so it must
+/// leave an artifact read alone or the pager only ever sees a handoff pointer.
+#[tokio::test]
+async fn handoff_leaves_an_artifact_read_for_the_pager_but_still_hands_off_other_results() {
+ use crate::agent::tinyagents::middleware::turn_context::{HandoffConfig, HandoffMiddleware};
+ let mw = HandoffMiddleware::new(HandoffConfig {
+ cache: Arc::new(crate::agent::harness::subagent_runner::ResultHandoffCache::new()),
+ agent_id: "agent-1".to_string(),
+ task_id: "task-1".to_string(),
+ });
+ // ~100k estimated tokens, above the 50k handoff threshold.
+ let oversized = "y".repeat(400_000);
+
+ let mut ordinary = tool_result("echo", &oversized);
+ mw.after_tool(&mut ctx(), &(), &mut ordinary).await.unwrap();
+ assert_ne!(
+ ordinary.content.len(),
+ oversized.len(),
+ "control: an oversized ordinary result is still handed off"
+ );
+
+ let mut call = TaToolCall::new(
+ "c1",
+ "file_read",
+ json!({"path": "artifacts/tool-results/s/shell/c.txt"}),
+ );
+ let mut ctx = ctx();
+ mw.before_tool(&mut ctx, &(), &mut call).await.unwrap();
+ let mut read = tool_result("file_read", &oversized);
+ mw.after_tool(&mut ctx, &(), &mut read).await.unwrap();
+ assert_eq!(
+ read.content.len(),
+ oversized.len(),
+ "an artifact read must reach the artifact pager with its bytes, not a handoff pointer"
+ );
+}