The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(agent): trim generated-media salvage tests to two invariants
kshitijk4poor64f81b18bec3
diff --git a/tests/agent/test_generated_media_durable.py b/tests/agent/test_generated_media_durable.pyindex 43a83979c62..d2bea97ea6c 100644--- a/tests/agent/test_generated_media_durable.py+++ b/tests/agent/test_generated_media_durable.py@@ -23,6 +23,8 @@ import osimport timefrom pathlib import Path+import pytest+PNG_1PX = bytes.fromhex("89504e470d0a1a0a0000000d49484452000000010000000108020000009077""53de00000010494441547801635c0e000000feff03000006000557bfabd400"@@ -35,82 +37,36 @@ def _backdate_hours(path, hours: float = 25.0) -> None:os.utime(path, (old, old))-class TestGeneratedImageSurvivesSweep:- def test_old_generated_image_not_swept(self, tmp_path, monkeypatch):- monkeypatch.setenv("HERMES_HOME", str(tmp_path))- from agent.image_gen_provider import save_b64_image- from gateway.platforms.base import cleanup_image_cache-- path = save_b64_image(base64.b64encode(PNG_1PX).decode(), prefix="red_test")- _backdate_hours(path)- removed = cleanup_image_cache(max_age_hours=24)- assert path.exists(), (- f"25h-old generated image was swept ({removed} removed): {path}"- )-- def test_generated_image_outside_swept_dir(self, tmp_path, monkeypatch):- monkeypatch.setenv("HERMES_HOME", str(tmp_path))- from agent.image_gen_provider import save_b64_image- from gateway.platforms.base import get_image_cache_dir-- path = save_b64_image(base64.b64encode(PNG_1PX).decode(), prefix="red_test")- assert path.parent != get_image_cache_dir()- assert path.exists()---class TestGeneratedVideoSurvivesSweep:- def test_old_generated_video_not_swept(self, tmp_path, monkeypatch):- monkeypatch.setenv("HERMES_HOME", str(tmp_path))- from agent.video_gen_provider import save_b64_video- from gateway.platforms.base import cleanup_video_cache+ "writer, sweeper",+ [+ ("agent.image_gen_provider:save_b64_image", "cleanup_image_cache"),+ ("agent.video_gen_provider:save_b64_video", "cleanup_video_cache"),+ ],+ ids=["image", "video"],+)+def test_old_generated_media_is_not_swept(tmp_path, monkeypatch, writer, sweeper):+ import importlib- path = save_b64_video(base64.b64encode(b"fake-video-bytes").decode(), prefix="red_test")- _backdate_hours(path)- removed = cleanup_video_cache(max_age_hours=24)- assert path.exists(), (- f"25h-old generated video was swept ({removed} removed): {path}"- )+ from gateway.platforms import base- def test_generated_video_outside_swept_dir(self, tmp_path, monkeypatch):- monkeypatch.setenv("HERMES_HOME", str(tmp_path))- from agent.video_gen_provider import save_b64_video- from gateway.platforms.base import get_video_cache_dir+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ mod, fn = writer.split(":")+ save = getattr(importlib.import_module(mod), fn)- path = save_b64_video(base64.b64encode(b"fake-video-bytes").decode(), prefix="red_test")- assert path.parent != get_video_cache_dir()- assert path.exists()+ path = save(base64.b64encode(PNG_1PX).decode(), prefix="red_test")+ _backdate_hours(path)+ removed = getattr(base, sweeper)(max_age_hours=24)+ assert path.exists(), f"25h-old generated media was swept ({removed} removed): {path}"-class TestGeneratedMediaReachesSandbox:+def test_generated_dir_is_mounted_from_the_writers_location(tmp_path, monkeypatch):"""Out-of-sweep is only half the job: remote backends must still see the file."""-- def test_generated_dir_is_mounted_from_the_writers_location(self, tmp_path, monkeypatch):- monkeypatch.setenv("HERMES_HOME", str(tmp_path))- from agent.image_gen_provider import save_b64_image- from tools.credential_files import get_cache_directory_mounts-- path = save_b64_image(base64.b64encode(PNG_1PX).decode(), prefix="red_test")- mounts = {m["container_path"]: m["host_path"] for m in get_cache_directory_mounts()}- assert "/root/.hermes/cache/generated" in mounts, sorted(mounts)- assert path.is_relative_to(Path(mounts["/root/.hermes/cache/generated"]).resolve())-- def test_generated_image_maps_into_the_sandbox(self, tmp_path, monkeypatch):- monkeypatch.setenv("HERMES_HOME", str(tmp_path))- from agent.image_gen_provider import save_b64_image- from tools.credential_files import map_cache_path_to_container-- path = save_b64_image(base64.b64encode(PNG_1PX).decode(), prefix="red_test")- rel = f"cache/generated/images/{path.name}"- assert map_cache_path_to_container(str(path)) == f"/root/.hermes/{rel}"- assert map_cache_path_to_container(str(path), container_base="~/.hermes") == f"~/.hermes/{rel}"-- def test_generated_video_is_in_the_per_file_sync_list(self, tmp_path, monkeypatch):- """Modal uploads file-by-file through iter_cache_files()."""- monkeypatch.setenv("HERMES_HOME", str(tmp_path))- from agent.video_gen_provider import save_bytes_video- from tools.credential_files import iter_cache_files-- path = save_bytes_video(b"fake-video-bytes", prefix="red_test")- # container paths are POSIX; normalize the host separator pytest sees on Windows- entries = {e["container_path"].replace("\\", "/") for e in iter_cache_files("~/.hermes")}- assert f"~/.hermes/cache/generated/videos/{path.name}" in entries+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ from agent.image_gen_provider import save_b64_image+ from tools.credential_files import get_cache_directory_mounts++ path = save_b64_image(base64.b64encode(PNG_1PX).decode(), prefix="red_test")+ mounts = {m["container_path"]: m["host_path"] for m in get_cache_directory_mounts()}+ assert "/root/.hermes/cache/generated" in mounts, sorted(mounts)+ assert path.is_relative_to(Path(mounts["/root/.hermes/cache/generated"]).resolve())diff --git a/tests/tools/test_image_generation_artifacts.py b/tests/tools/test_image_generation_artifacts.pyindex 45506979ecb..ae2e9bede39 100644--- a/tests/tools/test_image_generation_artifacts.py+++ b/tests/tools/test_image_generation_artifacts.py@@ -1,4 +1,3 @@-import base64import concurrent.futuresimport jsonimport threading@@ -148,44 +147,3 @@ def test_handle_image_generate_postprocesses_plugin_result(monkeypatch, tmp_pathassert seen_task_ids == ["plugin-task"]assert result["agent_visible_image"] == "/home/remote/.hermes/cache/images/plugin.png"---def test_real_generated_image_is_agent_visible_under_ssh_env(monkeypatch, tmp_path):- """#126445: the writers now target ``cache/generated/``, so that dir needs- its own credential-files mount entry — otherwise the SSH/Docker/Modal agent- never receives the bytes and the result silently loses ``agent_visible_image``.-- Uses the real ``save_b64_image`` writer (not a hand-made ``cache/images``- fixture), which is exactly the hole the sibling test above cannot see.- """- from agent.image_gen_provider import save_b64_image- from tools import image_generation_tool-- hermes_home = tmp_path / ".hermes"- hermes_home.mkdir()- monkeypatch.setenv("HERMES_HOME", str(hermes_home))-- image_path = save_b64_image(base64.b64encode(b"png").decode(), prefix="xai_real")-- sync_calls = []-- class FakeSyncManager:- def sync(self, *, force=False):- sync_calls.append(force)-- env = SimpleNamespace(- _remote_home="/home/remotesshuser",- _sync_manager=FakeSyncManager(),- )- monkeypatch.setattr(image_generation_tool, "_active_terminal_env", lambda task_id: env)-- raw = json.dumps({"success": True, "image": str(image_path)})- result = json.loads(- image_generation_tool._postprocess_image_generate_result(raw, task_id="task-1")- )-- assert result["host_image"] == str(image_path)- assert result["agent_visible_image"] == (- f"/home/remotesshuser/.hermes/cache/generated/images/{image_path.name}"- )- assert sync_calls == [True]