diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.py index fe582aca98..50373ca79d 100644 --- a/hermes_cli/profiles.py +++ b/hermes_cli/profiles.py @@ -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}" diff --git a/tests/tools/test_local_env_blocklist.py b/tests/tools/test_local_env_blocklist.py index 74573e2396..2e61c4ab66 100644 --- a/tests/tools/test_local_env_blocklist.py +++ b/tests/tools/test_local_env_blocklist.py @@ -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. diff --git a/tools/environments/local.py b/tools/environments/local.py index 017cc3e82a..761581a518 100644 --- a/tools/environments/local.py +++ b/tools/environments/local.py @@ -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 /profiles/. 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)