# Internal news aggregator — operations & costs (BIT-709) This runbook covers the **self-hosted** multi-source aggregator in `writer-pipeline/` and how the **bridge** uses it for writer candidates. ## Components | Piece | Role | |--------|------| | JSON store | `writer-pipeline/var/aggregated-items.json` (default path; override with `AGGREGATOR_STORE_PATH`) | | Ingest config | `writer-pipeline/config/{site}.sources.json` (e.g. `andebol.net.sources.json`) | | HTTP API | Fastify server (`npm run aggregator` → `dist/aggregator-server.js`) | | Bridge integration | Set `AGGREGATOR_URL` (e.g. `http://127.0.0.1:3310`) so `/writers/:id/candidates` queries the aggregator **before** merging WordPress posts | ## HTTP API (aggregator process) | Method | Path | Purpose | |--------|------|---------| | GET | `/health` | Liveness + resolved `storePath` | | GET | `/search?site=&limit=&q=&topic=&max_age_hours=` | Searchable listing: tokenized `q` matches title+summary; deduped store keyed by canonical URL at ingest | | GET | `/shortlist?site=&limit=&topic=` | Newest eligible items (no draft/skip) for one site | | GET | `/stats` | Total items, counts by site and by `site:sourceFeedId` | | POST | `/ingest?site=` | Run ingest for that site’s sources config and persist the store | Environment: - `AGGREGATOR_HOST` (default `127.0.0.1`), `AGGREGATOR_PORT` (default `3310`) - `AGGREGATOR_CONFIG_DIR` — directory of `*.sources.json` - `AGGREGATOR_STORE_PATH` — JSON backing file ## Bridge - **`AGGREGATOR_URL`**: base URL of the aggregator (no trailing slash required). If unset, candidates come only from WordPress (previous behaviour). - **`GET /writers/:agentId/candidates`**: loads aggregated items for `config.domain`, maps them to the same candidate shape as WordPress rows, adds **`source_url`** for external URLs, then merges WordPress posts and ranks. Response includes **`candidate_source`**: `aggregator` | `wordpress` | `mixed`. - **`GET /writers/:agentId/stats`**: when the aggregator is enabled, also calls `GET /stats` and returns `aggregator_total_items` and `aggregator_site_items` when available. ## Data contract (per item) Each aggregated item includes **source** (`sourceFeedId`, optional `sourceKind`), **timestamps** (`publishedAt`, `ingestedAt`), **technical summary** (`summary` + `title`), and **original URL** (`url`). Deduplication at ingest uses **canonical URL** → stable `id` (`store.itemId`). ## Sources, copyright, and wire services (Lusa) - **Operational config:** `writer-pipeline/config/andebol.net.sources.json`. RSS feeds may set **`maxItems`** to limit how many recent entries are ingested per run (large highlight feeds such as SAPO). - **Agência Lusa:** a direct, machine-readable **public Lusa wire feed suitable for unattended ingestion is not used here** — public endpoints are often HTML/portal pages or require a **commercial licence** and contractual terms. Syndicated PT feeds (e.g. SAPO highlights) may **credit Lusa or other agencies** in bylines; that does **not** replace checking rights for republication. Editors must follow `docs/ANDEBOL_EDITORIAL_BRIEF.md` (original rewrite, attribution, no verbatim copy). - **Third-party RSS (Record, Guardian, BBC, etc.):** use only as **story leads**; comply with each site’s **terms of use** and robots/API policy; **English** items are for triage only — **andebol.net** publishes **European Portuguese (PT-PT)** only. ## Cost model (read-phase, LLM) Structured read context for one item is built in `writer-pipeline/src/token-metrics.ts` (`aggregatorReadContext`). Reference **input** pricing used in metrics: **USD 0.15 / 1M tokens** (`REF_INPUT_USD_PER_1M`) — adjust when the production model changes. Typical savings vs full-page fetch for the same shortlist are documented in `docs/WRITER_PIPELINE_ANDEBOL.md` (example ratio ~9× fewer tokens on the read path). ## Suggested scheduling (self-hosted) 1. **Ingest** — `POST /ingest?site=andebol.net` on a timer (e.g. every 6–12 hours per feed stability), or run `node dist/run-cycle.js` where you already use the pipeline. 2. **Aggregator process** — `systemd` or PM2 keeping `npm run aggregator` up on the same host (or container) as the bridge; bridge reaches it via loopback or private network. 3. **Bridge** — ensure `AGGREGATOR_URL` is set in the bridge environment alongside Redis/WordPress settings. ## Verification ```bash cd writer-pipeline && npm run build && npm test cd ../bridge && npm run build # With aggregator running: curl -sS "http://127.0.0.1:3310/search?site=andebol.net&limit=5" ``` ## Related docs - `docs/AGGREGATOR_ARCHITECTURE.md` — long-term schema and phased plan - `docs/WRITER_PIPELINE_ANDEBOL.md` — pipeline commands and metrics - `docs/writer-tools.md` — bridge writer API