2a95791992
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.
134 lines
5.5 KiB
Python
134 lines
5.5 KiB
Python
"""Regression test for #17929: AIAgent.__init__ should try fallback_model
|
|
when primary provider credentials are exhausted."""
|
|
import pytest
|
|
from unittest.mock import patch, MagicMock
|
|
from run_agent import AIAgent
|
|
|
|
|
|
def _make_tool_defs():
|
|
return [{"type": "function", "function": {"name": "web_search",
|
|
"description": "search", "parameters": {"type": "object", "properties": {}}}}]
|
|
|
|
|
|
def _mock_client(api_key="fb-key-1234567890", base_url="https://fb.example.com/v1"):
|
|
c = MagicMock()
|
|
c.api_key = api_key
|
|
c.base_url = base_url
|
|
c._default_headers = None
|
|
return c
|
|
|
|
|
|
def test_init_tries_fallback_when_primary_returns_none():
|
|
"""When resolve_provider_client returns None for primary but succeeds for
|
|
a fallback entry, __init__ should NOT raise RuntimeError."""
|
|
fb = _mock_client()
|
|
|
|
def fake_resolve(provider, model=None, raw_codex=False,
|
|
explicit_base_url=None, explicit_api_key=None):
|
|
if provider == "tencent-token-plan":
|
|
return fb, "kimi2.5"
|
|
return None, None # primary exhausted
|
|
|
|
with patch("agent.auxiliary_client.resolve_provider_client", side_effect=fake_resolve), \
|
|
patch("model_tools.get_tool_definitions", return_value=_make_tool_defs()), \
|
|
patch("model_tools.check_toolset_requirements", return_value={}), \
|
|
patch("agent.process_bootstrap.OpenAI", return_value=MagicMock()):
|
|
|
|
agent = AIAgent(
|
|
provider="alibaba-coding-plan",
|
|
model="qwen3.6-plus",
|
|
api_key=None,
|
|
base_url=None,
|
|
quiet_mode=True,
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
fallback_model=[{"provider": "tencent-token-plan", "model": "kimi2.5"}],
|
|
)
|
|
assert agent.provider == "tencent-token-plan"
|
|
assert agent.model == "kimi2.5"
|
|
assert agent._fallback_activated is True
|
|
|
|
|
|
def test_init_raises_when_no_fallback_configured():
|
|
"""When primary returns None and no fallback is set, should raise."""
|
|
with patch("agent.auxiliary_client.resolve_provider_client", return_value=(None, None)), \
|
|
patch("model_tools.get_tool_definitions", return_value=_make_tool_defs()), \
|
|
patch("model_tools.check_toolset_requirements", return_value={}), \
|
|
patch("agent.process_bootstrap.OpenAI", return_value=MagicMock()):
|
|
|
|
with pytest.raises(RuntimeError, match="no API key was found"):
|
|
AIAgent(
|
|
provider="alibaba-coding-plan",
|
|
model="qwen3.6-plus",
|
|
api_key=None,
|
|
base_url=None,
|
|
quiet_mode=True,
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
fallback_model=None,
|
|
)
|
|
|
|
|
|
def test_init_tries_fallback_when_openrouter_pool_exhausted():
|
|
"""Regression: an exhausted ``openrouter`` pool (single credential entry
|
|
in 429-cooldown) must still reach fallback_providers at init.
|
|
|
|
Before the fix, the init-time fallback block was nested inside the
|
|
``_explicit not in {auto, openrouter, custom}`` guard, so the default
|
|
openrouter setup skipped the chain entirely and raised the misleading
|
|
"No LLM provider configured" RuntimeError (2026-08-23 outage).
|
|
"""
|
|
fb = _mock_client(api_key="local-key-1234567890",
|
|
base_url="http://127.0.0.1:11434/v1")
|
|
|
|
def fake_resolve(provider, model=None, raw_codex=False,
|
|
explicit_base_url=None, explicit_api_key=None):
|
|
if provider == "custom" and explicit_base_url:
|
|
return fb, "qwen3.5:4b"
|
|
return None, None # openrouter pool exhausted
|
|
|
|
with patch("agent.auxiliary_client.resolve_provider_client", side_effect=fake_resolve), \
|
|
patch("model_tools.get_tool_definitions", return_value=_make_tool_defs()), \
|
|
patch("model_tools.check_toolset_requirements", return_value={}), \
|
|
patch("agent.process_bootstrap.OpenAI", return_value=MagicMock()):
|
|
|
|
agent = AIAgent(
|
|
provider="openrouter",
|
|
model="z-ai/glm-5.2:free",
|
|
api_key=None,
|
|
base_url=None,
|
|
quiet_mode=True,
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
fallback_model=[
|
|
{"provider": "openrouter", "model": "poolside/laguna-s-2.1:free"},
|
|
{"provider": "custom", "model": "qwen3.5:4b",
|
|
"base_url": "http://127.0.0.1:11434/v1"},
|
|
],
|
|
)
|
|
assert agent.provider == "custom"
|
|
assert agent.model == "qwen3.5:4b"
|
|
assert agent._fallback_activated is True
|
|
|
|
|
|
def test_init_openrouter_exhausted_without_chain_keeps_generic_error():
|
|
"""openrouter exhausted + no usable fallback keeps the generic
|
|
'No LLM provider configured' error (not the named-provider one)."""
|
|
with patch("agent.auxiliary_client.resolve_provider_client", return_value=(None, None)), \
|
|
patch("model_tools.get_tool_definitions", return_value=_make_tool_defs()), \
|
|
patch("model_tools.check_toolset_requirements", return_value={}), \
|
|
patch("agent.process_bootstrap.OpenAI", return_value=MagicMock()):
|
|
|
|
with pytest.raises(RuntimeError, match="No LLM provider configured"):
|
|
AIAgent(
|
|
provider="openrouter",
|
|
model="z-ai/glm-5.2:free",
|
|
api_key=None,
|
|
base_url=None,
|
|
quiet_mode=True,
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
fallback_model=[{"provider": "openrouter",
|
|
"model": "poolside/laguna-s-2.1:free"}],
|
|
)
|