Files
hermes-agent/tests/run_agent/test_plugin_context_engine_init.py
Teknium 2a95791992 simplify(compat): run_agent/model_tools/toolsets/acp/providers — drop 42 re-exports/aliases, repoint 15 callers + 99 test files
run_agent.py: delete the `# noqa: F401` re-export block (agent.process_bootstrap
OpenAI/_SafeWriter/_get_proxy_*, model_tools get_tool_definitions/
handle_function_call/check_toolset_requirements, FailoverReason,
_qwen_portal_headers/_routermint_headers, session_persistence names,
estimate_request_tokens_rough, ContextCompressor + friends, jittered_backoff,
prompt_builder names, message_sanitization names, tool_dispatch_helpers
names) — 41 names run_agent never used itself — and the `_STREAM_DIAG_HEADERS`
back-compat class alias (no in-tree reader). run_agent now imports only what
it uses (get_toolset_for_tool, is_local_endpoint, coalesce/uniquify tool-call
ids, cleanup_vm/get_active_env from terminal_tool_lifecycle).

agent/*: `_ra().X` late-binds that only reached a re-export now import the
defining module directly (agent_runtime_helpers -> process_bootstrap.OpenAI,
model_tools.handle_function_call, session_persistence._safe_session_filename_component;
agent_init -> model_tools.get_tool_definitions/check_toolset_requirements,
_lazy_headers("agent.client_lifecycle", ...) for qwen/routermint;
system_prompt -> agent.prompt_builder / model_tools directly, dropping its
own _ra() shim and the `_r` parameter threading). `_ra()` stays for
run_agent-resident names (logger, AIAgent, _hermes_home, _set_interrupt, ...).

toolsets.py: remove resolve_multiple_toolsets (shim-only, restored by
34abf954bd); tests/test_toolsets.py pins the same union behavior via
resolve_toolset over each name.

providers/__init__.py: drop the OMIT_TEMPERATURE re-export (no callers via the
package); ProviderProfile stays because __init__ uses it for annotations —
2 tests repointed to providers.base.

agent/iteration_budget.py: drop the "run_agent re-exports the class"
docstring pointer; 4 tests import IterationBudget from its home.

model_tools.py (arg_coercion names), agent/tool_executor.py, and
hermes_cli/cli_session_mixin.py repoints landed via a sibling commit on this
shared worktree.

Callers repointed: gateway/run.py, hermes_cli/cli_chat_turn_mixin.py,
hermes_cli/cli_tui_mixin.py, tui_gateway/session_workdir.py,
agent/transports/codex.py (one-line imports) + comment pointers in
tools/file_state.py, tools/schema_sanitizer.py, scripts/tool_search_livetest.py.
Tests: patch("run_agent.X") / monkeypatch.setattr(run_agent, "X") /
`from run_agent import X` -> defining module across 99 test files.
2026-09-03 13:28:22 -07:00

213 lines
7.6 KiB
Python

"""Tests that plugin context engines get update_model() called during init.
Regression test for #9071 — plugin engines were never initialized with
context_length, causing the CLI status bar to show 'ctx --'.
"""
from unittest.mock import MagicMock, patch
from agent.context_engine import ContextEngine
class _StubEngine(ContextEngine):
"""Minimal concrete context engine for testing."""
@property
def name(self) -> str:
return "stub"
def update_from_response(self, usage):
pass
def should_compress(self, prompt_tokens=None):
return False
def compress(self, messages, current_tokens=None):
return messages
class _ToolEngine(_StubEngine):
def get_tool_schemas(self):
return [
{
"name": "stub_recover",
"description": "Recover context from the stub engine.",
"parameters": {"type": "object", "properties": {}},
}
]
def test_plugin_engine_gets_context_length_on_init():
"""Plugin context engine should have context_length set during AIAgent init."""
engine = _StubEngine()
assert engine.context_length == 0 # ABC default before fix
cfg = {"context": {"engine": "stub"}, "agent": {}}
with (
patch("hermes_cli.config.load_config", return_value=cfg), patch("hermes_cli.config.load_config_readonly", return_value=cfg),
patch("plugins.context_engine.load_context_engine", return_value=engine),
patch("agent.model_metadata.get_model_context_length", return_value=204_800),
patch("model_tools.get_tool_definitions", return_value=[]),
patch("model_tools.check_toolset_requirements", return_value={}),
patch("agent.process_bootstrap.OpenAI"),
):
from run_agent import AIAgent
agent = AIAgent(
api_key="test-key-1234567890",
base_url="https://openrouter.ai/api/v1",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert agent.context_compressor is engine
assert engine.context_length == 204_800
assert engine.threshold_tokens == int(204_800 * engine.threshold_percent)
def test_active_context_engine_tools_survive_explicit_platform_toolsets():
"""LCM-style recovery tools must survive saved `hermes tools` lists."""
engine = _ToolEngine()
cfg = {
"context": {"engine": "stub"},
"platform_toolsets": {"cli": ["web", "terminal"]},
"agent": {},
}
from hermes_cli.tools_config import _get_platform_tools
enabled_toolsets = _get_platform_tools(cfg, "cli", include_default_mcp_servers=False)
assert "context_engine" in enabled_toolsets
with (
patch("hermes_cli.config.load_config", return_value=cfg), patch("hermes_cli.config.load_config_readonly", return_value=cfg),
patch("plugins.context_engine.load_context_engine", return_value=engine),
patch("agent.model_metadata.get_model_context_length", return_value=204_800),
patch("model_tools.get_tool_definitions", return_value=[]),
patch("model_tools.check_toolset_requirements", return_value={}),
patch("agent.process_bootstrap.OpenAI"),
):
from run_agent import AIAgent
agent = AIAgent(
api_key="test-key-1234567890",
base_url="https://openrouter.ai/api/v1",
enabled_toolsets=sorted(enabled_toolsets),
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
assert "stub_recover" in getattr(agent, "valid_tool_names", set())
assert "stub_recover" in {
tool.get("function", {}).get("name")
for tool in getattr(agent, "tools", [])
}
def test_plugin_engine_update_model_args():
"""Verify update_model() receives model, context_length, base_url, api_key, provider."""
engine = _StubEngine()
engine.update_model = MagicMock()
cfg = {"context": {"engine": "stub"}, "agent": {}}
with (
patch("hermes_cli.config.load_config", return_value=cfg), patch("hermes_cli.config.load_config_readonly", return_value=cfg),
patch("plugins.context_engine.load_context_engine", return_value=engine),
patch("agent.model_metadata.get_model_context_length", return_value=131_072),
patch("model_tools.get_tool_definitions", return_value=[]),
patch("model_tools.check_toolset_requirements", return_value={}),
patch("agent.process_bootstrap.OpenAI"),
):
from run_agent import AIAgent
agent = AIAgent(
model="openrouter/auto",
api_key="test-key-1234567890",
base_url="https://openrouter.ai/api/v1",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
engine.update_model.assert_called_once()
kw = engine.update_model.call_args.kwargs
assert kw["context_length"] == 131_072
assert "model" in kw
assert "provider" in kw
assert "api_mode" in kw
def _codex_agent_kwargs():
return dict(
model="gpt-5.5",
provider="openai-codex",
api_key="test-key-1234567890",
base_url="https://chatgpt.com/backend-api/codex",
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
def test_codex_gpt55_autoraise_suppressed_for_plugin_engine():
"""Codex gpt-5.5 autoraise must not fire when an external engine is active.
Regression test for #44439 — the host compression threshold (including
the 0.85 autoraise) never reaches a plugin context engine, so the notice
announced a change that did not apply.
"""
engine = _StubEngine()
cfg = {
"context": {"engine": "stub"},
"compression": {"enabled": True, "threshold": 0.75},
"agent": {},
}
with (
patch("hermes_cli.config.load_config", return_value=cfg), patch("hermes_cli.config.load_config_readonly", return_value=cfg),
patch("plugins.context_engine.load_context_engine", return_value=engine),
patch("agent.model_metadata.get_model_context_length", return_value=272_000),
patch("model_tools.get_tool_definitions", return_value=[]),
patch("model_tools.check_toolset_requirements", return_value={}),
patch("agent.process_bootstrap.OpenAI"),
):
from run_agent import AIAgent
agent = AIAgent(**_codex_agent_kwargs())
assert agent.context_compressor is engine
assert agent._compression_threshold_autoraised is None
assert agent._compression_warning is None
# The engine's own policy is untouched by the host threshold.
assert engine.threshold_percent == 0.75
def test_codex_gpt55_autoraise_still_applies_to_builtin_compressor():
"""Stock built-in compressor keeps the 50% → 85% Codex gpt-5.5 autoraise."""
cfg = {
"compression": {"enabled": True, "threshold": 0.50},
"agent": {},
}
with (
patch("hermes_cli.config.load_config", return_value=cfg), patch("hermes_cli.config.load_config_readonly", return_value=cfg),
patch("agent.context_compressor.get_model_context_length", return_value=272_000),
patch("model_tools.get_tool_definitions", return_value=[]),
patch("model_tools.check_toolset_requirements", return_value={}),
patch("agent.process_bootstrap.OpenAI"),
):
from run_agent import AIAgent
agent = AIAgent(**_codex_agent_kwargs())
assert agent._compression_threshold_autoraised == {"model": "gpt-5.5", "from": 0.50, "to": 0.85}
assert agent.context_compressor.threshold_percent == 0.85
# Gateway parity: the notice is stashed for replay on turn 1.
assert agent._compression_warning and "85%" in agent._compression_warning