Skip to content

The agent that grows with you

fix(pm): support junction checks during legacy Python bootstrap

ethernet3af8084671e0
diff --git a/hermes_cli/data_cleanup.py b/hermes_cli/data_cleanup.py
index dc9d38293b6..082976ceadc 100644
--- a/hermes_cli/data_cleanup.py
+++ b/hermes_cli/data_cleanup.py
@@ -5,6 +5,8 @@ from dataclasses import dataclass
from pathlib import Path
import stat
+from pm.filesystem import is_junction
+
@dataclass(frozen=True)
class DataRemovalPlan:
@@ -54,7 +56,7 @@ def plan_data_removal(home: Path, project: Path, userdata: Path | None = None) -
resolved = path.resolve()
if any(path == root or resolved == root or path.is_relative_to(root) for root in protected):
keep.add(path)
- elif path.is_symlink() or path.is_junction():
+ elif path.is_symlink() or is_junction(path):
remove.append(path) # Remove the directory entry, never its target.
elif any(root.is_relative_to(path) for root in protected):
if stat.S_ISDIR(mode):
@@ -89,7 +91,7 @@ def remove_data(plan: DataRemovalPlan) -> tuple[list[Path], list[tuple[Path, str
mode = path.lstat().st_mode
if path.is_symlink() or not stat.S_ISDIR(mode):
path.unlink()
- elif path.is_junction():
+ elif is_junction(path):
path.rmdir()
else:
shutil.rmtree(path)
diff --git a/pm/filesystem.py b/pm/filesystem.py
new file mode 100644
index 00000000000..df86354f550
--- /dev/null
+++ b/pm/filesystem.py
@@ -0,0 +1,11 @@
+"""Filesystem checks used before PM can replace the caller's interpreter."""
+from __future__ import annotations
+
+import os
+from pathlib import Path
+import stat
+
+
+def is_junction(path: Path) -> bool:
+ """Keep junctions opaque even before Python 3.12's Path.is_junction exists."""
+ return os.name == "nt" and path.lstat().st_reparse_tag == stat.IO_REPARSE_TAG_MOUNT_POINT
diff --git a/pm/store.py b/pm/store.py
index 402d401f521..c663721b4b2 100644
--- a/pm/store.py
+++ b/pm/store.py
@@ -13,6 +13,8 @@ import time
from contextlib import contextmanager
from pathlib import Path
+from pm.filesystem import is_junction
+
_UA = {"User-Agent": "hermes-pm"}
@@ -236,7 +238,7 @@ def tree_digest(root: Path) -> str:
descend = []
for name in sorted(dirnames):
path = Path(dirpath) / name
- if path.is_symlink() or path.is_junction():
+ if path.is_symlink() or is_junction(path):
files.append((path.relative_to(root).as_posix(), path))
elif name != "__pycache__":
descend.append(name)
@@ -250,7 +252,7 @@ def tree_digest(root: Path) -> str:
for rel, path in files:
digest.update(rel.encode("utf-8"))
digest.update(b"\0")
- if path.is_symlink() or path.is_junction():
+ if path.is_symlink() or is_junction(path):
digest.update(os.readlink(path).encode("utf-8"))
else:
with open(path, "rb") as f:
diff --git a/tests/hermes_cli/test_data_uninstall.py b/tests/hermes_cli/test_data_uninstall.py
index 5dae9459d86..fc0edcb8e68 100644
--- a/tests/hermes_cli/test_data_uninstall.py
+++ b/tests/hermes_cli/test_data_uninstall.py
@@ -42,6 +42,7 @@ def test_data_only_preserves_runtime_and_sibling_homes(layout, monkeypatch, mode
import json
from hermes_cli.runtime_paths import install_state_dir, runtime_facts_path, selected_venv
+ monkeypatch.delattr(Path, "is_junction", raising=False)
home, witnesses, data = layout
source = uninstall.get_project_root()
generation = install_state_dir(source) / "environments" / "selected"
diff --git a/tests/pm/test_cold_runtime_e2e.py b/tests/pm/test_cold_runtime_e2e.py
index 7cf7bcbe590..16409624b06 100644
--- a/tests/pm/test_cold_runtime_e2e.py
+++ b/tests/pm/test_cold_runtime_e2e.py
@@ -50,7 +50,8 @@ def _bare(python, repo, code, *, env, expected=0):
@pytest.mark.platforms("linux")
-def test_cold_cli_builds_own_runtime_discovers_plugins_and_repairs_app(tmp_path):
[email protected]("bootstrap_name", [None, "python3.11"])
+def test_cold_cli_builds_own_runtime_discovers_plugins_and_repairs_app(tmp_path, bootstrap_name):
from pm.packages import Python, Uv
from pm.store import current_target, tree_digest
@@ -63,6 +64,9 @@ def test_cold_cli_builds_own_runtime_discovers_plugins_and_repairs_app(tmp_path)
python = Path(sys._base_executable).resolve()
if sys.version_info[:2] != (3, 14):
pytest.skip("the checked-in PM runtime currently requires Python 3.14")
+ bootstrap_python = shutil.which(bootstrap_name) if bootstrap_name else python
+ if bootstrap_python is None:
+ pytest.skip(f"{bootstrap_name} must be on PATH for the legacy bootstrap test")
source = Path(__file__).resolve().parents[2]
repo = tmp_path / "source"
@@ -150,9 +154,31 @@ assert importlib.util.find_spec('idna') is None
try:
assert not store.exists()
assert not (hermes_home / "installs").exists()
- result = _bare(python, repo, bootstrap + cli.format(action="install"), env=env)
+ result = _bare(bootstrap_python, repo, bootstrap + cli.format(action="install"), env=env)
assert "✓ venv" in result.stdout
assert "Preparing the isolated PM runtime" in result.stderr
+ if bootstrap_name:
+ # The installed launcher still starts on the old interpreter after
+ # a source swap. Completion must re-exec before importing the app.
+ (repo / ".git").mkdir()
+ (repo / "install-stamp.json").write_text(
+ json.dumps({"updateMechanism": "self"}), encoding="utf-8",
+ )
+ lock = repo / "uv.lock"
+ lock.write_bytes(lock.read_bytes() + b"\n# source update\n")
+ entry = repo / "launch_probe.py"
+ entry.write_text(
+ "import hermes_bootstrap\n"
+ "import idna, json, sys\n"
+ "print(json.dumps({'python': sys.executable, 'version': list(sys.version_info[:2]), "
+ "'idna': idna.__file__}))\n", encoding="utf-8",
+ )
+ launched = _run([str(bootstrap_python), "-B", str(entry)], cwd=repo, env=env)
+ launch_report = json.loads(launched.stdout)
+ assert Path(launch_report["python"]).is_relative_to(store)
+ assert launch_report["version"] == list(sys.version_info[:2])
+ assert Path(launch_report["idna"]).is_relative_to(hermes_home / "installs")
+ assert launched.stderr.count("completing source-update dependencies") == 1
finally:
server.shutdown()
server.server_close()
diff --git a/tests/pm/test_tree_digest_links.py b/tests/pm/test_tree_digest_links.py
index 44d1cbb0f2b..f11ac1ab866 100644
--- a/tests/pm/test_tree_digest_links.py
+++ b/tests/pm/test_tree_digest_links.py
@@ -7,6 +7,20 @@ import pytest
from pm.store import tree_digest
+def test_digest_without_pathlib_junction_api(tmp_path, monkeypatch):
+ # Source updates reach the store before replacing a pre-3.12 interpreter.
+ monkeypatch.delattr(Path, "is_junction", raising=False)
+ root = tmp_path / "tree"
+ nested = root / "lib"
+ nested.mkdir(parents=True)
+ payload = nested / "payload"
+ payload.write_bytes(b"before")
+ first = tree_digest(root)
+ assert tree_digest(root) == first
+ payload.write_bytes(b"after")
+ assert tree_digest(root) != first
+
+
@pytest.mark.platforms("windows", "posix")
def test_directory_links_bind_only_their_target_text(tmp_path):
root = tmp_path / "tree"
@@ -39,9 +53,10 @@ def test_directory_links_bind_only_their_target_text(tmp_path):
@pytest.mark.platforms("windows")
-def test_junctions_bind_target_text_without_walking_outside(tmp_path):
+def test_junctions_bind_target_text_without_walking_outside(tmp_path, monkeypatch):
import subprocess
+ monkeypatch.delattr(Path, "is_junction", raising=False)
root = tmp_path / "tree"
root.mkdir()
left, right = tmp_path / "left", tmp_path / "right"
@@ -57,7 +72,6 @@ def test_junctions_bind_target_text_without_walking_outside(tmp_path):
capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=15,
)
assert result.returncode == 0, result.stdout + result.stderr
- assert junction.is_junction()
point_at(left)
try: