Skip to content

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

feat(agent): add context ladder harness for tinyagents

Steven Enamakel311cfa3de0fd
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 ca8d63cb9f..7bbae3c93f 100644
--- a/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder.rs
@@ -154,16 +154,8 @@ pub(super) fn install_context_ladder(
// 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::agent::context::CLEARED_PLACEHOLDER,
- );
- let microcompact = match context_window.filter(|w| *w > 0) {
- Some(window) => microcompact.with_token_budget(legacy_max_input_tokens(window).max(1)),
- None => microcompact,
- };
+ // compression. See `microcompact_middleware` for the unknown-window case.
+ let microcompact = microcompact_middleware(microcompact_keep_recent, context_window);
// Emit `AgentEvent::Compressed` when a body is cleared. Off by default —
// the middleware was built as "a silent transcript rewrite" — which is
// why blanking was, until now, the one reduction step nobody could see
@@ -354,6 +346,33 @@ pub(super) fn compression_policy(
.map(|policy| with_turn_aware_tail(policy, window))
}
+/// Build the microcompact step of the context ladder.
+///
+/// With a known window the token budget is the trim's allowance
+/// (`legacy_max_input_tokens(window)`). Without one, the budget is sized
+/// against [`UNKNOWN_WINDOW_FALLBACK_TOKENS`] instead of being left unset:
+/// a bare microcompact blanks every tool result older than `keep_recent` on
+/// every turn, and each blanking rewrites an already-sent prefix position, so
+/// the provider's prompt (KV) cache misses from that point on. Observed on a
+/// router whose model id had no static window: 77 of 80 turns emitted
+/// `compressed` events, only 48-67% of input tokens were cached (99% when the
+/// same prompts were sent append-only) and cold calls took ~6-7 s vs ~3 s
+/// warm. The finite fallback keeps a short run append-only while still
+/// blanking once the transcript outgrows it, so growth stays bounded.
+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(
+ keep_recent,
+ crate::agent::context::CLEARED_PLACEHOLDER,
+ )
+ .with_token_budget(legacy_max_input_tokens(window).max(1))
+}
+
/// Share of the context window a compaction keeps verbatim as its tail.
const COMPACTION_TAIL_WINDOW_FRACTION: f64 = 0.30;