test(agent): drive the profile hint through the real resolver chain
Review feedback: the previous tests mocked get_hermes_home, get_default_hermes_root and _resolve_active_profile_name, so they checked template rendering but never exercised the relationship that causes the defect — _resolve_active_profile_name returns a named profile only when the active home is already <root>/profiles/<name>, which is precisely why appending that suffix doubled it. Replace them with tests that set a real HERMES_HOME under a tmp root and mock no resolver. They assert the chain first (_resolve_active_profile_name == "coder", get_hermes_home == the profile dir, get_default_hermes_root == the root), then the rendered prompt. Adds the default-profile branch the same way. This also removes the order-dependent failures noted in the PR description. Module-attribute monkeypatching stops taking effect once other files in tests/agent/ have run, which is what made the mocked tests fail in a full-directory run; the un-mocked tests are immune. tests/agent/ now reports 87 failures — identical to the baseline on unmodified main, with none in this file. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -116,72 +116,68 @@ class TestCodingContextBlock:
|
||||
assert "coding agent" not in _stable_prompt(agent)
|
||||
|
||||
|
||||
class TestNamedProfileHint:
|
||||
"""The profile hint must point at real paths (#72894).
|
||||
class TestNamedProfileHintIntegration:
|
||||
"""The same defect through the REAL resolution chain (#72894).
|
||||
|
||||
``_resolve_active_profile_name()`` only returns a non-default name when
|
||||
``get_hermes_home()`` has already resolved to ``<root>/profiles/<name>``,
|
||||
so appending ``/profiles/<name>`` to it doubled the segment, and using it
|
||||
for the *default* profile's data pointed those inside the active profile.
|
||||
``TestNamedProfileHint`` mocks ``get_hermes_home``,
|
||||
``get_default_hermes_root`` and ``_resolve_active_profile_name``, so it
|
||||
validates template rendering but not the relationship that causes the bug:
|
||||
``_resolve_active_profile_name`` returns a named profile *only* when the
|
||||
active home is already ``<root>/profiles/<name>``, which is exactly why
|
||||
appending that suffix again doubled it. Drive it with a real
|
||||
``HERMES_HOME`` and no resolver mocks.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def _full_prompt(monkeypatch, agent):
|
||||
# Pin the prompt shape: with the coding posture off there is no
|
||||
# workspace snapshot, so the hint's position doesn't depend on the
|
||||
# cwd the suite happens to run from. Join both parts regardless.
|
||||
def test_real_hermes_home_under_profiles_renders_correct_paths(
|
||||
self, tmp_path, monkeypatch
|
||||
):
|
||||
root = tmp_path / ".hermes"
|
||||
profile_home = root / "profiles" / "coder"
|
||||
profile_home.mkdir(parents=True)
|
||||
|
||||
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
||||
monkeypatch.setenv("HERMES_HOME", str(profile_home))
|
||||
monkeypatch.delenv("TERMINAL_CWD", raising=False)
|
||||
|
||||
# Sanity-check the real chain before asserting on the prompt.
|
||||
from agent.file_safety import _resolve_active_profile_name
|
||||
from hermes_constants import get_default_hermes_root, get_hermes_home
|
||||
|
||||
assert _resolve_active_profile_name() == "coder"
|
||||
assert get_hermes_home() == profile_home
|
||||
assert get_default_hermes_root() == root
|
||||
|
||||
agent = _make_agent(valid_tool_names=["read_file"])
|
||||
with patch("agent.coding_context._coding_mode", return_value="off"):
|
||||
return "\n\n".join(_prompt_parts(agent).values())
|
||||
prompt = "\n\n".join(_prompt_parts(agent).values())
|
||||
|
||||
@classmethod
|
||||
def _named_profile_prompt(cls, monkeypatch, name="mac"):
|
||||
import agent.system_prompt as system_prompt
|
||||
assert "Active Hermes profile: coder." in prompt
|
||||
assert f"reads and writes {profile_home}/." in prompt
|
||||
# The doubled form must not appear anywhere.
|
||||
assert f"{profile_home}/profiles/coder" not in prompt
|
||||
# Default-profile pointers belong at the root, not inside the profile.
|
||||
assert f"The default profile's data lives at {root}/skills/" in prompt
|
||||
assert f"{profile_home}/skills/" not in prompt
|
||||
|
||||
def test_real_default_home_renders_default_branch(self, tmp_path, monkeypatch):
|
||||
"""HERMES_HOME at the root resolves to the default profile, unchanged."""
|
||||
root = tmp_path / ".hermes"
|
||||
root.mkdir(parents=True)
|
||||
|
||||
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
||||
monkeypatch.setenv("HERMES_HOME", str(root))
|
||||
monkeypatch.delenv("TERMINAL_CWD", raising=False)
|
||||
|
||||
from agent.file_safety import _resolve_active_profile_name
|
||||
|
||||
assert _resolve_active_profile_name() == "default"
|
||||
|
||||
agent = _make_agent(valid_tool_names=["read_file"])
|
||||
monkeypatch.setattr(
|
||||
system_prompt, "get_hermes_home", lambda: Path(f"/hermes/profiles/{name}")
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
system_prompt, "get_default_hermes_root", lambda: Path("/hermes")
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"agent.file_safety._resolve_active_profile_name", lambda: name
|
||||
)
|
||||
return cls._full_prompt(monkeypatch, agent)
|
||||
with patch("agent.coding_context._coding_mode", return_value="off"):
|
||||
prompt = "\n\n".join(_prompt_parts(agent).values())
|
||||
|
||||
def test_session_home_is_not_doubled(self, monkeypatch):
|
||||
prompt = self._named_profile_prompt(monkeypatch)
|
||||
|
||||
assert "This session reads and writes /hermes/profiles/mac/." in prompt
|
||||
assert "/hermes/profiles/mac/profiles/mac" not in prompt
|
||||
|
||||
def test_default_profile_data_points_at_the_root(self, monkeypatch):
|
||||
prompt = self._named_profile_prompt(monkeypatch)
|
||||
|
||||
assert (
|
||||
"The default profile's data lives at /hermes/skills/, "
|
||||
"/hermes/plugins/, /hermes/cron/, /hermes/memories/"
|
||||
) in prompt
|
||||
# Never inside the active profile — that's the cross-profile
|
||||
# confusion this hint exists to prevent.
|
||||
assert "/hermes/profiles/mac/skills/" not in prompt
|
||||
assert "/hermes/profiles/mac/memories/" not in prompt
|
||||
|
||||
def test_default_profile_hint_is_unchanged(self, monkeypatch):
|
||||
import agent.system_prompt as system_prompt
|
||||
|
||||
agent = _make_agent(valid_tool_names=["read_file"])
|
||||
monkeypatch.setattr(system_prompt, "get_hermes_home", lambda: Path("/hermes"))
|
||||
monkeypatch.setattr(
|
||||
"agent.file_safety._resolve_active_profile_name", lambda: "default"
|
||||
)
|
||||
prompt = self._full_prompt(monkeypatch, agent)
|
||||
|
||||
assert (
|
||||
"Active Hermes profile: default. Other profiles (if any) live "
|
||||
"under /hermes/profiles/<name>/."
|
||||
) in prompt
|
||||
assert "Active Hermes profile: default." in prompt
|
||||
assert f"under {root}/profiles/<name>/." in prompt
|
||||
|
||||
|
||||
def test_build_system_prompt_records_stable_prefix():
|
||||
|
||||
Reference in New Issue
Block a user