hermes-agent/tests/hermes_cli/test_prompt_size.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

121 lines
4.2 KiB
Python

"""Tests for the ``hermes prompt-size`` diagnostic (issue #34667)."""
import json
import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
from hermes_cli.prompt_size import (
_SKILLS_BLOCK_RE,
_build_inspection_agent,
_compute_skills_breakdown,
compute_prompt_breakdown,
render_breakdown,
)
def _seed_memory(hermes_home, memory_text="", user_text=""):
mem_dir = hermes_home / "memories"
mem_dir.mkdir(parents=True, exist_ok=True)
if memory_text:
(mem_dir / "MEMORY.md").write_text(memory_text, encoding="utf-8")
if user_text:
(mem_dir / "USER.md").write_text(user_text, encoding="utf-8")
def _seed_skill(hermes_home, name, description):
skill_dir = hermes_home / "skills" / "demo" / name
skill_dir.mkdir(parents=True, exist_ok=True)
(skill_dir / "SKILL.md").write_text(
f"---\nname: {name}\ndescription: {description}\n---\n# {name}\nbody\n",
encoding="utf-8",
)
@pytest.fixture
def isolated_home(tmp_path, monkeypatch):
hermes_home = tmp_path / ".hermes"
hermes_home.mkdir()
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
monkeypatch.chdir(tmp_path) # avoid picking up the repo's AGENTS.md
return hermes_home
def test_runs_offline_without_credentials(isolated_home, monkeypatch):
"""No provider credentials configured → still produces a breakdown."""
for var in ("OPENROUTER_API_KEY", "OPENAI_API_KEY", "NOUS_API_KEY",
"ANTHROPIC_API_KEY"):
monkeypatch.delenv(var, raising=False)
data = compute_prompt_breakdown("cli")
assert data["system_prompt"]["bytes"] > 0
def test_skills_breakdown_shape_sorted_and_attributed(isolated_home):
"""Per-skill breakdown reports index-line + on-disk SKILL.md bytes.
Seeded before the first build (skills prompt is cached per-process).
"""
_seed_skill(isolated_home, "small-skill", "short desc")
_seed_skill(isolated_home, "big-skill", "a much longer description " * 20)
data = compute_prompt_breakdown("cli")
skills = data["skills_breakdown"]
names = {s["name"] for s in skills}
assert {"small-skill", "big-skill"} <= names
for s in skills:
assert set(s) >= {"name", "index_line_bytes", "skill_md_bytes", "path"}
assert s["index_line_bytes"] > 0
# Sorted largest-first by on-disk SKILL.md size.
md_sizes = [s["skill_md_bytes"] or 0 for s in skills]
assert md_sizes == sorted(md_sizes, reverse=True)
# On-disk bytes match the real file; big-skill's SKILL.md is the larger.
by_name = {s["name"]: s for s in skills}
big = by_name["big-skill"]
assert big["path"] and Path(big["path"]).stat().st_size == big["skill_md_bytes"]
assert big["skill_md_bytes"] > by_name["small-skill"]["skill_md_bytes"]
# Per-skill index lines are a subset of the whole <available_skills> block,
# so they never exceed it (on-disk SKILL.md bytes are separate and don't).
assert sum(s["index_line_bytes"] for s in skills) <= data["skills_index"]["bytes"]
def test_skills_breakdown_attributes_demoted_category_shared_line(isolated_home):
"""A real posture-demoted category retains every skill in the breakdown."""
from agent.prompt_builder import build_skills_system_prompt
_seed_skill(isolated_home, "alpha-skill", "alpha description")
_seed_skill(isolated_home, "beta-skill", "beta description")
prompt = build_skills_system_prompt(compact_categories=frozenset({"demo"}))
skills_match = _SKILLS_BLOCK_RE.search(prompt)
assert skills_match is not None
skills_block = skills_match.group(0)
shared_line = next(
line for line in skills_block.splitlines() if "demo [names only]" in line
)
entries = _compute_skills_breakdown(skills_block)
by_name = {entry["name"]: entry for entry in entries}
assert set(by_name) == {"alpha-skill", "beta-skill"}
shared_line_bytes = len(shared_line.encode("utf-8"))
assert sum(entry["index_line_bytes"] for entry in entries) == shared_line_bytes
for entry in entries:
assert entry["index_line_total_bytes"] == shared_line_bytes
assert entry["index_line_shared_bytes"] > 0
assert entry["index_line_skill_count"] == 2