hermes-agent/tests/agent/test_compression_fallback_budget.py
Teknium 6b81590c55
test: prune low-value tests suite-wide (wave 1) — 46,820 → 28,106 test functions
Systematic prune per AGENTS.md test policy, one pass over every major
test tree (gateway, hermes_cli, tools, agent, run_agent, plugins, cli,
cron, tui_gateway, honcho/openviking, root-level):

- DELETE: source-reading tests (read_text/getsource on prod files),
  change-detector tests (exact catalog counts, model-name snapshots,
  config version literals), mock-echo tests (assert a mock returns what
  it was told), assertion-free/trivial tests, near-duplicate
  parametrizations (boundaries + one representative kept), async/sync
  twin duplicates, cosmetic within-file variations.
- KEEP (mandatory): security/redaction/approval guards, message-role
  alternation invariants, prompt-caching/deterministic-call-id
  invariants, issue-number regression tests (deduped), E2E tests.
- 6 test files deleted outright (script-style/no-assert or fully
  redundant); conftest.py, fakes/, fixtures/ untouched.
- tests/acp/conftest.py added: autouse fixture stubs the live
  models.dev/GitHub/Copilot/Anthropic inventory fetches that ACP server
  tests performed on every session create — test_server.py 147s → 3.4s,
  and the tests are now genuinely hermetic.
- Sleep-based slowness shrunk where safe (codex_ttfb_watchdog,
  compression_concurrent_fork, etc.); no wall-clock assertion tightened.

Verification: full hermetic suite via scripts/run_tests.sh —
2439 files, 31,130 tests passed, 0 failed, 0 flaky retries, 315s wall
(baseline: 583s wall, 13,564s subprocess CPU).
2026-07-29 13:10:23 -07:00

132 lines
4.6 KiB
Python

"""Tests for #62452 — compression fallback timeout independence + escalating
timeout cooldown.
Two failure amplifiers when the auxiliary compression route times out:
1. Fallback candidates inherited the primary's exact ``effective_timeout``.
A fallback tuned differently (or simply slower-but-healthy) died on the
same clock the primary just burned, guaranteeing chain exhaustion.
Fix: ``auxiliary.<task>.fallback_chain`` entries may declare their own
``timeout``; ``_call_fallback_candidate_sync/async`` resolve it via
``_fallback_entry_timeout``.
2. A session whose transcript structurally cannot be summarised within the
deadline re-attempted every 60s, re-burning the full timeout on every
subsequent turn. Fix: consecutive timeout-class failures escalate the
cooldown 60s → 300s → 900s; any successful summary resets the streak.
"""
from types import SimpleNamespace
from unittest.mock import patch
import pytest
from agent.auxiliary_client import _fallback_entry_timeout, _call_fallback_candidate_sync
from agent.context_compressor import ContextCompressor
# ---------------------------------------------------------------------------
# _fallback_entry_timeout — label parsing + config resolution
# ---------------------------------------------------------------------------
def _patch_task_config(chain):
return patch(
"agent.auxiliary_client._get_auxiliary_task_config",
return_value={"fallback_chain": chain},
)
def test_fallback_candidate_call_uses_entry_timeout():
"""The wire call for a configured-chain candidate carries the entry's own
timeout, not the task-level one the primary just burned."""
seen = {}
class _FakeCompletions:
def create(self, **kwargs):
seen.update(kwargs)
return SimpleNamespace(
choices=[SimpleNamespace(message=SimpleNamespace(content="ok"))]
)
fb_client = SimpleNamespace(
base_url="https://example.invalid/v1",
chat=SimpleNamespace(completions=_FakeCompletions()),
)
chain = [{"provider": "custom", "timeout": 240}]
with _patch_task_config(chain):
resp = _call_fallback_candidate_sync(
fb_client, "deepseek-v4-flash", "fallback_chain[0](custom)",
task="compression", messages=[{"role": "user", "content": "hi"}],
temperature=None, max_tokens=None, tools=None,
effective_timeout=30.0, # the primary's burned budget
effective_extra_body={}, reasoning_config=None,
)
assert resp is not None
assert seen.get("timeout") == 240.0
# ---------------------------------------------------------------------------
# Escalating timeout cooldown — 60s → 300s → 900s, reset on success
# ---------------------------------------------------------------------------
def _make_compressor():
with patch("agent.context_compressor.get_model_context_length", return_value=100000):
return ContextCompressor(model="main-model", quiet_mode=True)
def _msgs():
return [
{"role": "user", "content": "u1 " + "x" * 200},
{"role": "assistant", "content": "a1 " + "y" * 200},
{"role": "user", "content": "u2 " + "z" * 200},
]
def _fail_with_timeout(compressor, now):
with patch(
"agent.context_compressor.call_llm",
side_effect=TimeoutError("Request timed out."),
), patch("agent.context_compressor.time.monotonic", return_value=now):
return compressor._generate_summary(_msgs())
def test_timeout_streak_resets_on_success():
c = _make_compressor()
assert _fail_with_timeout(c, 1000.0) is None
assert _fail_with_timeout(c, 2000.0) is None
assert c._consecutive_timeout_failures == 2
# A successful summary clears the cooldown AND the streak.
c._clear_compression_failure_cooldown()
assert c._consecutive_timeout_failures == 0
# The next timeout starts back at the 60s rung.
assert _fail_with_timeout(c, 5000.0) is None
assert c._summary_failure_cooldown_until == 5000.0 + 60
def test_non_timeout_transient_errors_keep_flat_cooldown():
"""Rate-limit / generic connection errors keep the flat 60s cooldown —
escalation is scoped to timeout-class failures only."""
c = _make_compressor()
with patch(
"agent.context_compressor.call_llm",
side_effect=RuntimeError("rate limit exceeded"),
), patch("agent.context_compressor.time.monotonic", return_value=1000.0):
assert c._generate_summary(_msgs()) is None
assert c._summary_failure_cooldown_until == 1000.0 + 60
# And it does not advance the timeout streak.
assert getattr(c, "_consecutive_timeout_failures", 0) == 0