Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 281 additions & 0 deletions tests/test_intent_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
"""Query routing and post-search reranking in mcp-server/intent_router.py.

Expectations come from the module's docstrings and comments ("first matching
branch wins", "newest release_date first; tie-break by search score", ...),
not from whatever the functions currently return.
"""

import sys
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "docs-agent-mcp" / "mcp-server"))

import intent_router # noqa: E402
from intent_router import ( # noqa: E402
KEYWORD_FETCH_LIMIT,
LATEST_RELEASE_FETCH_LIMIT,
RetrievalPlan,
boost_release_docs,
downgrade_plan_for_collection,
pick_search_plan,
rerank_by_release_date,
rerank_for_version_match,
rerank_hits_after_search,
retrieval_metadata,
)


def hit(hit_id, distance=0.5, **entity):
return {"id": hit_id, "distance": distance, "entity": entity}


def ids(hits):
return [h["id"] for h in hits]


# --- pick_search_plan ---------------------------------------------------------


@pytest.mark.parametrize(
"query",
[
"compare Katib and Optuna",
"KServe versus Seldon",
"difference between Trainer and Training Operator",
"Kubeflow 1.9 and 1.10 changes",
],
)
def test_comparison_queries_use_hybrid_with_keyword_depth(query):
plan = pick_search_plan(query)

assert plan.intent == "comparison"
assert plan.retrieval_mode == "hybrid"
assert plan.candidate_depth == KEYWORD_FETCH_LIMIT


def test_release_date_question_prefers_version_match():
plan = pick_search_plan("When was Kubeflow 1.9 released?")

assert plan.intent == "release_date"
assert plan.retrieval_mode == "bm25"
assert plan.candidate_depth == LATEST_RELEASE_FETCH_LIMIT
assert plan.prefer_version_match is True
assert plan.rerank_by_release_date is False


@pytest.mark.parametrize("word", ["latest", "current", "newest", "most recent", "supported"])
def test_latest_or_current_query_filters_release_notes_and_sorts_by_date(word):
plan = pick_search_plan(f"what is the {word} Kubeflow release")

assert plan.intent == "temporal"
assert plan.retrieval_mode == "bm25"
assert plan.filter_expr == 'doc_type == "release"'
assert plan.rerank_by_release_date is True
assert plan.candidate_depth == LATEST_RELEASE_FETCH_LIMIT


@pytest.mark.parametrize(
"query",
[
"install kfp 2.5.0",
"katib v1beta1 experiment spec",
"set spec.runPolicy.cleanPodPolicy",
"pipeline step failed with OOM",
"notebook spawn hits a timeout",
],
)
def test_exact_version_config_key_or_error_uses_bm25(query):
plan = pick_search_plan(query)

assert plan.intent == "exact"
assert plan.retrieval_mode == "bm25"
assert plan.candidate_depth == KEYWORD_FETCH_LIMIT


@pytest.mark.parametrize("query", ["how does Katib tune hyperparameters", "explain the KServe architecture"])
def test_conceptual_queries_use_hybrid_without_candidate_depth(query):
plan = pick_search_plan(query)

assert plan.intent == "conceptual"
assert plan.retrieval_mode == "hybrid"
assert plan.candidate_depth is None


def test_unmatched_query_falls_back_to_general_hybrid():
plan = pick_search_plan("Kubeflow notebooks")

assert plan.intent == "general"
assert plan.retrieval_mode == "hybrid"
assert plan.filter_expr == ""


@pytest.mark.parametrize(
"query, expected_intent",
[
# compare/vs is checked before every other rule
("compare the latest releases", "comparison"),
# release-date wording is checked before latest/current
("when was the latest release", "release_date"),
# latest/current is checked before exact version
("is 1.9 the current release", "temporal"),
# error wording is checked before how/why
("how do I fix this timeout error", "exact"),
],
)
def test_first_matching_rule_wins(query, expected_intent):
assert pick_search_plan(query).intent == expected_intent


# --- downgrade_plan_for_collection -------------------------------------------

TEMPORAL = pick_search_plan("latest Kubeflow release")


@pytest.mark.parametrize("mode", ["bm25", "hybrid"])
def test_collection_without_bm25_falls_back_to_plain_dense(mode):
plan = RetrievalPlan(
intent="temporal",
retrieval_mode=mode,
reason="r",
candidate_depth=50,
filter_expr='doc_type == "release"',
rerank_by_release_date=True,
prefer_version_match=True,
)

downgraded = downgrade_plan_for_collection(plan, has_bm25=False, has_release_fields=True)

assert downgraded.retrieval_mode == "dense"
assert downgraded.filter_expr == ""
assert downgraded.rerank_by_release_date is False
assert downgraded.prefer_version_match is False
assert downgraded.candidate_depth is None
assert "dense fallback" in downgraded.reason
assert downgraded.intent == "temporal"


