osint-dashboard/news/summerizer/tests/conftest.py
backend 625c935c80 fix(summarizer): fail loudly on a rejected model id; parallel map phase
The analyst produced no briefs for nine days because SUMMARY_MODEL had been
retired: every chat completion returned 404, nous_client.chat() swallowed it
into "", and the map phase "succeeded" with zero blocks.

- nous_client.chat(): log status_code + a 500-char body slice for every
  non-200. 400/404/422 now raise NousAPIError (bad model id / payload cannot
  succeed on a retry); 429/5xx are retried with backoff inside the call;
  401/other keep the old empty-return contract.
- Startup validation: one GET /models per process; if SUMMARY_MODEL is absent
  the run logs an ERROR with the configured id plus near matches and returns
  rc=2 before issuing any chat call. Fails open (warning) when /models itself
  is unreachable.
- Map phase: ThreadPoolExecutor fan-out capped by SUMMARY_MAP_CONCURRENCY
  (default 16). Grading still runs in batch order on the main thread; reduce
  stays serial; the 60 s per-call timeout is unchanged.
- summarize_news() now returns an exit code and __main__ sys.exit()s it:
  zero parsed map blocks, an empty reduce, or an unpersisted brief all exit
  non-zero. The scheduler logs rc at ERROR when it is not 0.
- Map calls start at 8192 completion tokens (they escalate to 16384): ~60% of
  real batches overflowed 4096 and were regenerated at 8192, doubling their
  wall time for no benefit.

Measured on the real backlog: NEWS_RECAP=1 NEWS_SUMMARIZE_FORCE=1
python summarizer.py over 2984 articles / 746 batches finished in 951 s
(15m51s), rc=0, 746/746 batches non-empty, 0 warnings.

Prompts, BATCH_SIZE=4, grading/rubric behaviour and the DB schema are
untouched (article_summaries.kind stays interval / daily_recap).
2026-12-04 23:34:54 -05:00

37 lines
1.2 KiB
Python

"""Keep summarizer unit tests importable without Postgres drivers, offline."""
from __future__ import annotations
import sys
from pathlib import Path
from types import ModuleType
_SUMMERIZER_DIR = Path(__file__).resolve().parent.parent
if str(_SUMMERIZER_DIR) not in sys.path:
sys.path.insert(0, str(_SUMMERIZER_DIR))
if "psycopg2" not in sys.modules:
fake = ModuleType("psycopg2")
fake.connect = lambda **kwargs: None # type: ignore[attr-defined]
sys.modules["psycopg2"] = fake
import pytest # noqa: E402
@pytest.fixture(autouse=True)
def offline_llm_defaults(monkeypatch):
"""No network and no retry sleeps unless a test asks for them.
``summarize_news()`` validates SUMMARY_MODEL against ``GET /models`` once
per process; unit tests must never leave the box for it. Tests that
exercise the missing-model path patch ``summarizer.model_available``
themselves (a later ``setattr`` wins over this one).
"""
import nous_client
import summarizer
monkeypatch.setenv("NOUS_RETRY_BACKOFF_S", "0")
nous_client.reset_model_cache()
monkeypatch.setattr(summarizer, "model_available", lambda **kwargs: True)
yield
nous_client.reset_model_cache()