The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(pm): set a held replaced tree aside instead of failing the install
teknium1bd75d3091704
diff --git a/pm/cli.py b/pm/cli.pyindex 11dc3c926a6..7134d3e8def 100644--- a/pm/cli.py+++ b/pm/cli.py@@ -11,8 +11,7 @@ import timefrom pathlib import Pathfrom pm import termux_libs-from pm.install import (_RESTORED_DISPLACEMENT_SUFFIX, _facts, _lockfile, _remove_entry,- _store, ensure, stage_only)+from pm.install import _facts, _lockfile, _reclaim_set_aside, _store, ensure, stage_onlyfrom pm.operations import lock_projectfrom pm.package import InstallErrorfrom pm.paths import repo_root@@ -491,28 +490,7 @@ def _gc_store(store, facts) -> tuple[int, int]:facts.reload()keep = facts.entries_in_use()collect_partials(partials_dir)- # A restored entry is live before its displaced replacement is removed.- # Only the sidecar written after that restore makes the orphan safe to GC.- for marker in sorted(store.root.glob(f".displaced-*{_RESTORED_DISPLACEMENT_SUFFIX}")):- if marker.is_symlink() or not marker.is_file():- continue- displaced_name = marker.name.removesuffix(_RESTORED_DISPLACEMENT_SUFFIX)- displaced = store.entry(displaced_name)- if displaced.is_symlink():- continue- existed = displaced.exists()- try:- _remove_entry(store, displaced_name)- except OSError as exc:- print(f"keeping {displaced_name}: {exc}")- continue- try:- marker.unlink(missing_ok=True)- except OSError as exc:- print(f"keeping reclaim marker {marker.name}: {exc}")- if existed:- print(f"removing {displaced_name}")- removed += 1+ removed += _reclaim_set_aside(store)for item in sorted(store.root.iterdir()):if not item.is_dir():continuediff --git a/pm/install.py b/pm/install.pyindex 72280fa8963..48f91d747b0 100644--- a/pm/install.py+++ b/pm/install.py@@ -217,7 +217,7 @@ def _refuse_lazy(name: str, what: str) -> InstallError:return error-def _remove_entry(store: Store, entry_name: str) -> None:+def _remove_entry(store: Store, entry_name: str, *, attempts: int = 5) -> None:"""Remove a replaced or failed entry, retrying transient Windows holds.Corruption may leave a file where the directory belonged. Failure@@ -226,7 +226,7 @@ def _remove_entry(store: Store, entry_name: str) -> None:import timeentry = store.entry(entry_name)- for attempt in range(5):+ for attempt in range(attempts):try:if entry.is_symlink() or not entry.is_dir():entry.unlink(missing_ok=True)@@ -235,28 +235,57 @@ def _remove_entry(store: Store, entry_name: str) -> None:returnexcept FileNotFoundError:return- except OSError as e:- if attempt == 4:+ except OSError:+ if attempt == attempts - 1:raisetime.sleep(0.2 * (attempt + 1))-_RESTORED_DISPLACEMENT_SUFFIX = ".restored"+_SET_ASIDE_PREFIX = ".reclaim-"-def _remove_downloads(store: Store, artifacts: list[dict]) -> None:- """Release this package's archives after publication, under its store lock."""- for artifact in artifacts:- _remove_entry(store, f"fetch-{artifact['sha256']}")+def _discard_entry(store: Store, entry_name: str) -> None:+ """Drop a tree a finished publish or restore left behind as garbage.+ A running Hermes process can keep the replaced interpreter's DLLs mapped+ far past the retry window, and failing here reports a completed install+ as broken (#124807). Windows still allows renaming a tree with mapped+ images, so it moves to a `.reclaim-*` name: its slot is free for the next+ publish, no restore ever picks it up, and the next install or+ `hermes pm gc` deletes it once the hold is gone.+ """+ import uuid-def _discard_previous_entry(store: Store, previous_entry) -> None:- # A stale previous entry is only a restore point. A locked file- # (mapped DLL, AV hold) must not fail the install that replaced it.try:- _remove_entry(store, previous_entry.name)+ _remove_entry(store, entry_name)+ returnexcept OSError as e:- LOG.warning("keeping %s: %s (will retry on next install; delete manually once unlocked)", previous_entry.name, e)+ error = e+ try:+ store.entry(entry_name).rename(store.entry(f"{_SET_ASIDE_PREFIX}{uuid.uuid4().hex}"))+ except OSError:+ LOG.warning("could not remove or set aside %s: %s", entry_name, error)+ return+ LOG.warning("%s is still in use (%s); set aside for the next install or `hermes pm gc`",+ entry_name, error)+++def _reclaim_set_aside(store: Store) -> int:+ """Delete set-aside trees whose hold is gone; the caller holds the store lock."""+ removed = 0+ for item in sorted(store.root.glob(f"{_SET_ASIDE_PREFIX}*")):+ try:+ _remove_entry(store, item.name, attempts=1)+ except OSError:+ continue+ removed += 1+ return removed+++def _remove_downloads(store: Store, artifacts: list[dict]) -> None:+ """Release this package's archives after publication, under its store lock."""+ for artifact in artifacts:+ _discard_entry(store, f"fetch-{artifact['sha256']}")def _entry_verified(package: Package, fact: dict, store: Store, target: str) -> bool:@@ -283,18 +312,7 @@ def _restore_previous_entry(store: Store, entry, previous) -> None:displaced.rename(entry)raiseif had_entry:- try:- _remove_entry(store, displaced.name)- except OSError as exc:- # The prior entry is live again. A Windows handle on the displaced- # tree must not turn successful recovery into another failed install.- # Mark only this post-restore orphan as safe for `hermes pm gc`.- marker = store.entry(f"{displaced.name}{_RESTORED_DISPLACEMENT_SUFFIX}")- try:- marker.touch()- except OSError:- raise exc- LOG.warning("Keeping locked restored displacement %s for pm gc: %s", displaced, exc)+ _discard_entry(store, displaced.name)@contextmanager@@ -313,7 +331,7 @@ def _publish_entry(package, store, staged, entry, previous_entry, target):_restore_previous_entry(store, entry, previous_entry)raiseif previous_entry.exists():- _discard_previous_entry(store, previous_entry)+ _discard_entry(store, previous_entry.name)def _settle_previous_entry(package, store, entry, previous_entry, previous, target) -> None:@@ -324,7 +342,7 @@ def _settle_previous_entry(package, store, entry, previous_entry, previous, targ# an interrupted stage always restores its prior usable bytes.if (previous and previous.get("entry") == entry.nameand _entry_verified(package, previous, store, target)):- _discard_previous_entry(store, previous_entry)+ _discard_entry(store, previous_entry.name)else:_restore_previous_entry(store, entry, previous_entry)@@ -404,6 +422,7 @@ def _install(previous = facts.get(package.name) if facts is not None else Noneprevious_entry = store.entry(f".previous-{'stage-' if facts is None else ''}{entry_name}")_settle_previous_entry(package, store, entry, previous_entry, previous, target)+ _reclaim_set_aside(store)if (_entry_current(package, lockfile, facts, store, entry, previous, version, pin, target)and not _fresh_copy):_remove_downloads(store, artifacts)