Skip to content

The agent that grows with you

fix(compression): a stall-fallback pin replaces the main route, never merges over it

Teknium46caa93680f2
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 0ede13100f5..f1e07b34872 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -3980,28 +3980,6 @@ Summary generation was unavailable, so this is a best-effort deterministic fallb
self.summary_model = "" # empty = use main model
self._clear_compression_failure_cooldown() # no cooldown — retry immediately
- def _apply_summary_route(self, call_kwargs: dict) -> None:
- """Pin the summary route onto ``call_kwargs`` for both compression entry points.
-
- After a fallback, an omitted route is NOT "use the main model": ``call_llm`` re-resolves
- ``auxiliary.compression`` from config — the model that just failed — and a second fallback
- is refused, so every later attempt aborts (#123362). Name the main runtime explicitly.
- """
- if self.summary_model:
- call_kwargs["model"] = self.summary_model
- return
- if not getattr(self, "_summary_model_fallen_back", False):
- return
- for key, value in (
- ("provider", self.provider),
- ("model", self.model),
- ("base_url", self.base_url),
- ("api_key", self.api_key),
- ("api_mode", getattr(self, "api_mode", "")),
- ):
- if value:
- call_kwargs[key] = value
-
def _call_summary_llm(self, prompt: str, prompt_started_at: float) -> str:
"""Issue the single aux summary call; return validated content text.
Raises RuntimeError for empty content or a length-truncated (PARTIAL) summary so the failure
@@ -4018,9 +3996,8 @@ Summary generation was unavailable, so this is a best-effort deterministic fallb
# NO max_tokens: Anthropic/NIM wires forward it and a hard cap truncates summaries
# (thinking models burn it on reasoning). Timeout comes from call_llm config.
}
- self._apply_summary_route(call_kwargs)
- # Pinned route (stall fallback) overrides task routing so the retry leaves the stalled backend.
- call_kwargs.update(_pinned_summary_call_kwargs())
+ # Pinned route (stall fallback) replaces task routing so the retry leaves the stalled backend.
+ self._apply_summary_route(call_kwargs, _pinned_summary_call_kwargs())
# Compression is atomic: protect the in-flight summary call from a mid-turn gateway interrupt.
# Without this, an incoming user message aborts the summary and compression falls back to a degraded
# static marker, losing the real handoff (#23975). Re-entrant: a main-model retry (_generate_summary
diff --git a/agent/context_compressor_summary.py b/agent/context_compressor_summary.py
index b0e38f1ded0..877eb3fec2a 100644
--- a/agent/context_compressor_summary.py
+++ b/agent/context_compressor_summary.py
@@ -27,6 +27,33 @@ def _accepts_keyword_argument(callable_obj: Any, name: str) -> bool:
class SummaryDispatchMixin:
+ def _apply_summary_route(self, call_kwargs: dict, pinned: Optional[Dict[str, Any]] = None) -> None:
+ """Pin the summary route onto ``call_kwargs`` for both compression entry points.
+
+ After a fallback, an omitted route is NOT "use the main model": ``call_llm`` re-resolves
+ ``auxiliary.compression`` from config — the model that just failed — and a second fallback
+ is refused, so every later attempt aborts (#123362). Name the main runtime explicitly.
+ A stall-fallback ``pinned`` route replaces the whole route: merged over the main runtime, the
+ main api_key/base_url would ride into the fallback entry's call (to its host, or instead of it).
+ """
+ if pinned:
+ call_kwargs.update(pinned)
+ return
+ if self.summary_model:
+ call_kwargs["model"] = self.summary_model
+ return
+ if not getattr(self, "_summary_model_fallen_back", False):
+ return
+ for key, value in (
+ ("provider", self.provider),
+ ("model", self.model),
+ ("base_url", self.base_url),
+ ("api_key", self.api_key),
+ ("api_mode", getattr(self, "api_mode", "")),
+ ):
+ if value:
+ call_kwargs[key] = value
+
def _summarize_window(
self, messages: List[Dict[str, Any]], turns_to_summarize: List[Dict[str, Any]], scan: "_HandoffScan",
focus_topic: Optional[str], memory_context: str, bypass_cooldown: bool,
diff --git a/tests/agent/test_compression_aux_fallback_route.py b/tests/agent/test_compression_aux_fallback_route.py
index 1146a1a4ae9..7b6f299dccb 100644
--- a/tests/agent/test_compression_aux_fallback_route.py
+++ b/tests/agent/test_compression_aux_fallback_route.py
@@ -8,13 +8,14 @@ that just failed — so the retry re-fails and later attempts abort without a se
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
-from agent.context_compressor import ContextCompressor
+from agent.context_compressor import ContextCompressor, pin_summary_route
def _compressor():
with patch("agent.context_compressor.get_model_context_length", return_value=100000):
return ContextCompressor(
model="main-model", provider="main-provider", quiet_mode=True,
+ base_url="https://main.example/v1", api_key="sk-MAIN-SECRET",
summary_model_override="quota-exhausted-aux",
)
@@ -42,14 +43,16 @@ def test_access_failed_aux_falls_back_to_named_main_route():
assert (second.kwargs.get("model"), second.kwargs.get("provider")) == ("main-model", "main-provider")
-def test_micro_summarize_also_names_main_route_after_fallback():
+def test_fallen_back_route_names_main_for_micro_and_never_rides_into_a_pin():
compressor = _compressor()
compressor._fallback_to_main_for_compression(_access_error(), "failed")
captured = {}
def _capture(**kwargs):
+ captured.clear()
captured.update(kwargs)
- return SimpleNamespace(choices=[SimpleNamespace(message=SimpleNamespace(content="merged"))])
+ return SimpleNamespace(choices=[SimpleNamespace(
+ message=SimpleNamespace(content="merged " * 20), finish_reason="stop")])
with patch("agent.auxiliary_client.call_llm", side_effect=_capture), patch.object(
compressor, "_build_micro_summary_prompt", return_value=[{"role": "user", "content": "x"}]
@@ -57,3 +60,12 @@ def test_micro_summarize_also_names_main_route_after_fallback():
compressor._micro_summarize_one("an exchange")
assert (captured.get("model"), captured.get("provider")) == ("main-model", "main-provider")
+
+ # A stall-fallback pin replaces the whole route: the main key never reaches another host.
+ pin = {"provider": "custom", "model": "llama3", "base_url": "http://other-host:8080/v1", "api_key": None}
+ with patch("agent.context_compressor.call_llm", side_effect=_capture), pin_summary_route(pin):
+ compressor._call_summary_llm("prompt", 0.0)
+
+ assert {k: captured.get(k) for k in ("provider", "model", "base_url", "api_key")} == {
+ "provider": "custom", "model": "llama3", "base_url": "http://other-host:8080/v1", "api_key": None,
+ }