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: isolate competitor context and exercise real orchestration (#1227)

Ilia Alshanetsky1fd968370712
diff --git a/changelog.d/+test-audit-peer-context.fixed.md b/changelog.d/+test-audit-peer-context.fixed.md
new file mode 100644
index 00000000..f649b577
--- /dev/null
+++ b/changelog.d/+test-audit-peer-context.fixed.md
@@ -0,0 +1 @@
+Prevent competitor research from inheriting the main topic's resolved context when the competitor has no context of its own.
diff --git a/skills/last30days/scripts/last30days.py b/skills/last30days/scripts/last30days.py
index 9031774f..e91d7e8a 100644
--- a/skills/last30days/scripts/last30days.py
+++ b/skills/last30days/scripts/last30days.py
@@ -4095,10 +4095,9 @@ def _main(
)
def _competitor_runner(entity: str) -> schema.Report:
- # Deep-copy config so per-entity auto_resolve context does not
- # leak across sub-runs. Each sub-run writes its own
- # `_auto_resolve_context` into its local config copy.
+ # Resolution belongs to each entity, not the shared command.
entity_config = dict(config)
+ entity_config.pop("_auto_resolve_context", None)
# The Amazon keyword is entity-SPECIFIC, unlike the depth caps
# this shallow copy exists to inherit. Leaving the main topic's
# keyword in place would search Weber SKUs for a Traeger peer,
diff --git a/tests/competitor_cli_audit_helpers.py b/tests/competitor_cli_audit_helpers.py
new file mode 100644
index 00000000..66734570
--- /dev/null
+++ b/tests/competitor_cli_audit_helpers.py
@@ -0,0 +1,54 @@
+"""Run real CLI/fanout wiring while collecting the research boundary inputs."""
+
+import sys
+from types import SimpleNamespace
+from unittest import mock
+
+import last30days as cli
+from lib import resolve, schema
+
+
+def run_competitor_cli(
+ monkeypatch, tmp_path, *, args=(), config=None, has_backend=False, resolutions=None
+):
+ config = {} if config is None else config
+ calls = {}
+ reports = {}
+
+ def research(**kwargs):
+ topic = kwargs["topic"]
+ calls[topic] = kwargs
+ report = schema.Report(
+ topic=topic, range_from="2026-06-01", range_to="2026-06-30",
+ generated_at="2026-06-30T12:00:00Z",
+ provider_runtime=schema.ProviderRuntime("local", "none", "none"),
+ query_plan=schema.QueryPlan("general", "recent", "none", topic, [], {}),
+ clusters=[], ranked_candidates=[], items_by_source={}, errors_by_source={},
+ )
+ reports[topic] = report
+ return report
+
+ if isinstance(resolutions, Exception):
+ resolver = mock.Mock(side_effect=resolutions)
+ else:
+ resolver = mock.Mock(side_effect=lambda topic, _config: (resolutions or {}).get(topic, {}))
+ monkeypatch.setattr(cli.env, "get_config", lambda **_kwargs: config)
+ monkeypatch.setattr(cli.env, "CONFIG_DIR", tmp_path / "config")
+ monkeypatch.setattr(cli.env, "CONFIG_FILE", tmp_path / "config" / ".env")
+ monkeypatch.setattr(cli.pipeline, "diagnose", lambda *_args, **_kwargs: {"available_sources": ["reddit"]})
+ monkeypatch.setattr(cli.pipeline, "run", research)
+ monkeypatch.setattr(resolve, "_has_backend", lambda _config: has_backend)
+ monkeypatch.setattr(resolve, "auto_resolve", resolver)
+ monkeypatch.setenv("LAST30DAYS_SKIP_PREFLIGHT", "1")
+ monkeypatch.setenv("LAST30DAYS_MEMORY_DIR", "")
+ monkeypatch.setenv("LAST30DAYS_STORE", "")
+ monkeypatch.delenv("LAST30DAYS_API_BASE", raising=False)
+ monkeypatch.delenv("LAST30DAYS_API_KEY", raising=False)
+ monkeypatch.setattr(sys, "argv", [
+ "last30days.py", "MainBrand", "--competitors-list", "PeerOne,PeerTwo",
+ "--emit=json", "--json-profile=raw", "--save-dir=", *args,
+ ])
+
+ assert cli.main() == 0
+ assert set(calls) == {"MainBrand", "PeerOne", "PeerTwo"}
+ return SimpleNamespace(calls=calls, reports=reports, resolver=resolver)
diff --git a/tests/test_competitor_subrun_isolation.py b/tests/test_competitor_subrun_isolation.py
index 7b834483..a5e67a46 100644
--- a/tests/test_competitor_subrun_isolation.py
+++ b/tests/test_competitor_subrun_isolation.py
@@ -1,189 +1,81 @@
-"""Regression tests: main-topic flags must not leak into competitor sub-runs.
-
-Based on 2026-04-22 Kanye West --competitors receipt where Drake and
-Kendrick Lamar sub-runs logged Kanye's resolved subreddit list as their own
-targeted search. Per-entity sub-runs must never inherit main-topic targeting
-via closure capture, config mutation, or any other path.
-"""
-
-from __future__ import annotations
-
-import io
-import unittest
-from contextlib import redirect_stderr
-from unittest import mock
-
-
-def _fake_report(topic: str):
- class _R:
- pass
-
- r = _R()
- r.topic = topic
- r.artifacts = {}
- return r
-
-
-class SubRunIsolationTests(unittest.TestCase):
- """Exercise the _competitor_runner closure pattern from main() directly.
-
- Builds the same closure shape main() uses, then invokes it with
- captured-in-scope main-topic flags to verify they do NOT leak into
- sub-run pipeline.run kwargs.
- """
-
- def _run_closure(self, main_flags, competitors, config=None, mock_flag=False):
- """Replicate _competitor_runner closure from last30days.py main().
-
- main_flags: dict of {x_handle, x_related, subreddits, tiktok_hashtags,
- tiktok_creators, ig_creators, github_user, github_repos}
- as they would exist in outer scope after argparse.
- competitors: list of entity names to run.
- Returns the list of kwargs dicts pipeline.run was called with.
- """
- from lib import pipeline, resolve as resolve_mod
-
- captured: list[dict] = []
-
- def fake_run(**kwargs):
- captured.append(kwargs)
- return _fake_report(kwargs["topic"])
-
- # Simulate main scope variables
- outer_subreddits = main_flags.get("subreddits")
- outer_x_handle = main_flags.get("x_handle")
- outer_x_related = main_flags.get("x_related")
- outer_tiktok_hashtags = main_flags.get("tiktok_hashtags")
- outer_tiktok_creators = main_flags.get("tiktok_creators")
- outer_ig_creators = main_flags.get("ig_creators")
- outer_github_user = main_flags.get("github_user")
- outer_github_repos = main_flags.get("github_repos")
-
- class _Args:
- pass
- args = _Args()
- args.mock = mock_flag
- args.web_backend = "auto"
- args.lookback_days = 30
-
- cfg = config or {}
-
- # This mirrors the real _competitor_runner closure structure.
- def competitor_runner(entity):
- entity_config = dict(cfg)
- resolved = {
- "entity": entity,
- "x_handle": "",
- "subreddits": [],
- "github_user": "",
- "github_repos": [],
- "context": "",
- }
- if not args.mock and resolve_mod._has_backend(entity_config):
- try:
- r = resolve_mod.auto_resolve(entity, entity_config)
- except Exception:
- r = {}
- resolved["x_handle"] = r.get("x_handle", "") or ""
- resolved["subreddits"] = list(r.get("subreddits") or [])
- resolved["github_user"] = r.get("github_user", "") or ""
- resolved["github_repos"] = list(r.get("github_repos") or [])
- resolved["context"] = r.get("context", "") or ""
- if resolved["context"]:
- entity_config["_auto_resolve_context"] = resolved["context"]
- pipeline.run(
- topic=entity,
- config=entity_config,
- depth="default",
- requested_sources=None,
- mock=args.mock,
- x_handle=resolved["x_handle"] or None,
- subreddits=resolved["subreddits"] or None,
- github_user=resolved["github_user"] or None,
- github_repos=resolved["github_repos"] or None,
- web_backend=args.web_backend,
- lookback_days=args.lookback_days,
- internal_subrun=True,
- )
-
- with mock.patch.object(pipeline, "run", side_effect=fake_run):
- for entity in competitors:
- competitor_runner(entity)
-
- return captured
-
- def test_main_subreddits_do_not_leak_to_peers(self):
- """Kanye receipt: main --subreddits=Kanye,hiphopheads leaked to Drake/Kendrick."""
- main_flags = {
- "subreddits": ["Kanye", "hiphopheads", "Music", "popheads", "kanyewest"],
- "x_handle": "kanyewest",
- }
- captured = self._run_closure(main_flags, ["Drake", "Kendrick Lamar"])
- self.assertEqual(len(captured), 2)
- for kwargs in captured:
- self.assertIsNone(
- kwargs["subreddits"],
- f"Main subreddits leaked into {kwargs['topic']!r}'s sub-run: "
- f"{kwargs['subreddits']}",
- )
-
- def test_main_x_handle_does_not_leak(self):
- main_flags = {"x_handle": "kanyewest"}
- captured = self._run_closure(main_flags, ["Drake"])
- self.assertIsNone(captured[0]["x_handle"])
-
- def test_main_github_does_not_leak(self):
- main_flags = {
- "github_user": "someuser",
- "github_repos": ["someuser/someproject"],
- }
- captured = self._run_closure(main_flags, ["Drake"])
- self.assertIsNone(captured[0]["github_user"])
- self.assertIsNone(captured[0]["github_repos"])
-
- def test_auto_resolve_context_does_not_leak_across_peers(self):
- """Per-entity auto_resolve context must not bleed between sub-runs."""
- from lib import resolve as resolve_mod
-
- def fake_resolve(entity, _cfg):
- per_topic = {
- "Drake": {"x_handle": "Drake", "subreddits": [], "github_user": "",
- "github_repos": [], "context": "Drake ICEMAN rollout",
- "category": None, "searches_run": 4},
- "Kendrick Lamar": {"x_handle": "kendricklamar", "subreddits": [],
- "github_user": "", "github_repos": [],
- "context": "Meet The Grahams revival",
- "category": None, "searches_run": 4},
- }
- return per_topic.get(entity, {})
-
- with mock.patch.object(resolve_mod, "auto_resolve", side_effect=fake_resolve), \
- mock.patch.object(resolve_mod, "_has_backend", return_value=True):
- captured = self._run_closure(
- main_flags={},
- competitors=["Drake", "Kendrick Lamar"],
- config={"BRAVE_API_KEY": "test"},
- )
-
- by_topic = {kw["topic"]: kw for kw in captured}
- # Each sub-run's config got its own context string.
- self.assertEqual(
- by_topic["Drake"]["config"].get("_auto_resolve_context"),
- "Drake ICEMAN rollout",
- )
- self.assertEqual(
- by_topic["Kendrick Lamar"]["config"].get("_auto_resolve_context"),
- "Meet The Grahams revival",
- )
- # Cross-entity check: neither config contains the other's context.
- self.assertNotIn(
- "Meet The Grahams",
- by_topic["Drake"]["config"].get("_auto_resolve_context", ""),
- )
- self.assertNotIn(
- "ICEMAN",
- by_topic["Kendrick Lamar"]["config"].get("_auto_resolve_context", ""),
- )
-
-if __name__ == "__main__":
- unittest.main()
+"""Main-topic targeting must not reach another entity's pipeline invocation."""
+
+import json
+
+import pytest
+
+from tests.competitor_cli_audit_helpers import run_competitor_cli
+
+
+ ("flag", "value", "keyword", "main_value"),
+ [
+ ("--subreddits", "MainOnly,MainNews", "subreddits", ["MainOnly", "MainNews"]),
+ ("--x-handle", "MainOnly", "x_handle", "MainOnly"),
+ ("--x-related", "MainFriend", "x_related", ["MainFriend"]),
+ ("--github-user", "MainOwner", "github_user", "mainowner"),
+ ("--github-repo", "mainowner/project", "github_repos", ["mainowner/project"]),
+ ("--tiktok-hashtags", "MainOnly", "tiktok_hashtags", ["MainOnly"]),
+ ("--tiktok-creators", "MainCreator", "tiktok_creators", ["MainCreator"]),
+ ("--ig-creators", "MainInstagram", "ig_creators", ["MainInstagram"]),
+ ],
+)
+def test_main_targeting_does_not_leak_to_peer(
+ monkeypatch, tmp_path, flag, value, keyword, main_value
+):
+ observed = run_competitor_cli(monkeypatch, tmp_path, args=[flag, value])
+
+ assert observed.calls["MainBrand"][keyword] == main_value
+ for peer in ("PeerOne", "PeerTwo"):
+ assert observed.calls[peer].get(keyword) is None
+
+
+def test_main_plan_is_not_reused_for_a_peer(monkeypatch, tmp_path):
+ main_plan = {
+ "intent": "general",
+ "freshness_mode": "recent",
+ "cluster_mode": "none",
+ "subqueries": [{
+ "label": "main-only", "search_query": "MainBrand only",
+ "ranking_query": "MainBrand", "sources": ["reddit"],
+ }],
+ }
+ plan_file = tmp_path / "main-plan.json"
+ plan_file.write_text(json.dumps(main_plan), encoding="utf-8")
+
+ observed = run_competitor_cli(monkeypatch, tmp_path, args=["--plan", str(plan_file)])
+
+ assert observed.calls["MainBrand"]["external_plan"] == main_plan
+ assert observed.calls["PeerOne"].get("external_plan") is None
+ assert observed.calls["PeerTwo"].get("external_plan") is None
+
+
+def test_peer_context_does_not_mutate_main_or_an_unresolved_peer(monkeypatch, tmp_path):
+ config = {"BRAVE_API_KEY": "dummy-brave-key"}
+ observed = run_competitor_cli(
+ monkeypatch, tmp_path, args=["--x-handle", "MainOnly"],
+ config=config, has_backend=True,
+ resolutions={"PeerOne": {"context": "PeerOne unique context"}, "PeerTwo": {}},
+ )
+
+ assert observed.calls["PeerOne"]["config"].get("_auto_resolve_context", "") == "PeerOne unique context"
+ assert observed.calls["PeerTwo"]["config"].get("_auto_resolve_context", "") == ""
+ assert observed.calls["MainBrand"]["config"].get("_auto_resolve_context", "") == ""
+ assert "_auto_resolve_context" not in config
+
+
+def test_main_context_does_not_leak_to_peer_without_context(monkeypatch, tmp_path):
+ observed = run_competitor_cli(
+ monkeypatch, tmp_path, args=["--auto-resolve"],
+ config={"BRAVE_API_KEY": "dummy-brave-key"}, has_backend=True,
+ resolutions={
+ "MainBrand": {"context": "MainBrand private launch context"},
+ "PeerOne": {"context": "PeerOne distinct context"},
+ "PeerTwo": {},
+ },
+ )
+
+ assert observed.calls["MainBrand"]["config"]["_auto_resolve_context"] == "MainBrand private launch context"
+ assert observed.calls["PeerOne"]["config"]["_auto_resolve_context"] == "PeerOne distinct context"
+ assert observed.calls["PeerTwo"]["config"].get("_auto_resolve_context", "") == ""
+ assert observed.reports["PeerTwo"].artifacts["resolved"]["context"] == ""
diff --git a/tests/test_competitors_resolve_integration.py b/tests/test_competitors_resolve_integration.py
index 31b36112..f7eecc3e 100644
--- a/tests/test_competitors_resolve_integration.py
+++ b/tests/test_competitors_resolve_integration.py
@@ -1,325 +1,118 @@
-"""Integration tests for per-entity Step 0.55 resolution inside competitor fan-out."""
-
-from __future__ import annotations
-
-import io
-import sys
-import unittest
-from contextlib import redirect_stderr
-from unittest import mock
-
-
-def _fake_report(topic: str):
- """Minimal Report stand-in for runner return values."""
- class _R:
- pass
-
- r = _R()
- r.topic = topic
- r.artifacts = {}
- return r
-
-
-def _build_main_args(*overrides):
- """Minimal argparse.Namespace-like object for the competitor path."""
- import argparse
- ns = argparse.Namespace(
- topic=["Kanye West"],
- mock=False,
- competitors=2,
- competitors_list=None,
- quick=False,
- deep=False,
- emit="compact",
- search=None,
- debug=False,
- diagnose=False,
- save_dir=None,
- save_suffix=None,
- store=False,
- x_handle=None,
- x_related=None,
- web_backend="auto",
- deep_research=False,
- plan=None,
- subreddits=None,
- tiktok_hashtags=None,
- tiktok_creators=None,
- ig_creators=None,
- lookback_days=30,
- auto_resolve=False,
- github_user=None,
- github_repo=None,
+"""Per-entity resolution through the real CLI and competitor fan-out."""
+
+import json
+
+import pytest
+
+from tests.competitor_cli_audit_helpers import run_competitor_cli
+
+
+def test_resolver_results_reach_each_competitor_pipeline(monkeypatch, tmp_path):
+ resolutions = {
+ "PeerOne": {
+ "x_handle": "peerone", "subreddits": ["PeerOneNews"],
+ "github_user": "ownerone", "github_repos": ["ownerone/project"],
+ "context": "PeerOne launch",
+ },
+ "PeerTwo": {
+ "x_handle": "peertwo", "subreddits": ["PeerTwoNews"],
+ "github_user": "ownertwo", "github_repos": ["ownertwo/project"],
+ "context": "PeerTwo launch",
+ },
+ }
+ observed = run_competitor_cli(
+ monkeypatch, tmp_path,
+ args=["--x-handle", "mainonly", "--subreddits", "MainOnly"],
+ config={"BRAVE_API_KEY": "dummy-brave-key"},
+ has_backend=True, resolutions=resolutions,
)
- return ns
-
-
-class PerEntityResolveTests(unittest.TestCase):
- """Verify each competitor sub-run calls auto_resolve with its own topic and
- that the resolved fields are threaded into pipeline.run."""
-
- def test_auto_resolve_called_per_competitor(self):
- from lib import resolve as resolve_mod
- from lib import pipeline as pipeline_mod
-
- config = {"BRAVE_API_KEY": "test-key"}
-
- captured_resolve_topics: list[str] = []
- captured_pipeline_kwargs: list[dict] = []
-
- def fake_resolve(topic, _cfg):
- captured_resolve_topics.append(topic)
- per_topic = {
- "Drake": {
- "x_handle": "Drake",
- "subreddits": ["DrakeTheType", "hiphopheads"],
- "github_user": "",
- "github_repos": [],
- "context": "Drake ICEMAN rollout",
- "category": None,
- "searches_run": 4,
- },
- "Kendrick Lamar": {
- "x_handle": "kendricklamar",
- "subreddits": ["KendrickLamar", "hiphopheads"],
- "github_user": "",
- "github_repos": [],
- "context": "Meet The Grahams revival",
- "category": None,
- "searches_run": 4,
- },
- }
- return per_topic.get(topic, {
- "x_handle": "", "subreddits": [], "github_user": "",
- "github_repos": [], "context": "",
- "category": None, "searches_run": 0,
- })
-
- def fake_pipeline_run(**kwargs):
- captured_pipeline_kwargs.append(kwargs)
- return _fake_report(kwargs["topic"])
-
- with mock.patch.object(resolve_mod, "auto_resolve", side_effect=fake_resolve), \
- mock.patch.object(resolve_mod, "_has_backend", return_value=True), \
- mock.patch.object(pipeline_mod, "run", side_effect=fake_pipeline_run):
- # Exercise the competitor_runner closure pattern from main() by
- # calling it directly with two competitors.
- self._run_competitor_closure(
- config=config,
- competitors=["Drake", "Kendrick Lamar"],
- mock_flag=False,
- )
-
- # auto_resolve was called once per competitor
- self.assertEqual(sorted(captured_resolve_topics), ["Drake", "Kendrick Lamar"])
- # pipeline.run received resolved fields per entity
- by_topic = {kw["topic"]: kw for kw in captured_pipeline_kwargs}
- self.assertEqual(by_topic["Drake"]["x_handle"], "Drake")
- self.assertEqual(
- by_topic["Drake"]["subreddits"], ["DrakeTheType", "hiphopheads"],
- )
- self.assertEqual(by_topic["Kendrick Lamar"]["x_handle"], "kendricklamar")
- # internal_subrun=True on all competitor sub-runs
- self.assertTrue(all(kw["internal_subrun"] for kw in captured_pipeline_kwargs))
-
- def test_mock_mode_skips_auto_resolve(self):
- from lib import resolve as resolve_mod
- from lib import pipeline as pipeline_mod
-
- resolve_called = []
-
- def fake_resolve(*a, **k):
- resolve_called.append((a, k))
- return {}
-
- with mock.patch.object(resolve_mod, "auto_resolve", side_effect=fake_resolve), \
- mock.patch.object(pipeline_mod, "run", side_effect=lambda **kw: _fake_report(kw["topic"])):
- self._run_competitor_closure(
- config={"BRAVE_API_KEY": "test-key"},
- competitors=["Anthropic"],
- mock_flag=True,
- )
-
- self.assertEqual(resolve_called, [])
- def test_no_backend_skips_auto_resolve(self):
- from lib import resolve as resolve_mod
- from lib import pipeline as pipeline_mod
-
- resolve_called = []
-
- def fake_resolve(*a, **k):
- resolve_called.append((a, k))
- return {}
-
- with mock.patch.object(resolve_mod, "auto_resolve", side_effect=fake_resolve), \
- mock.patch.object(resolve_mod, "_has_backend", return_value=False), \
- mock.patch.object(pipeline_mod, "run", side_effect=lambda **kw: _fake_report(kw["topic"])):
- self._run_competitor_closure(
- config={},
- competitors=["Anthropic"],
- mock_flag=False,
- )
-
- self.assertEqual(resolve_called, [])
-
- def test_resolve_failure_degrades_gracefully(self):
- from lib import resolve as resolve_mod
- from lib import pipeline as pipeline_mod
-
- captured_pipeline_kwargs: list[dict] = []
-
- def fake_resolve(_topic, _cfg):
- raise RuntimeError("upstream offline")
-
- def fake_pipeline_run(**kwargs):
- captured_pipeline_kwargs.append(kwargs)
- return _fake_report(kwargs["topic"])
-
- err = io.StringIO()
- with redirect_stderr(err), \
- mock.patch.object(resolve_mod, "auto_resolve", side_effect=fake_resolve), \
- mock.patch.object(resolve_mod, "_has_backend", return_value=True), \
- mock.patch.object(pipeline_mod, "run", side_effect=fake_pipeline_run):
- self._run_competitor_closure(
- config={"BRAVE_API_KEY": "test-key"},
- competitors=["Anthropic"],
- mock_flag=False,
- )
-
- # Warning logged but run continues with planner defaults
- self.assertIn("auto_resolve failed for 'Anthropic'", err.getvalue())
- self.assertEqual(len(captured_pipeline_kwargs), 1)
- self.assertIsNone(captured_pipeline_kwargs[0]["x_handle"])
- self.assertIsNone(captured_pipeline_kwargs[0]["subreddits"])
-
- def test_resolved_artifact_stored_on_report(self):
- from lib import resolve as resolve_mod
- from lib import pipeline as pipeline_mod
-
- with mock.patch.object(resolve_mod, "auto_resolve", return_value={
- "x_handle": "Drake",
- "subreddits": ["DrakeTheType"],
- "github_user": "",
- "github_repos": [],
- "context": "Drake context",
- "category": None,
- "searches_run": 4,
- }), \
- mock.patch.object(resolve_mod, "_has_backend", return_value=True), \
- mock.patch.object(pipeline_mod, "run", side_effect=lambda **kw: _fake_report(kw["topic"])):
- results = self._run_competitor_closure(
- config={"BRAVE_API_KEY": "test-key"},
- competitors=["Drake"],
- mock_flag=False,
- )
-
- self.assertIn("resolved", results[0].artifacts)
- resolved = results[0].artifacts["resolved"]
- self.assertEqual(resolved["entity"], "Drake")
- self.assertEqual(resolved["x_handle"], "Drake")
- self.assertEqual(resolved["subreddits"], ["DrakeTheType"])
- self.assertEqual(resolved["context"], "Drake context")
-
- def test_config_not_mutated_across_sub_runs(self):
- """_auto_resolve_context from entity A must not leak into entity B."""
- from lib import resolve as resolve_mod
- from lib import pipeline as pipeline_mod
-
- captured_contexts: list[str] = []
-
- def fake_resolve(topic, _cfg):
- per_topic = {
- "Drake": {"x_handle": "Drake", "subreddits": [], "github_user": "",
- "github_repos": [], "context": "Drake unique context",
- "category": None, "searches_run": 4},
- "Kendrick Lamar": {"x_handle": "kendricklamar", "subreddits": [],
- "github_user": "", "github_repos": [],
- "context": "Kendrick unique context",
- "category": None, "searches_run": 4},
- }
- return per_topic[topic]
-
- def fake_pipeline_run(**kwargs):
- captured_contexts.append(
- kwargs["config"].get("_auto_resolve_context", "")
- )
- return _fake_report(kwargs["topic"])
+ assert sorted(call.args[0] for call in observed.resolver.call_args_list) == ["PeerOne", "PeerTwo"]
+ for peer, expected in resolutions.items():
+ call = observed.calls[peer]
+ assert call["internal_subrun"] is True
+ for key in ("x_handle", "subreddits", "github_user", "github_repos"):
+ assert call[key] == expected[key]
+ assert call["config"]["_auto_resolve_context"] == expected["context"]
+ assert observed.reports[peer].artifacts["resolved"] == {
+ "entity": peer, "trustpilot_domain": "", **expected,
+ }
+
+
+def test_peer_plan_overrides_conflicting_resolver_values(monkeypatch, tmp_path):
+ plan = {
+ "PeerOne": {
+ "x_handle": "@planned_one", "x_related": ["@planned_friend"],
+ "github_user": "@PlannedOwner", "github_repos": ["plannedowner/project"],
+ "context": "Planned PeerOne context",
+ }
+ }
+ plan_file = tmp_path / "peer-plan.json"
+ plan_file.write_text(json.dumps(plan), encoding="utf-8")
+ observed = run_competitor_cli(
+ monkeypatch, tmp_path,
+ args=["--x-handle", "mainonly", "--subreddits", "MainOnly", "--competitors-plan", str(plan_file)],
+ config={"BRAVE_API_KEY": "dummy-brave-key"}, has_backend=True,
+ resolutions={
+ "PeerOne": {"x_handle": "resolved_one", "subreddits": ["ResolvedOne"],
+ "github_user": "resolvedowner", "github_repos": ["resolvedowner/project"],
+ "context": "Resolved PeerOne context"},
+ "PeerTwo": {},
+ },
+ )
- shared_config = {"BRAVE_API_KEY": "test-key"}
- with mock.patch.object(resolve_mod, "auto_resolve", side_effect=fake_resolve), \
- mock.patch.object(resolve_mod, "_has_backend", return_value=True), \
- mock.patch.object(pipeline_mod, "run", side_effect=fake_pipeline_run):
- self._run_competitor_closure(
- config=shared_config,
- competitors=["Drake", "Kendrick Lamar"],
- mock_flag=False,
- )
+ assert sorted(call.args[0] for call in observed.resolver.call_args_list) == ["PeerOne", "PeerTwo"]
+ peer = observed.calls["PeerOne"]
+ assert peer["x_handle"] == "planned_one"
+ assert peer["x_related"] == ["planned_friend"]
+ assert peer["subreddits"] == ["ResolvedOne"]
+ assert peer["github_user"] == "plannedowner"
+ assert peer["github_repos"] == ["plannedowner/project"]
+ assert peer["config"]["_auto_resolve_context"] == "Planned PeerOne context"
+ assert observed.reports["PeerOne"].artifacts["resolved"]["context"] == "Planned PeerOne context"
+
+
+def test_complete_peer_plan_skips_only_that_peers_resolver(monkeypatch, tmp_path):
+ plan_file = tmp_path / "peer-plan.json"
+ plan_file.write_text(json.dumps({"PeerOne": {"x_handle": "planned_one", "subreddits": ["PlannedOne"]}}))
+ observed = run_competitor_cli(
+ monkeypatch, tmp_path,
+ args=["--x-handle", "mainonly", "--competitors-plan", str(plan_file)],
+ config={"BRAVE_API_KEY": "dummy-brave-key"}, has_backend=True,
+ resolutions={"PeerTwo": {"x_handle": "resolved_two"}},
+ )
- # Each sub-run received its own entity's context — no cross-leak.
- self.assertIn("Drake unique context", captured_contexts)
- self.assertIn("Kendrick unique context", captured_contexts)
- # The shared outer config was not mutated
- self.assertNotIn("_auto_resolve_context", shared_config)
+ assert [call.args[0] for call in observed.resolver.call_args_list] == ["PeerTwo"]
+ assert observed.calls["PeerOne"]["x_handle"] == "planned_one"
+ assert observed.calls["PeerOne"]["subreddits"] == ["PlannedOne"]
+ assert observed.calls["PeerTwo"]["x_handle"] == "resolved_two"
- # --- test helpers -----------------------------------------------------
- def _run_competitor_closure(self, *, config, competitors, mock_flag):
- """Replicate the competitor_runner closure from last30days.main() and
- call it against each competitor. Returns the list of Reports."""
- from lib import pipeline, resolve as resolve_mod
[email protected]("mock_mode,has_backend", [(True, True), (False, False)])
+def test_unavailable_resolution_keeps_peer_defaults(monkeypatch, tmp_path, mock_mode, has_backend):
+ observed = run_competitor_cli(
+ monkeypatch, tmp_path,
+ args=["--x-handle", "mainonly", *(["--mock"] if mock_mode else [])],
+ has_backend=has_backend,
+ )
- class _Args:
- pass
- args = _Args()
- args.mock = mock_flag
- args.web_backend = "auto"
- args.lookback_days = 30
+ observed.resolver.assert_not_called()
+ for peer in ("PeerOne", "PeerTwo"):
+ assert observed.calls[peer]["x_handle"] is None
+ assert observed.calls[peer]["subreddits"] is None
- def runner(entity: str):
- entity_config = dict(config)
- resolved = {
- "entity": entity,
- "x_handle": "",
- "subreddits": [],
- "github_user": "",
- "github_repos": [],
- "context": "",
- }
- if not args.mock and resolve_mod._has_backend(entity_config):
- try:
- r = resolve_mod.auto_resolve(entity, entity_config)
- except Exception as exc:
- sys.stderr.write(
- f"[Competitors] auto_resolve failed for {entity!r}: "
- f"{type(exc).__name__}: {exc}\n"
- )
- r = {}
- resolved["x_handle"] = r.get("x_handle", "") or ""
- resolved["subreddits"] = list(r.get("subreddits") or [])
- resolved["github_user"] = r.get("github_user", "") or ""
- resolved["github_repos"] = list(r.get("github_repos") or [])
- resolved["context"] = r.get("context", "") or ""
- if resolved["context"]:
- entity_config["_auto_resolve_context"] = resolved["context"]
- report = pipeline.run(
- topic=entity,
- config=entity_config,
- depth="default",
- requested_sources=None,
- mock=args.mock,
- x_handle=resolved["x_handle"] or None,
- subreddits=resolved["subreddits"] or None,
- github_user=resolved["github_user"] or None,
- github_repos=resolved["github_repos"] or None,
- web_backend=args.web_backend,
- lookback_days=args.lookback_days,
- internal_subrun=True,
- )
- report.artifacts["resolved"] = resolved
- return report
- return [runner(c) for c in competitors]
+def test_resolver_failure_warns_and_runs_peer_with_defaults(monkeypatch, tmp_path, capsys):
+ observed = run_competitor_cli(
+ monkeypatch, tmp_path,
+ args=["--x-handle", "mainonly"], has_backend=True,
+ resolutions=RuntimeError("dummy resolver offline"),
+ )
-if __name__ == "__main__":
- unittest.main()
+ assert observed.resolver.call_count == 2
+ stderr = capsys.readouterr().err
+ for peer in ("PeerOne", "PeerTwo"):
+ assert f"auto_resolve failed for '{peer}': RuntimeError: dummy resolver offline" in stderr
+ assert observed.calls[peer]["x_handle"] is None
+ assert observed.calls[peer]["subreddits"] is None
+ assert observed.reports[peer].artifacts["resolved"]["entity"] == peer
diff --git a/tests/test_corpus_source.py b/tests/test_corpus_source.py
index c13fb18f..78f563d4 100644
--- a/tests/test_corpus_source.py
+++ b/tests/test_corpus_source.py
@@ -801,14 +801,31 @@ def test_sentinel_injection_cannot_escape_private_block():
assert lines[-1] == render.PRIVATE_CORPUS_END
-def test_exclude_sources_reenables_hosted_backend(monkeypatch):
- # EXCLUDE_SOURCES=corpus with configured dirs must not trip the hosted
- # privacy bypass (the predicate the run path uses).
- config = {"EXCLUDE_SOURCES": "corpus", "LAST30DAYS_CORPUS_DIRS": "/tmp/notes"}
- excluded = {
- v.strip().lower() for v in str(config.get("EXCLUDE_SOURCES") or "").split(",") if v.strip()
- }
- assert "corpus" in excluded
+def test_exclude_sources_reenables_hosted_backend(tmp_path, monkeypatch):
+ notes = tmp_path / "private-notes"
+ notes.mkdir()
+ (notes / "local.md").write_text("Private MCP research notes", encoding="utf-8")
+ config = {"EXCLUDE_SOURCES": "corpus", "LAST30DAYS_CORPUS_DIRS": str(notes)}
+ monkeypatch.setattr(env, "get_config", lambda **_kwargs: config)
+ monkeypatch.setenv("LAST30DAYS_API_KEY", "dummy-hosted-key")
+ monkeypatch.setenv("LAST30DAYS_API_BASE", "https://hosted.example.test")
+ hosted_run = mock.Mock(return_value=0)
+ monkeypatch.setattr("lib.hosted.run_hosted", hosted_run)
+ local_run = mock.Mock(return_value=_privacy_report())
+ monkeypatch.setattr(cli.pipeline, "run", local_run)
+ monkeypatch.setattr(cli.pipeline, "diagnose", lambda *_args, **_kwargs: {"available_sources": ["corpus"]})
+ monkeypatch.setattr(cli.env, "CONFIG_DIR", tmp_path / "config")
+ monkeypatch.setenv("LAST30DAYS_SKIP_PREFLIGHT", "1")
+ monkeypatch.setattr(sys, "argv", [
+ "last30days.py", "MCP servers", "--quick", "--emit=compact", "--save-dir=",
+ ])
+
+ assert cli.main() == 0
+
+ hosted_run.assert_called_once_with(
+ "MCP servers", "quick", emit="compact", save_dir="", save_suffix="",
+ )
+ local_run.assert_not_called()
def test_corpus_notes_never_contain_absolute_paths(tmp_path):
diff --git a/tests/test_creator_first_party.py b/tests/test_creator_first_party.py
index 52d8ea26..c076a86b 100644
--- a/tests/test_creator_first_party.py
+++ b/tests/test_creator_first_party.py
@@ -143,18 +143,29 @@ def test_scoped_exemption_keeps_creator_on_own_platform():
def test_deferred_x_floor_ignores_creator_only_handles():
- """The deferred X prune receives resolved_handles minus the creator sets:
- an X account that merely shares a creator's handle still faces the floor."""
- items = _annotate([
- _shortform_item("x-same-name", "x", "linkuptv", 0.0, {"likes": 0}),
- _shortform_item("x-hit", "x", "someone", 0.4, {"likes": 90, "reposts": 9}),
- ])
- kept = signals.prune_low_relevance(
- items,
- first_party_handles=set(), # creator handles were subtracted out
- first_party_by_source={"instagram": {"linkuptv"}},
- )
- assert "x-same-name" not in [item.item_id for item in kept]
+ raw = [
+ {"id": "collision", "author_handle": "linkuptv", "text": "Chocolate cake recipe",
+ "url": "https://x.com/linkuptv/status/1", "date": "2026-03-15",
+ "engagement": {"likes": 90}},
+ {"id": "subject", "author_handle": "steipete", "text": "Just shipped a new release",
+ "url": "https://x.com/steipete/status/2", "date": "2026-03-15",
+ "engagement": {"likes": 90}},
+ {"id": "relevant", "author_handle": "observer", "text": "steipete research results",
+ "url": "https://x.com/observer/status/3", "date": "2026-03-15",
+ "engagement": {"likes": 90}},
+ ]
+ with patch.object(pipeline, "_retrieve_stream", return_value=(raw, {})), \
+ patch.object(pipeline, "weighted_rrf", wraps=pipeline.weighted_rrf) as fuse:
+ report = pipeline.run(
+ topic="steipete research", x_handle="steipete", ig_creators=["linkuptv"],
+ config={}, depth="quick", mock=True, requested_sources=["x"],
+ web_backend="none", as_of_date="2026-03-31",
+ )
+ assert "linkuptv" in fuse.call_args.kwargs["first_party_handles"]
+ assert {item.item_id for stream in fuse.call_args.args[0].values()
+ for item in stream} == {"subject", "relevant"}
+ assert {item.item_id for candidate in report.ranked_candidates
+ for item in candidate.source_items} == {"subject", "relevant"}
def _raw_ig(item_id, author, text, views):
diff --git a/tests/test_drill_mode.py b/tests/test_drill_mode.py
index 4dd7d6df..21b3b06b 100644
--- a/tests/test_drill_mode.py
+++ b/tests/test_drill_mode.py
@@ -452,15 +452,14 @@ def test_cli_drill_runs_deep_updates_cache_and_can_chain(tmp_path: Path):
assert len(chained[0].artifacts["drill_history"]) == 2
-def test_drill_plan_does_not_gain_jobs_via_company_topic(monkeypatch):
- from lib import pipeline, schema
-
[email protected]("drill", [True, False])
+def test_drill_plan_does_not_gain_jobs_via_company_topic(monkeypatch, drill):
plan = schema.QueryPlan(
intent="general",
freshness_mode="balanced_recent",
cluster_mode="story",
raw_topic="OpenClaw",
- notes=["drill-mode"],
+ notes=["drill-mode"] if drill else [],
subqueries=[
schema.SubQuery(
label="drill",
@@ -471,32 +470,39 @@ def test_drill_plan_does_not_gain_jobs_via_company_topic(monkeypatch):
],
source_weights={"youtube": 1.0},
)
- pipeline._ensure_jobs_in_plan(plan, ["youtube", "jobs"], explicit=False, topic="OpenClaw")
- # Direct call still injects (documenting baseline)...
- assert "jobs" in plan.source_weights
- # ...but run()'s drill gate skips the injection entirely for drill plans;
- # assert the gate condition itself so the contract is pinned.
- assert "drill-mode" in plan.notes
+ monkeypatch.setattr(pipeline, "MOCK_AVAILABLE_SOURCES", ["youtube", "jobs"])
+ report = pipeline.run(
+ topic="OpenClaw", config={}, depth="quick", mock=True,
+ external_plan=schema.to_dict(plan), web_backend="none",
+ as_of_date="2026-07-10",
+ )
+
+ expected = {"youtube"} if drill else {"youtube", "jobs"}
+ assert report.query_plan.subqueries
+ assert all(set(subquery.sources) == expected for subquery in report.query_plan.subqueries)
def test_merge_collapses_exact_url_rediscoveries():
- from lib import pipeline, schema
- import copy
+ base = _report()
+ drill_report = _report(drill=True)
+ old = base.items_by_source["reddit"][0]
+ new = drill_report.items_by_source["reddit"][0]
+ old.body = "Initial rumor about the account restriction."
+ new.body = "Full interview transcript explains revised enforcement and appeal procedures."
+ new.snippet = "Appeals now receive human review."
+ new.metadata = {"top_comments": [{"text": "My appeal was approved."}]}
+ new.engagement = {"score": 321}
- def item(url, body):
- return schema.SourceItem(
- item_id=url, source="reddit", title="t", body=body, url=url,
- published_at="2026-07-01", snippet=body[:20], engagement={"score": 5},
- )
+ merged = pipeline.merge_drill_report(
+ base, drill_report, [base.clusters[0]], target="cluster 1",
+ )
- old = item("https://reddit.com/r/x/1", "original body")
- new = item("https://reddit.com/r/x/1", "enriched body with transcript and much longer text")
- from lib import dedupe
- new_urls = {new.url}
- filtered_old = [i for i in [old] if not (i.url and i.url in new_urls)]
- combined = dedupe.dedupe_items([copy.deepcopy(new), *filtered_old])
- assert len(combined) == 1
- assert combined[0].body.startswith("enriched")
+ retained = [item for item in merged.items_by_source["reddit"] if item.url == old.url]
+ assert len(retained) == 1
+ assert retained[0].body == "Full interview transcript explains revised enforcement and appeal procedures."
+ assert retained[0].snippet == "Appeals now receive human review."
+ assert retained[0].metadata == {"top_comments": [{"text": "My appeal was approved."}]}
+ assert retained[0].engagement == {"score": 321}
def test_write_last_run_returns_false_on_failure(monkeypatch, capsys):
diff --git a/tests/test_first_party_floor.py b/tests/test_first_party_floor.py
index 4fc1e8c5..c1f6b6f1 100644
--- a/tests/test_first_party_floor.py
+++ b/tests/test_first_party_floor.py
@@ -8,7 +8,11 @@ that silently loses the subject's own posts, and no prior test exercised it --
existing supplement-lane tests use single-item batches that trip the rescue.
"""
-from lib import schema, signals
+from unittest.mock import patch
+
+import pytest
+
+from lib import pipeline, schema, signals
def _x_item(item_id: str, author: str, relevance: float, engagement: dict | None = None):
@@ -193,29 +197,31 @@ def test_non_first_party_demotion_survives_the_floor_pass():
# --- Phase 1 / quick-depth wiring (Greptile) --------------------------------
-def test_phase_one_normalize_receives_the_explicit_handles():
- """Quick runs skip Phase 2 entirely, so an exemption reaching only the
- supplement path leaves quick-depth reports discarding the subject's posts."""
- import inspect
- from lib import pipeline
- src = inspect.getsource(pipeline.run)
- assert "explicit_first_party = {" in src, (
- "the user-named handles must be resolved before retrieval, not after"
- )
- assert "first_party_handles=explicit_first_party," in src, (
- "the Phase 1 per-source normalize must receive the exemption"
- )
-
-
-def test_explicit_handles_are_available_before_any_retrieval():
- """The entity-extracted set does not exist until Phase 2; the explicit one
- must be built from run()'s own arguments so Phase 1 can use it."""
- import inspect
- from lib import pipeline
- src = inspect.getsource(pipeline.run)
- build_at = src.index("explicit_first_party = {")
- first_use = src.index("first_party_handles=explicit_first_party,")
- assert build_at < first_use
[email protected]("handle_in_topic", [False, True])
+def test_pipeline_deferred_floor_keeps_subject_and_prunes_unrelated_account(handle_in_topic):
+ topic = "@steipete research" if handle_in_topic else "Peter Steinberger research"
+ raw = [
+ {"id": "subject", "author_handle": "steipete", "text": "Shipped another update",
+ "url": "https://x.com/steipete/status/1", "date": "2026-03-15",
+ "engagement": {"likes": 50}},
+ {"id": "noise", "author_handle": "rando_acct", "text": "Banana bread recipe",
+ "url": "https://x.com/rando_acct/status/2", "date": "2026-03-15",
+ "engagement": {"likes": 50}},
+ {"id": "relevant", "author_handle": "observer", "text": f"{topic} evaluation results",
+ "url": "https://x.com/observer/status/3", "date": "2026-03-15",
+ "engagement": {"likes": 50}},
+ ]
+ with patch.object(pipeline, "_retrieve_stream", return_value=(raw, {})), \
+ patch.object(pipeline, "weighted_rrf", wraps=pipeline.weighted_rrf) as fuse:
+ report = pipeline.run(
+ topic=topic, x_handle=None if handle_in_topic else "steipete",
+ config={}, depth="quick", mock=True, requested_sources=["x"],
+ web_backend="none", as_of_date="2026-03-31",
+ )
+ streams = fuse.call_args.args[0]
+ assert {item.item_id for stream in streams.values() for item in stream} == {"subject", "relevant"}
+ assert {item.item_id for candidate in report.ranked_candidates
+ for item in candidate.source_items} == {"subject", "relevant"}
def test_related_handles_lane_gets_the_exemption():
diff --git a/tests/test_grok_lanes_pipeline.py b/tests/test_grok_lanes_pipeline.py
index b3cfc7e7..3dc3aa82 100644
--- a/tests/test_grok_lanes_pipeline.py
+++ b/tests/test_grok_lanes_pipeline.py
@@ -10,6 +10,7 @@ import inspect
import pytest
+import last30days as cli
from lib import pipeline, schema
@@ -77,16 +78,6 @@ def test_name_lane_failure_does_not_abort_the_run():
assert "NAME-lane" in block
-def test_partial_coverage_is_recorded():
- """One-sided coverage must be visible, not look like thin discussion."""
- src = _supplements_source()
- assert "partial coverage" in src
- assert 'if empty and len(empty) < 3:' in src, (
- "an all-empty result is an ordinary no-results outcome, not partial "
- "coverage; only a mixed result is worth flagging"
- )
-
-
def test_by_lane_does_not_and_the_topic_by_default():
"""A prior defect emptied the from-lane by ANDing the topic into it.
@@ -108,21 +99,78 @@ def test_by_lane_does_not_and_the_topic_by_default():
# --- behavioral: the source-text assertions above cannot catch a crash -------
-def test_partial_coverage_does_not_raise_on_an_empty_x_source():
[email protected]("has_posts", [True, False])
+def test_partial_coverage_does_not_raise_on_an_empty_x_source(monkeypatch, has_posts):
"""Regression: partial coverage was recorded via bundle.record_failure with
the state string "degraded", which is not in SourceOutcome's valid_states.
With zero Phase-1 X items record_failure passes the caller's state straight
through, so it raised ValueError and killed the whole run -- on exactly the
entity topics this feature targets. No source-text assertion could catch
this; only executing the path does."""
- bundle = schema.RetrievalBundle()
- assert not bundle.items_by_source.get("x")
- empty = ["mention"]
- # Mirror the production call: this must not raise.
- bundle.artifacts.setdefault("x_partial_coverage", []).append(
- f"X partial coverage: {', '.join(empty)} lane(s) returned nothing"
+ runtime = schema.ProviderRuntime(
+ reasoning_provider="local", planner_model="", rerank_model="",
+ x_search_backend="grok",
+ )
+ monkeypatch.setattr(pipeline.providers, "resolve_runtime", lambda *_args: (runtime, None))
+ monkeypatch.setattr(pipeline, "available_sources", lambda *_args, **_kwargs: ["x"])
+ monkeypatch.setattr(pipeline.env, "x_backend_chain", lambda _config: ["grok"])
+ monkeypatch.setattr(pipeline, "_retrieve_stream_impl", lambda **_kwargs: ([], {}))
+ lanes = []
+
+ def from_lane(handles, topic, from_date, to_date, **kwargs):
+ lanes.append("by")
+ assert handles == ["steipete"]
+ assert kwargs["and_topic"] is False
+ if not has_posts:
+ return [], False
+ return [{
+ "id": "2087568620465607078",
+ "url": "https://x.com/steipete/status/2087568620465607078",
+ "author_handle": "steipete",
+ "text": "Peter Steinberger released a new agent toolkit.",
+ "date": "2026-08-12",
+ "relevance": 0.9,
+ "engagement": {"likes": 1462, "reposts": 48, "replies": 95},
+ }], False
+
+ def empty_lane(name):
+ def search(*_args, **_kwargs):
+ lanes.append(name)
+ return [], False
+ return search
+
+ monkeypatch.setattr(pipeline.grok_x, "search_handles", from_lane)
+ monkeypatch.setattr(pipeline.grok_x, "search_mentions", empty_lane("mention"))
+ monkeypatch.setattr(pipeline.grok_x, "search_name", empty_lane("name"))
+ report = pipeline.run(
+ topic="Peter Steinberger", config={}, depth="default",
+ requested_sources=["x"], x_handle="steipete", web_backend="none",
+ as_of_date="2026-08-13",
+ external_plan={
+ "intent": "general", "freshness_mode": "balanced_recent",
+ "cluster_mode": "story", "raw_topic": "Peter Steinberger",
+ "source_weights": {"x": 1.0},
+ "subqueries": [{"label": "primary", "search_query": "Peter Steinberger",
+ "ranking_query": "Peter Steinberger", "sources": ["x"]}],
+ },
+ )
+
+ warning = (
+ "X partial coverage: mention, name lane(s) returned nothing; "
+ "the report may show only one side of this entity."
)
- assert bundle.artifacts["x_partial_coverage"]
+ assert lanes == ["by", "mention", "name"]
+ expected_urls = [
+ "https://x.com/steipete/status/2087568620465607078",
+ ] if has_posts else []
+ assert [item.url for item in report.items_by_source["x"]] == expected_urls
+ expected_warnings = [warning] if has_posts else []
+ assert report.artifacts.get("x_partial_coverage", []) == expected_warnings
+ assert [note for note in report.warnings if note.startswith("X partial coverage:")] == expected_warnings
+ assert report.errors_by_source == {}
+ assert report.source_status["x"].state == ("ok" if has_posts else "no-results")
+ assert report.source_status["x"].items_returned == int(has_posts)
+ assert cli._strict_exit_code(report, None, {"LAST30DAYS_STRICT_EXIT": "1"}) == 0
def test_degraded_is_not_a_valid_source_outcome_state():
@@ -131,20 +179,3 @@ def test_degraded_is_not_a_valid_source_outcome_state():
schema.SourceOutcome(
source="x", state="degraded", items_returned=0, attempted=True,
)
-
-
-def test_partial_coverage_is_not_recorded_as_a_source_failure():
- """A one-sided lane result must not mark X partial: PARTIAL is outside
- _STRICT_EXIT_OK_STATES, so wrappers using LAST30DAYS_STRICT_EXIT would exit
- 3 on runs that returned good X coverage."""
- src = _supplements_source()
- # Strip comments: the rationale for NOT using record_failure names it.
- code = "\n".join(
- line for line in src.splitlines() if not line.strip().startswith("#")
- )
- idx = code.index("x_partial_coverage")
- window = code[max(0, idx - 400):idx]
- assert "record_failure" not in window, (
- "partial lane coverage must be a warning, not a source outcome: "
- "record_failure would set X to PARTIAL and trip strict-exit wrappers"
- )
diff --git a/tests/test_grok_x.py b/tests/test_grok_x.py
index e9750714..59073a7d 100644
--- a/tests/test_grok_x.py
+++ b/tests/test_grok_x.py
@@ -719,13 +719,24 @@ def test_empty_result_is_not_reported_as_an_error():
assert "error" not in result
-def test_depth_drives_the_fanout_call_count():
[email protected]("depth, expected_calls", [("quick", 1), ("deep", 4)])
+def test_depth_drives_the_fanout_call_count(monkeypatch, depth, expected_calls):
"""DEPTH_CONFIG was dead: grok returned 10 posts at every depth while
sitting ahead of bird, silently downgrading a deep run."""
- quick = grok_x._fanout_queries("t", "2026-07-14", "2026-08-13", 1)
- deep = grok_x._fanout_queries("t", "2026-07-14", "2026-08-13", 4)
- assert len(quick) == 1 and len(deep) == 4
- assert len(set(deep)) == 4, "fan-out variants must differ or they repeat one result set"
+ queries = []
+
+ def empty_query(query, from_date, to_date, **kwargs):
+ assert (from_date, to_date) == WINDOW
+ assert kwargs["depth"] == depth
+ queries.append(query)
+ return [], "", False
+
+ monkeypatch.setattr(grok_x, "_run_query", empty_query)
+ result = grok_x.search_x("Peter Steinberger", *WINDOW, depth=depth)
+
+ assert result == {"items": []}
+ assert len(queries) == expected_calls
+ assert len(set(queries)) == expected_calls, "fan-out must reach distinct result sets"
def test_fanout_queries_no_phrase_quote_for_place_names():
diff --git a/tests/test_http_fixtures.py b/tests/test_http_fixtures.py
index 970ade6b..59bf3e78 100644
--- a/tests/test_http_fixtures.py
+++ b/tests/test_http_fixtures.py
@@ -270,24 +270,36 @@ def test_post_ranking_cli_enrichment_records_and_replays(
engagement={"postCount": 1} if source == "digg" else {},
metadata={"clusterUrlId": "cluster-1"} if source == "digg" else {},
)
+ enrichment_calls = []
if source == "youtube":
def enrich(items, **_kwargs):
+ enrichment_calls.append(([entry.item_id for entry in items], _kwargs))
items[0].metadata["transcript_snippet"] = "recorded transcript"
monkeypatch.setattr(pipeline.youtube_yt, "backfill_transcripts", enrich)
+ expected_metadata = {"transcript_snippet": "recorded transcript"}
+ expected_kwargs = {"topic": "agents", "depth": "quick", "token": None}
else:
def enrich(items, **_kwargs):
+ enrichment_calls.append(([entry.item_id for entry in items], _kwargs))
items[0].metadata["posts"] = [{"url": "https://x.com/example/status/1"}]
return items
monkeypatch.setattr(pipeline.digg, "enrich_source_items", enrich)
+ expected_metadata = {
+ "clusterUrlId": "cluster-1",
+ "posts": [{"url": "https://x.com/example/status/1"}],
+ }
+ expected_kwargs = {"top_k": 3}
with http.recording_requests(fixture_dir):
recorded = pipeline._finalize_items_by_source(
{source: [item]}, topic="agents", depth="quick",
)
- expected_metadata = recorded[source][0].metadata
+ assert enrichment_calls == [(["item-1"], expected_kwargs)]
+ assert len(recorded[source]) == 1
+ assert recorded[source][0].metadata == expected_metadata
replay_item = schema.SourceItem(
item_id="item-1",
source=source,
@@ -308,7 +320,9 @@ def test_post_ranking_cli_enrichment_records_and_replays(
{source: [replay_item]}, topic="agents", depth="quick",
)
+ assert len(replayed[source]) == 1
assert replayed[source][0].metadata == expected_metadata
+ assert enrichment_calls == [(["item-1"], expected_kwargs)]
def test_record_fixtures_flag_is_dev_only_and_hidden_from_help():
diff --git a/tests/test_pipeline_v3.py b/tests/test_pipeline_v3.py
index c6c6ff97..6dffffab 100644
--- a/tests/test_pipeline_v3.py
+++ b/tests/test_pipeline_v3.py
@@ -376,52 +376,37 @@ class TestSourceFetchCap(unittest.TestCase):
self.assertEqual("deep-research", plan.subqueries[-1].label)
self.assertEqual(["perplexity"], plan.subqueries[-1].sources)
- def test_cap_logic_limits_source_submissions(self):
- """Verify the cap logic skips submissions beyond the limit."""
- subquery_sources = [
- ["x", "reddit", "youtube"],
- ["x", "reddit", "youtube"],
- ["x", "reddit", "youtube"],
- ["x", "reddit", "youtube"],
- ]
- source_fetch_count: dict[str, int] = {}
- submitted: list[str] = []
- for sources in subquery_sources:
- for source in sources:
- source_cap = pipeline.MAX_SOURCE_FETCHES.get(source)
- if source_cap is not None:
- current = source_fetch_count.get(source, 0)
- if current >= source_cap:
- continue
- source_fetch_count[source] = current + 1
- submitted.append(source)
-
- x_count = submitted.count("x")
- reddit_count = submitted.count("reddit")
- self.assertEqual(x_count, 2, f"X should be capped at 2, got {x_count}")
- self.assertEqual(reddit_count, 4, f"Reddit should be uncapped, got {reddit_count}")
-
+ @patch("lib.pipeline._retry_thin_sources")
@patch("lib.pipeline._retrieve_stream")
- def test_mock_run_caps_x_fetches(self, mock_retrieve):
- """Pipeline.run in mock mode should call _retrieve_stream for X at most 2 times."""
- mock_retrieve.side_effect = lambda **kwargs: pipeline._mock_stream_results(
- kwargs["source"], kwargs["subquery"]
- )
- pipeline.run(
- topic="compare iPhone vs Android vs Pixel vs Samsung",
- config={"LAST30DAYS_REASONING_PROVIDER": "gemini"},
- depth="quick",
+ def test_run_caps_primary_x_fetches_without_capping_reddit(self, mock_retrieve, _retry):
+ mock_retrieve.return_value = ([], {})
+ report = pipeline.run(
+ topic="compare iPhone vs Android",
+ config={},
+ depth="default",
requested_sources=["reddit", "x"],
mock=True,
+ web_backend="none",
+ external_plan={
+ "intent": "comparison",
+ "freshness_mode": "balanced_recent",
+ "cluster_mode": "topic",
+ "subqueries": [
+ {"label": label, "search_query": f"phones {label}",
+ "ranking_query": f"phones {label}", "sources": ["x", "reddit"]}
+ for label in ["price", "camera", "battery", "privacy"]
+ ],
+ },
)
- x_calls = [
- call for call in mock_retrieve.call_args_list
- if call.kwargs.get("source") == "x"
- ]
- self.assertLessEqual(
- len(x_calls), 2,
- f"X should be fetched at most 2 times, got {len(x_calls)}",
+ self.assertEqual(4, len(report.query_plan.subqueries))
+ submissions = {(call.kwargs["source"], call.kwargs["subquery"].label)
+ for call in mock_retrieve.call_args_list}
+ self.assertEqual(
+ {("x", "price"), ("x", "camera"),
+ *[("reddit", label) for label in ["price", "camera", "battery", "privacy"]]},
+ submissions,
)
+ self.assertEqual(6, mock_retrieve.call_count)
@patch("lib.pipeline._retrieve_stream")
def test_zero_source_fetch_override_suppresses_capped_source(self, mock_retrieve):
@@ -1551,23 +1536,29 @@ class TestSupplementalSearches(unittest.TestCase):
self.assertIn("timed out", outcome.detail)
self.assertIn("@analyst1", outcome.detail)
+ @patch("lib.bird_x.search_mentions", return_value=[])
@patch("lib.bird_x.search_handles")
@patch("lib.entity_extract.extract_entities")
- def test_supplemental_items_deduplicated_by_url(self, mock_extract, mock_handles):
+ def test_supplemental_items_deduplicated_by_url(self, mock_extract, mock_handles, mock_mentions):
"""Supplemental items with same URL as Phase 1 should not be duplicated."""
mock_extract.return_value = {"x_handles": ["analyst1"], "x_hashtags": [], "reddit_subreddits": []}
- # Return item with same URL as Phase 1
mock_handles.return_value = [
{
"id": "dup1",
- "text": "Same tweet",
+ "text": "AI safety technical analysis from the author",
"url": "https://x.com/analyst1/status/1",
"author_handle": "analyst1",
"date": "2026-03-15",
"engagement": {"likes": 50},
"relevance": 0.8,
"why_relevant": "duplicate",
- }
+ },
+ {
+ "id": "unique2", "text": "New AI safety benchmark results",
+ "url": "https://x.com/analyst1/status/2",
+ "author_handle": "analyst1", "date": "2026-03-16",
+ "engagement": {"likes": 20}, "relevance": 0.8,
+ },
]
bundle = schema.RetrievalBundle()
@@ -1587,15 +1578,16 @@ class TestSupplementalSearches(unittest.TestCase):
mock=False,
rate_limited_sources=set(),
rate_limit_lock=threading.Lock(),
+ x_handle="analyst1",
)
- # Should still have only 1 item (no duplicates)
+ mock_handles.assert_called_once()
+ mock_mentions.assert_called_once()
x_items = bundle.items_by_source.get("x", [])
urls = [item.url for item in x_items]
- self.assertEqual(
- urls.count("https://x.com/analyst1/status/1"), 1,
- f"Duplicate URL found: {urls}",
- )
+ self.assertCountEqual(["https://x.com/analyst1/status/1",
+ "https://x.com/analyst1/status/2"], urls)
+ self.assertIs(original, x_items[0])
@patch("lib.bird_x.search_mentions")
@patch("lib.bird_x.search_handles")
@@ -1903,37 +1895,51 @@ class TestThinSourceRetry(unittest.TestCase):
class TestErrorCleanup(unittest.TestCase):
- """Source errors should be cleared when the source has items from other subqueries."""
-
- def test_error_cleared_when_source_has_items(self):
- """A source that 429'd on one subquery but succeeded on another is not errored."""
- bundle = schema.RetrievalBundle(artifacts={})
- item = schema.SourceItem(
- item_id="x1", source="x", title="A tweet", body="content",
- url="https://x.com/user/status/1",
- )
- bundle.items_by_source["x"] = [item]
- bundle.errors_by_source["x"] = "HTTP 429: Too Many Requests"
-
- # Simulate the cleanup logic from pipeline.run()
- for source in list(bundle.errors_by_source):
- if bundle.items_by_source.get(source):
- del bundle.errors_by_source[source]
-
- self.assertNotIn("x", bundle.errors_by_source,
- "X should not be errored when it has items")
-
- def test_error_kept_when_source_has_no_items(self):
- """A source with zero items should remain in errors_by_source."""
- bundle = schema.RetrievalBundle(artifacts={})
- bundle.errors_by_source["x"] = "HTTP 429: Too Many Requests"
-
- for source in list(bundle.errors_by_source):
- if bundle.items_by_source.get(source):
- del bundle.errors_by_source[source]
-
- self.assertIn("x", bundle.errors_by_source,
- "X should remain errored when it has no items")
+ def _run_with_failed_stream(self, successful_items):
+ def retrieve(**kwargs):
+ if kwargs["subquery"].label == "failed":
+ raise RuntimeError("fixture lane unavailable")
+ return successful_items, {}
+
+ with patch.object(pipeline, "_retrieve_stream", side_effect=retrieve) as retrieval, \
+ patch.object(pipeline, "_retry_thin_sources"):
+ report = pipeline.run(
+ topic="AI safety", config={}, depth="default", mock=True,
+ requested_sources=["x"], web_backend="none", as_of_date="2026-03-17",
+ external_plan={
+ "intent": "exploration", "freshness_mode": "balanced_recent",
+ "cluster_mode": "topic",
+ "subqueries": [
+ {"label": label, "search_query": f"AI safety {label}",
+ "ranking_query": "AI safety", "sources": ["x"]}
+ for label in ["success", "failed"]
+ ],
+ },
+ )
+ self.assertCountEqual(["success", "failed"], [
+ call.kwargs["subquery"].label for call in retrieval.call_args_list])
+ return report
+
+ def test_error_becomes_partial_warning_when_another_stream_succeeds(self):
+ report = self._run_with_failed_stream([{
+ "id": "survivor", "text": "AI safety evaluation results",
+ "url": "https://x.com/researcher/status/1", "author_handle": "researcher",
+ "date": "2026-03-15", "engagement": {"likes": 50},
+ }])
+ self.assertEqual(["survivor"], [item.item_id for item in report.items_by_source["x"]])
+ self.assertNotIn("x", report.errors_by_source)
+ self.assertEqual(schema.PARTIAL, report.source_status["x"].state)
+ self.assertIn("fixture lane unavailable", report.source_status["x"].detail)
+ self.assertIn("Some sources returned partial results (degraded): x", report.warnings)
+ self.assertNotIn("Some sources failed: x", report.warnings)
+
+ def test_error_remains_hard_failure_when_no_stream_returns_items(self):
+ report = self._run_with_failed_stream([])
+ self.assertEqual([], report.items_by_source.get("x", []))
+ self.assertEqual("fixture lane unavailable", report.errors_by_source["x"])
+ self.assertNotEqual(schema.PARTIAL, report.source_status["x"].state)
+ self.assertIn("Some sources failed: x", report.warnings)
+ self.assertNotIn("Some sources returned partial results (degraded): x", report.warnings)
class TestXHandleFlag(unittest.TestCase):
diff --git a/tests/test_polymarket.py b/tests/test_polymarket.py
index ba823d5e..99f18dc8 100644
--- a/tests/test_polymarket.py
+++ b/tests/test_polymarket.py
@@ -466,24 +466,28 @@ def test_parse_polymarket_response_basic():
items = polymarket.parse_polymarket_response(response, topic="AI")
- assert len(items) >= 0
- # Items may be filtered by topic filter
+ assert len(items) == 1
+ assert items[0]["event_id"] == "evt-123"
+ assert items[0]["title"] == "Will AI surpass humans?"
+ assert items[0]["outcome_prices"] == [("Yes", 0.6), ("No", 0.4)]
def test_parse_polymarket_response_filters_closed():
"""Test that closed events are filtered."""
response = {
"events": [
- create_mock_event(closed=False),
- create_mock_event(closed=True),
+ create_mock_event(event_id="open", slug="open-event", closed=False,
+ markets=[create_mock_market(market_id="open-market")]),
+ create_mock_event(event_id="closed", slug="closed-event", closed=True,
+ markets=[create_mock_market(market_id="still-open-market")]),
]
}
items = polymarket.parse_polymarket_response(response)
- # Closed events should be filtered
- # (exact count depends on market filtering)
- assert isinstance(items, list)
+ assert [item["event_id"] for item in items] == ["open"]
+ historical = polymarket.parse_polymarket_response(response, include_closed=True)
+ assert [item["event_id"] for item in historical] == ["open", "closed"]
def test_parse_polymarket_response_empty():
@@ -506,9 +510,8 @@ def test_parse_polymarket_response_market_url():
items = polymarket.parse_polymarket_response(response, topic="test")
- if items: # If not filtered
- assert "url" in items[0]
- assert "polymarket.com" in items[0]["url"]
+ assert len(items) == 1
+ assert items[0]["url"] == "https://polymarket.com/event/test-event-slug"
def test_parse_polymarket_response_engagement():
@@ -518,14 +521,15 @@ def test_parse_polymarket_response_engagement():
title="AI Event",
volume24hr=250000,
liquidity=100000,
+ markets=[{**create_mock_market(liquidity="1234"), "volume24hr": 5678}],
)]
}
items = polymarket.parse_polymarket_response(response, topic="AI")
- if items: # If not filtered
- # Check for volume or liquidity fields
- assert "volume24hr" in items[0] or "liquidity" in items[0] or isinstance(items[0], dict)
+ assert len(items) == 1
+ assert items[0]["volume24hr"] == 250000.0
+ assert items[0]["liquidity"] == 100000.0
def _claude_downtime_response():
@@ -571,20 +575,21 @@ def test_parse_polymarket_response_narrow_subquery_leaks_noise():
def test_engagement_with_volume():
- """Test engagement calculation with volume."""
+ """Missing event metrics fall back to the selected market's values."""
response = {
"events": [create_mock_event(
title="Test",
- volume24hr=500000,
+ volume24hr=0,
+ liquidity=0,
+ markets=[{**create_mock_market(liquidity="6789"), "volume24hr": "1234"}],
)]
}
items = polymarket.parse_polymarket_response(response, topic="test")
- if items:
- engagement = items[0].get("engagement", {})
- # volume24hr should be captured
- assert "volume24hr" in engagement or isinstance(engagement, dict)
+ assert len(items) == 1
+ assert items[0]["volume24hr"] == 1234.0
+ assert items[0]["liquidity"] == 6789.0
# === Tests for noise-word query skipping ===
@@ -620,39 +625,45 @@ def test_expand_queries_all_noise_words_keeps_phrase():
def test_per_item_relevance_floor_drops_zero_items():
"""Items with relevance 0.0 should be dropped even if best item is high."""
- # Simulate the filtering logic directly
- items = [
- {"relevance": 0.85, "title": "Kanye market"},
- {"relevance": 0.45, "title": "Related market"},
- {"relevance": 0.0, "title": "Golf noise"},
- {"relevance": 0.0, "title": "Cycling noise"},
+ events = [
+ create_mock_event(event_id="golf", title="Golf championship"),
+ create_mock_event(event_id="relevant", title="Prediction market"),
+ create_mock_event(event_id="cycling", title="Cycling championship"),
]
- filtered = [i for i in items if i["relevance"] >= 0.10]
- assert len(filtered) == 2
- assert all(i["title"] != "Golf noise" for i in filtered)
+ # A generic topic passes the earlier title gate, leaving the score floor
+ # responsible for removing these otherwise valid events.
+ assert all(polymarket._passes_topic_filter("market", event["title"]) for event in events)
+ items = polymarket.parse_polymarket_response({"events": events}, topic="market")
+ assert [item["event_id"] for item in items] == ["relevant"]
-def test_per_item_relevance_floor_keeps_borderline():
+def test_per_item_relevance_floor_keeps_borderline(monkeypatch):
"""Items at exactly 0.10 should be kept."""
- items = [
- {"relevance": 0.85, "title": "Main market"},
- {"relevance": 0.10, "title": "Borderline market"},
- {"relevance": 0.09, "title": "Below floor"},
+ scores = {"Main market": 1.0, "Borderline market": 0.13, "Below floor": 0.12}
+ monkeypatch.setattr(polymarket, "_compute_text_similarity", lambda _topic, title, _outcomes: scores[title])
+ events = [
+ create_mock_event(
+ event_id=event_id, title=title, volume24hr=0, liquidity=1,
+ markets=[create_mock_market(volume="0", liquidity="1")],
+ )
+ for event_id, title in [("below", "Below floor"), ("main", "Main market"),
+ ("boundary", "Borderline market")]
+ ]
+ items = polymarket.parse_polymarket_response({"events": events}, topic="market")
+ assert [(item["event_id"], item["relevance"]) for item in items] == [
+ ("main", 0.75), ("boundary", 0.10),
]
- filtered = [i for i in items if i["relevance"] >= 0.10]
- assert len(filtered) == 2
- assert filtered[1]["title"] == "Borderline market"
def test_per_item_relevance_floor_no_drops_when_all_high():
"""Nothing dropped when all items are above the floor."""
- items = [
- {"relevance": 0.85, "title": "A"},
- {"relevance": 0.50, "title": "B"},
- {"relevance": 0.30, "title": "C"},
- ]
- filtered = [i for i in items if i["relevance"] >= 0.10]
- assert len(filtered) == 3
+ response = {"events": [
+ create_mock_event(event_id=event_id, title=f"Prediction market {event_id}")
+ for event_id in ["a", "b", "c"]
+ ]}
+ items = polymarket.parse_polymarket_response(response, topic="market")
+ assert [item["event_id"] for item in items] == ["a", "b", "c"]
+ assert all(item["relevance"] >= 0.30 for item in items)
if __name__ == "__main__":
pytest.main([__file__, "-v"])
diff --git a/tests/test_rerank_v3.py b/tests/test_rerank_v3.py
index ebcd0ed3..993ef7dd 100644
--- a/tests/test_rerank_v3.py
+++ b/tests/test_rerank_v3.py
@@ -1,6 +1,7 @@
import unittest
+from unittest.mock import patch
-from lib import rerank, schema
+from lib import pipeline, rerank, schema
def make_candidate(relevance: float) -> schema.Candidate:
@@ -982,12 +983,6 @@ class TestOutOfWindowDemotion(unittest.TestCase):
self.assertEqual(rerank._final_score(undated), rerank._final_score(dated))
def test_stale_cannot_lead_in_final_sort_even_with_dominant_score(self):
- """AE2: stale rerank_score=95 vs in-window rerank_score=10 — stale sorts below.
-
- The 0.35 multiplier alone is not enough: stale 95 * 0.35 ≈ 33 still beats
- fresh 10. The final sort key (the same as pipeline.run's final sort) must
- partition stale below fresh, regardless of individual final_score values.
- """
stale = self._candidate("stale", "2025-10-15", "low")
stale.rerank_score = 95.0
fresh = self._candidate("fresh", "2026-07-20", "high")
@@ -998,18 +993,18 @@ class TestOutOfWindowDemotion(unittest.TestCase):
self.assertGreater(stale.final_score, fresh.final_score)
- sorted_candidates = sorted(
- [stale, fresh],
- key=lambda candidate: (
- 1 if schema.candidate_out_of_window(candidate) else 0,
- -candidate.final_score,
- -(candidate.engagement or -1),
- candidate.candidate_id,
- ),
- )
+ for candidate in [stale, fresh]:
+ candidate.metadata.update(range_from="2026-07-01", range_to="2026-07-31")
+ with patch.object(pipeline, "_retrieve_stream", return_value=([], {})), \
+ patch.object(rerank, "rerank_candidates", side_effect=[[stale, fresh], []]) as rank:
+ report = pipeline.run(
+ topic="video research", config={}, depth="quick", mock=True,
+ requested_sources=["youtube"], web_backend="none", as_of_date="2026-07-31",
+ )
+ self.assertEqual(2, rank.call_count)
self.assertEqual(
["fresh", "stale"],
- [c.candidate_id for c in sorted_candidates],
+ [candidate.candidate_id for candidate in report.ranked_candidates],
)
diff --git a/tests/test_resolved_handles_wiring.py b/tests/test_resolved_handles_wiring.py
index ec14a78b..d1fdab0c 100644
--- a/tests/test_resolved_handles_wiring.py
+++ b/tests/test_resolved_handles_wiring.py
@@ -1,98 +1,70 @@
-"""Auto-discovered X handles must reach resolved_handles.
-
-resolved_handles gates every first-party protection downstream: the entity-miss
-exemption in rerank, FIRST_PARTY_FLOOR, the interaction floor, and the
-retrieval-floor exemption in signals. It was built only from --x-handle,
---github-user and --x-related, so on any run that did not pass --x-handle
-(the overwhelmingly common case) the whole mechanism was inert.
-"""
+"""Supplemental handle discovery reaches the first-party ranking boundaries."""
import inspect
+import threading
+from unittest.mock import patch
-from lib import pipeline
-
+from lib import pipeline, providers, schema
-def _call_supplements(**overrides):
- """Drive _run_supplemental_searches far enough to populate the out-param.
- The function bails early when no X or Reddit dicts are present unless a
- handle was supplied, so an explicit x_handle is the cheapest way to reach
- the resolution block without standing up a full retrieval bundle.
- """
- out: list[str] = []
- kwargs = dict(
- topic="Peter Steinberger steipete",
- bundle=overrides.pop("bundle"),
- plan=overrides.pop("plan"),
- config={},
- depth="default",
- date_range=("2026-07-14", "2026-08-13"),
- runtime=overrides.pop("runtime"),
- mock=True,
- rate_limited_sources=set(),
- rate_limit_lock=overrides.pop("lock"),
- resolved_handles_out=out,
+def _plan():
+ return schema.QueryPlan(
+ intent="exploration", freshness_mode="balanced_recent", cluster_mode="topic",
+ raw_topic="Example research", source_weights={"x": 1.0},
+ subqueries=[schema.SubQuery(label="primary", search_query="Example research",
+ ranking_query="Example research", sources=["x"])],
)
- kwargs.update(overrides)
- pipeline._run_supplemental_searches(**kwargs)
- return out
def test_out_param_is_part_of_the_contract():
- sig = inspect.signature(pipeline._run_supplemental_searches)
- assert "resolved_handles_out" in sig.parameters, (
- "the supplement pass must be able to report the handles it resolved; "
- "without it resolved_handles cannot see auto-discovered subjects"
- )
- assert sig.parameters["resolved_handles_out"].default is None, (
- "the out-param must stay optional so existing callers are unaffected"
- )
-
-
-def test_resolved_handles_includes_supplemental_handles():
- """The merge site must read the supplement pass's output.
-
- Matched on the assignment rather than an exact literal: an earlier version
- pinned "resolved_handles = {" and broke when the construction changed to
- merge the explicit set, proving nothing about behavior either way.
- """
- src = inspect.getsource(pipeline)
- start = src.index("resolved_handles =")
- block = src[start:start + 400]
- assert "supplemental_handles" in block, (
- "resolved_handles is still built without the handles the supplement "
- "pass discovered; auto-discovered subjects stay unprotected"
- )
- assert "explicit_first_party" in block, (
- "the user-named handles must still be part of resolved_handles"
- )
-
-
-def test_supplemental_handles_is_seeded_before_the_call():
- src = inspect.getsource(pipeline)
- assert "supplemental_handles: list[str] = []" in src
- assert "resolved_handles_out=supplemental_handles," in src, (
- "the supplement call must pass the collector it later merges from"
- )
-
-
-def test_handles_are_normalized_and_deduped():
- """Normalization must match resolved_handles' own lstrip/strip/lower form."""
- src = inspect.getsource(pipeline._run_supplemental_searches)
- block = src[src.index("resolved_handles_out is not None"):]
- assert 'lstrip("@")' in block and ".lower()" in block, (
- "handles must be normalized the same way resolved_handles normalizes, "
- "or the set comparison in rerank/_is_first_party will miss them"
- )
- assert "seen" in block, "duplicate handles must not accumulate"
-
-
-def test_population_precedes_the_no_handles_early_return():
- """A run whose lanes cannot execute must still contribute its handles."""
- src = inspect.getsource(pipeline._run_supplemental_searches)
- populate_at = src.index("resolved_handles_out is not None")
- early_return_at = src.index("if not handles and not related_handles:")
- assert populate_at < early_return_at, (
- "handles are surfaced after the early return, so a run with no usable "
- "handle lane would silently contribute nothing to resolved_handles"
- )
+ parameter = inspect.signature(pipeline._run_supplemental_searches).parameters["resolved_handles_out"]
+ assert parameter.default is None
+
+
+def test_collector_normalizes_deduplicates_and_requires_corroboration():
+ output = ["existing"]
+ with patch.object(pipeline.entity_extract, "extract_entities", return_value={
+ "x_handles": ["@ExampleDev", "EXAMPLEDEV", "commentator", "@Existing"],
+ "x_hashtags": [], "reddit_subreddits": [],
+ }), patch.object(pipeline.env, "x_backend_chain", return_value=[]):
+ pipeline._run_supplemental_searches(
+ topic="Example research", bundle=schema.RetrievalBundle(), plan=_plan(),
+ config={}, depth="default", date_range=("2026-03-01", "2026-03-31"),
+ runtime=providers.mock_runtime({}, "default"), mock=False,
+ rate_limited_sources=set(), rate_limit_lock=threading.Lock(),
+ x_handle="@Explicit", x_related=["@Related", "related"],
+ resolved_handles_out=output,
+ )
+ assert output == ["existing", "explicit", "exampledev", "related"]
+
+
+def test_discovered_handles_reach_real_fusion_and_reranking():
+ raw = [
+ {"id": "subject", "text": "A new release shipped today",
+ "author_handle": "ExampleDev", "url": "https://x.com/ExampleDev/status/1",
+ "date": "2026-03-15", "engagement": {"likes": 100}},
+ {"id": "mention", "text": "Example research by ExampleDev is detailed",
+ "author_handle": "observer", "url": "https://x.com/observer/status/2",
+ "date": "2026-03-16", "engagement": {"likes": 20}},
+ ]
+ runtime = providers.mock_runtime({}, "default")
+ with patch.object(pipeline.providers, "resolve_runtime", return_value=(runtime, None)), \
+ patch.object(pipeline, "available_sources", return_value=["x"]), \
+ patch.object(pipeline.env, "x_backend_chain", return_value=[]), \
+ patch.object(pipeline, "_retrieve_stream", return_value=(raw, {})), \
+ patch.object(pipeline, "_run_supplemental_searches", wraps=pipeline._run_supplemental_searches) as supplements, \
+ patch.object(pipeline, "weighted_rrf", wraps=pipeline.weighted_rrf) as fuse, \
+ patch.object(pipeline.rerank, "rerank_candidates", wraps=pipeline.rerank.rerank_candidates) as rank:
+ report = pipeline.run(
+ topic="Example research", config={}, depth="default", mock=False,
+ requested_sources=["x"], web_backend="none", as_of_date="2026-03-31",
+ external_plan=schema.to_dict(_plan()),
+ )
+ assert supplements.call_count == 1
+ assert supplements.call_args.kwargs["resolved_handles_out"] == ["exampledev"]
+ assert "exampledev" in fuse.call_args.kwargs["first_party_handles"]
+ assert "observer" not in fuse.call_args.kwargs["first_party_handles"]
+ assert rank.call_count == 2
+ assert all("exampledev" in call.kwargs["resolved_handles"] for call in rank.call_args_list)
+ assert any(item.author == "ExampleDev" for candidate in report.ranked_candidates
+ for item in candidate.source_items)