Skip to content

The agent that grows with you

refactor(pm): one install_root() in pm.paths replaces six HERMES_INSTALL_ROOT ladders

ethernetf6a30447de04
diff --git a/hermes_cli/_launchers.py b/hermes_cli/_launchers.py
index d37de86c878..cdce23c5e60 100644
--- a/hermes_cli/_launchers.py
+++ b/hermes_cli/_launchers.py
@@ -361,7 +361,9 @@ def expose_cli(project_root: Path | None = None, *, create: bool = True) -> dict
already own, without loading application config or enabling new exposure.
"""
# Resolved before the platform branch: the Windows path needs it too.
- root = Path(project_root or os.environ.get("HERMES_INSTALL_ROOT") or Path(__file__).resolve().parents[1]).resolve()
+ from pm.paths import install_root
+
+ root = Path(project_root or install_root()).resolve()
if _is_windows():
# The installer stages the user-facing commands into $HERMES_HOME\bin
# and registers that directory in the User PATH. An update skipped both
diff --git a/hermes_cli/boot_bootstrap.py b/hermes_cli/boot_bootstrap.py
index a5cd8dc9c7e..e8951b1d955 100644
--- a/hermes_cli/boot_bootstrap.py
+++ b/hermes_cli/boot_bootstrap.py
@@ -22,15 +22,6 @@ RECORD_SCHEMA_VERSION = 1
LOCK_STALE_SECONDS = 600
-def default_project_root() -> Path:
- """The tree this code runs from: HERMES_INSTALL_ROOT for sealed
- artifacts whose stamp lives outside the package dir (the override
- hermes_cli.version_info honours), the code root otherwise."""
- env = os.environ.get("HERMES_INSTALL_ROOT")
- if env:
- return Path(env)
- return Path(__file__).resolve().parents[1]
-
# ---------------------------------------------------------------------------
# current identity
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index e52a52dd583..199f8ee6889 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -3495,15 +3495,16 @@ def main():
pass
if _first_positional_argv() != "update":
- from hermes_cli.boot_bootstrap import default_project_root, maybe_run_boot_bootstrap
- maybe_run_boot_bootstrap(default_project_root())
+ from hermes_cli.boot_bootstrap import maybe_run_boot_bootstrap
+ from pm.paths import install_root
+ maybe_run_boot_bootstrap(install_root())
# Every dispatch, including fast chat/serve, gets one passive PM verdict.
try:
- from hermes_cli.boot_bootstrap import default_project_root
from hermes_cli.venv_sync import check_runtime
+ from pm.paths import install_root
- problem = check_runtime(default_project_root())
+ problem = check_runtime(install_root())
if problem:
print(f"⚠ {problem}", file=sys.stderr)
except Exception:
diff --git a/hermes_cli/post_update.py b/hermes_cli/post_update.py
index 776cacf9e2b..f0d71714bba 100644
--- a/hermes_cli/post_update.py
+++ b/hermes_cli/post_update.py
@@ -15,19 +15,11 @@ from pathlib import Path
from typing import Iterable
from hermes_cli._launchers import expose_cli
+from pm.paths import install_root
logger = logging.getLogger(__name__)
-def _install_root() -> Path:
- """The tree this code runs from: HERMES_INSTALL_ROOT for sealed
- artifacts whose stamp lives outside the package dir (the same
- override hermes_cli.version_info honours), the code root otherwise."""
- env = os.environ.get("HERMES_INSTALL_ROOT")
- if env:
- return Path(env)
- return Path(__file__).resolve().parents[1]
-
# ---------------------------------------------------------------------------
# config migration (backup / migrate / verify / restore)
@@ -184,7 +176,7 @@ def step_adopt_blessed_checkout(project_root: Path | None = None) -> dict:
from hermes_constants import get_hermes_home
- root = _install_root() if project_root is None else Path(project_root)
+ root = install_root() if project_root is None else Path(project_root)
# The blessed roots: the canonical locations installers create.
blessed = (
diff --git a/hermes_cli/update_channel.py b/hermes_cli/update_channel.py
index ec60af4671d..d70074e9146 100644
--- a/hermes_cli/update_channel.py
+++ b/hermes_cli/update_channel.py
@@ -39,6 +39,7 @@ import os
import re
from pathlib import Path
from typing import Any, Optional
+from pm.paths import install_root
logger = logging.getLogger(__name__)
@@ -75,16 +76,6 @@ def canary_tag_for_date(version: str, date_utc: str) -> str:
return f"v{major}.{minor}.{patch + 1}-canary.{date_utc}"
-def _default_root() -> Path:
- """This process's install root.
-
- Mirrors version_info's stamp resolution: HERMES_INSTALL_ROOT when the
- steward wrapper sets it (Nix points it at the sealed tree), else the
- code root of the executing checkout.
- """
- root = os.environ.get("HERMES_INSTALL_ROOT")
- return Path(root) if root else Path(__file__).parent.parent.resolve()
-
def install_id(project_root: Optional[Path] = None) -> str:
"""The sha16 id of the install at ``project_root`` (default: this one).
@@ -92,7 +83,7 @@ def install_id(project_root: Optional[Path] = None) -> str:
Same identity as the ``installs/<sha16>/`` state folder key.
"""
if project_root is None:
- project_root = _default_root()
+ project_root = install_root()
return install_key(Path(project_root))
@@ -139,7 +130,7 @@ def default_channel(project_root: Optional[Path] = None) -> str:
newest STABLE release, where that file does not exist (404), leaving
the install unable to update at all.
"""
- root = Path(project_root) if project_root is not None else _default_root()
+ root = Path(project_root) if project_root is not None else install_root()
stamp = _read_stamp(root)
if not _package_channel(stamp):
return CHANNEL_MAIN
@@ -156,7 +147,7 @@ def resolve_update_channel(
project_root: Optional[Path] = None,
) -> str:
"""Source records select releases or main; package tags fix bundle identity."""
- root = Path(project_root) if project_root is not None else _default_root()
+ root = Path(project_root) if project_root is not None else install_root()
if _package_channel(_read_stamp(root)):
return default_channel(root)
configured: Any = channel_record(config, root).get("channel")
@@ -177,7 +168,7 @@ def set_install_channel(
"""
from hermes_cli.update_contract import COMMIT_BUILD_UPDATE_MESSAGE, is_commit_build
- root = Path(project_root) if project_root is not None else _default_root()
+ root = Path(project_root) if project_root is not None else install_root()
if is_commit_build(root):
raise ValueError(COMMIT_BUILD_UPDATE_MESSAGE)
channel = validate_name(channel)
diff --git a/hermes_cli/version_info.py b/hermes_cli/version_info.py
index ef74e9473f5..50b643fc84e 100644
--- a/hermes_cli/version_info.py
+++ b/hermes_cli/version_info.py
@@ -15,7 +15,6 @@ Resolution order:
from __future__ import annotations
import json
-import os
import subprocess
from dataclasses import dataclass
from pathlib import Path
@@ -86,13 +85,10 @@ def _parse_nonnegative(value: str | None) -> int | None:
# HERMES_INSTALL_ROOT (the Nix wrapper points it at the store path's
# share/hermes-agent, where the stamp is baked). One resolution path for
# every steward; no stamp-specific env override.
-_CODE_ROOT = Path(__file__).parent.parent
-
-
def _resolve_stamp_file() -> Path | None:
- root = os.environ.get("HERMES_INSTALL_ROOT")
- base = Path(root) if root else _CODE_ROOT
- p = base / "install-stamp.json"
+ from pm.paths import install_root
+
+ p = install_root() / "install-stamp.json"
return p if p.is_file() else None
diff --git a/pm/paths.py b/pm/paths.py
index bf1b05587f1..2904023a28d 100644
--- a/pm/paths.py
+++ b/pm/paths.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import os
from pathlib import Path
@@ -9,6 +10,14 @@ def repo_root() -> Path:
return Path(__file__).resolve().parent.parent
+def install_root() -> Path:
+ """The tree this process runs from. ``HERMES_INSTALL_ROOT`` when a steward
+ wrapper sets it (Nix points it at the sealed tree whose install stamp lives
+ outside the package dir), else the executing checkout."""
+ env = os.environ.get("HERMES_INSTALL_ROOT")
+ return Path(env) if env else repo_root()
+
+
def lockfile_path() -> Path:
return Path(__file__).resolve().parent / "lock.json"
diff --git a/pm/runtime.py b/pm/runtime.py
index 1788aaa5c35..f6d7af7c36c 100644
--- a/pm/runtime.py
+++ b/pm/runtime.py
@@ -61,8 +61,9 @@ def _resident_runtime() -> tuple[Path, Path] | None:
if payload is not None:
runtime = payload / "pm-runtime"
else:
- install_root = Path(os.environ.get("HERMES_INSTALL_ROOT") or project)
- stamp_path = install_root / "install-stamp.json"
+ from pm.paths import install_root
+
+ stamp_path = install_root() / "install-stamp.json"
try:
stamp = json.loads(stamp_path.read_text(encoding="utf-8-sig"))
except FileNotFoundError:
diff --git a/pm/store.py b/pm/store.py
index 5d3c523feb1..a828edde8ed 100644
--- a/pm/store.py
+++ b/pm/store.py
@@ -15,7 +15,6 @@ from pathlib import Path
from pm.filesystem import is_junction
-_UA = {"User-Agent": "hermes-pm"}
ALL_TARGETS = (
@@ -116,7 +115,7 @@ def hash_url(url: str) -> str:
import http.client
import urllib.request
- from pm.downloader import _OPENER
+ from pm.downloader import _OPENER, _UA
from pm.network import retry_network
def request():
diff --git a/tests/hermes_cli/test_banner_distribution_label.py b/tests/hermes_cli/test_banner_distribution_label.py
index 4b6c572013e..e23703fdaa0 100644
--- a/tests/hermes_cli/test_banner_distribution_label.py
+++ b/tests/hermes_cli/test_banner_distribution_label.py
@@ -22,7 +22,7 @@ def desktop_stamp(tmp_path, monkeypatch):
"displayVersion": "0.17.6", "baseVersion": "0.17.6", "tag": tag,
}))
monkeypatch.setattr("hermes_cli.config.get_project_root", lambda: root)
- monkeypatch.setattr("hermes_cli.version_info._CODE_ROOT", root)
+ monkeypatch.setattr("pm.paths.repo_root", lambda: root)
monkeypatch.delenv("HERMES_INSTALL_ROOT", raising=False)
from hermes_cli.version_info import _reset_version_info_cache
_reset_version_info_cache()
diff --git a/tests/hermes_cli/test_boot_wiring.py b/tests/hermes_cli/test_boot_wiring.py
index f53dc5ecfbb..bbc057f3578 100644
--- a/tests/hermes_cli/test_boot_wiring.py
+++ b/tests/hermes_cli/test_boot_wiring.py
@@ -57,9 +57,9 @@ def test_cli_main_runs_boot_bootstrap_once(hermes_home, boot_probe, capsys):
assert len(boot_probe) == 1, "every dispatch through main() reaches the registry"
# the root probed is THIS checkout (identity: git HEAD)
- import hermes_cli.boot_bootstrap as bb
+ from pm.paths import install_root
- assert boot_probe[0] == str(bb.default_project_root())
+ assert boot_probe[0] == str(install_root())
def test_cli_main_skips_boot_bootstrap_during_update(hermes_home, boot_probe):
diff --git a/tests/hermes_cli/test_commit_build_updates.py b/tests/hermes_cli/test_commit_build_updates.py
index 0337e34f62a..3a4d455a22c 100644
--- a/tests/hermes_cli/test_commit_build_updates.py
+++ b/tests/hermes_cli/test_commit_build_updates.py
@@ -21,7 +21,7 @@ def commit_build(tmp_path, monkeypatch):
}))
monkeypatch.setattr("hermes_cli.image_provenance.IMAGE_PROVENANCE_PATH", tmp_path / "absent")
monkeypatch.setattr("hermes_cli.config.get_project_root", lambda: root)
- monkeypatch.setattr("hermes_cli.version_info._CODE_ROOT", root)
+ monkeypatch.setattr("pm.paths.repo_root", lambda: root)
monkeypatch.delenv("HERMES_INSTALL_ROOT", raising=False)
from hermes_cli.version_info import _reset_version_info_cache
_reset_version_info_cache()
diff --git a/tests/hermes_cli/test_version_info.py b/tests/hermes_cli/test_version_info.py
index c82c57d42cd..f9ce8979d76 100644
--- a/tests/hermes_cli/test_version_info.py
+++ b/tests/hermes_cli/test_version_info.py
@@ -196,6 +196,6 @@ def test_resolve_stamp_file_falls_back_to_code_root_when_env_unset(tmp_path, mon
monkeypatch.delenv("HERMES_INSTALL_ROOT", raising=False)
stamp = {"commit": "f" * 40, "source": "docker", "updateMechanism": "external"}
(tmp_path / "install-stamp.json").write_text(json.dumps(stamp))
- monkeypatch.setattr("hermes_cli.version_info._CODE_ROOT", tmp_path)
+ monkeypatch.setattr("pm.paths.repo_root", lambda: tmp_path)
assert _resolve_stamp_file() == tmp_path / "install-stamp.json"