Files
OpenJarvis/tests/learning/test_router.py
T
05f2c02131 feat: Algolia DocSearch + learning subsystem reorganization (#43)
* chore: create learning subdirectory structure (routing, agents, intelligence)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: extract classify_query to routing/_utils.py

Move the classify_query() function and its regex patterns into a shared
utility module so multiple routing policies can import it without
depending on the full trace_policy module.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: move routing files to learning/routing/ subdirectory

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: create LearnedRouterPolicy merging trace-driven + SFT routing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add conditional Algolia DocSearch integration

Add Algolia DocSearch as an optional search upgrade — native lunr.js
search remains the default until credentials are configured. Includes
CDN assets, Jinja2 conditional config injection, init script with
graceful fallback, light/dark theme CSS, improved search tokenization
for snake_case/dotted identifiers, and search boosts for key pages.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: move agent_evolver and skill_discovery to learning/agents/

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: move learning/orchestrator to learning/intelligence/orchestrator

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: delete removed learning policies, rewrite __init__.py, clean up api_routes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add SFT/GRPO/DSPy/GEPA config dataclasses, update LearningConfig

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add general-purpose SFT trainer (intelligence/sft_trainer.py)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: update stale imports in multi_model_router example

Update imports to use new learning/routing/ paths after the
subdirectory reorganization. Replace BanditRouterPolicy with
LearnedRouterPolicy.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add general-purpose GRPO trainer (intelligence/grpo_trainer.py)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add DSPy agent optimizer (agents/dspy_optimizer.py)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add GEPA agent optimizer (agents/gepa_optimizer.py)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add learning-dspy and learning-gepa optional dependency extras

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: update integration test to check for learned policy instead of grpo

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: clean up stale APIs and unused params in examples

- deep_research: remove system_prompt and max_turns params not accepted
  by Jarvis.ask(), inline system prompt into the query instead
- doc_qa: remove unused --top-k CLI arg that was never passed to the API
- multi_model_router: fix select_model() call to match single-arg signature

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: import SFT/GRPO trainers in intelligence/__init__.py for registry

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: remove .md file changes from PR

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: restore search boost frontmatter for key docs pages

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 21:34:31 -07:00

145 lines
4.6 KiB
Python

"""Tests for the heuristic model router (canonical location)."""
from __future__ import annotations
from openjarvis.core.registry import ModelRegistry
from openjarvis.core.types import ModelSpec
from openjarvis.learning._stubs import RoutingContext
from openjarvis.learning.routing.router import (
HeuristicRouter,
build_routing_context,
)
def _register_models() -> None:
"""Register a small set of models for testing."""
ModelRegistry.register_value(
"small",
ModelSpec(
model_id="small", name="Small",
parameter_count_b=3.0, context_length=4096,
),
)
ModelRegistry.register_value(
"large",
ModelSpec(
model_id="large", name="Large",
parameter_count_b=70.0, context_length=131072,
),
)
ModelRegistry.register_value(
"coder",
ModelSpec(
model_id="coder", name="DeepSeek Coder",
parameter_count_b=16.0, context_length=131072,
),
)
class TestBuildRoutingContext:
def test_code_detection(self) -> None:
ctx = build_routing_context("def hello():\n pass")
assert ctx.has_code is True
assert ctx.has_math is False
def test_math_detection(self) -> None:
ctx = build_routing_context("solve the integral of x^2")
assert ctx.has_math is True
assert ctx.has_code is False
def test_length(self) -> None:
ctx = build_routing_context("Hi")
assert ctx.query_length == 2
def test_urgency_default(self) -> None:
ctx = build_routing_context("test")
assert ctx.urgency == 0.5
class TestHeuristicRouter:
def test_short_query_prefers_small(self) -> None:
_register_models()
router = HeuristicRouter(
available_models=["small", "large", "coder"],
)
ctx = RoutingContext(query="Hi", query_length=2)
assert router.select_model(ctx) == "small"
def test_code_prefers_coder(self) -> None:
_register_models()
router = HeuristicRouter(
available_models=["small", "large", "coder"],
)
ctx = RoutingContext(
query="def foo():", query_length=10, has_code=True,
)
assert router.select_model(ctx) == "coder"
def test_math_prefers_large(self) -> None:
_register_models()
router = HeuristicRouter(
available_models=["small", "large", "coder"],
)
ctx = RoutingContext(
query="solve x", query_length=7, has_math=True,
)
assert router.select_model(ctx) == "large"
def test_long_query_prefers_large(self) -> None:
_register_models()
router = HeuristicRouter(
available_models=["small", "large", "coder"],
)
ctx = RoutingContext(query="x" * 501, query_length=501)
assert router.select_model(ctx) == "large"
def test_high_urgency_overrides_to_small(self) -> None:
_register_models()
router = HeuristicRouter(
available_models=["small", "large", "coder"],
)
ctx = RoutingContext(
query="x" * 501, query_length=501, urgency=0.9,
)
assert router.select_model(ctx) == "small"
def test_fallback_chain(self) -> None:
_register_models()
router = HeuristicRouter(
available_models=["small", "large"],
default_model="large",
fallback_model="small",
)
# Medium-length, no code/math, no reasoning → falls to default
ctx = RoutingContext(
query="Tell me about cats", query_length=60,
)
assert router.select_model(ctx) == "large"
def test_no_available_models(self) -> None:
router = HeuristicRouter(
available_models=[], default_model="fallback-model",
)
ctx = RoutingContext(query="test", query_length=4)
assert router.select_model(ctx) == "fallback-model"
def test_reasoning_keywords_prefer_large(self) -> None:
_register_models()
router = HeuristicRouter(available_models=["small", "large"])
query = (
"Please explain step by step how the process"
" of photosynthesis works in plants"
)
ctx = build_routing_context(query)
assert router.select_model(ctx) == "large"
def test_code_without_coder_falls_to_large(self) -> None:
_register_models()
router = HeuristicRouter(
available_models=["small", "large"],
)
ctx = RoutingContext(
query="def foo():", query_length=10, has_code=True,
)
assert router.select_model(ctx) == "large"