Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions deeptutor/agents/research/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
)
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,
Expand Down Expand Up @@ -95,6 +96,7 @@
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
Expand Down Expand Up @@ -2285,14 +2287,17 @@ def _build_client(self) -> Any:
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,
)

Expand Down
7 changes: 4 additions & 3 deletions deeptutor/services/llm/reasoning_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# 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",
Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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",
]
74 changes: 74 additions & 0 deletions tests/agents/research/test_outline_reasoning_retry.py
Original file line number Diff line number Diff line change
@@ -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"]
Loading