Skip to content

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

fix(math-animator): preserve render error cause and guide LaTeX repair (#1545)

Bingxi Zhao (Frank)178c1813a18a
diff --git a/deeptutor/agents/math_animator/renderer.py b/deeptutor/agents/math_animator/renderer.py
index 3613499c5..c4bbc66b6 100644
--- a/deeptutor/agents/math_animator/renderer.py
+++ b/deeptutor/agents/math_animator/renderer.py
@@ -13,7 +13,7 @@ from typing import Awaitable, Callable
from deeptutor.services.path_service import get_path_service
from .models import RenderedArtifact, RenderResult
-from .utils import slugify_filename, trim_error_message
+from .utils import build_repair_error_message, slugify_filename, trim_error_message
YON_IMAGE_PATTERN = re.compile(
r"###\s*YON_IMAGE_(\d+)_START\s*###\s*(.*?)\s*###\s*YON_IMAGE_\1_END\s*###",
@@ -203,13 +203,14 @@ class ManimRenderService:
return_code = process.wait()
await self._emit_progress(f"Manim process finished with exit code {return_code}.", raw=True)
if return_code != 0:
- raise ManimRenderError(
- trim_error_message(
- "\n".join(
- part for part in ["\n".join(stdout_lines), "\n".join(stderr_lines)] if part
- )
+ detail = trim_error_message(
+ "\n".join(
+ 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"))
+ )
async def _emit_progress(self, message: str, raw: bool = False) -> None:
if self.progress_callback is None:
diff --git a/deeptutor/agents/math_animator/utils.py b/deeptutor/agents/math_animator/utils.py
index 6d075a27e..886c010ba 100644
--- a/deeptutor/agents/math_animator/utils.py
+++ b/deeptutor/agents/math_animator/utils.py
@@ -18,10 +18,15 @@ def trim_error_message(stderr: str, limit: int = 1200) -> str:
text = (stderr or "").strip()
if len(text) <= limit:
return text
- return text[-limit:]
+ separator = "\n…\n"
+ if limit <= len(separator):
+ return text[:limit]
+ head = (limit - len(separator)) // 2
+ tail = limit - len(separator) - head
+ return text[:head] + separator + text[-tail:]
-def build_repair_error_message(error_message: str) -> str:
+def build_repair_error_message(error_message: str, *, code: str = "") -> str:
text = (error_message or "").strip()
lowered = text.lower()
hints: list[str] = []
@@ -41,6 +46,15 @@ def build_repair_error_message(error_message: str) -> str:
"Check any custom point lists, helper lines, braces, polygons, or manually assembled VMobject paths."
)
+ if re.search(r"\b(?:MathTex|Tex)\s*\(", code) and (
+ "filenotfounderror" in lowered or "winerror 2" in lowered
+ ):
+ hints.append(
+ "This scene uses MathTex/Tex and a renderer executable was not found. "
+ "Check that latex and dvisvgm are installed on PATH, or use Text "
+ "when a LaTeX toolchain is unavailable."
+ )
+
if not hints:
return text
diff --git a/tests/agents/math_animator/test_utils.py b/tests/agents/math_animator/test_utils.py
index adc6ed059..b4319c538 100644
--- a/tests/agents/math_animator/test_utils.py
+++ b/tests/agents/math_animator/test_utils.py
@@ -5,6 +5,7 @@ from deeptutor.agents.math_animator.utils import (
describe_unusable_output,
escalated_max_tokens,
extract_json_object,
+ trim_error_message,
)
from deeptutor.services.llm import StreamOutcome
@@ -27,6 +28,19 @@ def test_build_repair_error_message_keeps_unknown_errors_plain() -> None:
assert build_repair_error_message(error_message) == error_message
+def test_render_error_keeps_start_and_end_with_latex_repair_hint() -> None:
+ stderr = "FileNotFoundError: latex executable missing\n" + "noise\n" * 300 + "last frame"
+ excerpt = trim_error_message(stderr, limit=120)
+ assert len(excerpt) <= 120
+ assert "latex executable missing" in excerpt
+ assert excerpt.endswith("last frame")
+
+ repaired = build_repair_error_message(excerpt, code="MathTex(r'x^2')")
+ assert "latex and dvisvgm" in repaired
+ assert "use Text" in repaired
+ assert build_repair_error_message(excerpt, code="Text('x')") == excerpt
+
+
def test_extract_json_object_accepts_trailing_extra_data() -> None:
raw = (
'{"code":"from manim import *\\nclass A(Scene):\\n pass","rationale":"fix syntax"}\n'