Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix: entity-miss demotion is inert on topics without a distinctive entity (#888)

Terry Li27151e7fe1d6
diff --git a/changelog.d/887.fixed.md b/changelog.d/887.fixed.md
new file mode 100644
index 00000000..0d17b7e2
--- /dev/null
+++ b/changelog.d/887.fixed.md
@@ -0,0 +1 @@
+Entity-miss demotion no longer goes inert on long topics without a distinctive named entity. Intent modifiers are stripped only as trailing suffixes, and generic-headed topics use stronger trailing anchors while broad words such as "code", "review", and "work" cannot ground a result by themselves.
diff --git a/skills/last30days/scripts/lib/rerank.py b/skills/last30days/scripts/lib/rerank.py
index cf88f478..0c1d5327 100644
--- a/skills/last30days/scripts/lib/rerank.py
+++ b/skills/last30days/scripts/lib/rerank.py
@@ -195,21 +195,24 @@ INTERACTION_FLOOR = 35.0
# not a win.
FIRST_PARTY_FLOOR = 25.0
-# Intent modifiers to strip before extracting the primary entity so that,
-# for example, "Hermes Agent use cases" yields primary_entity="hermes agent"
-# rather than "hermes agent use cases". Kept in sync with
-# planner._INTENT_MODIFIER_PATTERNS.
-_INTENT_MODIFIER_RE = re.compile(
+# Only strip trailing intent modifiers. A word such as "review" may instead be
+# the subject of a longer topic ("AI code review bottleneck").
+_TRAILING_INTENT_MODIFIER_RE = re.compile(
+ r"(?:\s+(?:and|or|&|,)\s*)?"
r"\b("
r"use cases|use case|workflows|workflow|"
r"examples|example|tutorial|tutorials|"
r"review|reviews|comparison|applications|"
r"in practice|production use|production|"
r"how i use"
- r")\b",
+ r")\b[\s?.,:;!]*$",
re.IGNORECASE,
)
+_GENERIC_GROUNDING_MIN_TOKENS = 4
+_GROUNDING_ANCHOR_MIN_LENGTH = 6
+_GROUNDING_LOW_SIGNAL_TOKENS = relevance.LOW_SIGNAL_QUERY_TOKENS | {"still", "work"}
+
INTENT_SCORING_HINTS: dict[str, str] = {
"comparison": (
"Prefer items that directly compare, contrast, or benchmark the entities"
@@ -621,18 +624,33 @@ def _entity_grounded(haystack: str, primary_entity: str) -> bool:
items that omit the descriptor. Items that never name the brand at all still
miss the head token and stay demoted.
- Trade-off: a proper noun with a generic head ("New York Times" -> "new")
- under-demotes rather than over-demotes - the safe direction, since the
- observed harm was burying real high-engagement signal. Substring (not
- word-boundary) matching is likewise deliberate: it catches plurals and
- compounds ("stripes"), and vacuous matches from very short heads ("X",
- "Go") merely disable the penalty rather than burying good items.
+ Long natural-language topics with a generic head use stronger trailing
+ anchors. Short generic-headed topics remain a safe no-op so entities such as
+ "Go" are not falsely demoted.
"""
haystack = haystack.lower()
- tokens = primary_entity.lower().split()
+ tokens = re.findall(r"\w+", primary_entity.lower())
if not tokens:
return True
- return tokens[0] in haystack
+ head = tokens[0]
+ if len(head) > 3 and head not in relevance.LOW_SIGNAL_QUERY_TOKENS:
+ return head in haystack
+
+ # A long natural-language topic headed by "AI", "how", or another generic
+ # word needs a stronger anchor. Short entity-like topics keep the historical
+ # safe no-op rather than risking false demotion.
+ if len(tokens) < _GENERIC_GROUNDING_MIN_TOKENS:
+ return True
+ anchors = [
+ token
+ for token in tokens[1:]
+ if len(token) >= _GROUNDING_ANCHOR_MIN_LENGTH
+ and token not in relevance.STOPWORDS
+ and token not in _GROUNDING_LOW_SIGNAL_TOKENS
+ ]
+ if not anchors:
+ return True
+ return any(re.search(rf"\b{re.escape(token)}", haystack) for token in anchors)
def _fallback_tuple(
@@ -689,8 +707,12 @@ def _primary_entity(topic: str) -> str:
string for topics that are all intent modifier with no entity, so
callers can skip the grounding check.
"""
- stripped = _INTENT_MODIFIER_RE.sub(" ", topic)
- # Also collapse multiple spaces and strip punctuation.
+ stripped = topic
+ while True:
+ shortened = _TRAILING_INTENT_MODIFIER_RE.sub("", stripped, count=1)
+ if shortened == stripped:
+ break
+ stripped = shortened
stripped = re.sub(r"\s+", " ", stripped).strip(" \t\r\n?.,:;!")
return stripped
diff --git a/tests/test_rerank_generic_topic_grounding.py b/tests/test_rerank_generic_topic_grounding.py
new file mode 100644
index 00000000..3defd6a8
--- /dev/null
+++ b/tests/test_rerank_generic_topic_grounding.py
@@ -0,0 +1,62 @@
+"""Regression tests for entity grounding on generic, entity-less topics."""
+
+import pytest
+
+from lib import rerank
+
+GENERIC_TOPIC = "AI code review bottleneck: can reviewers still judge AI-produced work"
+
+
+def test_primary_entity_keeps_review_when_review_is_the_subject():
+ entity = rerank._primary_entity(GENERIC_TOPIC)
+ assert "code review" in entity.lower(), (
+ f"expected the subject phrase to survive intent-modifier stripping, got {entity!r}"
+ )
+
+
+def test_ubiquitous_head_token_does_not_ground_off_topic_items():
+ entity = rerank._primary_entity(GENERIC_TOPIC)
+ off_topic = (
+ "INSTEAD OF WATCHING NETFLIX TONIGHT Spend 1 hour with this Claude AI FULL "
+ "COURSE that teaches you how to BUILD and AUTOMATE anything"
+ )
+ assert not rerank._entity_grounded(off_topic, entity), (
+ "a course advertisement sharing only the token 'AI' should not count as grounded"
+ )
+
+
+def test_broad_token_buried_inside_another_word_does_not_ground():
+ entity = rerank._primary_entity(GENERIC_TOPIC)
+ unrelated = "my weekend project: a barcode scanner written in rust"
+ assert not rerank._entity_grounded(unrelated, entity), (
+ "'code' inside 'barcode' should not ground an unrelated candidate"
+ )
+
+
+ "unrelated",
+ [
+ "This code works",
+ "The review was helpful",
+ "Work is changing",
+ ],
+)
+def test_broad_trailing_token_alone_does_not_ground(unrelated):
+ entity = rerank._primary_entity(GENERIC_TOPIC)
+ assert not rerank._entity_grounded(unrelated, entity)
+
+
+def test_inflected_trailing_token_still_grounds():
+ entity = rerank._primary_entity(GENERIC_TOPIC)
+ on_topic = "our reviewers cannot keep up with generated diffs"
+ assert rerank._entity_grounded(on_topic, entity)
+
+
+def test_on_topic_item_is_grounded_control():
+ entity = rerank._primary_entity(GENERIC_TOPIC)
+ on_topic = "How to keep QA from being a giant bottleneck with AI coding"
+ assert rerank._entity_grounded(on_topic, entity)
+
+
+def test_short_generic_head_keeps_safe_no_op():
+ assert rerank._entity_grounded("Golang concurrency patterns", "Go programming")