From 19a357d92bf9137a06c8c38c6a07d6d803f51739 Mon Sep 17 00:00:00 2001 From: shay Date: Sat, 18 Apr 2026 00:02:34 -0400 Subject: [PATCH] feat(readme): remove references to deleted production pipeline design. --- .github/workflows/docker.yml | 36 +- .gitignore | 5 + CLAUDE.md | 63 - README.md | 1 - .../plans/2026-04-17-production-pipeline.md | 2883 ----------------- .../2026-04-17-production-pipeline-design.md | 531 --- 6 files changed, 39 insertions(+), 3480 deletions(-) delete mode 100644 CLAUDE.md delete mode 100644 docs/superpowers/plans/2026-04-17-production-pipeline.md delete mode 100644 docs/superpowers/specs/2026-04-17-production-pipeline-design.md diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 8847bc9..b7b2061 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -6,10 +6,16 @@ on: pull_request: branches: [main] +env: + REGISTRY: ghcr.io + jobs: build: name: ${{ matrix.service }} runs-on: ubuntu-latest + permissions: + contents: read + packages: write strategy: fail-fast: false matrix: @@ -19,11 +25,37 @@ jobs: - uses: docker/setup-buildx-action@v3 + - name: Log in to GHCR + if: github.event_name == 'push' + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Compute image name (lowercase) + id: img + env: + OWNER: ${{ github.repository_owner }} + SERVICE: ${{ matrix.service }} + run: echo "name=${REGISTRY}/${OWNER,,}/ghstats-${SERVICE}" >> "$GITHUB_OUTPUT" + + - name: Extract metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ steps.img.outputs.name }} + tags: | + type=ref,event=branch + type=sha,format=short + type=raw,value=latest,enable={{is_default_branch}} + - name: Build ${{ matrix.service }} image uses: docker/build-push-action@v6 with: context: ./${{ matrix.service }} - push: false - tags: ghstats-${{ matrix.service }}:ci + push: ${{ github.event_name == 'push' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha,scope=${{ matrix.service }} cache-to: type=gha,mode=max,scope=${{ matrix.service }} diff --git a/.gitignore b/.gitignore index cc6f313..4654445 100644 --- a/.gitignore +++ b/.gitignore @@ -234,3 +234,8 @@ generator/frontend/node_modules/ .superpowers/ .playwright-mcp/ screenshots/ + +# Kept locally, not tracked +/CLAUDE.md +/docs/superpowers/plans/ +/docs/superpowers/specs/ diff --git a/CLAUDE.md b/CLAUDE.md deleted file mode 100644 index b698548..0000000 --- a/CLAUDE.md +++ /dev/null @@ -1,63 +0,0 @@ -# CLAUDE.md - -Guidance for Claude Code working in this repository. - -## Layout - -This is a monorepo of three independent Python services plus a Vite+React -frontend. Each service has its own `requirements.txt`, `pytest.ini`, -`Dockerfile`, and README: - -- `fetcher/` — port 5001. Owns the GitHub PAT and `fetcher.db`. Refresh - cron polls GitHub, upserts raw payloads, serves `/data/` to internal - callers only. -- `generator/` — port 5002. Owns settings + widgets SQLite DBs. Build - worker renders SVGs from fetcher data. Serves the React SPA from - `src/static/` and all `/api/*` routes. Frontend sources live in - `generator/frontend/` and get built into `src/static/` by the - multi-stage Dockerfile. -- `edge/` — port 5003. Cache-first SVG proxy in front of the generator; - Flask-Caching + Flask-Compress, optional Redis. - -Per-service READMEs cover run/test/Docker commands. The design and -implementation plan live in `docs/superpowers/`. - -## Working on a single service - -Each service is self-contained. `cd && pytest` is the test -entry point; `cd && pip install -r requirements.txt` is the -dependency install. Don't cross-import across services — they talk -over HTTP (generator → fetcher via `generator/src/fetcher_client.py`; -edge → generator via `edge/src/api.py::_fetch_origin`). - -## Configuration - -All runtime tuning is in each service's `src/config.py` and is -overridable via env vars: - -- `fetcher/src/config.py` — `GITHUB_PAT`, `FETCHER_INTERNAL_TOKEN`, - `COLLABORATOR_*`, `COMMIT_*`, `API_TIMEOUT`, refresh cadence. -- `generator/src/config.py` — `FETCHER_URL`, `FETCHER_INTERNAL_TOKEN`, - `ENROLLMENT_DAILY_CAP`, `WIDGET_LRU_PER_USER`, `POLL_INTERVAL_MINUTES`, - `TAG_MAX_COUNT`, `TAG_LANGUAGE_MAP`, `TAG_TOPIC_MAP`, - `ENABLED_WIDGETS`, `WIDGET_ORDER`, `HIDDEN_LANGUAGES`. -- `edge/src/config.py` — `GENERATOR_URL`, `CACHE_TYPE`, - `CACHE_REDIS_URL`, `CACHE_DEFAULT_TIMEOUT`, `CACHE_THRESHOLD`, - `UPSTREAM_TIMEOUT`. - -## Conventions - -- Widget renderers return SVG strings; only `generator/src/worker.py` - persists them (via `db.put_widgets`). -- The `X-Widget-Status` header (`ready | building | rate_limited | - not_found`) is the contract between generator and edge for what to - cache. -- Generator ↔ fetcher traffic uses the shared secret - `FETCHER_INTERNAL_TOKEN` (HTTP header `X-Internal-Token`). Public - routes on the generator don't require auth in v1. -- Adding a widget still means: new `generator/src/widgets/.py`, - export from `generator/src/widgets/__init__.py`, wire into - `generator/src/processor.py::generate_widgets_from_github`, add the - key to `WIDGET_ORDER` / `ENABLED_WIDGETS` in - `generator/src/config.py`. -- Adding a theme: entry in `generator/src/themes/themes.py`. diff --git a/README.md b/README.md index d213b04..c64bd20 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ each self-contained. See each service's README for details. ## Design Docs - Spec: [`docs/superpowers/specs/2026-04-17-production-pipeline-design.md`](docs/superpowers/specs/2026-04-17-production-pipeline-design.md) -- Plan: [`docs/superpowers/plans/2026-04-17-production-pipeline.md`](docs/superpowers/plans/2026-04-17-production-pipeline.md) ## Quick start (local dev, no Docker) diff --git a/docs/superpowers/plans/2026-04-17-production-pipeline.md b/docs/superpowers/plans/2026-04-17-production-pipeline.md deleted file mode 100644 index 7e58a28..0000000 --- a/docs/superpowers/plans/2026-04-17-production-pipeline.md +++ /dev/null @@ -1,2883 +0,0 @@ -# Production Pipeline Implementation Plan - -> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. -> -> **Commit approval:** Global user rule forbids `git commit` / `git push` without explicit approval. The commit step in each task must ask the user for approval before running. Do not amend prior commits; always create new ones. Never `git push` unless the user explicitly asks. - -**Goal:** Split the existing monolithic widget-generator repo into three independent Python services (`fetcher`, `generator`, `edge`) plus a React/Vite frontend folded into `generator/frontend`, each with its own Dockerfile, tests, and README. End state: a public `GET /` on the edge returns a precomputed SVG (or a clean placeholder) for any GitHub user, with a background pipeline that enrolls, fetches, builds, and refreshes. - -**Architecture:** Three Flask services communicating over HTTP. Fetcher owns the GitHub PAT and `fetcher.db`. Generator owns settings + widgets DBs, runs a build worker + cron, and serves both the React SPA (at `/`) and JSON/SVG APIs (at `/api/*`). Edge is a thin Flask-Caching / Flask-Compress proxy that serves widget SVGs cache-first with the generator as origin. Redis is designed for but not deployed — `cache.py` is a no-op wrapper in v1. - -**Tech Stack:** Python 3.11, Flask, SQLite (WAL mode), Flask-Caching (SimpleCache in v1), Flask-Compress, `requests`, `pytest`, `responses` (HTTP mocking), React + Vite + TypeScript (existing), Docker (multi-stage for generator). - -**Spec:** `docs/superpowers/specs/2026-04-17-production-pipeline-design.md` - ---- - -## Phases - -- **Phase 0** — Scaffolding + removal of retired code -- **Phase 1** — Fetcher service (full) -- **Phase 2** — Generator: DB, cache wrapper, placeholder, processor, fetcher client -- **Phase 3** — Generator: API, worker, cron, integration test -- **Phase 4** — Edge service -- **Phase 5** — Frontend migration into generator + multi-stage Dockerfile -- **Phase 6** — Top-level README, CLAUDE.md update, final gitignore - ---- - -## Phase 0 — Scaffolding - -### Task 0.1: Create service folders and .gitignore - -**Files:** -- Create: `fetcher/`, `fetcher/src/`, `fetcher/tests/`, `fetcher/data/` -- Create: `generator/`, `generator/src/`, `generator/tests/`, `generator/tests/integration/`, `generator/data/` -- Create: `edge/`, `edge/src/`, `edge/tests/` -- Modify: `.gitignore` - -- [ ] **Step 1: Create directories** - -```bash -mkdir -p fetcher/src fetcher/tests fetcher/data -mkdir -p generator/src generator/tests/integration generator/data -mkdir -p edge/src edge/tests -touch fetcher/src/__init__.py fetcher/tests/__init__.py -touch generator/src/__init__.py generator/tests/__init__.py generator/tests/integration/__init__.py -touch edge/src/__init__.py edge/tests/__init__.py -touch fetcher/data/.gitkeep generator/data/.gitkeep -``` - -- [ ] **Step 2: Update `.gitignore`** - -Append to `.gitignore`: - -``` -# Per-service SQLite databases (runtime data) -fetcher/data/*.db -fetcher/data/*.db-wal -fetcher/data/*.db-shm -generator/data/*.db -generator/data/*.db-wal -generator/data/*.db-shm - -# Built frontend bundle -generator/src/static/ -generator/frontend/dist/ -generator/frontend/node_modules/ - -# Python build artifacts -**/__pycache__/ -**/*.pyc -.pytest_cache/ - -# Widget build artifacts at repo root (retired) -/widget_*.svg -/data/ghstats.db -``` - -- [ ] **Step 3: Commit (ask user for approval first)** - -```bash -git add .gitignore fetcher/ generator/ edge/ -git commit -m "chore(scaffold): create fetcher/generator/edge service folders" -``` - ---- - -### Task 0.2: Remove retired top-level code and artifacts - -**Files to delete:** -- `run.py` -- `src/generate.py`, `src/fetcher_api.py`, `src/generator_api.py` -- `src/data/ARCHITECTURE.md` -- `widget_*.svg` at repo root -- `data/ghstats.db` -- `REPORT.md`, `CHANGELOG.md` (stale) -- `src/README.md` - -Do NOT delete yet: `src/config.py`, `src/widgets/`, `src/themes/`, `src/models/`, `src/utils/`, `src/data/fetcher.py`, `src/data/processor.py`, `src/db/`. These move in later phases via `git mv`. - -- [ ] **Step 1: Stage removals** - -```bash -git rm run.py src/generate.py src/fetcher_api.py src/generator_api.py -git rm src/data/ARCHITECTURE.md src/README.md -git rm REPORT.md CHANGELOG.md -git rm widget_achievements.svg widget_collaborators.svg widget_focus.svg widget_grade.svg widget_impact.svg widget_languages.svg widget_shaymanor.svg -git rm data/ghstats.db -``` - -- [ ] **Step 2: Verify no remaining imports of deleted modules** - -Run: `grep -rn "from src.generate\|from src.fetcher_api\|from src.generator_api\|import run" src/ 2>/dev/null || echo "clean"` -Expected: `clean` - -- [ ] **Step 3: Commit (ask user for approval first)** - -```bash -git commit -m "chore(cleanup): remove retired entry points and build artifacts" -``` - ---- - -## Phase 1 — Fetcher Service - -### Task 1.1: Fetcher scaffolding (requirements, config, pytest, Dockerfile) - -**Files:** -- Create: `fetcher/requirements.txt` -- Create: `fetcher/pytest.ini` -- Create: `fetcher/src/config.py` -- Create: `fetcher/Dockerfile` - -- [ ] **Step 1: `fetcher/requirements.txt`** - -``` -flask==3.0.3 -requests==2.32.3 -pytest==8.3.3 -responses==0.25.3 -gunicorn==23.0.0 -``` - -- [ ] **Step 2: `fetcher/pytest.ini`** - -```ini -[pytest] -testpaths = tests -python_files = test_*.py -pythonpath = . -addopts = -ra --strict-markers -``` - -- [ ] **Step 3: `fetcher/src/config.py`** - -```python -"""Fetcher service configuration, overridable via env vars.""" -import os - -PORT = int(os.getenv("FETCHER_PORT", "5001")) -DB_PATH = os.getenv("FETCHER_DB_PATH", "./data/fetcher.db") -GITHUB_PAT = os.getenv("GITHUB_PAT", "") -INTERNAL_TOKEN = os.getenv("FETCHER_INTERNAL_TOKEN", "") -REFRESH_INTERVAL_HOURS = int(os.getenv("FETCHER_REFRESH_INTERVAL_HOURS", "24")) -TRIAL_GC_DAYS = int(os.getenv("FETCHER_TRIAL_GC_DAYS", "7")) -API_TIMEOUT = int(os.getenv("API_TIMEOUT", "15")) -``` - -- [ ] **Step 4: `fetcher/Dockerfile`** - -```dockerfile -FROM python:3.11-slim - -WORKDIR /app -COPY requirements.txt ./ -RUN pip install --no-cache-dir -r requirements.txt - -COPY src ./src -RUN mkdir -p /app/data - -EXPOSE 5001 -ENV FETCHER_DB_PATH=/app/data/fetcher.db - -# Default is the API. Override CMD for cron: ["python", "-m", "src.cron"] -CMD ["gunicorn", "-b", "0.0.0.0:5001", "-w", "2", "src.api:app"] -``` - -- [ ] **Step 5: Commit** - -```bash -git add fetcher/requirements.txt fetcher/pytest.ini fetcher/src/config.py fetcher/Dockerfile -git commit -m "feat(fetcher): scaffold config, requirements, pytest, Dockerfile" -``` - ---- - -### Task 1.2: Fetcher `db.py` — schema, WAL, CRUD, payload hashing - -**Files:** -- Create: `fetcher/src/db.py` -- Create: `fetcher/tests/test_db.py` - -- [ ] **Step 1: Write the failing tests `fetcher/tests/test_db.py`** - -```python -import os -import tempfile -import pytest -from src import db as dbmod - - -@pytest.fixture -def tmp_db(monkeypatch): - with tempfile.TemporaryDirectory() as d: - path = os.path.join(d, "t.db") - monkeypatch.setattr(dbmod, "DB_PATH", path) - dbmod.init_db() - yield path - - -def test_init_creates_schema_with_wal(tmp_db): - with dbmod._connect() as c: - mode = c.execute("PRAGMA journal_mode").fetchone()[0] - assert mode.lower() == "wal" - cols = [r[1] for r in c.execute("PRAGMA table_info(users)").fetchall()] - assert set(cols) == {"username", "data_json", "payload_hash", - "fetched_at", "last_requested_at"} - - -def test_upsert_and_get_round_trip(tmp_db): - dbmod.upsert_user("alice", {"user": {"login": "alice"}, "repos": []}) - row = dbmod.get_user("alice") - assert row["data"]["user"]["login"] == "alice" - assert row["payload_hash"] - assert row["fetched_at"] - - -def test_payload_hash_is_deterministic_and_changes_on_diff(): - h1 = dbmod.payload_hash({"a": 1, "b": 2}) - h2 = dbmod.payload_hash({"b": 2, "a": 1}) - h3 = dbmod.payload_hash({"a": 1, "b": 3}) - assert h1 == h2 - assert h1 != h3 - - -def test_get_updates_last_requested_at(tmp_db): - dbmod.upsert_user("alice", {"user": {"login": "alice"}}) - first = dbmod.get_user("alice")["last_requested_at"] - import time; time.sleep(1.1) - second = dbmod.get_user("alice")["last_requested_at"] - assert second > first - - -def test_delete_stale_removes_old_rows(tmp_db): - dbmod.upsert_user("ghost", {"user": {"login": "ghost"}}) - # Manually backdate last_requested_at - with dbmod._connect() as c: - c.execute("UPDATE users SET last_requested_at='2020-01-01T00:00:00Z' WHERE username='ghost'") - removed = dbmod.delete_stale(days=7) - assert removed == 1 - assert dbmod.get_user("ghost") is None - - -def test_users_due_for_refresh(tmp_db): - dbmod.upsert_user("alice", {"user": {"login": "alice"}}) - # Backdate fetched_at so alice is due - with dbmod._connect() as c: - c.execute("UPDATE users SET fetched_at='2020-01-01T00:00:00Z' WHERE username='alice'") - due = dbmod.users_due_for_refresh(hours=24, active_within_days=7) - assert "alice" in due -``` - -- [ ] **Step 2: Run tests — expect failures** - -Run: `cd fetcher && pytest tests/test_db.py -v` -Expected: `ModuleNotFoundError` or similar (db module not yet complete). - -- [ ] **Step 3: Implement `fetcher/src/db.py`** - -```python -"""SQLite repository for the fetcher service. - -Single table: users (raw GitHub payloads keyed by username). -Uses WAL mode so concurrent readers don't block the writer. -""" -import hashlib -import json -import os -import sqlite3 -from contextlib import contextmanager -from datetime import datetime, timedelta -from typing import Optional - -from . import config - -DB_PATH = config.DB_PATH - -SCHEMA_SQL = """ -CREATE TABLE IF NOT EXISTS users ( - username TEXT PRIMARY KEY, - data_json TEXT NOT NULL, - payload_hash TEXT NOT NULL, - fetched_at TEXT NOT NULL, - last_requested_at TEXT NOT NULL -); -CREATE INDEX IF NOT EXISTS idx_users_fetched_at ON users(fetched_at); -CREATE INDEX IF NOT EXISTS idx_users_last_requested_at ON users(last_requested_at); -""" - - -def _now() -> str: - return datetime.utcnow().isoformat(timespec="seconds") + "Z" - - -@contextmanager -def _connect(): - os.makedirs(os.path.dirname(DB_PATH) or ".", exist_ok=True) - conn = sqlite3.connect(DB_PATH, timeout=10) - conn.row_factory = sqlite3.Row - conn.execute("PRAGMA journal_mode=WAL") - conn.execute("PRAGMA synchronous=NORMAL") - conn.execute("PRAGMA foreign_keys=ON") - conn.execute("PRAGMA busy_timeout=5000") - try: - yield conn - finally: - conn.close() - - -def init_db() -> None: - with _connect() as c: - c.executescript(SCHEMA_SQL) - c.commit() - - -def payload_hash(data: dict) -> str: - canonical = json.dumps(data, sort_keys=True, separators=(",", ":")) - return hashlib.sha256(canonical.encode("utf-8")).hexdigest() - - -def upsert_user(username: str, data: dict) -> str: - h = payload_hash(data) - now = _now() - with _connect() as c: - c.execute( - """INSERT INTO users(username, data_json, payload_hash, fetched_at, last_requested_at) - VALUES (?, ?, ?, ?, ?) - ON CONFLICT(username) DO UPDATE SET - data_json = excluded.data_json, - payload_hash = excluded.payload_hash, - fetched_at = excluded.fetched_at""", - (username, json.dumps(data), h, now, now), - ) - c.commit() - return h - - -def get_user(username: str) -> Optional[dict]: - """Returns {data, payload_hash, fetched_at, last_requested_at} or None. Updates last_requested_at.""" - now = _now() - with _connect() as c: - row = c.execute( - "SELECT data_json, payload_hash, fetched_at, last_requested_at FROM users WHERE username=?", - (username,), - ).fetchone() - if row is None: - return None - c.execute("UPDATE users SET last_requested_at=? WHERE username=?", (now, username)) - c.commit() - return { - "data": json.loads(row["data_json"]), - "payload_hash": row["payload_hash"], - "fetched_at": row["fetched_at"], - "last_requested_at": now, - } - - -def delete_stale(days: int) -> int: - cutoff = (datetime.utcnow() - timedelta(days=days)).isoformat(timespec="seconds") + "Z" - with _connect() as c: - cur = c.execute("DELETE FROM users WHERE last_requested_at < ?", (cutoff,)) - c.commit() - return cur.rowcount - - -def users_due_for_refresh(hours: int, active_within_days: int) -> list[str]: - """Return usernames with fetched_at older than `hours` and last_requested_at within `active_within_days`.""" - stale_before = (datetime.utcnow() - timedelta(hours=hours)).isoformat(timespec="seconds") + "Z" - active_after = (datetime.utcnow() - timedelta(days=active_within_days)).isoformat(timespec="seconds") + "Z" - with _connect() as c: - rows = c.execute( - "SELECT username FROM users WHERE fetched_at < ? AND last_requested_at > ?", - (stale_before, active_after), - ).fetchall() - return [r["username"] for r in rows] - - -def list_usernames() -> list[str]: - with _connect() as c: - return [r["username"] for r in c.execute("SELECT username FROM users ORDER BY username").fetchall()] -``` - -- [ ] **Step 4: Run tests — expect pass** - -Run: `cd fetcher && pytest tests/test_db.py -v` -Expected: all 6 tests PASS. - -- [ ] **Step 5: Commit** - -```bash -git add fetcher/src/db.py fetcher/tests/test_db.py -git commit -m "feat(fetcher): add SQLite db module with WAL, payload hashing, GC" -``` - ---- - -### Task 1.3: Fetcher `github.py` — move from `src/data/fetcher.py` - -**Files:** -- Move: `src/data/fetcher.py` → `fetcher/src/github.py` -- Create: `fetcher/tests/test_github.py` - -- [ ] **Step 1: Move file** - -```bash -git mv src/data/fetcher.py fetcher/src/github.py -``` - -- [ ] **Step 2: Adjust imports inside `fetcher/src/github.py`** - -Inside that file, change `from src.config import ...` to `from . import config` and update all config references to `config.NAME`. Remove any `src.models` or other generator-only imports if present (the fetcher only needs to produce raw JSON — no typed models). - -- [ ] **Step 3: Write smoke tests `fetcher/tests/test_github.py`** - -```python -import responses -import pytest -from src import github - - -@responses.activate -def test_fetch_user_data_returns_user_payload(): - responses.add( - responses.POST, "https://api.github.com/graphql", - json={"data": {"user": {"contributionsCollection": {"contributionCalendar": {"weeks": []}}}}}, - status=200, - ) - responses.add( - responses.GET, "https://api.github.com/users/alice", - json={"login": "alice", "public_repos": 3, "followers": 5, "avatar_url": "https://avatars.example/1"}, - status=200, - ) - responses.add( - responses.GET, "https://api.github.com/users/alice/repos", - json=[], status=200, - ) - responses.add( - responses.GET, "https://api.github.com/users/alice/events", - json=[], status=200, - ) - data = github.fetch_github_data("alice", token="t") - assert data["user"]["login"] == "alice" - assert "repos" in data - assert "events" in data - - -@responses.activate -def test_fetch_handles_404(): - responses.add( - responses.GET, "https://api.github.com/users/nope", - json={"message": "Not Found"}, status=404, - ) - responses.add( - responses.POST, "https://api.github.com/graphql", - json={"data": {"user": None}}, status=200, - ) - responses.add( - responses.GET, "https://api.github.com/users/nope/repos", - json={"message": "Not Found"}, status=404, - ) - responses.add( - responses.GET, "https://api.github.com/users/nope/events", - json={"message": "Not Found"}, status=404, - ) - data = github.fetch_github_data("nope", token="t") - # The existing fetcher returns whatever GitHub gave it; we just assert it doesn't crash - # and the caller can detect not_found via data["user"].get("message") - assert data["user"].get("message") == "Not Found" or data["user"] is None -``` - -- [ ] **Step 4: Run tests** - -Run: `cd fetcher && pytest tests/test_github.py -v` -Expected: both PASS. If the second fails because the existing code expects a specific shape, adjust the test to match the actual return shape (read `fetcher/src/github.py` to see what it returns on 404). - -- [ ] **Step 5: Commit** - -```bash -git add fetcher/src/github.py fetcher/tests/test_github.py -git commit -m "feat(fetcher): move GitHub client into fetcher service" -``` - ---- - -### Task 1.4: Fetcher `api.py` — Flask routes + shared-secret auth - -**Files:** -- Create: `fetcher/src/api.py` -- Create: `fetcher/tests/test_api.py` - -- [ ] **Step 1: Write failing tests `fetcher/tests/test_api.py`** - -```python -import os -import tempfile -import pytest -import responses as resp_lib -from src import db as dbmod -from src import api as apimod - - -@pytest.fixture -def client(monkeypatch): - with tempfile.TemporaryDirectory() as d: - monkeypatch.setattr(dbmod, "DB_PATH", os.path.join(d, "t.db")) - monkeypatch.setattr(apimod.config, "INTERNAL_TOKEN", "secret") - monkeypatch.setattr(apimod.config, "GITHUB_PAT", "ghp_test") - dbmod.init_db() - app = apimod.app - app.config["TESTING"] = True - with app.test_client() as c: - yield c - - -def test_health_no_auth_required(client): - r = client.get("/health") - assert r.status_code == 200 - assert r.get_json()["service"] == "fetcher" - - -def test_endpoints_require_internal_token(client): - r = client.get("/data/alice") - assert r.status_code == 401 - - -@resp_lib.activate -def test_data_auto_fetches_on_miss(client, monkeypatch): - resp_lib.add(resp_lib.POST, "https://api.github.com/graphql", - json={"data": {"user": {"contributionsCollection": {"contributionCalendar": {"weeks": []}}}}}, status=200) - resp_lib.add(resp_lib.GET, "https://api.github.com/users/alice", - json={"login": "alice", "public_repos": 1, "followers": 0, "avatar_url": "https://avatars.example/a"}, status=200) - resp_lib.add(resp_lib.GET, "https://api.github.com/users/alice/repos", json=[], status=200) - resp_lib.add(resp_lib.GET, "https://api.github.com/users/alice/events", json=[], status=200) - - r = client.get("/data/alice", headers={"X-Internal-Token": "secret"}) - assert r.status_code == 200 - body = r.get_json() - assert body["payload_hash"] - assert body["data"]["user"]["login"] == "alice" - - -def test_force_fetch_requires_auth(client): - r = client.post("/fetch", json={"username": "alice"}) - assert r.status_code == 401 -``` - -- [ ] **Step 2: Run — expect fail** - -Run: `cd fetcher && pytest tests/test_api.py -v` -Expected: import error (no api module yet). - -- [ ] **Step 3: Implement `fetcher/src/api.py`** - -```python -"""Flask app for the fetcher service. All endpoints require X-Internal-Token.""" -import hmac -import requests -from flask import Flask, jsonify, request, Response -from functools import wraps - -from . import config, db, github - -app = Flask(__name__) - - -def require_internal_token(fn): - @wraps(fn) - def wrapper(*args, **kwargs): - token = request.headers.get("X-Internal-Token", "") - if not config.INTERNAL_TOKEN or not hmac.compare_digest(token, config.INTERNAL_TOKEN): - return jsonify({"error": "unauthorized"}), 401 - return fn(*args, **kwargs) - return wrapper - - -@app.route("/health", methods=["GET"]) -def health(): - return jsonify({"status": "ok", "service": "fetcher", "users": len(db.list_usernames())}) - - -@app.route("/data/", methods=["GET"]) -@require_internal_token -def get_data(username: str): - row = db.get_user(username) - if row is None: - # Auto-fetch path - data = github.fetch_github_data(username, token=config.GITHUB_PAT) - if _is_github_not_found(data): - data = {"error": "not_found"} - h = db.upsert_user(username, data) - return jsonify({"data": data, "payload_hash": h, "fetched": True}) - return jsonify({ - "data": row["data"], - "payload_hash": row["payload_hash"], - "fetched_at": row["fetched_at"], - "fetched": False, - }) - - -@app.route("/fetch", methods=["POST"]) -@require_internal_token -def force_fetch(): - body = request.get_json(silent=True) or {} - username = body.get("username") - if not username: - return jsonify({"error": "username required"}), 400 - try: - data = github.fetch_github_data(username, token=config.GITHUB_PAT) - except Exception as e: - return jsonify({"error": f"fetch failed: {e}"}), 502 - if _is_github_not_found(data): - data = {"error": "not_found"} - old = db.get_user(username) - old_hash = old["payload_hash"] if old else None - new_hash = db.upsert_user(username, data) - return jsonify({"stored": True, "payload_hash": new_hash, "changed": old_hash != new_hash}) - - -@app.route("/avatar/", methods=["GET"]) -@require_internal_token -def avatar(username: str): - r = requests.get(f"https://github.com/{username}.png", timeout=config.API_TIMEOUT, allow_redirects=True) - if r.status_code != 200: - return jsonify({"error": "avatar unavailable"}), 404 - return Response(r.content, mimetype=r.headers.get("Content-Type", "image/png")) - - -def _is_github_not_found(data: dict) -> bool: - user = data.get("user") - if user is None: - return True - if isinstance(user, dict) and user.get("message") == "Not Found": - return True - return False - - -def main(): - db.init_db() - app.run(host="0.0.0.0", port=config.PORT) - - -if __name__ == "__main__": - main() -``` - -- [ ] **Step 4: Run tests — expect pass** - -Run: `cd fetcher && pytest tests/test_api.py -v` -Expected: all PASS. - -- [ ] **Step 5: Commit** - -```bash -git add fetcher/src/api.py fetcher/tests/test_api.py -git commit -m "feat(fetcher): add Flask API with auth, auto-fetch, avatar" -``` - ---- - -### Task 1.5: Fetcher `cron.py` — scheduled refresh + GC - -**Files:** -- Create: `fetcher/src/cron.py` -- Create: `fetcher/tests/test_cron.py` - -- [ ] **Step 1: Write failing tests** - -```python -import os -import tempfile -import pytest -from unittest.mock import patch -from src import db as dbmod -from src import cron as cronmod - - -@pytest.fixture -def tmp_db(monkeypatch): - with tempfile.TemporaryDirectory() as d: - monkeypatch.setattr(dbmod, "DB_PATH", os.path.join(d, "t.db")) - dbmod.init_db() - yield - - -def test_refresh_tick_calls_github_for_due_users(tmp_db): - dbmod.upsert_user("alice", {"user": {"login": "alice"}}) - # Backdate - with dbmod._connect() as c: - c.execute("UPDATE users SET fetched_at='2020-01-01T00:00:00Z', last_requested_at=? WHERE username='alice'", - (dbmod._now(),)) - with patch("src.cron.github.fetch_github_data", return_value={"user": {"login": "alice", "updated": True}}) as gh: - stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7) - assert gh.called - assert stats["refreshed"] == 1 - - -def test_tick_runs_gc_for_abandoned_trial_users(tmp_db): - dbmod.upsert_user("ghost", {"user": {"login": "ghost"}}) - with dbmod._connect() as c: - c.execute("UPDATE users SET last_requested_at='2020-01-01T00:00:00Z' WHERE username='ghost'") - stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7) - assert stats["gc_removed"] == 1 - assert dbmod.get_user("ghost") is None -``` - -- [ ] **Step 2: Run — expect fail** - -Run: `cd fetcher && pytest tests/test_cron.py -v` -Expected: fail. - -- [ ] **Step 3: Implement `fetcher/src/cron.py`** - -```python -"""Scheduled refresh + GC loop. Run as its own container (CMD override).""" -import logging -import time - -from . import config, db, github - -log = logging.getLogger("fetcher.cron") - - -def tick(hours: int, active_within_days: int, gc_days: int) -> dict: - """Refresh due users, then GC abandoned ones. Returns counts.""" - refreshed = 0 - failed = 0 - due = db.users_due_for_refresh(hours=hours, active_within_days=active_within_days) - for username in due: - try: - data = github.fetch_github_data(username, token=config.GITHUB_PAT) - user = data.get("user") - if user is None or (isinstance(user, dict) and user.get("message") == "Not Found"): - data = {"error": "not_found"} - db.upsert_user(username, data) - refreshed += 1 - except Exception as e: - log.warning("refresh failed for %s: %s", username, e) - failed += 1 - gc_removed = db.delete_stale(days=gc_days) - return {"refreshed": refreshed, "failed": failed, "gc_removed": gc_removed} - - -def main(): - logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") - db.init_db() - interval = 3600 # 1h between ticks - while True: - try: - stats = tick( - hours=config.REFRESH_INTERVAL_HOURS, - active_within_days=config.TRIAL_GC_DAYS, - gc_days=config.TRIAL_GC_DAYS, - ) - log.info("tick complete: %s", stats) - except Exception: - log.exception("tick failed") - time.sleep(interval) - - -if __name__ == "__main__": - main() -``` - -- [ ] **Step 4: Run tests — expect pass** - -Run: `cd fetcher && pytest tests/test_cron.py -v` -Expected: PASS. - -- [ ] **Step 5: Write `fetcher/README.md`** - -```markdown -# Fetcher Service - -Owns every GitHub API interaction. Holds the PAT. Exposes cached payloads -over an internal-only HTTP API protected by a shared secret. - -## Run locally - - cd fetcher - pip install -r requirements.txt - FETCHER_INTERNAL_TOKEN=dev GITHUB_PAT=ghp_xxx python -m src.api - -Cron (separate process): - - FETCHER_INTERNAL_TOKEN=dev GITHUB_PAT=ghp_xxx python -m src.cron - -## Test - - cd fetcher && pytest - -## Docker - - docker build -t ghstats-fetcher . - docker run -e GITHUB_PAT=ghp_xxx -e FETCHER_INTERNAL_TOKEN=dev -p 5001:5001 -v $(pwd)/data:/app/data ghstats-fetcher - # cron: override CMD to ["python","-m","src.cron"] - -## Endpoints - -| Method | Path | Auth | Description | -|---|---|---|---| -| GET | /health | none | health check | -| GET | /data/ | X-Internal-Token | return stored payload; auto-fetches on miss | -| POST | /fetch | X-Internal-Token | force re-fetch (body: {username}) | -| GET | /avatar/ | X-Internal-Token | proxied avatar bytes | - -## Env - -See `src/config.py`. -``` - -- [ ] **Step 6: Commit** - -```bash -git add fetcher/src/cron.py fetcher/tests/test_cron.py fetcher/README.md -git commit -m "feat(fetcher): add cron refresh + GC and service README" -``` - ---- - -## Phase 2 — Generator: DB, Cache, Placeholder, Processor, Fetcher client - -### Task 2.1: Generator scaffolding + move widgets/themes/models/utils/processor - -**Files:** -- Create: `generator/requirements.txt`, `generator/pytest.ini` -- Move: `src/widgets/` → `generator/src/widgets/` -- Move: `src/themes/` → `generator/src/themes/` -- Move: `src/models/` → `generator/src/models/` -- Move: `src/utils/` → `generator/src/utils/` -- Move: `src/data/processor.py` → `generator/src/processor.py` -- Move: `src/config.py` → `generator/src/config.py` (merge with new env vars) - -- [ ] **Step 1: `generator/requirements.txt`** - -``` -flask==3.0.3 -flask-caching==2.3.0 -flask-compress==1.15 -requests==2.32.3 -pytest==8.3.3 -responses==0.25.3 -gunicorn==23.0.0 -``` - -- [ ] **Step 2: `generator/pytest.ini`** - -```ini -[pytest] -testpaths = tests -python_files = test_*.py -pythonpath = . -addopts = -ra --strict-markers -``` - -- [ ] **Step 3: Move existing modules** - -```bash -git mv src/widgets generator/src/widgets -git mv src/themes generator/src/themes -git mv src/models generator/src/models -git mv src/utils generator/src/utils -git mv src/data/processor.py generator/src/processor.py -git mv src/config.py generator/src/config.py -``` - -- [ ] **Step 4: Adjust imports in moved files** - -In every moved `.py` file under `generator/src/`, replace: - -- `from src.config` → `from .. import config` (if in subpackage) or `from . import config` (if at top level) -- `from src.widgets` → `from .widgets` (in top-level modules) or `from ..widgets` (in subpackages) -- `from src.themes`, `from src.models`, `from src.utils` similarly -- `from src.data.processor` → `from . import processor` - -Run a sweep: - -```bash -grep -rn "from src\." generator/src/ && echo "FIX THESE" || echo "clean" -``` - -Fix each reported line until the grep prints `clean`. - -- [ ] **Step 5: Extend `generator/src/config.py` with new env vars** - -Append to existing config.py: - -```python -# --- Service-specific additions (v2 of config) --- - -PORT = int(os.getenv("GENERATOR_PORT", "5002")) -SETTINGS_DB_PATH = os.getenv("GENERATOR_SETTINGS_DB_PATH", "./data/settings.db") -WIDGETS_DB_PATH = os.getenv("GENERATOR_WIDGETS_DB_PATH", "./data/widgets.db") -FETCHER_URL = os.getenv("FETCHER_URL", "http://localhost:5001") -FETCHER_INTERNAL_TOKEN = os.getenv("FETCHER_INTERNAL_TOKEN", "") -REDIS_URL = os.getenv("REDIS_URL", "") -ENROLLMENT_DAILY_CAP = int(os.getenv("ENROLLMENT_DAILY_CAP", "50")) -WIDGET_LRU_PER_USER = int(os.getenv("WIDGET_LRU_PER_USER", "10")) -POLL_INTERVAL_MINUTES = int(os.getenv("GENERATOR_POLL_INTERVAL_MINUTES", "15")) -``` - -- [ ] **Step 6: Delete now-empty `src/` tree** - -```bash -# After moves above, src/data/__init__.py and src/__init__.py still exist. -# Remove the retired db module and empty src/ -git rm -r src/db src/data src/__init__.py -rmdir src 2>/dev/null || true -``` - -- [ ] **Step 7: Smoke-check generator-side imports load** - -```bash -cd generator && python -c "from src import processor, widgets, themes, models, utils, config; print('ok')" -``` - -Expected: `ok`. If not, fix the failing import and repeat. - -- [ ] **Step 8: Commit** - -```bash -git add generator/requirements.txt generator/pytest.ini generator/src/ -A -git add -u src/ 2>/dev/null || true -git commit -m "feat(generator): scaffold + move widgets/themes/models/utils/processor/config" -``` - ---- - -### Task 2.2: Generator `db.py` — settings + widgets + jobs + enrollments - -**Files:** -- Create: `generator/src/db.py` -- Create: `generator/tests/test_db.py` - -- [ ] **Step 1: Write failing tests `generator/tests/test_db.py`** - -```python -import os -import tempfile -import pytest -from src import db as dbmod - - -@pytest.fixture -def tmp_dbs(monkeypatch): - with tempfile.TemporaryDirectory() as d: - monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db")) - monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db")) - dbmod.init_dbs() - yield d - - -def test_settings_hash_is_deterministic(): - h1 = dbmod.settings_hash({"theme": "dark", "widgets": ["a", "b"]}) - h2 = dbmod.settings_hash({"widgets": ["a", "b"], "theme": "dark"}) - assert h1 == h2 - - -def test_enroll_and_get_settings(tmp_dbs): - defaults = {"theme": "dark", "widgets": ["grade"]} - job_id = dbmod.enroll("alice", defaults) - assert job_id > 0 - s = dbmod.get_settings("alice") - assert s["settings"] == defaults - assert s["manual_refresh_used"] == 0 - - -def test_enrollments_daily_counter(tmp_dbs): - dbmod.enroll("alice", {"theme": "dark"}) - dbmod.enroll("bob", {"theme": "dark"}) - assert dbmod.enrollments_today() == 2 - - -def test_update_settings_changes_hash(tmp_dbs): - dbmod.enroll("alice", {"theme": "dark"}) - h_before = dbmod.get_settings("alice")["settings_hash"] - dbmod.update_settings("alice", {"theme": "light"}) - h_after = dbmod.get_settings("alice")["settings_hash"] - assert h_before != h_after - - -def test_claim_job_marks_running(tmp_dbs): - job_id = dbmod.enroll("alice", {"theme": "dark"}) - job = dbmod.claim_next_job() - assert job["id"] == job_id - assert job["status"] == "running" - assert dbmod.claim_next_job() is None # already running - - -def test_complete_job(tmp_dbs): - jid = dbmod.enroll("alice", {"theme": "dark"}) - dbmod.claim_next_job() - dbmod.complete_job(jid) - with dbmod._settings_conn() as c: - status = c.execute("SELECT status FROM jobs WHERE id=?", (jid,)).fetchone()[0] - assert status == "done" - - -def test_widgets_put_and_get(tmp_dbs): - dbmod.put_widgets("alice", "hash1", {"composite": "", "grade": ""}) - row = dbmod.get_current_widget("alice", "composite") - assert row == "" - - -def test_current_widget_flips_atomically(tmp_dbs): - dbmod.put_widgets("alice", "hash1", {"composite": "v1"}) - dbmod.put_widgets("alice", "hash2", {"composite": "v2"}) - assert dbmod.get_current_widget("alice", "composite") == "v2" - - -def test_lru_trim_keeps_n_newest(tmp_dbs): - for i in range(12): - dbmod.put_widgets("alice", f"hash{i}", {"composite": f"{i}"}) - dbmod.lru_trim("alice", keep=10) - with dbmod._widgets_conn() as c: - n = c.execute("SELECT COUNT(DISTINCT settings_hash) FROM widgets WHERE username='alice'").fetchone()[0] - assert n == 10 - - -def test_reclaim_stuck_running_jobs(tmp_dbs): - jid = dbmod.enroll("alice", {"theme": "dark"}) - dbmod.claim_next_job() - # Backdate updated_at to simulate dead worker - with dbmod._settings_conn() as c: - c.execute("UPDATE jobs SET updated_at='2020-01-01T00:00:00Z' WHERE id=?", (jid,)) - c.commit() - dbmod.reclaim_stuck_jobs(older_than_minutes=10) - job = dbmod.claim_next_job() - assert job["id"] == jid -``` - -- [ ] **Step 2: Run — expect fail** - -- [ ] **Step 3: Implement `generator/src/db.py`** - -```python -"""Generator's two SQLite DBs: settings.db (settings + jobs + enrollments) and widgets.db (svg cache). - -All access goes through this module so a future Postgres swap is local to here. -Both DBs are opened with WAL mode. -""" -import hashlib -import json -import os -import sqlite3 -from contextlib import contextmanager -from datetime import datetime, timedelta -from typing import Optional - -from . import config - -SETTINGS_DB_PATH = config.SETTINGS_DB_PATH -WIDGETS_DB_PATH = config.WIDGETS_DB_PATH - -_SETTINGS_SCHEMA = """ -CREATE TABLE IF NOT EXISTS users ( - username TEXT PRIMARY KEY, - settings_json TEXT NOT NULL, - settings_hash TEXT NOT NULL, - enrolled_at TEXT NOT NULL, - last_fetcher_payload_hash TEXT, - manual_refresh_used INTEGER DEFAULT 0, - last_requested_at TEXT NOT NULL -); -CREATE TABLE IF NOT EXISTS enrollments_daily ( - day TEXT PRIMARY KEY, - count INTEGER NOT NULL -); -CREATE TABLE IF NOT EXISTS jobs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - kind TEXT NOT NULL, - username TEXT NOT NULL, - status TEXT NOT NULL, - attempts INTEGER DEFAULT 0, - last_error TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL -); -CREATE INDEX IF NOT EXISTS idx_jobs_pending ON jobs(status, created_at); -""" - -_WIDGETS_SCHEMA = """ -CREATE TABLE IF NOT EXISTS widgets ( - username TEXT NOT NULL, - settings_hash TEXT NOT NULL, - widget_name TEXT NOT NULL, - svg TEXT NOT NULL, - built_at TEXT NOT NULL, - PRIMARY KEY (username, settings_hash, widget_name) -); -CREATE TABLE IF NOT EXISTS current_widget ( - username TEXT PRIMARY KEY, - settings_hash TEXT NOT NULL, - updated_at TEXT NOT NULL -); -CREATE INDEX IF NOT EXISTS idx_widgets_username ON widgets(username); -""" - - -def _now() -> str: - return datetime.utcnow().isoformat(timespec="seconds") + "Z" - - -def _today() -> str: - return datetime.utcnow().strftime("%Y-%m-%d") - - -def _open(path: str) -> sqlite3.Connection: - os.makedirs(os.path.dirname(path) or ".", exist_ok=True) - conn = sqlite3.connect(path, timeout=10) - conn.row_factory = sqlite3.Row - conn.execute("PRAGMA journal_mode=WAL") - conn.execute("PRAGMA synchronous=NORMAL") - conn.execute("PRAGMA foreign_keys=ON") - conn.execute("PRAGMA busy_timeout=5000") - return conn - - -@contextmanager -def _settings_conn(): - conn = _open(SETTINGS_DB_PATH) - try: - yield conn - finally: - conn.close() - - -@contextmanager -def _widgets_conn(): - conn = _open(WIDGETS_DB_PATH) - try: - yield conn - finally: - conn.close() - - -def init_dbs() -> None: - with _settings_conn() as c: - c.executescript(_SETTINGS_SCHEMA) - c.commit() - with _widgets_conn() as c: - c.executescript(_WIDGETS_SCHEMA) - c.commit() - - -def settings_hash(settings: dict) -> str: - canonical = json.dumps(settings, sort_keys=True, separators=(",", ":")) - return hashlib.sha256(canonical.encode("utf-8")).hexdigest() - - -# ---- settings / enrollment ---- - -def enroll(username: str, defaults: dict) -> int: - """Insert a settings row + increment daily counter + enqueue build. Returns job_id.""" - sh = settings_hash(defaults) - now = _now() - with _settings_conn() as c: - c.execute( - """INSERT INTO users(username, settings_json, settings_hash, enrolled_at, last_requested_at) - VALUES (?, ?, ?, ?, ?) - ON CONFLICT(username) DO NOTHING""", - (username, json.dumps(defaults), sh, now, now), - ) - c.execute( - """INSERT INTO enrollments_daily(day, count) VALUES (?, 1) - ON CONFLICT(day) DO UPDATE SET count = count + 1""", - (_today(),), - ) - cur = c.execute( - """INSERT INTO jobs(kind, username, status, created_at, updated_at) - VALUES ('build', ?, 'pending', ?, ?)""", - (username, now, now), - ) - c.commit() - return cur.lastrowid - - -def enrollments_today() -> int: - with _settings_conn() as c: - row = c.execute("SELECT count FROM enrollments_daily WHERE day=?", (_today(),)).fetchone() - return row["count"] if row else 0 - - -def get_settings(username: str) -> Optional[dict]: - with _settings_conn() as c: - row = c.execute("SELECT * FROM users WHERE username=?", (username,)).fetchone() - if row is None: - return None - return { - "settings": json.loads(row["settings_json"]), - "settings_hash": row["settings_hash"], - "manual_refresh_used": row["manual_refresh_used"], - "last_fetcher_payload_hash": row["last_fetcher_payload_hash"], - "enrolled_at": row["enrolled_at"], - } - - -def update_settings(username: str, new_settings: dict) -> int: - """Updates settings + enqueues rebuild. Returns job_id.""" - sh = settings_hash(new_settings) - now = _now() - with _settings_conn() as c: - c.execute( - "UPDATE users SET settings_json=?, settings_hash=? WHERE username=?", - (json.dumps(new_settings), sh, username), - ) - cur = c.execute( - """INSERT INTO jobs(kind, username, status, created_at, updated_at) - VALUES ('build', ?, 'pending', ?, ?)""", - (username, now, now), - ) - c.commit() - return cur.lastrowid - - -def mark_manual_refresh(username: str) -> bool: - """Sets manual_refresh_used=1 only if it was 0. Returns True if successfully flipped.""" - with _settings_conn() as c: - cur = c.execute( - "UPDATE users SET manual_refresh_used=1 WHERE username=? AND manual_refresh_used=0", - (username,), - ) - c.commit() - return cur.rowcount == 1 - - -def touch_last_requested(username: str) -> None: - with _settings_conn() as c: - c.execute("UPDATE users SET last_requested_at=? WHERE username=?", (_now(), username)) - c.commit() - - -def list_enrolled() -> list[str]: - with _settings_conn() as c: - return [r["username"] for r in c.execute("SELECT username FROM users").fetchall()] - - -def set_last_fetcher_hash(username: str, h: str) -> None: - with _settings_conn() as c: - c.execute("UPDATE users SET last_fetcher_payload_hash=? WHERE username=?", (h, username)) - c.commit() - - -# ---- jobs ---- - -def enqueue_build(username: str) -> int: - now = _now() - with _settings_conn() as c: - cur = c.execute( - """INSERT INTO jobs(kind, username, status, created_at, updated_at) - VALUES ('build', ?, 'pending', ?, ?)""", - (username, now, now), - ) - c.commit() - return cur.lastrowid - - -def claim_next_job() -> Optional[dict]: - now = _now() - with _settings_conn() as c: - row = c.execute( - "SELECT * FROM jobs WHERE status='pending' ORDER BY created_at LIMIT 1" - ).fetchone() - if row is None: - return None - c.execute("UPDATE jobs SET status='running', updated_at=?, attempts=attempts+1 WHERE id=?", - (now, row["id"])) - c.commit() - return {"id": row["id"], "username": row["username"], "kind": row["kind"], "status": "running"} - - -def complete_job(job_id: int) -> None: - with _settings_conn() as c: - c.execute("UPDATE jobs SET status='done', updated_at=? WHERE id=?", (_now(), job_id)) - c.commit() - - -def fail_job(job_id: int, err: str, retry: bool) -> None: - status = "pending" if retry else "failed" - with _settings_conn() as c: - c.execute("UPDATE jobs SET status=?, last_error=?, updated_at=? WHERE id=?", - (status, err, _now(), job_id)) - c.commit() - - -def reclaim_stuck_jobs(older_than_minutes: int) -> int: - cutoff = (datetime.utcnow() - timedelta(minutes=older_than_minutes)).isoformat(timespec="seconds") + "Z" - with _settings_conn() as c: - cur = c.execute( - "UPDATE jobs SET status='pending', updated_at=? WHERE status='running' AND updated_at < ?", - (_now(), cutoff), - ) - c.commit() - return cur.rowcount - - -# ---- widgets ---- - -def put_widgets(username: str, hash_: str, widgets: dict[str, str]) -> None: - """Insert widget rows for this (user, hash) and flip current_widget to point at it.""" - now = _now() - with _widgets_conn() as c: - for name, svg in widgets.items(): - c.execute( - """INSERT INTO widgets(username, settings_hash, widget_name, svg, built_at) - VALUES (?, ?, ?, ?, ?) - ON CONFLICT(username, settings_hash, widget_name) DO UPDATE SET - svg = excluded.svg, built_at = excluded.built_at""", - (username, hash_, name, svg, now), - ) - c.execute( - """INSERT INTO current_widget(username, settings_hash, updated_at) - VALUES (?, ?, ?) - ON CONFLICT(username) DO UPDATE SET settings_hash=excluded.settings_hash, updated_at=excluded.updated_at""", - (username, hash_, now), - ) - c.commit() - - -def get_current_widget(username: str, widget_name: str) -> Optional[str]: - with _widgets_conn() as c: - row = c.execute( - """SELECT w.svg FROM widgets w - JOIN current_widget cw ON cw.username=w.username AND cw.settings_hash=w.settings_hash - WHERE w.username=? AND w.widget_name=?""", - (username, widget_name), - ).fetchone() - return row["svg"] if row else None - - -def lru_trim(username: str, keep: int) -> int: - """Keep only the N newest settings_hashes per user; delete older rows.""" - with _widgets_conn() as c: - hashes = [r["settings_hash"] for r in c.execute( - """SELECT settings_hash, MAX(built_at) AS latest FROM widgets - WHERE username=? GROUP BY settings_hash ORDER BY latest DESC""", - (username,), - ).fetchall()] - to_delete = hashes[keep:] - if not to_delete: - return 0 - placeholders = ",".join("?" * len(to_delete)) - cur = c.execute( - f"DELETE FROM widgets WHERE username=? AND settings_hash IN ({placeholders})", - (username, *to_delete), - ) - c.commit() - return cur.rowcount -``` - -- [ ] **Step 4: Run tests — expect pass** - -Run: `cd generator && pytest tests/test_db.py -v` -Expected: all 10 tests PASS. - -- [ ] **Step 5: Commit** - -```bash -git add generator/src/db.py generator/tests/test_db.py -git commit -m "feat(generator): add settings+widgets db with jobs, enrollment, LRU" -``` - ---- - -### Task 2.3: Generator `cache.py` — no-op wrapper (Redis later) - -**Files:** -- Create: `generator/src/cache.py` -- Create: `generator/tests/test_cache.py` - -- [ ] **Step 1: Tests** - -```python -from src import cache - - -def test_noop_when_redis_url_empty(monkeypatch): - monkeypatch.setattr(cache.config, "REDIS_URL", "") - c = cache.Cache() - assert c.get("k") is None - c.set("k", "v", 60) - c.delete("k") - assert c.get("k") is None # still None because no-op - - -def test_enabled_flag_reflects_config(monkeypatch): - monkeypatch.setattr(cache.config, "REDIS_URL", "") - assert cache.Cache().enabled is False -``` - -- [ ] **Step 2: Implement `generator/src/cache.py`** - -```python -"""Tiny cache wrapper. v1 = no-op (REDIS_URL empty). v2 = real Redis.""" -from typing import Optional - -from . import config - - -class Cache: - def __init__(self): - self.enabled = bool(config.REDIS_URL) - self._client = None - if self.enabled: - import redis # local import so v1 doesn't require redis in runtime path - self._client = redis.from_url(config.REDIS_URL, decode_responses=True) - - def get(self, key: str) -> Optional[str]: - if not self.enabled: - return None - try: - return self._client.get(key) - except Exception: - return None - - def set(self, key: str, value: str, ttl_seconds: int) -> None: - if not self.enabled: - return - try: - self._client.setex(key, ttl_seconds, value) - except Exception: - pass - - def delete(self, *keys: str) -> None: - if not self.enabled or not keys: - return - try: - self._client.delete(*keys) - except Exception: - pass -``` - -- [ ] **Step 3: Run tests — pass.** Commit: - -```bash -git add generator/src/cache.py generator/tests/test_cache.py -git commit -m "feat(generator): add no-op cache wrapper (enables Redis later via env)" -``` - ---- - -### Task 2.4: Generator `placeholder.py` — three SVG variants - -**Files:** -- Create: `generator/src/placeholder.py` -- Create: `generator/tests/test_placeholder.py` - -- [ ] **Step 1: Write failing tests** - -```python -from src import placeholder - - -def test_building_contains_username_and_svg_tag(): - svg = placeholder.render("building", "alice", theme="dark") - assert svg.startswith("" in svg - assert "alice" in svg - assert "Building" in svg or "building" in svg.lower() - - -def test_rate_limited_variant(): - svg = placeholder.render("rate_limited", "alice", theme="dark") - assert "tomorrow" in svg.lower() or "try again" in svg.lower() - - -def test_not_found_variant(): - svg = placeholder.render("not_found", "ghost", theme="dark") - assert "ghost" in svg - assert "doesn't exist" in svg or "not found" in svg.lower() - - -def test_unknown_variant_raises(): - import pytest - with pytest.raises(ValueError): - placeholder.render("bogus", "alice", theme="dark") -``` - -- [ ] **Step 2: Implement `generator/src/placeholder.py`** - -```python -"""Placeholder SVG renderer — three variants for unknown / rate-limited / not-found users. - -Uses the same card wrapper + theme system as real widgets so they look consistent. -""" -from .themes.themes import get_theme -from .utils.svg_helpers import card_wrapper - -_MESSAGES = { - "building": ("Building @{u}'s widget…", "This usually takes under a minute."), - "rate_limited": ("Too many new users today", "Try again tomorrow."), - "not_found": ("GitHub user @{u} doesn't exist", "Check the spelling of the username."), -} - - -def render(variant: str, username: str, theme: str = "dark") -> str: - if variant not in _MESSAGES: - raise ValueError(f"unknown placeholder variant: {variant}") - title_tpl, subtitle = _MESSAGES[variant] - title = title_tpl.format(u=username) - palette = get_theme(theme) - - body = f""" - {_esc(title)} - {_esc(subtitle)} -""" - return card_wrapper(body=body, width=400, height=120, theme_name=theme) - - -def _esc(s: str) -> str: - return (s.replace("&", "&").replace("<", "<") - .replace(">", ">").replace('"', """)) -``` - -Note: if `themes.get_theme` or `card_wrapper` have different signatures in the moved code, adjust accordingly — read `generator/src/themes/themes.py` and `generator/src/utils/svg_helpers.py` first and match the actual API. - -- [ ] **Step 3: Run tests — pass.** Commit: - -```bash -git add generator/src/placeholder.py generator/tests/test_placeholder.py -git commit -m "feat(generator): add three placeholder SVG variants" -``` - ---- - -### Task 2.5: Generator `fetcher_client.py` — HTTP client to fetcher service - -**Files:** -- Create: `generator/src/fetcher_client.py` -- Create: `generator/tests/test_fetcher_client.py` - -Rationale: the spec says "fetcher HTTP client inline" — but both `api.py` and `worker.py` need it, and worker should not import Flask. Keeping it as its own 40-line module is the cleanest resolution; it's not a new service, just a DRY helper. - -- [ ] **Step 1: Tests** - -```python -import responses -import pytest -from src import fetcher_client as fc - - -@pytest.fixture(autouse=True) -def cfg(monkeypatch): - monkeypatch.setattr(fc.config, "FETCHER_URL", "http://fetcher:5001") - monkeypatch.setattr(fc.config, "FETCHER_INTERNAL_TOKEN", "secret") - - -@responses.activate -def test_get_data_returns_payload_and_hash(): - responses.add( - responses.GET, "http://fetcher:5001/data/alice", - json={"data": {"user": {"login": "alice"}}, "payload_hash": "abc"}, - status=200, - ) - result = fc.get_data("alice") - assert result["payload_hash"] == "abc" - assert result["data"]["user"]["login"] == "alice" - - -@responses.activate -def test_get_data_sends_auth_header(): - responses.add(responses.GET, "http://fetcher:5001/data/alice", - json={"data": {}, "payload_hash": "h"}, status=200) - fc.get_data("alice") - assert responses.calls[0].request.headers["X-Internal-Token"] == "secret" - - -@responses.activate -def test_force_fetch(): - responses.add(responses.POST, "http://fetcher:5001/fetch", - json={"stored": True, "payload_hash": "new", "changed": True}, status=200) - r = fc.force_fetch("alice") - assert r["changed"] is True -``` - -- [ ] **Step 2: Implement `generator/src/fetcher_client.py`** - -```python -"""HTTP client to the fetcher service. Used by api.py, worker.py, cron.py.""" -import requests - -from . import config - - -def _headers() -> dict: - return {"X-Internal-Token": config.FETCHER_INTERNAL_TOKEN} - - -def get_data(username: str) -> dict: - r = requests.get(f"{config.FETCHER_URL}/data/{username}", headers=_headers(), timeout=30) - r.raise_for_status() - return r.json() - - -def force_fetch(username: str) -> dict: - r = requests.post(f"{config.FETCHER_URL}/fetch", headers=_headers(), - json={"username": username}, timeout=30) - r.raise_for_status() - return r.json() -``` - -- [ ] **Step 3: Run — pass. Commit:** - -```bash -git add generator/src/fetcher_client.py generator/tests/test_fetcher_client.py -git commit -m "feat(generator): add typed HTTP client to the fetcher service" -``` - ---- - -## Phase 3 — Generator API, Worker, Cron, Integration - -### Task 3.1: Generator `worker.py` — build worker loop - -**Files:** -- Create: `generator/src/worker.py` -- Create: `generator/tests/test_worker.py` - -- [ ] **Step 1: Tests** - -```python -import os, tempfile, pytest -from unittest.mock import patch, MagicMock -from src import db as dbmod -from src import worker - - -@pytest.fixture -def tmp_dbs(monkeypatch): - with tempfile.TemporaryDirectory() as d: - monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db")) - monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db")) - dbmod.init_dbs() - yield d - - -def test_process_one_renders_and_stores_widgets(tmp_dbs): - dbmod.enroll("alice", {"theme": "dark", "enabled": ["grade"]}) - fake_payload = {"user": {"login": "alice"}, "repos": [], "events": [], - "commits": [], "total_commits": 0, "recent_commits": [], - "total_prs": 0, "collaborators_data": [], "avatar_b64": ""} - with patch("src.worker.fetcher_client.get_data", - return_value={"data": fake_payload, "payload_hash": "h1"}), \ - patch("src.worker._render_widgets", - return_value={"composite": "c", "grade": "g"}): - worker.process_one() - svg = dbmod.get_current_widget("alice", "composite") - assert svg == "c" - - -def test_not_found_payload_renders_not_found_placeholder(tmp_dbs): - dbmod.enroll("ghost", {"theme": "dark"}) - with patch("src.worker.fetcher_client.get_data", - return_value={"data": {"error": "not_found"}, "payload_hash": "x"}): - worker.process_one() - svg = dbmod.get_current_widget("ghost", "composite") - assert svg and "ghost" in svg - - -def test_worker_retries_on_fetcher_failure(tmp_dbs): - dbmod.enroll("alice", {"theme": "dark"}) - with patch("src.worker.fetcher_client.get_data", side_effect=RuntimeError("boom")): - worker.process_one() - # Job should have been re-queued with an incremented attempt count - with dbmod._settings_conn() as c: - row = c.execute("SELECT status, attempts, last_error FROM jobs WHERE username='alice'").fetchone() - assert row["status"] == "pending" - assert row["attempts"] == 1 - assert "boom" in row["last_error"] - - -def test_worker_marks_failed_after_max_attempts(tmp_dbs): - dbmod.enroll("alice", {"theme": "dark"}) - with patch("src.worker.fetcher_client.get_data", side_effect=RuntimeError("boom")): - for _ in range(4): - worker.process_one() - with dbmod._settings_conn() as c: - row = c.execute("SELECT status, attempts FROM jobs WHERE username='alice'").fetchone() - assert row["status"] == "failed" - assert row["attempts"] >= 3 -``` - -- [ ] **Step 2: Implement `generator/src/worker.py`** - -```python -"""Build worker. Pulls pending jobs and renders widgets. - -Run as its own container: CMD python -m src.worker -""" -import logging -import time -from typing import Optional - -from . import cache, config, db, fetcher_client, placeholder, processor -from .widgets import compose_widget - -log = logging.getLogger("generator.worker") -MAX_ATTEMPTS = 3 - - -def _render_widgets(username: str, payload: dict, settings: dict) -> dict[str, str]: - """Wrap the existing processor pipeline.""" - enabled = settings.get("enabled") or config.ENABLED_WIDGETS - order = settings.get("widget_order") or config.WIDGET_ORDER - theme = settings.get("theme", "dark") - widgets = processor.generate_widgets_from_github( - payload, - theme=theme, - custom_tags=settings.get("custom_tags"), - hidden_languages=settings.get("hidden_languages"), - enabled=enabled, - widget_settings=settings.get("widget_settings") or {}, - ) - ordered = [w for w in order if w in enabled and w in widgets and widgets[w]] - composite = compose_widget( - widgets=widgets, enabled=ordered, theme_name=theme, - username=username, avatar_b64=payload.get("avatar_b64", ""), - ) - out = {name: svg for name, svg in widgets.items() if svg} - out["composite"] = composite - return out - - -def process_one() -> bool: - """Process one pending job. Returns True if a job was handled.""" - job = db.claim_next_job() - if job is None: - return False - username = job["username"] - try: - result = fetcher_client.get_data(username) - payload = result.get("data") or {} - if payload.get("error") == "not_found": - svg = placeholder.render("not_found", username, theme="dark") - db.put_widgets(username, "not_found", {"composite": svg}) - db.complete_job(job["id"]) - log.info("not_found marker persisted for %s", username) - return True - - settings_row = db.get_settings(username) - if settings_row is None: - db.fail_job(job["id"], "settings missing", retry=False) - return True - - widgets = _render_widgets(username, payload, settings_row["settings"]) - db.put_widgets(username, settings_row["settings_hash"], widgets) - db.set_last_fetcher_hash(username, result.get("payload_hash", "")) - db.lru_trim(username, config.WIDGET_LRU_PER_USER) - c = cache.Cache() - c.delete(f"widget:composite:{username}", *[f"widget:{n}:{username}" for n in widgets]) - db.complete_job(job["id"]) - log.info("built widgets for %s", username) - return True - except Exception as e: - retry = job.get("attempts", 0) < MAX_ATTEMPTS - db.fail_job(job["id"], str(e)[:500], retry=retry) - log.warning("build failed for %s (retry=%s): %s", username, retry, e) - return True - - -def main(): - logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") - db.init_dbs() - while True: - db.reclaim_stuck_jobs(older_than_minutes=10) - if not process_one(): - time.sleep(0.5) - - -if __name__ == "__main__": - main() -``` - -- [ ] **Step 3: Run tests — pass. Commit:** - -```bash -git add generator/src/worker.py generator/tests/test_worker.py -git commit -m "feat(generator): add build worker with retries and placeholder fallback" -``` - ---- - -### Task 3.2: Generator `api.py` — Flask routes + SPA static + auth stub - -**Files:** -- Create: `generator/src/api.py` -- Create: `generator/tests/test_api.py` - -- [ ] **Step 1: Tests** - -```python -import os, tempfile, pytest -from unittest.mock import patch -from src import db as dbmod -from src import api as apimod - - -@pytest.fixture -def client(monkeypatch): - with tempfile.TemporaryDirectory() as d: - monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db")) - monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db")) - dbmod.init_dbs() - app = apimod.app - app.config["TESTING"] = True - with app.test_client() as c: - yield c - - -def test_health(client): - r = client.get("/api/health") - assert r.status_code == 200 - assert r.get_json()["service"] == "generator" - - -def test_get_unknown_user_auto_enrolls_and_returns_placeholder(client): - r = client.get("/api/alice") - assert r.status_code == 200 - assert r.headers["X-Widget-Status"] == "building" - assert "Building" in r.data.decode() - assert dbmod.get_settings("alice") is not None - - -def test_rate_limit_returns_rate_limited_placeholder(client, monkeypatch): - monkeypatch.setattr(apimod.config, "ENROLLMENT_DAILY_CAP", 1) - client.get("/api/alice") # uses the one slot - r = client.get("/api/bob") - assert r.headers["X-Widget-Status"] == "rate_limited" - - -def test_enrolled_user_with_built_widget_returns_ready(client): - dbmod.enroll("alice", {"theme": "dark"}) - dbmod.put_widgets("alice", "h1", {"composite": "ready"}) - # Force settings_hash to match - with dbmod._settings_conn() as c: - c.execute("UPDATE users SET settings_hash='h1' WHERE username='alice'") - c.commit() - r = client.get("/api/alice") - assert r.status_code == 200 - assert r.headers["X-Widget-Status"] == "ready" - assert b"ready" in r.data - - -def test_settings_patch_enqueues_rebuild(client): - dbmod.enroll("alice", {"theme": "dark"}) - r = client.patch("/api/alice/settings", json={"theme": "light"}) - assert r.status_code == 200 - assert dbmod.get_settings("alice")["settings"]["theme"] == "light" - - -def test_refresh_is_one_shot(client): - dbmod.enroll("alice", {"theme": "dark"}) - with patch("src.api.fetcher_client.force_fetch", return_value={"changed": True, "payload_hash": "x", "stored": True}): - r1 = client.post("/api/alice/refresh") - assert r1.status_code == 200 - r2 = client.post("/api/alice/refresh") - assert r2.status_code == 409 - - -def test_not_found_status_header(client): - dbmod.enroll("ghost", {"theme": "dark"}) - dbmod.put_widgets("ghost", "not_found", {"composite": "404"}) - with dbmod._settings_conn() as c: - c.execute("UPDATE users SET settings_hash='not_found' WHERE username='ghost'") - c.commit() - r = client.get("/api/ghost") - assert r.headers["X-Widget-Status"] == "not_found" -``` - -- [ ] **Step 2: Implement `generator/src/api.py`** - -```python -"""Flask app for the generator service. - -Serves: - / -> React SPA (static/index.html) - /assets/

-> SPA bundles - /api/* -> JSON/SVG API -""" -import os -from functools import wraps -from flask import Flask, jsonify, request, Response, send_from_directory - -from . import config, db, fetcher_client, placeholder - -_STATIC_DIR = os.path.join(os.path.dirname(__file__), "static") - -app = Flask(__name__, static_folder=None) - - -# ---- auth stub (v1: no-op; swap to real OAuth later) ---- - -def require_auth(fn): - @wraps(fn) - def wrapper(*args, **kwargs): - # v1: honor system — no check. Future: verify Authorization header. - return fn(*args, **kwargs) - return wrapper - - -# ---- SPA ---- - -@app.route("/", defaults={"path": ""}) -@app.route("/") -def spa(path: str): - # Reserve /api/* for API; everything else serves the SPA. - if path.startswith("api/"): - return jsonify({"error": "not found"}), 404 - full = os.path.join(_STATIC_DIR, path) if path else os.path.join(_STATIC_DIR, "index.html") - if path and os.path.isfile(full): - return send_from_directory(_STATIC_DIR, path) - index = os.path.join(_STATIC_DIR, "index.html") - if os.path.isfile(index): - return send_from_directory(_STATIC_DIR, "index.html") - return jsonify({"error": "frontend not built"}), 503 - - -# ---- API ---- - -@app.route("/api/health", methods=["GET"]) -def health(): - return jsonify({"status": "ok", "service": "generator"}) - - -@app.route("/api/", methods=["GET"]) -def get_widget(username: str): - return _serve(username, widget_name="composite") - - -@app.route("/api//.svg", methods=["GET"]) -def get_widget_named(username: str, widget: str): - return _serve(username, widget_name=widget) - - -def _serve(username: str, widget_name: str) -> Response: - settings_row = db.get_settings(username) - - # Unknown user -> try to enroll - if settings_row is None: - if db.enrollments_today() >= config.ENROLLMENT_DAILY_CAP: - return _placeholder_response("rate_limited", username) - defaults = { - "theme": "dark", - "enabled": config.ENABLED_WIDGETS, - "widget_order": config.WIDGET_ORDER, - } - db.enroll(username, defaults) - return _placeholder_response("building", username) - - db.touch_last_requested(username) - svg = db.get_current_widget(username, widget_name) - if svg is None: - return _placeholder_response("building", username, theme=settings_row["settings"].get("theme", "dark")) - - # Detect "not_found" widget stashed by the worker - if settings_row.get("settings_hash") == "not_found": - return Response(svg, mimetype="image/svg+xml", - headers={"X-Widget-Status": "not_found", "Cache-Control": "no-store"}) - - return Response(svg, mimetype="image/svg+xml", - headers={"X-Widget-Status": "ready", - "Cache-Control": "public, max-age=3600"}) - - -def _placeholder_response(variant: str, username: str, theme: str = "dark") -> Response: - svg = placeholder.render(variant, username, theme=theme) - return Response(svg, mimetype="image/svg+xml", - headers={"X-Widget-Status": variant, "Cache-Control": "no-store"}) - - -@app.route("/api/enroll", methods=["POST"]) -def enroll_endpoint(): - body = request.get_json(silent=True) or {} - username = body.get("username") - if not username: - return jsonify({"error": "username required"}), 400 - if db.get_settings(username) is not None: - return jsonify({"error": "already_enrolled"}), 409 - if db.enrollments_today() >= config.ENROLLMENT_DAILY_CAP: - return jsonify({"error": "rate_limited"}), 429 - defaults = {"theme": "dark", "enabled": config.ENABLED_WIDGETS, "widget_order": config.WIDGET_ORDER} - job_id = db.enroll(username, defaults) - return jsonify({"enrolled": True, "job_id": job_id}) - - -@app.route("/api//settings", methods=["GET"]) -@require_auth -def get_settings(username: str): - s = db.get_settings(username) - if s is None: - return jsonify({"error": "not_enrolled"}), 404 - return jsonify(s) - - -@app.route("/api//settings", methods=["PATCH"]) -@require_auth -def patch_settings(username: str): - if db.get_settings(username) is None: - return jsonify({"error": "not_enrolled"}), 404 - body = request.get_json(silent=True) or {} - merged = {**db.get_settings(username)["settings"], **body} - job_id = db.update_settings(username, merged) - return jsonify({"updated": True, "job_id": job_id}) - - -@app.route("/api//refresh", methods=["POST"]) -@require_auth -def refresh(username: str): - s = db.get_settings(username) - if s is None: - return jsonify({"error": "not_enrolled"}), 404 - if not db.mark_manual_refresh(username): - return jsonify({"error": "already_used"}), 409 - try: - fetcher_client.force_fetch(username) - except Exception as e: - return jsonify({"error": f"fetch failed: {e}"}), 502 - job_id = db.enqueue_build(username) - return jsonify({"refreshed": True, "job_id": job_id}) - - -def main(): - db.init_dbs() - app.run(host="0.0.0.0", port=config.PORT) - - -if __name__ == "__main__": - main() -``` - -- [ ] **Step 3: Run tests — pass. Commit:** - -```bash -git add generator/src/api.py generator/tests/test_api.py -git commit -m "feat(generator): add Flask API with SPA static, auth stub, rate limit" -``` - ---- - -### Task 3.3: Generator `cron.py` — poll fetcher, enqueue rebuilds - -**Files:** -- Create: `generator/src/cron.py` -- Create: `generator/tests/test_cron.py` - -- [ ] **Step 1: Tests** - -```python -import os, tempfile, pytest -from unittest.mock import patch -from src import db as dbmod -from src import cron as cronmod - - -@pytest.fixture -def tmp_dbs(monkeypatch): - with tempfile.TemporaryDirectory() as d: - monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db")) - monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db")) - dbmod.init_dbs() - yield - - -def test_tick_enqueues_build_when_hash_changed(tmp_dbs): - dbmod.enroll("alice", {"theme": "dark"}) - dbmod.set_last_fetcher_hash("alice", "old") - # Clear initial enroll job - with dbmod._settings_conn() as c: - c.execute("DELETE FROM jobs") - c.commit() - with patch("src.cron.fetcher_client.get_data", - return_value={"data": {}, "payload_hash": "new"}): - stats = cronmod.tick() - assert stats["enqueued"] == 1 - - -def test_tick_skips_when_hash_unchanged(tmp_dbs): - dbmod.enroll("alice", {"theme": "dark"}) - dbmod.set_last_fetcher_hash("alice", "same") - with dbmod._settings_conn() as c: - c.execute("DELETE FROM jobs") - c.commit() - with patch("src.cron.fetcher_client.get_data", - return_value={"data": {}, "payload_hash": "same"}): - stats = cronmod.tick() - assert stats["enqueued"] == 0 -``` - -- [ ] **Step 2: Implement `generator/src/cron.py`** - -```python -"""Generator cron: poll fetcher for payload-hash changes, enqueue rebuilds. - -Run as its own container: CMD python -m src.cron -""" -import logging -import time - -from . import config, db, fetcher_client - -log = logging.getLogger("generator.cron") - - -def tick() -> dict: - enqueued = 0 - failed = 0 - for username in db.list_enrolled(): - try: - info = db.get_settings(username) - current = info.get("last_fetcher_payload_hash") - r = fetcher_client.get_data(username) - latest = r.get("payload_hash") - if latest and latest != current: - db.enqueue_build(username) - db.set_last_fetcher_hash(username, latest) - enqueued += 1 - except Exception as e: - log.warning("poll failed for %s: %s", username, e) - failed += 1 - return {"enqueued": enqueued, "failed": failed} - - -def main(): - logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") - db.init_dbs() - interval_s = config.POLL_INTERVAL_MINUTES * 60 - while True: - try: - log.info("tick: %s", tick()) - except Exception: - log.exception("tick failed") - time.sleep(interval_s) - - -if __name__ == "__main__": - main() -``` - -- [ ] **Step 3: Run tests — pass. Commit:** - -```bash -git add generator/src/cron.py generator/tests/test_cron.py -git commit -m "feat(generator): add poll cron that enqueues on payload-hash change" -``` - ---- - -### Task 3.4: Integration test + generator README - -**Files:** -- Create: `generator/tests/integration/test_end_to_end.py` -- Create: `generator/README.md` - -- [ ] **Step 1: Integration test** - -```python -"""End-to-end: api + worker in-process + mocked fetcher HTTP.""" -import os, tempfile, threading, time -import pytest -import responses -from unittest.mock import patch -from src import api as apimod, db as dbmod, worker - - -@pytest.fixture -def env(monkeypatch): - with tempfile.TemporaryDirectory() as d: - monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db")) - monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db")) - monkeypatch.setattr(apimod.config, "FETCHER_URL", "http://fetcher-mock") - monkeypatch.setattr(apimod.config, "FETCHER_INTERNAL_TOKEN", "t") - dbmod.init_dbs() - yield - - -@responses.activate -def test_first_request_builds_then_serves_ready(env): - responses.add( - responses.GET, "http://fetcher-mock/data/alice", - json={"data": {"user": {"login": "alice"}, "repos": [], "events": [], - "commits": [], "total_commits": 0, "recent_commits": [], - "total_prs": 0, "collaborators_data": [], "avatar_b64": ""}, - "payload_hash": "h1"}, status=200, - ) - client = apimod.app.test_client() - - r1 = client.get("/api/alice") - assert r1.headers["X-Widget-Status"] == "building" - - # Drain any pending jobs - for _ in range(5): - if not worker.process_one(): - break - - r2 = client.get("/api/alice") - assert r2.headers["X-Widget-Status"] == "ready" - assert r2.data.startswith(b" | none | composite SVG; auto-enrolls unknown users | -| GET | /api//.svg | none | individual widget SVG | -| POST | /api/enroll | none | explicit enroll (body: {username}) | -| GET | /api//settings | stub | current settings | -| PATCH | /api//settings | stub | update settings (enqueues rebuild) | -| POST | /api//refresh | stub | one-shot re-fetch + rebuild | - -Responses carry `X-Widget-Status: ready | building | rate_limited | not_found`. - -## Env - -See `src/config.py`. -``` - -- [ ] **Step 4: Commit** - -```bash -git add generator/tests/integration/test_end_to_end.py generator/README.md -git commit -m "test(generator): add end-to-end integration test + README" -``` - ---- - -## Phase 4 — Edge Service - -### Task 4.1: Edge scaffolding + requirements + Dockerfile - -**Files:** -- Create: `edge/requirements.txt`, `edge/pytest.ini`, `edge/Dockerfile` -- Create: `edge/src/config.py` - -- [ ] **Step 1: `edge/requirements.txt`** - -``` -flask==3.0.3 -flask-caching==2.3.0 -flask-compress==1.15 -requests==2.32.3 -pytest==8.3.3 -responses==0.25.3 -gunicorn==23.0.0 -``` - -- [ ] **Step 2: `edge/pytest.ini`** - -```ini -[pytest] -testpaths = tests -python_files = test_*.py -pythonpath = . -addopts = -ra --strict-markers -``` - -- [ ] **Step 3: `edge/src/config.py`** - -```python -import os - -PORT = int(os.getenv("EDGE_PORT", "5003")) -GENERATOR_URL = os.getenv("GENERATOR_URL", "http://localhost:5002") -CACHE_TYPE = os.getenv("CACHE_TYPE", "SimpleCache") -CACHE_REDIS_URL = os.getenv("CACHE_REDIS_URL", "") -CACHE_DEFAULT_TIMEOUT = int(os.getenv("CACHE_DEFAULT_TIMEOUT", "86400")) -CACHE_THRESHOLD = int(os.getenv("CACHE_THRESHOLD", "10000")) -UPSTREAM_TIMEOUT = int(os.getenv("EDGE_UPSTREAM_TIMEOUT", "10")) -``` - -- [ ] **Step 4: `edge/Dockerfile`** - -```dockerfile -FROM python:3.11-slim - -WORKDIR /app -COPY requirements.txt ./ -RUN pip install --no-cache-dir -r requirements.txt -COPY src ./src - -EXPOSE 5003 -CMD ["gunicorn", "-b", "0.0.0.0:5003", "-w", "2", "src.api:app"] -``` - -- [ ] **Step 5: Commit** - -```bash -git add edge/requirements.txt edge/pytest.ini edge/src/config.py edge/Dockerfile -git commit -m "feat(edge): scaffold config, requirements, Dockerfile" -``` - ---- - -### Task 4.2: Edge `cache.py` + `api.py` with Flask-Caching and Flask-Compress - -**Files:** -- Create: `edge/src/cache.py` -- Create: `edge/src/api.py` -- Create: `edge/tests/test_api.py` - -- [ ] **Step 1: Tests `edge/tests/test_api.py`** - -```python -import pytest -import responses -from src import api as apimod - - -@pytest.fixture -def client(monkeypatch): - monkeypatch.setattr(apimod.config, "GENERATOR_URL", "http://gen:5002") - # Reset cache between tests - apimod.cache_ext.clear() - apimod.app.config["TESTING"] = True - with apimod.app.test_client() as c: - yield c - - -@responses.activate -def test_miss_then_hit_serves_from_cache(client): - responses.add(responses.GET, "http://gen:5002/api/alice", - body=b"ready", status=200, - headers={"X-Widget-Status": "ready", "Content-Type": "image/svg+xml"}) - r1 = client.get("/alice") - assert r1.status_code == 200 - assert r1.data == b"ready" - # second call should not hit origin - responses.reset() - r2 = client.get("/alice") - assert r2.status_code == 200 - assert r2.data == b"ready" - - -@responses.activate -def test_placeholder_not_cached(client): - responses.add(responses.GET, "http://gen:5002/api/alice", - body=b"building", status=200, - headers={"X-Widget-Status": "building", "Content-Type": "image/svg+xml"}) - r1 = client.get("/alice") - assert r1.status_code == 200 - # Origin MUST be called again on the second request because building wasn't cached - r2 = client.get("/alice") - assert len(responses.calls) == 2 - - -@responses.activate -def test_origin_error_returns_503(client): - responses.add(responses.GET, "http://gen:5002/api/alice", status=500) - r = client.get("/alice") - assert r.status_code == 503 - - -def test_health(client): - r = client.get("/health") - assert r.status_code == 200 -``` - -- [ ] **Step 2: `edge/src/cache.py`** - -```python -"""Flask-Caching config. v1 = SimpleCache (in-process). Swap to RedisCache via env.""" -from flask_caching import Cache -from . import config - - -def build_cache(app) -> Cache: - cfg = { - "CACHE_TYPE": config.CACHE_TYPE, - "CACHE_DEFAULT_TIMEOUT": config.CACHE_DEFAULT_TIMEOUT, - "CACHE_THRESHOLD": config.CACHE_THRESHOLD, - } - if config.CACHE_TYPE == "RedisCache" and config.CACHE_REDIS_URL: - cfg["CACHE_REDIS_URL"] = config.CACHE_REDIS_URL - c = Cache(config=cfg) - c.init_app(app) - return c -``` - -- [ ] **Step 3: `edge/src/api.py`** - -```python -"""Edge service — cache-first SVG proxy in front of the generator.""" -import logging -import requests -from flask import Flask, Response, jsonify, request -from flask_compress import Compress - -from . import cache as cache_mod, config - -app = Flask(__name__) -Compress(app) -cache_ext = cache_mod.build_cache(app) -log = logging.getLogger("edge") - - -def _cache_key(path: str) -> str: - return f"edge:{path}" - - -def _fetch_origin(path: str) -> requests.Response: - url = f"{config.GENERATOR_URL}/api/{path}" - return requests.get(url, timeout=config.UPSTREAM_TIMEOUT) - - -@app.route("/health", methods=["GET"]) -def health(): - ok = True - try: - r = requests.get(f"{config.GENERATOR_URL}/api/health", timeout=config.UPSTREAM_TIMEOUT) - ok = r.status_code == 200 - except Exception: - ok = False - return jsonify({"service": "edge", "cache_type": config.CACHE_TYPE, "upstream_ok": ok}) - - -@app.route("/", methods=["GET"]) -def serve(username: str): - return _serve(username, path=username) - - -@app.route("//.svg", methods=["GET"]) -def serve_widget(username: str, widget: str): - return _serve(f"{username}/{widget}", path=f"{username}/{widget}.svg") - - -def _serve(key_suffix: str, path: str) -> Response: - ck = _cache_key(key_suffix) - cached = cache_ext.get(ck) - if cached is not None: - body, content_type = cached - return Response(body, mimetype=content_type, headers={ - "X-Widget-Status": "ready", - "X-Cache": "HIT", - "Cache-Control": "public, max-age=3600, s-maxage=86400, stale-while-revalidate=86400", - }) - - try: - r = _fetch_origin(path) - except Exception as e: - log.warning("origin unreachable for %s: %s", path, e) - return jsonify({"error": "origin unreachable"}), 503 - - if r.status_code >= 500: - return jsonify({"error": "origin error"}), 503 - - status = r.headers.get("X-Widget-Status", "ready") - content_type = r.headers.get("Content-Type", "image/svg+xml") - if status == "ready" and r.status_code == 200: - cache_ext.set(ck, (r.content, content_type), timeout=config.CACHE_DEFAULT_TIMEOUT) - cc = "public, max-age=3600, s-maxage=86400, stale-while-revalidate=86400" - else: - cc = "no-store" - - return Response(r.content, status=r.status_code, mimetype=content_type, headers={ - "X-Widget-Status": status, - "X-Cache": "MISS", - "Cache-Control": cc, - }) - - -def main(): - app.run(host="0.0.0.0", port=config.PORT) - - -if __name__ == "__main__": - main() -``` - -- [ ] **Step 4: Run tests — pass** - -Run: `cd edge && pytest -v` -Expected: all PASS. - -- [ ] **Step 5: Commit** - -```bash -git add edge/src/cache.py edge/src/api.py edge/tests/test_api.py -git commit -m "feat(edge): add cache-first SVG proxy with Flask-Caching + Flask-Compress" -``` - ---- - -### Task 4.3: Edge README - -**Files:** -- Create: `edge/README.md` - -- [ ] **Step 1: Write** - -```markdown -# Edge Service - -Cache-first SVG proxy in front of the generator. Deploy many instances -around the world. No DB of its own; optional Redis for shared cache. - -## Run locally - - cd edge - pip install -r requirements.txt - GENERATOR_URL=http://localhost:5002 python -m src.api - -## Test - - cd edge && pytest - -## Docker - - docker build -t ghstats-edge . - docker run --rm -e GENERATOR_URL=http://host.docker.internal:5002 -p 5003:5003 ghstats-edge - -## Endpoints - -| Method | Path | Description | -|---|---|---| -| GET | /health | cache type + upstream reachability | -| GET | / | composite SVG (cached on X-Widget-Status: ready) | -| GET | //.svg | individual widget SVG | - -## Env - -- `GENERATOR_URL` — origin (required) -- `CACHE_TYPE` — `SimpleCache` (default) or `RedisCache` -- `CACHE_REDIS_URL` — if using RedisCache -- `CACHE_DEFAULT_TIMEOUT` — seconds, default 86400 -- `CACHE_THRESHOLD` — max entries (SimpleCache only), default 10000 -``` - -- [ ] **Step 2: Commit** - -```bash -git add edge/README.md -git commit -m "docs(edge): add README" -``` - ---- - -## Phase 5 — Frontend Migration - -### Task 5.1: Move frontend into generator + rename API paths - -**Files:** -- Move: `frontend/*` → `generator/frontend/*` -- Modify: any React sources that call the API — change hardcoded paths to `/api/*` - -- [ ] **Step 1: Move** - -```bash -git mv frontend generator/frontend -``` - -- [ ] **Step 2: Update React API calls** - -```bash -grep -rn "'/enroll\|'/users\|'/settings\|'/generate\|'/health\|'/refresh" generator/frontend/src || echo "clean" -``` - -Open each result and prepend `/api` to the path. Do the same for any `fetch('http://localhost:5002/...')` — replace with a `VITE_GENERATOR_URL` env var + `/api/`. - -- [ ] **Step 3: Add `generator/frontend/.env.example`** - -``` -VITE_GENERATOR_URL=http://localhost:5002 -VITE_EDGE_URL=http://localhost:5003 -``` - -- [ ] **Step 4: Update `vite.config.ts` dev proxy** - -In `generator/frontend/vite.config.ts`, add a dev proxy so `/api/*` during `npm run dev` hits the local Flask: - -```typescript -// Inside defineConfig({ server: { ... } }) -server: { - proxy: { - '/api': 'http://localhost:5002', - }, -}, -``` - -- [ ] **Step 5: Verify frontend builds** - -```bash -cd generator/frontend && npm install && npm run build -ls -la dist/ -``` - -Expected: `dist/index.html` and `dist/assets/` present. - -- [ ] **Step 6: Commit** - -```bash -git add generator/frontend/ -A -git commit -m "feat(generator): move frontend into generator + namespace API calls under /api/" -``` - ---- - -### Task 5.2: Multi-stage Dockerfile for generator - -**Files:** -- Create: `generator/Dockerfile` - -- [ ] **Step 1: Write** - -```dockerfile -# --- stage 1: build the React SPA --- -FROM node:20-alpine AS frontend-build -WORKDIR /app/frontend -COPY frontend/package.json frontend/package-lock.json ./ -RUN npm ci -COPY frontend/ ./ -# VITE_* env vars can be injected at build time via --build-arg -ARG VITE_GENERATOR_URL="" -ARG VITE_EDGE_URL="" -ENV VITE_GENERATOR_URL=$VITE_GENERATOR_URL -ENV VITE_EDGE_URL=$VITE_EDGE_URL -RUN npm run build - -# --- stage 2: python runtime --- -FROM python:3.11-slim AS runtime -WORKDIR /app -COPY requirements.txt ./ -RUN pip install --no-cache-dir -r requirements.txt - -COPY src ./src -RUN mkdir -p /app/src/static /app/data -COPY --from=frontend-build /app/frontend/dist/ /app/src/static/ - -EXPOSE 5002 -ENV GENERATOR_SETTINGS_DB_PATH=/app/data/settings.db -ENV GENERATOR_WIDGETS_DB_PATH=/app/data/widgets.db - -# Default is the API. Worker/cron containers override CMD. -CMD ["gunicorn", "-b", "0.0.0.0:5002", "-w", "2", "src.api:app"] -``` - -- [ ] **Step 2: Verify build succeeds** - -```bash -cd generator && docker build -t ghstats-generator-test . -``` - -Expected: image builds successfully. If frontend build fails because of missing env vars, set them via `--build-arg`. - -- [ ] **Step 3: Commit** - -```bash -git add generator/Dockerfile -git commit -m "feat(generator): add multi-stage Dockerfile (node build -> python runtime)" -``` - ---- - -### Task 5.3: `generator/frontend/README.md` - -**Files:** -- Create or overwrite: `generator/frontend/README.md` - -- [ ] **Step 1: Write** - -```markdown -# Frontend (Vite + React) - -Part of the generator service. Sources live here; the production build -is served as static files by the generator's Flask app (at `/`) and -produced by the generator's Dockerfile. - -## Dev - - npm install - VITE_GENERATOR_URL=http://localhost:5002 VITE_EDGE_URL=http://localhost:5003 npm run dev - -Vite proxies `/api/*` to the Flask app at :5002. - -## Build - - npm run build - # produces dist/ which is copied into generator/src/static/ by the Docker build -``` - -- [ ] **Step 2: Commit** - -```bash -git add generator/frontend/README.md -git commit -m "docs(frontend): update README for new location + API contract" -``` - ---- - -## Phase 6 — Polish - -### Task 6.1: Top-level README + CLAUDE.md update - -**Files:** -- Modify: `README.md` -- Modify: `CLAUDE.md` - -- [ ] **Step 1: Replace `README.md` contents** - -```markdown -# github-readme-stats - -A monorepo of three independent Python services plus a React frontend, -each self-contained. See each service's README for details. - -## Services - -| Folder | Port | Responsibility | -|---|---|---| -| [`fetcher/`](./fetcher/README.md) | 5001 | Owns GitHub PAT + `fetcher.db`; cron-refreshes data | -| [`generator/`](./generator/README.md) | 5002 | Settings + widgets DBs, build worker, serves React SPA + `/api/*` | -| [`edge/`](./edge/README.md) | 5003 | Cache-first SVG proxy; deploy globally | - -## Design Docs - -- Spec: [`docs/superpowers/specs/2026-04-17-production-pipeline-design.md`](docs/superpowers/specs/2026-04-17-production-pipeline-design.md) -- Plan: [`docs/superpowers/plans/2026-04-17-production-pipeline.md`](docs/superpowers/plans/2026-04-17-production-pipeline.md) - -## Quick start (local dev, no Docker) - - # 1) fetcher - (cd fetcher && pip install -r requirements.txt && - FETCHER_INTERNAL_TOKEN=dev GITHUB_PAT= python -m src.api) & - - # 2) generator API, worker, cron (three processes) - cd generator && pip install -r requirements.txt - FETCHER_URL=http://localhost:5001 FETCHER_INTERNAL_TOKEN=dev python -m src.api & - FETCHER_URL=http://localhost:5001 FETCHER_INTERNAL_TOKEN=dev python -m src.worker & - FETCHER_URL=http://localhost:5001 FETCHER_INTERNAL_TOKEN=dev python -m src.cron & - - # 3) edge - (cd ../edge && pip install -r requirements.txt && - GENERATOR_URL=http://localhost:5002 python -m src.api) & - - # 4) frontend (dev server) - cd ../generator/frontend && npm install && npm run dev - -Test everything: - - (cd fetcher && pytest) && (cd generator && pytest) && (cd edge && pytest) -``` - -- [ ] **Step 2: Update `CLAUDE.md`** - -Replace its Commands + Architecture sections with a brief pointer to the new layout and per-service READMEs. Leave configuration + conventions sections intact but update paths (e.g., `src/config.py` → `generator/src/config.py`). - -- [ ] **Step 3: Commit** - -```bash -git add README.md CLAUDE.md -git commit -m "docs: top-level README + CLAUDE.md reflect three-service layout" -``` - ---- - -### Task 6.2: Remove stale top-level artifacts + final verification - -**Files:** -- Delete: `CONFIGURATION.md`, `TAG_CUSTOMIZATION.md` (if kept, move their content into `generator/README.md`; if not needed, drop) -- Verify: every service's tests pass end-to-end - -- [ ] **Step 1: Decide stale-docs disposition** - -Inspect `CONFIGURATION.md` and `TAG_CUSTOMIZATION.md`. Either: -- Move their useful sections into `generator/README.md` under an "Advanced config" heading, OR -- `git rm` them if their content is covered in the service READMEs. - -- [ ] **Step 2: Full test sweep** - -```bash -(cd fetcher && pytest -q) && (cd generator && pytest -q) && (cd edge && pytest -q) -``` - -Expected: all green. If anything fails, fix before committing. - -- [ ] **Step 3: Docker build sweep** - -```bash -docker build ./fetcher -t ghstats-fetcher-test -docker build ./generator -t ghstats-generator-test -docker build ./edge -t ghstats-edge-test -``` - -All three must succeed independently (no shared build context). - -- [ ] **Step 4: Commit** - -```bash -git add -A -git commit -m "chore: final cleanup — remove stale docs, verify three-service build" -``` - ---- - -## Completion Criteria - -- [ ] All three services build independently via `docker build ./`. -- [ ] All three services pass their own `pytest`. -- [ ] `GET http:///` returns a placeholder on unknown user; a real SVG after the build worker runs. -- [ ] `PATCH /api//settings` on the generator enqueues a rebuild and the edge serves updated SVG after cache expiry (or via future Redis invalidation). -- [ ] `POST /api//refresh` works once, returns 409 the second time. -- [ ] Retired files (`run.py`, old `src/*_api.py`, root `widget_*.svg`, `data/ghstats.db`, `REPORT.md`, `CHANGELOG.md`) are gone from the working tree. -- [ ] Frontend builds and is served by the generator container at `/`. -- [ ] `REDIS_URL` unset → every `cache.Cache()` call is a no-op; setting it later enables real caching with no code changes. - ---- - -## Execution Handoff - -Plan complete and saved to `docs/superpowers/plans/2026-04-17-production-pipeline.md`. Two execution options: - -**1. Subagent-Driven (recommended)** — dispatch a fresh subagent per task, review between tasks, fast iteration. - -**2. Inline Execution** — execute tasks in this session using `executing-plans`, batch execution with checkpoints. - -Which approach? diff --git a/docs/superpowers/specs/2026-04-17-production-pipeline-design.md b/docs/superpowers/specs/2026-04-17-production-pipeline-design.md deleted file mode 100644 index 7a6fe65..0000000 --- a/docs/superpowers/specs/2026-04-17-production-pipeline-design.md +++ /dev/null @@ -1,531 +0,0 @@ -# Production Pipeline Design — github-readme-stats - -**Date:** 2026-04-17 -**Status:** Approved for planning -**Scope:** Restructure the repo into three independent Python services (fetcher, generator, edge), each with its own Docker image, tests, README, and API. The existing React/Vite frontend folds into the generator service (built and served by it, but kept as a decoupled React app with its own sources). Redis is designed for but not stood up in v1; the edge uses in-process Flask-Caching + Flask-Compress as free wins. Hosting/orchestration is explicitly out of scope. - -## Goals - -- A public, unauthenticated URL `GET /` returns an SVG profile card suitable for embedding in a GitHub README. -- If the username is unknown at request time, return a clean "we're building your widget" placeholder immediately; fetch and build in the background. -- GitHub API calls are tightly rate-limited and live behind a single internal service (the fetcher) that holds the only GitHub PAT. -- The serving path is always a precomputed SVG lookup — never a render on the hot path. -- The three services must be genuinely independent so the generator can later be split into a build worker + a serverless edge serving layer without reshuffling the other services. - -## Non-Goals (v1) - -- No GitHub OAuth, no login, no per-user auth on the settings API. An auth stub is included so auth can be added later without touching call sites. -- No hosting, CI, cloud config, or docker-compose. -- No analytics, no metrics dashboard, no admin UI. -- No Postgres / remote DB. SQLite with WAL is sufficient for the v1 workload (one writer per DB, small volumes, mostly reads). All DB access lives behind one `db.py` per service so a swap to Postgres is a localized change. -- **No Redis deployment in v1.** Redis is designed in this spec so the integration points are correct, but v1 ships without Redis standing up. Edge uses in-process Flask-Caching; generator/fetcher skip the cache layer and talk directly over HTTP. Enabling Redis later is a config toggle — see the Redis section. - -## Architecture - -``` - GitHub README embed Browser (user visits app) - │ │ - ▼ ▼ - ┌───────────────┐ ┌─────────────────────────────┐ - │ Edge service │ │ Generator service │ - │ (many nodes) │──── HTTP ─────►│ / + /assets/* → React SPA │ - │ Flask- │ origin fetch │ /api/ → widget │ - │ Caching + │ on miss │ /api/enroll → enroll │ - │ Compress │ │ /api//... → settings │ - │ - / │ │ + build worker container │ - │ - // │ │ + poll cron container │ - └───────────────┘ └──────┬──────────┬───────────┘ - │ │ - settings.db widgets.db - ▲ - │ - HTTP (internal, - X-Internal-Token) - │ - ┌─────────────────▼───────┐ - │ Fetcher service │ - │ - /data/ (auto- │ - │ fetches on miss) │ - │ - /fetch (force) │ - │ - /avatar │ - │ + cron container │ - └──────┬──────────────────┘ - ▼ - fetcher.db - │ - ▼ - GitHub API - - (v2) Redis slots between edge↔generator and generator↔fetcher. - cache.py is a no-op wrapper until REDIS_URL is set. -``` - -The edge has no DB of its own and no origin DB access — it's a cache-first proxy. The generator serves both the React SPA (static files at `/`) and the API (`/api/*`). Deploy as many edge instances around the world as you like. - -## Redis (designed, not deployed in v1) - -Specified here so the code knows the integration shape. v1 behavior: every `REDIS_URL` env var is unset → the cache wrapper short-circuits to a no-op, and every caller falls through to the underlying service. No code changes required to enable later — set `REDIS_URL` and it starts caching. - -One Redis instance, accessible from fetcher, generator, and edge. Two key namespaces: - -| Key | Value | TTL | Written by | Read by | -|---|---|---|---|---| -| `fetcher:data:` | JSON payload from fetcher | 1h | fetcher (on miss-fill), generator cron on invalidation | generator API + worker | -| `fetcher:hash:` | Current `payload_hash` | 1h | fetcher | generator cron (cheap change detection) | -| `widget:composite:` | SVG string | 24h | edge (on miss-fill), generator (on rebuild invalidation) | edge | -| `widget::` | SVG string per widget | 24h | same as composite | edge | -| `enroll:day:` | Counter | 48h | generator API | generator API (rate limit check) | - -Rules: - -- **Redis is a cache, never a source of truth.** Every key has a TTL. Losing Redis is a latency event, not a correctness event. -- **Invalidation on write, not just TTL.** When the build worker writes new widget rows, it immediately `DEL`s the matching `widget:*:` keys so the next edge read re-populates. Same for fetcher on re-fetch. -- **Single Redis instance in v1.** If edge latency to a single Redis becomes a problem, swap to an edge-native KV (Cloudflare KV, Upstash) — only `edge/src/api.py` changes. - -## Services - -### 1. Fetcher - -**Purpose:** Own every GitHub API interaction. Hold the PAT. Expose cached payloads to the generator. - -**Endpoints** (all internal; not exposed to the public internet): - -- `GET /data/` — return stored payload JSON. If not stored, fetch from GitHub, store, return. This is the first-enrollment path. 502 on GitHub failure; 404 if GitHub says the user doesn't exist (fetcher stores a `{error: "not_found"}` marker so repeat calls are cheap). -- `POST /fetch {username}` — force re-fetch, ignoring any existing cache. Used by the fetcher's own cron and by manual admin action. Returns `{stored, payload_hash, changed}`. -- `GET /avatar/` — return avatar bytes. GitHub's `https://github.com/.png` is public and requires no PAT, so this is cheap. Used by the generator's placeholder renderer. -- `GET /health` - -**Auth:** every request requires `X-Internal-Token: ` header. The generator holds the matching secret. The fetcher never listens on a public interface. - -**Storage:** `fetcher.db` (SQLite), single table: - -```sql -CREATE TABLE users ( - username TEXT PRIMARY KEY, - data_json TEXT NOT NULL, -- full GitHub payload OR {"error":"not_found"} - payload_hash TEXT NOT NULL, -- sha256 of data_json - fetched_at TEXT NOT NULL, -- UTC ISO 8601 - last_requested_at TEXT NOT NULL -- updated on every GET /data; drives GC -); -``` - -**Cron (`fetcher/src/cron.py`):** runs every hour. Each tick: - -1. Select users with `fetched_at < now - 24h` AND `last_requested_at > now - 7d`. Stagger calls across the hour so we don't burst GitHub. -2. For each, call `POST /fetch` internally (or the equivalent function). Update `payload_hash`. -3. Delete rows where `last_requested_at < now - 7d` (trial GC — unused enrollments drop out automatically). `last_requested_at` is updated on every `GET /data/`, so actively-embedded users are never dropped. - -**Env vars:** - -- `FETCHER_PORT` (default `5001`) -- `FETCHER_DB_PATH` (default `./data/fetcher.db` inside container) -- `GITHUB_PAT` (required) -- `FETCHER_INTERNAL_TOKEN` (required; the shared secret) -- `FETCHER_REFRESH_INTERVAL_HOURS` (default `24`) -- `FETCHER_TRIAL_GC_DAYS` (default `7`) -- `REDIS_URL` (optional; if set, writes to cache on fetch) - -**DB pragmas:** on connection open, run `PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL; PRAGMA foreign_keys=ON; PRAGMA busy_timeout=5000;`. - -### 2. Generator - -**Purpose:** Serve precomputed SVGs to the public internet. Hold user settings. Build widgets in the background. Never call GitHub directly; only talk to the fetcher over HTTP. - -All API endpoints are under the `/api/` prefix. Root `/` and `/assets/*` serve the React SPA built into `src/static/`. - -**Public API endpoints:** - -- `GET /api/` — return composite SVG from `widgets.db`. Placeholder on miss. Sets `X-Widget-Status` header. -- `GET /api//.svg` — individual widget. Same behavior. -- `POST /api/enroll {username}` — explicit enrollment (SPA uses this). Also auto-invoked when `GET /api/` arrives. Enforces daily rate limit. Returns `{enrolled, job_id}` or `{error: "rate_limited"}`. -- `GET /api/health` - -**Auth-gated API endpoints** (auth stub in v1 — passes everything through; real auth later): - -- `GET /api//settings` — current settings. -- `PATCH /api//settings` — update settings; enqueues a rebuild. Unlimited. Does NOT re-fetch GitHub. -- `POST /api//refresh` — one-shot manual re-fetch + rebuild. Calls fetcher's `POST /fetch`, then enqueues a rebuild. Gated by `manual_refresh_used` flag (allowed once per user, ever). Returns 409 `already_used` after the first call. - -**Storage:** - -**`settings.db`** (SQLite): - -```sql -CREATE TABLE users ( - username TEXT PRIMARY KEY, - settings_json TEXT NOT NULL, -- theme, enabled widgets, order, custom_tags, achievements, widget_settings - settings_hash TEXT NOT NULL, -- sha256 of canonicalized settings_json - enrolled_at TEXT NOT NULL, - last_fetcher_payload_hash TEXT, -- last hash we rebuilt from; cron compares - manual_refresh_used INTEGER DEFAULT 0, -- enforces one-shot user-triggered re-fetch - last_requested_at TEXT NOT NULL -- updated on every GET /; drives trial GC -); - -CREATE TABLE enrollments_daily ( - day TEXT PRIMARY KEY, -- YYYY-MM-DD, UTC - count INTEGER NOT NULL -); - -CREATE TABLE jobs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - kind TEXT NOT NULL, -- "build" - username TEXT NOT NULL, - status TEXT NOT NULL, -- "pending" | "running" | "done" | "failed" - attempts INTEGER DEFAULT 0, - last_error TEXT, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL -); -CREATE INDEX idx_jobs_pending ON jobs(status, created_at); -``` - -**`widgets.db`** (SQLite in v1; designed for swap to remote backend later): - -```sql -CREATE TABLE widgets ( - username TEXT NOT NULL, - settings_hash TEXT NOT NULL, - widget_name TEXT NOT NULL, -- "composite" | "grade" | "focus" | ... - svg TEXT NOT NULL, - built_at TEXT NOT NULL, - PRIMARY KEY (username, settings_hash, widget_name) -); - -CREATE TABLE current_widget ( - username TEXT PRIMARY KEY, - settings_hash TEXT NOT NULL, -- pointer to the "live" set - updated_at TEXT NOT NULL -); -CREATE INDEX idx_widgets_username ON widgets(username); -``` - -The `widgets_repo.py` module wraps all access. Future swap to Postgres / Turso / D1 / S3 changes only that one file. - -**Build worker (`generator/src/build_worker.py`):** - -- In-process thread polling `jobs` table every 500ms. -- For each pending job: mark `running`, load settings, `GET {fetcher}/data/` (fetcher auto-fetches on miss), compute `settings_hash`, render all enabled widgets + composite, upsert into `widgets.db`, flip `current_widget` pointer atomically, LRU-trim to 10 rows per user, mark `done`. -- Failure: increment `attempts`, exponential backoff (1min, 5min, 15min); after 3 attempts mark `failed` and leave the existing widget (if any) in place. Placeholder continues to serve for un-built users. -- Extractable to a standalone process later (same DB, same code, separate entrypoint). - -**Generator cron (`generator/src/cron.py`):** runs every 15 minutes. - -1. For each enrolled user, `GET {fetcher}/data/` (response includes `payload_hash`). -2. If `payload_hash != last_fetcher_payload_hash`, enqueue a build job and update `last_fetcher_payload_hash`. -3. LRU-trim widgets per user. - -**Enrollment rate limit:** global cap of 50 new enrollments per UTC day (`ENROLLMENT_DAILY_CAP`). Over cap → `POST /enroll` returns `rate_limited`; `GET /` returns a "try again tomorrow" placeholder. Enrollment counter increments only on *new* users, not on re-requests. - -**Placeholder renderer (`generator/src/placeholder.py`):** produces SVGs matching the card wrapper and theme of real widgets. Three variants: - -- **building**: "Building @username's widget... This usually takes under a minute." Shows avatar if fetched (generator calls `GET {fetcher}/avatar/`). Serves when user is enrolled but widgets not yet built. -- **rate_limited**: "Too many new users today — try again tomorrow." Shown when enrollment cap is hit. -- **not_found**: "GitHub user @username doesn't exist." Shown when fetcher stored a `not_found` marker. - -Placeholders are rendered on the fly (no DB lookup needed) and served with `Cache-Control: no-store`. - -**Env vars:** - -- `GENERATOR_PORT` (default `5002`) -- `GENERATOR_SETTINGS_DB_PATH` (default `./data/settings.db`) -- `GENERATOR_WIDGETS_DB_PATH` (default `./data/widgets.db`) -- `FETCHER_URL` (required, e.g. `http://mini-pc:5001`) -- `FETCHER_INTERNAL_TOKEN` (required, matches fetcher's) -- `REDIS_URL` (optional; if set, read-through to fetcher cache and invalidation on rebuild) -- `ENROLLMENT_DAILY_CAP` (default `50`) -- `WIDGET_LRU_PER_USER` (default `10`) -- `GENERATOR_POLL_INTERVAL_MINUTES` (default `15`) - -**DB pragmas:** same as fetcher (WAL + NORMAL sync + 5s busy_timeout). - -### 3. Edge - -**Purpose:** Serve widget SVGs cache-first, with the generator as origin. Deploy many instances around the world (Cloud Run, Fly, Lambda container, etc.). No DB of its own. - -**Endpoints:** - -- `GET /` — check cache; hit → return; miss → fetch generator `GET /api/`, cache, return. -- `GET //.svg` — same pattern. -- `GET /health` — reports cache type + origin reachability. - -**Caching (v1):** [Flask-Caching](https://flask-caching.readthedocs.io/) with `CACHE_TYPE=SimpleCache` — in-process LRU, per-instance, no external dependency. Wrapped by a thin `cache.py` so `CACHE_TYPE=RedisCache` + `CACHE_REDIS_URL` swaps in Redis later with no call-site changes. TTL: 24h. Max entries per instance: 10000. - -**Compression:** [Flask-Compress](https://github.com/colour-science/flask-compress) on, default settings. SVG is text — gzip typically 70-80%% savings. - -**HTTP cache headers on hits:** -- `Cache-Control: public, max-age=3600, s-maxage=86400, stale-while-revalidate=86400` - -**Placeholder handling:** generator sets `X-Widget-Status: ready | building | rate_limited | not_found`. Edge only caches `ready` responses. Others are passed through with `Cache-Control: no-store` so a CDN in front doesn't memorize them. - -**Implementation:** single `edge/src/api.py` Flask file (~100 lines) + `edge/src/cache.py` (~30 lines). - -**Env vars:** -- `EDGE_PORT` (default `5003`) -- `GENERATOR_URL` (required — origin) -- `CACHE_TYPE` (default `SimpleCache`; later `RedisCache`) -- `CACHE_REDIS_URL` (optional) -- `CACHE_DEFAULT_TIMEOUT` (default `86400`) - -### Frontend (served by the generator) - -**Not a separate service.** The existing React/Vite frontend moves into `generator/frontend/` (source code) and is built by the generator's Dockerfile. The Flask app in `generator/src/api.py` serves the built static bundle from `/` (and `/assets/*`). The architecture stays decoupled — React sources live in their own folder, are built by their own toolchain, and only the static output is served by Flask. - -**URL split on the generator:** - -| Path | Served | Notes | -|---|---|---| -| `/` | `generator/frontend/dist/index.html` | React SPA entry | -| `/assets/*` | `generator/frontend/dist/assets/*` | JS/CSS bundles | -| `/api/` | Flask route | widget origin (called by edge) | -| `/api//.svg` | Flask route | individual widget origin | -| `/api/enroll` | Flask route | enrollment | -| `/api//settings` | Flask route | settings CRUD | -| `/api//refresh` | Flask route | one-shot re-fetch | -| `/api/health` | Flask route | health | - -**Why `/api/` prefix:** keeps the frontend `/` clean for the SPA and keeps widget origin paths from colliding with React routes. The edge rewrites `GET /` → `GET /api/` when calling origin. - -**Dockerfile (one, multi-stage):** - -1. **Stage `frontend-build`** — `node:20-alpine`, `npm ci && npm run build` inside `/app/frontend`, produces `/app/frontend/dist`. -2. **Stage `runtime`** — `python:3.11-slim`, install `requirements.txt`, copy `src/` and the built `frontend/dist` from stage 1 into `src/static/`. Flask serves from there. - -**Flow (v1, no auth):** - -1. User opens `https://generator.example.com/` → React SPA loads. -2. User enters their GitHub username → calls `POST /api/enroll`. -3. UI polls `GET {EDGE_URL}/` preview (placeholder until build completes). -4. Settings form edits go to `PATCH /api//settings`. -5. "Refresh from GitHub" button calls `POST /api//refresh`. Disabled after one use (server enforces 409). -6. UI displays the embeddable edge URL for pasting into their README. - -**Vite env vars:** `VITE_GENERATOR_URL`, `VITE_EDGE_URL` (baked at build time by the Dockerfile). - -## File Layout - -Simplified — each Python service is ~4-6 files plus its rendering assets. - -``` -github-readme-stats/ -├── fetcher/ -│ ├── src/ -│ │ ├── __init__.py -│ │ ├── api.py # Flask app + all routes + auth header check -│ │ ├── github.py # GitHub GraphQL/REST client (from src/data/fetcher.py) -│ │ ├── db.py # SQLite repo, WAL setup, schema migrations -│ │ ├── cron.py # scheduled refresh + GC; run as separate container -│ │ └── config.py -│ ├── tests/ -│ │ ├── test_api.py -│ │ ├── test_github.py -│ │ ├── test_db.py -│ │ └── test_cron.py -│ ├── data/ # gitignored -│ ├── Dockerfile -│ ├── requirements.txt -│ ├── pytest.ini -│ └── README.md -│ -├── generator/ -│ ├── frontend/ # React+Vite sources (moved from top-level frontend/) -│ │ ├── src/ -│ │ ├── package.json -│ │ ├── vite.config.ts -│ │ └── tsconfig.json -│ ├── src/ -│ │ ├── __init__.py -│ │ ├── api.py # Flask app + all /api/* routes + SPA static serve + fetcher HTTP client inline + auth stub inline -│ │ ├── worker.py # build worker loop; run as separate container -│ │ ├── cron.py # polls fetcher, enqueues rebuilds; run as separate container -│ │ ├── db.py # settings_repo + widgets_repo together -│ │ ├── cache.py # no-op in v1 (REDIS_URL unset); real wrapper otherwise -│ │ ├── placeholder.py # three SVG variants -│ │ ├── processor.py # widget rendering pipeline (from src/data/processor.py) -│ │ ├── config.py -│ │ ├── static/ # populated at Docker build time from frontend/dist -│ │ ├── widgets/ # (from src/widgets/) -│ │ ├── themes/ # (from src/themes/) -│ │ ├── models/ # (from src/models/) -│ │ └── utils/ # (from src/utils/) -│ ├── tests/ -│ │ ├── test_api.py -│ │ ├── test_worker.py -│ │ ├── test_cron.py -│ │ ├── test_db.py -│ │ ├── test_placeholder.py -│ │ ├── test_processor.py -│ │ └── integration/ -│ │ └── test_end_to_end.py -│ ├── data/ # gitignored -│ ├── Dockerfile # multi-stage: node build → python runtime (api+worker+cron share image) -│ ├── requirements.txt -│ ├── pytest.ini -│ └── README.md -│ -├── edge/ -│ ├── src/ -│ │ ├── __init__.py -│ │ ├── api.py # Flask app + Flask-Caching + Flask-Compress + origin fetch -│ │ ├── cache.py # Flask-Caching config (SimpleCache now, RedisCache later) -│ │ └── config.py -│ ├── tests/ -│ │ └── test_api.py -│ ├── Dockerfile -│ ├── requirements.txt -│ ├── pytest.ini -│ └── README.md -│ -├── docs/superpowers/specs/2026-04-17-production-pipeline-design.md -├── .gitignore # **/data/*.db, built artifacts -└── README.md # points to each service's README -``` - -**Independence rules:** - -- No Python imports cross service folders. Communication only via HTTP + Redis. -- No shared package. Duplicate any trivial util (e.g., `payload_hash`) per service. -- Each service has its own `requirements.txt`, `pytest.ini`, `Dockerfile`, `README.md`. -- `docker build ./fetcher` etc. succeeds with no repo-root build context. -- Generator ships one Docker image; three containers run from it with different `CMD`s: `python -m src.api`, `python -m src.worker`, `python -m src.cron`. - -## Code to Delete - -The current `src/` layout is retired wholesale. Specifically: - -- `run.py` — CLI entry point replaced by frontend + API. -- `src/generate.py` — replaced by the generator service. -- `src/fetcher_api.py`, `src/generator_api.py` — replaced by `fetcher/src/api.py` and `generator/src/api.py`. -- `src/db/` — schema moves to fetcher's `repository.py`; dummy user pattern replaced by placeholders. -- `src/data/ARCHITECTURE.md` — superseded by this spec. -- `widget_*.svg` at repo root — build artifacts, no longer produced. -- `data/ghstats.db` at repo root — replaced by per-service `data/*.db`. -- `REPORT.md`, `CHANGELOG.md` — stale; drop unless user wants them kept. -- `src/README.md` — superseded by per-service READMEs. - -`CLAUDE.md` is updated to reflect the new layout (sections on commands, architecture, configuration). `CONFIGURATION.md` and `TAG_CUSTOMIZATION.md` move into the generator's `README.md` (or its own `docs/` under `generator/`). - -Existing widget/theme/model code under `src/widgets/`, `src/themes/`, `src/models/`, `src/utils/`, and most of `src/data/processor.py` move into `generator/src/` and are kept. `src/data/fetcher.py` moves into `fetcher/src/github.py`. The top-level `frontend/` folder moves wholesale into `generator/frontend/`. - -## Data Flow - -### First-time `GET /shaymanor` (from a README embed, hitting the edge) - -1. Edge: Flask-Caching lookup for `/shaymanor` → miss. -2. Edge: HTTP `GET {generator}/api/shaymanor` → origin. -3. Generator: `widgets_repo.get_current(shaymanor)` → miss. -4. Generator: `settings_repo.get(shaymanor)` → miss → user unenrolled. -5. Enrollment path: - a. Check today's counter in `enrollments_daily`. If `>= 50`, return `rate_limited` placeholder with `X-Widget-Status: rate_limited` and `Cache-Control: no-store`. - b. Insert default settings row. Increment counter. - c. Insert `build` job. -6. Generator returns `building` placeholder with `X-Widget-Status: building`, `Cache-Control: no-store`. -7. Edge sees non-`ready` status header, returns SVG to the reader without caching. - -### Build worker processing a job - -1. Mark job `running`. -2. `cache.get("fetcher:data:")` via the wrapper (v1 = no-op, always miss). Miss → `GET {fetcher}/data/` with internal token (fetcher auto-fetches from GitHub on miss). Hit → use cached payload. -3. If payload is the `not_found` marker: upsert a `not_found` placeholder into widgets; flip `current_widget`; mark job `done`. No retries on this user. -4. Otherwise: load settings, compute `settings_hash`, render all enabled widgets + composite via the existing `processor.py` pipeline. -5. Transaction: insert widget rows, flip `current_widget`, update `settings.last_fetcher_payload_hash`. -6. `cache.delete("widget:composite:")` and per-widget keys (v1 no-op; real invalidation once Redis is enabled). -7. LRU-trim widgets for this user to 10 hashes. -8. Mark job `done`. - -The edge clears its own Flask-Caching entry via TTL expiry (24h) in v1. Users who want instant invalidation on rebuild can call the edge's `POST /admin/purge/` (added in v2 when Redis comes online — not in v1). - -### Cron-driven refresh - -1. Fetcher cron (hourly) re-fetches due users; updates `payload_hash` on change. -2. Generator cron (15 min) calls fetcher `GET /data/` per enrolled user (response includes `payload_hash`). Compares with `last_fetcher_payload_hash`. On mismatch, enqueues `build` job. (When Redis comes online in v2, this reads `fetcher:hash:` from Redis — HTTP becomes the fallback.) -3. Trial users not requested in 7 days are GC'd by fetcher cron. - -### Settings change from frontend - -1. `PATCH /shaymanor/settings` (auth stub passes through). -2. Generator computes new `settings_hash`, updates `settings_json`. -3. Enqueue `build` job. -4. Old `(username, old_hash)` rows remain in `widgets` until LRU-trimmed. `current_widget` flips once the build completes — no user-visible flicker. - -### Manual refresh (one-shot, user-triggered) - -1. `POST /shaymanor/refresh`. -2. If `manual_refresh_used = 1`, return 409 `already_used`. -3. Otherwise: set `manual_refresh_used = 1`, call fetcher `POST /fetch {username: shaymanor}` (this burns one GitHub-quota hit), enqueue `build` job. The rebuild will pick up the freshly-fetched data on the next poll. - -After this one-shot is spent, the user's data only changes via fetcher cron (every 24h). Settings edits continue to trigger unlimited rebuilds but never re-fetch. - -## Error Handling - -| Failure mode | Behavior | -|---|---| -| Fetcher unreachable from generator | Build job retries with backoff; placeholder continues to serve; `/health` reports degraded. | -| GitHub returns 5xx | Fetcher returns 502 to generator; build job retries. | -| GitHub returns 404 | Fetcher stores `{error: "not_found"}`; generator builds a `not_found` placeholder once, stops retrying. | -| GitHub rate-limit | Fetcher returns 429 to generator; build job retries with longer backoff. | -| Enrollment cap exceeded | `POST /enroll` → `rate_limited`. `GET /` → `rate_limited` placeholder. Next day resets. | -| Settings DB or widgets DB corrupt | Generator serves 503. Intentional — this is a server bug, not a missing-user condition. | -| Build worker dies mid-job | On next start, any `running` job older than 10 minutes is reclaimed to `pending`. | -| Manual refresh reused | 409 `already_used`. | - -## Testing - -Each service has its own `tests/` and runs with `cd && pytest`. - -**Fetcher:** -- `test_repository.py`: upsert, payload_hash stability, last_requested_at update on read. -- `test_github_client.py`: mocked HTTP with the `responses` library; covers GraphQL + REST paths, 404, 429, 502. -- `test_api.py`: auth-header enforcement, auto-fetch on miss, 404 handling. -- `test_cron.py`: staggered refresh, GC of stale trial users. - -**Generator:** -- `test_db.py`: settings + widgets CRUD, settings_hash canonicalization, LRU trimming, `current_widget` pointer atomicity, WAL pragma applied. -- `test_placeholder.py`: each of the three placeholder variants renders valid SVG and matches theme. -- `test_worker.py`: picks jobs, retries on fetcher failures, handles `not_found` marker, invalidates Redis on success. -- `test_api.py`: full surface — auth stub, rate limit, enrollment auto-trigger on `GET /api/`, serves precomputed on hit, placeholder on miss, `X-Widget-Status` header correct per case, SPA served at `/`. -- `test_cron.py`: polls fetcher via HTTP, enqueues only on hash change. -- `test_processor.py`: carry-over tests for widget rendering (from existing code). - -**Edge (`edge/tests/`):** -- `test_api.py`: SimpleCache hit returns cached SVG; miss fetches origin, caches, returns; `X-Widget-Status: ready` is cached, others pass through uncached; gzip content-encoding applied to responses > threshold. - -**Integration (`generator/tests/integration/`):** spin up generator API + worker + cron + edge + a fake fetcher (responses/respx mock) in-process, drive `GET /` end to end via the edge. Assert placeholder → build → real SVG transition and that the edge serves from cache on second request. - -**Frontend:** Vitest smoke tests: form submit, preview fetch, settings save. No E2E required in v1. - -## Security - -- Fetcher's `X-Internal-Token` is required on every endpoint except `/health`. Constant-time comparison. 401 on mismatch. -- No endpoint on the fetcher is exposed to the public internet. Deployment guidance in the fetcher README: bind to `127.0.0.1` or a private interface. -- Generator does not echo stored data anywhere except via the widget SVG — no `/data/` mirror. Prevents the generator from leaking raw GitHub payloads. -- Frontend talks to generator over HTTPS in production (user's responsibility). -- No secrets logged. No `GITHUB_PAT` in generator env at all. - -## Observability - -- Each service logs to stdout in a line-oriented format: `ts level service event key=val ...`. -- `/health` on each service returns `{status, service, db_ok, upstream_ok?}`. -- No metrics backend in v1. - -## Migration Plan (executed by the implementation phase) - -1. Create new folder scaffolding and move code wholesale. Do not keep the old `src/` in parallel — cut over in one branch. -2. Split `src/db/repository.py` into fetcher's `repository.py`. -3. Define new settings_repo and widgets_repo in `generator/src/`. -4. Write placeholder renderer using the existing card wrapper util (moved into `generator/src/utils/`). -5. Wire build worker + cron. -6. Update `.gitignore`; delete retired files and build artifacts. -7. Update the top-level `README.md` to point to the three service READMEs. -8. Update `CLAUDE.md` to reflect the new layout. - -## Open Questions - -None at time of writing. All design choices confirmed in brainstorming. - -## Future Work (out of scope for this spec) - -- GitHub OAuth on settings endpoints. -- Postgres migration (swap `db.py` per service; all call sites unchanged). -- Edge-native KV store (Cloudflare KV / Upstash) if single-Redis latency is a problem from far regions. -- Admin endpoints for cron pause / manual re-fetch / user purge. -- Metrics + alerting.