Skip to content

Latest commit

 

History

History
324 lines (255 loc) · 13.5 KB

runbook.md

File metadata and controls

324 lines (255 loc) · 13.5 KB

Runbook

Operational procedures for wizardsofodd.com. For what and why, see architecture.md and adr/.

Deploy

cd <repo-root>
npm run deploy                                # predeploy gates, then wrangler deploy

npm run deploy is the only supported way to ship. Its predeploy step runs npm run check (the drift gate npm run demo:drift, the portrait check, local D1 schema drift, tsc, the test suite), then npm run verify:db:remote (production D1 schema drift) and npm run build:web, and stops at the first failure. Never call wrangler deploy directly: it skips every one of those gates.

Deploy credentials. npm run deploy, npm run db:migrate:remote and npm run verify:db:remote run through scripts/cf-deploy.sh, which uses the site's narrow deploy-only token, not wrangler login OAuth or an account-wide key. It pulls CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD and CLOUDFLARE_ACCOUNT_ID from Infisical (deploy project, dev env) via keys-axi run, exposes the token to wrangler as CLOUDFLARE_API_TOKEN, drops any inherited CLOUDFLARE_EMAIL / CLOUDFLARE_API_KEY / CLOUDFLARE_API_TOKEN, and runs the repo's own pinned wrangler from node_modules/.bin. The token covers Workers Scripts, D1 and Workers KV writes plus Account Settings read on the account, and Workers Routes write plus Zone read on wizardsofodd.com only, so a command outside that scope (such as wrangler tail) still needs your own login.

Production deploys are direct from main. Smoke-check:

curl -s https://wizardsofodd.com/api/health   # {"ok":true}

Live council smoke test

curl -sN -X POST https://wizardsofodd.com/api/council \
  -H 'content-type: application/json' \
  -d '{"question":"What was the South Sea Bubble?"}' \
  | grep -E '^(event|data):' | head -30

Look for: 5 event: wizard frames, 1 event: verdict with the 4-point structure ending in High confidence: ... / Open question: ..., then event: done. Lorekeeper's reply should contain real URLs (Wikipedia / StackOverflow / etc.) when SearXNG is up; otherwise his prompt lampshades the unreachable library.

Local dev

echo 'ANTHROPIC_API_KEY = "<dev key from <your-secrets-file>>"' > .dev.vars
npm run dev                                   # wrangler dev, default port 8787

Open the printed URL. .dev.vars is gitignored and only used by wrangler dev.

Regenerate visual assets

  1. Start ComfyUI (if not already running):

    cd <your ComfyUI checkout>
    nohup ./venv/bin/python main.py --listen 127.0.0.1 --port 8188 --lowvram \
      > /tmp/comfy.log 2>&1 &
    disown

    Use --lowvram to avoid the BrokenPipeError cold-start issue.

  2. Wait until ready:

    until curl -s http://127.0.0.1:8188/system_stats >/dev/null; do sleep 1; done; echo ready
  3. Iterate on a prompt (scripts/prompts/<slug>.txt) and regenerate:

    npm run gen:assets -- --wizard grumbel            # one wizard
    npm run gen:assets -- --background                # the background
    npm run gen:assets                                # all 7

    Outputs land at public/background.webp and public/portraits/<slug>.webp.

  4. Stop ComfyUI when done:

    pkill -f "main.py --listen"
  5. If the verdict-seal regenerates, also redo the favicons:

    npm run gen:favicons                              # writes 16/32/180/192/512.png
  6. Commit + deploy — same as the deploy procedure above.

See ADR-0003 for why local ComfyUI instead of RunPod/CF AI for one-off content.

Adding a wizard

Adding (or replacing) a wizard is a pure metadata change — no TypeScript edits:

  1. Wizard identity — author data/wizards/<id>.yaml (id, name, title, technique, bickersWith, usesSearch, techniqueBlurb, defaultEnabled, optional order).
  2. Prompt declarations — add two template.prompt nodes (<id>System, <id>User) to metaobjects/prompts/meta-prompts-wizards.yaml, following the existing pattern (payloadRef + textRef + format).
  3. Personality — author data/templates/wizards/<id>-system.mustache (voice, technique guidance) and <id>-user.mustache (how the wizard reads the question, prior takes, sources).
  4. Portrait — drop public/portraits/<id>.webp (lowercase id).
  5. Regenerate — npm run gen:db reruns codegen (the wizard-registry generator fails naming any missing declaration), then npm run check runs the drift gate, validates, and runs tests.

