Spaces:
Sleeping
feat(closer): KI-105 — wire ranked recommendation handoff on closer intent
Browse filesRoot cause (live 15-persona smoke test): when fact-find was complete and the
user asked for a recommendation ("show me policies", "what would you recommend",
"compare top 3"), the bot didn't produce a ranked shortlist. It either re-asked
a filled slot, refused with "I'd rather not speculate", or leaked CoT — even
though middle-conversation turns had already surfaced 15+ named policies.
Two intertwined bugs:
HYPOTHESIS A (PRIMARY): classify_intent misrouted closer phrases.
- "show me the top 3 policies", "show me policies", "give me the top 3",
"rank the best", "pitch me the top 3", "your top picks" → qa
- "what would you recommend" → fact_find (matched FACT_FIND_TRIGGERS
entry "what do you recommend" first)
Because intent never landed on recommendation/comparison, pick_brain
routed to the fast brain (DeepSeek-V4-Flash) with the default advisor
prompt — designed for short conservative single-clause answers, not for
multi-policy synthesis.
HYPOTHESIS B (SECONDARY): even when intent did route correctly, the persona
system prompt had no output contract for ranked-shortlist generation.
ADVISOR_SYSTEM_PROMPT_V1 emphasises 60-word voice replies, refusal on
weak grounding, and "it is OK to say 'this policy isn't right for you'" —
nothing tells the brain "produce 3 ranked policies with citations."
Fix (touches orchestrator.py + persona.py + tests only; rag/retrieve.py
and backend/main.py left for siblings KI-106/KI-107):
1. classify_intent gains an explicit-closer override checked BEFORE the
FACT_FIND_TRIGGERS short-circuit. _EXPLICIT_CLOSER_RECOMMENDATION /
_EXPLICIT_CLOSER_COMPARISON cover the live-smoke phrases. The KI-013
guard (empty-profile recommendation → force fact-find) still works
downstream via should_route_to_fact_find — unchanged.
2. COMPARISON_KEYWORDS + RECOMMEND_KEYWORDS extended with "rank",
"shortlist", "side by side", "show me", "top 3", "your top picks",
etc. so non-explicit-closer variants still classify correctly.
3. New RECOMMENDATION_CLOSER_ADDENDUM appended to the system prompt by
build_messages(intent=...) ONLY when intent in {recommendation,
comparison} AND user_profile is non-empty. The addendum is a strict
output contract: 150-220 words, exactly 3 ranked policies with
"<rank>. <Insurer> — <Policy Name>" line + 2-3 line rationale +
[Source: ...] citation per claim, no new fact-find questions, no
refusal unless retrieval returned zero relevant chunks.
4. orchestrator.handle_turn widens top_k to 12 on closer intents (was
5) so the candidate set has cross-insurer diversity for the 3-policy
contract; synthesises a profile dict from session.profile when the
caller passed user_profile=None on a closer turn (so the addendum
fires); bumps temperature 0.2→0.3 on closer turns so rationale
prose doesn't read as boilerplate copy-paste between policies.
5. New tests in test_routing_regression.py:
- TestIntentClassification.test_ki105_closer_phrases_classify_as_recommendation
- TestIntentClassification.test_ki105_closer_phrases_classify_as_comparison
- TestKI105CloserPersona (4 cases): addendum present on
recommendation/comparison turns, absent on qa, absent on
empty-profile recommendation (KI-013 belt-and-braces).
All 75 tests in tests/ pass. classify_intent verified manually on the
full closer-phrase set plus the existing qa/fact_find regression set.
Faithfulness gate still runs over the closer reply: every cited policy
name + numeric must trace to a retrieved chunk, so the addendum cannot
itself invent insurer names — it just templates the output shape over
retrieved evidence.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- backend/orchestrator.py +117 -5
- backend/persona.py +43 -1
- tests/test_routing_regression.py +104 -0
|
@@ -45,8 +45,26 @@ FACT_FIND_TRIGGERS = (
|
|
| 45 |
"hi", "hello", "hey", "namaste",
|
| 46 |
)
|
| 47 |
|
| 48 |
-
COMPARISON_KEYWORDS = (
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
INDIC_KEYWORDS = (
|
| 51 |
# Devanagari letters
|
| 52 |
"क", "ख", "ग", "घ", "च", "ज", "ट", "ड", "त", "द", "न", "प", "ब", "म", "य", "र", "ल", "व", "स", "ह",
|
|
@@ -70,8 +88,46 @@ def _phrase_present(phrase: str, q: str) -> bool:
|
|
| 70 |
return re.search(pattern, q) is not None
|
| 71 |
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
def classify_intent(query: str) -> str:
|
| 74 |
q = query.lower().strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# Greeting / advice-seeking openers → fact-find flow
|
| 76 |
if any(_phrase_present(kw, q) for kw in FACT_FIND_TRIGGERS) and len(q.split()) < 25:
|
| 77 |
return "fact_find"
|
|
@@ -648,9 +704,22 @@ async def handle_turn(
|
|
| 648 |
# Chroma at POST /api/profile time) gets boosted to the top of the
|
| 649 |
# context. Without session_id this path is dormant and the brain never
|
| 650 |
# sees the user's profile inline with policy text.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
chunks: list[RetrievedChunk] = await retrieve(
|
| 652 |
query=user_text,
|
| 653 |
-
top_k=
|
| 654 |
policy_ids=policy_filter_ids,
|
| 655 |
session_id=session_id,
|
| 656 |
)
|
|
@@ -660,12 +729,44 @@ async def handle_turn(
|
|
| 660 |
pick = pick_brain(intent, language)
|
| 661 |
|
| 662 |
# 4. Generate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
messages_dict = build_messages(
|
| 664 |
user_query=user_text,
|
| 665 |
retrieved_context=context_str,
|
| 666 |
chat_history=chat_history,
|
| 667 |
-
user_profile=
|
| 668 |
view_context=view_context,
|
|
|
|
| 669 |
)
|
| 670 |
messages = [ChatMessage(role=m["role"], content=m["content"]) for m in messages_dict]
|
| 671 |
|
|
@@ -680,8 +781,19 @@ async def handle_turn(
|
|
| 680 |
# is in synchronous probe_all() refresh. 30s is generous enough for
|
| 681 |
# normal MoE responses but short enough to fall through to the chain's
|
| 682 |
# error path before the user gives up.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
llm_result = await asyncio.wait_for(
|
| 684 |
-
pick.provider.chat(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
timeout=30.0,
|
| 686 |
)
|
| 687 |
|
|
|
|
| 45 |
"hi", "hello", "hey", "namaste",
|
| 46 |
)
|
| 47 |
|
| 48 |
+
COMPARISON_KEYWORDS = (
|
| 49 |
+
"compare", "comparison", "vs", "versus", "between policy", "which is better",
|
| 50 |
+
# KI-105 (2026-05-15) — live 15-persona smoke surfaced that closer phrases
|
| 51 |
+
# like "compare top 3", "rank the best", "shortlist" were misrouted to qa
|
| 52 |
+
# because they didn't hit any keyword here. "rank" + "shortlist" are
|
| 53 |
+
# comparison-shaped requests across the candidate set.
|
| 54 |
+
"rank", "ranking", "shortlist", "side by side", "side-by-side",
|
| 55 |
+
)
|
| 56 |
+
RECOMMEND_KEYWORDS = (
|
| 57 |
+
"recommend", "should i", "which one", "best for", "suit me",
|
| 58 |
+
# KI-105 (2026-05-15) — closer phrases that the live smoke caught as
|
| 59 |
+
# misclassified to qa instead of recommendation. Without these the
|
| 60 |
+
# orchestrator never routes to the heavy brain + never templates a
|
| 61 |
+
# ranked output, so the bot either re-asks a slot or refuses.
|
| 62 |
+
"show me", "show me policies", "show me policy", "show me the top",
|
| 63 |
+
"top 3", "top three", "top policies", "give me the top",
|
| 64 |
+
"what would you recommend", "what do you suggest", "your top picks",
|
| 65 |
+
"your top pick", "your best", "my best option", "my best options",
|
| 66 |
+
"pitch me", "pitch your", "policies for me", "policy for me",
|
| 67 |
+
)
|
| 68 |
INDIC_KEYWORDS = (
|
| 69 |
# Devanagari letters
|
| 70 |
"क", "ख", "ग", "घ", "च", "ज", "ट", "ड", "त", "द", "न", "प", "ब", "म", "य", "र", "ल", "व", "स", "ह",
|
|
|
|
| 88 |
return re.search(pattern, q) is not None
|
| 89 |
|
| 90 |
|
| 91 |
+
# KI-105 (2026-05-15) — explicit closer phrases that ALWAYS win the
|
| 92 |
+
# comparison / recommendation label, even when a fact-find trigger
|
| 93 |
+
# also matches. Live 15-persona smoke caught "show me the top 3
|
| 94 |
+
# policies" being routed to fact_find (because "show me" is not in
|
| 95 |
+
# FACT_FIND_TRIGGERS but the q itself is parsed as qa) or to fact-find
|
| 96 |
+
# on phrases like "what would you recommend" (matched
|
| 97 |
+
# FACT_FIND_TRIGGERS["what do you recommend"]). The bot then re-asked
|
| 98 |
+
# slots the user had already filled instead of producing a ranked
|
| 99 |
+
# shortlist. These phrases unambiguously mean "produce a ranked
|
| 100 |
+
# output now"; the downstream session-state-aware routing
|
| 101 |
+
# (`should_route_to_fact_find`) still keeps the KI-013 guard against
|
| 102 |
+
# pitching to empty profiles.
|
| 103 |
+
_EXPLICIT_CLOSER_COMPARISON = (
|
| 104 |
+
"compare top", "compare the top", "rank the top", "rank top",
|
| 105 |
+
"side by side", "side-by-side",
|
| 106 |
+
"compare these", "compare those", "compare them",
|
| 107 |
+
)
|
| 108 |
+
_EXPLICIT_CLOSER_RECOMMENDATION = (
|
| 109 |
+
"show me policies", "show me the policies", "show me policy",
|
| 110 |
+
"show me the top", "show me top",
|
| 111 |
+
"top 3", "top three", "top policies", "top picks",
|
| 112 |
+
"give me the top", "give me top", "give me your top",
|
| 113 |
+
"your top picks", "your top pick", "your best policy",
|
| 114 |
+
"pitch me", "pitch your top",
|
| 115 |
+
"what would you recommend", "what do you suggest",
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
def classify_intent(query: str) -> str:
|
| 120 |
q = query.lower().strip()
|
| 121 |
+
|
| 122 |
+
# KI-105 — explicit closer override. These phrases are unambiguous
|
| 123 |
+
# ranked-output requests; they must beat the fact-find trigger
|
| 124 |
+
# short-circuit below (which would otherwise misroute "show me the
|
| 125 |
+
# top 3 policies" or "what would you recommend" to fact-find).
|
| 126 |
+
if any(_phrase_present(kw, q) for kw in _EXPLICIT_CLOSER_COMPARISON):
|
| 127 |
+
return "comparison"
|
| 128 |
+
if any(_phrase_present(kw, q) for kw in _EXPLICIT_CLOSER_RECOMMENDATION):
|
| 129 |
+
return "recommendation"
|
| 130 |
+
|
| 131 |
# Greeting / advice-seeking openers → fact-find flow
|
| 132 |
if any(_phrase_present(kw, q) for kw in FACT_FIND_TRIGGERS) and len(q.split()) < 25:
|
| 133 |
return "fact_find"
|
|
|
|
| 704 |
# Chroma at POST /api/profile time) gets boosted to the top of the
|
| 705 |
# context. Without session_id this path is dormant and the brain never
|
| 706 |
# sees the user's profile inline with policy text.
|
| 707 |
+
#
|
| 708 |
+
# KI-105 (2026-05-15) — closer-intent retrieval widening. When the user
|
| 709 |
+
# explicitly asks for a ranked shortlist ("show me the top 3 policies",
|
| 710 |
+
# "what would you recommend", "compare top 3"), top_k=5 was sometimes
|
| 711 |
+
# returning 4 chunks from the SAME policy plus 1 profile chunk — leaving
|
| 712 |
+
# the brain only one named insurer to rank. Widen to top_k=12 on closer
|
| 713 |
+
# intents so the candidate set has enough cross-insurer diversity for the
|
| 714 |
+
# closer-mode addendum (which asks for 3 distinct named policies) to
|
| 715 |
+
# actually fire instead of refusing.
|
| 716 |
+
effective_top_k = top_k
|
| 717 |
+
if intent in ("recommendation", "comparison") and not policy_filter_ids:
|
| 718 |
+
effective_top_k = max(top_k, 12)
|
| 719 |
+
|
| 720 |
chunks: list[RetrievedChunk] = await retrieve(
|
| 721 |
query=user_text,
|
| 722 |
+
top_k=effective_top_k,
|
| 723 |
policy_ids=policy_filter_ids,
|
| 724 |
session_id=session_id,
|
| 725 |
)
|
|
|
|
| 729 |
pick = pick_brain(intent, language)
|
| 730 |
|
| 731 |
# 4. Generate
|
| 732 |
+
#
|
| 733 |
+
# KI-105 — pass intent through so build_messages can append the
|
| 734 |
+
# CLOSER MODE addendum on recommendation/comparison turns. The
|
| 735 |
+
# addendum forces a ranked top-3 output instead of the default
|
| 736 |
+
# conservative 60-word advisor reply that re-asked slots or refused
|
| 737 |
+
# on weak grounding (live 15-persona smoke caught this gap).
|
| 738 |
+
#
|
| 739 |
+
# Two sources of "user_profile" exist: the dict passed in by callers
|
| 740 |
+
# AND the session.profile we built up via fact-find. The dict is the
|
| 741 |
+
# historical wire format; when it's None on a closer turn but the
|
| 742 |
+
# session has captured facts, synthesize a profile dict so the
|
| 743 |
+
# closer-mode contract still fires (rank-grounding requires SOME
|
| 744 |
+
# context to anchor on).
|
| 745 |
+
profile_for_prompt = user_profile
|
| 746 |
+
if (
|
| 747 |
+
intent in ("recommendation", "comparison")
|
| 748 |
+
and not profile_for_prompt
|
| 749 |
+
and getattr(session, "profile", None) is not None
|
| 750 |
+
):
|
| 751 |
+
sp = session.profile
|
| 752 |
+
candidate: dict = {}
|
| 753 |
+
for fld in ("age", "dependents", "income_band", "existing_cover_inr",
|
| 754 |
+
"primary_goal", "location_tier", "parents_to_insure",
|
| 755 |
+
"parents_age_max", "parents_has_ped", "budget_band",
|
| 756 |
+
"health_conditions"):
|
| 757 |
+
v = getattr(sp, fld, None)
|
| 758 |
+
if v not in (None, "", []):
|
| 759 |
+
candidate[fld] = v
|
| 760 |
+
if candidate:
|
| 761 |
+
profile_for_prompt = candidate
|
| 762 |
+
|
| 763 |
messages_dict = build_messages(
|
| 764 |
user_query=user_text,
|
| 765 |
retrieved_context=context_str,
|
| 766 |
chat_history=chat_history,
|
| 767 |
+
user_profile=profile_for_prompt,
|
| 768 |
view_context=view_context,
|
| 769 |
+
intent=intent,
|
| 770 |
)
|
| 771 |
messages = [ChatMessage(role=m["role"], content=m["content"]) for m in messages_dict]
|
| 772 |
|
|
|
|
| 781 |
# is in synchronous probe_all() refresh. 30s is generous enough for
|
| 782 |
# normal MoE responses but short enough to fall through to the chain's
|
| 783 |
# error path before the user gives up.
|
| 784 |
+
# KI-105 — closer turns produce a 3-policy ranked shortlist
|
| 785 |
+
# (~150-220 words + per-line citations + acknowledger + caveat).
|
| 786 |
+
# 1500 tokens is comfortably enough; we keep it but bump
|
| 787 |
+
# temperature slightly so the rationale prose doesn't read as a
|
| 788 |
+
# boilerplate copy of the previous closer turn. Faithfulness gate
|
| 789 |
+
# still validates every cited claim against the retrieved chunks.
|
| 790 |
+
_closer_turn = intent in ("recommendation", "comparison")
|
| 791 |
llm_result = await asyncio.wait_for(
|
| 792 |
+
pick.provider.chat(
|
| 793 |
+
messages=messages,
|
| 794 |
+
temperature=0.3 if _closer_turn else 0.2,
|
| 795 |
+
max_tokens=1500,
|
| 796 |
+
),
|
| 797 |
timeout=30.0,
|
| 798 |
)
|
| 799 |
|
|
@@ -68,14 +68,47 @@ FORMAT FOR YOUR REPLIES
|
|
| 68 |
"""
|
| 69 |
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def build_messages(
|
| 72 |
user_query: str,
|
| 73 |
retrieved_context: str,
|
| 74 |
chat_history: list[dict] | None = None,
|
| 75 |
user_profile: dict | None = None,
|
| 76 |
view_context: dict | None = None,
|
|
|
|
| 77 |
) -> list[dict]:
|
| 78 |
-
"""Assemble the message list for the LLM call.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
system = ADVISOR_SYSTEM_PROMPT_V1
|
| 80 |
if user_profile:
|
| 81 |
profile_summary = "\nUSER PROFILE (for personalization, do not echo verbatim):\n" + "\n".join(
|
|
@@ -83,6 +116,15 @@ def build_messages(
|
|
| 83 |
)
|
| 84 |
system = system + profile_summary
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
if view_context:
|
| 87 |
bits: list[str] = []
|
| 88 |
av = view_context.get("active_view")
|
|
|
|
| 68 |
"""
|
| 69 |
|
| 70 |
|
| 71 |
+
RECOMMENDATION_CLOSER_ADDENDUM = """
|
| 72 |
+
|
| 73 |
+
CLOSER MODE (intent = recommendation OR comparison, profile is mostly filled).
|
| 74 |
+
The user has explicitly asked for a ranked shortlist or a side-by-side comparison.
|
| 75 |
+
Your job on THIS turn is to deliver one — NOT to ask more fact-find questions.
|
| 76 |
+
|
| 77 |
+
OUTPUT CONTRACT (override the 60-word default just for this turn — go to ~150-220 words):
|
| 78 |
+
1. Open with one sentence acknowledging the user's profile drivers (age, dependents, budget, conditions). Example: "Based on your profile — 34, self+spouse, ₹10-25L income, mild hypertension — here are the three best-fit policies."
|
| 79 |
+
2. Produce EXACTLY 3 ranked policies, in priority order. For each:
|
| 80 |
+
- LINE 1: "<rank>. <Insurer> — <Policy Name>" (use insurer + policy_name straight from the cited chunk header)
|
| 81 |
+
- LINE 2: 2-3 sentences of rationale grounded in the retrieved clauses, citing the specific feature that makes it a fit (room rent, copay, PED waiting, network size, NCB). End each cited fact with the [Source: ...] citation as usual.
|
| 82 |
+
3. Close with ONE caveat sentence + the standard "confirm with the insurer directly before finalizing" line.
|
| 83 |
+
|
| 84 |
+
ABSOLUTE RULES FOR CLOSER MODE:
|
| 85 |
+
- DO NOT ask any new fact-find question on a closer turn. The user explicitly asked to be sold to.
|
| 86 |
+
- DO NOT refuse with "I'd rather not speculate" unless ZERO retrieved chunks are relevant — if the retrieved context has 3+ named policies, you MUST rank them.
|
| 87 |
+
- DO NOT echo profile back as a question ("you're 34, right?"). State it as fact and move on.
|
| 88 |
+
- DO NOT recommend any policy whose retrieved chunk implies a demographic mismatch (senior-citizen-only when user is <60; critical-illness-only when user didn't ask about it).
|
| 89 |
+
- If FEWER than 3 distinct named policies appear in the retrieved context, rank whatever you have (1 or 2) and explicitly say "I only have grounded evidence for these N policies right now."
|
| 90 |
+
- Citation grammar is unchanged — every factual claim still ends with [Source: <Policy Name> (<insurer>), p.X].
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
|
| 94 |
def build_messages(
|
| 95 |
user_query: str,
|
| 96 |
retrieved_context: str,
|
| 97 |
chat_history: list[dict] | None = None,
|
| 98 |
user_profile: dict | None = None,
|
| 99 |
view_context: dict | None = None,
|
| 100 |
+
intent: str | None = None,
|
| 101 |
) -> list[dict]:
|
| 102 |
+
"""Assemble the message list for the LLM call.
|
| 103 |
+
|
| 104 |
+
KI-105 (2026-05-15) — when intent is 'recommendation' or 'comparison'
|
| 105 |
+
AND a user_profile is provided (i.e., fact-find has produced enough
|
| 106 |
+
context to rank), append the CLOSER MODE addendum. This tells the
|
| 107 |
+
brain to produce a ranked top-3 shortlist instead of falling back to
|
| 108 |
+
the default conservative 60-word advisor reply that re-asks slots or
|
| 109 |
+
refuses on weak grounding. The addendum is intent-gated so it never
|
| 110 |
+
leaks into pure QA or fact-find turns.
|
| 111 |
+
"""
|
| 112 |
system = ADVISOR_SYSTEM_PROMPT_V1
|
| 113 |
if user_profile:
|
| 114 |
profile_summary = "\nUSER PROFILE (for personalization, do not echo verbatim):\n" + "\n".join(
|
|
|
|
| 116 |
)
|
| 117 |
system = system + profile_summary
|
| 118 |
|
| 119 |
+
# KI-105 — closer mode: ranked-shortlist contract appended only when
|
| 120 |
+
# the orchestrator says this turn is a recommendation/comparison.
|
| 121 |
+
# Gate on profile presence too — empty-profile closer turns are
|
| 122 |
+
# already redirected to fact-find by `should_route_to_fact_find`,
|
| 123 |
+
# but defence in depth: if a future caller bypasses that guard, the
|
| 124 |
+
# addendum still won't fire without a profile to ground on.
|
| 125 |
+
if intent in ("recommendation", "comparison") and user_profile:
|
| 126 |
+
system = system + RECOMMENDATION_CLOSER_ADDENDUM
|
| 127 |
+
|
| 128 |
if view_context:
|
| 129 |
bits: list[str] = []
|
| 130 |
av = view_context.get("active_view")
|
|
@@ -67,6 +67,110 @@ class TestIntentClassification(unittest.TestCase):
|
|
| 67 |
# recommending anyway. Pinned here so we notice if the order changes.
|
| 68 |
self.assertEqual(classify_intent("Which one should I get?"), "fact_find")
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
class TestFactFindRouting(unittest.TestCase):
|
| 72 |
"""KI-018 regression: empty-profile sessions must NOT trap qa in fact-find."""
|
|
|
|
| 67 |
# recommending anyway. Pinned here so we notice if the order changes.
|
| 68 |
self.assertEqual(classify_intent("Which one should I get?"), "fact_find")
|
| 69 |
|
| 70 |
+
def test_ki105_closer_phrases_classify_as_recommendation(self) -> None:
|
| 71 |
+
"""KI-105 (2026-05-15) — explicit-closer regression.
|
| 72 |
+
|
| 73 |
+
Live 15-persona smoke caught these phrases being routed to qa or
|
| 74 |
+
fact_find instead of recommendation, so the heavy brain was never
|
| 75 |
+
called + no ranked shortlist was ever produced. The explicit-closer
|
| 76 |
+
override (checked BEFORE FACT_FIND_TRIGGERS) lifts them into the
|
| 77 |
+
recommendation lane unambiguously.
|
| 78 |
+
"""
|
| 79 |
+
recommendation_closers = [
|
| 80 |
+
"show me the top 3 policies",
|
| 81 |
+
"show me policies",
|
| 82 |
+
"give me the top 3",
|
| 83 |
+
"what would you recommend",
|
| 84 |
+
"your top picks",
|
| 85 |
+
"pitch me the top 3",
|
| 86 |
+
]
|
| 87 |
+
for q in recommendation_closers:
|
| 88 |
+
with self.subTest(question=q):
|
| 89 |
+
self.assertEqual(
|
| 90 |
+
classify_intent(q),
|
| 91 |
+
"recommendation",
|
| 92 |
+
f"REGRESSION: {q!r} should classify as recommendation. "
|
| 93 |
+
f"See KI-105 — without this the bot doesn't produce a "
|
| 94 |
+
f"ranked shortlist on the closer turn.",
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
def test_ki105_closer_phrases_classify_as_comparison(self) -> None:
|
| 98 |
+
"""KI-105 — comparison-shaped closer phrases.
|
| 99 |
+
|
| 100 |
+
'compare HDFC Ergo and Niva Bupa' and 'rank top 3' both explicitly
|
| 101 |
+
ask for a side-by-side / ranked output across the candidate set.
|
| 102 |
+
They must NOT fall through to qa or fact_find.
|
| 103 |
+
"""
|
| 104 |
+
comparison_closers = [
|
| 105 |
+
"compare HDFC Ergo and Niva Bupa",
|
| 106 |
+
"compare top 3",
|
| 107 |
+
"compare the top 3 policies",
|
| 108 |
+
"rank top 3 for me",
|
| 109 |
+
"side-by-side these policies",
|
| 110 |
+
]
|
| 111 |
+
for q in comparison_closers:
|
| 112 |
+
with self.subTest(question=q):
|
| 113 |
+
self.assertEqual(
|
| 114 |
+
classify_intent(q),
|
| 115 |
+
"comparison",
|
| 116 |
+
f"REGRESSION: {q!r} should classify as comparison.",
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
class TestKI105CloserPersona(unittest.TestCase):
|
| 121 |
+
"""KI-105 (2026-05-15) — the persona must append the CLOSER MODE
|
| 122 |
+
addendum ONLY on recommendation/comparison turns AND only when a
|
| 123 |
+
profile is present. Otherwise the brain reverts to its default
|
| 124 |
+
60-word conservative reply that re-asks slots or refuses."""
|
| 125 |
+
|
| 126 |
+
def test_closer_addendum_present_on_recommendation_turn(self) -> None:
|
| 127 |
+
from backend.persona import build_messages
|
| 128 |
+
msgs = build_messages(
|
| 129 |
+
user_query="show me the top 3 policies",
|
| 130 |
+
retrieved_context="[Source: Care Supreme (care-health), p.5]\nSample chunk text.",
|
| 131 |
+
user_profile={"age": 34, "dependents": "self+spouse", "budget_band": "30k_60k"},
|
| 132 |
+
intent="recommendation",
|
| 133 |
+
)
|
| 134 |
+
system = msgs[0]["content"]
|
| 135 |
+
self.assertIn("CLOSER MODE", system)
|
| 136 |
+
self.assertIn("ranked shortlist", system.lower() + "") # phrase appears in addendum
|
| 137 |
+
|
| 138 |
+
def test_closer_addendum_present_on_comparison_turn(self) -> None:
|
| 139 |
+
from backend.persona import build_messages
|
| 140 |
+
msgs = build_messages(
|
| 141 |
+
user_query="compare HDFC Ergo and Niva Bupa",
|
| 142 |
+
retrieved_context="[Source: Optima Secure (hdfc-ergo), p.5]\nSample.",
|
| 143 |
+
user_profile={"age": 34, "dependents": "self+spouse"},
|
| 144 |
+
intent="comparison",
|
| 145 |
+
)
|
| 146 |
+
self.assertIn("CLOSER MODE", msgs[0]["content"])
|
| 147 |
+
|
| 148 |
+
def test_closer_addendum_absent_on_qa_turn(self) -> None:
|
| 149 |
+
"""A 'what is the PED waiting period?' turn must NOT get the
|
| 150 |
+
ranked-shortlist contract appended — that would force the brain
|
| 151 |
+
to invent 3 policies on a single-policy factual lookup."""
|
| 152 |
+
from backend.persona import build_messages
|
| 153 |
+
msgs = build_messages(
|
| 154 |
+
user_query="What is the PED waiting period under Activ Assure?",
|
| 155 |
+
retrieved_context="[Source: Activ Assure (aditya-birla), p.10]\nClause text.",
|
| 156 |
+
user_profile={"age": 34},
|
| 157 |
+
intent="qa",
|
| 158 |
+
)
|
| 159 |
+
self.assertNotIn("CLOSER MODE", msgs[0]["content"])
|
| 160 |
+
|
| 161 |
+
def test_closer_addendum_absent_on_empty_profile(self) -> None:
|
| 162 |
+
"""KI-013 belt-and-braces: even if a closer intent slips
|
| 163 |
+
through with no profile, the addendum must NOT fire — otherwise
|
| 164 |
+
the brain might pitch a senior-only policy to an anonymous user."""
|
| 165 |
+
from backend.persona import build_messages
|
| 166 |
+
msgs = build_messages(
|
| 167 |
+
user_query="show me the top 3 policies",
|
| 168 |
+
retrieved_context="[Source: Care Senior (care-health), p.5]\nSample.",
|
| 169 |
+
user_profile=None,
|
| 170 |
+
intent="recommendation",
|
| 171 |
+
)
|
| 172 |
+
self.assertNotIn("CLOSER MODE", msgs[0]["content"])
|
| 173 |
+
|
| 174 |
|
| 175 |
class TestFactFindRouting(unittest.TestCase):
|
| 176 |
"""KI-018 regression: empty-profile sessions must NOT trap qa in fact-find."""
|