Skip to content

The agent that grows with you

fix(compression): name the main model when aux summary falls back

Anthony Ruiz71448afc55d2
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 0f821b11c34..0ede13100f5 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -3980,6 +3980,28 @@ 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
@@ -3996,8 +4018,7 @@ 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.
}
- if self.summary_model:
- call_kwargs["model"] = self.summary_model
+ 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())
# Compression is atomic: protect the in-flight summary call from a mid-turn gateway interrupt.
diff --git a/agent/micro_compaction.py b/agent/micro_compaction.py
index 5829c118984..7ccb2b71a34 100644
--- a/agent/micro_compaction.py
+++ b/agent/micro_compaction.py
@@ -124,8 +124,7 @@ class MicroCompactionMixin:
"max_tokens": min(1500, self.max_summary_tokens or 1500),
"temperature": 0.1,
}
- if self.summary_model:
- call_kwargs["model"] = self.summary_model
+ self._apply_summary_route(call_kwargs)
if self.model:
call_kwargs.setdefault("main_runtime", {
"model": self.model, "provider": self.provider or "", "base_url": self.base_url or "",
diff --git a/tests/agent/test_compression_auto_summary_fallback.py b/tests/agent/test_compression_auto_summary_fallback.py
index 0894c2beb0f..7d3643ddc6e 100644
--- a/tests/agent/test_compression_auto_summary_fallback.py
+++ b/tests/agent/test_compression_auto_summary_fallback.py
@@ -40,7 +40,7 @@ def test_auto_resolved_summary_model_falls_back_to_main_on_empty_content():
result = c._generate_summary(_msgs())
assert mock_call.call_count == 2 # first auto route failed → retried on main
- assert "model" not in mock_call.call_args_list[1].kwargs
+ assert mock_call.call_args_list[1].kwargs.get("model") == "main-model"
assert result is not None and "summary via main model" in result
# The model that actually failed (the auto-resolved one) is recorded for the user warning.
assert c._last_aux_model_failure_model == "z-ai/glm-5.3"
diff --git a/tests/agent/test_compression_aux_fallback_route.py b/tests/agent/test_compression_aux_fallback_route.py
new file mode 100644
index 00000000000..1146a1a4ae9
--- /dev/null
+++ b/tests/agent/test_compression_aux_fallback_route.py
@@ -0,0 +1,59 @@
+"""The aux-summary fallback must actually reach the main model (#123362).
+
+``_fallback_to_main_for_compression`` clears ``summary_model``, but an omitted route makes
+``call_llm(task="compression")`` re-resolve ``auxiliary.compression`` from config — the model
+that just failed — so the retry re-fails and later attempts abort without a second fallback.
+"""
+
+from types import SimpleNamespace
+from unittest.mock import MagicMock, patch
+
+from agent.context_compressor import ContextCompressor
+
+
+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,
+ summary_model_override="quota-exhausted-aux",
+ )
+
+
+def _access_error():
+ error = Exception("Error code: 403 - model is not available in the current token plan")
+ error.status_code = 403
+ return error
+
+
+def test_access_failed_aux_falls_back_to_named_main_route():
+ ok = MagicMock()
+ ok.choices = [MagicMock()]
+ ok.choices[0].message.content = "summary via main model"
+ compressor = _compressor()
+
+ with patch("agent.context_compressor.call_llm", side_effect=[_access_error(), ok]) as mock_call:
+ summary = compressor._generate_summary([
+ {"role": "user", "content": "do something"}, {"role": "assistant", "content": "ok"},
+ ])
+
+ assert summary is not None and "summary via main model" in summary
+ first, second = mock_call.call_args_list
+ assert first.kwargs.get("model") == "quota-exhausted-aux"
+ assert (second.kwargs.get("model"), second.kwargs.get("provider")) == ("main-model", "main-provider")
+
+
+def test_micro_summarize_also_names_main_route_after_fallback():
+ compressor = _compressor()
+ compressor._fallback_to_main_for_compression(_access_error(), "failed")
+ captured = {}
+
+ def _capture(**kwargs):
+ captured.update(kwargs)
+ return SimpleNamespace(choices=[SimpleNamespace(message=SimpleNamespace(content="merged"))])
+
+ with patch("agent.auxiliary_client.call_llm", side_effect=_capture), patch.object(
+ compressor, "_build_micro_summary_prompt", return_value=[{"role": "user", "content": "x"}]
+ ):
+ compressor._micro_summarize_one("an exchange")
+
+ assert (captured.get("model"), captured.get("provider")) == ("main-model", "main-provider")
diff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.py
index 5d8a9de8c6c..9bf28b1c225 100644
--- a/tests/agent/test_context_compressor.py
+++ b/tests/agent/test_context_compressor.py
@@ -1391,8 +1391,8 @@ class TestSummaryFallbackToMainModel:
assert mock_call.call_count == 2
# First call used the misconfigured aux model
assert mock_call.call_args_list[0].kwargs.get("model") == "broken-aux-model"
- # Second call used the main model (no model kwarg → call_llm uses main)
- assert "model" not in mock_call.call_args_list[1].kwargs
+ # Second call names the main model: an omitted model would re-resolve auxiliary.compression
+ assert mock_call.call_args_list[1].kwargs.get("model") == "main-model"
assert result is not None
assert "summary via main model" in result
# Aux-model failure is recorded even though retry succeeded — this is
@@ -1426,7 +1426,7 @@ class TestSummaryFallbackToMainModel:
assert mock_call.call_count == 2
assert mock_call.call_args_list[0].kwargs.get("model") == "flaky-aux-model"
- assert "model" not in mock_call.call_args_list[1].kwargs
+ assert mock_call.call_args_list[1].kwargs.get("model") == "main-model"
assert result is not None
assert "summary via main model after empty aux" in result
assert c._last_aux_model_failure_model == "flaky-aux-model"
@@ -1489,7 +1489,7 @@ class TestSummaryFallbackToMainModel:
assert mock_call.call_count == 2
assert mock_call.call_args_list[0].kwargs.get("model") == "aux-via-broken-proxy"
- assert "model" not in mock_call.call_args_list[1].kwargs
+ assert mock_call.call_args_list[1].kwargs.get("model") == "main-model"
assert result is not None
assert "summary via main model" in result
# Aux-model failure recorded so /usage / gateway warnings can surface it