The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): step up the reasoning effort when a 400 rejects the level's *value*
Sergey Shabalin5b7a75cbfd36
diff --git a/agent/error_classifier.py b/agent/error_classifier.pyindex 3938594c5c5..addf3917fe8 100644--- a/agent/error_classifier.py+++ b/agent/error_classifier.py@@ -531,19 +531,46 @@ _REASONING_REQUIRED_MARKERS = ("always enabled", "cannot be turned off",)+# Same rejection, wording by *value*: an endpoint that publishes a closed level set refuses the+# thinking-OFF encoding because ``none`` is not in it — "'reasoning_effort' must be one of:+# 'minimal', 'low', 'medium', 'high', 'xhigh', 'max', 'ultra'" (Alibaba token-plan / DashScope+# routes, code ``invalid_parameter_error``). The field is understood and has to stay on the wire;+# only the disabled level is invalid, so the floor step-up (the weakest accepted level) is the+# right recovery. Neither the strip rung nor its "unsupported" wording matches this message, so+# without these markers the title lane fails outright instead of retrying at the floor.+_REASONING_LEVEL_REJECTION_MARKERS = ("must be one of", "invalid value for")+++def is_reasoning_level_rejection(error_msg: str) -> bool:+ """Provider 400 rejecting the *value* of a reasoning wire control against the endpoint's closed+ level set ("'reasoning_effort' must be one of: 'minimal', 'low', …", Alibaba token-plan /+ DashScope routes, code ``invalid_parameter_error``). The reasoning-field token must be present+ next to the wording — a 400 enum complaint about some other field is not this class. The field is+ understood and has to stay on the wire; only the disabled level ("none") is invalid, so the+ recovery is the floor step-up in the auxiliary ladder and the disable-drop in the main loop, not+ the strip rung and not an abort to the fallback chain."""+ msg = (error_msg or "").lower()+ token = _REASONING_FIELD_TOKEN.search(msg)+ if token is None:+ return False+ near = msg[max(0, token.start() - 48):token.end() + 96]+ return any(m in near for m in _REASONING_LEVEL_REJECTION_MARKERS)+def is_reasoning_required_rejection(error_msg: str) -> bool:"""Provider 400 saying the model's reasoning cannot be switched OFF ("Reasoning is mandatory for- this endpoint and cannot be disabled", the Nous Portal on gpt-6-astra). The opposite of- ``is_reasoning_field_rejection``: the field is understood, the *disable* is refused, so the right- reaction is to step the effort up to the lowest level rather than drop the field (a dropped field- also works, but tells the caller nothing about the next call)."""+ this endpoint and cannot be disabled", the Nous Portal on gpt-6-astra) — or refusing the OFF+ *level* because the endpoint's closed set starts at ``minimal``+ (``is_reasoning_level_rejection``). The opposite of ``is_reasoning_field_rejection``: the field is+ understood, the *disable* is refused, so the right reaction is to step the effort up to the+ lowest level rather than drop the field (a dropped field also works, but tells the caller nothing+ about the next call)."""msg = (error_msg or "").lower()token = _REASONING_FIELD_TOKEN.search(msg)if token is None:return Falsenear = msg[max(0, token.start() - 48):token.end() + 96]- return any(m in near for m in _REASONING_REQUIRED_MARKERS)+ return any(m in near for m in _REASONING_REQUIRED_MARKERS) or is_reasoning_level_rejection(msg)def is_reasoning_field_rejection(error_msg: str) -> bool:@@ -1170,10 +1197,14 @@ def _classify_400(c: _Ctx) -> Verdict:):return _V_INVALID_ENCRYPTED# Route rejecting a reasoning disable: a reasoning-mandatory route (GLM-5.3 on Nous Portal /- # OpenRouter) or a chat-only relay that does not accept ``reasoning_effort: none`` at all- # (#114460). Deterministic for the request shape, but the only bad field is the disable — the- # loop drops it and retries once. Must precede request-validation, which would abort as format_error.- if _REASONING_MANDATORY_PATTERN in msg or is_reasoning_field_rejection(msg):+ # OpenRouter), a chat-only relay that does not accept ``reasoning_effort: none`` at all+ # (#114460), or an endpoint whose closed level set starts above the disable ("must be one of:+ # 'minimal', …" — Alibaba token-plan). Deterministic for the request shape, but the only bad+ # value is the disable — the loop drops it (or, when the disable is refused rather than the+ # field, steps the effort up to the floor) and retries once. Must precede request-validation,+ # which would abort as format_error.+ if (_REASONING_MANDATORY_PATTERN in msg or is_reasoning_field_rejection(msg)+ or is_reasoning_level_rejection(msg)):return _V_REASONING_MANDATORY# 400 blaming a field this route never sent (Codex OAuth injects then rejects# prompt_cache_retention ~20% of the time): transient, retry identical request.diff --git a/tests/agent/test_auxiliary_reasoning_floor.py b/tests/agent/test_auxiliary_reasoning_floor.pyindex 2eefef4a6c7..a30cd2cf6da 100644--- a/tests/agent/test_auxiliary_reasoning_floor.py+++ b/tests/agent/test_auxiliary_reasoning_floor.py@@ -74,3 +74,43 @@ def test_field_rejection_still_strips_instead_of_stepping_up():retry = client.chat.completions.create.call_args_list[1].kwargsassert "reasoning_effort" not in retryassert not auxiliary_reasoning_floor._FLOORED_ROUTES+++# Live body from the firstVDS qwencloud route (Alibaba token-plan, deepseek-v4.1-flash, 2026-09-21):+# a closed level set that starts at ``minimal``, so the thinking-off encoding ``none`` is refused by+# value rather than by field name.+_TOKENPLAN_400 = (+ 'Error code: 400 - {\'error\':{\'code\':\'invalid_parameter_error\',\'param\':None,'+ '\'message\':"\'reasoning_effort\' must be one of: \'minimal\', \'low\', \'medium\', \'high\', '+ '\'xhigh\', \'max\', \'ultra\'",\'type\':\'invalid_request_error\'}}'+)+++def test_tokenplan_level_rejection_is_a_reasoning_rejection_not_a_field_rejection():+ """The closed-set wording names the field but blames its value: it must classify as a *required*+ rejection (floor step-up), not as a field rejection (strip) — the endpoint needs the field on the+ wire with an accepted level."""+ from agent.error_classifier import is_reasoning_field_rejection, is_reasoning_required_rejection++ assert is_reasoning_required_rejection(_TOKENPLAN_400)+ assert not is_reasoning_field_rejection(_TOKENPLAN_400)+++def test_tokenplan_level_rejection_steps_up_to_the_floor_and_remembers_the_route():+ """Alibaba token-plan (qwencloud) on deepseek-v4.1-flash: ``none`` → 400 → retry at ``low`` → ok,+ and the next thinking-off aux call on that route starts at the floor."""+ client = MagicMock()+ client.base_url = "https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"+ client.chat.completions.create.side_effect = [+ RuntimeError(_TOKENPLAN_400), {"ok": True}, {"ok": True},+ ]++ assert _call(client) == {"ok": True}+ first, retry = (c.kwargs for c in client.chat.completions.create.call_args_list[:2])+ assert first["reasoning_effort"] == "none"+ assert retry["reasoning_effort"] == auxiliary_reasoning_floor.REASONING_FLOOR_EFFORT+ assert retry["extra_body"]["response_format"] == {"type": "json_object"}++ assert _call(client) == {"ok": True}+ upfront = client.chat.completions.create.call_args_list[2].kwargs+ assert upfront["reasoning_effort"] == auxiliary_reasoning_floor.REASONING_FLOOR_EFFORTdiff --git a/tests/agent/test_error_classifier.py b/tests/agent/test_error_classifier.pyindex a7718912624..ca1928a50bd 100644--- a/tests/agent/test_error_classifier.py+++ b/tests/agent/test_error_classifier.py@@ -940,15 +940,20 @@ class TestClassifyApiError:def test_reasoning_field_rejection_is_reasoning_mandatory(self):"""A 400 rejecting a reasoning wire control by name — reversed ("reasoning_effort 'none'unsupported; use ...", #114460), forward ("Unrecognized request argument supplied:- reasoning_effort"), or an enum rejection whose only field name sits in the structured- 'param' tail (commandcode.ai, #115277) — takes the drop-the-disable rung, not the- format_error abort; a model-id segment (kimi-k2-thinking) stays route gating."""+ reasoning_effort"), an enum rejection whose only field name sits in the structured+ 'param' tail (commandcode.ai, #115277), or a closed-set value rejection that names the+ field but blames its value (Alibaba token-plan: "'reasoning_effort' must be one of:+ 'minimal', ...") — takes the drop-the-disable / step-up rung, not the format_error abort;+ a model-id segment (kimi-k2-thinking) stays route gating."""for msg in ("Error code: 400 - reasoning_effort 'none' unsupported; use minimal|low|medium|high|xhigh","Unrecognized request argument supplied: reasoning_effort","Error code: 400 - {'error': {'message': 'Invalid option: expected one of ""\"low\"|\"medium\"|\"high\"|\"xhigh\"|\"max\"', 'type': 'invalid_request_error', ""'param': 'reasoning_effort'}}",+ "Error code: 400 - {'error':{'code':'invalid_parameter_error','param':None,"+ "'message':\"'reasoning_effort' must be one of: 'minimal', 'low', 'medium', 'high', "+ "'xhigh', 'max', 'ultra'\",'type':'invalid_request_error'}}",):result = classify_api_error(MockAPIError(msg, status_code=400), provider="custom", model="m")assert result.reason == FailoverReason.reasoning_mandatory, msg