Skip to content

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

fix(tinyagents): keep microcompact budget when context window is unknown

Steven Enamakeld25b4c465d03
diff --git a/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder.rs b/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder.rs
index 7bbae3c93f..db899671ae 100644
--- a/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder.rs
@@ -21,6 +21,15 @@ use crate::agent::tinyagents::host::OpenHumanRunContext;
use crate::agent::tinyagents::model::TurnChatModel;
use crate::agent::tinyagents::turn_outcome::ToolOutcomeSink;
+/// Window assumed for sizing microcompact when the model's context window is
+/// unknown (not in any static table, no `context_length` from `/v1/models`).
+/// Deliberately conservative: small enough that a runaway transcript is still
+/// blanked well before any real model overflows, large enough that a short run
+/// stays append-only and prompt-cache eligible. Used only for microcompact;
+/// compression and trim still treat an unknown window as "nothing to size
+/// against", and known-window behaviour is unchanged.
+pub(super) const UNKNOWN_WINDOW_FALLBACK_TOKENS: u64 = 64_000;
+
/// Fractions of a capped turn's model-call budget at which the model is told
/// how many calls are left (#6958; `FinalCallWrapUpMiddleware::with_budget_notice`).
const BUDGET_NOTICE_THRESHOLDS: [f64; 2] = [0.5, 0.8];
@@ -363,14 +372,14 @@ pub(super) fn microcompact_middleware(
keep_recent: usize,
context_window: Option<u64>,
) -> tinyagents_harness::middleware::MicrocompactMiddleware {
- let window = context_window
- .filter(|w| *w > 0)
- .unwrap_or(UNKNOWN_WINDOW_FALLBACK_TOKENS);
- tinyagents_harness::middleware::MicrocompactMiddleware::new(
+ let mw = tinyagents_harness::middleware::MicrocompactMiddleware::new(
keep_recent,
crate::agent::context::CLEARED_PLACEHOLDER,
- )
- .with_token_budget(legacy_max_input_tokens(window).max(1))
+ );
+ match context_window.filter(|w| *w > 0) {
+ Some(window) => mw.with_token_budget(legacy_max_input_tokens(window).max(1)),
+ None => mw,
+ }
}
/// Share of the context window a compaction keeps verbatim as its tail.
diff --git a/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder_tests.rs b/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder_tests.rs
index 83d8e43df6..ffd5ed57bc 100644
--- a/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder_tests.rs
@@ -60,3 +60,33 @@ fn an_override_without_a_window_still_pins_with_a_token_tail() {
assert!(policy.pin_turn_user_message);
assert_eq!(policy.keep_recent_tokens, Some(19_200));
}
+
+// ── microcompact budget (prompt-cache churn when the window is unknown) ──────
+
+#[test]
+fn an_unknown_window_still_gives_microcompact_a_budget_so_short_runs_stay_append_only() {
+ // Unbudgeted microcompact blanks every old tool body on every turn, which
+ // rewrites the sent prefix and misses the provider's prompt cache.
+ for window in [None, Some(0)] {
+ let mw = microcompact_middleware(5, window);
+ assert_eq!(
+ mw.token_budget(),
+ Some(legacy_max_input_tokens(UNKNOWN_WINDOW_FALLBACK_TOKENS)),
+ "window {window:?}"
+ );
+ }
+}
+
+#[test]
+fn the_unknown_window_budget_is_finite_so_blanking_still_backstops_growth() {
+ let budget = microcompact_middleware(5, None).token_budget().unwrap();
+ assert!(budget < UNKNOWN_WINDOW_FALLBACK_TOKENS);
+ assert!(budget > 0);
+}
+
+#[test]
+fn a_known_window_keeps_the_trim_allowance_as_the_microcompact_budget() {
+ let mw = microcompact_middleware(5, Some(200_000));
+ assert_eq!(mw.token_budget(), Some(legacy_max_input_tokens(200_000)));
+ assert_eq!(mw.keep_recent(), 5);
+}