Repo guide · KWL Cadence

Cadence — repo guide (for Jeth)

How the npilabs/KWL repo is laid out: where the front end, the backend, the Skill, and the data pipeline live, what each depends on, and what we need for the next quarter’s data. Written Aug 2026, after the repo moved to the npilabs org.


The two repos

Cadence spans two GitHub repos, both now under npilabs:

Repo What it holds
npilabs/KWL (this repo) The Cadence product: website, backend API, the Skill, and the report/data-generation pipeline.
npilabs/Karla (../Scraper locally) KARLA scraping harness — the raw data fetchers and the Python env the pipeline runs in. The kwl_*.py fetchers and the data contract live here.

They work together: KARLA collects raw signals; the pipeline in this repo turns them into reports and the Skill’s data.


The four parts of Cadence

Almost everything active is under site/, skill-worker/, and analysis/:

KWL/
├── site/astro/          1 + 2  — website (front end) + backend API
├── skill-worker/           3   — the Cadence Skill (Cloudflare Worker)
├── analysis/               4   — the quarterly report/data pipeline
├── run_quarterly.sh        4   — one-command pipeline runner (root)
├── docs/                       — reports, GTM, strategy, this guide
├── mockups/  data/             — supporting artefacts
└── (legacy dirs — ignore, see bottom)

1. Front end — site/astro/src/

Astro 5 static site, deploys to Cloudflare Pages (project cadence-site).

