Skip to content

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

fix(tinyagents): bound restoration too when no context window is advertised\n\nThe previous commit gave the no-window fallback to the contents list alone, so\n`restore_allowance = 0.saturating_sub(512)` handed `FinalCallWrapUpMiddleware`\nthe very sentinel the fallback existed to avoid. On a capped top-level turn\nagainst a model advertising no window, restoration could then refill every\ncleared result with no trim middleware behind it — overflowing the provider\nlimit and losing the in-loop conclusion this whole path exists to deliver.\n\n`split_input_allowance` replaces the two independently derived bounds with one\nfunction returning both, so neither can be `0`: it takes the fallback total\nwhen there is no window, gives the contents list a tenth (floored at 64,\ncapped at half so restoration keeps a real share), and hands the remainder to\nrestoration. Making the pair the return value is the point — the bug was twice\na consequence of deriving them apart.\n\nReported by CodeRabbit on #6068.\n\n

sanil-23a04f77a2cb28
diff --git a/src/openhuman/agent/tinyagents/middleware_part_06.rs b/src/openhuman/agent/tinyagents/middleware_part_06.rs
index 9d6b4914fa..2b83092286 100644
--- a/src/openhuman/agent/tinyagents/middleware_part_06.rs
+++ b/src/openhuman/agent/tinyagents/middleware_part_06.rs
@@ -276,32 +276,48 @@ fn captured_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.
+/// The input allowance the reductions divide up when no context window is
+/// advertised.
///
/// 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;
+/// installed either, which is precisely why neither share may be left unbounded
+/// on that path (CodeRabbit on #6068): these are the two additions nothing
+/// downstream can shrink, on the one configuration where nothing downstream is
+/// watching. Sized so the contents list's tenth lands on the 512 tokens a
+/// realistic handful of artifacts needs.
+const NO_WINDOW_ALLOWANCE: u64 = 5_120;
-/// 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".
+/// The floor under the contents list's proportional share, so a small window
+/// still yields a cap rather than collapsing to `0` — which both middlewares
+/// read as "unbounded".
const TOC_ALLOWANCE_MIN: u64 = 64;
-/// The contents list's share of a turn's input allowance.
+/// Split a turn's input allowance between the two things that add to the
+/// request: the artifact contents list and the wrap-up's result restoration.
///
-/// 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)
+/// Returns `(toc, restore)`. **Neither is ever `0`** — that is the sentinel both
+/// middlewares read as "no cap", so handing it to either is the bug this
+/// function exists to make unrepresentable. It bit twice on #6068: first when
+/// the two bounds were computed independently and the pair went unbounded, then
+/// when the no-window fallback was given to the contents list alone and
+/// `saturating_sub` handed restoration the sentinel it had just been rescued
+/// from.
+///
+/// The contents list takes a tenth — generous for the realistic case, firm
+/// about the pathological one — floored so a small window cannot round it away,
+/// and capped at half so restoration keeps a real share of a small allowance.
+pub(crate) fn split_input_allowance(trim_allowance: u64) -> (u64, u64) {
+ let total = if trim_allowance == 0 {
+ NO_WINDOW_ALLOWANCE
+ } else {
+ trim_allowance
+ };
+ let toc = (total / 10)
+ .max(TOC_ALLOWANCE_MIN)
+ .min(total.div_ceil(2))
+ .max(1);
+ (toc, total.saturating_sub(toc).max(1))
}
/// Renders the run's persisted-artifact index into the request as a short
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 b7781a9e86..7266cd8654 100644
--- a/src/openhuman/agent/tinyagents/middleware_tests_part_03_tests.rs
+++ b/src/openhuman/agent/tinyagents/middleware_tests_part_03_tests.rs
@@ -483,28 +483,34 @@ async fn toc_is_capped_and_reports_what_it_omitted() {
);
}
-/// 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.
+/// Neither share may ever be `0`, because `0` is the sentinel both middlewares
+/// read as "unbounded" — so the allowances that round down into it are the ones
+/// to pin (CodeRabbit on #6068, twice). A model advertising no window is the
+/// sharp case: no window means no `ImageAwareMessageTrimMiddleware` either, so
+/// these two additions are the ones 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");
+fn neither_share_is_ever_the_unbounded_sentinel() {
+ for trim_allowance in [0_u64, 1, 5, 9, 99, 100, 2_000, 100_000] {
+ let (toc, restore) = split_input_allowance(trim_allowance);
+ assert!(toc > 0, "allowance {trim_allowance} disabled the TOC cap");
assert!(
- share <= allowance,
- "a {allowance}-token allowance yielded a larger share: {share}"
+ restore > 0,
+ "allowance {trim_allowance} left restoration unbounded"
);
+ if trim_allowance > 1 {
+ assert!(
+ toc + restore <= trim_allowance.max(NO_WINDOW_ALLOWANCE.min(u64::MAX)),
+ "allowance {trim_allowance} split into more than it had: {toc} + {restore}"
+ );
+ }
}
+ // With no window the split is taken from the fallback total, not from zero.
+ let (toc, restore) = split_input_allowance(0);
+ assert_eq!(toc, 512);
+ assert_eq!(toc + restore, NO_WINDOW_ALLOWANCE);
// 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);
+ assert_eq!(split_input_allowance(2_000), (200, 1_800));
+ assert_eq!(split_input_allowance(100_000), (10_000, 90_000));
}
/// The end of the same argument: with no window, a run holding far more
@@ -526,7 +532,7 @@ async fn the_contents_list_stays_bounded_when_no_window_is_advertised() {
.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 mw = ArtifactIndexTocMiddleware::new(split_input_allowance(0).0);
let mut request = ModelRequest {
messages: vec![TaMessage::user("what did you find?")],
..Default::default()
@@ -545,3 +551,48 @@ async fn the_contents_list_stays_bounded_when_no_window_is_advertised() {
estimate_text_tokens(&text)
);
}
+
+/// The other half of the no-window split: restoration must stop too. Without a
+/// window there is no trim middleware behind it, so an unbounded wrap-up would
+/// hand the provider a request it rejects — losing the in-loop conclusion the
+/// whole cap-checkpoint path exists to deliver (CodeRabbit on #6068).
+#[tokio::test]
+async fn wrap_up_restoration_stays_bounded_when_no_window_is_advertised() {
+ let big = "x".repeat(8_000);
+ let entries: Vec<(String, String)> = (0..40)
+ .map(|i| (format!("call-{i}"), big.clone()))
+ .collect();
+ let borrowed: Vec<(&str, &str)> = entries
+ .iter()
+ .map(|(a, b)| (a.as_str(), b.as_str()))
+ .collect();
+ let (_toc, restore) = split_input_allowance(0);
+ let mw = FinalCallWrapUpMiddleware::new("CONCLUDE NOW", sink_with(&borrowed), restore);
+ let mut ctx = RunContext::new(RunConfig::new("mw-test").with_max_model_calls(2), ());
+ ctx.limits.record_model_call().unwrap();
+ ctx.limits.record_model_call().unwrap();
+ let mut request = ModelRequest {
+ messages: borrowed
+ .iter()
+ .map(|(id, _)| TaMessage::tool(*id, CLEARED_PLACEHOLDER))
+ .collect(),
+ ..Default::default()
+ };
+
+ mw.before_model(&mut ctx, &(), &mut request).await.unwrap();
+
+ let used: u64 = request.messages.iter().map(estimate_message_tokens).sum();
+ assert!(
+ used <= NO_WINDOW_ALLOWANCE,
+ "the no-window fallback must bound the whole request, not just the list: {used}"
+ );
+ let restored = request
+ .messages
+ .iter()
+ .filter(|m| m.text().starts_with("xxx"))
+ .count();
+ assert!(
+ restored >= 1 && restored < 40,
+ "some restored, not all 40 — got {restored}"
+ );
+}
diff --git a/src/openhuman/agent/tinyagents/mod_part_04.rs b/src/openhuman/agent/tinyagents/mod_part_04.rs
index abfa6dbeb5..4456d54895 100644
--- a/src/openhuman/agent/tinyagents/mod_part_04.rs
+++ b/src/openhuman/agent/tinyagents/mod_part_04.rs
@@ -611,8 +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 = middleware::toc_allowance_share(trim_allowance);
- let restore_allowance = trim_allowance.saturating_sub(toc_allowance);
+ let (toc_allowance, restore_allowance) = middleware::split_input_allowance(trim_allowance);
let wrap_up_mw = (pause_at_cap && subagent_scope.is_none()).then(|| {
Arc::new(middleware::FinalCallWrapUpMiddleware::new(