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: honor source selection in supplemental X searches (#1210)

Ilia Alshanetsky262a0a625242
diff --git a/changelog.d/+cr-005-supplemental-source-selection.fixed.md b/changelog.d/+cr-005-supplemental-source-selection.fixed.md
new file mode 100644
index 00000000..88a7ef78
--- /dev/null
+++ b/changelog.d/+cr-005-supplemental-source-selection.fixed.md
@@ -0,0 +1 @@
+Honor the research plan's source selection for supplemental X handle searches and host-fetched X posts, preventing excluded X sources from returning evidence or making extra backend requests.
diff --git a/skills/last30days/scripts/lib/pipeline.py b/skills/last30days/scripts/lib/pipeline.py
index 722ff777..f8bded86 100644
--- a/skills/last30days/scripts/lib/pipeline.py
+++ b/skills/last30days/scripts/lib/pipeline.py
@@ -3886,6 +3886,10 @@ def _run_supplemental_searches(
resolved_handles_out: list[str] | None = None,
) -> None:
"""Phase 2: extract entities from Phase 1 results, run targeted supplemental searches."""
+ # The sanitized plan already intersects requested, available, and excluded
+ # sources. A handle or host envelope must not expand that source boundary.
+ if not any("x" in subquery.sources for subquery in plan.subqueries):
+ return
from_date, to_date = date_range
# Host-fetched X lane: the envelope's lane calls replace the backend
diff --git a/tests/test_supplemental_source_selection.py b/tests/test_supplemental_source_selection.py
new file mode 100644
index 00000000..d83dcfba
--- /dev/null
+++ b/tests/test_supplemental_source_selection.py
@@ -0,0 +1,98 @@
+"""Supplemental X lanes must stay inside the effective research plan."""
+
+from unittest.mock import patch
+from uuid import uuid4
+
+import pytest
+
+from lib import dates, pipeline, x_envelope
+
+
[email protected]("depth", ["default", "deep"])
[email protected]("lane", ["backend", "envelope"])
+ "requested,excluded,planned,allowed",
+ [
+ pytest.param(["reddit"], "x", ["reddit"], False, id="reddit-and-exclusion"),
+ pytest.param(["reddit"], "", ["reddit", "x"], False, id="requested-reddit"),
+ pytest.param(None, "x", ["reddit", "x"], False, id="excluded-x"),
+ pytest.param(["reddit", "x"], "x", ["reddit", "x"], False, id="exclusion-wins"),
+ pytest.param(["reddit", "x"], "", ["reddit"], False, id="plan-omits-x"),
+ pytest.param(None, "", ["reddit", "x"], True, id="available-x"),
+ pytest.param(["reddit", "x"], "", ["reddit", "x"], True, id="requested-x"),
+ pytest.param(["xquik"], "", ["reddit", "x"], True, id="requested-alias"),
+ ],
+)
+def test_supplemental_lanes_obey_effective_sources(depth, lane, requested, excluded, planned, allowed):
+ topic = "widget development"
+ marker = uuid4().hex
+ window = dates.get_date_range(30)
+ posts = [
+ {
+ "id": str(index),
+ "url": f"https://x.com/example/status/{index}",
+ "author_handle": "example",
+ "text": f"{topic} {marker}: {label}",
+ "date": window[1],
+ "engagement": {"likes": 120, "retweets": 10, "replies": 5},
+ "relevance": 1.0,
+ }
+ for index, label in enumerate([
+ "shipping a compiler release with faster incremental builds and reduced memory consumption",
+ "community feedback suggests clearer tutorials explaining deployment authentication errors for beginners",
+ ], start=1)
+ ]
+ envelope = None
+ if lane == "envelope":
+ envelope = x_envelope.Envelope(
+ path="fixture.json", sha256="fixture", status="ok", error_category="",
+ provider="x-connector", window=window, topic_items=[],
+ lane_calls=[
+ x_envelope.EnvelopeCall(0, "from", ["example"], [posts[0]]),
+ x_envelope.EnvelopeCall(1, "mention", ["example"], [posts[1]]),
+ ],
+ counters={}, accepted=2, total=2,
+ lane_counts={"from": 1, "mention": 1}, call_lanes=["from", "mention"],
+ )
+ config = {
+ "X_BEARER_TOKEN": "dummy-bearer",
+ "LAST30DAYS_X_BACKEND": "xapi",
+ "EXCLUDE_SOURCES": excluded,
+ }
+ plan = {
+ "intent": "opinion", "freshness_mode": "balanced_recent", "cluster_mode": "debate",
+ "subqueries": [{
+ "label": "primary", "search_query": topic,
+ "ranking_query": topic, "sources": planned,
+ }],
+ }
+ with (
+ patch.object(pipeline.reddit_public, "search_reddit_public", autospec=True, return_value=[]),
+ patch.object(pipeline.x_api, "search_x", autospec=True, return_value={"items": []}),
+ patch.object(pipeline.x_api, "search_handles", autospec=True, return_value=[posts[0]]) as from_lane,
+ patch.object(pipeline.x_api, "search_mentions", autospec=True, return_value=[posts[1]]) as about_lane,
+ ):
+ report = pipeline.run(
+ topic=topic, config=config, depth=depth, requested_sources=requested,
+ external_plan=plan, x_handle="example", x_posts=envelope,
+ web_backend="none", save_dir="",
+ )
+
+ evidence = report.items_by_source.get("x", [])
+ if allowed:
+ assert {item.url for item in evidence} == {post["url"] for post in posts}
+ assert all(marker in item.body for item in evidence)
+ else:
+ assert not evidence
+ if allowed and lane == "backend":
+ from_lane.assert_called_once()
+ about_lane.assert_called_once()
+ else:
+ from_lane.assert_not_called()
+ about_lane.assert_not_called()
+ if envelope is not None:
+ remaining = envelope.take_lanes()
+ if allowed:
+ assert remaining is None
+ else:
+ assert remaining == envelope.lane_calls