The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(pets): delete row strips after extracting their frames
Adolanium7de5a6590626
diff --git a/agent/pet/generate/orchestrate.py b/agent/pet/generate/orchestrate.pyindex 54a1adf5b07..13f002ea8e5 100644--- a/agent/pet/generate/orchestrate.py+++ b/agent/pet/generate/orchestrate.py@@ -246,6 +246,7 @@ def hatch_pet(if cancelled():return state, Nonestrict = attempt < _ROW_GEN_ATTEMPTS - 1+ strips: list[Path] = []try:strips = imagegen.generate(prompts.build_row_prompt(state, count, label, style=style),@@ -274,6 +275,18 @@ def hatch_pet("pet hatch %r: row %r attempt %d/%d failed: %s",slug, state, attempt + 1, _ROW_GEN_ATTEMPTS, exc,)+ finally:+ # The strip is an intermediate. extract_strip_frames has already+ # decoded its frames into memory, so drop the row image after+ # every attempt (success or failure). Nothing prunes+ # cache/images outside the gateway housekeeping loop, so a CLI+ # or desktop hatch would otherwise leave each strip behind for+ # good and grow the cache without bound.+ for strip in strips:+ try:+ Path(strip).unlink(missing_ok=True)+ except OSError:+ passlogger.warning("pet hatch %r: row %r gave up after %.1fs: %s",slug, state, time.monotonic() - t0, last_exc,diff --git a/tests/agent/test_pet_generate.py b/tests/agent/test_pet_generate.pyindex 2715dce46bc..6d78c209b98 100644--- a/tests/agent/test_pet_generate.py+++ b/tests/agent/test_pet_generate.py@@ -8,6 +8,7 @@ exercised hermetically.from __future__ import annotationsimport os+from pathlib import Pathimport pytest@@ -267,6 +268,96 @@ def test_hatch_pet_end_to_end(monkeypatch, tmp_path):assert store.load_pet("mocky").exists+def test_hatch_pet_removes_row_strips_after_extraction(monkeypatch, tmp_path):+ """Row strips are intermediates. Once their frames are decoded, the strip+ files are removed so the image cache does not grow on every hatch (nothing+ prunes cache/images outside the gateway housekeeping loop)."""+ from agent.pet.generate import atlas as atlas_mod+ from agent.pet.generate import imagegen, orchestrate++ base = tmp_path / "base.png"+ _strip(1).save(base)++ produced: list = []++ def fake_generate(prompt, *, n=1, reference_images=None, provider=None, prefix="pet", aspect_ratio="square"):+ state = prefix.replace("pet_row_", "")+ count = atlas_mod.FRAME_COUNTS.get(state, 6)+ p = tmp_path / f"{prefix}.png"+ _strip(count).save(p)+ produced.append(p)+ return [p]++ monkeypatch.setattr(imagegen, "resolve_provider", lambda **_: object())+ monkeypatch.setattr(imagegen, "generate", fake_generate)++ orchestrate.hatch_pet(base_image=base, slug="cleanup", concept="a fox")++ assert produced, "expected row strips to be generated"+ leftover = [p for p in produced if p.exists()]+ assert leftover == [], f"row strips left in cache: {leftover}"+++def test_hatch_pet_removes_row_strips_after_failed_attempt(monkeypatch, tmp_path):+ from agent.pet.generate import atlas as atlas_mod+ from agent.pet.generate import imagegen, orchestrate++ base = tmp_path / "base.png"+ _strip(1).save(base)++ attempts: dict[str, int] = {}++ def fake_generate(prompt, *, n=1, reference_images=None, provider=None, prefix="pet", aspect_ratio="square"):+ attempts[prefix] = attempts.get(prefix, 0) + 1+ state = prefix.replace("pet_row_", "")+ count = atlas_mod.FRAME_COUNTS.get(state, 6)+ path = tmp_path / f"{prefix}_{attempts[prefix]}.png"+ _strip(count).save(path)+ return [path]++ extract_strip_frames = atlas_mod.extract_strip_frames+ failed_once = False++ def flaky_extract(strip, count, *args, **kwargs):+ nonlocal failed_once+ if Path(strip).name.startswith("pet_row_idle_") and not failed_once:+ failed_once = True+ raise ValueError("retry idle row")+ return extract_strip_frames(strip, count, *args, **kwargs)++ monkeypatch.setattr(imagegen, "resolve_provider", lambda **_: object())+ monkeypatch.setattr(imagegen, "generate", fake_generate)+ monkeypatch.setattr(atlas_mod, "extract_strip_frames", flaky_extract)++ orchestrate.hatch_pet(base_image=base, slug="retry-cleanup", concept="a fox")++ assert failed_once+ assert attempts["pet_row_idle"] == 2+ assert not list(tmp_path.glob("pet_row_*"))+++def test_hatch_pet_idle_fallback_when_row_fails(monkeypatch, tmp_path):+ from agent.pet.generate import atlas as atlas_mod+ from agent.pet.generate import imagegen, orchestrate+ from agent.pet.generate.imagegen import GenerationError++ base = tmp_path / "base.png"+ _strip(1).save(base)++ def fake_generate(prompt, *, n=1, reference_images=None, provider=None, prefix="pet", aspect_ratio="square"):+ if prefix == "pet_row_idle":+ raise GenerationError("boom")+ state = prefix.replace("pet_row_", "")+ count = atlas_mod.FRAME_COUNTS.get(state, 6)+ p = tmp_path / f"{prefix}.png"+ _strip(count).save(p)+ return [p]++ monkeypatch.setattr(imagegen, "resolve_provider", lambda **_: object())+ monkeypatch.setattr(imagegen, "generate", fake_generate)++ result = orchestrate.hatch_pet(base_image=base, slug="fallbacky", concept="a fox")+ assert "idle" in result.states # filled by the base-image fallback