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(render): keep scraped ## headings out of evidence blocks

Trevin Chow4c7b87b86d6e
diff --git a/changelog.d/874.fixed.md b/changelog.d/874.fixed.md
new file mode 100644
index 00000000..d426914e
--- /dev/null
+++ b/changelog.d/874.fixed.md
@@ -0,0 +1 @@
+Scraped evidence text can no longer inject structural `##` markdown headings into the EVIDENCE FOR SYNTHESIS block — continuation lines stay indented and leading ATX markers are escaped.
diff --git a/skills/last30days/scripts/lib/render.py b/skills/last30days/scripts/lib/render.py
index 8a4f0c0d..fae62f28 100644
--- a/skills/last30days/scripts/lib/render.py
+++ b/skills/last30days/scripts/lib/render.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import json
import pathlib
+import re
from collections import Counter
from datetime import date
from urllib.parse import urlparse
@@ -1602,16 +1603,21 @@ def render_full(report: schema.Report) -> str:
if item.container:
lines.append(f" *{item.container}*")
if item.snippet:
- lines.append(f" {item.snippet[:500]}")
+ lines.append(
+ f" {_format_untrusted_evidence(item.snippet, 500, continuation_indent=' ')}"
+ )
# Top comments for Reddit, YouTube, TikTok, HackerNews.
top_comments = item.metadata.get("top_comments", [])
if top_comments and isinstance(top_comments[0], dict):
vote_label = _vote_label_for(item.source)
for tc in top_comments[:3]:
- excerpt = tc.get("excerpt", tc.get("text", ""))[:200]
+ excerpt = tc.get("excerpt", tc.get("text", ""))
tc_score = tc.get("score", "")
attribution = _comment_attribution(item.source, tc.get("author"))
- lines.append(f" Top comment {attribution} ({tc_score} {vote_label}): {excerpt}")
+ lines.append(
+ f" Top comment {attribution} ({tc_score} {vote_label}): "
+ f"{_format_untrusted_evidence(excerpt, 200, continuation_indent=' ')}"
+ )
# Digg: inline X-post quotes attached to the cluster.
for post in _digg_posts_for(item, limit=3):
lines.append(f" > {_format_digg_quote(post)}")
@@ -1620,18 +1626,24 @@ def render_full(report: schema.Report) -> str:
if insights:
lines.append(" Insights:")
for ins in insights[:3]:
- lines.append(f" - {ins[:200]}")
+ lines.append(
+ f" - {_format_untrusted_evidence(ins, 200, continuation_indent=' ')}"
+ )
# Transcript highlights for YouTube
highlights = item.metadata.get("transcript_highlights", [])
if highlights:
lines.append(" Highlights (auto-generated transcript; may contain transcription errors):")
for hl in highlights[:5]:
- lines.append(f' - "{hl[:200]}"')
+ lines.append(
+ f' - "{_format_untrusted_evidence(hl, 200, continuation_indent=" ")}"'
+ )
# Full transcript snippet for YouTube
transcript = item.metadata.get("transcript_snippet", "")
if transcript and len(transcript) > 100:
lines.append(f" <details><summary>Transcript ({len(transcript.split())} words; auto-generated — may contain transcription errors)</summary>")
- lines.append(f" {transcript[:5000]}")
+ lines.append(
+ f" {_format_untrusted_evidence(transcript, 5000, continuation_indent=' ')}"
+ )
lines.append(" </details>")
# Polymarket outcome prices and market details
outcome_prices = item.metadata.get("outcome_prices") or []
@@ -1741,7 +1753,10 @@ def render_context(report: schema.Report, cluster_limit: int = 6) -> str:
]
lines.append(f" - {' | '.join(detail_parts)}")
if candidate.snippet:
- lines.append(f" Evidence: {_truncate(candidate.snippet, 180)}")
+ lines.append(
+ f" Evidence: "
+ f"{_format_untrusted_evidence(candidate.snippet, 180, continuation_indent=' ')}"
+ )
corpus_section = _render_corpus_section(report)
if corpus_section:
lines.extend(["", *corpus_section])
@@ -1815,7 +1830,9 @@ def render_brief(report: schema.Report, cluster_limit: int = 8) -> str:
if not candidate:
continue
if candidate.snippet:
- lines.append(f"- {_truncate(candidate.snippet, 280)}")
+ lines.append(
+ f"- {_format_untrusted_evidence(candidate.snippet, 280, continuation_indent=' ')}"
+ )
explanation = _format_explanation(candidate)
if explanation:
lines.append(f" _Why: {explanation}_")
@@ -2020,24 +2037,29 @@ def _render_candidate(
if explanation:
lines.append(f" - Why: {explanation}")
if candidate.snippet:
- lines.append(f" - Evidence: {_truncate(candidate.snippet, 360)}")
+ lines.append(
+ f" - Evidence: {_format_untrusted_evidence(candidate.snippet, 360)}"
+ )
for tc in _top_comments_list(primary):
excerpt = tc.get("excerpt") or tc.get("text") or ""
score = tc.get("score", "")
vote_label = _vote_label_for(primary.source) if primary else "upvotes"
source = primary.source if primary else None
attribution = _comment_attribution(source, tc.get("author"))
- lines.append(f" - {attribution} ({score} {vote_label}): {_truncate(excerpt.strip(), 240)}")
+ lines.append(
+ f" - {attribution} ({score} {vote_label}): "
+ f"{_format_untrusted_evidence(excerpt.strip(), 240)}"
+ )
for post in _digg_posts_for(primary):
lines.append(f" - {_format_digg_quote(post)}")
insight = _comment_insight(primary)
if insight:
- lines.append(f" - Insight: {_truncate(insight, 220)}")
+ lines.append(f" - Insight: {_format_untrusted_evidence(insight, 220)}")
highlights = _transcript_highlights(primary)
if highlights:
lines.append(" - Highlights (auto-generated transcript; may contain transcription errors):")
for hl in highlights:
- lines.append(f' - "{_truncate(hl, 200)}"')
+ lines.append(f' - "{_format_untrusted_evidence(hl, 200)}"')
return lines
@@ -2965,7 +2987,10 @@ def _render_best_takes(
crowd_tag = " +crowd" if crowd_boost >= 5.0 else ""
score_tag = f"(fun:{candidate.fun_score:.0f}{crowd_tag})"
reason = f" -- {candidate.fun_explanation}" if candidate.fun_explanation and candidate.fun_explanation != "heuristic-fallback" else ""
- lines.append(f'- "{_truncate(text, 280)}" -- {attribution} {score_tag}{reason}')
+ lines.append(
+ f'- "{_format_untrusted_evidence(text, 280, continuation_indent=" ")}" '
+ f"-- {attribution} {score_tag}{reason}"
+ )
return lines
@@ -3040,7 +3065,10 @@ def _render_top_comments(
attribution = _comment_attribution(cand.source, tc.get("author"))
url = tc.get("url") or cand.url or ""
url_part = f" — {url}" if url else ""
- lines.append(f'- "{_truncate(body, 240)}" — {attribution} ({score} {vote_label}){url_part}')
+ lines.append(
+ f'- "{_format_untrusted_evidence(body, 240, continuation_indent=" ")}" '
+ f"— {attribution} ({score} {vote_label}){url_part}"
+ )
return lines
@@ -3049,3 +3077,44 @@ def _truncate(text: str, limit: int) -> str:
if len(text) <= limit:
return text
return text[: limit - 3].rstrip() + "..."
+
+
+_ATX_HEADING_PREFIX = re.compile(r"^(#{1,6})(\s|$)")
+
+
+def _escape_atx_heading_prefix(line: str) -> str:
+ """Neutralize leading ATX heading markers so scraped text cannot mint sections."""
+ stripped = line.lstrip()
+ if not stripped:
+ return line
+ leading = line[: len(line) - len(stripped)]
+ match = _ATX_HEADING_PREFIX.match(stripped)
+ if not match:
+ return line
+ hashes = match.group(1)
+ rest = stripped[len(hashes) :]
+ return f"{leading}{'\\#' * len(hashes)}{rest}"
+
+
+def _format_untrusted_evidence(
+ text: str,
+ limit: int,
+ *,
+ continuation_indent: str = " ",
+) -> str:
+ """Truncate scraped text and keep it from injecting markdown structure.
+
+ Multi-line snippets previously broke out of the `` - Evidence:`` indent
+ so a bare ``##`` from a jobs page became a sibling of engine section
+ headings inside the EVIDENCE FOR SYNTHESIS block (#874). Continuation
+ lines stay indented (CommonMark ATX headings need ≤3 leading spaces), and
+ leading ``#`` runs are escaped as defense in depth.
+ """
+ truncated = _truncate(text, limit)
+ if not truncated:
+ return truncated
+ lines = truncated.splitlines()
+ safe: list[str] = [_escape_atx_heading_prefix(lines[0])]
+ for line in lines[1:]:
+ safe.append(continuation_indent + _escape_atx_heading_prefix(line))
+ return "\n".join(safe)
diff --git a/tests/test_render_v3.py b/tests/test_render_v3.py
index f2f6460c..1e660b88 100644
--- a/tests/test_render_v3.py
+++ b/tests/test_render_v3.py
@@ -1190,6 +1190,65 @@ class TranscriptCaveatTests(unittest.TestCase):
)
+class TestUntrustedEvidenceSanitization(unittest.TestCase):
+ """Scraped markdown must not mint structural ## headings in evidence (#874)."""
+
+ def test_format_untrusted_evidence_indents_and_escapes_headings(self):
+ raw = (
+ "Sales Operations Key Account Manager at Traeger Grills · JobsRadar\n"
+ "\n"
+ "Jobs› Companies› Traeger Grills\n"
+ "\n"
+ "## About this Sales Operations Key Account Manager role at Traeger Grills\n"
+ "\n"
+ "Traeger Grills · Onsite · Salt Lake City, UT"
+ )
+ formatted = render._format_untrusted_evidence(raw, 360)
+ self.assertNotRegex(formatted, r"(?m)^## ")
+ self.assertIn(r"\#\# About this Sales Operations", formatted)
+ # Continuation lines stay under the Evidence bullet indent.
+ for line in formatted.splitlines()[1:]:
+ self.assertTrue(line.startswith(" ") or line == " ")
+
+ def test_render_candidate_evidence_has_no_column_zero_heading(self):
+ item = schema.SourceItem(
+ item_id="j1",
+ source="jobs",
+ title="Sales Operations Key Account Manager",
+ body="body",
+ url="https://jobs-radar.com/job/example",
+ published_at="2026-07-01",
+ date_confidence="high",
+ engagement={},
+ snippet=(
+ "Sales Operations role\n\n"
+ "## About this Sales Operations Key Account Manager role at Traeger Grills\n"
+ "Welcome To The Traegerhood"
+ ),
+ )
+ candidate = schema.Candidate(
+ candidate_id="c1",
+ item_id=item.item_id,
+ source="jobs",
+ title=item.title,
+ url=item.url,
+ snippet=item.snippet,
+ subquery_labels=["primary"],
+ native_ranks={"jobs": 1},
+ local_relevance=1.0,
+ freshness=1,
+ engagement=1,
+ source_quality=1.0,
+ rrf_score=1.0,
+ sources=["jobs"],
+ source_items=[item],
+ )
+ text = "\n".join(render._render_candidate(candidate, "1."))
+ self.assertIn(" - Evidence:", text)
+ self.assertNotRegex(text, r"(?m)^## ")
+ self.assertIn(r"\#\# About this Sales Operations", text)
+
+
if __name__ == "__main__":
unittest.main()