To remove a wizard, delete its data/template/portrait files and its two prompt nodes, then npm run gen:db && npm run check. test/wizards.test.ts asserts the roster count — update the expected number to match.

Rotate keys

All Worker secrets are set via wrangler secret put <NAME> (interactive paste — never on the command line). The host-side copies live in <your-secrets-file> (chmod 600).

Secret Source What it does
ANTHROPIC_API_KEY Anthropic console → Settings → API Keys LLM calls for council
CF_ACCESS_CLIENT_ID CF Zero Trust → Access → Service Auth → Service Tokens → wizardsofodd-worker Search-endpoint auth
CF_ACCESS_CLIENT_SECRET Same (paired with above; shown ONCE on creation)

Rotation procedure:

# 1. Create a fresh value in the issuing console (Anthropic / CF dashboard).
# 2. Push to Worker:
cd <repo-root>
npx wrangler secret put ANTHROPIC_API_KEY     # paste new value
# 3. Update the host-side mirror:
$EDITOR <your-secrets-file>
chmod 600 <your-secrets-file>
# 4. Revoke the old value in the issuing console.
# 5. Smoke-test the live council (above) to confirm nothing broke.

For the CF Access service token specifically, the dashboard path is: Zero Trust → Access → Service Auth → Service Tokens → find wizardsofodd-worker → Refresh (creates a new secret; the policy on the "Home Portal" Access app keeps pointing at the same token id, no policy change needed).

Inspect / manage the CF Access plumbing

The Worker reaches the protected SearXNG via the wizardsofodd-worker service token, which is attached to the "Home Portal" Access app (id <access-app-id>, covers <your-host> + searxng.example.com via self_hosted_domains).

# Verify the policy is still in place (needs a CF API token with Access:Apps:Edit)
CF=<your-api-token>
ACCT=<account-id>
APP=<access-app-id>
curl -s "https://api.cloudflare.com/client/v4/accounts/$ACCT/access/apps/$APP" \
  -H "Authorization: Bearer $CF" \
  | python3 -c "import sys,json;a=json.load(sys.stdin)['result'];print([p['name'] for p in (a.get('policies') or [])])"
# Expect: ["Allow wizardsofodd-worker service token"]

Logs

npx wrangler tail wizardsofodd                # live tail

console.error calls from the stream's catastrophic-error branch ([council] stream error) show up here.

Migrate

New D1 migrations in migrations/ are generated from the metadata by meta migrate --dialect d1. After changing an entity under metaobjects/, run npm run gen:db, then write the migration and apply it locally:

npx meta migrate --dialect d1 --d1 DB --out-dir migrations --slug <what-changed>
npm run verify:db                             # applies it to local D1, then checks schema drift
npm run db:migrate:remote                     # apply to production D1 (before npm run deploy)

meta migrate also writes a down file under migrations/.down/; move it to migrations/rollbacks/<name>.down.sql (wrangler applies every .sql in migrations/, so a down file must not sit there). Read the generated SQL before committing it: a change SQLite cannot ALTER (a new CHECK, a narrower column) is a table rebuild, and a CHECK fails on any existing row that violates it, so query production for violating rows first.

