Skip to content

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

fix(agent): state the summarized size after the output caps, keep the tail of long task hints (#6283)

M3gA-Mind871a159fffe1
diff --git a/crates/openhuman-core/src/agent/registry/agents/summarizer/prompt.md b/crates/openhuman-core/src/agent/registry/agents/summarizer/prompt.md
index 155c1a4745..73ed19dfac 100644
--- a/crates/openhuman-core/src/agent/registry/agents/summarizer/prompt.md
+++ b/crates/openhuman-core/src/agent/registry/agents/summarizer/prompt.md
@@ -48,7 +48,7 @@ Structure:
(Only include this section if the payload contained IDs/URLs/hashes. Skip otherwise.)
```
-Do not state the payload's size and do not add an "Original size" section: the runtime appends the exact byte count after your note. The input tells you how many bytes the payload has and that all of it is between the markers, so never describe it as truncated unless the payload text itself says it was cut.
+Do not state the payload's size and do not add an "Original size" section: the runtime states the exact byte count alongside your note. The input tells you how many bytes the payload has and that all of it is between the markers, so never describe it as truncated unless the payload text itself says it was cut.
## Edge cases
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output.rs
index 461ebcfbfd..93bcfe3939 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output.rs
@@ -179,6 +179,10 @@ impl Middleware<()> for ToolOutputMiddleware {
// tool's own output, which is what it is a contract about, rather than
// openhuman's annotation about it.
let mut pending_notice: Option<&'static str> = None;
+ // The byte count a summary replaced, stated in step 5 for the same
+ // reason as the notice: a cap that truncates the summary must not take
+ // the authoritative size with it (#6283).
+ let mut summarized_from_bytes: Option<usize> = None;
if !compaction_exempt {
if let Some(ps) = &self.payload_summarizer {
@@ -202,6 +206,7 @@ impl Middleware<()> for ToolOutputMiddleware {
from_tokens: estimate_output_tokens(payload.original_bytes),
to_tokens: estimate_output_tokens(payload.summary_bytes),
});
+ summarized_from_bytes = Some(payload.original_bytes);
result.content = payload.summary;
}
// The payload was fine as it was. Say nothing: a notice on
@@ -359,6 +364,12 @@ impl Middleware<()> for ToolOutputMiddleware {
if let Some(notice) = pending_notice {
result.content = format!("{notice}\n\n{}", result.content);
}
+ if let Some(bytes) = summarized_from_bytes {
+ result.content = format!(
+ "[openhuman: summary of {bytes} bytes of tool output, complete]\n\n{}",
+ result.content
+ );
+ }
Ok(())
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs
index 426b38ea66..8502a0b59c 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs
@@ -678,3 +678,47 @@ async fn the_turns_task_hint_reaches_the_payload_summarizer() {
"the turn's task hint must be handed to the payload summarizer"
);
}
+
+/// #6283 review: the authoritative size is stated after the output caps, so a
+/// tool cap shorter than the summary cannot cut it away.
+#[tokio::test]
+async fn the_summarized_size_survives_a_tool_cap_shorter_than_the_summary() {
+ let mut tool_policies = HashMap::new();
+ tool_policies.insert(
+ "terse".to_string(),
+ TaToolPolicy::classified().with_runtime(tinyagents_harness::tool::ToolRuntime {
+ timeout_ms: None,
+ timeout: tinyagents_harness::tool::ToolTimeout::Inherit,
+ max_retries: None,
+ idempotent: false,
+ cancelable: true,
+ sandbox: tinyagents_harness::tool::SandboxMode::Inherit,
+ max_result_bytes: Some(12),
+ streaming: false,
+ }),
+ );
+ let summary = "summary ".repeat(50);
+ let mut mw = summarizer_mw(StubSummarizer::ok(SummarizeOutcome::Summarized(
+ crate::agent::tinyagents::payload_summarizer::SummarizedPayload {
+ summary_bytes: summary.len(),
+ summary,
+ original_bytes: 119_796,
+ },
+ )));
+ mw.tool_policies = tool_policies;
+ let mut result = tool_result("terse", &"payload ".repeat(200));
+
+ mw.after_tool(&mut ctx(), &(), &mut result).await.unwrap();
+
+ assert!(
+ result
+ .content
+ .starts_with("[openhuman: summary of 119796 bytes of tool output, complete]"),
+ "the real size must lead the content whatever the caps did, got {:?}",
+ result.content.chars().take(160).collect::<String>()
+ );
+ assert!(
+ result.content.contains("[truncated by tool cap:"),
+ "the summary itself is still bound by the tool's cap"
+ );
+}
diff --git a/crates/openhuman-core/src/agent/tinyagents/payload_summarizer.rs b/crates/openhuman-core/src/agent/tinyagents/payload_summarizer.rs
index e71071854e..82ce4d134b 100644
--- a/crates/openhuman-core/src/agent/tinyagents/payload_summarizer.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/payload_summarizer.rs
@@ -517,14 +517,10 @@ impl SubagentPayloadSummarizer {
self.record_failure();
return Ok(SummarizeOutcome::Unavailable(UnavailableReason::Failed));
}
- // The size is the runtime's to state, not the model's. The model
- // was never told it and estimated it instead — live summaries
- // reported "~9,000 bytes (truncated payload)" for a complete
- // 119,796-byte input, and the orchestrator reasoned from that.
- let summary = format!(
- "{summary}\n\n## Original size\n{} bytes of tool output, complete, summarized into this note.",
- raw.len()
- );
+ // No size is written into the summary. The size is the runtime's
+ // to state, and `ToolOutputMiddleware` states it after the output
+ // caps, so a cap that cuts the summary cannot cut the size too.
+ let summary = summary.to_string();
if summary.len() >= raw.len() {
warn!(
tool = tool_name,
@@ -647,6 +643,24 @@ fn remember_summary(key: SummaryCacheKey, summary: String) {
/// the intent, not the whole thing.
const TASK_HINT_MAX_CHARS: usize = 2_000;
+/// Bound a task hint to [`TASK_HINT_MAX_CHARS`], keeping both ends. A pasted
+/// log or document usually carries the actual request at the end ("…find the
+/// authentication failure"), so a prefix-only cut drops the one part the
+/// summarizer needs.
+fn clip_task_hint(hint: &str) -> String {
+ let chars: Vec<char> = hint.chars().collect();
+ if chars.len() <= TASK_HINT_MAX_CHARS {
+ return hint.to_string();
+ }
+ let half = TASK_HINT_MAX_CHARS / 2;
+ let head: String = chars[..half].iter().collect();
+ let tail: String = chars[chars.len() - half..].iter().collect();
+ format!(
+ "{head}\n[... {} characters omitted ...]\n{tail}",
+ chars.len() - 2 * half
+ )
+}
+
/// Build the user-message prompt fed into the summarizer sub-agent.
///
/// Wraps the raw payload in `--- BEGIN ---` / `--- END ---` markers so
@@ -660,10 +674,7 @@ fn build_summarizer_prompt(tool_name: &str, parent_task_hint: Option<&str>, raw:
let hint_line = parent_task_hint
.map(str::trim)
.filter(|h| !h.is_empty())
- .map(|h| {
- let clipped: String = h.chars().take(TASK_HINT_MAX_CHARS).collect();
- format!("Parent task hint: {clipped}\n\n")
- })
+ .map(|h| format!("Parent task hint: {}\n\n", clip_task_hint(h)))
.unwrap_or_default();
format!(
"Tool name: {tool_name}\n\n{hint_line}Raw tool output: {} bytes, complete, all of it between the markers below (summarize per the extraction contract in your system prompt):\n\n--- BEGIN ---\n{raw}\n--- END ---",
diff --git a/crates/openhuman-core/src/agent/tinyagents/payload_summarizer_tests.rs b/crates/openhuman-core/src/agent/tinyagents/payload_summarizer_tests.rs
index a3d013b9c0..5dbb5091ed 100644
--- a/crates/openhuman-core/src/agent/tinyagents/payload_summarizer_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/payload_summarizer_tests.rs
@@ -264,7 +264,7 @@ async fn a_summary_written_for_another_goal_is_not_reused() {
}
#[test]
-fn a_successful_summary_is_remembered_and_states_the_real_size() {
+fn a_successful_summary_is_remembered_for_reuse() {
let summarizer = low_threshold_summarizer();
let raw = "x".repeat(50_000);
let key = summary_cache_key("size_tool", Some("size goal"), &raw);
@@ -274,7 +274,7 @@ fn a_successful_summary_is_remembered_and_states_the_real_size() {
"size_tool",
&raw,
std::time::Instant::now(),
- Ok("model note: original was ~9,000 bytes (truncated payload)".to_string()),
+ Ok("model note about the payload".to_string()),
key,
)
.expect("a usable summary is not an error");
@@ -282,12 +282,10 @@ fn a_successful_summary_is_remembered_and_states_the_real_size() {
let SummarizeOutcome::Summarized(payload) = outcome else {
panic!("a non-empty, smaller summary must be accepted");
};
- assert!(
- payload
- .summary
- .contains("50000 bytes of tool output, complete"),
- "the size the orchestrator reads must come from the real byte count, got: {}",
- payload.summary
+ assert_eq!(
+ payload.original_bytes,
+ raw.len(),
+ "the summarized size handed to the middleware must be the real byte count"
);
assert_eq!(
cached_summary(&key).as_deref(),
@@ -305,3 +303,25 @@ fn build_summarizer_prompt_states_the_real_byte_count() {
"the summarizer must be told the payload's exact size, got: {prompt}"
);
}
+
+#[test]
+fn a_long_task_hint_keeps_its_trailing_request() {
+ let hint = format!(
+ "{}\nfind the authentication failure",
+ "log line ".repeat(1_000)
+ );
+ let prompt = build_summarizer_prompt("hint_tool", Some(&hint), "payload");
+ assert!(
+ prompt.contains("find the authentication failure"),
+ "the request at the end of a long message must survive clipping"
+ );
+ assert!(
+ prompt.contains("log line"),
+ "the start of the message is kept too"
+ );
+ assert!(
+ prompt.len() < hint.len(),
+ "the hint must still be bounded, got a {}-byte prompt",
+ prompt.len()
+ );
+}