mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-30 19:09:28 +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.
141 lines
5.1 KiB
Python
141 lines
5.1 KiB
Python
"""Tests for tools.tool_output_limits.
|
|
|
|
Covers:
|
|
1. Default values when no config is provided.
|
|
2. Config override picks up user-supplied max_bytes / max_lines /
|
|
max_line_length.
|
|
3. Malformed values (None, negative, wrong type) fall back to defaults
|
|
rather than raising.
|
|
4. Integration: the helpers return what the terminal_tool and
|
|
file_operations call paths will actually consume.
|
|
|
|
Port-tracking: anomalyco/opencode PR #23770
|
|
(feat(truncate): allow configuring tool output truncation limits).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from tools import tool_output_limits as tol
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _reset_limits_cache():
|
|
"""get_tool_output_limits() now memoizes its result for the process
|
|
lifetime, so each test must start from a clean cache to observe the
|
|
config value it patches in."""
|
|
tol._reset_tool_output_limits_cache()
|
|
yield
|
|
tol._reset_tool_output_limits_cache()
|
|
|
|
|
|
class TestDefaults:
|
|
def test_defaults_match_previous_hardcoded_values(self):
|
|
assert tol.DEFAULT_MAX_BYTES == 50_000
|
|
assert tol.DEFAULT_MAX_LINES == 2000
|
|
assert tol.DEFAULT_MAX_LINE_LENGTH == 2000
|
|
|
|
|
|
def test_get_limits_returns_defaults_when_load_config_raises(self):
|
|
def _boom():
|
|
raise RuntimeError("boom")
|
|
|
|
with patch("hermes_cli.config.load_config", side_effect=_boom):
|
|
limits = tol.get_tool_output_limits()
|
|
assert limits["max_lines"] == tol.DEFAULT_MAX_LINES
|
|
|
|
|
|
class TestOverrides:
|
|
def test_user_config_overrides_all_three(self):
|
|
cfg = {
|
|
"tool_output": {
|
|
"max_bytes": 100_000,
|
|
"max_lines": 5000,
|
|
"max_line_length": 4096,
|
|
}
|
|
}
|
|
with patch("hermes_cli.config.load_config", return_value=cfg):
|
|
limits = tol.get_tool_output_limits()
|
|
assert limits == {
|
|
"max_bytes": 100_000,
|
|
"max_lines": 5000,
|
|
"max_line_length": 4096,
|
|
}
|
|
|
|
|
|
def test_section_not_a_dict_falls_back(self):
|
|
cfg = {"tool_output": "nonsense"}
|
|
with patch("hermes_cli.config.load_config", return_value=cfg):
|
|
limits = tol.get_tool_output_limits()
|
|
assert limits["max_bytes"] == tol.DEFAULT_MAX_BYTES
|
|
|
|
|
|
class TestCoercion:
|
|
@pytest.mark.parametrize("bad", [None, "not a number", -1, 0, [], {}])
|
|
def test_invalid_values_fall_back_to_defaults(self, bad):
|
|
cfg = {"tool_output": {"max_bytes": bad, "max_lines": bad, "max_line_length": bad}}
|
|
with patch("hermes_cli.config.load_config", return_value=cfg):
|
|
limits = tol.get_tool_output_limits()
|
|
assert limits["max_bytes"] == tol.DEFAULT_MAX_BYTES
|
|
assert limits["max_lines"] == tol.DEFAULT_MAX_LINES
|
|
assert limits["max_line_length"] == tol.DEFAULT_MAX_LINE_LENGTH
|
|
|
|
def test_string_integer_is_coerced(self):
|
|
cfg = {"tool_output": {"max_bytes": "75000"}}
|
|
with patch("hermes_cli.config.load_config", return_value=cfg):
|
|
limits = tol.get_tool_output_limits()
|
|
assert limits["max_bytes"] == 75_000
|
|
|
|
|
|
class TestShortcuts:
|
|
def test_individual_accessors_delegate_to_get_tool_output_limits(self):
|
|
cfg = {
|
|
"tool_output": {
|
|
"max_bytes": 111,
|
|
"max_lines": 222,
|
|
"max_line_length": 333,
|
|
}
|
|
}
|
|
with patch("hermes_cli.config.load_config", return_value=cfg):
|
|
assert tol.get_max_bytes() == 111
|
|
assert tol.get_max_lines() == 222
|
|
assert tol.get_max_line_length() == 333
|
|
|
|
|
|
class TestDefaultConfigHasSection:
|
|
"""The DEFAULT_CONFIG in hermes_cli.config must expose tool_output so
|
|
that ``hermes setup`` and default installs stay in sync with the
|
|
helpers here."""
|
|
|
|
def test_default_config_contains_tool_output_section(self):
|
|
from hermes_cli.config import DEFAULT_CONFIG
|
|
assert "tool_output" in DEFAULT_CONFIG
|
|
section = DEFAULT_CONFIG["tool_output"]
|
|
assert isinstance(section, dict)
|
|
assert section["max_bytes"] == tol.DEFAULT_MAX_BYTES
|
|
assert section["max_lines"] == tol.DEFAULT_MAX_LINES
|
|
assert section["max_line_length"] == tol.DEFAULT_MAX_LINE_LENGTH
|
|
|
|
|
|
class TestIntegrationReadPagination:
|
|
"""normalize_read_pagination uses get_max_lines() — verify the plumbing."""
|
|
|
|
def test_pagination_limit_clamped_by_config_value(self):
|
|
from tools.file_operations import normalize_read_pagination
|
|
cfg = {"tool_output": {"max_lines": 50}}
|
|
with patch("hermes_cli.config.load_config", return_value=cfg):
|
|
offset, limit = normalize_read_pagination(offset=1, limit=1000)
|
|
# limit should have been clamped to 50 (the configured max_lines)
|
|
assert limit == 50
|
|
assert offset == 1
|
|
|
|
def test_pagination_default_when_config_missing(self):
|
|
from tools.file_operations import normalize_read_pagination
|
|
with patch("hermes_cli.config.load_config", return_value={}):
|
|
offset, limit = normalize_read_pagination(offset=10, limit=100000)
|
|
# Clamped to default MAX_LINES (2000).
|
|
assert limit == tol.DEFAULT_MAX_LINES
|
|
assert offset == 10
|