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(youtube): report a failed yt-dlp search as an error, not no results (#1195)
Jay Hemnani0f80ea27cf99
diff --git a/changelog.d/+youtube-search-failure.fixed.md b/changelog.d/+youtube-search-failure.fixed.mdnew file mode 100644index 00000000..2304f6e0--- /dev/null+++ b/changelog.d/+youtube-search-failure.fixed.md@@ -0,0 +1 @@+A failed yt-dlp YouTube search (network error, bot check, broken install) is now reported as a YouTube failure in Source Coverage instead of as "no results".diff --git a/skills/last30days/scripts/lib/pipeline.py b/skills/last30days/scripts/lib/pipeline.pyindex a7d850a2..6efa6fc6 100644--- a/skills/last30days/scripts/lib/pipeline.py+++ b/skills/last30days/scripts/lib/pipeline.py@@ -2430,6 +2430,7 @@ def run(print("[Planner] (no subqueries in plan)", file=sys.stderr)bundle = schema.RetrievalBundle(artifacts={"grounding": []})+ deferred_retryable_failures: dict[str, dict[str, Any]] = {}if envelope is not None:# The footer names the X provenance (render._render_stats).bundle.artifacts["x_provenance"] = envelope.provenance@@ -2777,12 +2778,19 @@ def run(if isinstance(artifact, dict) and artifact.get("_source_outcome"):artifact = dict(artifact)outcome_note = artifact.pop("_source_outcome")- bundle.record_failure(- source,- outcome_note["state"],- outcome_note["detail"],- attempted=outcome_note.get("attempted", True),- )+ if (+ source == "youtube"+ and not raw_items+ and _retryable_youtube_outcome(outcome_note)+ ):+ deferred_retryable_failures.setdefault(source, outcome_note)+ else:+ bundle.record_failure(+ source,+ outcome_note["state"],+ outcome_note["detail"],+ attempted=outcome_note.get("attempted", True),+ )if isinstance(artifact, dict) and artifact.get("_source_outcome_detail"):artifact = dict(artifact)lane_state = artifact.pop("_source_outcome_detail_state", None)@@ -2818,6 +2826,19 @@ def run(source, normalized, stream_limit, topic,host_fetched_x=host_fetched_x, protected_authors=explicit_first_party,)+ if isinstance(artifact, dict) and artifact.get("_source_outcome_if_empty"):+ artifact = dict(artifact)+ deferred_outcome = artifact.pop("_source_outcome_if_empty")+ if not normalized:+ if _retryable_youtube_outcome(deferred_outcome):+ deferred_retryable_failures.setdefault(source, deferred_outcome)+ else:+ bundle.record_failure(+ source,+ deferred_outcome["state"],+ deferred_outcome["detail"],+ attempted=deferred_outcome.get("attempted", True),+ )bundle.add_items(subquery.label, source, normalized)if artifact:bundle.artifacts.setdefault("grounding", []).append(artifact)@@ -2847,6 +2868,10 @@ def run(_github_skip_retry = {"corpus"}if _github_person_done or _github_custom_done:_github_skip_retry.add("github")+ pre_retry_counts = {+ source: len(bundle.items_by_source.get(source, []))+ for source in deferred_retryable_failures+ }_retry_thin_sources(topic=topic,bundle=bundle,@@ -2869,6 +2894,16 @@ def run(first_party_by_source=creator_first_party,run_started=run_started,)+ for source, outcome in deferred_retryable_failures.items():+ if (+ source in bundle.errors_by_source+ or len(bundle.items_by_source.get(source, [])) > pre_retry_counts[source]+ ):+ continue+ bundle.record_failure(+ source, outcome["state"], outcome["detail"],+ attempted=outcome.get("attempted", True),+ )# Reclassify partial failures as DEGRADED instead of silently dropping them.# A source that 429'd on one subquery but succeeded on another is not a hard@@ -4057,6 +4092,15 @@ def _is_transient_error(exc: Exception) -> bool:return _mentions_status(str(exc), r"5\d\d", _SERVER_ERROR_PHRASES)+def _retryable_youtube_outcome(outcome: dict[str, Any]) -> bool:+ state = outcome.get("state")+ if state in (health.TIMEOUT, health.UNREACHABLE):+ return True+ return state == health.ERROR and _is_transient_error(+ SourceRunError(str(outcome.get("detail") or ""))+ )++def _topic_handle_mentions(topic: str) -> set[str]:"""@mentions in the topic, which are real X handles.@@ -4755,6 +4799,8 @@ def _retry_thin_sources(normalized = _apply_reddit_stream_keepers(source, normalized, settings["per_stream_limit"], topic)+ if not normalized and isinstance(artifact, dict):+ outcome_note = outcome_note or artifact.get("_source_outcome_if_empty")return source, normalized, outcome_note, (detail_note, detail_state)retryable = [s for s in thin_sources if s not in rate_limited_sources]@@ -5532,6 +5578,7 @@ def _retrieve_stream_impl(yt_query = raw_topic or subquery.search_queryresult = Noneyoutube_failure: str | None = None+ recovered_failure: str | None = None# ScrapeCreators key (when present) is the default-on backup tier: it# powers the per-video transcript fallback, the SC search fallback, and# comment enrichment. None when no key, which keeps everything keyless.@@ -5558,6 +5605,9 @@ def _retrieve_stream_impl()if result.get("error"):youtube_failure = str(result["error"])+ elif result.get("items") and youtube_failure:+ recovered_failure = youtube_failure+ youtube_failure = Noneexcept Exception as exc:youtube_failure = str(exc)result = None@@ -5573,6 +5623,14 @@ def _retrieve_stream_impl(state = youtube_yt.classify_run_failure(youtube_failure)attempted = state != schema.SKIPPED_UNCONFIGUREDreturn items, _outcome_artifact(state, youtube_failure, attempted=attempted)+ if recovered_failure:+ state = youtube_yt.classify_run_failure(recovered_failure)+ attempted = state != schema.SKIPPED_UNCONFIGURED+ return items, {+ "_source_outcome_if_empty": _outcome_artifact(+ state, recovered_failure, attempted=attempted,+ )["_source_outcome"]+ }return items, {}if source == "tiktok":# Use raw_topic so expand_tiktok_queries() generates diverse variantsdiff --git a/skills/last30days/scripts/lib/youtube_yt.py b/skills/last30days/scripts/lib/youtube_yt.pyindex 9bfb10f5..0927f79b 100644--- a/skills/last30days/scripts/lib/youtube_yt.py+++ b/skills/last30days/scripts/lib/youtube_yt.py@@ -548,16 +548,19 @@ def search_youtube(return publishedstdout = result.stdout- if ssh_host and result.returncode != 0 and not stdout.strip():+ if result.returncode != 0 and not stdout.strip():stderr_first = (result.stderr or "").strip().splitlines()first_line = stderr_first[0] if stderr_first else "(no stderr)"- _log(- f"YouTube search via SSH host {ssh_host!r} failed "- f"(rc={result.returncode}): {first_line}"- )- published = _publish(- {"items": [], "error": f"SSH routing to {ssh_host!r} failed: {first_line}"},- )+ if ssh_host:+ _log(+ f"YouTube search via SSH host {ssh_host!r} failed "+ f"(rc={result.returncode}): {first_line}"+ )+ error = f"SSH routing to {ssh_host!r} failed: {first_line}"+ else:+ _log(f"YouTube search failed (rc={result.returncode}): {first_line}")+ error = f"yt-dlp search failed: {first_line}"+ published = _publish({"items": [], "error": error})return publishedif not stdout.strip():_log("YouTube search returned 0 results")diff --git a/tests/test_youtube_yt.py b/tests/test_youtube_yt.pyindex b9d97b8e..aed56443 100644--- a/tests/test_youtube_yt.py+++ b/tests/test_youtube_yt.py@@ -1158,9 +1158,358 @@ class TestYouTubeSearchTimeoutAndCache(unittest.TestCase):def setUp(self):youtube_yt.reset_search_cache()- def _fake_result(self, stdout: str = "", returncode: int = 0):+ def _fake_result(self, stdout: str = "", returncode: int = 0, stderr: str = ""):from lib.subproc import SubprocResult- return SubprocResult(returncode=returncode, stdout=stdout, stderr="")+ return SubprocResult(returncode=returncode, stdout=stdout, stderr=stderr)++ def test_search_nonzero_exit_reports_error_not_empty(self):+ stderr = (+ "ERROR: [youtube] abc123: Sign in to confirm you're not a bot. "+ "Use --cookies-from-browser or --cookies for the authentication.\n"+ )+ with mock.patch.object(youtube_yt, "is_ytdlp_installed", return_value=True), \+ mock.patch.object(+ youtube_yt.subproc, "run_with_timeout",+ return_value=self._fake_result(returncode=1, stderr=stderr),+ ):+ out = youtube_yt.search_youtube("Vuori", "2026-06-01", "2026-07-01")+ self.assertEqual(out.get("items"), [])+ self.assertIn("not a bot", out.get("error") or "")+ self.assertEqual(+ youtube_yt.classify_run_failure(out["error"]),+ youtube_yt.health.RATE_LIMITED,+ )++ def test_sc_fallback_results_clear_ytdlp_search_failure(self):+ from lib import pipeline, schema+ video = {"id": "abc123", "title": "Vuori review", "url": "https://www.youtube.com/watch?v=abc123"}+ with mock.patch.object(pipeline, "which", return_value="/usr/bin/yt-dlp"), \+ mock.patch.object(+ youtube_yt, "search_and_transcribe",+ return_value={"items": [], "error": "yt-dlp search failed: ERROR: Sign in to confirm you're not a bot"},+ ), \+ mock.patch.object(youtube_yt, "search_youtube_sc", return_value={"items": [video]}), \+ mock.patch.object(pipeline.env, "is_youtube_comments_available", return_value=False):+ items, artifact = pipeline._retrieve_stream_impl(+ topic="Vuori",+ subquery=schema.SubQuery(label="q", search_query="Vuori", ranking_query="Vuori", sources=["youtube"]),+ source="youtube",+ config={"SCRAPECREATORS_API_KEY": "k"},+ depth="default",+ date_range=("2026-06-01", "2026-07-01"),+ runtime=schema.ProviderRuntime(reasoning_provider="mock", planner_model="mock", rerank_model="mock"),+ mock=False,+ )+ self.assertEqual(items, [video])+ self.assertNotIn("_source_outcome", artifact)+ self.assertEqual(+ artifact["_source_outcome_if_empty"]["state"],+ youtube_yt.health.RATE_LIMITED,+ )++ def _run_sc_fallback_report(+ self, video, *, initial_empty=False, depth="quick", retry_video=None,+ search_error="yt-dlp search failed: Sign in to confirm you're not a bot",+ ):+ from lib import pipeline++ retrieve = pipeline._retrieve_stream+ calls = 0++ def retrieve_live_youtube(*args, **kwargs):+ nonlocal calls+ calls += 1+ if initial_empty and calls == 1:+ return [], {}+ return retrieve(*args, **{**kwargs, "mock": False})++ plan = {+ "intent": "general",+ "freshness_mode": "balanced_recent",+ "cluster_mode": "story",+ "subqueries": [{+ "label": "primary",+ "search_query": "Vuori",+ "ranking_query": "Vuori",+ "sources": ["youtube"],+ }],+ "source_weights": {"youtube": 1.0},+ }+ sc_results = [{"items": [video]}]+ if retry_video is not None:+ sc_results.append({"items": [retry_video]})+ with mock.patch.object(pipeline, "which", return_value="/usr/bin/yt-dlp"), \+ mock.patch.object(pipeline, "_retrieve_stream", side_effect=retrieve_live_youtube), \+ mock.patch.object(+ youtube_yt, "search_and_transcribe",+ return_value={"items": [], "error": search_error},+ ), \+ mock.patch.object(+ youtube_yt, "search_youtube_sc",+ side_effect=sc_results if retry_video is not None else None,+ return_value=sc_results[0],+ ) as sc_search, \+ mock.patch.object(pipeline.env, "is_youtube_comments_available", return_value=False):+ report = pipeline.run(+ topic="Vuori",+ config={"SCRAPECREATORS_API_KEY": "k"},+ depth=depth,+ requested_sources=["youtube"],+ mock=True,+ external_plan=plan,+ as_of_date="2026-07-01",+ )+ return report, sc_search.call_count++ def test_sc_fallback_without_usable_video_preserves_search_failure_in_coverage(self):+ from lib import render, schema++ report, _ = self._run_sc_fallback_report({+ "id": "abc123",+ "title": "Vuori review",+ "url": "https://www.youtube.com/watch?v=abc123",+ "date": "2026-05-01",+ "transcript_snippet": "",+ })++ self.assertEqual(report.items_by_source.get("youtube"), [])+ self.assertEqual(report.source_status["youtube"].state, schema.RATE_LIMITED)+ self.assertIn("not a bot", report.source_status["youtube"].detail)+ self.assertIn("rate-limited", "\n".join(render._render_source_coverage(report)))++ def test_sc_fallback_with_usable_video_reports_recovered_coverage(self):+ from lib import health, render++ report, _ = self._run_sc_fallback_report({+ "id": "abc123",+ "title": "Vuori review",+ "url": "https://www.youtube.com/watch?v=abc123",+ "date": "2026-06-15",+ "transcript_snippet": "",+ })++ self.assertEqual(report.source_status["youtube"].state, health.OK)+ self.assertEqual(len(report.items_by_source["youtube"]), 1)+ self.assertEqual("\n".join(render._render_source_coverage(report)),+ "## Source Coverage\n\n- YouTube: 1 item")++ def test_sc_fallback_retry_without_usable_video_preserves_search_failure(self):+ from lib import schema++ report, _ = self._run_sc_fallback_report({+ "id": "abc123",+ "title": "Vuori review",+ "url": "https://www.youtube.com/watch?v=abc123",+ "date": "2026-05-01",+ "transcript_snippet": "",+ }, initial_empty=True, depth="default")++ self.assertEqual(report.source_status["youtube"].state, schema.RATE_LIMITED)+ self.assertIn("not a bot", report.source_status["youtube"].detail)++ def test_sc_fallback_transient_failure_recovers_on_thin_retry(self):+ from lib import health++ old = {+ "id": "old", "title": "Vuori archive", "date": "2026-05-01",+ "url": "https://www.youtube.com/watch?v=old", "transcript_snippet": "",+ }+ recent = {+ "id": "recent", "title": "Vuori review", "date": "2026-06-15",+ "url": "https://www.youtube.com/watch?v=recent", "transcript_snippet": "",+ }+ for search_error in (+ "yt-dlp search failed: connection reset",+ "yt-dlp search failed: HTTP Error 503",+ ):+ with self.subTest(search_error=search_error):+ report, calls = self._run_sc_fallback_report(+ old, depth="default", retry_video=recent,+ search_error=search_error,+ )+ self.assertEqual(calls, 2)+ self.assertEqual(report.source_status["youtube"].state, health.OK)+ self.assertEqual(+ [item.item_id for item in report.items_by_source["youtube"]],+ ["recent"],+ )++ def test_sc_fallback_transient_failure_survives_unusable_thin_retry(self):+ from lib import health++ old = {+ "id": "old", "title": "Vuori archive", "date": "2026-05-01",+ "url": "https://www.youtube.com/watch?v=old", "transcript_snippet": "",+ }+ report, calls = self._run_sc_fallback_report(+ old, depth="default", retry_video=old,+ search_error="yt-dlp search failed: connection reset",+ )++ self.assertEqual(calls, 2)+ self.assertEqual(report.source_status["youtube"].state, health.UNREACHABLE)+ self.assertIn("connection reset", report.source_status["youtube"].detail)++ def test_sc_fallback_quick_transient_failure_remains_visible(self):+ from lib import health++ old = {+ "id": "old", "title": "Vuori archive", "date": "2026-05-01",+ "url": "https://www.youtube.com/watch?v=old", "transcript_snippet": "",+ }+ report, calls = self._run_sc_fallback_report(+ old, search_error="yt-dlp search failed: connection reset",+ )++ self.assertEqual(calls, 1)+ self.assertEqual(report.source_status["youtube"].state, health.UNREACHABLE)+ self.assertIn("youtube", report.errors_by_source)++ def test_unrecovered_stream_remains_partial_when_another_stream_has_videos(self):+ from lib import health, pipeline++ fresh = [{+ "id": f"recent-{index}", "title": title,+ "url": f"https://www.youtube.com/watch?v=recent-{index}",+ "date": "2026-06-15", "relevance": 1.0,+ } for index, title in enumerate((+ "Vuori earnings report", "Vuori trail running gear review",+ "Vuori store opening Toronto",+ ))]+ stale = [{+ "id": "old", "title": "Vuori archive", "date": "2026-05-01",+ "url": "https://www.youtube.com/watch?v=old", "transcript_snippet": "",+ }]++ def retrieve_stream(*_args, **kwargs):+ if kwargs["subquery"].label == "usable":+ return fresh, {}+ return stale, {"_source_outcome_if_empty": {+ "state": health.UNREACHABLE,+ "detail": "yt-dlp search failed: connection reset",+ "attempted": True,+ }}++ plan = {+ "intent": "general", "freshness_mode": "balanced_recent",+ "cluster_mode": "story",+ "subqueries": [+ {"label": label, "search_query": "Vuori", "ranking_query": "Vuori", "sources": ["youtube"]}+ for label in ("usable", "stale")+ ],+ "source_weights": {"youtube": 1.0},+ }+ with mock.patch.object(pipeline, "_retrieve_stream", side_effect=retrieve_stream) as search:+ report = pipeline.run(+ topic="Vuori", config={}, depth="default",+ requested_sources=["youtube"], mock=True,+ external_plan=plan, as_of_date="2026-07-01",+ )++ self.assertEqual(search.call_count, 2)+ self.assertEqual(report.source_status["youtube"].state, health.PARTIAL)+ self.assertEqual(report.source_status["youtube"].items_returned, 3)+ self.assertIn("connection reset", report.source_status["youtube"].detail)++ def test_sc_fallback_auth_and_rate_limit_failures_skip_thin_retry(self):+ from lib import health++ old = {+ "id": "old", "title": "Vuori archive", "date": "2026-05-01",+ "url": "https://www.youtube.com/watch?v=old", "transcript_snippet": "",+ }+ recent = {+ "id": "recent", "title": "Vuori review", "date": "2026-06-15",+ "url": "https://www.youtube.com/watch?v=recent", "transcript_snippet": "",+ }+ for search_error, state in (+ ("yt-dlp search failed: Sign in to confirm you're not a bot", health.RATE_LIMITED),+ ("yt-dlp search failed: login required", health.AUTH_FAILED),+ ):+ with self.subTest(state=state):+ report, calls = self._run_sc_fallback_report(+ old, depth="default", retry_video=recent, search_error=search_error,+ )+ self.assertEqual(calls, 1)+ self.assertEqual(report.source_status["youtube"].state, state)++ def _run_keyless_search_report(self, responses, *, depth="default"):+ from lib import pipeline++ retrieve = pipeline._retrieve_stream++ def retrieve_live_youtube(*args, **kwargs):+ return retrieve(*args, **{**kwargs, "mock": False})++ plan = {+ "intent": "general", "freshness_mode": "balanced_recent",+ "cluster_mode": "story",+ "subqueries": [{+ "label": "primary", "search_query": "Vuori",+ "ranking_query": "Vuori", "sources": ["youtube"],+ }],+ "source_weights": {"youtube": 1.0},+ }+ with mock.patch.object(pipeline, "which", return_value="/usr/bin/yt-dlp"), \+ mock.patch.object(pipeline, "_retrieve_stream", side_effect=retrieve_live_youtube), \+ mock.patch.object(youtube_yt, "search_and_transcribe", side_effect=responses) as search, \+ mock.patch.object(youtube_yt, "search_youtube_sc") as sc_search:+ report = pipeline.run(+ topic="Vuori", config={}, depth=depth,+ requested_sources=["youtube"], mock=True,+ external_plan=plan, as_of_date="2026-07-01",+ )+ return report, search.call_count, sc_search.call_count++ def test_keyless_transient_search_failure_recovers_on_thin_retry(self):+ from lib import health++ recent = {+ "id": "recent", "title": "Vuori review", "date": "2026-06-15",+ "url": "https://www.youtube.com/watch?v=recent",+ }+ report, calls, sc_calls = self._run_keyless_search_report([+ {"items": [], "error": "yt-dlp search failed: connection reset"},+ {"items": [recent]},+ ])++ self.assertEqual((calls, sc_calls), (2, 0))+ self.assertEqual(report.source_status["youtube"].state, health.OK)+ self.assertEqual([item.item_id for item in report.items_by_source["youtube"]], ["recent"])++ def test_keyless_transient_search_failure_survives_failed_thin_retry(self):+ from lib import health++ failure = {"items": [], "error": "yt-dlp search failed: connection reset"}+ report, calls, sc_calls = self._run_keyless_search_report([failure, failure])++ self.assertEqual((calls, sc_calls), (2, 0))+ self.assertEqual(report.source_status["youtube"].state, health.UNREACHABLE)+ self.assertIn("connection reset", report.source_status["youtube"].detail)++ def test_keyless_auth_and_rate_limit_failures_skip_thin_retry(self):+ from lib import health++ for error, state in (+ ("yt-dlp search failed: Sign in to confirm you're not a bot", health.RATE_LIMITED),+ ("yt-dlp search failed: login required", health.AUTH_FAILED),+ ):+ with self.subTest(state=state):+ report, calls, sc_calls = self._run_keyless_search_report([+ {"items": [], "error": error},+ {"items": [{"id": "recent", "title": "Vuori review", "date": "2026-06-15"}]},+ ])+ self.assertEqual((calls, sc_calls), (1, 0))+ self.assertEqual(report.source_status["youtube"].state, state)++ def test_search_zero_exit_with_no_output_is_clean_empty(self):+ with mock.patch.object(youtube_yt, "is_ytdlp_installed", return_value=True), \+ mock.patch.object(+ youtube_yt.subproc, "run_with_timeout",+ return_value=self._fake_result(),+ ):+ out = youtube_yt.search_youtube("Vuori", "2026-06-01", "2026-07-01")+ self.assertEqual(out, {"items": []})def test_search_timeout_reports_timeout_error_not_empty(self):with mock.patch.object(youtube_yt, "is_ytdlp_installed", return_value=True), \