Skip to content

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

fix(agent): keep the unavailable notice intact, and stop it overclaiming

M3gA-Mindfd1e0ce4ac49
diff --git a/src/openhuman/agent/tinyagents/middleware.rs b/src/openhuman/agent/tinyagents/middleware.rs
index 3a101fc55e..b0c61f1a2e 100644
--- a/src/openhuman/agent/tinyagents/middleware.rs
+++ b/src/openhuman/agent/tinyagents/middleware.rs
@@ -718,6 +718,17 @@ impl Middleware<()> for ToolOutputMiddleware {
// `if let Ok(Some(payload)) = …`, which discarded `Err(_)` and
// `Ok(None)` identically — so a failed summarization reached the
// model as an unannounced raw dump and it re-called the same tool.
+ // Held until after the caps below rather than prefixed here. The notice
+ // is ~165 chars; a tool declaring a `max_result_size_chars` smaller than
+ // that had step 3 run `chars().take(cap)` straight through it, cutting
+ // the reason text and the do-not-re-run sentence mid-word — so the one
+ // stage that exists to stop a re-dispatch loop was removed exactly when
+ // the output was most aggressively truncated. Capping the payload first
+ // and prefixing afterwards also means a tool's declared cap bounds the
+ // 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;
+
if !compaction_exempt {
if let Some(ps) = &self.payload_summarizer {
match ps
@@ -747,7 +758,7 @@ impl Middleware<()> for ToolOutputMiddleware {
?reason,
"[tinyagents::mw] payload_summarizer unavailable; disclosing raw output"
);
- result.content = format!("{}\n\n{}", reason.notice(), result.content);
+ pending_notice = Some(reason.notice());
}
// Reserved for fatal misconfiguration. Previously
// indistinguishable from "nothing to do"; the model is now
@@ -759,11 +770,7 @@ impl Middleware<()> for ToolOutputMiddleware {
error = %error,
"[tinyagents::mw] payload_summarizer errored; disclosing raw output"
);
- result.content = format!(
- "{}\n\n{}",
- UnavailableReason::Failed.notice(),
- result.content
- );
+ pending_notice = Some(UnavailableReason::Failed.notice());
}
}
}
@@ -887,6 +894,15 @@ impl Middleware<()> for ToolOutputMiddleware {
}
result.content = capped;
}
+
+ // 5. The disclosure, last, so no cap above can eat it. The model has to
+ // be able to read *why* the payload is raw and that re-running will
+ // not summarize it — a half-truncated notice is worse than none,
+ // because it still looks like tool output.
+ if let Some(notice) = pending_notice {
+ result.content = format!("{notice}\n\n{}", result.content);
+ }
+
Ok(())
}
}
@@ -3578,6 +3594,71 @@ mod tests {
assert_eq!(mw.tool_char_cap("other"), None);
}
+ /// openhuman#5722 review: the disclosure used to be prefixed *before* the
+ /// per-tool char cap, so a tool declaring a cap shorter than the notice had
+ /// `chars().take(cap)` slice through the notice itself — dropping the
+ /// reason and the do-not-re-run sentence, and leaving the model a truncated
+ /// fragment that still reads as tool output. The notice is applied after
+ /// every cap now, so it survives intact whatever the tool declared.
+ #[tokio::test]
+ async fn an_unavailable_notice_survives_a_tool_cap_shorter_than_itself() {
+ 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,
+ // Far shorter than the ~165-char notice.
+ max_result_bytes: Some(12),
+ streaming: false,
+ }),
+ );
+ let mw = ToolOutputMiddleware {
+ // Large enough that the byte-budget backstop never fires, so this
+ // observes the per-tool cap alone.
+ budget_bytes: 10_000_000,
+ payload_summarizer: Some(StubSummarizer::ok(SummarizeOutcome::Unavailable(
+ UnavailableReason::Failed,
+ ))),
+ artifact_store: None,
+ tokenjuice_compaction_enabled: false,
+ tokenjuice_compression:
+ crate::openhuman::inference::tokenjuice::AgentTokenjuiceCompression::Off,
+ tool_policies,
+ };
+
+ let mut result = tool_result("terse", &"payload ".repeat(200));
+ mw.after_tool(&mut ctx(), &(), &mut result).await.unwrap();
+
+ let notice = UnavailableReason::Failed.notice();
+ assert!(
+ result.content.starts_with(notice),
+ "the complete notice must lead the content, got {:?}",
+ result.content.chars().take(200).collect::<String>()
+ );
+ assert!(
+ result
+ .content
+ .contains("Re-running it will not produce a summary."),
+ "the do-not-re-run sentence is the whole point of the notice and must survive"
+ );
+ // The payload itself is still capped — deferring the notice must not
+ // smuggle the tool past its own declared limit.
+ let payload = result
+ .content
+ .strip_prefix(notice)
+ .expect("notice prefix")
+ .trim_start();
+ assert!(
+ payload.contains("[truncated by tool cap:"),
+ "the raw payload must still be truncated to the tool's cap, got {payload:?}"
+ );
+ }
+
#[tokio::test]
async fn tool_output_honors_a_tools_own_cap() {
let mut tool_policies = HashMap::new();
diff --git a/src/openhuman/agent/tinyagents/payload_summarizer.rs b/src/openhuman/agent/tinyagents/payload_summarizer.rs
index ad077f3077..460dff608a 100644
--- a/src/openhuman/agent/tinyagents/payload_summarizer.rs
+++ b/src/openhuman/agent/tinyagents/payload_summarizer.rs
@@ -94,31 +94,43 @@ pub enum UnavailableReason {
impl UnavailableReason {
/// The model-facing notice for this reason.
///
- /// **Prefixed** to the tool result, never appended: the downstream
+ /// **Prefixed** to the tool result, never appended, and prefixed by the
+ /// caller *after* its truncation stages have run. Both halves matter. The
/// per-tool char cap keeps the head (`content.chars().take(cap)`), so an
- /// appended notice is the first thing truncation removes.
+ /// appended notice is the first thing truncation removes — but a notice
+ /// prefixed *before* that cap is no safer, because a tool declaring a
+ /// `max_result_size_chars` below this string's length truncates the notice
+ /// itself. Applying it last is the only placement that survives both.
///
- /// Every variant ends by saying a retry is pointless. Without that, the
- /// model's reasonable response to a truncated dump is to call the same
- /// tool again — the silent re-dispatch loop that reads to a user as a
- /// hang.
+ /// Every variant ends by telling the model not to re-run the tool *for a
+ /// summary*. Without that, the model's reasonable response to a truncated
+ /// dump is to call the same tool again — the silent re-dispatch loop that
+ /// reads to a user as a hang.
+ ///
+ /// Note what this deliberately does **not** say: that a re-run returns the
+ /// same bytes. It usually will not. `Failed` leaves the breaker open for
+ /// further attempts, so a second summarization can genuinely succeed, and
+ /// an API-backed tool is time-varying regardless. Asserting result identity
+ /// would be false in the direction that suppresses a legitimate retry the
+ /// model had good reason to make. The claim is scoped to the only thing
+ /// this code actually knows: re-running will not conjure a summary.
#[must_use]
pub fn notice(self) -> &'static str {
match self {
Self::PayloadTooLarge => concat!(
"[openhuman: summarization unavailable — this output exceeds the summarizer's ",
"size cap, so the raw output follows and may be truncated. ",
- "Re-running this tool will return the same result.]"
+ "Re-running it will not produce a summary.]"
),
Self::Disabled => concat!(
"[openhuman: summarization unavailable — it is switched off for this session ",
"after repeated failures, so the raw output follows. ",
- "Re-running this tool will return the same result.]"
+ "Re-running it will not produce a summary.]"
),
Self::Failed => concat!(
"[openhuman: summarization unavailable — the summarizer did not return a usable ",
"summary, so the raw output follows. ",
- "Re-running this tool will return the same result.]"
+ "Re-running it will not produce a summary.]"
),
}
}
@@ -688,7 +700,7 @@ mod tests {
] {
let notice = reason.notice();
assert!(
- notice.contains("Re-running this tool will return the same result."),
+ notice.contains("Re-running it will not produce a summary."),
"{reason:?} must tell the model not to retry: {notice}"
);
assert!(