Path What
src/pages/ every route (home, product, pricing, reports/[market].astro, industries, account, research, Field Notes, legal, etc.)
src/components/ shared UI (incl. PaddleCheckout.astro)
src/layouts/Base.astro page shell + the i18n applier
src/i18n/fr.ts French translations (keyed data-i18n swap)
src/styles/ brand tokens + global CSS (ink #16181A, green #1F6F50, paper #F1F0EA)
src/data/ the generated report data the site reads — editions/, europe.json, etc. (produced by the pipeline, part 4)
src/config/paddle.ts public Paddle config (client token + price ids + environment)
src/lib/ client helpers

2. Backend / API — site/astro/functions/

Cloudflare Pages Functions (serverless, deploy with the site). Bound to a D1 database (edge SQLite; schema in site/astro/schema.sql).

File Endpoint / role
_lib/auth.ts HMAC session cookies + shared email shell
api/auth/{request,verify,logout}.ts magic-link sign-in
api/account/{data,update,portal}.ts account page data, profile edit, Paddle billing portal
api/contact.ts all site forms → Resend + D1 (enquiry, gate, newsletter, research)
api/invoice.ts sales-assisted invoice request
api/webhooks/paddle.ts Paddle webhook → grants entitlements + Skill key + fulfilment email
reports/_middleware.ts the report paywall (France open; 10 markets preview + gated depth; Europe teaser)

3. The Skill — skill-worker/

Standalone Cloudflare Worker (cadence-skill…workers.dev). REST + MCP over a trimmed copy of the report data, with per-market entitlement gating (reads the same D1).

File What
src/index.ts routes (REST + MCP), tool dispatch, entitlement gating
src/cadence.ts the read logic / tools
src/entitlement.ts credential → account → entitlement: coverage (which markets) and cross (the European synthesis)
scripts/build-data.mjs bundles analysis/ editions into the Worker’s data/data.json
src/rest-routes.ts the /v1/* surface, declared once: the router runs it and the spec is printed from it
scripts/build-openapi.mjs generates openapi.json + mcp-tools.json from the two files above
openapi.json, mcp-tools.json the published contract. Generated — do not edit. npm run openapi
README.md, CHATGPT-SETUP.md setup notes

The MCP interface. The Worker serves two surfaces from the same code, same key-scoped auth (Authorization: Bearer <key> → entitlement → held markets):

The tools (identical over MCP tools/call and REST) are defined in cadence.ts and dispatched + entitlement-gated in index.ts. mcp-tools.json is the generated registry — name, title, description, input schema and the REST paths that are the same capability — so the count is not written here: it said “eight” while there were ten, the two missing being sector_read and objects. Per-market tools return “not in your plan” unless the key holds that market; europe_picture/league need a key whose account holds every market (market = NULL). “France always allowed” was true and is not. OPEN_MARKETS was removed on 12 October 2026: it had been serving France to every live key, so a single-market subscriber to Germany was also being served France. Authentication was deciding access, which is the entitlement system’s job. The all-markets SKU itself was retired as a commercial offer on 20 October 2026, and remains in SKILL_SKUS only because existing holders resolve through it.

Smoke test: node skill-worker/scripts/smoke.mjs <baseUrl> <apiKey>. Note: mcp/cadence_mcp.py in the repo root is the old local dev MCP — superseded by this Worker; the hosted Worker is the only sellable/production MCP.

4. The data / report pipeline — analysis/ + run_quarterly.sh

This is where raw KARLA signals become reports. Orchestrated by run_quarterly.sh at the repo root, which calls the analysis/*.py scripts using the KARLA Python env (../Scraper/.venv, which has Athena / SearchApi / Wikidata access).

./run_quarterly.sh enrich     # origin + brand activity + movers  (the data layer)
./run_quarterly.sh generate   # editions + Europe control room    (the reports)
./run_quarterly.sh ship       # build + deploy
./run_quarterly.sh all        # enrich → generate → ship (default)
Script Produces
analysis/origin_unified.py artist origin (ISRC + Wikidata citizenship + genre), all markets
analysis/collect_brand_activity.py brand demand × ad-supply, per market
analysis/eu_movers.py behavioural movers, all markets
analysis/gen_history.py historical reconstruction (adds each new quarter)
analysis/gen_editions.py per-market editions (signals + data-generated narrative) → site/astro/src/data/editions/
analysis/gen_europe.py the Europe control room

Output lands in site/astro/src/data/, which the front end (part 1) and the Skill (part 3, via build-data.mjs) both read. Markets: fr de it es nl se pt pl be at ie.


Dependencies & environments

Thing Where
Front-end + Functions deps site/astro/package.json (Astro, TS). Build: npm run build:prod from site/astro.
Skill deps skill-worker/package.json (wrangler). Deploy: npx wrangler deploy.
Pipeline runtime ../Scraper/.venv (KARLA repo) — boto3/requests, Athena + SearchApi + Wikidata creds.
Database Cloudflare D1 (kwl-cadence-forms). Schema: site/astro/schema.sql.
Secrets Never in the repo. Cloudflare Pages/Worker secrets only (RESEND_API_KEY, SESSION_SECRET, PADDLE_WEBHOOK_SECRET, PADDLE_API_KEY). Public/non-secret vars are in wrangler.toml.
Deploy model Push to main → Cloudflare Pages auto-builds & deploys. The Worker deploys separately via wrangler.

Working rule: always branch + ff-merge into main; never commit straight to main.

Useful in-repo docs: site/DEPLOY.md, site/RUNBOOK-public-domain.md, site/astro/GO-LIVE.md, site/astro/FORMS-SETUP.md, and CLAUDE.md (project overview).


What we need for next quarter (Q3 2026)

The live editions are Q2 2026. We need a Q3 refresh, published early October. The non-music side (macro, GDELT, Netflix, Trends, Wikipedia) refreshes itself from KARLA. The music layer is the part blocked on engineering — i.e. you:

  1. Q3 chart data — Spotify Top 50 + Viral 50 through the Q3 window, landed into the usual store (Athena / eclectron) so origin_unified.py and the movers pick it up.
  2. Audio characteristics for post-Nov-2024 tracks — Spotify deprecated the audio-features endpoint (Nov 2024), so newly-charting tracks have no valence/energy/ tempo/etc. We need the agreed audio-characteristics solution. Alex’s preference is imputing features from genre (rather than Essentia/Musicnn extraction); whichever we ship must be validated to keep the existing correlations honest (the r ≈ −0.52 / +0.43 relationships) before it feeds a published edition.
  3. Once (1) and (2) are in, the rest is our side: run ./run_quarterly.sh enrich (add the new quarter to gen_history.py first) → generate → ship. That regenerates every edition + Europe + the Skill data.

Timeline: land the music data through late September so we can generate and publish early October. If the audio-imputation approach needs a spike, flag it early — it’s the long pole.


Legacy — ignore these (pre-pivot)

Not part of Cadence; kept only for history. Don’t build on them.

Dir Was
backend/ catalogue-iq-backend (rights.fm / Catalogue-IQ era)
mcp/ local dev-only MCP (cadence_mcp.py) — superseded by skill-worker/
website/, website-next/ earlier site builds
NEW_PROD/, Cadence/ (empty) scratch

If in doubt, “active Cadence” = site/, skill-worker/, analysis/ (+ ../Scraper for raw data). Everything else in the root is support or legacy.