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

159 lines
5.1 KiB
Python

"""Regression tests for the "ghost diagnostics" staleness bug.
Scenario: the agent edits a TypeScript file, tsserver takes a long
time to re-check it, and the old diagnostics (for the PRE-edit
content) were reported as if they were current — the agent then
chases errors it already fixed.
The contract under test:
- ``wait_for_diagnostics`` must NOT be satisfied by diagnostics left
over from a previous edit cycle; it returns True only when fresh
(post-didChange) data arrived, False on timeout.
- ``diagnostics_for(fresh_only=True)`` must exclude stale stores.
- ``LSPService.get_diagnostics_sync`` must return [] ("no data")
rather than the stale diagnostics when the server never re-checks
within the wait budget, and must NOT mark the server broken.
- A slow-but-eventually-correct server ("slow_push") is waited on,
honouring the configured ``lsp.wait_timeout``.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
import pytest
from agent.lsp.client import LSPClient
MOCK_SERVER = str(Path(__file__).parent / "_mock_lsp_server.py")
def _client(workspace: Path, script: str, **env_extra: str) -> LSPClient:
env = {
"MOCK_LSP_SCRIPT": script,
"PYTHONPATH": os.environ.get("PYTHONPATH", ""),
**env_extra,
}
return LSPClient(
server_id=f"mock-{script}",
workspace_root=str(workspace),
command=[sys.executable, MOCK_SERVER],
env=env,
cwd=str(workspace),
)
@pytest.mark.asyncio
async def test_slow_push_is_waited_for(tmp_path: Path):
"""A server that re-checks slowly (but within budget) gets waited on,
and the fresh (clean) result replaces the old error."""
f = tmp_path / "x.py"
f.write_text("bad code\n")
client = _client(tmp_path, "slow_push", MOCK_LSP_PUSH_DELAY="0.8")
await client.start()
try:
v0 = await client.open_file(str(f), language_id="python")
assert await client.wait_for_diagnostics(str(f), v0, mode="document", timeout=2.0)
assert len(client.diagnostics_for(str(f), fresh_only=True)) == 1
f.write_text("good code\n")
v1 = await client.open_file(str(f), language_id="python")
fresh = await client.wait_for_diagnostics(str(f), v1, mode="document", timeout=5.0)
assert fresh is True, "slow push within budget must satisfy the wait"
assert client.diagnostics_for(str(f), fresh_only=True) == []
finally:
await client.shutdown()
# ---------------------------------------------------------------------------
# Service-level: stale data must surface as "no data", never as errors
# ---------------------------------------------------------------------------
def _install_mock_server(script: str, server_id: str = "pyright"):
"""Replace one registered server with a wrapper spawning the mock.
Mirrors the helper in test_service.py — reuse pyright so .py files
route to the mock without a real toolchain.
"""
from agent.lsp.servers import SERVERS, ServerContext, ServerDef, SpawnSpec
target_index = next(i for i, s in enumerate(SERVERS) if s.server_id == server_id)
original = SERVERS[target_index]
def _spawn(root: str, ctx: ServerContext) -> SpawnSpec:
return SpawnSpec(
command=[sys.executable, MOCK_SERVER],
workspace_root=root,
cwd=root,
env={"MOCK_LSP_SCRIPT": script},
initialization_options={},
)
SERVERS[target_index] = ServerDef(
server_id=server_id,
extensions=original.extensions,
resolve_root=lambda fp, ws: ws,
build_spawn=_spawn,
seed_first_push=False,
description="mock " + server_id,
)
return target_index, original
@pytest.fixture
def stale_repo(monkeypatch, tmp_path):
repo = tmp_path / "repo"
repo.mkdir()
(repo / ".git").mkdir()
(repo / "pyproject.toml").write_text("")
monkeypatch.chdir(str(repo))
idx, original = _install_mock_server("stale")
yield repo
from agent.lsp.servers import SERVERS
SERVERS[idx] = original
def test_service_reports_no_data_not_stale_errors(stale_repo):
"""When the server never re-checks the edited content in budget,
get_diagnostics_sync must return [] and keep the server usable."""
from agent.lsp.manager import LSPService
f = stale_repo / "x.py"
f.write_text("bad code\n")
svc = LSPService(
enabled=True,
wait_mode="document",
wait_timeout=1.0,
install_strategy="manual",
)
try:
# First contact: didOpen gets the (real) pre-edit error push.
first = svc.get_diagnostics_sync(str(f), delta=False)
assert len(first) == 1
# Edit the file — mock never re-publishes (slow tsserver model).
f.write_text("good code\n")
ghost = svc.get_diagnostics_sync(str(f), delta=False)
assert ghost == [], "stale pre-edit error must not be reported as current"
# Not marked broken: slow is not dead.
assert svc.enabled_for(str(f))
status = svc.get_status()
assert status["broken"] == []
finally:
svc.shutdown()