Spaces:
Sleeping
fix(profile + ux): KI-068 humanize readback / strip markdown / async log; KI-069 fix name-capture false-positive
Browse filesKI-068 β Humanize readback summary + strip markdown leak (needs_finder.py, orchestrator.py)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
User screenshot showed the readback rendering raw enum strings:
"primary goal: first_buy; metro; budget 30k_60k. **If anything's
wrong, just tell me** (e.g., ...)"
- Added label tables (_PRIMARY_GOAL_LABELS, _LOCATION_TIER_LABELS,
_BUDGET_BAND_LABELS, _INCOME_BAND_LABELS, _DEPENDENTS_LABELS) and
use them in `readback_summary`. New output for the same profile:
"29 years old; covering just yourself; income βΉ25L+; no existing
cover; goal: first health policy; metro city; budget
βΉ30,000β60,000/year"
- Stripped the `**...**` markdown wrapper around "If anything's
wrong, just tell me" β was leaking as literal asterisks in the
chat panel + being read by TTS as "asterisk asterisk".
Also made KI-063 shown-policy logging fire-and-forget via
`asyncio.create_task(asyncio.to_thread(...))` so N disk writes per
recommendation citation don't block the reply path.
KI-069 β Stop "this is correct" being misread as a self-introduction
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
User completed fact-find, then said "No, I I think this is correct. Uh,
first, uh, show me which are the few policies..." Bot replied:
"Sorry, I didn't catch that. Let me ask again β First β what should I
call you?" β restarting the session.
Root cause: KI-059's `_SELF_INTRO_RE` matched "this is correct" and
captured "correct" as the user's name. Validation failed β ambiguous β
re-ask name β session loop.
Fix:
- `_NON_NAME_TOKENS` blocklist (50+ entries): confirmatory adjectives
(correct/right/fine/good/ok/...), negations (wrong/no/false/...),
common nouns/adjectives mistaken for names (first/last/my/the/...).
- Tighter regex match acceptance: the captured first word must START
WITH UPPERCASE in the original text. Real names are capitalised in
chat input and STT output; adjectives aren't. Names like "Sam"
still match because they're capitalised; "correct" fails both gates.
Verified 11/11 cases β every false-positive from the bug report stops,
every legitimate "Hi this is Rohit" / "My name is Anjali" / "I'm Sarah
Connor" / "Hello, my name is Priya Sharma" still passes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- backend/needs_finder.py +50 -6
- backend/orchestrator.py +85 -24
|
@@ -266,15 +266,59 @@ def infer_dependents_from_text(text: str) -> Optional[str]:
|
|
| 266 |
return None
|
| 267 |
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
def readback_summary(profile: Profile) -> str:
|
| 270 |
-
"""One-paragraph human-readable summary of the gathered profile.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
bits = []
|
| 272 |
if profile.age:
|
| 273 |
bits.append(f"{profile.age} years old")
|
| 274 |
if profile.dependents:
|
| 275 |
-
bits.append(f"covering {profile.dependents}")
|
| 276 |
if profile.income_band:
|
| 277 |
-
bits.append(f"income {profile.income_band}")
|
| 278 |
ec = profile.existing_cover_inr
|
| 279 |
if isinstance(ec, str):
|
| 280 |
try:
|
|
@@ -288,9 +332,9 @@ def readback_summary(profile: Profile) -> str:
|
|
| 288 |
else "no existing cover"
|
| 289 |
)
|
| 290 |
if profile.primary_goal:
|
| 291 |
-
bits.append(f"
|
| 292 |
if profile.location_tier:
|
| 293 |
-
bits.append(profile.location_tier)
|
| 294 |
if profile.parents_age_max:
|
| 295 |
bits.append(f"parents up to age {profile.parents_age_max}")
|
| 296 |
if profile.health_conditions:
|
|
@@ -304,5 +348,5 @@ def readback_summary(profile: Profile) -> str:
|
|
| 304 |
if hc:
|
| 305 |
bits.append(f"conditions: {', '.join(str(c) for c in hc)}")
|
| 306 |
if profile.budget_band:
|
| 307 |
-
bits.append(f"budget {profile.budget_band}")
|
| 308 |
return "; ".join(bits) if bits else "(no profile yet)"
|
|
|
|
| 266 |
return None
|
| 267 |
|
| 268 |
|
| 269 |
+
# KI-068 (2026-05-15) β enum β human-readable labels for the readback
|
| 270 |
+
# summary. Previously the bot read back "primary goal: first_buy; metro;
|
| 271 |
+
# budget 30k_60k" verbatim, which sounds and reads like a schema dump.
|
| 272 |
+
_PRIMARY_GOAL_LABELS = {
|
| 273 |
+
"first_buy": "first health policy",
|
| 274 |
+
"upgrade": "upgrading existing cover",
|
| 275 |
+
"compare_specific": "comparing specific policies",
|
| 276 |
+
"tax_planning": "tax planning",
|
| 277 |
+
}
|
| 278 |
+
_LOCATION_TIER_LABELS = {
|
| 279 |
+
"metro": "metro city",
|
| 280 |
+
"tier1": "tier-1 city",
|
| 281 |
+
"tier2": "tier-2 city",
|
| 282 |
+
"tier3": "tier-3 city",
|
| 283 |
+
}
|
| 284 |
+
_BUDGET_BAND_LABELS = {
|
| 285 |
+
"under_15k": "under βΉ15,000/year",
|
| 286 |
+
"15k_30k": "βΉ15,000β30,000/year",
|
| 287 |
+
"30k_60k": "βΉ30,000β60,000/year",
|
| 288 |
+
"60k+": "βΉ60,000+/year",
|
| 289 |
+
}
|
| 290 |
+
_INCOME_BAND_LABELS = {
|
| 291 |
+
"under_5L": "under βΉ5L",
|
| 292 |
+
"5_10L": "βΉ5β10L",
|
| 293 |
+
"10_25L": "βΉ10β25L",
|
| 294 |
+
"25L+": "βΉ25L+",
|
| 295 |
+
}
|
| 296 |
+
_DEPENDENTS_LABELS = {
|
| 297 |
+
"self": "just yourself",
|
| 298 |
+
"self+spouse": "you and your spouse",
|
| 299 |
+
"self+spouse+kids": "you, your spouse, and kids",
|
| 300 |
+
"self+kids": "you and your kids",
|
| 301 |
+
"self+parents": "you and your parents",
|
| 302 |
+
"self+spouse+parents": "you, your spouse, and parents",
|
| 303 |
+
"self+spouse+kids+parents": "you, your spouse, kids, and parents",
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
|
| 307 |
def readback_summary(profile: Profile) -> str:
|
| 308 |
+
"""One-paragraph human-readable summary of the gathered profile.
|
| 309 |
+
|
| 310 |
+
KI-068 (2026-05-15) β converts schema-level enum values
|
| 311 |
+
("first_buy", "30k_60k", "metro") into spoken labels
|
| 312 |
+
("first health policy", "βΉ30,000β60,000/year", "metro city") so the
|
| 313 |
+
readback reads like a sentence, not a JSON dump.
|
| 314 |
+
"""
|
| 315 |
bits = []
|
| 316 |
if profile.age:
|
| 317 |
bits.append(f"{profile.age} years old")
|
| 318 |
if profile.dependents:
|
| 319 |
+
bits.append(f"covering {_DEPENDENTS_LABELS.get(profile.dependents, profile.dependents)}")
|
| 320 |
if profile.income_band:
|
| 321 |
+
bits.append(f"income {_INCOME_BAND_LABELS.get(profile.income_band, profile.income_band)}")
|
| 322 |
ec = profile.existing_cover_inr
|
| 323 |
if isinstance(ec, str):
|
| 324 |
try:
|
|
|
|
| 332 |
else "no existing cover"
|
| 333 |
)
|
| 334 |
if profile.primary_goal:
|
| 335 |
+
bits.append(f"goal: {_PRIMARY_GOAL_LABELS.get(profile.primary_goal, profile.primary_goal)}")
|
| 336 |
if profile.location_tier:
|
| 337 |
+
bits.append(_LOCATION_TIER_LABELS.get(profile.location_tier, profile.location_tier))
|
| 338 |
if profile.parents_age_max:
|
| 339 |
bits.append(f"parents up to age {profile.parents_age_max}")
|
| 340 |
if profile.health_conditions:
|
|
|
|
| 348 |
if hc:
|
| 349 |
bits.append(f"conditions: {', '.join(str(c) for c in hc)}")
|
| 350 |
if profile.budget_band:
|
| 351 |
+
bits.append(f"budget {_BUDGET_BAND_LABELS.get(profile.budget_band, profile.budget_band)}")
|
| 352 |
return "; ".join(bits) if bits else "(no profile yet)"
|
|
@@ -252,18 +252,57 @@ _GREETING_LEAD = re.compile(
|
|
| 252 |
)
|
| 253 |
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
def _contains_self_introduction(text: str) -> bool:
|
| 256 |
"""True if the message volunteers a name via "I'm X" / "this is X" /
|
| 257 |
"my name is X" / "call me X" β with or without a greeting prefix.
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
"""
|
| 263 |
if not text:
|
| 264 |
return False
|
| 265 |
-
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
|
| 269 |
# KI-061 (2026-05-15) β human-readable summaries for the welcome-back
|
|
@@ -884,10 +923,13 @@ async def handle_turn(
|
|
| 884 |
readback_opener = "Here's what I've understood:"
|
| 885 |
else:
|
| 886 |
readback_opener = f"{readback_opener} Here's what I've understood:"
|
|
|
|
|
|
|
|
|
|
| 887 |
reply = (
|
| 888 |
f"{readback_opener} {summary}. "
|
| 889 |
-
f"
|
| 890 |
-
f"\"I want to cover my parents too\"
|
| 891 |
f"Otherwise β want me to suggest 2-3 policies that fit your profile, "
|
| 892 |
f"or do you have a specific policy in mind to dig into?"
|
| 893 |
)
|
|
@@ -1126,6 +1168,10 @@ async def handle_turn(
|
|
| 1126 |
# comparison) AND only when faithfulness passed (we don't log cites that
|
| 1127 |
# the safety gates rejected). Anonymous users (no profile.name) get no
|
| 1128 |
# log β there's no key to persist against.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1129 |
try:
|
| 1130 |
if (
|
| 1131 |
intent in ("recommendation", "comparison")
|
|
@@ -1134,28 +1180,43 @@ async def handle_turn(
|
|
| 1134 |
and session.profile.name
|
| 1135 |
and citations
|
| 1136 |
):
|
|
|
|
| 1137 |
from backend.profile_store import record_policy_event
|
| 1138 |
-
|
| 1139 |
-
|
| 1140 |
-
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
| 1147 |
-
|
| 1148 |
-
|
| 1149 |
-
|
| 1150 |
-
|
| 1151 |
-
|
| 1152 |
-
|
| 1153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1154 |
except Exception as e:
|
| 1155 |
# Never let logging failures break the chat reply.
|
| 1156 |
import logging
|
| 1157 |
logging.warning(
|
| 1158 |
-
"KI-063 shown_policies
|
| 1159 |
session_id, type(e).__name__, str(e)[:200],
|
| 1160 |
)
|
| 1161 |
|
|
|
|
| 252 |
)
|
| 253 |
|
| 254 |
|
| 255 |
+
# KI-069 (2026-05-15) β words that follow "this is" / "I'm" / "I am" but
|
| 256 |
+
# are NOT names. Without this guard KI-059 mis-fired on natural feedback
|
| 257 |
+
# like "this is correct" / "I'm good" / "I am sure" and routed users back
|
| 258 |
+
# to the name slot. Lowercase comparison.
|
| 259 |
+
_NON_NAME_TOKENS: frozenset[str] = frozenset({
|
| 260 |
+
# Confirmatory adjectives
|
| 261 |
+
"correct", "right", "fine", "good", "okay", "ok", "alright", "great",
|
| 262 |
+
"true", "perfect", "accurate", "exactly", "yes", "yeah", "yep", "sure",
|
| 263 |
+
# Negations / wrong-flag
|
| 264 |
+
"wrong", "incorrect", "no", "false", "off", "bad", "mistaken",
|
| 265 |
+
# Common adjectives / nouns mistaken for names
|
| 266 |
+
"first", "second", "third", "last", "next", "another", "the", "a", "an",
|
| 267 |
+
"my", "your", "his", "her", "their", "our", "this", "that", "these",
|
| 268 |
+
"happy", "sad", "tired", "busy", "ready", "done", "new", "old",
|
| 269 |
+
# Common stop-words that sneak through capitalisation rules
|
| 270 |
+
"looking", "buying", "shopping", "interested", "trying", "thinking",
|
| 271 |
+
"here", "there",
|
| 272 |
+
})
|
| 273 |
+
|
| 274 |
+
|
| 275 |
def _contains_self_introduction(text: str) -> bool:
|
| 276 |
"""True if the message volunteers a name via "I'm X" / "this is X" /
|
| 277 |
"my name is X" / "call me X" β with or without a greeting prefix.
|
| 278 |
|
| 279 |
+
KI-059 (2026-05-15) β original detector.
|
| 280 |
+
KI-069 (2026-05-15) β tightened to avoid false positives:
|
| 281 |
+
1. The captured token must NOT be in the non-name blocklist
|
| 282 |
+
(catches "this is correct" / "I'm good" / "first, show me ...").
|
| 283 |
+
2. The captured name must start with an UPPERCASE letter in the
|
| 284 |
+
ORIGINAL text. Real names get capitalized in chat input even
|
| 285 |
+
after voice transcription; adjectives don't.
|
| 286 |
"""
|
| 287 |
if not text:
|
| 288 |
return False
|
| 289 |
+
stripped_original = _GREETING_LEAD.sub("", text.strip())
|
| 290 |
+
m = _SELF_INTRO_RE.search(stripped_original)
|
| 291 |
+
if not m:
|
| 292 |
+
return False
|
| 293 |
+
captured = m.group(1).strip()
|
| 294 |
+
if not captured:
|
| 295 |
+
return False
|
| 296 |
+
# First word of the captured span is the strongest "is this a name?" signal.
|
| 297 |
+
first_word = captured.split()[0]
|
| 298 |
+
if first_word.lower() in _NON_NAME_TOKENS:
|
| 299 |
+
return False
|
| 300 |
+
# Names are typically capitalised in user input (chat AND voice STT).
|
| 301 |
+
# If the first character isn't uppercase, the captured span is almost
|
| 302 |
+
# certainly an adjective/adverb, not a name.
|
| 303 |
+
if not first_word[0].isupper():
|
| 304 |
+
return False
|
| 305 |
+
return True
|
| 306 |
|
| 307 |
|
| 308 |
# KI-061 (2026-05-15) β human-readable summaries for the welcome-back
|
|
|
|
| 923 |
readback_opener = "Here's what I've understood:"
|
| 924 |
else:
|
| 925 |
readback_opener = f"{readback_opener} Here's what I've understood:"
|
| 926 |
+
# KI-068 (2026-05-15) β dropped the `**bold**` wrapper which was
|
| 927 |
+
# leaking as literal asterisks in the chat panel + getting read by
|
| 928 |
+
# TTS as "asterisk asterisk".
|
| 929 |
reply = (
|
| 930 |
f"{readback_opener} {summary}. "
|
| 931 |
+
f"If anything's wrong, just tell me β e.g., \"actually I'm 31\", or "
|
| 932 |
+
f"\"I want to cover my parents too\". "
|
| 933 |
f"Otherwise β want me to suggest 2-3 policies that fit your profile, "
|
| 934 |
f"or do you have a specific policy in mind to dig into?"
|
| 935 |
)
|
|
|
|
| 1168 |
# comparison) AND only when faithfulness passed (we don't log cites that
|
| 1169 |
# the safety gates rejected). Anonymous users (no profile.name) get no
|
| 1170 |
# log β there's no key to persist against.
|
| 1171 |
+
#
|
| 1172 |
+
# KI-068 (2026-05-15) β fire-and-forget via asyncio.create_task so disk
|
| 1173 |
+
# writes don't block the reply path. The user shouldn't wait for N JSON
|
| 1174 |
+
# file writes (one per cited policy) before seeing the bot's recommend.
|
| 1175 |
try:
|
| 1176 |
if (
|
| 1177 |
intent in ("recommendation", "comparison")
|
|
|
|
| 1180 |
and session.profile.name
|
| 1181 |
and citations
|
| 1182 |
):
|
| 1183 |
+
import asyncio as _asyncio
|
| 1184 |
from backend.profile_store import record_policy_event
|
| 1185 |
+
|
| 1186 |
+
def _log_shown_policies() -> None:
|
| 1187 |
+
"""Run all record_policy_event writes off the reply path."""
|
| 1188 |
+
try:
|
| 1189 |
+
seen_slugs: set[str] = set()
|
| 1190 |
+
for cite in citations:
|
| 1191 |
+
slug = cite.get("policy_id") or cite.get("policy_slug")
|
| 1192 |
+
insurer = cite.get("insurer_slug") or cite.get("insurer")
|
| 1193 |
+
if not slug or not insurer or slug in seen_slugs:
|
| 1194 |
+
continue
|
| 1195 |
+
seen_slugs.add(slug)
|
| 1196 |
+
record_policy_event(
|
| 1197 |
+
persona_id_or_name=session.profile.name,
|
| 1198 |
+
profile=session.profile,
|
| 1199 |
+
event_type="shown",
|
| 1200 |
+
policy_slug=slug,
|
| 1201 |
+
insurer=insurer,
|
| 1202 |
+
session_id=session_id,
|
| 1203 |
+
reason="shown_in_recommendation",
|
| 1204 |
+
)
|
| 1205 |
+
except Exception as inner:
|
| 1206 |
+
import logging
|
| 1207 |
+
logging.warning(
|
| 1208 |
+
"KI-063 shown_policies log failed (session=%s): %s: %s",
|
| 1209 |
+
session_id, type(inner).__name__, str(inner)[:200],
|
| 1210 |
+
)
|
| 1211 |
+
|
| 1212 |
+
# asyncio.to_thread offloads the file writes to the default
|
| 1213 |
+
# threadpool; we don't await so the handler returns immediately.
|
| 1214 |
+
_asyncio.create_task(_asyncio.to_thread(_log_shown_policies))
|
| 1215 |
except Exception as e:
|
| 1216 |
# Never let logging failures break the chat reply.
|
| 1217 |
import logging
|
| 1218 |
logging.warning(
|
| 1219 |
+
"KI-063 shown_policies scheduling failed (session=%s): %s: %s",
|
| 1220 |
session_id, type(e).__name__, str(e)[:200],
|
| 1221 |
)
|
| 1222 |
|