fix(summarizer): ignore future-dated rows; require a score floor for the fallback

Two leaks let low-value items reach the operator brief:

- get_recent_news() only bounded the window from below, so event
  listings dated days ahead (wiseye.org, +3d/+6d) were re-fetched,
  re-graded and re-billed every interval for a week. The window is now
  (now - N, now]; NULL timestamps stay excluded as before.
- select_graded() returned ordered[:fallback_top] with no floor, so a
  15/100 event listing entered the brief as 'low'. New
  SUMMARY_FALLBACK_MIN_SCORE (default 40, clamped 0-100) filters the
  fallback set; when nothing clears it the reduce phase gets empty DATA
  and writes the empty-brief response. The fallback WARNING now logs the
  floor.

Docs: .env.example and docs/news.md document the new knob and window.
This commit is contained in:
backend 2026-11-13 21:56:46 -05:00
commit 57c0444a69
7 changed files with 311 additions and 23 deletions

View file

@ -177,6 +177,35 @@ def test_fallback_top_zero_sends_empty_data(harness, monkeypatch):
assert not any(row[4] for row in _score_rows(db_log))
def test_fallback_below_the_floor_sends_empty_data(harness, monkeypatch, caplog):
"""Every block scores under the floor (max 92 < 93): no fallback at all."""
calls, db_log = harness
monkeypatch.setenv("SUMMARY_MIN_SCORE", "95")
monkeypatch.setenv("SUMMARY_FALLBACK_MIN_SCORE", "93")
with caplog.at_level("WARNING"):
summarizer.summarize_news()
reduce_prompt = [prompt for kind, prompt in calls if kind == "json"][0]
assert "Score: 92" not in reduce_prompt
assert reduce_prompt.rstrip().endswith("DATA:")
assert not any(row[4] for row in _score_rows(db_log))
assert any("fallback_min_score=93" in rec.message for rec in caplog.records)
def test_fallback_floor_is_logged_in_the_warning(harness, monkeypatch, caplog):
calls, _ = harness
monkeypatch.setenv("SUMMARY_MIN_SCORE", "95")
monkeypatch.setenv("SUMMARY_FALLBACK_TOP", "3")
monkeypatch.setenv("SUMMARY_FALLBACK_MIN_SCORE", "40")
with caplog.at_level("WARNING"):
summarizer.summarize_news()
warnings = [rec.message for rec in caplog.records if rec.levelname == "WARNING"]
assert any(
"fallback to top 3 with score >= 40 (max score 92)" in msg for msg in warnings
), warnings
def test_score_insert_failure_does_not_abort_the_run(harness, monkeypatch):
calls, db_log = harness