diff --git a/hermes_cli/codex_runtime_plugin_migration.py b/hermes_cli/codex_runtime_plugin_migration.py index dd7faa097943f..6a1572f515cf3 100644 --- a/hermes_cli/codex_runtime_plugin_migration.py +++ b/hermes_cli/codex_runtime_plugin_migration.py @@ -42,6 +42,8 @@ from pathlib import Path from typing import Any, Optional +from hermes_constants import get_hermes_home + logger = logging.getLogger(__name__) @@ -445,9 +447,16 @@ def _build_hermes_tools_mcp_entry() -> dict: env: dict[str, str] = {} # HERMES_HOME passes through if set so the MCP subprocess sees the # same config / auth / sessions DB as the parent CLI. - hermes_home = os.environ.get("HERMES_HOME") + # Derive HERMES_HOME canonically from get_hermes_home() rather than + # reading os.environ at migrate-time. This prevents pytest tempdir + # leakage into user's real ~/.codex/config.toml (Bug #26250-C). + # Sanity check: the path must exist and not be a pytest tempdir. + hermes_home = str(get_hermes_home()) if hermes_home: - env["HERMES_HOME"] = hermes_home + # Sanity checks: path must exist and not be a pytest tempdir + hermes_path = Path(hermes_home) + if hermes_path.exists() and "pytest-of-" not in hermes_home: + env["HERMES_HOME"] = hermes_home # PYTHONPATH passes through so a worktree-launched hermes finds the # branch's modules instead of the installed package. pythonpath = os.environ.get("PYTHONPATH") diff --git a/tests/hermes_cli/test_codex_runtime_plugin_migration.py b/tests/hermes_cli/test_codex_runtime_plugin_migration.py index b2e27f8c97bc1..2dce7db22a80a 100644 --- a/tests/hermes_cli/test_codex_runtime_plugin_migration.py +++ b/tests/hermes_cli/test_codex_runtime_plugin_migration.py @@ -635,3 +635,78 @@ def test_summary_reports_migration_count(self, tmp_path): assert "Migrated 2 MCP server(s)" in summary assert "- a" in summary assert "- b" in summary + +class TestHERMESHomeSanity: + """Bug #26250-C: HERMES_HOME should not leak pytest tempdir into config.toml.""" + + def test_pytest_tempdir_blocked(self, tmp_path, monkeypatch): + """When HERMES_HOME points at a pytest tempdir, it should NOT be written.""" + import os + + # Simulate pytest monkeypatch setting HERMES_HOME to a tempdir + fake_tempdir = tmp_path / "pytest-of-test123" + fake_tempdir.mkdir(exist_ok=True) + + # Save original HERMES_HOME + original_hermes_home = os.environ.get("HERMES_HOME") + output_content = None + try: + monkeypatch.setenv("HERMES_HOME", str(fake_tempdir)) + report = migrate( + {"mcp_servers": {"test-server": {"command": "echo"}}}, + codex_home=tmp_path, + expose_hermes_tools=False, + ) + # Cache output before pytest cleans up tmp_path + output_content = (tmp_path / "config.toml").read_text() + finally: + # Restore original HERMES_HOME + if original_hermes_home is None: + os.environ.pop("HERMES_HOME", None) + else: + os.environ["HERMES_HOME"] = original_hermes_home + + # Verify migration succeeded + assert report.written + # BUT HERMES_HOME from tempdir should NOT be in the output + assert output_content is not None + assert "HERMES_HOME" not in output_content, ( + "HERMES_HOME from pytest tempdir leaked into config.toml" + ) + # The test-server entry should still be there + assert "[mcp_servers.test-server]" in output_content + + def test_nonexistent_hermes_home_blocked(self, tmp_path, monkeypatch): + """When HERMES_HOME points at a nonexistent path, it should NOT be written.""" + import os + + fake_path = tmp_path / "does-not-exist" / ".hermes" + + # Save original HERMES_HOME + original_hermes_home = os.environ.get("HERMES_HOME") + output_content = None + try: + monkeypatch.setenv("HERMES_HOME", str(fake_path)) + report = migrate( + {"mcp_servers": {"test-server": {"command": "echo"}}}, + codex_home=tmp_path, + expose_hermes_tools=False, + ) + # Cache output before pytest cleans up tmp_path + output_content = (tmp_path / "config.toml").read_text() + finally: + # Restore original HERMES_HOME + if original_hermes_home is None: + os.environ.pop("HERMES_HOME", None) + else: + os.environ["HERMES_HOME"] = original_hermes_home + + # Verify migration succeeded + assert report.written + # BUT HERMES_HOME for a nonexistent path should NOT be in the output + assert output_content is not None + assert "HERMES_HOME" not in output_content, ( + "HERMES_HOME for nonexistent path leaked into config.toml" + ) + # The test-server entry should still be there + assert "[mcp_servers.test-server]" in output_content