Skip to content

The agent that grows with you

fix(pets): remove the non-PNG base draft after hardening

Adolaniumb9672ea24e9b
diff --git a/agent/pet/generate/orchestrate.py b/agent/pet/generate/orchestrate.py
index 13f002ea8e5..1b267a4de21 100644
--- a/agent/pet/generate/orchestrate.py
+++ b/agent/pet/generate/orchestrate.py
@@ -71,8 +71,24 @@ def _harden_transparency(path: Path) -> Path:
# Zero the RGB of any leftover semi-transparent edge pixels so a keyed
# draft has no colored halo when composited on the dark UI.
keyed = atlas._clear_transparent_rgb(keyed)
- out = path.with_suffix(".png")
+ # PNG inputs are hardened in place, including mixed-case suffixes like
+ # .PNG. with_suffix(".png") would name a different Path string that still
+ # resolves to the same file on case-insensitive filesystems (macOS APFS,
+ # Windows), and unlinking path after save would delete the hardened output.
+ if path.suffix.lower() == ".png":
+ out = path
+ else:
+ out = path.with_suffix(".png")
keyed.save(out, format="PNG")
+ if out != path:
+ # The hardened PNG stands in for the draft. When the provider handed
+ # back a non-PNG file (webp, jpg, gif), out is a different path, so
+ # remove the original instead of leaving it behind in cache/images
+ # (nothing prunes that directory outside the gateway loop).
+ try:
+ path.unlink(missing_ok=True)
+ except OSError:
+ pass
return out
except Exception as exc: # noqa: BLE001 - cosmetic; fall back to the raw image
logger.debug("base draft transparency hardening failed for %s: %s", path, exc)
diff --git a/tests/agent/test_pet_generate.py b/tests/agent/test_pet_generate.py
index 6d78c209b98..84d303281c0 100644
--- a/tests/agent/test_pet_generate.py
+++ b/tests/agent/test_pet_generate.py
@@ -231,6 +231,55 @@ def test_generate_base_drafts_hardens_opaque_background(monkeypatch, tmp_path):
assert rgba.getpixel((rgba.width // 2, rgba.height // 2))[3] > 0
+def test_harden_transparency_removes_non_png_original(tmp_path):
+ """A non-PNG base draft is replaced by a hardened PNG, and the original
+ draft file is not left behind in the image cache."""
+ from agent.pet.generate import orchestrate
+
+ src = tmp_path / "pet_base_sample.webp"
+ _strip(1).save(src, format="WEBP")
+
+ out = orchestrate._harden_transparency(src)
+
+ assert out.suffix == ".png"
+ assert out.exists()
+ assert not src.exists()
+
+
+def test_harden_transparency_keeps_png_input_in_place(tmp_path):
+ """A PNG base draft is hardened in place, so the returned path is the input
+ path and there is no separate original to remove."""
+ from agent.pet.generate import orchestrate
+
+ src = tmp_path / "pet_base_sample.png"
+ _strip(1).save(src, format="PNG")
+
+ out = orchestrate._harden_transparency(src)
+
+ assert out == src
+ assert out.exists()
+
+
+def test_harden_transparency_keeps_mixed_case_png_in_place(tmp_path):
+ """A PNG path with a mixed-case suffix is hardened in place.
+
+ path.with_suffix('.png') yields a different Path string than 'pet.PNG', but
+ on case-insensitive filesystems (macOS APFS, Windows) both resolve to the
+ same file. Unlinking the input after save would delete the hardened output.
+ """
+ from agent.pet.generate import orchestrate
+
+ src = tmp_path / "pet_base_sample.PNG"
+ _strip(1).save(src, format="PNG")
+
+ out = orchestrate._harden_transparency(src)
+
+ assert out == src
+ assert out.suffix == ".PNG"
+ assert out.exists()
+ assert src.exists()
+
+
def test_hatch_pet_end_to_end(monkeypatch, tmp_path):
from agent.pet import store
from agent.pet.generate import atlas as atlas_mod