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(competitors): refuse a comparison headed by a competitor when the main topic fails (#1117)

Ilia Alshanetsky571b51756a59
diff --git a/changelog.d/+comparison-main-topic-failure.fixed.md b/changelog.d/+comparison-main-topic-failure.fixed.md
new file mode 100644
index 00000000..df6bac36
--- /dev/null
+++ b/changelog.d/+comparison-main-topic-failure.fixed.md
@@ -0,0 +1 @@
+A comparison run whose main topic fails now exits with an error instead of silently promoting a competitor to be the report's subject. `run_competitor_fanout` drops a failed sub-run, and the render treats the first surviving entry as the subject, so a main topic that raised while two or more peers succeeded produced a complete-looking comparison headed by a peer, saved under that peer's slug, with the requested topic unmentioned. Entities that were dropped are also recorded as a report warning, so a narrower comparison than requested is visible rather than silent.
diff --git a/skills/last30days/scripts/last30days.py b/skills/last30days/scripts/last30days.py
index 36f41276..a3dec51b 100644
--- a/skills/last30days/scripts/last30days.py
+++ b/skills/last30days/scripts/last30days.py
@@ -556,6 +556,17 @@ def comparison_topic(entity_reports: list[tuple[str, schema.Report]]) -> str:
return " vs ".join(label for label, _ in entity_reports)
+def comparison_label_key(label: str) -> str:
+ """Normalize an entity label for duplicate detection.
+
+ Comparison labels double as keys in the fan-out's results dict, so two
+ entities differing only in case, surrounding space, or a repeated space
+ collide there while still looking distinct on the command line. Spaces
+ are collapsed, never stripped: "Open AI" stays distinct from "OpenAI".
+ """
+ return " ".join(label.split()).casefold()
+
+
def compute_save_path_display(save_dir: str, topic: str, suffix: str, emit: str) -> str:
"""Compute the user-friendly save path string that will be shown in the footer.
@@ -3997,6 +4008,33 @@ def _main(
)
return 2
+ # run_competitor_fanout keys its results by label, so two
+ # submissions sharing one collapse to a single report while the
+ # returned list still carries two entries. That yields a
+ # comparison of an entity against itself, and it hides a failed
+ # main topic from the survivor check below: the duplicate peer's
+ # report answers for the label the main run was supposed to fill.
+ distinct_peers: list[str] = []
+ claimed_labels = {comparison_label_key(topic)}
+ for peer in discovered:
+ key = comparison_label_key(peer)
+ if key in claimed_labels:
+ sys.stderr.write(
+ f"[Competitors] Dropping {peer!r}: duplicates the main "
+ "topic or an earlier peer.\n"
+ )
+ continue
+ claimed_labels.add(key)
+ distinct_peers.append(peer)
+ if not distinct_peers:
+ sys.stderr.write(
+ f"[Competitors] No peer distinct from {topic!r} remains; "
+ "there is nothing to compare against. Pass "
+ "--competitors-list with distinct entities.\n"
+ )
+ return 2
+ discovered = distinct_peers
+
sys.stderr.write(
f"[Competitors] Comparing: {topic} vs " + " vs ".join(discovered) + "\n"
)
@@ -4105,6 +4143,25 @@ def _main(
competitors=discovered,
competitor_runner=_competitor_runner,
)
+ # run_competitor_fanout drops a failed sub-run from the list, and
+ # the render takes entity_reports[0] as the comparison's subject.
+ # Without this check, a main topic that raised while >=2 peers
+ # succeeded silently promoted a competitor to be the subject: the
+ # report was headed by that peer, saved under its slug, and the
+ # topic the user actually asked about went unmentioned.
+ survived = {label for label, _ in entity_reports}
+ dropped = [
+ label for label in (topic, *discovered) if label not in survived
+ ]
+ if topic not in survived:
+ progress.end_processing()
+ sys.stderr.write(
+ f"[Competitors] The main topic {topic!r} failed; "
+ f"{len(entity_reports)} competitor sub-run(s) survived. "
+ "Refusing to render a comparison headed by a competitor. "
+ "Check the warnings above.\n"
+ )
+ return 1
if len(entity_reports) < 2:
progress.end_processing()
sys.stderr.write(
@@ -4114,6 +4171,14 @@ def _main(
)
return 1
report = entity_reports[0][1]
+ if dropped:
+ # A narrower comparison than the user asked for is a result
+ # they need to see, not a silent substitution.
+ report.warnings.append(
+ "Comparison is incomplete: "
+ f"{len(dropped)} of {len(discovered) + 1} entities failed and "
+ f"were dropped ({', '.join(dropped)})."
+ )
else:
entity_reports = None
report = _main_runner()
diff --git a/skills/last30days/scripts/lib/fanout.py b/skills/last30days/scripts/lib/fanout.py
index 2ccc4ad9..18c2fb78 100644
--- a/skills/last30days/scripts/lib/fanout.py
+++ b/skills/last30days/scripts/lib/fanout.py
@@ -46,6 +46,12 @@ def run_competitor_fanout(
Ordered list of (entity_name, Report) tuples for runs that succeeded.
Empty list if every run raised; the caller decides how to surface
partial-failure modes.
+
+ ``main_topic`` is NOT guaranteed to be present: a main run that raised
+ is dropped like any other. Since the render treats element 0 as the
+ comparison's subject, a caller must verify ``main_topic`` survived
+ before using the list, or it will silently head the report with a
+ competitor.
"""
if not competitors:
report = main_runner()
diff --git a/skills/last30days/scripts/lib/render.py b/skills/last30days/scripts/lib/render.py
index 0cf07964..ab9a1f90 100644
--- a/skills/last30days/scripts/lib/render.py
+++ b/skills/last30days/scripts/lib/render.py
@@ -1645,6 +1645,19 @@ def render_comparison_multi_context(
if resolved_block:
lines.extend(resolved_block)
lines.append("")
+ # render_context surfaces warnings for a single-entity run; omitting them
+ # here left context mode the one supported output where a dropped entity
+ # or a failed source is invisible. Placed above the per-entity sections so
+ # it survives tail truncation, matching render_comparison_multi.
+ aggregated_warnings = [
+ f"[{label}] {warning}"
+ for label, report in entity_reports
+ for warning in report.warnings
+ ]
+ if aggregated_warnings:
+ lines.append("Warnings:")
+ lines.extend(f"- {warning}" for warning in aggregated_warnings)
+ lines.append("")
for label, report in entity_reports:
evidence_report = schema.without_sources(report, {"corpus"})
requested_clusters = evidence_report.clusters[:cluster_limit]
diff --git a/tests/test_cli_competitors.py b/tests/test_cli_competitors.py
index edb47ff5..cf859e9a 100644
--- a/tests/test_cli_competitors.py
+++ b/tests/test_cli_competitors.py
@@ -3,10 +3,18 @@
from __future__ import annotations
import io
+import sys
import unittest
from contextlib import redirect_stderr
+from unittest import mock
import last30days as cli
+from lib import fanout
+
+
+def _fake_report(topic: str):
+ """Duck-typed Report stand-in; the guard runs before any field is read."""
+ return type("R", (), {"topic": topic, "warnings": []})()
def _parse(*argv: str):
@@ -128,3 +136,92 @@ class CompetitorsCliTests(unittest.TestCase):
if __name__ == "__main__":
unittest.main()
+
+
+class CompetitorMainTopicFailureTests(unittest.TestCase):
+ """The CLI layer above run_competitor_fanout.
+
+ The fan-out drops a failed sub-run from its list, and the render takes
+ element 0 as the comparison's subject. Nothing between them checked that
+ the main topic survived, so a main run that raised while >=2 peers
+ succeeded produced a complete-looking comparison headed by a peer, saved
+ under that peer's slug, with the user's topic absent.
+ """
+
+ def _run(self, surviving_labels):
+ surviving = [(label, _fake_report(label)) for label in surviving_labels]
+ argv = [
+ "last30days", "OpenAI",
+ "--competitors-list", "Anthropic,xAI",
+ "--mock", "--emit=json",
+ ]
+ err = io.StringIO()
+ # last30days imports fanout locally inside _main, so patch the
+ # source module rather than an attribute on the CLI module.
+ with mock.patch.object(
+ fanout, "run_competitor_fanout", return_value=surviving
+ ), mock.patch.object(sys, "argv", argv), redirect_stderr(err):
+ rc = cli.main()
+ return rc, err.getvalue()
+
+ def test_main_topic_failure_is_not_a_competitor_promotion(self):
+ rc, err = self._run(["Anthropic", "xAI"])
+ self.assertEqual(1, rc)
+ self.assertIn("main topic 'OpenAI' failed", err)
+ self.assertIn("Refusing to render a comparison", err)
+
+
+class CompetitorDuplicateLabelTests(unittest.TestCase):
+ """run_competitor_fanout keys its results by label, so a peer sharing the
+ main topic's label collapsed both submissions onto one report while the
+ returned list still had two entries: a self-comparison whose failed main
+ run the survivor check could not see."""
+
+ def _run(self, peers):
+ seen: dict[str, list[str]] = {}
+
+ def _capture(**kwargs):
+ seen["competitors"] = list(kwargs["competitors"])
+ # One survivor trips the <2 guard, so nothing renders from the
+ # duck-typed report. The assertion is on what fan-out received.
+ return [(kwargs["main_topic"], _fake_report(kwargs["main_topic"]))]
+
+ argv = [
+ "last30days", "OpenAI",
+ "--competitors-list", peers,
+ "--mock", "--emit=json",
+ ]
+ err = io.StringIO()
+ with mock.patch.object(
+ fanout, "run_competitor_fanout", side_effect=_capture
+ ), mock.patch.object(sys, "argv", argv), redirect_stderr(err):
+ rc = cli.main()
+ return rc, err.getvalue(), seen.get("competitors")
+
+ def test_peer_equal_to_main_topic_is_dropped(self):
+ _rc, err, competitors = self._run("OpenAI,Anthropic")
+ self.assertEqual(["Anthropic"], competitors)
+ self.assertIn("Dropping 'OpenAI'", err)
+
+ def test_duplicate_match_ignores_case_and_surrounding_space(self):
+ _rc, err, competitors = self._run(" OPENAI ,Anthropic")
+ self.assertEqual(["Anthropic"], competitors)
+ self.assertIn("Dropping", err)
+
+ def test_differently_worded_peer_is_kept(self):
+ # Normalization collapses whitespace runs and folds case; it does not
+ # strip spaces. "Open AI" stays a distinct entity from "OpenAI",
+ # because merging those would silently drop a real peer.
+ _rc, _err, competitors = self._run("Open AI,Anthropic")
+ self.assertEqual(["Open AI", "Anthropic"], competitors)
+
+ def test_repeated_peer_is_dropped(self):
+ _rc, _err, competitors = self._run("Anthropic,anthropic,xAI")
+ self.assertEqual(["Anthropic", "xAI"], competitors)
+
+ def test_all_peers_duplicate_main_topic_aborts(self):
+ rc, err, competitors = self._run("openai, OpenAI ")
+ self.assertEqual(2, rc)
+ self.assertIsNone(competitors, "fan-out must not run with no peers")
+ self.assertIn("No peer distinct from 'OpenAI'", err)
+
diff --git a/tests/test_render_comparison_multi.py b/tests/test_render_comparison_multi.py
index 8783abb7..957a105a 100644
--- a/tests/test_render_comparison_multi.py
+++ b/tests/test_render_comparison_multi.py
@@ -146,6 +146,31 @@ class RenderComparisonMultiTests(unittest.TestCase):
with self.assertRaises(ValueError):
render.render_comparison_multi([])
+ def test_context_emit_carries_warnings(self):
+ # render_context shows warnings for a single entity; comparison
+ # context mode dropped them, so --emit=context was the one supported
+ # output where a dropped entity or failed source was invisible.
+ report_a = _build_report("OpenAI", ["GPT-5 drop"])
+ report_b = _build_report("Anthropic", ["Claude 4.7"])
+ report_a.warnings.append(
+ "Comparison is incomplete: 1 of 3 entities failed and were dropped (xAI)."
+ )
+ report_b.warnings.append("Exa returned 0 results")
+ out = render.render_comparison_multi_context(
+ [("OpenAI", report_a), ("Anthropic", report_b)]
+ )
+ self.assertIn("Warnings:", out)
+ self.assertIn("[OpenAI] Comparison is incomplete", out)
+ self.assertIn("[Anthropic] Exa returned 0 results", out)
+ # Above the per-entity sections so it survives tail truncation.
+ self.assertLess(out.index("Warnings:"), out.index("## OpenAI"))
+
+ def test_context_emit_omits_empty_warnings_block(self):
+ out = render.render_comparison_multi_context(
+ [("OpenAI", _build_report("OpenAI", ["GPT-5 drop"]))]
+ )
+ self.assertNotIn("Warnings:", out)
+
def test_context_emit(self):
reports = [
("OpenAI", _build_report("OpenAI", ["GPT-5 drop"])),