feat(summarizer): score articles in the map phase, brief only the important ones #53
Loading…
Reference in a new issue
No description provided.
Delete branch "wt/t_87992985"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What this changes
The reduce phase no longer sees every article in the window. The map phase grades each article 0-100 against an explicit rubric, the summarizer parses that score, and only the blocks at or above
SUMMARY_MIN_SCOREreach the brief, best first.1. Map prompt emits a machine-parsable score
MAP_PROMPT_DEFAULTnow carries the rubric verbatim, and- Score: <0-100>/- Importance: critical|high|medium|low|noneare the first two lines of every article block:Existing bullets, location, entities, category, OSINT-signal lines and the
Batch theme:trailer are unchanged.2. Parse and select (
news/summerizer/intel.py, pure, no DB)parse_map_scores(raw, *, articles, batch_index, batch_size)splits onArticle <n>:(case-insensitive, markdown/whitespace tolerant), reads the firstScore:(clamped 0–100), falls back to theImportance:label (critical 95 / high 78 / medium 50 / low 22 / none 5), then to 50, and never raises. Blocknof batchbmaps toarticles[b*BATCH_SIZE + n - 1]; blocks whose number does not land in the batch are counted asunattributedand dropped (nothing to attribute them to).select_graded(blocks, *, min_score, top_n, fallback_top)sorts score-desc (stable on ties), keepsscore >= min_scorecapped attop_n; when nothing clears the bar it returns the topfallback_topblocks.SUMMARY_FALLBACK_TOP=0returns an empty selection and the reduce phase receives an empty DATA section.join_blocks()builds the reduce DATA text from the selected blocks only, original text (Score line included), highest score first.{final_input}contract unchanged.3. Reduce prompts stop demanding filler
SUMMARY_PROMPT_DEFAULTandRECAP_PROMPT_DEFAULTlost "fill the brief/recap with minor incidents and crime reports" and "fill ticker with medium then low incidents and crime". They now state that DATA is pre-filtered by score and ordered best-first, to lead with the highest-scoring breaking stories, never promote an item above its score, and to return{"summary_en":"No significant developments in this window.","ticker":[],"map_items":[]}for empty DATA. JSON shape, ticker/map caps and the never-truncate rule are unchanged.4. Grades persisted
New
article_scorestable (article_id, summary_id, score, category, selected, model, created_at, PK(article_id, created_at), index onsummary_id), created inensure_tables()and as alembic012_article_scores(down_revision011_geofence_alerts_fence).save_batch()now returns the new summary id;persist_article_scores()writes one row per graded article after it (selected true/false). Insert failure is logged and does not abort the run.get_recent_news()now also selectsarticles.id— needed for attribution. Nothing else about the query changed.5. Config
SUMMARY_MIN_SCORE(60),SUMMARY_TOP_N(12),SUMMARY_FALLBACK_TOP(3), read per run via_env_int/selection_config(), documented in.env.examplenext toNEWS_BATCH_SIZEand indocs/news.md("Grading and selection" + config table). Nohomek8sedits.Non-goals respected: no extra LLM calls, no
app/requirements.txt,BATCH_SIZE, scraper,urls.txt,app/main.pymodels,news_itemsschema ornous_client.pychanges, no new dependency.Verification (real output)
1.
pytest -qin the repo — before:344 passed, 17 skipped(master). After:344 passed, 17 skipped(the default run does not collectnews/summerizer/tests— see next block).2. Summarizer suite (before this branch:
29 passed):New/updated:
test_grading.py(score present / missing per Importance label / garbage / clamp / markdown+case tolerance / batch attribution / omitted+reordered / out-of-range+duplicate / never raises; threshold+cap, stable ties, fallback,fallback_top=0, no leaked low-score block),test_grading_pipeline.py(offline end-to-end, monkeypatchedcall_llm, recording fake psycopg2: multi-batch attribution, reduce prompt contains only selected blocks best-first, all-low fallback,SUMMARY_FALLBACK_TOP=0empty DATA, score-insert failure does not abort),test_prompts.py(rubric lines present and Score first in the block; neither reduce prompt contains the old filler sentences).3. Offline end-to-end assertions (fake DB, 6 articles, BATCH_SIZE=3, scripted scores 92/34/81 + 12/66/no-Score→low): reduce prompt contains
Score: 92,Score: 81,Score: 66in that order and none ofScore: 34,Score: 12,Importance: low;article_scoresrows selected = articles {1,3,5} withsummary_id=42and the run's model.4. Scratch Postgres (postgres:16 container), real DB writes, monkeypatched LLM — 6 seeded articles incl. a
wiseye.orgevent-calendar listing and one article with noScore:line:The reduce prompt's DATA section contained exactly
Article 1(93),Article 5(78, label fallback) andArticle 3(71) — the Brent tape, the 5K and the event-calendar listing were dropped.5. Alembic:
alembic heads→012_article_scores (head)(single head, linear after011_geofence_alerts_fence). On a scratch PostGIS 16 DB stamped at 011,alembic upgrade headapplies 012 and\d article_scoresshows PK(article_id, created_at)plusix_article_scores_summary. (A full001→headreplay needs the PostGIS and TimescaleDB extensions, unavailable in the scratch images; that is an environment limit, not a diff issue.)Premise flag (not silently widened)
I have not run this against the live free-tier model (
meituan/longcat-2.5:free) — that check needs the cluster and the real key, and belongs to the post-merge live verification. TheImportance:-label fallback is implemented and unit-tested precisely so a model that dropsScore:still yields a usable ranking, and every fallback is counted per batch in the logs (map batch N graded x/y (score fallback=k, unattributed=u)). If the live model dropsScore:for most articles, the evidence to look for isfallback=k≈graded; the fix then should be a second, tiny grading request over titles only — not a wider rubric squeezed into the map prompt.Merge policy
PR only. Do not merge — the operator merges, rebuilds the amd64
news-summarizerimage, pins it insirius/homek8s apps/base/osint/news-summarizer-deployment.yamland does the live prompt-quality check. The red Pi compose workflow on master pushes is the known-broken unrelated job.