The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(pm): publish seeded cache files atomically
Riccardo Vecchi44c32dfa7909
diff --git a/pm/packages.py b/pm/packages.pyindex e2a72875ba2..18b1ffcbf24 100644--- a/pm/packages.py+++ b/pm/packages.py@@ -313,7 +313,16 @@ def _copy_seed_file(src: str | os.PathLike, dst: str | os.PathLike) -> str | os.return dstexcept FileNotFoundError:pass- return shutil.copy2(src, dst)+ # Other profiles share this cache: publish only a complete copy, without+ # truncating the live entry or leaving a failed copy visible to readers.+ fd, staging = tempfile.mkstemp(prefix=".seed-", dir=Path(dst).parent)+ os.close(fd)+ try:+ shutil.copy2(src, staging)+ os.replace(staging, dst)+ finally:+ Path(staging).unlink(missing_ok=True)+ return dstdef uv_cache_dir() -> Path:diff --git a/tests/pm/test_uv_cache_publication.py b/tests/pm/test_uv_cache_publication.pynew file mode 100644index 00000000000..11f721eb4a3--- /dev/null+++ b/tests/pm/test_uv_cache_publication.py@@ -0,0 +1,79 @@+"""Cache readers must never see an unfinished payload seed copy."""++from pathlib import Path++import pytest++import hermes_constants+from pm import packages, paths+++def seed_paths(tmp_path, monkeypatch):+ machine = tmp_path / "machine"+ monkeypatch.setattr(hermes_constants, "get_default_hermes_root", lambda: machine)+ payload = tmp_path / "payload"+ store = payload / "tools"+ store.mkdir(parents=True)+ source = payload / "uv-cache" / "entry"+ source.parent.mkdir()+ source.write_bytes(b"complete-new-payload")+ monkeypatch.setattr(paths, "store_root", lambda: store)+ repo = tmp_path / "repo"+ repo.mkdir()+ (repo / "uv.lock").write_text("version = 1\n", encoding="utf-8")+ monkeypatch.setattr(paths, "repo_root", lambda: repo)+ return source, machine / "cache" / "uv" / "entry"++[email protected]("previous", [None, b"old"])+def test_seed_publishes_only_a_complete_file(seed_paths, monkeypatch, previous):+ source, destination = seed_paths+ destination.parent.mkdir(parents=True)+ if previous is not None:+ destination.write_bytes(previous)+ real_copy = packages.shutil.copy2+ observed = []++ def observe_copy(src, dst):+ Path(dst).write_bytes(b"partial")+ observed.append(destination.read_bytes() if destination.exists() else None)+ return real_copy(src, dst)++ monkeypatch.setattr(packages.shutil, "copy2", observe_copy)+ packages.uv_cache_dir()++ assert observed == [previous]+ assert destination.read_bytes() == source.read_bytes()+ assert (destination.parent / ".seeded").is_file()+ assert {p.name for p in destination.parent.iterdir()} == {"entry", ".seeded"}++[email protected]("previous", [None, b"old"])+def test_interrupted_seed_preserves_readers_and_retries(seed_paths, monkeypatch, previous):+ source, destination = seed_paths+ destination.parent.mkdir(parents=True)+ if previous is not None:+ destination.write_bytes(previous)+ real_copy = packages.shutil.copy2+ attempts = []++ def interrupted_copy(src, dst):+ attempts.append(1)+ if len(attempts) == 1:+ Path(dst).write_bytes(b"partial")+ raise OSError("synthetic copy interruption")+ return real_copy(src, dst)++ monkeypatch.setattr(packages.shutil, "copy2", interrupted_copy)+ packages.uv_cache_dir()++ assert (destination.read_bytes() if destination.exists() else None) == previous+ assert not (destination.parent / ".seeded").exists()+ assert {p.name for p in destination.parent.iterdir()} == ({"entry"} if previous else set())++ packages.uv_cache_dir()++ assert destination.read_bytes() == source.read_bytes()+ assert (destination.parent / ".seeded").is_file()+ assert {p.name for p in destination.parent.iterdir()} == {"entry", ".seeded"}diff --git a/tests/pm/test_uv_cache_seed.py b/tests/pm/test_uv_cache_seed.pyindex 60102c8a2ea..1c216e9d26c 100644--- a/tests/pm/test_uv_cache_seed.py+++ b/tests/pm/test_uv_cache_seed.py@@ -44,7 +44,7 @@ def test_update_merges_the_new_payload_into_an_existing_seed(payload_cache, tmp_added = paths.store_root().parent / "uv-cache" / "archive-v0" / "bucket" / "new.py"added.write_text("new pin\n", encoding="utf-8")- (code / "uv.lock").write_text("version = 1 # new\n", encoding="utf-8")+ (code / "uv.lock").write_text("version = 1 # updated\n", encoding="utf-8")packages.uv_cache_dir()assert (payload_cache / "archive-v0" / "bucket" / "new.py").read_text(encoding="utf-8-sig") == "new pin\n"