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(hackernews): link HN comment quotes to the comment, not the article (#1259)

Matt Van Horn6df866c66c58
diff --git a/changelog.d/+hn-comment-permalinks.fixed.md b/changelog.d/+hn-comment-permalinks.fixed.md
new file mode 100644
index 00000000..2549dd52
--- /dev/null
+++ b/changelog.d/+hn-comment-permalinks.fixed.md
@@ -0,0 +1 @@
+Hacker News comment quotes now link to the comment itself (or the HN discussion page when the comment id is missing) instead of the story's article URL.
diff --git a/skills/last30days/scripts/lib/hackernews.py b/skills/last30days/scripts/lib/hackernews.py
index b78b96f7..a2f130de 100644
--- a/skills/last30days/scripts/lib/hackernews.py
+++ b/skills/last30days/scripts/lib/hackernews.py
@@ -275,7 +275,7 @@ def parse_hackernews_response(response: Dict[str, Any], query: str = "") -> List
# Article URL vs HN discussion URL
article_url = hit.get("url") or ""
- hn_url = f"https://news.ycombinator.com/item?id={object_id}"
+ hn_url = _hn_item_url(object_id)
# Relevance: blend Algolia rank with token-overlap content matching
rank_score = max(0.3, 1.0 - (i * 0.02)) # 1.0 -> 0.3 over 35 items
@@ -304,6 +304,11 @@ def parse_hackernews_response(response: Dict[str, Any], query: str = "") -> List
return items
+def _hn_item_url(item_id: Any) -> str:
+ """HN discussion/comment permalink for an item id."""
+ return f"https://news.ycombinator.com/item?id={item_id}"
+
+
def _fetch_item_comments(object_id: str, max_comments: int = 5) -> Dict[str, Any]:
"""Fetch top-level comments for a story from Algolia items endpoint.
@@ -333,13 +338,18 @@ def _fetch_item_comments(object_id: str, max_comments: int = 5) -> Dict[str, Any
comments = []
insights = []
+ # A quote links to where it was said: the comment's own permalink, else the
+ # story's discussion page -- never the article URL.
+ discussion_url = _hn_item_url(object_id)
for c in real_comments[:max_comments]:
text = _strip_html(c.get("text", ""))
excerpt = text[:300] + "..." if len(text) > 300 else text
+ comment_id = c.get("id")
comments.append({
"author": c.get("author", ""),
"text": excerpt,
"points": c.get("points"),
+ "url": _hn_item_url(comment_id) if comment_id else discussion_url,
})
# First sentence as insight
first_sentence = text.split(". ")[0].split("\n")[0][:200]
diff --git a/skills/last30days/scripts/lib/render.py b/skills/last30days/scripts/lib/render.py
index 1e9d3706..e2fe253e 100644
--- a/skills/last30days/scripts/lib/render.py
+++ b/skills/last30days/scripts/lib/render.py
@@ -2451,9 +2451,17 @@ def _render_candidate(
if score is not None and score != ""
else ""
)
+ # HN comment lines carry their own link: the candidate URL above is
+ # the article, not where the quote was said.
+ hn_link = (
+ _comment_link(primary, tc)
+ if primary is not None and primary.source == "hackernews"
+ else ""
+ )
lines.append(
f" - {attribution}{vote_part}: "
f"{_format_untrusted_evidence(excerpt.strip(), 240)}"
+ + (f" — {hn_link}" if hn_link else "")
)
for post in _digg_posts_for(primary):
lines.append(f" - {_format_digg_quote(post)}")
@@ -3633,6 +3641,26 @@ def _comment_attribution(source: str | None, author: str | None) -> str:
return f"{prefix}{author}" if prefix else author
+def _comment_link(item: schema.SourceItem, comment: dict, fallback: str = "") -> str:
+ """Link for a quoted comment: where it was said, never the HN article.
+
+ HN items carry the article as their URL, so HN comments fall back to the
+ item's discussion page (``hn_url`` metadata, else built from a numeric item
+ id) instead of ``fallback``. Other sources keep ``fallback``.
+ """
+ if comment.get("url"):
+ return str(comment["url"])
+ if item.source != "hackernews":
+ return fallback or ""
+ hn_url = (item.metadata or {}).get("hn_url")
+ if hn_url:
+ return str(hn_url)
+ item_id = str(item.item_id or "")
+ if item_id.isdigit():
+ return f"https://news.ycombinator.com/item?id={item_id}"
+ return ""
+
+
def _top_comments_list(
item: schema.SourceItem | None, limit: int = 3, min_score: int | None = None
) -> list[dict]:
@@ -3919,7 +3947,7 @@ def _render_top_comments(
score = tc.get("score", "")
vote_label = _vote_label_for(item.source)
attribution = _comment_attribution(item.source, tc.get("author"))
- url = tc.get("url") or cand.url or ""
+ url = _comment_link(item, tc, fallback=cand.url)
url_part = f" — {url}" if url else ""
vote_part = (
f" ({score} {vote_label})"
diff --git a/tests/test_hackernews.py b/tests/test_hackernews.py
index d1f5398a..ebd35904 100644
--- a/tests/test_hackernews.py
+++ b/tests/test_hackernews.py
@@ -550,7 +550,13 @@ def test_pipeline_hackernews_stream_attaches_top_comments(mock_request):
by_id = {item["id"]: item for item in items}
assert by_id["1"]["top_comments"] == [
- {"author": "alice", "text": "Great launch.", "points": 7}
+ {
+ "author": "alice",
+ "text": "Great launch.",
+ "points": 7,
+ # No Algolia child id, so the link is the story's discussion page.
+ "url": "https://news.ycombinator.com/item?id=1",
+ }
]
assert by_id["1"]["comment_insights"] == ["Great launch."]
item_urls = [
diff --git a/tests/test_hackernews_comment_floor.py b/tests/test_hackernews_comment_floor.py
index 5853acdc..2968607d 100644
--- a/tests/test_hackernews_comment_floor.py
+++ b/tests/test_hackernews_comment_floor.py
@@ -331,3 +331,91 @@ def test_best_takes_uses_hn_excerpt_retained_by_fused_candidate():
assert any(
call.args == ("hackernews", 93) for call in normalized_vote.call_args_list
)
+
+
+# --- Comment permalinks (R7/R8): a quote links to where it was said. -------
+
+ARTICLE_URL = "https://example.test/thing"
+
+
+def _fetched_hn_item(children):
+ """Run Algolia children through fetch -> normalize, like the live path."""
+ raw = _hn_item()
+ with patch("lib.hackernews.http.request") as request:
+ request.return_value = {"children": children}
+ raw["top_comments"] = hackernews._fetch_item_comments("42")["comments"]
+ return normalize._normalize_hackernews("hackernews", raw, 0, FROM_DATE, TO_DATE)
+
+
+def _comment_lines(rendered, *needles):
+ return [
+ line for line in rendered.splitlines() if any(n in line for n in needles)
+ ]
+
+
+def _render_comment_paths(hn):
+ candidate = _candidate("hackernews", hn)
+ report = _report(hn, candidate)
+ return {
+ "top_comments": "\n".join(render._render_top_comments(report)),
+ "cluster": "\n".join(render._render_candidate(candidate, prefix="1.")),
+ }
+
+
+def test_hn_comment_with_id_links_its_own_permalink():
+ hn = _fetched_hn_item(
+ [
+ {"id": 4242, "author": "alice", "text": "The single clearest explanation I have read.", "points": None},
+ {"id": 4243, "author": "bob", "text": "Counterpoint: the benchmark skips the hard cases.", "points": None},
+ ]
+ )
+ assert hn.metadata["top_comments"][0]["url"] == "https://news.ycombinator.com/item?id=4242"
+ for path, rendered in _render_comment_paths(hn).items():
+ (line,) = _comment_lines(rendered, "clearest explanation")
+ assert "https://news.ycombinator.com/item?id=4242" in line, f"{path}: {line}"
+ assert ARTICLE_URL not in line, f"{path}: {line}"
+
+
+def test_hn_comment_without_id_links_the_discussion_page():
+ hn = _fetched_hn_item(
+ [
+ {"author": "alice", "text": "The single clearest explanation I have read.", "points": None},
+ {"author": "bob", "text": "Counterpoint: the benchmark skips the hard cases.", "points": None},
+ ]
+ )
+ for path, rendered in _render_comment_paths(hn).items():
+ lines = _comment_lines(rendered, "clearest explanation", "hard cases")
+ assert len(lines) == 2, f"{path}:\n{rendered}"
+ for line in lines:
+ assert "https://news.ycombinator.com/item?id=42" in line, f"{path}: {line}"
+ assert ARTICLE_URL not in line, f"{path}: {line}"
+
+
+def test_pre_fix_hn_item_falls_back_to_hn_url_metadata_not_article():
+ """An item normalized before comments carried URLs still links the discussion."""
+ raw = _hn_item()
+ raw["hn_url"] = "https://news.ycombinator.com/item?id=4200"
+ hn = normalize._normalize_hackernews("hackernews", raw, 0, FROM_DATE, TO_DATE)
+ assert all("url" not in c for c in hn.metadata["top_comments"])
+ assert hn.url == ARTICLE_URL
+ for path, rendered in _render_comment_paths(hn).items():
+ lines = _comment_lines(rendered, "clearest explanation", "hard cases")
+ assert len(lines) == 2, f"{path}:\n{rendered}"
+ for line in lines:
+ assert "https://news.ycombinator.com/item?id=4200" in line, f"{path}: {line}"
+ assert ARTICLE_URL not in line, f"{path}: {line}"
+
+
+def test_hn_comment_retained_by_non_hn_candidate_links_hn_not_candidate_url():
+ reddit = normalize._normalize_reddit("reddit", _reddit_item(), 0, FROM_DATE, TO_DATE)
+ hn = _fetched_hn_item(
+ [
+ {"id": 4242, "author": "dave", "text": "A retained HN comment with its own link.", "points": None},
+ ]
+ )
+ fused = _candidate("reddit", reddit)
+ fused.source_items = [reddit, hn]
+ rendered = "\n".join(render._render_top_comments(_report(reddit, fused)))
+ (line,) = _comment_lines(rendered, "retained HN comment")
+ assert "https://news.ycombinator.com/item?id=4242" in line
+ assert fused.url not in line
diff --git a/tests/test_registers.py b/tests/test_registers.py
index a1c845de..05b4e0e0 100644
--- a/tests/test_registers.py
+++ b/tests/test_registers.py
@@ -210,8 +210,9 @@ def test_default_register_is_byte_identical_when_omitted(monkeypatch):
# comment-rendering changes, the quiet footer (no outcome text, no
# ## Source Errors in compact), and the comments pool reading every
# floor-clearing cluster, and the canonical boundary's inline-citation
- # wording (no tool-contract override claim).
- "86e7903a3f4e401025681126ed6f932d65c3a752444f0cbd2e89fb83e7e7a414"
+ # wording (no tool-contract override claim), and HN cluster-evidence
+ # comment lines carrying the comment's own link.
+ "946f97c3c37cca4dc8f2d687482ea510fb240a2b51587cc2dde19d33a768636b"
)