fix(manim): preserve review failures and reap cancelled geometry probes

This commit is contained in:
Bingxi Zhao (Frank)
2026-10-31 16:56:47 +08:00
parent 8f1db978cc
commit 4d2678954d
4 changed files with 71 additions and 15 deletions

View File

@@ -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 []

View File

@@ -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

View File

@@ -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(

View File

@@ -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