Article pages: structured "Why it belongs" editorial read

Per Codex — make /a/<id> feel like Upbeat Bytes has editorial judgment, not just
a summary wrapper. Trust-building, short, not an essay.

* article_summaries gains what_happened / why_matters / why_belongs (+ migration).
* summarize.explain_article: a separate, fallback-able LLM pass producing three
  short notes (parsed from a labelled WHAT/MATTERS/BELONGS format). generate_summary
  now stores them alongside the summary, and tops up older summaries on next view.
  get_explanation returns them only when all three are present.
* API: share_page + /api/summary expose the explanation.
* share.py: renders the three-part section (accent rule) when complete; otherwise
  the single "Why it's here" reason line is the calm fallback. The page polls and
  swaps in both the summary and the section as they cache.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
jay
2026-06-09 20:05:26 -04:00
parent 9befbffd94
commit 337dc3f901
6 changed files with 192 additions and 21 deletions
+3 -1
View File
@@ -21,6 +21,8 @@ def test_generate_caches_summary(tmp_path, monkeypatch):
c = _db(tmp_path / "t.sqlite3")
monkeypatch.setattr(summarize, "_fetch_text", lambda url: "Full article body about voles.")
monkeypatch.setattr(summarize, "summarize_article", lambda client, t, s, b: "Our own short summary.")
monkeypatch.setattr(summarize, "explain_article",
lambda client, t, s, b: {"what_happened": "w", "why_matters": "m", "why_belongs": "b"})
# avoid constructing a real LLM client
monkeypatch.setattr(summarize.LocalModelClient, "from_env", classmethod(lambda cls: SimpleNamespace(model="m")))
out = summarize.generate_summary(c, 1)
@@ -50,7 +52,7 @@ def test_summary_endpoint_pending_then_ready(tmp_path, monkeypatch):
first = tc.get("/api/summary/1").json()
assert first["status"] == "pending"
# TestClient runs the background task; the next call sees the cache
assert tc.get("/api/summary/1").json() == {"status": "ready", "summary": "Cached summary."}
assert tc.get("/api/summary/1").json() == {"status": "ready", "summary": "Cached summary.", "explanation": None}
# and the share page now embeds it (no pending poll)
html = tc.get("/a/1").text
assert "Cached summary." in html