def test_collection_without_release_fields_drops_release_filter_and_reranks():
downgraded = downgrade_plan_for_collection(TEMPORAL, has_bm25=True, has_release_fields=False)

assert downgraded.retrieval_mode == "bm25"
assert downgraded.candidate_depth == TEMPORAL.candidate_depth
assert downgraded.filter_expr == ""
assert downgraded.rerank_by_release_date is False
assert downgraded.prefer_version_match is False


def test_capable_collection_keeps_the_plan():
assert downgrade_plan_for_collection(TEMPORAL, has_bm25=True, has_release_fields=True) == TEMPORAL


# --- reranking ----------------------------------------------------------------


def test_boost_release_docs_moves_release_notes_first_keeping_order():
hits = [hit(1, doc_type="guide"), hit(2, doc_type="release"), hit(3), hit(4, doc_type="release")]

assert ids(boost_release_docs(hits)) == [2, 4, 1, 3]


def test_boost_release_docs_without_release_notes_is_unchanged():
hits = [hit(1), hit(2, doc_type="guide")]

assert boost_release_docs(hits) == hits


def test_rerank_by_release_date_puts_newest_first_and_undated_last():
hits = [
hit(1, release_date=1_700_000_000),
hit(2),
hit(3, release_date=1_720_000_000),
hit(4, release_date=1_710_000_000),
]

assert ids(rerank_by_release_date(hits, top_k=10)) == [3, 4, 1, 2]


def test_rerank_by_release_date_breaks_ties_by_score():
hits = [hit(1, 0.2, release_date=1), hit(2, 0.9, release_date=1)]

assert ids(rerank_by_release_date(hits, top_k=10)) == [2, 1]


def test_rerank_by_release_date_trims_to_top_k():
hits = [hit(i, release_date=i) for i in range(5)]

assert ids(rerank_by_release_date(hits, top_k=2)) == [4, 3]


def test_rerank_by_release_date_without_dates_only_trims():
hits = [hit(1), hit(2), hit(3)]

assert ids(rerank_by_release_date(hits, top_k=2)) == [1, 2]


@pytest.mark.parametrize(
"entity",
[
{"version": "1.9"},
{"content_text": "Kubeflow 1.9 was released in July"},
{"file_path": "content/en/docs/releases/kubeflow-1.9.md"},
{"content_text": "see v1.9 notes"},
],
)
def test_rerank_for_version_match_keeps_hits_mentioning_the_version(entity):
hits = [hit(1, version="1.8"), hit(2, **entity)]

assert ids(rerank_for_version_match(hits, "when was v1.9 released", top_k=5)) == [2]


def test_rerank_for_version_match_keeps_all_hits_when_none_match():
hits = [hit(1, version="1.7"), hit(2, version="1.8"), hit(3)]

assert ids(rerank_for_version_match(hits, "when was 1.9 released", top_k=2)) == [1, 2]


def test_rerank_for_version_match_without_version_only_trims():
hits = [hit(1), hit(2), hit(3)]

assert ids(rerank_for_version_match(hits, "when was Katib released", top_k=2)) == [1, 2]


def test_temporal_rerank_boosts_release_notes_then_sorts_by_date():
hits = [hit(1, release_date=1_730_000_000), hit(2, doc_type="release", release_date=1_700_000_000), hit(3)]

reranked = rerank_hits_after_search(TEMPORAL, hits, "latest release", top_k=3)

assert ids(reranked) == [1, 2, 3]


def test_release_date_rerank_filters_to_version_matches():
plan = pick_search_plan("When was Kubeflow 1.9 released?")
hits = [hit(1, version="1.8"), hit(2, version="1.9"), hit(3, version="1.9.1")]

assert ids(rerank_hits_after_search(plan, hits, "When was Kubeflow 1.9 released?", top_k=5)) == [2, 3]


def test_plans_without_rerank_flags_only_trim():
plan = pick_search_plan("install kfp 2.5.0")
hits = [hit(i) for i in range(10)]

assert ids(rerank_hits_after_search(plan, hits, "install kfp 2.5.0", top_k=3)) == [0, 1, 2]


def test_retrieval_metadata_reports_plan_and_extras():
meta = retrieval_metadata(TEMPORAL, candidate_depth=50, filter_fallback=True)

assert meta == {
"retrieval_mode": "bm25",
"intent": "temporal",
"reason": TEMPORAL.reason,
"candidate_depth": 50,
"filter_fallback": True,
}


def test_configmap_aliases_match_fetch_limits():
assert intent_router.AUTO_TEMPORAL_CANDIDATE_DEPTH == LATEST_RELEASE_FETCH_LIMIT
assert intent_router.AUTO_BM25_CANDIDATE_DEPTH == KEYWORD_FETCH_LIMIT
Loading
Loading