hermes-agent/tests/agent/test_manual_compression_feedback.py
Teknium eb7be2edde fix(compress): classify unconfirmed lock-acquire failures and cover all manual-compress surfaces
Follow-up to the salvaged #57634 commits:

- agent/manual_compression_feedback.py: new describe_compression_lock_skip()
  — single source of truth for lock-skip wording. A descriptive holder
  string means another compressor CONFIRMED holds the lock ('already in
  progress (holder: ...)'); True/None means acquisition failed without a
  confirmed holder (hermes_state.try_acquire_compression_lock catches
  sqlite3.Error internally and returns False), so the message says
  'could not acquire ... the lock check failed' instead of falsely
  claiming a concurrent compression is running.
- cli.py, gateway/slash_commands.py, tui_gateway/server.py (all three
  in-process consumers: session.compress RPC, command.dispatch compress
  branch, slash.exec mirror) now route through the shared helper.
- tui_gateway/server.py command.dispatch compress branch: catch
  CompressionLockHeld explicitly — it previously fell into the generic
  'compress failed' error handler.
- Deferred-notify contract (#69324): lock-skip discards the pending
  context-engine notification (committed=False) in _compress_session_history
  and the CLI path before returning.
- tests: lock-skip wording pins per surface, VISIBLE_COMPRESSION_MESSAGES
  noise-filter carve-outs for both wordings, MagicMock signal opt-outs for
  sibling tests added on main after the original PR.
2026-07-23 08:19:14 -07:00

111 lines
3.6 KiB
Python

"""Behavioral coverage for manual compression status messages."""
from types import SimpleNamespace
from agent.manual_compression_feedback import (
describe_compression_lock_skip,
summarize_manual_compression,
)
def _messages(count: int) -> list[dict[str, str]]:
return [
{"role": "user" if index % 2 == 0 else "assistant", "content": str(index)}
for index in range(count)
]
def test_aborted_compression_reports_preserved_messages_and_reason():
messages = _messages(12)
state = SimpleNamespace(
_last_compress_aborted=True,
_last_summary_fallback_used=False,
_last_summary_error=(
"Provider 'opencode-zen' is set in config.yaml but no API key was found."
),
)
feedback = summarize_manual_compression(
messages,
list(messages),
120_000,
120_000,
compression_state=state,
)
assert feedback["aborted"] is True
assert feedback["fallback_used"] is False
assert feedback["headline"] == "Compression aborted: 12 messages preserved"
assert "no messages were removed" in feedback["note"]
assert "no API key was found" in feedback["note"]
def test_failure_reason_redaction_is_forced_at_ui_boundary(monkeypatch):
messages = _messages(12)
fake_secret = "sk-proj-" + "X" * 40
state = SimpleNamespace(
_last_compress_aborted=True,
_last_summary_fallback_used=False,
_last_summary_error=f"provider rejected OPENAI_API_KEY={fake_secret}",
)
monkeypatch.setattr("agent.redact._REDACT_ENABLED", False, raising=False)
feedback = summarize_manual_compression(
messages,
list(messages),
120_000,
120_000,
compression_state=state,
)
assert fake_secret not in feedback["note"]
assert "OPENAI_API_KEY=" in feedback["note"]
def test_fallback_compression_reports_dropped_message_count():
before = _messages(12)
after = before[:2] + before[-2:]
state = SimpleNamespace(
_last_compress_aborted=False,
_last_summary_fallback_used=True,
_last_summary_dropped_count=8,
_last_summary_error="summary provider returned an invalid response",
)
feedback = summarize_manual_compression(
before,
after,
120_000,
40_000,
compression_state=state,
)
assert feedback["aborted"] is False
assert feedback["fallback_used"] is True
assert feedback["headline"] == "Compressed with fallback: 12 → 4 messages"
assert "removed 8 message(s)" in feedback["note"]
assert "invalid response" in feedback["note"]
def test_lock_skip_with_confirmed_holder_names_it():
"""A descriptive holder string means another compressor CONFIRMED holds
the lock — say so and name the holder."""
text = describe_compression_lock_skip("pid=12345:tid=7:agent=1:nonce=ab")
assert "Compression already in progress" in text
assert "pid=12345:tid=7:agent=1:nonce=ab" in text
assert "wait for it to finish" in text
def test_lock_skip_without_confirmed_holder_does_not_claim_concurrency():
"""signal=True / None / '' / whitespace: acquisition failed but the
holder is unconfirmed (hermes_state.try_acquire_compression_lock catches
sqlite3.Error internally and returns False). The message must not assert
another compression is definitely running."""
for signal in (True, None, "", " "):
text = describe_compression_lock_skip(signal)
assert "already in progress" not in text, f"signal={signal!r}"
assert "Compression skipped" in text
assert "could not acquire" in text
assert "try again" in text