fix: drop the reasoning fields when the route rejects an enabled reasoning level

A structured reasoning-field 400 on an ENABLED config (#100536:
`reasoning.effort: max` on a Responses relay) reached the
reasoning_mandatory rung, which only knows how to drop a disable: for an
enabled config _reasoning_config_for_wire returned the config unchanged,
so the one-shot retry resent the identical request (a guaranteed second
400) under a notice that blamed 'disabling reasoning'.

Record what the wire builder actually sent (agent._wire_reasoning_config)
and, when the rejected request carried an enabled effort, omit the
reasoning fields for the rest of the session — the route default, as the
auxiliary ladder's _without_reasoning_fields already does — under a
'rejects reasoning effort <x>' notice. A rejected disable keeps the
existing contract: the user's own config goes out verbatim on the retry.

Test drives classify_api_error -> recover_after_classification ->
_build_api_kwargs_for_mode and asserts the retry's reasoning_config is
None; control keeps the disable copy and the verbatim resend.
This commit is contained in:
teknium1
2026-09-19 01:19:57 -07:00
Unverified
parent e343fbd28b
commit 64dceb9c26
3 changed files with 93 additions and 0 deletions
+11
View File
@@ -1254,6 +1254,12 @@ def _reasoning_config_for_wire(agent):
"""
cfg = agent.reasoning_config
ephemeral_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 None
if 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 = None
return None
agent._wire_reasoning_config = cfg
return cfg
if 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 = cfg
return cfg
+9
View File
@@ -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_pool
agent._reasoning_disable_rejected = True
try:
from hermes_cli.models_reasoning_caps import refresh_reasoning_caps_async
@@ -9,6 +9,7 @@ from types import SimpleNamespace
from unittest.mock import patch
from 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)