b4fbd6fe9f
Deploy Site / deploy-vercel (push) Has been skipped
Deploy Site / deploy-docs (push) Has been skipped
Build Skills Index / build-index (push) Has been skipped
CI / Deny unrelated histories (push) Has been skipped
CI / Detect affected areas (push) Successful in 27m35s
CI / OSV scan (push) Failing after 4s
CI / Build&Test Docker image (push) Successful in 9s
CI / Supply-chain scan (push) Has been skipped
CI / Lint Docker scripts (push) Failing after 5m13s
CI / Check contributors (push) Failing after 12m8s
CI / Docs Site (push) Failing after 12m8s
CI / TypeScript (push) Failing after 12m8s
CI / Python lints (push) Failing after 12m9s
CI / Python tests (push) Failing after 12m9s
CI / Check uv.lock (push) Failing after 23m22s
CI / CI timing report (push) Has been cancelled
Build Skills Index / trigger-deploy (push) Has been cancelled
CI / All required checks pass (push) Has been cancelled
76 lines
2.9 KiB
Python
76 lines
2.9 KiB
Python
"""Tests that switch_model does not inherit stale context_length overrides."""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from run_agent import AIAgent
|
|
from agent.context_compressor import ContextCompressor
|
|
|
|
|
|
def _make_agent_with_compressor(config_context_length=None) -> AIAgent:
|
|
"""Build a minimal AIAgent with a context_compressor, skipping __init__."""
|
|
agent = AIAgent.__new__(AIAgent)
|
|
|
|
# Primary model settings
|
|
agent.model = "primary-model"
|
|
agent.provider = "openrouter"
|
|
agent.base_url = "https://openrouter.ai/api/v1"
|
|
agent.api_key = "sk-primary"
|
|
agent.api_mode = "chat_completions"
|
|
agent.client = MagicMock()
|
|
agent.quiet_mode = True
|
|
|
|
# Store the initial config_context_length override used at agent construction.
|
|
agent._config_context_length = config_context_length
|
|
|
|
# Context compressor with primary model values
|
|
compressor = ContextCompressor(
|
|
model="primary-model",
|
|
threshold_percent=0.50,
|
|
base_url="https://openrouter.ai/api/v1",
|
|
api_key="sk-primary",
|
|
provider="openrouter",
|
|
quiet_mode=True,
|
|
config_context_length=config_context_length,
|
|
)
|
|
agent.context_compressor = compressor
|
|
|
|
# For switch_model
|
|
agent._primary_runtime = {}
|
|
|
|
return agent
|
|
|
|
|
|
@patch("agent.model_metadata.get_model_context_length", return_value=131_072)
|
|
def test_switch_model_clears_previous_config_context_length(mock_ctx_len):
|
|
"""Switching models must not reuse the previous model.context_length override."""
|
|
agent = _make_agent_with_compressor(config_context_length=32_768)
|
|
|
|
assert agent.context_compressor.model == "primary-model"
|
|
assert agent.context_compressor.context_length == 32_768 # From config override
|
|
|
|
# Switch model
|
|
agent.switch_model("new-model", "openrouter", api_key="sk-new", base_url="https://openrouter.ai/api/v1")
|
|
|
|
# Verify the old config override is not passed to the new model.
|
|
mock_ctx_len.assert_called_once()
|
|
call_kwargs = mock_ctx_len.call_args.kwargs
|
|
assert call_kwargs.get("config_context_length") is None
|
|
|
|
# Verify compressor was updated from the newly resolved model metadata.
|
|
assert agent.context_compressor.model == "new-model"
|
|
assert agent.context_compressor.context_length == 131_072
|
|
|
|
|
|
def test_switch_model_without_config_context_length():
|
|
"""When switching models without config override, config_context_length should be None."""
|
|
agent = _make_agent_with_compressor(config_context_length=None)
|
|
|
|
with patch("agent.model_metadata.get_model_context_length", return_value=128_000) as mock_ctx_len:
|
|
# Switch model
|
|
agent.switch_model("new-model", "openrouter", api_key="sk-new", base_url="https://openrouter.ai/api/v1")
|
|
|
|
# Verify get_model_context_length was called with None
|
|
mock_ctx_len.assert_called_once()
|
|
call_kwargs = mock_ctx_len.call_args.kwargs
|
|
assert call_kwargs.get("config_context_length") is None
|