Skip to content

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

fix(math-animator): keep render failure when source vanishes (#1545)

Bingxi Zhao (Frank)fe5c6fe93653
diff --git a/deeptutor/agents/math_animator/renderer.py b/deeptutor/agents/math_animator/renderer.py
index c4bbc66b6..f5702e638 100644
--- a/deeptutor/agents/math_animator/renderer.py
+++ b/deeptutor/agents/math_animator/renderer.py
@@ -208,9 +208,13 @@ class ManimRenderService:
part for part in ["\n".join(stdout_lines), "\n".join(stderr_lines)] if part
)
)
- raise ManimRenderError(
- build_repair_error_message(detail, code=code_path.read_text(encoding="utf-8"))
- )
+ try:
+ generated_code = code_path.read_text(encoding="utf-8")
+ except OSError:
+ # The renderer's stderr is still the cause if the source file
+ # disappeared while Manim was running.
+ generated_code = ""
+ raise ManimRenderError(build_repair_error_message(detail, code=generated_code))
async def _emit_progress(self, message: str, raw: bool = False) -> None:
if self.progress_callback is None:
diff --git a/tests/agents/math_animator/test_renderer_error.py b/tests/agents/math_animator/test_renderer_error.py
new file mode 100644
index 000000000..14784fe18
--- /dev/null
+++ b/tests/agents/math_animator/test_renderer_error.py
@@ -0,0 +1,35 @@
+"""A missing generated source must not hide Manim's actual failure."""
+
+from __future__ import annotations
+
+from io import BytesIO
+
+import pytest
+
+from deeptutor.agents.math_animator.renderer import ManimRenderError, ManimRenderService
+
+
+async def test_failed_manim_preserves_stderr_when_source_disappears(
+ monkeypatch: pytest.MonkeyPatch, tmp_path
+) -> None:
+ class FailedProcess:
+ stdout = BytesIO(b"")
+ stderr = BytesIO(b"FileNotFoundError: latex compiler missing\n")
+
+ def wait(self) -> int:
+ return 1
+
+ monkeypatch.setattr(
+ "deeptutor.agents.math_animator.renderer.subprocess.Popen",
+ lambda *_args, **_kwargs: FailedProcess(),
+ )
+ service = ManimRenderService("test", output_dir=tmp_path)
+
+ with pytest.raises(ManimRenderError, match="latex compiler missing"):
+ await service._run_manim(
+ code_path=tmp_path / "missing_scene.py",
+ scene_name="TestScene",
+ quality="low",
+ save_last_frame=False,
+ )