mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
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.
132 lines
4.6 KiB
Python
132 lines
4.6 KiB
Python
"""Regression tests for the rg/grep error guard in content search.
|
|
|
|
The guard in ``_search_with_rg`` / ``_search_with_grep`` had two defects on
|
|
``origin/main`` (see PR replacing #39710):
|
|
|
|
1. **Unreachable on a hard error.** Both methods pipe the search through
|
|
``| head`` with no ``pipefail``, so the pipeline reported head's exit code
|
|
(0), masking rg/grep's error code (2). The guard never fired, and the
|
|
error text — merged into stdout by ``_exec`` (``stderr=subprocess.STDOUT``)
|
|
— was parsed as bogus match lines instead of being surfaced.
|
|
|
|
2. **Would have nuked partial results if it ever did fire.** A broad
|
|
``exit_code == 2`` check discards real matches whenever rg/grep also hit a
|
|
non-fatal error (e.g. one unreadable file in a tree that otherwise
|
|
matched), which both tools signal with exit 2.
|
|
|
|
The fix adds ``set -o pipefail`` so the real exit code propagates, splits
|
|
tool diagnostics from match output by *shape*, and only surfaces an error
|
|
when exit==2 AND no usable match payload remains.
|
|
|
|
These tests drive the real methods through the real local terminal backend.
|
|
"""
|
|
|
|
import os
|
|
import shutil
|
|
|
|
import pytest
|
|
|
|
from tools.file_operations import (
|
|
ShellFileOperations,
|
|
_pattern_has_regex_newline,
|
|
_split_tool_diagnostics,
|
|
)
|
|
from tools.environments.local import LocalEnvironment
|
|
|
|
|
|
def _ops(root):
|
|
return ShellFileOperations(LocalEnvironment(cwd=str(root)), cwd=str(root))
|
|
|
|
|
|
@pytest.fixture
|
|
def match_tree(tmp_path):
|
|
"""A tree with several files all containing 'needle'."""
|
|
for i in range(5):
|
|
(tmp_path / f"f{i}.txt").write_text(f"needle line {i}\n")
|
|
return tmp_path
|
|
|
|
|
|
@pytest.fixture
|
|
def partial_error_tree(tmp_path):
|
|
"""A tree with matches plus one unreadable file (forces exit 2 + matches)."""
|
|
for i in range(4):
|
|
(tmp_path / f"f{i}.txt").write_text(f"needle line {i}\n")
|
|
sub = tmp_path / "sub"
|
|
sub.mkdir()
|
|
locked = sub / "locked.txt"
|
|
locked.write_text("needle in locked\n")
|
|
os.chmod(locked, 0o000)
|
|
yield tmp_path
|
|
os.chmod(locked, 0o755) # let pytest clean up tmp_path
|
|
|
|
|
|
# Run every test once per available backend method.
|
|
_METHODS = ["_search_with_grep"]
|
|
if shutil.which("rg"):
|
|
_METHODS.append("_search_with_rg")
|
|
|
|
|
|
def _search(ops, method, pattern, path, **kw):
|
|
fn = getattr(ops, method)
|
|
return fn(pattern, str(path), kw.get("file_glob"), kw.get("limit", 50),
|
|
kw.get("offset", 0), kw.get("output_mode", "content"),
|
|
kw.get("context", 0))
|
|
|
|
|
|
@pytest.mark.parametrize("method", _METHODS)
|
|
class TestSearchErrorGuard:
|
|
def test_happy_path_returns_matches(self, method, match_tree):
|
|
res = _search(_ops(match_tree), method, "needle", match_tree)
|
|
assert res.error is None
|
|
assert len(res.matches) == 5
|
|
|
|
def test_hard_error_is_surfaced(self, method, match_tree):
|
|
# An invalid regex makes rg/grep exit 2 with only diagnostics in
|
|
# stdout. The guard MUST surface it — not return empty matches.
|
|
res = _search(_ops(match_tree), method, "[", match_tree)
|
|
assert res.error is not None, "search error was silently swallowed"
|
|
assert "Search failed" in res.error
|
|
assert not res.matches
|
|
|
|
|
|
def test_count_mode_with_partial_error(self, method, partial_error_tree):
|
|
res = _search(_ops(partial_error_tree), method, "needle",
|
|
partial_error_tree, output_mode="count")
|
|
assert res.error is None
|
|
assert res.total_count >= 4
|
|
|
|
|
|
class TestSearchContentNewlineWarning:
|
|
def test_odd_backslash_n_is_detected_as_regex_newline(self):
|
|
assert _pattern_has_regex_newline(r"needle\n")
|
|
assert _pattern_has_regex_newline(r"needle\\\n")
|
|
|
|
|
|
def test_literal_backslash_n_pattern_does_not_warn(self, match_tree):
|
|
res = _ops(match_tree).search(
|
|
r"absent\\npattern",
|
|
path=str(match_tree),
|
|
target="content",
|
|
)
|
|
|
|
assert res.error is None
|
|
assert res.total_count == 0
|
|
assert res.warning is None
|
|
|
|
|
|
class TestSplitToolDiagnostics:
|
|
"""Unit coverage for the shape-based diagnostic/payload splitter."""
|
|
|
|
def test_pure_error_has_empty_payload(self):
|
|
out = "rg: regex parse error:\n (?:[)\n ^\nerror: unclosed character class\n"
|
|
diagnostics, payload = _split_tool_diagnostics(out)
|
|
assert payload.strip() == ""
|
|
assert "regex parse error" in diagnostics
|
|
|
|
|
|
def test_context_lines_and_separator_are_payload(self):
|
|
out = "a.py:5:hit\na.py-6-after\n--\nb.py:9:hit\n"
|
|
diagnostics, payload = _split_tool_diagnostics(out)
|
|
assert diagnostics == ""
|
|
assert "--" in payload
|
|
assert "a.py-6-after" in payload
|