Skip to content

The agent that grows with you

fix(home): mkdir_under_hermes_home always takes the elevated-grant path; no ImportError fallback

Teknium6e1a8e057aa7
diff --git a/hermes_constants.py b/hermes_constants.py
index 43e219010ea..30252854c2e 100644
--- a/hermes_constants.py
+++ b/hermes_constants.py
@@ -429,12 +429,11 @@ def mkdir_under_hermes_home(path: str | Path) -> Path:
"""Create *path*, but never materialize a deleted/missing named profile."""
target = Path(path)
assert_named_profile_home_live(target)
- try: # lazy: this module is in the launcher's bootstrap closure and must import alone
- from hermes_dir_acl import make_user_dir
- except ImportError:
- target.mkdir(parents=True, exist_ok=True)
- else:
- make_user_dir(target, parents=True, exist_ok=True)
+ # Lazy so this module still imports alone; hermes_dir_acl (stdlib only) ships beside it in every
+ # closure that creates home dirs, and a missing copy fails loudly instead of silently skipping the
+ # elevated grant (tests/pm/test_bootstrap_import_closure.py).
+ from hermes_dir_acl import make_user_dir
+ make_user_dir(target, parents=True, exist_ok=True)
return target
diff --git a/tests/pm/test_bootstrap_import_closure.py b/tests/pm/test_bootstrap_import_closure.py
index 3c1f794ff46..e2ac0a875c5 100644
--- a/tests/pm/test_bootstrap_import_closure.py
+++ b/tests/pm/test_bootstrap_import_closure.py
@@ -38,6 +38,48 @@ print(root)
assert Path(result.stdout.strip()) == store
+
+def _constants_stage(tmp_path, *, with_acl: bool) -> Path:
+ repo = Path(__file__).resolve().parents[2]
+ stage = tmp_path / "stage"
+ stage.mkdir()
+ names = ["hermes_constants.py", "hermes_dir_acl.py"] if with_acl else ["hermes_constants.py"]
+ for name in names:
+ shutil.copy2(repo / name, stage / name)
+ return stage
+
+
+def _mkdir_in_stage(stage: Path, home: Path) -> subprocess.CompletedProcess:
+ script = "import hermes_constants, sys; print(hermes_constants.mkdir_under_hermes_home(sys.argv[1]))"
+ return subprocess.run([sys.executable, "-S", "-c", script, str(home / "logs")], cwd=stage,
+ env=dict(os.environ, HERMES_HOME=str(home), PYTHONPATH=str(stage)),
+ capture_output=True, text=True, timeout=15, check=False)
+
+
+def test_home_mkdir_carries_the_acl_module_in_the_closure(tmp_path):
+ """hermes_dir_acl is stdlib-only and ships beside hermes_constants: the two together create a
+ home dir, and hermes_constants still imports alone (the import is lazy)."""
+ home = tmp_path / "home"
+ home.mkdir()
+ result = _mkdir_in_stage(_constants_stage(tmp_path, with_acl=True), home)
+ assert result.returncode == 0, result.stderr
+ assert (home / "logs").is_dir()
+
+
+def test_home_mkdir_never_silently_skips_the_elevated_grant(tmp_path):
+ """Review #140490: an ImportError fallback to plain mkdir made whether an elevated run grants
+ the user access depend on which files happened to be present. A missing module is loud."""
+ home = tmp_path / "home"
+ home.mkdir()
+ stage = _constants_stage(tmp_path, with_acl=False)
+ alone = subprocess.run([sys.executable, "-S", "-c", "import hermes_constants"], cwd=stage,
+ env=dict(os.environ, PYTHONPATH=str(stage)), capture_output=True, text=True,
+ timeout=15, check=False)
+ assert alone.returncode == 0, alone.stderr
+ result = _mkdir_in_stage(stage, home)
+ assert result.returncode != 0 and "hermes_dir_acl" in result.stderr, result.stdout + result.stderr
+
+
def test_managed_python_signing_import_needs_no_pm_or_application(tmp_path):
repo = Path(__file__).resolve().parents[2]
script = """