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

@ -54,6 +54,7 @@ BATCH_SIZE = int(os.getenv("BATCH_SIZE", "50"))
DEFAULT_SUMMARY_MIN_SCORE = 60
DEFAULT_SUMMARY_TOP_N = 12
DEFAULT_SUMMARY_FALLBACK_TOP = 3
DEFAULT_SUMMARY_FALLBACK_MIN_SCORE = 40
def _env_int(name: str, default: int) -> int:
@ -67,12 +68,15 @@ def _env_int(name: str, default: int) -> int:
return default
def selection_config() -> tuple[int, int, int]:
"""(min_score, top_n, fallback_top) from env, read per run."""
def selection_config() -> tuple[int, int, int, int]:
"""(min_score, top_n, fallback_top, fallback_min_score) from env, per run."""
min_score = max(0, min(100, _env_int("SUMMARY_MIN_SCORE", DEFAULT_SUMMARY_MIN_SCORE)))
top_n = max(0, _env_int("SUMMARY_TOP_N", DEFAULT_SUMMARY_TOP_N))
fallback_top = max(0, _env_int("SUMMARY_FALLBACK_TOP", DEFAULT_SUMMARY_FALLBACK_TOP))
return min_score, top_n, fallback_top
fallback_min_score = max(
0, min(100, _env_int("SUMMARY_FALLBACK_MIN_SCORE", DEFAULT_SUMMARY_FALLBACK_MIN_SCORE))
)
return min_score, top_n, fallback_top, fallback_min_score
def _summary_window_minutes() -> int:
@ -389,12 +393,18 @@ def ensure_tables() -> None:
def get_recent_news(window_minutes: int | None = None) -> list[dict]:
"""Fetch articles from the look-back window (content > 100 chars)."""
"""Fetch articles from the look-back window ``(now - window, now]``.
The upper bound is deliberate: future-dated rows (event listings, bad
scrape timestamps) must not be re-graded every interval. Rows with a NULL
timestamp have never matched the lower bound and still do not.
"""
mins = window_minutes if window_minutes is not None else effective_window_minutes()
query = """
SELECT id, title, content, url, domain
FROM articles
WHERE timestamp > NOW() - make_interval(mins => %s)
AND timestamp <= NOW()
AND content IS NOT NULL AND length(content) > 100
ORDER BY timestamp DESC;
"""
@ -629,12 +639,12 @@ def summarize_news() -> None:
logger.info("No new articles found in the last %s min.", window)
return
min_score, top_n, fallback_top = selection_config()
min_score, top_n, fallback_top, fallback_min_score = selection_config()
logger.info(
"Processing %d articles with %s (batch_size=%d, recap=%s, window_min=%s, "
"min_score=%d, top_n=%d, fallback_top=%d)...",
"min_score=%d, top_n=%d, fallback_top=%d, fallback_min_score=%d)...",
len(articles), model, BATCH_SIZE, recap, window,
min_score, top_n, fallback_top,
min_score, top_n, fallback_top, fallback_min_score,
)
graded: list[dict] = []
@ -669,7 +679,11 @@ def summarize_news() -> None:
return
selected, used_fallback = select_graded(
graded, min_score=min_score, top_n=top_n, fallback_top=fallback_top
graded,
min_score=min_score,
top_n=top_n,
fallback_top=fallback_top,
fallback_min_score=fallback_min_score,
)
selected_positions = {block["position"] for block in selected}
for block in graded:
@ -677,8 +691,9 @@ def summarize_news() -> None:
if used_fallback:
logger.warning(
"No article cleared SUMMARY_MIN_SCORE=%d — fallback to top %d "
"(max score %d)",
min_score, len(selected), max(b["score"] for b in graded),
"with score >= %d (max score %d)",
min_score, len(selected), fallback_min_score,
max(b["score"] for b in graded),
)
logger.info(
"selected %d/%d graded articles for reduce (scores=%s)",
@ -688,8 +703,9 @@ def summarize_news() -> None:
final_input = join_blocks(selected)
if not final_input.strip():
logger.warning(
"Selection is empty (min_score=%d, fallback_top=%d) — reduce gets empty DATA.",
min_score, fallback_top,
"Selection is empty (min_score=%d, fallback_top=%d, "
"fallback_min_score=%d) — reduce gets empty DATA.",
min_score, fallback_top, fallback_min_score,
)
master_raw = call_llm(