Skip to content

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

feat: add cross-turn cumulative failure gate for stage degradation

unknown4e072537bc33
diff --git a/deeptutor/capabilities/guided_learning.py b/deeptutor/capabilities/guided_learning.py
index a574f6051..a88d45859 100644
--- a/deeptutor/capabilities/guided_learning.py
+++ b/deeptutor/capabilities/guided_learning.py
@@ -310,6 +310,14 @@ class GuidedLearningCapability(BaseCapability):
timeout: float | None = None,
) -> str | None:
"""Call LLM with bounded retry. Returns None if all retries exhausted."""
+ # Cross-turn cumulative failure gate
+ if progress.stage_failure_counts.get(stage_name, 0) >= self._STAGE_MAX_CUMULATIVE_FAILURES:
+ await stream.content(
+ f"阶段 {stage_name} 已多次失败,跳过。",
+ source=self.manifest.name,
+ metadata={"type": "stage_skipped", "stage": stage_name},
+ )
+ return None
for attempt in range(self._STAGE_MAX_FAILURES):
try:
return await self._call_llm_with_timeout(system_prompt, user_message, timeout=timeout)
@@ -570,6 +578,7 @@ class GuidedLearningCapability(BaseCapability):
_RAG_TIMEOUT_SECONDS = 10
_LLM_CHAIN_TIMEOUT_SECONDS = 60
_STAGE_MAX_FAILURES = 2
+ _STAGE_MAX_CUMULATIVE_FAILURES = 4
_ERROR_DIAGNOSIS_TIMEOUT_SECONDS = 45
def _advance_after_kp(self, progress: LearningProgress, kps: list) -> None:
diff --git a/deeptutor/learning/tests/test_timeout_degradation.py b/deeptutor/learning/tests/test_timeout_degradation.py
index 4b4cc7c99..541ad34d0 100644
--- a/deeptutor/learning/tests/test_timeout_degradation.py
+++ b/deeptutor/learning/tests/test_timeout_degradation.py
@@ -272,3 +272,45 @@ async def test_error_diagnosis_records_failure_count():
assert progress.current_stage == LearningStage.MODULE_TEST
assert progress.stage_failure_counts.get("error_diagnosis", 0) >= 1
assert "error_diagnosis" in progress.stage_failure_notes
+
+
+# ── Cross-turn cumulative failure gate ────────────────────────────────
+
+
+async def test_cumulative_failure_skips_stage():
+ """When cumulative failures reach threshold, stage should skip without LLM call."""
+ cap = _make_capability()
+ cap._call_llm = AsyncMock(return_value="should not be called")
+ progress = _make_progress_with_module()
+ progress.current_stage = LearningStage.EXPLAIN
+ progress.stage_failure_counts["explain"] = 4 # at threshold
+ stream = FakeStream()
+
+ await cap._run_explain(progress, None, stream)
+
+ # Should skip without calling LLM
+ cap._call_llm.assert_not_called()
+ # Should show "多次失败" message
+ content_texts = [t for _, t in stream.events if _ == "content"]
+ assert any("多次失败" in t for t in content_texts)
+ # Should advance stage
+ assert progress.current_stage != LearningStage.EXPLAIN
+
+
+async def test_cumulative_failure_below_threshold_retries():
+ """When cumulative failures are below threshold, normal retry should proceed."""
+ cap = _make_capability()
+ cap._call_llm = AsyncMock(side_effect=RuntimeError("LLM unavailable"))
+ progress = _make_progress_with_module()
+ progress.current_stage = LearningStage.EXPLAIN
+ progress.stage_failure_counts["explain"] = 2 # below threshold of 4
+ stream = FakeStream()
+
+ await cap._run_explain(progress, None, stream)
+
+ # Should have attempted LLM calls (retries happened)
+ assert cap._call_llm.call_count >= 1
+ # Failure count should have increased
+ assert progress.stage_failure_counts["explain"] > 2