Personal AI, On Personal Devices
Python
fix: reorder HeuristicRouter rules so low-complexity check precedes math check (#671)
Ari Vitor S. Lazzarotto8d90e3dff164
diff --git a/src/openjarvis/learning/routing/router.py b/src/openjarvis/learning/routing/router.pyindex 1950e48d..78956ea4 100644--- a/src/openjarvis/learning/routing/router.py+++ b/src/openjarvis/learning/routing/router.py@@ -93,11 +93,16 @@ class HeuristicRouter(RouterPolicy):Rules (applied in order):1. Code detected → prefer model with "code"/"coder" in name- 2. Math detected → prefer larger model- 3. Low complexity (score < 0.20) → prefer smaller/faster model+ 2. Low complexity (score <= 0.20) → prefer smaller/faster model+ 3. Math detected → prefer larger model4. High complexity (score >= 0.55 OR reasoning keywords) → prefer larger model5. High urgency (>0.8) → override to smaller model6. Default fallback → default_model → fallback_model → first available++ Low complexity is checked before the math check so that simple arithmetic+ ("calculate 2+2") routes to the smallest model instead of always escalating+ on the "math" keyword; math problems above the low-complexity threshold+ still escalate to the larger model."""def __init__(@@ -134,14 +139,15 @@ class HeuristicRouter(RouterPolicy):# Fall through to larger model for codereturn _largest_model(available) or available[0]- # Rule 2: Math detected → prefer larger model+ # Rule 2: Low complexity → prefer smaller model (checked before the math+ # rule so simple arithmetic doesn't escalate to the largest model)+ if context.complexity_score <= 0.20:+ return _smallest_model(available) or available[0]++ # Rule 3: Math detected → prefer larger modelif context.has_math:return _largest_model(available) or available[0]- # Rule 3: Low complexity → prefer smaller model- if context.complexity_score < 0.20:- return _smallest_model(available) or available[0]-# Rule 4: High complexity or reasoning → prefer larger modelif context.complexity_score >= 0.55 or context.has_reasoning:return _largest_model(available) or available[0]diff --git a/tests/learning/test_router.py b/tests/learning/test_router.pyindex 736a7f8c..e4b7def3 100644--- a/tests/learning/test_router.py+++ b/tests/learning/test_router.py@@ -88,13 +88,25 @@ class TestHeuristicRouter:router = HeuristicRouter(available_models=["small", "large", "coder"],)- ctx = RoutingContext(- query="solve x",- query_length=7,- has_math=True,- )+ ctx = build_routing_context("solve the integral of x^2 dx")+ assert ctx.has_math is True+ assert ctx.complexity_score > 0.20assert router.select_model(ctx) == "large"+ def test_low_complexity_math_prefers_small(self) -> None:+ """Regression test: a trivial math query ("calculate 2+2") must not+ escalate to the largest model just because it contains a math+ keyword — the low-complexity rule takes priority over the math rule.+ """+ _register_models()+ router = HeuristicRouter(+ available_models=["small", "large", "coder"],+ )+ ctx = build_routing_context("calculate 2+2")+ assert ctx.has_math is True+ assert ctx.complexity_score == 0.20+ assert router.select_model(ctx) == "small"+def test_high_complexity_prefers_large(self) -> None:_register_models()router = HeuristicRouter(diff --git a/tests/learning/test_routing_models.py b/tests/learning/test_routing_models.pyindex ebe4917a..ceea8de9 100644--- a/tests/learning/test_routing_models.py+++ b/tests/learning/test_routing_models.py@@ -77,11 +77,9 @@ class TestRouterWithNewModels:router = HeuristicRouter(available_models=NEW_LOCAL_MODELS,)- ctx = RoutingContext(- query="solve the integral of x^2 dx",- query_length=29,- has_math=True,- )+ ctx = build_routing_context("solve the integral of x^2 dx")+ assert ctx.has_math is True+ assert ctx.complexity_score > 0.20selected = router.select_model(ctx)assert selected == "gpt-oss:120b"