Skip to content

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

fix(tinyagents): split one input allowance between the two things that add to it\n\nCodeRabbit on #6068: the restored results and the artifact contents list each\nbounded themselves against `legacy_max_input_tokens`, computed independently —\nso restoration could fill the whole allowance and the contents list add its\ntenth on top. Each was bounded; the pair was not.\n\nThe allowance is now derived once and divided: a tenth to the contents list, the\nremainder to restoration. The sum is the bound rather than either half, and the\nmiddlewares receive their final share instead of a figure they divide again.\n

sanil-23b38fdd8a5ef4
diff --git a/src/openhuman/agent/tinyagents/middleware_part_06.rs b/src/openhuman/agent/tinyagents/middleware_part_06.rs
index 87575f2a2d..36266dbe3c 100644
--- a/src/openhuman/agent/tinyagents/middleware_part_06.rs
+++ b/src/openhuman/agent/tinyagents/middleware_part_06.rs
@@ -313,9 +313,10 @@ const ARTIFACT_INDEX_NAMESPACE: &str = "tool_results";
/// appended at the tail rather than the head so the cacheable prompt prefix is
/// untouched.
pub(crate) struct ArtifactIndexTocMiddleware {
- /// The input-token allowance the trim enforces, so the contents list can be
- /// capped against it. `0` disables the cap (no advertised window, and no
- /// trim installed either).
+ /// This middleware's share of the turn's input allowance (a tenth, split at
+ /// the install site so restoration and this list cannot each claim the
+ /// whole). `0` disables the cap — no advertised window, and no trim
+ /// installed either.
input_budget: u64,
}
@@ -396,7 +397,10 @@ impl Middleware<()> for ArtifactIndexTocMiddleware {
// rule the rest of this ladder follows, and the reason the artifacts are
// findable at all.
if self.input_budget > 0 {
- let cap = (self.input_budget / 10).max(1);
+ // Already this middleware's share of the turn's allowance — the
+ // split happens once, at the install site, so the two things that
+ // add to the request cannot each spend the whole of it.
+ let cap = self.input_budget.max(1);
let mut used: u64 = 0;
let mut kept = 0usize;
for row in &rows {
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 7f8af33803..d794f35905 100644
--- a/src/openhuman/agent/tinyagents/middleware_tests_part_03_tests.rs
+++ b/src/openhuman/agent/tinyagents/middleware_tests_part_03_tests.rs
@@ -462,7 +462,9 @@ async fn toc_is_capped_and_reports_what_it_omitted() {
.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(2_000);
+ // This middleware's share of the allowance, already split at the install
+ // site — not the whole turn allowance.
+ let mw = ArtifactIndexTocMiddleware::new(200);
let mut request = ModelRequest {
messages: vec![TaMessage::user("what did you find?")],
..Default::default()
@@ -476,7 +478,7 @@ async fn toc_is_capped_and_reports_what_it_omitted() {
"an omitted count must be disclosed, not silently dropped: {text}"
);
assert!(
- estimate_text_tokens(&text) < 2_000,
- "the list must stay inside the allowance it was given"
+ estimate_text_tokens(&text) < 400,
+ "the list must stay inside the share it was given (200, plus the header)"
);
}
diff --git a/src/openhuman/agent/tinyagents/mod_part_04.rs b/src/openhuman/agent/tinyagents/mod_part_04.rs
index 4164b6cf60..a98407ce12 100644
--- a/src/openhuman/agent/tinyagents/mod_part_04.rs
+++ b/src/openhuman/agent/tinyagents/mod_part_04.rs
@@ -565,14 +565,12 @@ fn assemble_turn_harness(
if microcompact_keep_recent > 0 {
// The token-budget gate the crate added for this (#4755) and this call
// site never used. Constructed bare, microcompact blanks on EVERY call
- // once past `keep_recent` — not only under pressure — so a ten-call turn
- // answered from the last five results while sitting well inside a window
- // with room for all ten. Not a capped-turn problem: every turn.
- //
- // The budget matches the trim's allowance, which is what puts
- // microcompact strictly behind compression. With no advertised window
- // there is nothing to size against and nothing else bounding the
- // transcript, so the legacy always-blank behaviour stays as the backstop.
+ // past `keep_recent` — not only under pressure — so a ten-call turn
+ // answered from the last five results while well inside a window with
+ // room for all ten. Not a capped-turn problem: every turn. The budget
+ // matches the trim's allowance, putting microcompact strictly behind
+ // compression. With no window there is nothing to size against and
+ // nothing else bounding growth, so always-blank stays as the backstop.
let microcompact = tinyagents_harness::middleware::MicrocompactMiddleware::new(
microcompact_keep_recent,
crate::openhuman::agent::context::CLEARED_PLACEHOLDER,
@@ -605,17 +603,24 @@ fn assemble_turn_harness(
// turn that answers a human. It also leaves a child's budget alone: the
// conclusion costs a call, and turning every delegated run's N tool rounds
// into N-1 is not a trade to impose as a side effect.
+ // One allowance, split between the two things that add to the request
+ // (CodeRabbit on #6068). Computed independently they were each bounded and
+ // the pair was not: restoration could fill the whole allowance and the
+ // contents list add its tenth on top. `0` when no window is advertised.
+ let trim_allowance = context_window
+ .filter(|w| *w > 0)
+ .map(|w| middleware::legacy_max_input_tokens(w).max(1))
+ .unwrap_or(0);
+ let toc_allowance = trim_allowance / 10;
+ let restore_allowance = trim_allowance.saturating_sub(toc_allowance);
+
let wrap_up_mw = (pause_at_cap && subagent_scope.is_none()).then(|| {
Arc::new(middleware::FinalCallWrapUpMiddleware::new(
crate::openhuman::agent::harness::session::turn_checkpoint::MAX_ITER_CHECKPOINT_INSTRUCTION,
tool_outcome_sink.clone(),
- // The trim's own allowance, so restoration stops short of
- // provoking an eviction (see the middleware). `0` = no window
- // advertised, so no bound and no trim either.
- context_window
- .filter(|w| *w > 0)
- .map(|w| middleware::legacy_max_input_tokens(w).max(1))
- .unwrap_or(0),
+ // What is left after the contents list's share, so restoration
+ // stops short of provoking an eviction (see the middleware).
+ restore_allowance,
))
});
let wrap_up_fired = wrap_up_mw.as_ref().map(|mw| mw.fired());
@@ -624,17 +629,12 @@ fn assemble_turn_harness(
}
// Issue #6014: say where the offloaded results went, from the index rather
- // than from the transcript. Registered after the reduction steps above so it
- // renders whatever survived them, and before the trim so its message is
- // counted in that budget — the trim never drops a system message, so being
- // counted costs nothing and being uncounted could push the request over.
+ // than the transcript. After the reduction steps so it renders what
+ // survived them, before the trim so its message is counted in that budget.
+ // Its share of the allowance split above: the list is a system message, so
+ // nothing downstream can shrink it (see the middleware).
harness.push_middleware(Arc::new(middleware::ArtifactIndexTocMiddleware::new(
- // Bounded against the trim's own allowance: the list is a system
- // message, so nothing downstream can shrink it (see the middleware).
- context_window
- .filter(|w| *w > 0)
- .map(|w| middleware::legacy_max_input_tokens(w).max(1))
- .unwrap_or(0),
+ toc_allowance,
)));
if let Some(window) = context_window.filter(|w| *w > 0) {