Files
news-platform/docs/WRITER_PIPELINE_ANDEBOL.md
José Alves 213c1f87fd
Tests / writer-pipeline tests (push) Successful in 4m52s
Tests / bridge tests (push) Successful in 2m38s
Tests / frontend lib tests (push) Successful in 3m3s
Tests / bridge tests (pull_request) Successful in 4m30s
Tests / writer-pipeline tests (pull_request) Successful in 4m43s
Tests / frontend lib tests (pull_request) Successful in 3m38s
test(writer-pipeline): featured-image regression guard for revista-imprensa (BIT-28134)
Adds unit coverage for resolveFeaturedImageForDraft() so the mandatory
featured-image ERRSIG cluster (BIT-27615 / BIT-26192 / BIT-22288) cannot
silently regress: ordinary drafts still throw when no image resolves,
revista-imprensa leads (by sourceKind or URL) resolve to null instead,
and non-revista URLs are not over-matched. Also covers upsertItem
preserving sourceKind across updates that omit it.

Paperclip-Issue: BIT-28134
Paperclip-Run: d45309d9-128d-4266-93db-9afe285c4486
2026-08-04 20:09:47 +01:00

107 lines
6.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Writer pipeline — andebol.net (aggregator-first)
**Scope:** `writer-pipeline/` in this repo. **Parent:** aggregator epic (Paperclip **BIT-709**); delivery tracked under **BIT-713**.
## Editorial Standards
**All content must follow the [Editorial Brief](ANDEBOL_EDITORIAL_BRIEF.md).** Key rules:
- **Language:** European Portuguese (PT-PT) only — no English
- **Real news only:** verify facts, cite sources, no speculation
- **No plagiarism:** rewrite from scratch, never copy-paste
- **Quality checks:** headlines must match content; images must have clear rights
See [`docs/ANDEBOL_EDITORIAL_BRIEF.md`](ANDEBOL_EDITORIAL_BRIEF.md) for detailed publication criteria, writing templates, and common pitfalls.
## What it does
1. **Ingest** — Pulls configured sources (RSS + optional sitemap/HTML) from `writer-pipeline/config/andebol.net.sources.json` into a local JSON store (`writer-pipeline/var/aggregated-items.json`, gitignored). Portuguese highlights (e.g. Record, SAPO) and international feeds (e.g. Guardian world, BBC handball) extend the shortlist; **Lusa** and other wires are covered under usage notes in `docs/AGGREGATOR_OPERATIONS.md` § *Sources, copyright, and wire services (Lusa)*.
2. **Shortlist** — Newest items that are not yet drafted in WordPress and not skipped.
3. **Editorial filter** — Each shortlisted item is evaluated against the [Editorial Brief](ANDEBOL_EDITORIAL_BRIEF.md) (language, fact-checking, source attribution) before writing. Skip items that do not meet criteria.
4. **Read path (aggregator-first)** — Before any LLM *reading* step, the writer uses only structured fields from the store (`title`, `summary`, `url`, `sourceFeedId`). No full-page fetch is required for the primary path.
5. **Metrics** — Each cycle appends one JSON line to `writer-pipeline/var/metrics.jsonl` with estimated **read-phase** tokens for:
- **Aggregator path:** `aggregatorReadContext()` (title + summary + URL + instruction block).
- **Legacy path:** full HTML text fetch per shortlisted URL (for comparison only). If a fetch fails, that item falls back to summary-sized estimate so the cycle still completes.
6. **Publish** — Optional: creates **WordPress drafts** from the shortlist using Application Passwords (never required for metrics-only runs).
## Fallback behaviour
- **Per feed:** a failing RSS URL is logged in `metrics.errors`; other feeds still run.
- **Per URL (legacy comparison):** failed HTML fetch does not block the cycle; aggregator metrics still apply for production reads.
- **WP publish:** if credentials are missing, the cycle completes with a clear error string and **no** draft creation.
- **Featured image (BIT-22288 / BIT-22292 / BIT-24388):** drafts require a rights-cleared image. The pipeline uses RSS enclosure / media:content / media:thumbnail / `og:image` / `twitter:image` first. If none are present, it can **list a WebDAV collection** (authenticated Basic auth) and pick the best-matching filename against the headline, then upload to WP with caption/alt. Configure with `WRITER_WEBDAV_*` env vars (see `deploy/systemd/writer-andebol.env.example`). When WebDAV is **not** enabled, the archive is never contacted (only RSS/HTML hints apply). If WebDAV is enabled but PROPFIND/GET fails, draft creation for that item fails with an explicit error (no silent fallback to a random web image) — operational fallback is to disable `WRITER_WEBDAV_ENABLED` or restore connectivity before retrying. **Note:** FPA RSS feed uses `<media:content>` for featured images (BIT-24388), which is now properly extracted and attached to WordPress posts.
## Regression tests — mandatory featured image (BIT-28134)
`resolveFeaturedImageForDraft()` in `writer-pipeline/src/wp-publish.ts` has a unit-test regression
guard (`writer-pipeline/src/wp-publish.test.ts`) against the mandatory-featured-image ERRSIG
cluster (BIT-27615 / BIT-26192 / BIT-22288) triggered by revista-imprensa / archive drafts. It
mocks `fetch` (no live network) and asserts, per draft class:
- **Ordinary drafts** (not a revista-imprensa lead) still **throw** `Mandatory featured image`
when no image source resolves — required drafts must never ship without a featured image
(BIT-22288).
- **Revista-imprensa leads**, synthetic `/revista-imprensa/<date>/<n>` pages that are not real
article pages, resolve to **`null`** instead of throwing — whether detected via `sourceKind`
(BIT-26192) or via the URL path when `sourceKind` is missing/stripped by a later store upsert
(BIT-27615).
- A **non-revista URL** with `sourceKind` missing still throws — the URL heuristic must not
over-match plain article URLs.
`writer-pipeline/src/store-extra.test.ts` also covers `upsertItem` preserving `sourceKind` across
an update that omits it (the other half of the BIT-27615 fix).
Run via the existing `npm test` script (builds then runs `node --test`):
```bash
cd writer-pipeline
npm test
```
To confirm the guard actually catches the regression, revert the BIT-27615 URL-detection fallback
in `resolveFeaturedImageForDraft` (drop the `revistaPublisherHome != null` clause) and re-run
`npm test` — the `BIT-27615 regression guard` test fails; restore the fix and it passes again.
## Commands
```bash
cd writer-pipeline
npm install
npm run build
# Ingest + metrics + optional legacy comparison (no WP)
node dist/run-cycle.js --site andebol.net --dry-run --shortlist 10
# Create up to N drafts (needs ANDEBOL_WP_* env)
node dist/run-cycle.js --site andebol.net --publish-max 1 --shortlist 10
```
**Aggregator HTTP API** (optional, for cron or external tools):
```bash
npm run aggregator
# GET /health
# GET /shortlist?site=andebol.net&limit=10
# GET /search?site=andebol.net&limit=20&q=benfica&max_age_hours=168
# GET /stats
# POST /ingest?site=andebol.net
```
Set **`AGGREGATOR_URL`** on the bridge (e.g. `http://127.0.0.1:3310`) so writer candidates use this API first; see `docs/AGGREGATOR_OPERATIONS.md`.
## Sample outcome (2026-04-02, dry-run, 5-item shortlist)
| Metric | Value |
|--------|------|
| `tokensReadAggregatorPath` | ~675 |
| `tokensReadLegacyPath` | ~6.2k |
| Ratio (legacy ÷ aggregator) | ~9× |
Pricing reference in code uses **USD 0.15 / 1M input tokens** (`token-metrics.ts`) for *read-phase* estimates only — adjust when the production model changes.
## Next steps
- Add more `sources[]` entries (competition sites, press rooms) and tune dedup via canonical URLs; use per-feed **`maxItems`** on noisy RSS feeds.
- Plug a real LLM **generation** step that receives only `aggregatorReadContext(item)` — no full HTML in the prompt unless a human toggles a break-glass flag.
- Run `npm run aggregator` under systemd on the LXC with periodic `POST /ingest` and a single `run-cycle` cron for drafts.