The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: drop the reasoning fields when the route rejects an enabled reasoning level
teknium164dceb9c2632
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex f1a9d4541af..338274a3504 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -1254,6 +1254,12 @@ def _reasoning_config_for_wire(agent):"""cfg = agent.reasoning_configephemeral_off = _consume_ephemeral_reasoning_off(agent)+ if getattr(agent, "_reasoning_effort_rejected", False):+ # The route rejected the configured reasoning LEVEL itself (#100536: ``reasoning.effort:+ # max`` on an enabled config). Omit the reasoning fields for the rest of the session —+ # the route default — as the auxiliary ladder does; resending would 400 identically.+ agent._wire_reasoning_config = None+ return Noneif getattr(agent, "_reasoning_disable_rejected", False):# The route rejects disables. Resend exactly what the session has# been sending — the user's own config — so the retry lands on the@@ -1263,10 +1269,15 @@ def _reasoning_config_for_wire(agent):if isinstance(cfg, dict) and (cfg.get("enabled") is False or cfg.get("effort") == "none"):+ agent._wire_reasoning_config = Nonereturn None+ agent._wire_reasoning_config = cfgreturn cfgif ephemeral_off:cfg = {**(cfg or {}), "enabled": False, "effort": "none"}+ # What actually went out: the reasoning-rejection rung reads it to tell a rejected+ # disable (drop the disable) from a rejected level (drop the reasoning fields).+ agent._wire_reasoning_config = cfgreturn cfgdiff --git a/agent/turn_recovery.py b/agent/turn_recovery.pyindex 008b3b3649f..4ebd362e59d 100644--- a/agent/turn_recovery.py+++ b/agent/turn_recovery.py@@ -608,6 +608,15 @@ def recover_after_classification(and not _retry.reasoning_mandatory_retry_attempted):_retry.reasoning_mandatory_retry_attempted = True+ sent = getattr(agent, "_wire_reasoning_config", None)+ if isinstance(sent, dict) and sent.get("enabled") is not False and sent.get("effort") not in (None, "none"):+ # The rejected request carried an ENABLED config: the route refuses that reasoning+ # level (#100536: ``reasoning.effort: max`` on a Responses relay). Dropping a disable+ # would resend the identical request; omit the reasoning fields instead (route default).+ agent._reasoning_effort_rejected = True+ _vlines(agent, f"⚠️ {agent.model} rejects reasoning effort {sent['effort']} — using the route's default for this session, retrying...")+ logger.warning("%sReasoning-effort recovery: dropping reasoning config for %s", agent.log_prefix, agent.model)+ return True, recovered_with_poolagent._reasoning_disable_rejected = Truetry:from hermes_cli.models_reasoning_caps import refresh_reasoning_caps_asyncdiff --git a/tests/agent/test_reasoning_disable_recovery.py b/tests/agent/test_reasoning_disable_recovery.pyindex 081a1c087f7..eacf2936ec1 100644--- a/tests/agent/test_reasoning_disable_recovery.py+++ b/tests/agent/test_reasoning_disable_recovery.py@@ -9,6 +9,7 @@ from types import SimpleNamespacefrom unittest.mock import patchfrom agent.error_classifier import FailoverReason, classify_api_error+from agent.turn_retry_state import TurnRetryState_REVERSED_400 = "reasoning_effort 'none' unsupported; use minimal|low|medium|high|xhigh"@@ -88,3 +89,75 @@ def test_spent_disable_drop_falls_back_instead_of_replaying_the_request():agent, verdict = _settle(disable_drop_attempted=False)assert verdict.action == "fallthrough"assert agent.activated == []+++# #100536: a Responses relay rejects an unsupported reasoning LEVEL (``reasoning.effort: max`` on+# an ENABLED config) with a message-less structured 400. The classifier routes it to+# reasoning_mandatory; the rung must drop the effort for the retry instead of resending it.+_STRUCTURED_LEVEL_400 = (+ "Error code: 400 - {'error': {'param': 'reasoning.effort', "+ "'error_code': 'invalid_reasoning_effort', 'retryable': False}}"+)+++class _WireAgent(_Agent):+ api_mode = "chat_completions"+ base_url = "http://relay.example/v1"+ request_overrides = None++ def __init__(self, reasoning_config):+ super().__init__()+ self.reasoning_config = reasoning_config+ self.notices = []+ # ``_Agent.__getattr__`` fabricates callables for unknown names; wire flags must read False.+ self._ephemeral_reasoning_off = False+ self._reasoning_effort_rejected = False+ self._fast_until = 0.0+ self.service_tier = None++ def _vprint(self, line, **kwargs):+ self.notices.append(line)+++def _wire_reasoning_config(agent):+ """What the main-loop request builder hands the transport as ``reasoning_config``."""+ from agent.chat_completion_helpers import _build_api_kwargs_for_mode++ def _capture(agent, api_messages, tools_for_api, reasoning_config, request_overrides, cache_scope_id):+ return {"reasoning_config": reasoning_config}++ with patch("agent.chat_completion_helpers._build_chat_completions_kwargs", _capture):+ return _build_api_kwargs_for_mode(agent, [], [])["reasoning_config"]+++def _recover(agent, message):+ from agent.turn_recovery import recover_after_classification++ err = _FakeApiError(400, message)+ classified = classify_api_error(err, provider="custom", model=agent.model)+ assert classified.reason == FailoverReason.reasoning_mandatory+ retry = TurnRetryState()+ with patch("hermes_cli.models_reasoning_caps.refresh_reasoning_caps_async", lambda provider: None):+ retry_now, _ = recover_after_classification(+ agent, err, classified, retry, status_code=400, error_context={}, messages=[], api_messages=[],+ )+ assert retry_now is True+ return agent+++def test_rejected_reasoning_level_retries_without_the_effort():+ agent = _WireAgent({"enabled": True, "effort": "max"})+ assert _wire_reasoning_config(agent) == {"enabled": True, "effort": "max"} # the request that 400ed+ _recover(agent, _STRUCTURED_LEVEL_400)+ assert _wire_reasoning_config(agent) is None, "retry must not resend reasoning.effort=max"+ assert any("rejects reasoning effort max" in n for n in agent.notices)+ assert not any("rejects disabling reasoning" in n for n in agent.notices)++ # Control: a rejected DISABLE keeps the existing contract — the user's own enabled config+ # goes out verbatim on the retry and the notice names the disable.+ agent = _WireAgent({"enabled": True, "effort": "high"})+ agent._ephemeral_reasoning_off = True+ assert _wire_reasoning_config(agent) == {"enabled": False, "effort": "none"} # the request that 400ed+ _recover(agent, _REVERSED_400)+ assert _wire_reasoning_config(agent) == {"enabled": True, "effort": "high"}+ assert any("rejects disabling reasoning" in n for n in agent.notices)