Skip to content

The agent that grows with you

fix(pm): anchor downloader partials to a writable shared root

ethernet578a15a190f7
diff --git a/pm/cli.py b/pm/cli.py
index 710b7a732ee..1c22d859b44 100644
--- a/pm/cli.py
+++ b/pm/cli.py
@@ -320,32 +320,34 @@ def _gc_store(store, facts) -> tuple[int, int]:
(removed, kept).
"""
from pm.downloader import gc_protected_names
+ from pm import paths
- if not store.root.is_dir():
+ partials_dir = paths.partials_root()
+ if not store.root.is_dir() and not partials_dir.is_dir():
return (0, 0)
removed = 0
with store.install_lock():
facts.reload()
keep = facts.entries_in_use()
# Partials an in-flight (or recently interrupted) download still
- # owns must survive the sweep.
- protected_partials = gc_protected_names(store.root / "partials")
+ # owns must survive the sweep. They live in the writable partials
+ # area, NOT the store root, so sweep that area directly.
+ protected_partials = gc_protected_names(partials_dir)
+ if partials_dir.is_dir():
+ for child in sorted(partials_dir.iterdir()):
+ if child.name in protected_partials:
+ continue
+ print(f"removing partials/{child.name}")
+ if child.is_dir():
+ shutil.rmtree(child, ignore_errors=True)
+ else:
+ try:
+ child.unlink()
+ except OSError:
+ pass
for item in sorted(store.root.iterdir()):
if not item.is_dir() or item.name.startswith("."):
continue
- if item.name == "partials" and protected_partials:
- for child in sorted(item.iterdir()):
- if child.name in protected_partials:
- continue
- print(f"removing {item.name}/{child.name}")
- if child.is_dir():
- shutil.rmtree(child, ignore_errors=True)
- else:
- try:
- child.unlink()
- except OSError:
- pass
- continue
if item.name in keep:
continue
print(f"removing {item.name}")
diff --git a/pm/downloader.py b/pm/downloader.py
index 93b21ec8125..f8bde3f6f63 100644
--- a/pm/downloader.py
+++ b/pm/downloader.py
@@ -125,12 +125,6 @@ def _existing_dest_ok(source: "Source") -> bool:
return False
-def _default_partials() -> Path:
- from pm import paths
-
- return paths.store_root() / "partials"
-
-
# `pm gc` must not delete partials an in-flight download is still writing.
# A sidecar's mtime is the heartbeat: it is (re)written whenever a download
# pauses or errors, and the .part grows on every chunk. Anything touched
@@ -197,7 +191,12 @@ class Download:
self.sources = [Source(s.url, Path(s.dest), s.sha256) for s in sources]
self.resume = resume
self.connections = max(1, int(connections))
- self.partials_dir = Path(partials_dir) if partials_dir else _default_partials()
+ if partials_dir:
+ self.partials_dir = Path(partials_dir)
+ else:
+ from pm import paths
+
+ self.partials_dir = paths.partials_root()
self._paused = threading.Event()
def pause(self) -> None:
diff --git a/pm/paths.py b/pm/paths.py
index 4af1b829a55..6343baa3ad9 100644
--- a/pm/paths.py
+++ b/pm/paths.py
@@ -48,5 +48,20 @@ def store_root() -> Path:
return get_default_hermes_root() / "tools"
+def partials_root() -> Path:
+ """The downloader's managed partials area: machine-scoped and shared
+ (keyed by sha256(url), so two callers or two profiles reuse one
+ partial), but anchored to the DEFAULT hermes root — NOT the byte
+ store. The store can live inside a read-only sealed payload
+ (WindowsApps/agent-payload), and partials are mutable state the
+ downloader writes continuously, so they must land somewhere writable
+ on every install kind: ``%LOCALAPPDATA%\\hermes\\cache\\partials`` on
+ Windows, ``~/.hermes/cache/partials`` on POSIX.
+ """
+ from hermes_constants import get_default_hermes_root
+
+ return get_default_hermes_root() / "cache" / "partials"
+
+
def facts_path() -> Path:
return store_root() / "facts.json"
diff --git a/pm/store.py b/pm/store.py
index 4efbfc1cec9..8c08bd1a8d3 100644
--- a/pm/store.py
+++ b/pm/store.py
@@ -109,11 +109,11 @@ def download(url: str, dest: Path, sha256: str, progress=None) -> Path:
"""Fetch url into dest dir, hash-verified, via the resumable downloader.
The digest is proven before the caller ever sees the file.
``progress(done, total)`` ticks per chunk — a several-hundred-MB engine
- archive on a slow line must never look hung. Partial state lives in the
- store's managed partials area (outside scratch, keyed by sha256(url)), so
- an interrupted = failed fetch resumes on the next call instead of
- re-fetching the whole archive. Non-https/non-loopback urls are refused by
- Download itself (ValueError)."""
+ archive on a slow line must never look hung. Partial state lives in
+ the downloader's writable partials area (outside scratch, keyed by
+ sha256(url)), so an interrupted = failed fetch resumes on the next
+ call instead of re-fetching the whole archive. Non-https/non-loopback
+ urls are refused by Download itself (ValueError)."""
from pm.downloader import Download, Source
dest.mkdir(parents=True, exist_ok=True)
diff --git a/tests/pm/test_pm_authority.py b/tests/pm/test_pm_authority.py
index 432f9a07408..d230cc38d24 100644
--- a/tests/pm/test_pm_authority.py
+++ b/tests/pm/test_pm_authority.py
@@ -352,15 +352,16 @@ def test_adopt_refuses_when_fact_lacks_identity(pm_env):
def test_gc_protects_in_flight_partials(pm_env):
- """gc sweeps the store root, but partials an in-flight download still
- owns (fresh mtimes) survive; stale partials are swept."""
+ """gc sweeps the store root and the partials area, but partials an
+ in-flight download still owns (fresh mtimes) survive; stale partials
+ are swept."""
from pm.cli import cmd_gc
env = pm_env
runtime = env["runtime"]
runtime.mkdir(parents=True, exist_ok=True)
- partials = runtime / "partials"
- partials.mkdir()
+ partials = paths.partials_root()
+ partials.mkdir(parents=True, exist_ok=True)
fresh = partials / "abc.part"
fresh.write_bytes(b"x")
stale = partials / "old.part"
diff --git a/tests/pm/test_store_resume.py b/tests/pm/test_store_resume.py
index 53471a4ddf5..ae150c05484 100644
--- a/tests/pm/test_store_resume.py
+++ b/tests/pm/test_store_resume.py
@@ -67,7 +67,7 @@ def test_store_fetch_resumes_interrupted_download(tmp_path, dl_server, monkeypat
url = _url(dl_server, f"/{name}")
store = Store(runtime / "store")
- partials = paths.store_root() / "partials"
+ partials = paths.partials_root()
key = hashlib.sha256(url.encode("utf-8")).hexdigest()
# First fetch: the server drops the connection ~halfway through the body,