Skip to content

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

Merge upstream/main into bench-6960-compaction-keeps-task

Steven Enamakel49775fa2ed86
diff --cc crates/openhuman-core/src/agent/tinyagents/harness_context_ladder.rs
index 35ef3f0b50,b2ebda9599..718bc57555
--- a/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder.rs
@@@ -250,6 -236,100 +236,133 @@@ pub(super) fn install_context_ladder
(compression_mw, wrap_up_fired)
}
+ /// Longest verbatim tail kept by a task-state compaction (tokens). Measured
+ /// default for large-window coding agents; small windows keep a fifth of
+ /// the window instead.
+ const TASK_STATE_KEEP_RECENT_TOKENS: u64 = 20_000;
+
+ /// The compression middleware for `strategy`.
+ ///
+ /// `TaskState` (the default) writes a typed task-state checkpoint
+ /// (`TaskStateSummarizer`) over a token-budgeted tail: `min(20k, window/5)`
+ /// recent tokens verbatim, and history folded in chunks of at most 40% of the
+ /// window so a small model never sees more than it can read. `Summary` is the
+ /// free-form `ModelSummarizer` over the policy's last `keep_last` messages.
+ pub(super) fn compression_middleware(
+ policy: tinyagents_harness::summarization::SummarizationPolicy,
+ strategy: crate::config::CompactionStrategy,
+ context_window: Option<u64>,
+ summarizer_model: TurnChatModel,
+ model: &str,
+ ) -> ContextCompressionMiddleware {
+ use tinyagents_harness::summarization::{
+ FaultTolerantCachingSummarizer, ModelSummarizer, TaskStateSummarizer,
+ DEFAULT_TASK_STATE_CHUNK_TOKENS,
+ };
+ let window = context_window.filter(|w| *w > 0);
+ match strategy {
+ crate::config::CompactionStrategy::TaskState => {
+ let keep_recent = window.map_or(TASK_STATE_KEEP_RECENT_TOKENS, |w| {
+ (w / 5).min(TASK_STATE_KEEP_RECENT_TOKENS)
+ });
+ let chunk = window.map_or(DEFAULT_TASK_STATE_CHUNK_TOKENS, |w| {
+ (w * 2 / 5).clamp(4_000, DEFAULT_TASK_STATE_CHUNK_TOKENS)
+ });
+ tracing::info!(
+ model,
+ keep_recent_tokens = keep_recent,
+ max_chunk_tokens = chunk,
+ context_window = ?window,
+ "[context_compression] strategy=task_state"
+ );
+ let summarizer = FaultTolerantCachingSummarizer::new(
+ Box::new(
+ TaskStateSummarizer::new(summarizer_model, model).with_max_chunk_tokens(chunk),
+ ),
+ &policy,
+ );
+ ContextCompressionMiddleware::with_summarizer(policy, Box::new(summarizer))
+ .with_keep_recent_tokens(keep_recent)
+ }
+ crate::config::CompactionStrategy::Summary => {
+ tracing::info!(model, "[context_compression] strategy=summary");
+ let summarizer = FaultTolerantCachingSummarizer::new(
+ Box::new(ModelSummarizer::new(summarizer_model, model)),
+ &policy,
+ );
+ ContextCompressionMiddleware::with_summarizer(policy, Box::new(summarizer))
+ }
+ }
+ }
+
+ /// The summarization policy for this turn, or `None` when compaction has
+ /// nothing to size against.
+ ///
+ /// Normally `min(80% of the model's context window, 350k tokens)` (TinyAgents
+ /// `summarization_policy`). `trigger_override` (the
+ /// `[context].compaction_trigger_tokens` / `OPENHUMAN_COMPACTION_TRIGGER_TOKENS`
+ /// bench knob) pins the trigger to that absolute token count instead, and
+ /// enables compaction even when the window is unknown.
+ pub(super) fn compression_policy(
+ context_window: Option<u64>,
+ trigger_override: Option<u64>,
+ ) -> Option<tinyagents_harness::summarization::SummarizationPolicy> {
+ use tinyagents_harness::summarization::{
+ summarization_policy, SummarizationPolicy, DEFAULT_SUMMARIZE_KEEP_LAST,
+ };
+ let window = context_window.filter(|w| *w > 0);
+ match trigger_override.filter(|t| *t > 0) {
+ Some(tokens) => {
+ tracing::info!(
+ trigger_tokens = tokens,
+ context_window = ?window,
+ "[context_compression] compaction trigger override active"
+ );
+ let base = window
+ .map(summarization_policy)
+ .unwrap_or(SummarizationPolicy {
+ keep_last: DEFAULT_SUMMARIZE_KEEP_LAST,
+ ..SummarizationPolicy::default()
+ });
+ Some(base.with_trigger_override(tokens))
+ }
+ None => window.map(summarization_policy),
+ }
++ .map(|policy| with_turn_aware_tail(policy, window))
++}
++
++/// Share of the context window a compaction keeps verbatim as its tail.
++const COMPACTION_TAIL_WINDOW_FRACTION: f64 = 0.30;
++
++/// Size the kept tail in tokens and pin the turn's user message (#6960).
++///
++/// Split by count (the last eight messages), a long tool-driven turn that
++/// crosses the trigger mid-turn folds its only user message — the assignment
++/// being worked on — into the summary, and the agent loses the task. This
++/// keeps ~30% of the window verbatim instead, capped at half the trigger so a
++/// capped trigger (350k on a 1M window) still has room to fold, and pins the
++/// turn's user message to the front of that tail. With no known window the
++/// tail is 30% of the trigger.
++fn with_turn_aware_tail(
++ mut policy: tinyagents_harness::summarization::SummarizationPolicy,
++ window: Option<u64>,
++) -> tinyagents_harness::summarization::SummarizationPolicy {
++ let trigger = policy.trigger_budget();
++ let keep_tokens = match window {
++ Some(window) => ((window as f64 * COMPACTION_TAIL_WINDOW_FRACTION) as u64).min(trigger / 2),
++ None => (trigger as f64 * COMPACTION_TAIL_WINDOW_FRACTION) as u64,
++ };
++ tracing::debug!(
++ context_window = ?window,
++ trigger,
++ keep_tokens,
++ "[context_compression] token tail with the turn's user message pinned"
++ );
++ policy.keep_recent_tokens = Some(keep_tokens);
++ policy.pin_turn_user_message = true;
++ policy
+ }
+
#[cfg(test)]
#[path = "harness_context_ladder_tests.rs"]
mod tests;
diff --cc crates/openhuman-core/src/agent/tinyagents/harness_context_ladder_tests.rs
index 9815611729,0307333780..83d8e43df6
--- a/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/harness_context_ladder_tests.rs
@@@ -1,16 -1,35 +1,62 @@@
use super::*;
+ use tinyagents_harness::summarization::DEFAULT_SUMMARIZE_KEEP_LAST;
+
+ #[test]
+ fn no_window_and_no_override_installs_no_compression() {
+ assert!(compression_policy(None, None).is_none());
+ assert!(compression_policy(Some(0), None).is_none());
+ assert!(compression_policy(None, Some(0)).is_none());
+ }
+
+ #[test]
+ fn a_known_window_triggers_at_eighty_percent_capped_at_350k() {
+ let policy = compression_policy(Some(200_000), None).expect("policy");
+ assert_eq!(policy.trigger_budget(), 160_000);
+ assert_eq!(policy.keep_last, DEFAULT_SUMMARIZE_KEEP_LAST);
+ // DeepSeek V4 Flash's discovered 1M window compacts at 350k, not ~840k.
+ let policy = compression_policy(Some(1_048_576), None).expect("policy");
+ assert!(policy.trigger_budget().abs_diff(350_000) <= 1);
+ }
+
+ #[test]
+ fn the_override_replaces_the_window_fraction() {
+ let policy = compression_policy(Some(1_000_000), Some(64_000)).expect("policy");
+ assert_eq!(policy.trigger_budget(), 64_000);
+ assert!(policy.exceeds_trigger(64_001));
+ assert!(!policy.exceeds_trigger(63_000));
+ assert_eq!(policy.keep_last, DEFAULT_SUMMARIZE_KEEP_LAST);
+ }
+
+ #[test]
+ fn the_override_works_without_a_known_window() {
+ let policy = compression_policy(None, Some(64_000)).expect("policy");
+ assert_eq!(policy.trigger_budget(), 64_000);
+ assert_eq!(policy.keep_last, DEFAULT_SUMMARIZE_KEEP_LAST);
+ }
+
+/// Issue #6960: a compaction that fires mid-turn must keep the turn's
+/// assignment verbatim and size its tail in tokens, not by message count.
+#[test]
- fn compression_policy_keeps_a_token_tail_and_pins_the_turn_user_message() {
- let policy = compression_policy(200_000);
-
- assert_eq!(policy.context_window, Some(200_000));
- assert_eq!(
- policy.threshold_fraction,
- tinyagents_harness::summarization::DEFAULT_SUMMARIZE_THRESHOLD_FRACTION
- );
++fn the_policy_keeps_a_token_tail_and_pins_the_turn_user_message() {
++ let policy = compression_policy(Some(200_000), None).expect("policy");
++ assert!(policy.pin_turn_user_message);
++ // 30% of the window.
+ assert_eq!(policy.keep_recent_tokens, Some(60_000));
++}
++
++#[test]
++fn the_token_tail_never_exceeds_half_the_trigger() {
++ // A 1M window compacts at 350k; 30% of the window (300k) would leave the
++ // compaction almost nothing to fold.
++ let policy = compression_policy(Some(1_048_576), None).expect("policy");
++ assert_eq!(policy.keep_recent_tokens, Some(policy.trigger_budget() / 2));
++ let policy = compression_policy(Some(1_000_000), Some(64_000)).expect("policy");
++ assert_eq!(policy.keep_recent_tokens, Some(32_000));
++}
++
++#[test]
++fn an_override_without_a_window_still_pins_with_a_token_tail() {
++ let policy = compression_policy(None, Some(64_000)).expect("policy");
+ assert!(policy.pin_turn_user_message);
++ assert_eq!(policy.keep_recent_tokens, Some(19_200));
+}
diff --cc vendor/tinyagents
index e4fbb89235,f1a14339e3..5a1ac17b40
--- a/vendor/tinyagents
+++ b/vendor/tinyagents
@@@ -1,1 -1,1 +1,1 @@@
- Subproject commit e4fbb89235a62d4e757c18da3e9e02b54bf124ba
-Subproject commit f1a14339e3f3a13cdb2d5b7f7b83f6956de8d89b
++Subproject commit 5a1ac17b407ba8fd34cb07e3c7e14869e48426b0