Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions agent/context_compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,17 +903,23 @@ def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topi
or "does not exist" in _err_str
or "no available channel" in _err_str
)
_is_rate_limited = (
_status == 413
or "rate limit" in _err_str
or "tpm" in _err_str
or "tokens per minute" in _err_str
)
if (
_is_model_not_found
and self.summary_model
and self.summary_model != self.model
and not getattr(self, "_summary_model_fallen_back", False)
and (not self.summary_model or self.summary_model != self.model)
):
self._summary_model_fallen_back = True
_had_aux_model = bool(self.summary_model)
logging.warning(
"Summary model '%s' not available (%s). "
"Falling back to main model '%s' for compression.",
self.summary_model, e, self.model,
self.summary_model or "(default)", e, self.model,
)
# Record the aux-model failure so callers can warn the user
# even if the retry-on-main succeeds — a misconfigured aux
Expand All @@ -922,8 +928,15 @@ def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topi
if len(_err_text) > 220:
_err_text = _err_text[:217].rstrip() + "..."
self._last_aux_model_failure_error = _err_text
self._last_aux_model_failure_model = self.summary_model
self.summary_model = "" # empty = use main model
self._last_aux_model_failure_model = self.summary_model or "(default)"
# When summary_model was explicitly set to a different model,
# clear it so the retry uses the default (main) provider.
# When it was empty (no override configured), the default
# provider may differ from self.model — set it explicitly.
if _had_aux_model:
self.summary_model = ""
else:
self.summary_model = self.model
self._summary_failure_cooldown_until = 0.0 # no cooldown
return self._generate_summary(turns_to_summarize, focus_topic=focus_topic) # retry immediately

Expand All @@ -937,15 +950,18 @@ def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topi
# aggregator rejections, etc.) where auto-retry is still safer
# than dropping the turns.
if (
self.summary_model
and self.summary_model != self.model
and not getattr(self, "_summary_model_fallen_back", False)
not getattr(self, "_summary_model_fallen_back", False)
and (
(self.summary_model and self.summary_model != self.model)
or (not self.summary_model and (_is_rate_limited or _is_model_not_found))
)
):
self._summary_model_fallen_back = True
_had_aux_model = bool(self.summary_model)
logging.warning(
"Summary model '%s' failed (%s). "
"Retrying on main model '%s' before giving up.",
self.summary_model, e, self.model,
self.summary_model or "(default)", e, self.model,
)
# Record the aux-model failure (see 404 branch above) — user
# should know their configured model is broken even if main
Expand All @@ -954,8 +970,11 @@ def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topi
if len(_err_text) > 220:
_err_text = _err_text[:217].rstrip() + "..."
self._last_aux_model_failure_error = _err_text
self._last_aux_model_failure_model = self.summary_model
self.summary_model = "" # empty = use main model
self._last_aux_model_failure_model = self.summary_model or "(default)"
if _had_aux_model:
self.summary_model = ""
else:
self.summary_model = self.model
self._summary_failure_cooldown_until = 0.0
return self._generate_summary(turns_to_summarize, focus_topic=focus_topic)

Expand Down
38 changes: 38 additions & 0 deletions tests/agent/test_context_compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,44 @@ def test_fallback_only_happens_once_per_compressor(self):
assert result is None
assert c._summary_model_fallen_back is True

def test_empty_summary_model_413_falls_back_to_main(self):
"""When summary_model_override is None (default), self.summary_model
is empty. If the default provider returns a 413 rate-limit error,
the compressor should fall back to the main model explicitly."""
mock_ok = MagicMock()
mock_ok.choices = [MagicMock()]
mock_ok.choices[0].message.content = "summary via main model"

err_413 = Exception("413 TPM exhausted: rate limit exceeded")
err_413.status_code = 413

with patch("agent.context_compressor.get_model_context_length", return_value=100000):
c = ContextCompressor(
model="main-model",
summary_model_override=None, # default — no aux model
quiet_mode=True,
)

# summary_model should be empty when no override is set
assert c.summary_model == ""

with patch(
"agent.context_compressor.call_llm",
side_effect=[err_413, mock_ok],
) as mock_call:
result = c._generate_summary(self._msgs())

# Should retry: first call fails, second succeeds on main model
assert mock_call.call_count == 2
# Second call should explicitly use the main model
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 with "(default)" placeholder
assert c._last_aux_model_failure_model == "(default)"
assert c._last_aux_model_failure_error is not None
assert "413" in c._last_aux_model_failure_error


class TestAuxModelFallbackSurfacedToCallers:
"""When summary_model fails but retry-on-main succeeds, compress() must
Expand Down
Loading