Skip to content

Personal AI, On Personal Devices

Python

perf(routing): avoid repeated scans in query signals (#1097)

Huanyi Xie26f70d20064f
diff --git a/src/openjarvis/learning/routing/complexity.py b/src/openjarvis/learning/routing/complexity.py
index 3a60c7ac..97551eb4 100644
--- a/src/openjarvis/learning/routing/complexity.py
+++ b/src/openjarvis/learning/routing/complexity.py
@@ -36,17 +36,29 @@ _REASONING_PATTERNS = re.compile(
re.IGNORECASE,
)
_MULTI_STEP_PATTERNS = re.compile(
- r"\bthen\b.*\bthen\b|\bfirst\b.*\bnext\b|\bstep\s*\d"
+ r"\bstep\s*\d"
r"|\b(?:and\s+also|additionally|furthermore)\b"
r"|\b\d+\.\s",
re.IGNORECASE | re.DOTALL,
)
_CREATIVE_PATTERNS = re.compile(
- r"\bwrite\b.*\b(?:essay|story|article|report|poem)\b"
- r"|\bgenerate\b.*\b(?:code|script|program)\b"
- r"|\bcreate\b|\bdesign\b|\bdraft\b|\bcompose\b",
+ r"\bcreate\b|\bdesign\b|\bdraft\b|\bcompose\b",
re.IGNORECASE,
)
+_MULTI_STEP_PAIRS = (
+ (re.compile(r"\bthen\b", re.IGNORECASE), re.compile(r"\bthen\b", re.IGNORECASE)),
+ (re.compile(r"\bfirst\b", re.IGNORECASE), re.compile(r"\bnext\b", re.IGNORECASE)),
+)
+_CREATIVE_PAIRS = (
+ (
+ re.compile(r"\bwrite\b", re.IGNORECASE),
+ re.compile(r"\b(?:essay|story|article|report|poem)\b", re.IGNORECASE),
+ ),
+ (
+ re.compile(r"\bgenerate\b", re.IGNORECASE),
+ re.compile(r"\b(?:code|script|program)\b", re.IGNORECASE),
+ ),
+)
# Models known to use internal chain-of-thought that consumes output tokens.
_THINKING_MODEL_PATTERNS = re.compile(
@@ -96,6 +108,12 @@ def _count_sub_tasks(query: str) -> int:
return numbered + bulleted
+def _ordered_words(text: str, first: re.Pattern[str], second: re.Pattern[str]) -> bool:
+ """Check word order without rescanning the suffix for each opening word."""
+ opening = first.search(text)
+ return opening is not None and second.search(text, opening.end()) is not None
+
+
def score_complexity(query: str) -> ComplexityResult:
"""Score a query's complexity from 0.0 (trivial) to 1.0 (very complex).
@@ -139,7 +157,9 @@ def score_complexity(query: str) -> ComplexityResult:
# --- Reasoning signal (0–0.25) ---
has_reasoning = bool(_REASONING_PATTERNS.search(query))
- has_multi_step = bool(_MULTI_STEP_PATTERNS.search(query))
+ has_multi_step = bool(_MULTI_STEP_PATTERNS.search(query)) or any(
+ _ordered_words(query, first, second) for first, second in _MULTI_STEP_PAIRS
+ )
reasoning_score = 0.0
if has_reasoning:
reasoning_score = 0.6
@@ -168,7 +188,11 @@ def score_complexity(query: str) -> ComplexityResult:
score += 0.15 * multi_score
# --- Creative / generative signal (0–0.15) ---
- has_creative = bool(_CREATIVE_PATTERNS.search(query))
+ has_creative = bool(_CREATIVE_PATTERNS.search(query)) or any(
+ _ordered_words(line, first, second)
+ for line in query.split("\n")
+ for first, second in _CREATIVE_PAIRS
+ )
creative_score = 0.7 if has_creative else 0.0
signals["creative"] = creative_score
signals["has_creative"] = has_creative
diff --git a/tests/learning/routing/test_complexity.py b/tests/learning/routing/test_complexity.py
index 999eaf1e..825504b2 100644
--- a/tests/learning/routing/test_complexity.py
+++ b/tests/learning/routing/test_complexity.py
@@ -2,6 +2,11 @@
from __future__ import annotations
+import subprocess
+import sys
+
+import pytest
+
from openjarvis.learning.routing.complexity import (
ComplexityQueryAnalyzer,
ComplexityResult,
@@ -47,6 +52,51 @@ class TestScoreComplexity:
result = score_complexity("First do X, then do Y, then do Z")
assert result.signals["has_multi_step"] is True
+ @pytest.mark.parametrize(
+ ("query", "expected"),
+ [
+ ("First " + "context " * 500 + "next", True),
+ ("then\nmore context\nTHEN", True),
+ ("next, first", False),
+ ("then only once", False),
+ ("firsthand knowledge comes next", False),
+ ],
+ )
+ def test_multi_step_preserves_order_and_word_boundaries(self, query, expected):
+ assert score_complexity(query).signals["has_multi_step"] is expected
+
+ @pytest.mark.parametrize(
+ ("query", "expected"),
+ [
+ ("Write " + "context " * 500 + "a story", True),
+ ("generate a SCRIPT", True),
+ ("story, then write", False),
+ ("write\nan essay", False),
+ ("write\ran essay", True),
+ ("generate\ncode", False),
+ ],
+ )
+ def test_creative_preserves_order_and_line_boundaries(self, query, expected):
+ assert score_complexity(query).signals["has_creative"] is expected
+
+ @pytest.mark.parametrize("word", ["first", "write"])
+ def test_repeated_opening_word_does_not_block_scoring(self, word):
+ code = (
+ "import sys\n"
+ "from openjarvis.learning.routing.complexity import score_complexity\n"
+ "result = score_complexity(sys.stdin.read())\n"
+ "assert not result.signals['has_multi_step']\n"
+ "assert not result.signals['has_creative']\n"
+ )
+ subprocess.run(
+ [sys.executable, "-c", code],
+ input=f"{word} " * 40_000,
+ text=True,
+ capture_output=True,
+ check=True,
+ timeout=10,
+ )
+
def test_reasoning_and_multi_step_combined(self) -> None:
result = score_complexity(
"Explain why X works, then analyze Y, then compare them"