hermes-agent/tests/test_get_tool_definitions_cache_isolation.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

82 lines
3.4 KiB
Python

"""Regression tests for issue #17335.
The ``quiet_mode=True`` fast path in :func:`model_tools.get_tool_definitions`
memoizes results to avoid re-walking the registry on every Gateway call. The
cached object must NOT be aliased into callers' return values \u2014 long-lived
Gateway processes mutate the returned list (``run_agent`` appends memory and
LCM context-engine tool schemas to ``self.tools``), and a shared list would
poison subsequent agent inits with duplicate tool names. Providers that
enforce uniqueness (DeepSeek, Xiaomi MiMo, Moonshot/Kimi) then reject the
API call with HTTP 400.
These tests pin:
- the cache-hit path returns a fresh list (existing #17098 behavior)
- the first uncached call also returns a fresh list (the fix)
- every call returns a list that is not the cached one, even after mutation
"""
from __future__ import annotations
import pytest
import model_tools
@pytest.fixture(autouse=True)
def _clear_cache():
"""Each test starts with an empty quiet_mode cache."""
model_tools._tool_defs_cache.clear()
yield
model_tools._tool_defs_cache.clear()
class TestQuietModeCacheIsolation:
def test_first_uncached_call_returns_fresh_list(self):
"""The first quiet_mode call must not alias the cached object \u2014
otherwise a caller mutating the returned list mutates the cache."""
first = model_tools.get_tool_definitions(quiet_mode=True)
assert isinstance(first, list)
# Find the cached value to compare identity.
assert len(model_tools._tool_defs_cache) == 1
cached = next(iter(model_tools._tool_defs_cache.values()))
assert first is not cached, (
"issue #17335: first quiet_mode call returned the cached list "
"by reference \u2014 mutations will leak into subsequent calls."
)
def test_cache_hit_returns_fresh_list(self):
"""The cache-hit path already returned a copy pre-fix; pin it."""
first = model_tools.get_tool_definitions(quiet_mode=True)
second = model_tools.get_tool_definitions(quiet_mode=True)
assert first is not second
cached = next(iter(model_tools._tool_defs_cache.values()))
assert second is not cached
def test_cache_bounded_by_eviction(self):
"""The cache evicts the oldest entry when it reaches the cap,
keeping the cache bounded instead of growing unbounded over a
long-lived Gateway's lifetime (#19251)."""
cap = model_tools._TOOL_DEFS_CACHE_MAX
# Fill cache to the cap with distinct keys by varying enabled_toolsets.
for i in range(cap):
model_tools.get_tool_definitions(
enabled_toolsets=[f"fake_toolset_{i}"], quiet_mode=True,
)
assert len(model_tools._tool_defs_cache) == cap
# Adding one more must evict the oldest, not clear everything and
# not grow past the cap.
model_tools.get_tool_definitions(
enabled_toolsets=["fake_toolset_overflow"], quiet_mode=True,
)
assert len(model_tools._tool_defs_cache) == cap, (
"Eviction should keep the cache at the cap, not clear it or grow"
)
def test_non_quiet_mode_does_not_use_cache(self):
"""Sanity: quiet_mode=False (TUI path) skips the cache entirely \u2014
explains why the bug only hit Gateway."""
model_tools.get_tool_definitions(quiet_mode=False)
assert len(model_tools._tool_defs_cache) == 0