From 4d2678954d30ce8a292a2870f3a4ae5ef2a065be Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Sat, 31 Oct 2026 16:56:47 +0800 Subject: [PATCH] fix(manim): preserve review failures and reap cancelled geometry probes --- .../agents/math_animator/layout_probe.py | 29 ++++++++---- deeptutor/agents/math_animator/pipeline.py | 5 +- .../agents/math_animator/retry_manager.py | 5 +- .../math_animator/test_retry_manager.py | 47 +++++++++++++++++++ 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/deeptutor/agents/math_animator/layout_probe.py b/deeptutor/agents/math_animator/layout_probe.py index e1d1160d3..7b78ae9f3 100644 --- a/deeptutor/agents/math_animator/layout_probe.py +++ b/deeptutor/agents/math_animator/layout_probe.py @@ -15,7 +15,6 @@ import asyncio import json from pathlib import Path import re -import subprocess # nosec B404 - fixed argv: this interpreter + this file import sys import tempfile @@ -195,17 +194,27 @@ async def geometry_review(code: str): path = Path(tmp) / "scene.py" path.write_text(code, encoding="utf-8") try: - proc = await asyncio.to_thread( - subprocess.run, - [sys.executable, str(Path(__file__).resolve()), str(path), scene], - capture_output=True, - text=True, - timeout=PROBE_TIMEOUT_S, + proc = await asyncio.create_subprocess_exec( + sys.executable, + str(Path(__file__).resolve()), + str(path), + scene, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, cwd=tmp, - ) # nosec B603 - except subprocess.TimeoutExpired: + ) + except OSError: return None - lines = [ln for ln in proc.stdout.splitlines() if ln.startswith("{")] + try: + stdout, _ = await asyncio.wait_for(proc.communicate(), timeout=PROBE_TIMEOUT_S) + except (TimeoutError, asyncio.CancelledError) as exc: + if proc.returncode is None: + proc.kill() + await proc.wait() + if isinstance(exc, asyncio.CancelledError): + raise + return None + lines = [ln for ln in stdout.decode(errors="replace").splitlines() if ln.startswith("{")] if proc.returncode != 0 or not lines: return None issues = json.loads(lines[-1]).get("issues") or [] diff --git a/deeptutor/agents/math_animator/pipeline.py b/deeptutor/agents/math_animator/pipeline.py index 84edbadaa..1f777331c 100644 --- a/deeptutor/agents/math_animator/pipeline.py +++ b/deeptutor/agents/math_animator/pipeline.py @@ -217,13 +217,12 @@ class MathAnimatorPipeline: async def _geometry_callback( current_code: str, render_result: RenderResult - ) -> VisualReviewResult: + ) -> VisualReviewResult | None: # Record-only: what the rendered scene still has, for the result payload. review = await geometry_review(current_code) if review is None: - return VisualReviewResult(passed=True, summary="Layout check unavailable.") + return None if not review.passed: - review.passed = True review.summary = "Layout issues remain after repair rounds: " + review.summary return review diff --git a/deeptutor/agents/math_animator/retry_manager.py b/deeptutor/agents/math_animator/retry_manager.py index 97a59f95d..a29a38cfd 100644 --- a/deeptutor/agents/math_animator/retry_manager.py +++ b/deeptutor/agents/math_animator/retry_manager.py @@ -24,7 +24,8 @@ class CodeRetryManager: *, renderer: ManimRenderService, repair_callback: Callable[[str, str, int], Awaitable[GeneratedCode]], - review_callback: Callable[[str, object], Awaitable[VisualReviewResult]] | None = None, + review_callback: Callable[[str, object], Awaitable[VisualReviewResult | None]] + | None = None, on_retry: Callable[[RetryAttempt], Awaitable[None]] | None = None, on_status: Callable[[str], Awaitable[None]] | None = None, max_retries: int = 4, @@ -76,7 +77,7 @@ class CodeRetryManager: f"Visual quality review timed out after {int(self.review_timeout_seconds)}s." ) from timeout_exc render_result.visual_review = review_result - if not review_result.passed: + if review_result is not None and not review_result.passed: if attempt >= self.max_retries: if self.on_status is not None: await self.on_status( diff --git a/tests/agents/math_animator/test_retry_manager.py b/tests/agents/math_animator/test_retry_manager.py index d3fb63e84..7f3108afb 100644 --- a/tests/agents/math_animator/test_retry_manager.py +++ b/tests/agents/math_animator/test_retry_manager.py @@ -183,3 +183,50 @@ async def test_retry_manager_returns_best_effort_result_when_visual_review_still assert status_messages[-1] == ( "Visual review still found issues after all retries. Returning the best available result with a warning." ) + + +@pytest.mark.asyncio +async def test_unavailable_review_is_not_recorded_as_passed(): + class Renderer: + async def render(self, **kwargs): + return RenderResult(output_mode="video", quality="low") + + async def repair(*args): + raise AssertionError("unavailable review is not a layout failure") + + async def review(*args): + return None + + manager = CodeRetryManager(renderer=Renderer(), repair_callback=repair, review_callback=review) + _, result = await manager.render_with_retries( + initial_code="scene", output_mode="video", quality="low" + ) + assert result.visual_review is None + + +@pytest.mark.asyncio +async def test_geometry_probe_reaps_cancelled_process(monkeypatch): + from deeptutor.agents.math_animator import layout_probe + + class Process: + returncode = None + killed = waited = False + + async def communicate(self): + raise asyncio.CancelledError + + def kill(self): + self.killed = True + + async def wait(self): + self.waited = True + + proc = Process() + + async def create(*args, **kwargs): + return proc + + monkeypatch.setattr(asyncio, "create_subprocess_exec", create) + with pytest.raises(asyncio.CancelledError): + await layout_probe.geometry_review("class Demo(Scene): pass") + assert proc.killed and proc.waited