mirror of
https://github.com/open-jarvis/OpenJarvis.git
synced 2026-07-28 05:12:26 +00:00
`jarvis ask "..."` (no `--agent` flag) routed straight to
`engine.generate()` regardless of `agent.default_agent` in the user's
config. As a result the persona stack — `agent.default_system_prompt`,
SOUL.md, MEMORY.md, USER.md — was silently bypassed for the most
common command.
Behavior now:
- `--agent X` → use agent X (unchanged)
- `--agent ""` (empty) → explicit opt-out, direct-to-engine mode
- (omitted) → fall back to `config.agent.default_agent`,
which dataclass-defaults to `"simple"`,
so persona settings finally take effect
Tests:
- Rename `test_no_agent_uses_direct_mode` to
`test_no_agent_flag_falls_back_to_config_default_agent` and update
its docstring to document the new behavior.
- Add `test_explicit_empty_agent_opts_out_of_agent_mode`.
- Add `test_no_agent_with_blank_config_default_uses_direct_mode` to
cover the case where the user clears `default_agent`.
- Update `_patch_engine` (test_ask_router) and `_patch_ask`
(test_ask_e2e) to re-register `SimpleAgent` after the autouse
`_clean_registries` conftest fixture, since the agent path now
runs in tests that previously short-circuited to direct mode.
- Add explicit `cfg.agent.default_agent = ""` to two
`test_ask_router` tests that mock load_config with a MagicMock
(so `cfg.agent.default_agent` doesn't auto-create as a truthy mock).
Note: `tests/cli/test_ask_context.py` has 2 unrelated pre-existing
failures on origin/main (memory backend returns None on Windows);
those are out of scope for this PR.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
149 lines
5.0 KiB
Python
149 lines
5.0 KiB
Python
"""Tests for model resolution fallback chain in jarvis ask."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
from unittest import mock
|
|
|
|
from click.testing import CliRunner
|
|
|
|
from openjarvis.cli import cli
|
|
|
|
_ask_mod = importlib.import_module("openjarvis.cli.ask")
|
|
|
|
|
|
def _mock_engine():
|
|
"""Create a mock engine that returns a simple response."""
|
|
engine = mock.MagicMock()
|
|
engine.engine_id = "mock"
|
|
engine.health.return_value = True
|
|
engine.list_models.return_value = ["test-model"]
|
|
engine.generate.return_value = {
|
|
"content": "Hello!",
|
|
"usage": {"prompt_tokens": 5, "completion_tokens": 3, "total_tokens": 8},
|
|
"model": "test-model",
|
|
"finish_reason": "stop",
|
|
}
|
|
return engine
|
|
|
|
|
|
def _register_agents():
|
|
"""Re-register agents after the conftest registry clear.
|
|
|
|
The default ``JarvisConfig().agent.default_agent`` is ``"simple"``,
|
|
so ``jarvis ask "..."`` (without ``--agent``) routes through SimpleAgent.
|
|
Without this re-registration, that path raises ``Unknown agent: simple``.
|
|
"""
|
|
from openjarvis.agents.simple import SimpleAgent
|
|
from openjarvis.core.registry import AgentRegistry
|
|
|
|
if not AgentRegistry.contains("simple"):
|
|
AgentRegistry.register_value("simple", SimpleAgent)
|
|
|
|
|
|
def _patch_engine(engine):
|
|
"""Return context managers that patch engine discovery to use our mock."""
|
|
_register_agents()
|
|
return (
|
|
mock.patch.object(
|
|
_ask_mod,
|
|
"get_engine",
|
|
return_value=("mock", engine),
|
|
),
|
|
mock.patch.object(
|
|
_ask_mod,
|
|
"discover_engines",
|
|
return_value={"mock": engine},
|
|
),
|
|
mock.patch.object(
|
|
_ask_mod,
|
|
"discover_models",
|
|
return_value={"mock": ["test-model"]},
|
|
),
|
|
mock.patch.object(_ask_mod, "register_builtin_models"),
|
|
mock.patch.object(_ask_mod, "merge_discovered_models"),
|
|
mock.patch.object(_ask_mod, "TelemetryStore"),
|
|
)
|
|
|
|
|
|
class TestAskModelResolution:
|
|
def test_default_model_from_config(self) -> None:
|
|
"""When no -m flag, uses config.intelligence.default_model."""
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
with patches[0], patches[1], patches[2], patches[3], patches[4], patches[5]:
|
|
result = CliRunner().invoke(cli, ["ask", "Hello"])
|
|
assert result.exit_code == 0
|
|
assert "Hello!" in result.output
|
|
|
|
def test_explicit_model_flag(self) -> None:
|
|
"""The -m flag directly selects a model, bypassing fallback chain."""
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
with patches[0], patches[1], patches[2], patches[3], patches[4], patches[5]:
|
|
result = CliRunner().invoke(
|
|
cli,
|
|
["ask", "-m", "test-model", "Hello"],
|
|
)
|
|
assert result.exit_code == 0
|
|
assert "Hello!" in result.output
|
|
|
|
def test_fallback_to_engine_models(self) -> None:
|
|
"""When default_model is empty, falls back to first engine model."""
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
with (
|
|
patches[0],
|
|
patches[1],
|
|
patches[2],
|
|
patches[3],
|
|
patches[4],
|
|
patches[5],
|
|
mock.patch.object(
|
|
_ask_mod,
|
|
"load_config",
|
|
) as mock_config,
|
|
):
|
|
cfg = mock_config.return_value
|
|
cfg.telemetry.enabled = False
|
|
cfg.intelligence.default_model = ""
|
|
cfg.intelligence.fallback_model = ""
|
|
cfg.intelligence.temperature = 0.7
|
|
cfg.intelligence.max_tokens = 1024
|
|
cfg.agent.context_from_memory = False
|
|
cfg.agent.default_agent = ""
|
|
result = CliRunner().invoke(cli, ["ask", "Hello"])
|
|
assert result.exit_code == 0
|
|
|
|
def test_fallback_to_fallback_model(self) -> None:
|
|
"""When default_model is empty and no engine models, uses fallback_model."""
|
|
engine = _mock_engine()
|
|
patches = _patch_engine(engine)
|
|
# Override discover_models to return empty list
|
|
with (
|
|
patches[0],
|
|
patches[1],
|
|
mock.patch.object(
|
|
_ask_mod,
|
|
"discover_models",
|
|
return_value={"mock": []},
|
|
),
|
|
patches[3],
|
|
patches[4],
|
|
patches[5],
|
|
mock.patch.object(
|
|
_ask_mod,
|
|
"load_config",
|
|
) as mock_config,
|
|
):
|
|
cfg = mock_config.return_value
|
|
cfg.telemetry.enabled = False
|
|
cfg.intelligence.default_model = ""
|
|
cfg.intelligence.fallback_model = "fallback-model"
|
|
cfg.intelligence.temperature = 0.7
|
|
cfg.intelligence.max_tokens = 1024
|
|
cfg.agent.context_from_memory = False
|
|
cfg.agent.default_agent = ""
|
|
result = CliRunner().invoke(cli, ["ask", "Hello"])
|
|
assert result.exit_code == 0
|