fix(rerank): ground entity-miss demotion on head token, not full phrase

The entity-grounding demotion required the full multi-word primary
entity as a contiguous substring, so on-entity items missing a trailing
search descriptor were buried: a 323-pt HN thread "Stripe is friendly
to 'friendly fraud'" scored 0 on a "Stripe payments" query. New
_entity_grounded helper keys on the brand head token; items that never
name the brand still miss it and stay demoted. reddit_keyless
_slot_priority, which had re-implemented the old check while claiming
to mirror rerank's signal, now calls the shared helper so the two
paths cannot diverge.
This commit is contained in:
Trevin Chow
2026-06-09 16:24:16 -07:00
parent fd0e47d99f
commit 6a92f63a56
4 changed files with 71 additions and 22 deletions
+22
View File
@@ -221,6 +221,28 @@ class EntityGroundingTests(unittest.TestCase):
self.assertIn("entity-miss", off_topic.explanation or "")
self.assertEqual(on_topic.explanation, "fallback-local-score")
def test_fallback_grounds_on_head_token_not_full_phrase(self):
# Regression: a 323-pt HN thread titled "Stripe is friendly to
# 'friendly fraud'" was demoted to score 0 on a "Stripe payments"
# query because it lacked the trailing word "payments". The brand
# token alone must ground the item - trailing descriptors are search
# hints, not part of the entity.
brand_only = self._candidate(
"Stripe is friendly to 'friendly fraud'", "discussion of chargebacks and disputes"
)
rerank._apply_fallback_scores([brand_only], primary_entity="Stripe payments")
self.assertEqual("fallback-local-score", brand_only.explanation)
self.assertNotIn("entity-miss", brand_only.explanation or "")
def test_fallback_still_demotes_when_head_token_absent_on_multiword_topic(self):
# The fix must not neuter the demotion: an item that never names the
# brand head token stays demoted even on a multi-word topic.
off_topic = self._candidate(
"PayPal raises dispute fees again", "merchants react to the new pricing"
)
rerank._apply_fallback_scores([off_topic], primary_entity="Stripe payments")
self.assertIn("entity-miss", off_topic.explanation or "")
def test_fallback_match_is_case_insensitive(self):
on_topic = self._candidate("HERMES agent rocks", "some text")
rerank._apply_fallback_scores([on_topic], primary_entity="Hermes Agent")