Files
hermes-agent/tests/hermes_cli/test_cli_manual_compress.py
T
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

120 lines
4.1 KiB
Python

from contextlib import nullcontext
from agent.conversation_compression import (
_queue_context_engine_compression_notification,
)
from cli import HermesCLI
class DummyAgent:
def __init__(self):
self.compression_enabled = True
self._cached_system_prompt = "FULL CACHED SYSTEM PROMPT SHOULD NOT BE NESTED"
self.session_id = "new-session"
self.calls = []
self.flush_calls = []
self.flush_error = None
self.host_events = []
self.boundary_calls = []
self.context_compressor = type("ContextEngineStub", (), {})()
self.context_compressor.on_session_start = self._record_boundary
def _record_boundary(self, session_id, **kwargs):
self.host_events.append("notify")
self.boundary_calls.append((session_id, kwargs))
def _flush_messages_to_session_db(self, messages, _session_id=None):
self.host_events.append("persist")
self.flush_calls.append((list(messages), _session_id))
if self.flush_error is not None:
raise self.flush_error
def _compress_context(
self,
messages,
system_message,
*,
approx_tokens=None,
focus_topic=None,
force=False,
defer_context_engine_notification=False,
):
self.calls.append(
{
"messages": messages,
"system_message": system_message,
"approx_tokens": approx_tokens,
"focus_topic": focus_topic,
"force": force,
"defer_context_engine_notification": (
defer_context_engine_notification
),
}
)
if defer_context_engine_notification:
_queue_context_engine_compression_notification(
self,
new_session_id=self.session_id,
old_session_id="old-session",
)
return ([{"role": "user", "content": "[CONTEXT SUMMARY]: compacted"}], "new system prompt")
def test_manual_compress_does_not_pass_cached_system_prompt(monkeypatch):
"""Manual /compress should rebuild the next prompt without nesting the old one."""
cli = HermesCLI.__new__(HermesCLI)
cli.conversation_history = [
{"role": "user", "content": "one"},
{"role": "assistant", "content": "two"},
{"role": "user", "content": "three"},
{"role": "assistant", "content": "four"},
]
cli.agent = DummyAgent()
cli.session_id = "old-session"
cli._pending_title = "old title"
cli._busy_command = lambda _message, **_kwargs: nullcontext()
monkeypatch.setattr(
"agent.manual_compression_feedback.summarize_manual_compression",
lambda *args, **kwargs: {
"noop": False,
"headline": "compressed",
"token_line": "tokens reduced",
"note": "",
},
)
cli._manual_compress("/compress database schema")
assert len(cli.agent.calls) == 1
call = cli.agent.calls[0]
assert call["system_message"] is None
assert call["system_message"] != cli.agent._cached_system_prompt
assert call["focus_topic"] == "database schema"
assert cli.session_id == "new-session"
assert cli._pending_title is None
assert len(cli.agent.flush_calls) == 1
assert cli.agent.host_events == ["persist", "notify"]
assert len(cli.agent.boundary_calls) == 1
def test_manual_compress_flush_failure_discards_notification(monkeypatch):
cli = HermesCLI.__new__(HermesCLI)
cli.conversation_history = [
{"role": "user", "content": "one"},
{"role": "assistant", "content": "two"},
{"role": "user", "content": "three"},
{"role": "assistant", "content": "four"},
]
cli.agent = DummyAgent()
cli.agent.flush_error = RuntimeError("synthetic child flush failure")
cli.session_id = "old-session"
cli._pending_title = "old title"
cli._busy_command = lambda _message, **_kwargs: nullcontext()
cli._manual_compress("/compress")
assert len(cli.agent.flush_calls) == 1
assert cli.agent.host_events == ["persist"]
assert cli.agent.boundary_calls == []