From ec974bb4268be6f764eb8c794fb6e8a24dc429e8 Mon Sep 17 00:00:00 2001 From: xlx1212 Date: Wed, 5 Aug 2026 05:41:38 +0800 Subject: [PATCH] fix: use proportional safety reserve for context compression trigger Replace fixed 10K safety reserve with a 10% ratio of context window (minimum 10K floor for small windows). This makes compression trigger fire at a consistent ~65% ratio for large-context models (128K-256K) instead of the previous 67-71%, addressing the 'triggers too late' complaint in #1529. Closes #1529 --- .../core/src/agentic/execution/execution_engine.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/crates/assembly/core/src/agentic/execution/execution_engine.rs b/src/crates/assembly/core/src/agentic/execution/execution_engine.rs index 9e60a91ea..14150db76 100644 --- a/src/crates/assembly/core/src/agentic/execution/execution_engine.rs +++ b/src/crates/assembly/core/src/agentic/execution/execution_engine.rs @@ -461,7 +461,8 @@ pub struct ExecutionEngine { } impl ExecutionEngine { - const AUTO_COMPRESSION_SAFETY_RESERVE_TOKENS: usize = 10_000; + const AUTO_COMPRESSION_SAFETY_RESERVE_RATIO: f32 = 0.10; + const AUTO_COMPRESSION_MIN_SAFETY_RESERVE_TOKENS: usize = 10_000; const MAX_COMPRESSION_OVERFLOW_ATTEMPTS: usize = 4; const MAX_MAIN_CONTEXT_OVERFLOW_RECOVERIES: usize = 2; const FINALIZE_AFTER_REPEATED_TOOL_FAILURES_REMINDER: &'static str = "This turn must end now because repeated tool failures have prevented further progress. Ignore any unfinished work. Your task now is to give the user a final answer. Do not call any more tools; any tool call will fail. Respond in plain text only. Summarize what was completed, what failed, the evidence available from the tool results, and the single best next step for the user."; @@ -637,7 +638,9 @@ impl ExecutionEngine { let output_reserve_tokens = configured_max_tokens .map(|value| value as usize) .unwrap_or_else(|| automatic_max_output_tokens(context_window as u32) as usize); - let safety_reserve_tokens = Self::AUTO_COMPRESSION_SAFETY_RESERVE_TOKENS; + let safety_reserve_tokens = + (((context_window as f32) * Self::AUTO_COMPRESSION_SAFETY_RESERVE_RATIO) as usize) + .max(Self::AUTO_COMPRESSION_MIN_SAFETY_RESERVE_TOKENS); let input_limit = context_window.saturating_sub(output_reserve_tokens + safety_reserve_tokens);