/ tests / run_agent / test_plugin_context_engine_init.py
test_plugin_context_engine_init.py
 1  """Tests that plugin context engines get update_model() called during init.
 2  
 3  Regression test for #9071 — plugin engines were never initialized with
 4  context_length, causing the CLI status bar to show 'ctx --'.
 5  """
 6  
 7  from unittest.mock import MagicMock, patch
 8  
 9  from agent.context_engine import ContextEngine
10  
11  
12  class _StubEngine(ContextEngine):
13      """Minimal concrete context engine for testing."""
14  
15      @property
16      def name(self) -> str:
17          return "stub"
18  
19      def update_from_response(self, usage):
20          pass
21  
22      def should_compress(self, prompt_tokens=None):
23          return False
24  
25      def compress(self, messages, current_tokens=None):
26          return messages
27  
28  
29  def test_plugin_engine_gets_context_length_on_init():
30      """Plugin context engine should have context_length set during AIAgent init."""
31      engine = _StubEngine()
32      assert engine.context_length == 0  # ABC default before fix
33  
34      cfg = {"context": {"engine": "stub"}, "agent": {}}
35  
36      with (
37          patch("hermes_cli.config.load_config", return_value=cfg),
38          patch("plugins.context_engine.load_context_engine", return_value=engine),
39          patch("agent.model_metadata.get_model_context_length", return_value=204_800),
40          patch("run_agent.get_tool_definitions", return_value=[]),
41          patch("run_agent.check_toolset_requirements", return_value={}),
42          patch("run_agent.OpenAI"),
43      ):
44          from run_agent import AIAgent
45  
46          agent = AIAgent(
47              api_key="test-key-1234567890",
48              base_url="https://openrouter.ai/api/v1",
49              quiet_mode=True,
50              skip_context_files=True,
51              skip_memory=True,
52          )
53  
54      assert agent.context_compressor is engine
55      assert engine.context_length == 204_800
56      assert engine.threshold_tokens == int(204_800 * engine.threshold_percent)
57  
58  
59  def test_plugin_engine_update_model_args():
60      """Verify update_model() receives model, context_length, base_url, api_key, provider."""
61      engine = _StubEngine()
62      engine.update_model = MagicMock()
63  
64      cfg = {"context": {"engine": "stub"}, "agent": {}}
65  
66      with (
67          patch("hermes_cli.config.load_config", return_value=cfg),
68          patch("plugins.context_engine.load_context_engine", return_value=engine),
69          patch("agent.model_metadata.get_model_context_length", return_value=131_072),
70          patch("run_agent.get_tool_definitions", return_value=[]),
71          patch("run_agent.check_toolset_requirements", return_value={}),
72          patch("run_agent.OpenAI"),
73      ):
74          from run_agent import AIAgent
75  
76          agent = AIAgent(
77              model="openrouter/auto",
78              api_key="test-key-1234567890",
79              base_url="https://openrouter.ai/api/v1",
80              quiet_mode=True,
81              skip_context_files=True,
82              skip_memory=True,
83          )
84  
85      engine.update_model.assert_called_once()
86      kw = engine.update_model.call_args.kwargs
87      assert kw["context_length"] == 131_072
88      assert "model" in kw
89      assert "provider" in kw
90      # Should NOT pass api_mode — the ABC doesn't accept it
91      assert "api_mode" not in kw