Files
hermes-agent/tests/hermes_cli/test_cli_manual_compress.py
teknium1 b002dfc04d fix: pruned skill_view/read_file results reload after a proactive prune
A committed proactive tool-result prune (`prune_tool_results_only`, driven from
`agent/turn_preflight.py::compress_after_tool_results`) demotes old skill_view and
read_file bodies to one-line markers, but only the full-compaction path called
`_reset_read_dedup_caches`. The repeat-view dedup therefore kept answering
"unchanged / content_returned: false" for content that no longer existed in the
transcript, so the `[SKILL_PRUNED: ... reload with skill_view(...)]` marker asked
for a reload the tool then refused (#112763). Treat the committed prune as the same
content-loss boundary compaction already is: reset the task's read/skill dedup right
where the pruned list is committed.

Sibling surface: manual `/compress` on CLI, TUI and the messaging gateway called
`compress_now()` without `task_id`, so the boundary reset hit the "default" bucket
instead of the session's. Pass the session-scoped task id on all three.

Test double in tests/hermes_cli/test_cli_manual_compress.py gains the `task_id`
kwarg the real `_compress_context` facade already accepts.

Supersedes #103268 (@jo0wz), which reached the same path with a process-global
ghost registry (not task-keyed, skill_view only).
2026-09-16 17:23:44 -07:00

121 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,
task_id="default",
):
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 == []