Shipped so far: 0001_init.sql to 0005_drop_dead_followups.sql (hand-written, from before the project adopted meta migrate), 0006_reconcile-schema.sql (rebuild that brought the live schema in line with the metadata's CHECKs) and 0007_link-verdict-and-council-id.sql (rebuild for the verdict confidence CHECK, council_turns.council_id at the slug width, and the 600-char question cap).

Precheck before migration 0007. Run these against production D1 before npm run db:migrate:remote:

SELECT count(*) FROM councils WHERE verdict_confidence < 0 OR verdict_confidence > 1;
SELECT count(*) FROM councils WHERE length(question) > 600;
SELECT count(*) FROM council_turns WHERE length(council_id) <> 8;

If the first returns a non-zero count, the rebuild's INSERT fails on the new confidence CHECK: stop, then correct or clamp those rows before migrating. The other two do not fail the migration (SQLite does not enforce VARCHAR widths) but mark rows that break the declared model: review them before migrating.

db:migrate:remote is NOT automatic — for a change that adds a migration, run it against production D1 before npm run deploy. The predeploy step runs npm run verify:db:remote, which fails on the schema drift until the migration is applied, so the deploy stops there instead of shipping a Worker that writes to a schema production does not have yet.

Test schema: the Workers-pool test D1 replays migrations/*.sql directly (test/setup.ts loads them via a Vite ?raw glob, inlined at build time since the workerd pool has no filesystem). New migrations are picked up automatically — no manual schema sync.

Common gotchas

  • Wrangler is on 3.x and out-of-date; works fine, but emits a warning on every command. Don't blindly run wrangler@4 upgrade — verify against the Vitest pool's pinned compat first.
  • compatibility_date is 2024-12-30 intentionally (matches the Workers runtime version the test pool bundles). Don't bump it past whatever the installed @cloudflare/vitest-pool-workers supports or tests will warn-spam.
  • wrangler.jsonc's run_worker_first: ["/api/*"] is what lets /api/* paths reach the Worker through the SPA fallback. If /api/health ever starts returning the homepage HTML, that's the first thing to check.
  • ComfyUI cold start sometimes hits BrokenPipeError on the first generation. Always start with --lowvram; if it errors anyway, restart it.

Turnstile setup (one-time, per environment)

  1. Create the Turnstile site in the Cloudflare dashboard:

    • Zero Trust → Turnstile → Add site
    • Domain: wizardsofodd.com (add www.wizardsofodd.com and localhost too)
    • Widget mode: Managed (lets CF pick invisible vs interactive per request)
    • Copy the site key (public) and secret key (private)
  2. Swap the public site key in public/index.html:

    sed -i 's|1x00000000000000000000AA|<your-real-sitekey>|' public/index.html

    The placeholder 1x00000000000000000000AA is CF's "always passes" test key — fine for dev, must be replaced for prod. Commit the change.

  3. Set the secret key as a Worker secret (NOT a var):

    echo -n '<your-real-secret>' | npx wrangler secret put TURNSTILE_SECRET

    This overrides the TEST_BYPASS_TURNSTILE value in wrangler.jsonc for the deployed Worker. Local wrangler dev continues using the bypass marker (no real verification calls during dev).

Anthropic console budget cap (one-time, per API key)

The ultimate safety net — independent of any Worker code path.

  1. Anthropic Console → Settings → Billing → Usage limits
  2. Pick the wizards-of-odd API key
  3. Set Monthly spend limit: $20
  4. When hit, Anthropic returns 429s; our existing error path renders "the tower went dark unexpectedly" gracefully.

Pre-deploy checklist

Before every npm run deploy:

# 1. Gates green (predeploy runs this again, but run it before the checks below)
npm run check

# 2. Turnstile secret is set as a SECRET (overriding the bypass var)
npx wrangler secret list | grep -q '^TURNSTILE_SECRET' \
  || (echo 'FATAL: TURNSTILE_SECRET not set as secret'; exit 1)

# 3. public/index.html has the real sitekey (not the test placeholder)
grep -q '1x00000000000000000000AA' public/index.html \
  && (echo 'FATAL: public/index.html still has the test Turnstile sitekey'; exit 1)

# 4. Deploy (predeploy reruns the gates, then wrangler deploy)
npm run deploy

The grep guards are intentionally hard-fail. If you see "FATAL", fix before deploying — bypassing them ships the site open to abuse.

Live verification after deploy

# Healthcheck
curl -s https://wizardsofodd.com/api/health   # {"ok":true}

# Missing-token → 429
curl -s -X POST https://wizardsofodd.com/api/council \
  -H 'content-type: application/json' \
  -d '{"question":"test"}' \
  | grep -i ghost   # should match "...not a ghost."

Then open https://wizardsofodd.com in a browser and confirm:

  • Turnstile widget renders
  • Solving it (or auto-solving) → form submits → the selected wizards (5 by default) + verdict

Tech debt — friction driven upstream

Through the rc era this project relied on several codegen workarounds (vendored tarballs, local Zod schemas, as unknown as casts) for bugs in the payload/queries generators. They all shipped upstream and were removed; the project now tracks the released line (the @metaobjectsdev/* pins in package.json). The short version of that friction — and the bugs this project surfaced, including the drift-gate blind spot it had and the promptRender FQN fix that landed in 0.15.18 — lives in the friction log in how-metaobjects-is-used.md.