Skip to content

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

fix(tinyagents): give the contents list a cap when the allowance rounds to zero

sanil-2334b4d3518549
diff --git a/src/openhuman/agent/tinyagents/middleware_part_06.rs b/src/openhuman/agent/tinyagents/middleware_part_06.rs
index 36266dbe3c..9d6b4914fa 100644
--- a/src/openhuman/agent/tinyagents/middleware_part_06.rs
+++ b/src/openhuman/agent/tinyagents/middleware_part_06.rs
@@ -198,7 +198,7 @@ impl Middleware<()> for FinalCallWrapUpMiddleware {
if body.trim() != CLEARED_PLACEHOLDER {
continue;
}
- let Some(outcome) = self_outcome_for(&outcomes, &tool.tool_call_id) else {
+ let Some(outcome) = captured_outcome_for(&outcomes, &tool.tool_call_id) else {
continue;
};
if outcome.trim().is_empty() {
@@ -258,8 +258,10 @@ impl Middleware<()> for FinalCallWrapUpMiddleware {
/// The captured content for one tool call id, if the sink holds it.
///
/// A free function so the borrow of the locked sink stays scoped to the lookup
-/// rather than being held across the mutation of `request.messages`.
-fn self_outcome_for(
+/// rather than being held across the mutation of `request.messages`. Named
+/// without a `self_` prefix (tinysweeper on #6068): it takes no receiver, and
+/// the prefix read as a method on something.
+fn captured_outcome_for(
outcomes: &[crate::openhuman::agent::tinyagents::ToolCallOutcome],
call_id: &str,
) -> Option<String> {
@@ -274,6 +276,34 @@ fn self_outcome_for(
/// Namespace `ToolOutputMiddleware` writes each persisted artifact under.
const ARTIFACT_INDEX_NAMESPACE: &str = "tool_results";
+/// The contents list's cap when the model advertises no context window.
+///
+/// A window is what every other bound here is derived from, so without one
+/// there is nothing to take a share of — and no `ImageAwareMessageTrimMiddleware`
+/// installed either, which is precisely why the list cannot be left unbounded
+/// on that path (CodeRabbit on #6068): it is the one message nothing downstream
+/// can shrink, on the one configuration where nothing downstream is watching.
+/// Small enough to stay a contents list, large enough for the realistic case of
+/// a handful of artifacts.
+const TOC_ALLOWANCE_NO_WINDOW: u64 = 512;
+
+/// The floor under a proportional share, so a small window still yields a cap
+/// rather than collapsing to `0` — which this middleware reads as "unbounded".
+const TOC_ALLOWANCE_MIN: u64 = 64;
+
+/// The contents list's share of a turn's input allowance.
+///
+/// A tenth: generous for the realistic case (a handful of artifacts is a few
+/// hundred bytes) and firm about the pathological one. Floored so a small
+/// window cannot round the share down to the sentinel that disables the cap,
+/// and given a fixed fallback when there is no window to take a share of.
+pub(crate) fn toc_allowance_share(trim_allowance: u64) -> u64 {
+ if trim_allowance == 0 {
+ return TOC_ALLOWANCE_NO_WINDOW;
+ }
+ (trim_allowance / 10).max(TOC_ALLOWANCE_MIN).min(trim_allowance)
+}
+
/// Renders the run's persisted-artifact index into the request as a short
/// contents list, so the model can always see what this turn has gathered and
/// where the full copy lives.
diff --git a/src/openhuman/agent/tinyagents/middleware_tests_part_03_tests.rs b/src/openhuman/agent/tinyagents/middleware_tests_part_03_tests.rs
index d794f35905..b7781a9e86 100644
--- a/src/openhuman/agent/tinyagents/middleware_tests_part_03_tests.rs
+++ b/src/openhuman/agent/tinyagents/middleware_tests_part_03_tests.rs
@@ -482,3 +482,66 @@ async fn toc_is_capped_and_reports_what_it_omitted() {
"the list must stay inside the share it was given (200, plus the header)"
);
}
+
+/// The share must never be `0`, because `0` is the sentinel that disables the
+/// cap — so the two allowances that round down to it are the ones to pin
+/// (CodeRabbit on #6068). A model advertising no window is the sharper of the
+/// two: no window means no `ImageAwareMessageTrimMiddleware` either, so the
+/// contents list is the one message that nothing downstream would shrink.
+#[test]
+fn a_small_or_absent_allowance_still_yields_a_cap() {
+ assert!(
+ toc_allowance_share(0) > 0,
+ "no window must still bound the list, not disable the cap"
+ );
+ for allowance in [1_u64, 5, 9, 99] {
+ let share = toc_allowance_share(allowance);
+ assert!(share > 0, "a {allowance}-token allowance rounded to no cap");
+ assert!(
+ share <= allowance,
+ "a {allowance}-token allowance yielded a larger share: {share}"
+ );
+ }
+ // Proportional sizing is untouched where there is something to divide.
+ assert_eq!(toc_allowance_share(2_000), 200);
+ assert_eq!(toc_allowance_share(100_000), 10_000);
+}
+
+/// The end of the same argument: with no window, a run holding far more
+/// artifacts than any list should carry still produces a bounded message.
+#[tokio::test]
+async fn the_contents_list_stays_bounded_when_no_window_is_advertised() {
+ let entries: Vec<(String, String, String, u64)> = (0..400)
+ .map(|i| {
+ (
+ format!("call-{i}"),
+ "file_read".to_string(),
+ format!("outputs/a-fairly-long-artifact-path-{i}.json"),
+ 1_000,
+ )
+ })
+ .collect();
+ let borrowed: Vec<(&str, &str, &str, u64)> = entries
+ .iter()
+ .map(|(a, b, c, d)| (a.as_str(), b.as_str(), c.as_str(), *d))
+ .collect();
+ let mut ctx = ctx_with_artifacts(&borrowed).await;
+ let mw = ArtifactIndexTocMiddleware::new(toc_allowance_share(0));
+ let mut request = ModelRequest {
+ messages: vec![TaMessage::user("what did you find?")],
+ ..Default::default()
+ };
+
+ mw.before_model(&mut ctx, &(), &mut request).await.unwrap();
+
+ let text = request.messages.last().expect("a contents list").text();
+ assert!(
+ text.contains("not listed here"),
+ "an omitted count must be disclosed, not silently dropped: {text}"
+ );
+ assert!(
+ estimate_text_tokens(&text) < 1_024,
+ "the no-window fallback must bound the list: {}",
+ estimate_text_tokens(&text)
+ );
+}
diff --git a/src/openhuman/agent/tinyagents/mod_part_04.rs b/src/openhuman/agent/tinyagents/mod_part_04.rs
index a98407ce12..abfa6dbeb5 100644
--- a/src/openhuman/agent/tinyagents/mod_part_04.rs
+++ b/src/openhuman/agent/tinyagents/mod_part_04.rs
@@ -611,7 +611,7 @@ fn assemble_turn_harness(
.filter(|w| *w > 0)
.map(|w| middleware::legacy_max_input_tokens(w).max(1))
.unwrap_or(0);
- let toc_allowance = trim_allowance / 10;
+ let toc_allowance = middleware::toc_allowance_share(trim_allowance);
let restore_allowance = trim_allowance.saturating_sub(toc_allowance);
let wrap_up_mw = (pause_at_cap && subagent_scope.is_none()).then(|| {