AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary
Python ai-promptsai-skillblueskyclaudeclaude-codeclawhubdeep-researchhackernewsinstagramopenclawpolymarketrecencyredditresearchsocial-mediatiktoktrendstwitterweb-searchyoutube
fix(planner): honor operator --plan subquery sources at default/deep (#1093)
Gustavo Samudio1dbdbcfc0b52
diff --git a/changelog.d/1073.fixed.md b/changelog.d/1073.fixed.mdnew file mode 100644index 00000000..1172c750--- /dev/null+++ b/changelog.d/1073.fixed.md@@ -0,0 +1 @@+Operator-supplied `--plan` now keeps each subquery's `sources` instead of silently replacing them with the full available list. Unavailable-only subqueries are visibly skipped; if none remain, the run fails before retrieval with configuration guidance. Engine-internal LLM plans retain their fallback and expansion behavior.diff --git a/skills/last30days/SKILL.md b/skills/last30days/SKILL.mdindex b2d83593..9ba168d2 100644--- a/skills/last30days/SKILL.md+++ b/skills/last30days/SKILL.md@@ -1412,7 +1412,7 @@ Only show lines for platforms where something was resolved. Skip empty lines. On**Rules for your plan:**- Emit 1 to 4 subqueries (more for complex/multi-faceted topics, fewer for simple ones)-- **CRITICAL: Your PRIMARY subquery MUST include every applicable source from `ACTIVE_SOURCES_LIST` among reddit, x, youtube, tiktok, instagram, hackernews, polymarket.** Never invent an unavailable source. Preserve X whenever it is active; when it is unavailable, continue with the rest. Never omit active Reddit (highest-signal discussion) or active YouTube (unique transcripts + official content). Secondary subqueries can target specific platforms.+- **CRITICAL: Your PRIMARY subquery MUST include every applicable source from `ACTIVE_SOURCES_LIST` among reddit, x, youtube, tiktok, instagram, hackernews, polymarket.** Never invent an unavailable source. Preserve X whenever it is active; when it is unavailable, continue with the rest. Never omit active Reddit (highest-signal discussion) or active YouTube (unique transcripts + official content). Secondary subqueries can target specific platforms. Per-subquery `sources` on `--plan` is a contract at every depth (quick, default, and deep): the engine searches only those sources and does not expand a narrow list to every available source. Restrict a subquery only when you mean it.- `search_query` should be concise and keyword-heavy - match how content is TITLED on platforms- `ranking_query` should read like a natural language question- **X disambiguation:** express your disambiguation intent in `ranking_query` (e.g., "What are people saying about Rome the city in Italy, not AS Roma or Rome Odunze?") — do not phrase-quote `search_query` for X or invent X operators; the engine handles X query compilation internally.diff --git a/skills/last30days/scripts/lib/pipeline.py b/skills/last30days/scripts/lib/pipeline.pyindex e3242501..5bc4b301 100644--- a/skills/last30days/scripts/lib/pipeline.py+++ b/skills/last30days/scripts/lib/pipeline.py@@ -2083,6 +2083,7 @@ def run(planner.validate_external_plan(external_plan)plan = planner._sanitize_plan(external_plan, topic, available, planner_requested_sources, depth,+ honor_plan_sources=True,)plan_source = "external"else:diff --git a/skills/last30days/scripts/lib/planner.py b/skills/last30days/scripts/lib/planner.pyindex f04d6003..02a4cdd6 100644--- a/skills/last30days/scripts/lib/planner.py+++ b/skills/last30days/scripts/lib/planner.py@@ -7,7 +7,7 @@ import reimport unicodedatafrom collections import Counter-from . import categories, competitors, entity_extract, http, providers, query, relevance, schema+from . import categories, competitors, entity_extract, http, log, providers, query, relevance, schema# Hebrew Unicode block: U+0590–U+05FF_HEBREW_RE = re.compile(r'[\u0590-\u05FF]')@@ -374,7 +374,9 @@ def plan_query(if provider and model:try:raw = provider.generate_json(model, prompt)- plan = _sanitize_plan(raw, topic, available_sources, requested_sources, depth)+ plan = _sanitize_plan(+ raw, topic, available_sources, requested_sources, depth,+ )if plan.subqueries:return planexcept (ValueError, KeyError, json.JSONDecodeError, OSError, http.HTTPError) as exc:@@ -466,6 +468,8 @@ def _sanitize_plan(available_sources: list[str],requested_sources: list[str] | None,depth: str,+ *,+ honor_plan_sources: bool = False,) -> schema.QueryPlan:intent_hint = str(raw.get("intent") or _infer_intent(topic)).strip()if intent_hint not in ALLOWED_INTENTS:@@ -504,6 +508,15 @@ def _sanitize_plan(if requested:sources = [source for source in sources if source in requested]if not sources:+ if honor_plan_sources:+ label = str(subquery.get("label") or f"q{index}")+ log.source_log(+ "Planner",+ f"Skipping external-plan subquery {label}: none of its planned "+ "sources are available under the current source configuration.",+ tty_only=False,+ )+ continuesources = list(source_weights)search_query = str(subquery.get("search_query") or "").strip()ranking_query = str(subquery.get("ranking_query") or "").strip()@@ -521,6 +534,11 @@ def _sanitize_plan(if depth == "quick" and subqueries:subqueries = subqueries[:1]if not subqueries:+ if honor_plan_sources:+ raise ValueError(+ "No available planned sources remain. Enable a source named in "+ "--plan or revise the plan/source configuration; no retrieval was started."+ )return _fallback_plan(topic, available_sources, requested_sources, depth)intent = intent_hint@@ -543,6 +561,7 @@ def _sanitize_plan(depth,eligible_sources,requested_sources=requested_sources,+ honor_plan_sources=honor_plan_sources,)),source_weights=source_weights,@@ -578,11 +597,13 @@ def _trim_subqueries_for_depth(depth: str,available_sources: list[str],requested_sources: list[str] | None = None,+ honor_plan_sources: bool = False,) -> list[schema.SubQuery]:# At non-quick depth, expand sources: use capability routing for intents# that define it, or all available sources otherwise. The LLM planner may# assign narrow source lists; we override to let fusion decide quality.- if depth != "quick":+ # Operator-supplied --plan is a contract: keep per-subquery sources.+ if depth != "quick" and not honor_plan_sources:expanded_sources = _default_sources_for_intent(intent, available_sources)return [schema.SubQuery(diff --git a/tests/test_external_plan_source_contract.py b/tests/test_external_plan_source_contract.pynew file mode 100644index 00000000..08c0b372--- /dev/null+++ b/tests/test_external_plan_source_contract.py@@ -0,0 +1,55 @@+"""Unavailable external-plan sources must never broaden retrieval."""++from unittest.mock import patch++import pytest++from lib import pipeline, planner+++def plan_with(*source_lists):+ return {+ "intent": "opinion",+ "freshness_mode": "balanced_recent",+ "cluster_mode": "debate",+ "subqueries": [+ {"label": f"part-{i}", "search_query": "test topic", "ranking_query": "test topic",+ "sources": sources, "weight": 1.0}+ for i, sources in enumerate(source_lists)+ ],+ }++[email protected]("depth", ["quick", "default", "deep"])+def test_all_unavailable_fails_before_retrieval(depth, capsys):+ with patch.object(pipeline, "_retrieve_stream") as retrieve:+ with pytest.raises(ValueError, match="No available planned sources"):+ pipeline.run(+ topic="test topic", config={}, mock=True, web_backend="none",+ requested_sources=["reddit", "hackernews"], depth=depth,+ external_plan=plan_with(["instagram"]),+ )+ retrieve.assert_not_called()+ assert "part-0" in capsys.readouterr().err++[email protected]("depth", ["quick", "default", "deep"])+def test_mixed_plan_skips_empty_subquery_without_expansion(depth, capsys):+ with patch.object(pipeline, "_retrieve_stream", return_value=([], {})) as retrieve:+ report = pipeline.run(+ topic="test topic", config={}, mock=True, web_backend="none",+ requested_sources=["reddit", "hackernews"], depth=depth,+ external_plan=plan_with(["instagram"], ["reddit", "instagram"]),+ )+ assert report.query_plan.subqueries[0].label == "part-1"+ assert report.query_plan.subqueries[0].sources == ["reddit"]+ assert retrieve.call_args_list+ assert {call.kwargs["source"] for call in retrieve.call_args_list} == {"reddit"}+ assert "part-0" in capsys.readouterr().err+++def test_internal_empty_intersection_retains_fallback():+ plan = planner._sanitize_plan(+ plan_with(["instagram"]), "test topic", ["reddit", "hackernews"], None, "default",+ )+ assert plan.subqueries[0].sources == ["reddit", "hackernews"]diff --git a/tests/test_internals_v3.py b/tests/test_internals_v3.pyindex b790e1e6..4dd9f75a 100644--- a/tests/test_internals_v3.py+++ b/tests/test_internals_v3.py@@ -314,6 +314,20 @@ class TestTrimSubqueriesForDepth(unittest.TestCase):# Deep comparison should also use capability expansion, not trimself.assertGreaterEqual(len(result[0].sources), 4)+ def test_honor_plan_sources_skips_expansion_at_default_and_deep(self):+ available = ["reddit", "x", "youtube", "hackernews", "polymarket", "github"]+ sqs = [self._sq(sources=["reddit", "x", "youtube"])]+ for depth in ("default", "deep"):+ with self.subTest(depth=depth):+ result = planner._trim_subqueries_for_depth(+ sqs,+ "opinion",+ depth,+ available,+ honor_plan_sources=True,+ )+ self.assertEqual(["reddit", "x", "youtube"], result[0].sources)+# ---------------------------------------------------------------------------# signals.annotate_stream# ---------------------------------------------------------------------------diff --git a/tests/test_pipeline_v3.py b/tests/test_pipeline_v3.pyindex 316f26a1..13c34836 100644--- a/tests/test_pipeline_v3.py+++ b/tests/test_pipeline_v3.py@@ -66,6 +66,39 @@ class PipelineV3Tests(unittest.TestCase):mock=True,)+ def test_external_plan_honors_per_subquery_sources_at_default_depth(self):+ # Issue #1073: --plan sources are a contract at default/deep.+ plan = {+ "intent": "opinion",+ "freshness_mode": "balanced_recent",+ "cluster_mode": "debate",+ "subqueries": [+ {+ "label": "primary",+ "search_query": "late diagnosed autism adults",+ "ranking_query": "What are people saying about late diagnosed autism in adults?",+ "sources": ["reddit", "x", "youtube"],+ "weight": 1.0,+ }+ ],+ }+ report = pipeline.run(+ topic="late diagnosed autism adults",+ config={"LAST30DAYS_REASONING_PROVIDER": "gemini"},+ depth="default",+ requested_sources=["reddit", "x", "youtube", "hackernews", "polymarket", "github"],+ mock=True,+ web_backend="none",+ external_plan=plan,+ )+ self.assertEqual(+ ["reddit", "x", "youtube"],+ report.query_plan.subqueries[0].sources,+ )+ self.assertNotIn("hackernews", report.items_by_source)+ self.assertNotIn("polymarket", report.items_by_source)+ self.assertNotIn("github", report.items_by_source)+def test_planner_trace_always_fires_on_mock_run(self):"""Unit 5: The unified planner trace emits one summary line plus oneline per subquery on every run, regardless of --debug. 2026-04-19diff --git a/tests/test_planner_v3.py b/tests/test_planner_v3.pyindex d1d37687..fd3902d7 100644--- a/tests/test_planner_v3.py+++ b/tests/test_planner_v3.py@@ -142,6 +142,96 @@ class PlannerV3Tests(unittest.TestCase):self.assertEqual(1, len(plan.subqueries))self.assertEqual(["reddit", "x"], plan.subqueries[0].sources)+ def test_operator_plan_keeps_per_subquery_sources_at_default_and_deep(self):+ # Issue #1073: operator --plan sources must not be replaced with the+ # full available list outside --quick.+ raw = {+ "intent": "opinion",+ "freshness_mode": "balanced_recent",+ "cluster_mode": "debate",+ "subqueries": [{+ "label": "primary",+ "search_query": "late diagnosed autism adults",+ "ranking_query": "late diagnosed autism adults",+ "sources": ["reddit", "x", "youtube"],+ "weight": 1.0,+ }],+ }+ avail = ["reddit", "x", "youtube", "hackernews", "polymarket", "github"]+ for depth in ("default", "deep"):+ with self.subTest(depth=depth):+ plan = planner._sanitize_plan(+ raw,+ "late diagnosed autism adults",+ avail,+ None,+ depth,+ honor_plan_sources=True,+ )+ self.assertEqual(["reddit", "x", "youtube"], plan.subqueries[0].sources)++ def test_llm_plan_still_expands_narrow_sources_at_default_and_deep(self):+ # Engine-internal LLM plans keep the expansion that lets fusion+ # decide quality. Same snippet as #1073, without honor_plan_sources.+ raw = {+ "intent": "opinion",+ "freshness_mode": "balanced_recent",+ "cluster_mode": "debate",+ "subqueries": [{+ "label": "primary",+ "search_query": "late diagnosed autism adults",+ "ranking_query": "late diagnosed autism adults",+ "sources": ["reddit", "x", "youtube"],+ "weight": 1.0,+ }],+ }+ avail = ["reddit", "x", "youtube", "hackernews", "polymarket", "github"]+ for depth in ("default", "deep"):+ with self.subTest(depth=depth):+ plan = planner._sanitize_plan(+ raw,+ "late diagnosed autism adults",+ avail,+ None,+ depth,+ )+ self.assertEqual(avail, plan.subqueries[0].sources)++ def test_operator_plan_preserves_distinct_per_subquery_source_lists(self):+ raw = {+ "intent": "breaking_news",+ "freshness_mode": "strict_recent",+ "cluster_mode": "story",+ "subqueries": [+ {+ "label": "primary",+ "search_query": "kanye west",+ "ranking_query": "What happened with Kanye West?",+ "sources": ["reddit", "x", "youtube"],+ "weight": 1.0,+ },+ {+ "label": "album",+ "search_query": "kanye west bully album",+ "ranking_query": "How was Kanye West's BULLY album received?",+ "sources": ["youtube", "reddit"],+ "weight": 0.8,+ },+ ],+ }+ avail = ["reddit", "x", "youtube", "hackernews", "polymarket", "github"]+ plan = planner._sanitize_plan(+ raw,+ "Kanye West",+ avail,+ None,+ "default",+ honor_plan_sources=True,+ )+ by_label = {sq.label: sq.sources for sq in plan.subqueries}+ self.assertEqual(["reddit", "x", "youtube"], by_label["primary"])+ self.assertEqual(["youtube", "reddit"], by_label["album"])+def test_quick_mode_prioritizes_explicit_requested_sources_within_cap(self):raw = {"intent": "product",