Update agri

From atanasster/electionsbg · View interactive version

Update agri is an AI agent skill from the GitHub repository atanasster/electionsbg. Refresh the ДФ „Земеделие" (State Fund Agriculture) farm-subsidy data behind /subsidies — the agri_subsidies + agri_payloads Postgres tables. Two sources — the data.egov.bg org-56 open-data CSVs (financial years 2015–2023, EIK-keyed) and the СЕУ interactive register (seu.dfz.bg) for the current roll

How to use this skill

Skill file

Update ДФ „Земеделие" farm subsidies skill

<!-- pipeline-pointer:start (generated from the registry; edit scripts/pipeline/registry) -->

Pipeline: handler CODE0 is still delegated — pipeline:ingest lists it under Needs agent; why is a comment on its entry in scripts/pipeline/registry/handlers/. Follow this runbook to run it.

<!-- pipeline-pointer:end -->

Refreshes the data behind the farm-subsidy pack on /subsidies (dashboard, /subsidies/browse, /farm/:eik, and the "Земеделски субсидии" tile on /company/:eik). The pack is Postgres-only — the ingest writes the agri_subsidies (per-payment detail) + agri_payloads (precomputed per-scope overview + per-recipient jsonb) tables directly, no static JSON on disk (so there is NO bucket:sync step). Memory: [[project_agri_subsidies_pack]].

Two sources

When to run

| Trigger | Action |

|---|---|

| Watcher flags dfz_subsidies (egov published / re-uploaded a year) | pnpm run agri:seu && pnpm run agri:ingest, then publish BOTH loaders (Step 3) |

| User asks "refresh farm subsidies" / "pull the latest ДФЗ year" | same |

| Fresh clone, agri_subsidies table empty | pnpm run db:pg:up first, then the same |

| Monthly freshness pass for the rolling СЕУ years (no watcher fires — see note) | same |

Procedure

  1. Fetch the СЕУ rolling years (headless Playwright, ~1 min/year). The СЕУ window updates continuously, so always re-pull:

```bash

pnpm run agri:seu # → raw_data/agri/seu_2024.csv, seu_2025.csv

```

(If Playwright can't reach the register — network / a register redesign — the ingest still runs on whatever СЕУ CSVs are cached, plus the full egov backbone.)

  1. Ingest → Postgres (needs pnpm run db:pg:up):

```bash

pnpm run agri:ingest

pnpm run db:load:agri-hub-stats:pg

```

The second is not optional locally either. agri:ingest refreshes migration 162

only; 163 (agri_political_link, agri_cross_programme) has this loader as its

sole applier. Leaving it out means local is NOT source of truth for those two

tables — which matters directly at Step 3, where db:sync:cloud is offered as an

alternative that requires exactly that.

Reads the egov org-56 CSVs (cached, else re-fetched from the API) + the СЕУ CSVs, normalises + converts BGN→EUR, name-matches СЕУ→egov for EIK, and publishes agri_subsidies via an UNLOGGED stage twin + one-transaction DELETE+INSERT (RowExclusiveLock only, readers never blocked; a <5%-shrink guard refuses a half-parsed source — NOT a rename swap: two matviews depend on the table's OID) and agri_payloads via the shared stage merge. It applies migration 046_agri_subsidies.sql idempotently. Prints per-year row counts + € totals + the СЕУ EIK-match rate. The pure-LOAD half is pnpm run db:load:agri:pg — cache-only, throws on a partial cache, part of db:refresh.

  1. Publish to Cloud SQL so the live tiles update (the pack is DB-served, so this — not a bucket sync — is what ships it).

Publishing is the cloud LOAD wrapper (it reads the local raw_data/agri/ cache and writes over the proxy — no re-fetch; db:dump:cloud does NOT publish, it snapshots cloud outward to GCS):

```bash

pnpm run db:load:agri:pg:cloud

pnpm run db:load:agri-hub-stats:pg:cloud

```

One committed blob does read this corpus: the /sector/edu hub's research

arm (edu-hub-v1 T5) sums ССА/БАН payouts from agri_subsidies. After a local

reload run pnpm run db:gen-edu-hub-stats, commit

data/education/derived/hub_stats.json and

pnpm run bucket:sync:paths education/derived/hub_stats.json.

⚠️ **The second command is not a refinement of the first — it publishes a different

migration, and skipping it takes two pages DOWN rather than stale.** agri:ingest

refreshes migration 162 (agri_hub_stats_cache) and nothing else;

db:load:agri-hub-stats:pg is the ONLY applier of 163, which creates

agri_political_link and agri_cross_programme. Those two are DbDataTable

resources with no missingMigration degrade, so on a Cloud SQL that never ran it

/subsidies/political and /subsidies/cross-programme answer 500, not empty.

It is also the cheap half: ~6 s against the full ingest's ~5m44s. Its cache has five

inputs — agri_subsidies/agri_payloads, person_role+person, fund_projects,

contracts and budget_muni_transfer — so it is worth re-running after ANY of those

is reloaded on the cloud side, not only after an agri ingest.

Needs the Cloud SQL proxy on 127.0.0.1:5434 with the .pgpass line present — the password-less URL is correct here, and must never leak into a later local command (it would fail 28P01; unset DATABASE_URL after).

The FIRST of those re-uploads ~2M rows over the proxy, so it is the slow one; the hub-stats loader is small by comparison (its local run is ~6 s against the full ingest's ~5m44s — both LOCAL figures, and Cloud SQL is slower, since it REFRESHes three matviews and vacuums over the proxy — that gap was measured on the old db-g1-small, and prod has changed tier twice since — db-perf-optimized-N-2 from 2026-08-20, db-custom-2-13312 from 2026-08-27 (see CLAUDE.md „The serving instance") — so re-measure rather than assume the old ratio). For a whole-DB reconcile when several PG-backed skills ran, pnpm run db:sync:cloud --yes (pg_dump local → pg_restore cloud) is faster, but destructive (--clean) and requires local to be source of truth — including the unregenerable КЗК tier-2 outcome rows.

Optionally follow with pnpm run db:dump:cloud to refresh the GCS restore point. Only redeploy functions (firebase deploy --only functions:db) if the serving migration/route changed — 046 + the agri-payload route already shipped once.

  1. Sanity-check the console: FY2023 ≈ €1.14bn; FY2024/2025 ≈ €1.6bn each; all-years ≈ €11bn; СЕУ match rate ~25% of groups (the recurring entities). A big drop in a year's total or match rate means a source/parse regression.
  2. Stamp state/ingest/update-agri.json with lastSuccessfulIngest (current UTC ISO) + a one-line summary, so process-watch-report won't re-trigger.

Notes

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.