Files
nousresearch--hermes-agent/tests/agent/test_compression_count_warning_36908.py
T
wehub-resource-sync b4fbd6fe9f
Deploy Site / deploy-vercel (push) Has been skipped
Deploy Site / deploy-docs (push) Has been skipped
Build Skills Index / build-index (push) Has been skipped
CI / Deny unrelated histories (push) Has been skipped
CI / Detect affected areas (push) Successful in 27m35s
CI / OSV scan (push) Failing after 4s
CI / Build&Test Docker image (push) Successful in 9s
CI / Supply-chain scan (push) Has been skipped
CI / Lint Docker scripts (push) Failing after 5m13s
CI / Check contributors (push) Failing after 12m8s
CI / Docs Site (push) Failing after 12m8s
CI / TypeScript (push) Failing after 12m8s
CI / Python lints (push) Failing after 12m9s
CI / Python tests (push) Failing after 12m9s
CI / Check uv.lock (push) Failing after 23m22s
CI / CI timing report (push) Has been cancelled
Build Skills Index / trigger-deploy (push) Has been cancelled
CI / All required checks pass (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:56:03 +08:00

88 lines
3.3 KiB
Python

"""Regression for #36908: the repeated-compression warning must reach the
TUI / gateway, not just CLI stdout.
When a session is compressed >= 2 times, ``compress_context`` warns that
accuracy may degrade. That warning used to go through ``_vprint`` (stdout
only), so the Ink TUI / Telegram / Discord never saw it — unlike the two
other compression warnings in the same module, which route through
``_emit_status`` (and store ``_compression_warning`` for late-bound
gateway replay). This pins the warning onto the gateway-aware channel.
"""
from __future__ import annotations
import os
from pathlib import Path
from unittest.mock import MagicMock, patch
from hermes_state import SessionDB
def _build_agent_with_db(db: SessionDB, session_id: str, compression_count: int):
with patch.dict(os.environ, {"OPENROUTER_API_KEY": "test-key"}):
from run_agent import AIAgent
agent = AIAgent(
api_key="test-key",
base_url="https://openrouter.ai/api/v1",
model="test/model",
quiet_mode=True,
session_db=db,
session_id=session_id,
skip_context_files=True,
skip_memory=True,
)
compressor = MagicMock()
compressor.compress.return_value = [
{"role": "user", "content": "[CONTEXT COMPACTION] summary"},
{"role": "user", "content": "tail"},
]
compressor.compression_count = compression_count
compressor.last_prompt_tokens = 0
compressor.last_completion_tokens = 0
compressor._last_summary_error = None
compressor._last_compress_aborted = False
compressor._last_aux_model_failure_model = None
compressor._last_aux_model_failure_error = None
agent.context_compressor = compressor
return agent
def test_repeated_compression_warning_routed_through_emit_status(tmp_path: Path) -> None:
db = SessionDB(db_path=tmp_path / "state.db")
sid = "PARENT_36908"
db.create_session(sid, source="cli")
# compression_count == 2 → the "compressed N times" warning should fire.
agent = _build_agent_with_db(db, sid, compression_count=2)
emitted: list[str] = []
agent._emit_status = lambda message: emitted.append(message)
messages = [{"role": "user", "content": f"m{i}"} for i in range(20)]
agent._compress_context(messages, "sys", approx_tokens=120_000)
# The warning reached the gateway-aware channel...
assert any("compressed 2 times" in m.lower() for m in emitted), (
f"repeated-compression warning not emitted via _emit_status: {emitted}"
)
# ...and was stored for late-bound gateway status_callback replay.
assert "compressed 2 times" in (getattr(agent, "_compression_warning", "") or "").lower()
def test_no_warning_below_threshold(tmp_path: Path) -> None:
db = SessionDB(db_path=tmp_path / "state.db")
sid = "PARENT_36908_ONCE"
db.create_session(sid, source="cli")
# compression_count == 1 → no repeated-compression warning.
agent = _build_agent_with_db(db, sid, compression_count=1)
emitted: list[str] = []
agent._emit_status = lambda message: emitted.append(message)
messages = [{"role": "user", "content": f"m{i}"} for i in range(20)]
agent._compress_context(messages, "sys", approx_tokens=120_000)
assert not any("compressed" in m.lower() and "times" in m.lower() for m in emitted)