From 9e5a4e64c952a66f034dad934e854e376af04768 Mon Sep 17 00:00:00 2001 From: Oikantik Basu <140976895+Golden007-prog@users.noreply.github.com> Date: Tue, 1 Dec 2026 07:04:04 +0530 Subject: [PATCH] fix: preserve thinking-off settings on research outline retries (#3134) --- deeptutor/agents/research/pipeline.py | 11 ++- deeptutor/services/llm/reasoning_params.py | 7 +- .../research/test_outline_reasoning_retry.py | 74 +++++++++++++++++++ 3 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 tests/agents/research/test_outline_reasoning_retry.py diff --git a/deeptutor/agents/research/pipeline.py b/deeptutor/agents/research/pipeline.py index 1d77830b0..ef7560165 100644 --- a/deeptutor/agents/research/pipeline.py +++ b/deeptutor/agents/research/pipeline.py @@ -60,6 +60,7 @@ from deeptutor.agents.research.data_structures import ( ) from deeptutor.agents.research.utils.citation_manager import CitationManager from deeptutor.core.context import Attachment, UnifiedContext +from deeptutor.core.reasoning_efforts import normalize_reasoning_effort from deeptutor.core.trace import ( build_trace_metadata, derive_trace_metadata, @@ -95,6 +96,7 @@ from deeptutor.services.llm import ( get_llm_config, prepare_multimodal_messages, ) +from deeptutor.services.llm.reasoning_params import RETRY_REASONING_EFFORT, THINKING_OFF_EFFORTS from deeptutor.services.llm.structured_retry import payload_with_reasoning_retry from deeptutor.services.prompt import get_prompt_manager from deeptutor.services.prompt.language import append_language_directive @@ -2285,14 +2287,17 @@ class ResearchPipeline: def _completion_kwargs( self, max_tokens: int, reasoning_effort: str | None = None ) -> dict[str, Any]: + # A structured retry may lower thinking, but must not turn it back on. + if ( + reasoning_effort == RETRY_REASONING_EFFORT + and normalize_reasoning_effort(self.reasoning_effort) in THINKING_OFF_EFFORTS + ): + reasoning_effort = None return build_completion_kwargs( temperature=self._temperature, model=self.model, max_tokens=max_tokens, binding=self.binding, - # A step may turn thinking down for one call (a retry after the - # model spent the whole budget on it); otherwise the configured - # level stands. reasoning_effort=reasoning_effort or self.reasoning_effort, ) diff --git a/deeptutor/services/llm/reasoning_params.py b/deeptutor/services/llm/reasoning_params.py index 25d432975..b6391c34c 100644 --- a/deeptutor/services/llm/reasoning_params.py +++ b/deeptutor/services/llm/reasoning_params.py @@ -19,7 +19,7 @@ _THINKING_STYLE_MAP = { # response. ``minimal``/``minimum`` are retained as off sentinels for # provider-native thinking controls for backwards compatibility; OpenRouter # receives the canonical ``none`` value below. -_THINKING_OFF_EFFORTS = frozenset({"none", "minimal", "minimum"}) +THINKING_OFF_EFFORTS = frozenset({"none", "minimal", "minimum"}) _PROVIDER_THINKING_STYLES = { "deepseek": "thinking_type", "volcengine": "thinking_type", @@ -251,7 +251,7 @@ def build_openai_compatible_reasoning_kwargs( if resolved_effort: suppress_top_level = bool( thinking_style - and (semantic_effort in _THINKING_OFF_EFFORTS or thinking_style == "enable_thinking") + and (semantic_effort in THINKING_OFF_EFFORTS or thinking_style == "enable_thinking") ) if not suppress_top_level and not ( provider_name == "openrouter" and semantic_effort == "none" @@ -263,7 +263,7 @@ def build_openai_compatible_reasoning_kwargs( and resolved_effort is not None and not (provider_name == "openrouter" and semantic_effort == "none") ): - thinking_enabled = semantic_effort not in _THINKING_OFF_EFFORTS + thinking_enabled = semantic_effort not in THINKING_OFF_EFFORTS extra = _THINKING_STYLE_MAP.get(thinking_style, lambda _enabled: None)(thinking_enabled) if extra: kwargs.setdefault("extra_body", {}).update(extra) @@ -277,6 +277,7 @@ def build_openai_compatible_reasoning_kwargs( __all__ = [ "RETRY_REASONING_EFFORT", + "THINKING_OFF_EFFORTS", "build_openai_compatible_reasoning_kwargs", "default_reasoning_effort_for", ] diff --git a/tests/agents/research/test_outline_reasoning_retry.py b/tests/agents/research/test_outline_reasoning_retry.py new file mode 100644 index 000000000..305096f33 --- /dev/null +++ b/tests/agents/research/test_outline_reasoning_retry.py @@ -0,0 +1,74 @@ +"""Structured research retries should respect the turn's reasoning choice.""" + +from types import SimpleNamespace +from unittest.mock import AsyncMock + +import pytest + +from deeptutor.agents.research.pipeline import ResearchPipeline +from deeptutor.agents.research.utils.citation_manager import CitationManager +from deeptutor.core.context import UnifiedContext +from deeptutor.runtime.stream_bus import StreamBus + + +async def _outline_stream(payload: str): + yield SimpleNamespace( + choices=[ + SimpleNamespace( + delta=SimpleNamespace(content=f"``OUTLINE``\n{payload}", tool_calls=None), + finish_reason="stop", + ) + ], + usage=None, + ) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("binding", "model", "effort"), + [ + ("dashscope", "qwen-plus", "none"), + ("dashscope", "qwen-plus", "minimal"), + ("dashscope", "qwen-plus", "minimum"), + ("openai", "gpt-5", "high"), + ], +) +@pytest.mark.parametrize("stage", ["decompose", "report_outline"]) +async def test_outline_retry_respects_turn_reasoning_effort( + monkeypatch, tmp_path, binding, model, effort, stage +): + config = SimpleNamespace(binding=binding, model=model, reasoning_effort=effort) + monkeypatch.setattr("deeptutor.agents.research.pipeline.get_llm_config", lambda: config) + pipeline = ResearchPipeline(language="en", runtime_config={}) + payload = ( + '[{"title": "Recovered outline"}]' + if stage == "decompose" + else '{"sections": [{"title": "Recovered outline"}]}' + ) + create = AsyncMock(side_effect=[_outline_stream("{}"), _outline_stream(payload)]) + client = SimpleNamespace(chat=SimpleNamespace(completions=SimpleNamespace(create=create))) + stream = StreamBus() + + if stage == "decompose": + items = await pipeline._decompose( + topic="Topic", + context=UnifiedContext(session_id="research-s1", user_message="Topic"), + stream=stream, + client=client, + ) + else: + outline = await pipeline._gen_report_outline( + topic="Topic", + blocks=[], + citations=CitationManager("research-s1", cache_dir=tmp_path), + stream=stream, + client=client, + ) + items = outline.sections + + assert [item.title for item in items] == ["Recovered outline"] + requests = [call.kwargs for call in create.await_args_list] + if binding == "dashscope": + assert [request["extra_body"]["enable_thinking"] for request in requests] == [False, False] + else: + assert [request["reasoning_effort"] for request in requests] == ["high", "low"]