fix(tools): keep junction lexical root across profile re-home
Confirmed on native Windows 11 with a real junction and the real startup chain: when the desktop/CLI spawns the backend with HERMES_HOME in the configured (lexical) spelling and --profile / sticky active_profile is in play, _apply_profile_override() re-homes HERMES_HOME through resolve_profile_env(), which resolves the junction under the platform default and returns the PHYSICAL spelling. tools.environments.local is imported after that mutation, so _hermes_repo_root_aliases is built from the physical home, the lexical repo-root spelling written into PYTHONPATH by the launcher (D:\hermes\hermes-agent) is not derivable, and the entry survives stripping (reproduced: cases --profile default / named / sticky active_profile / cross-drive junction all leave it in place; no-profile strips it). Two narrow changes, no heuristics, no new env vars: - hermes_cli/profiles.py::resolve_profile_env: when HERMES_HOME is set, the configured spelling IS the launch root (junction-transparent, physically identical dirs); keep it instead of re-deriving the native default. This is the same producer contract _preserve_hermes_home_path already follows. - tools/environments/local.py::_build_hermes_repo_root_aliases: when the configured home is a profile home (<root>/profiles/<name>), also derive the root spelling lexically (parent of the profiles component, same rule get_default_hermes_root uses) and run the exact-ownership mapping against it, so the launcher's lexical root is recovered after re-home without ever matching arbitrary descendants of HERMES_HOME. Regression test test_profile_rehome_keeps_junction_lexical_alias covers junction + profile re-home + inherited lexical PYTHONPATH end to end.
This commit is contained in:
+20
-2
@@ -2360,12 +2360,30 @@ def resolve_profile_env(profile_name: str) -> str:
|
||||
|
||||
Called early in the CLI entry point, before any hermes modules
|
||||
are imported, to set the HERMES_HOME environment variable.
|
||||
|
||||
When HERMES_HOME is already set, the configured spelling IS the
|
||||
launch root (it may be a junction/symlink alias of the platform
|
||||
default). Keep that spelling so profile re-home does not destroy
|
||||
the launcher's lexical provenance -- the subprocess sanitizer needs
|
||||
it to match Hermes-owned PYTHONPATH entries written in the same
|
||||
spelling (#82581 junction follow-up). Physically the paths are
|
||||
identical (junction-transparent); only the spelling is preserved.
|
||||
"""
|
||||
canon = normalize_profile_name(profile_name)
|
||||
validate_profile_name(canon)
|
||||
profile_dir = get_profile_dir(canon)
|
||||
env_home = os.environ.get("HERMES_HOME", "").strip()
|
||||
if env_home:
|
||||
env_path = Path(env_home)
|
||||
# A profile-shaped env value means the root is the grandparent
|
||||
# (mirrors get_default_hermes_root()).
|
||||
root = env_path.parent.parent if env_path.parent.name == "profiles" else env_path
|
||||
else:
|
||||
root = _get_default_hermes_home()
|
||||
if canon == "default":
|
||||
return str(root)
|
||||
profile_dir = root / "profiles" / canon
|
||||
|
||||
if canon != "default" and not profile_dir.is_dir():
|
||||
if not profile_dir.is_dir():
|
||||
raise FileNotFoundError(
|
||||
f"Profile '{canon}' does not exist. "
|
||||
f"Create it with: hermes profile create {canon}"
|
||||
|
||||
@@ -928,6 +928,52 @@ class TestPythonpathSelectiveStrip:
|
||||
"/home/user/my-lib",
|
||||
]
|
||||
|
||||
def test_profile_rehome_keeps_junction_lexical_alias(self, tmp_path, monkeypatch):
|
||||
"""Profile re-home must not lose the launcher's lexical repo-root spelling.
|
||||
|
||||
The desktop/CLI spawn children with HERMES_HOME and PYTHONPATH in the
|
||||
configured (junction) spelling, but --profile / sticky active_profile
|
||||
re-home HERMES_HOME through resolve_profile_env() before the
|
||||
sanitizer loads. Regression (junction + profile re-home): the alias
|
||||
builder must still recover the lexical root so the inherited lexical
|
||||
repo-root entry is stripped.
|
||||
"""
|
||||
import tools.environments.local as local
|
||||
from hermes_cli.profiles import resolve_profile_env
|
||||
|
||||
physical_home = tmp_path / "physical-home"
|
||||
physical_root = physical_home / "hermes-agent"
|
||||
physical_root.mkdir(parents=True)
|
||||
(physical_home / "profiles" / "coder").mkdir(parents=True)
|
||||
configured_home = tmp_path / "configured-home"
|
||||
try:
|
||||
_make_directory_link(configured_home, physical_home)
|
||||
except OSError as exc:
|
||||
pytest.skip(f"directory link unavailable on this host: {exc}")
|
||||
|
||||
# Launcher contract: the configured spelling is the env and the root.
|
||||
monkeypatch.setenv("HERMES_HOME", str(configured_home))
|
||||
lexical_root = configured_home / "hermes-agent"
|
||||
|
||||
# Profile re-home keeps the configured spelling (physically identical
|
||||
# through the link; lexically the launcher spelling is preserved).
|
||||
assert Path(resolve_profile_env("default")) == configured_home
|
||||
assert Path(resolve_profile_env("coder")) == configured_home / "profiles" / "coder"
|
||||
|
||||
# The sanitizer now runs under the re-homed (profile) HERMES_HOME.
|
||||
aliases = local._build_hermes_repo_root_aliases(
|
||||
physical_root.resolve(),
|
||||
physical_root,
|
||||
configured_home / "profiles" / "coder",
|
||||
)
|
||||
assert any(local._same_path(a, lexical_root) for a in aliases)
|
||||
|
||||
monkeypatch.setattr(local, "_hermes_repo_root_aliases", aliases)
|
||||
env = {"PYTHONPATH": os.pathsep.join([str(lexical_root), "/home/user/my-lib"])}
|
||||
local._strip_hermes_owned_pythonpath(env)
|
||||
assert env["PYTHONPATH"].split(os.pathsep) == ["/home/user/my-lib"]
|
||||
|
||||
|
||||
def test_deep_path_under_repo_root_preserved(self):
|
||||
"""A deeper path under the repo root (depth=2) is preserved.
|
||||
|
||||
|
||||
@@ -1380,15 +1380,28 @@ def _build_hermes_repo_root_aliases(
|
||||
add(resolved_root)
|
||||
add(lexical_root)
|
||||
|
||||
try:
|
||||
resolved_home = configured_home.resolve()
|
||||
home_key = os.path.normcase(str(resolved_home))
|
||||
root_key = os.path.normcase(str(resolved_root))
|
||||
if os.path.commonpath([home_key, root_key]) == home_key:
|
||||
relative_root = os.path.relpath(str(resolved_root), str(resolved_home))
|
||||
add(configured_home / relative_root)
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
# Profile re-home: with --profile / sticky active_profile the configured
|
||||
# home becomes <root>/profiles/<name>. The repo root then lives beside
|
||||
# the profiles directory (not under the profile home), so the home-
|
||||
# relative mapping below cannot reach it. Derive the root spelling
|
||||
# lexically the same way get_default_hermes_root() does (parent of a
|
||||
# "profiles" component) and run the same exact-ownership mapping against
|
||||
# it -- this recovers the launcher's lexical root under profile re-home
|
||||
# while still never matching arbitrary descendants of HERMES_HOME.
|
||||
home_candidates = [configured_home]
|
||||
if configured_home.parent.name == "profiles":
|
||||
home_candidates.append(configured_home.parent.parent)
|
||||
|
||||
for home in home_candidates:
|
||||
try:
|
||||
resolved_home = home.resolve()
|
||||
home_key = os.path.normcase(str(resolved_home))
|
||||
root_key = os.path.normcase(str(resolved_root))
|
||||
if os.path.commonpath([home_key, root_key]) == home_key:
|
||||
relative_root = os.path.relpath(str(resolved_root), str(resolved_home))
|
||||
add(home / relative_root)
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
|
||||
return tuple(aliases)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user