From 858b0d863e621f169e231e675c05ce60599a6247 Mon Sep 17 00:00:00 2001 From: sanil-23 Date: Sun, 22 Nov 2026 18:03:27 +0530 Subject: [PATCH] fix(agent_loop): resend a refused reasoning-off call with the provider's default Some provider endpoints cannot run without reasoning and refuse a request sent with ReasoningEffort::None ("HTTP 400: Reasoning is mandatory for this endpoint and cannot be disabled."). The refusal ended the run. With RunPolicy::truncated_empty_reasoning_off the call after a dead one goes out with reasoning off on whichever endpoint a router picks, so a run died whenever that endpoint was one of these. The refused request now goes out once more with no reasoning setting. It is not counted as a retry and cannot repeat. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 10 ++ .../src/agent_loop/README.md | 8 + .../src/agent_loop/model_call.rs | 46 ++++++ .../model_call_reasoning_refused_tests.rs | 156 ++++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 crates/tinyagents-harness/src/agent_loop/model_call_reasoning_refused_tests.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index a41c351dd..9d820ad4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,16 @@ during the 2.x migration are no longer part of the TinyAgents workspace. The unreleased entries below describe the changes accumulated before this major release. +## Unreleased: a refused reasoning-off call is sent again + +A provider endpoint that cannot run without reasoning refuses a request sent +with `ReasoningEffort::None` (`HTTP 400: Reasoning is mandatory for this +endpoint and cannot be disabled.`). That refusal used to end the run. The +request now goes out once more with no reasoning setting, so the provider's +default applies. It mattered most with `RunPolicy::truncated_empty_reasoning_off`, +which turns reasoning off for the call after a dead one on whichever endpoint a +router picks next. + ## Unreleased: tool hook denials answer the model **Breaking (behaviour).** A `tinyagents-hooks` tool-hook `Deny` no longer diff --git a/crates/tinyagents-harness/src/agent_loop/README.md b/crates/tinyagents-harness/src/agent_loop/README.md index 913078cbb..fe990b70a 100644 --- a/crates/tinyagents-harness/src/agent_loop/README.md +++ b/crates/tinyagents-harness/src/agent_loop/README.md @@ -176,6 +176,14 @@ threshold applies independently of the one-call setting. These controls can combine with constant-cap retries to avoid spending a larger budget on the same reasoning-only failure. +Some provider endpoints cannot run without reasoning and refuse a request +that turns it off (OpenRouter relays `HTTP 400: Reasoning is mandatory for +this endpoint and cannot be disabled.`). Any request sent with +`ReasoningEffort::None` that is refused this way, whether these controls or +the caller turned reasoning off, goes out once more with no reasoning setting, +so the provider's own default applies. The resend is not a retry: it is not +counted against `RunPolicy::retry` and emits no `RetryScheduled`. + A hosted reasoning model on a high effort setting can deliberate past the boosted budget too. Once the retries are spent the loop does not finish on the blank reply: up to `RunPolicy::truncated_empty_nudges` times (default `1`) it diff --git a/crates/tinyagents-harness/src/agent_loop/model_call.rs b/crates/tinyagents-harness/src/agent_loop/model_call.rs index 1ab0c96ca..3e260e2a9 100644 --- a/crates/tinyagents-harness/src/agent_loop/model_call.rs +++ b/crates/tinyagents-harness/src/agent_loop/model_call.rs @@ -857,6 +857,23 @@ impl AgentHarness { match attempt_result { Ok(response) => break Ok(response), Err(error) => { + // A provider that cannot run without reasoning + // refuses a request that turns it off. No retry of + // the same request can pass and no fallback is owed: + // the request goes out once more with the provider's + // own reasoning default, which is the only way that + // endpoint answers. The attempt is not counted + // against the retry policy, and it cannot repeat, + // since the request no longer turns reasoning off. + if reasoning_off_refused(&attempt_request, &error) { + tracing::warn!( + call_id = %call_id.as_str(), + model = %current_name, + "[agent_loop] provider refused reasoning off; resending with its default" + ); + attempt_request.reasoning = None; + continue; + } // Provider and run-budget refusals are terminal: a // retry or another model cannot make the exhausted // budget available again. @@ -2218,6 +2235,35 @@ fn retarget_request_model(request: &mut ModelRequest, name: &str) { } } +/// Whether `error` is a provider refusing `request` because the request +/// turns reasoning off and that provider's endpoint cannot run without it +/// (OpenRouter relays this as HTTP 400, "Reasoning is mandatory for this +/// endpoint and cannot be disabled."). Read from the message, as +/// `classify_provider_failure` reads the others: no provider gives the +/// refusal a code of its own. A request that leaves reasoning alone, or asks +/// for some, is never one of these whatever the message says. +fn reasoning_off_refused(request: &ModelRequest, error: &TinyAgentsError) -> bool { + let turned_off = request.reasoning.as_ref().is_some_and(|reasoning| { + reasoning.effort == Some(tinyinference_llm::model::ReasoningEffort::None) + }); + if !turned_off { + return false; + } + let message = match error { + TinyAgentsError::Provider(error) => error.message.to_ascii_lowercase(), + TinyAgentsError::Model(message) => message.to_ascii_lowercase(), + _ => return false, + }; + message.contains("reasoning") + && ["mandatory", "cannot be disabled", "can't be disabled"] + .iter() + .any(|refusal| message.contains(refusal)) +} + #[cfg(test)] #[path = "model_call_reasoning_policy_tests.rs"] mod reasoning_policy_tests; + +#[cfg(test)] +#[path = "model_call_reasoning_refused_tests.rs"] +mod reasoning_refused_tests; diff --git a/crates/tinyagents-harness/src/agent_loop/model_call_reasoning_refused_tests.rs b/crates/tinyagents-harness/src/agent_loop/model_call_reasoning_refused_tests.rs new file mode 100644 index 000000000..09e8117c6 --- /dev/null +++ b/crates/tinyagents-harness/src/agent_loop/model_call_reasoning_refused_tests.rs @@ -0,0 +1,156 @@ +//! A provider endpoint that cannot run without reasoning refuses a request +//! that turns it off. The call is sent again with the provider's own default +//! instead of failing the run. + +use super::*; + +use std::sync::{Arc, Mutex}; + +use async_trait::async_trait; +use tinyinference_llm::message::Message; +use tinyinference_llm::model::{ + ChatModel, ModelRequest, ModelResponse, ProviderError, ReasoningConfig, ReasoningEffort, +}; + +use crate::retry::RetryPolicy; +use crate::runtime::{AgentHarness, RunPolicy}; + +const REFUSAL: &str = "Reasoning is mandatory for this endpoint and cannot be disabled."; + +/// Answers every request but one that turns reasoning off, which it refuses +/// with `refusal` as HTTP 400. Records each request it was sent. +struct ReasoningRequired { + refusal: &'static str, + requests: Mutex>, +} + +impl ReasoningRequired { + fn refusing_with(refusal: &'static str) -> Arc { + Arc::new(Self { + refusal, + requests: Mutex::new(Vec::new()), + }) + } + + fn efforts(&self) -> Vec> { + let requests = self.requests.lock().unwrap(); + requests + .iter() + .map(|request| request.reasoning.as_ref().and_then(|r| r.effort)) + .collect() + } +} + +#[async_trait] +impl ChatModel<()> for ReasoningRequired { + async fn invoke( + &self, + _state: &(), + request: ModelRequest, + ) -> tinyinference_llm::Result { + let off = request + .reasoning + .as_ref() + .is_some_and(|reasoning| reasoning.effort == Some(ReasoningEffort::None)); + self.requests.lock().unwrap().push(request); + if off { + return Err(tinyinference_llm::Error::Provider(Box::new( + ProviderError { + provider: "test".into(), + status: Some(400), + message: self.refusal.into(), + retryable: false, + ..ProviderError::default() + }, + ))); + } + Ok(ModelResponse::assistant("answered")) + } +} + +/// A harness on `model` alone that sends every call at `effort`, with no +/// retry of its own. +fn harness(model: &Arc, effort: ReasoningEffort) -> AgentHarness<()> { + let mut harness: AgentHarness<()> = AgentHarness::new(); + harness.with_model_resolver(Arc::new(crate::host::FixedModelResolver::named( + "primary", + model.clone(), + ))); + harness.with_policy(RunPolicy { + default_reasoning: Some(ReasoningConfig::effort(effort)), + retry: RetryPolicy::default() + .with_max_attempts(1) + .with_backoff_sleep(false), + ..RunPolicy::default() + }); + harness +} + +#[tokio::test] +async fn a_refused_reasoning_off_call_is_sent_again_with_the_providers_default() { + let model = ReasoningRequired::refusing_with(REFUSAL); + let run = harness(&model, ReasoningEffort::None) + .invoke_default(&(), vec![Message::user("hi")]) + .await + .expect("the refusal is answered by resending, not by failing the run"); + assert_eq!(run.text(), Some("answered".to_string())); + assert_eq!( + model.efforts(), + [Some(ReasoningEffort::None), None], + "sent once more, with no reasoning setting" + ); +} + +#[tokio::test] +async fn another_refusal_of_a_reasoning_off_call_still_fails() { + let model = ReasoningRequired::refusing_with("Invalid request: unknown parameter."); + let error = harness(&model, ReasoningEffort::None) + .invoke_default(&(), vec![Message::user("hi")]) + .await + .expect_err("a 400 about something else is not resent"); + assert!( + matches!(error, TinyAgentsError::Provider(_)), + "got {error:?}" + ); + assert_eq!(model.efforts(), [Some(ReasoningEffort::None)]); +} + +#[tokio::test] +async fn a_call_that_keeps_its_reasoning_is_sent_once() { + let model = ReasoningRequired::refusing_with(REFUSAL); + let run = harness(&model, ReasoningEffort::Low) + .invoke_default(&(), vec![Message::user("hi")]) + .await + .expect("a call that asks for reasoning is answered"); + assert_eq!(run.text(), Some("answered".to_string())); + assert_eq!(model.efforts(), [Some(ReasoningEffort::Low)]); +} + +#[test] +fn only_a_reasoning_off_request_refused_for_it_counts() { + let off = ModelRequest { + reasoning: Some(ReasoningConfig::effort(ReasoningEffort::None)), + ..ModelRequest::default() + }; + let on = ModelRequest { + reasoning: Some(ReasoningConfig::effort(ReasoningEffort::High)), + ..ModelRequest::default() + }; + // As a gateway relays it in a plain model error, and as a provider error. + let relayed = TinyAgentsError::Model(format!("ephemeral-route returned HTTP 400: {REFUSAL}")); + let provider = TinyAgentsError::Provider(Box::new(ProviderError { + status: Some(400), + message: REFUSAL.into(), + ..ProviderError::default() + })); + assert!(reasoning_off_refused(&off, &relayed)); + assert!(reasoning_off_refused(&off, &provider)); + assert!( + !reasoning_off_refused(&on, &relayed), + "it asked for reasoning" + ); + assert!(!reasoning_off_refused(&ModelRequest::default(), &relayed)); + let other = TinyAgentsError::Model("HTTP 400: reasoning effort `none` is not a level".into()); + assert!(!reasoning_off_refused(&off, &other)); + assert!(!reasoning_off_refused(&off, &TinyAgentsError::Cancelled)); +}