Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(agentic): replay reasoning in repair and report retries

Bingxi Zhao (Frank)ffbe0a541bff
diff --git a/deeptutor/agents/research/pipeline.py b/deeptutor/agents/research/pipeline.py
index 2cb3e914d..bf348c87e 100644
--- a/deeptutor/agents/research/pipeline.py
+++ b/deeptutor/agents/research/pipeline.py
@@ -1860,6 +1860,7 @@ class ResearchPipeline:
last_reason = "empty response"
for attempt in range(1, DEFAULT_REPORT_STEP_MAX_ATTEMPTS + 1):
body = ""
+ step: LabeledStepResult | None = None
try:
step = await self._run_labeled_step(
client=client,
@@ -1932,10 +1933,11 @@ class ResearchPipeline:
)
messages.extend(
[
- {
- "role": "assistant",
- "content": f"``{expected_label}``\n{body}" if body else "",
- },
+ assistant_message(
+ f"``{expected_label}``\n{body}" if body else "",
+ reasoning_content=step.reasoning_content if step else None,
+ thinking_blocks=list(step.thinking_blocks) if step else None,
+ ),
{
"role": "user",
"content": self._t(
diff --git a/deeptutor/runtime/agentic/loop.py b/deeptutor/runtime/agentic/loop.py
index b3337bfc1..e54298c1e 100644
--- a/deeptutor/runtime/agentic/loop.py
+++ b/deeptutor/runtime/agentic/loop.py
@@ -451,9 +451,11 @@ def _append_repair_messages(
provider rejects a history that lost it.
"""
clipped = str(step.text or "").strip()
- if clipped:
+ if clipped or step.reasoning_content or step.thinking_blocks:
if len(clipped) > _REPAIR_PREVIEW_CHARS:
clipped = clipped[:_REPAIR_PREVIEW_CHARS].rstrip() + "\n...[truncated]"
+ # A reasoning-only response has no visible draft, but it still
+ # produced an assistant turn whose provider state belongs in history.
messages.append(
assistant_message(
clipped,
diff --git a/tests/agents/research/test_report_stream_formatting.py b/tests/agents/research/test_report_stream_formatting.py
index 7f7853cd6..693124b3a 100644
--- a/tests/agents/research/test_report_stream_formatting.py
+++ b/tests/agents/research/test_report_stream_formatting.py
@@ -145,6 +145,45 @@ async def test_report_step_retries_an_idle_truncated_response_before_streaming(
assert "Complete section" in live_content
+async def test_report_retry_replays_reasoning_from_incomplete_attempt(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ pipeline = _make_pipeline(monkeypatch)
+ requests: list[list[dict]] = []
+
+ async def fake_run_labeled_step(self, **kwargs):
+ requests.append([dict(message) for message in kwargs["messages"]])
+ if len(requests) == 1:
+ return LabeledStepResult(
+ label=LABEL_SECTION,
+ text="## 2. Partial section",
+ finish_reason="length",
+ reasoning_content="Plan the missing evidence.",
+ )
+ return LabeledStepResult(
+ label=LABEL_SECTION,
+ text=_complete_section_body(2),
+ finish_reason="stop",
+ )
+
+ pipeline._run_labeled_step = types.MethodType(fake_run_labeled_step, pipeline)
+ await pipeline._stream_report_step(
+ system_prompt="system",
+ user_prompt="user",
+ protocol=_PROTOCOL_REPORT_SECTION,
+ stream=StreamBus(),
+ client=None,
+ label="Write section",
+ call_id_root="test-report-reasoning-replay",
+ max_tokens=1000,
+ expected_section_number=2,
+ )
+
+ assistant = next(message for message in requests[1] if message["role"] == "assistant")
+ assert assistant["content"] == "``SECTION``\n## 2. Partial section"
+ assert assistant["reasoning_content"] == "Plan the missing evidence."
+
+
async def test_report_step_rejects_empty_success_after_all_retries(
monkeypatch: pytest.MonkeyPatch,
) -> None:
diff --git a/tests/core/agentic/test_loop_reasoning_replay.py b/tests/core/agentic/test_loop_reasoning_replay.py
index b544633f4..599d5a91c 100644
--- a/tests/core/agentic/test_loop_reasoning_replay.py
+++ b/tests/core/agentic/test_loop_reasoning_replay.py
@@ -234,6 +234,21 @@ async def test_a_repair_round_replays_its_reasoning() -> None:
assert second_request[0]["reasoning_content"] == "Forgot the label."
+async def test_a_reasoning_only_repair_round_replays_its_reasoning() -> None:
+ client = await _run(
+ [
+ [_reasoning_chunk("The answer needs a label.")],
+ [_content_chunk("``FINISH``\ndone")],
+ ]
+ )
+
+ second_request = _assistant_messages(client.calls[1])
+ assert len(second_request) == 1
+ assert second_request[0]["content"] == ""
+ assert second_request[0]["reasoning_content"] == "The answer needs a label."
+
+
@pytest.mark.asyncio
async def test_signed_thinking_blocks_survive_the_round() -> None:
"""Anthropic's blocks are signed, so they cannot be rebuilt from text."""