# pie-night (agent notes) Internet-visible package app. `src/app.ts` is the fetch handler (`kody.app.entry`); static files are `kody.app.assets` in `public/`. One optional credential slot, `webPush` (signing, provider `web-push`). ## Layout - Pages: `/` (home, or a 302 to the home-redirect event unless `?home`), `/new` (create; needs Kody sign-in), `/e/:code` (canonical slug 302s), `/e/:slug/admin`, `/e/:slug/og.png?v=` (immutable when `v` matches), `/e/:slug/manifest.webmanifest`, `/offline`, `/sw.js` (with `Service-Worker-Allowed`), `/favicon.svg`, `/manifest.webmanifest`, `/robots.txt` (Disallow all, pages send `noindex`). - API: `GET /api/me`; `POST /api/events` (signed in; 10/user/day, 50 per user); `/api/e/:slug` `GET`, `/pies` `GET|POST`, `/pies/:uuid` `PATCH|DELETE` (anyone), `/presence`, `/push` (`{subscription, host}`, `{check, endpoint}`, `{off, endpoint}`), `/admin/{state,event,reset,restore/:uuid,hosts,test-push}` (hosts only). Old `/api/pies*` and `/api/presence` map to the home-redirect event so cached single-event pages keep working. - Hosts: the event creator, its co-hosts, or a member of the owning org (`X-Kody-Visitor.role`). Login link: `/-/auth/login?app=&return_to=...`. - `src/store.ts`: SQL tables `pies` (keyed by `night`), `events` (slug PK, `night`, `version` bumps on every edit), `event_presence`, `push_subs` (event + endpoint; max 500 per event). Reset writes a new `night`. `migrateLegacy` moves the old single-event storage keys (`event`, `admin_usernames`) into a named event, reusing its `night` so no pie rows move; the old keys stay untouched. - `src/pies.ts`: pure helpers (limits, slugs, themes, parsing, formatting). - `src/page.ts`: HTML for every page; `src/art.ts`: the pie SVGs. - `src/og.ts`: Satori (asm yoga, no wasm: the Kody runtime refuses WebAssembly) lays out text to SVG paths; `src/raster.ts` is a small pure-JS rasterizer that fills them onto the pre-rendered background in `src/og-bg.ts`. Fonts are subset TTFs in `src/fonts.ts` (`scripts/embed-fonts.mjs`). In-memory cache of 40 renders; the `?v=` version makes CDN caching safe. - `src/push.ts` + `src/push-crypto.ts`: RFC 8291 aes128gcm, VAPID JWT signed by `credential('webPush').sign(...)`. Only known push hosts. 404/410 removes the subscription. Sends after the response via `ctx.waitUntil`. - `src/sw.ts`: service worker (offline shell, network-first pages and pie lists with one retry on TypeError, cache-first assets with content-type checks, push + notificationclick). `public/pwa.js` registers it (off on localhost unless `?sw=1`), shows the Refresh bar only while a waiting worker differs from the controller, posts both `SKIP_WAITING` shapes, reloads on `controllerchange` with a 4s fallback. - `public/app.js` (event page), `public/home.js` (home, create, not found), `public/styles.css` (themes via `body[data-theme]`, dark via `prefers-color-scheme`). Bump `ASSET_VERSION` in `src/app.ts` when any of these or the service worker change. - Exports: `.` (read one event's pies), `./setup-push`, `./migrate`, `./settings` (home redirect). Storage is per caller, so these only touch the caller's own install. - `scripts/render-assets.mjs`: icons, badge, fallback `public/og.png`, the share background, and `.kody/icon.png` (needs `playwright-core`). - Tests: `node --test test/`. ## Edge cases - Never put the street address in the app or its share preview. - Max 200 pies per night; fields capped at 60/80/140 chars. Push: a pie add notifies every subscriber of that event except the adding device. ## Batches and presence - `POST /api/e/:slug/pies`: `{ who, pies: [{ pie, note }], website? }`. Max 6 filled rows; blank rows skipped. Legacy single `{ who, pie, note }` still accepted. One batch uses one write-rate-limit hit; SQL inserts all rows in one statement. - The browser groups by case-insensitive, whitespace-collapsed family name. - `POST /api/e/:slug/presence`: `{ sid, name, typing? }` or `{ sid, leave: true }`. Random per-tab sid (not saved), heartbeats every 10s (25s while the socket is live), TTL 40s, hidden tabs leave. Public names are explicitly self-chosen and not authentication. Anonymous visitors show a pie/leaf avatar. Presence gets a separate rate-limit bucket (300/IP per 10 min, 20k total/hour), not the pie-write bucket. Old rows pruned as soon as they pass the 40s TTL (which pushes a presence update); active list capped at 40. ## Realtime - Manifest: `kody.app.realtime = { anonymous: "receive" }`. Changing this changes the capability fingerprint and needs owner approval at `https://kody.codes/@/pie-night/approve-capabilities`. Until approved, anonymous `/ws` upgrades are refused and the browser stays on polling. - Browser connects to `/ws/` (one facet per event), sends `kody:ping` every 25s, reconnects with exponential backoff (max 60s). Receive mode means visitors never send app messages over the socket. - Server calls `runtime.realtime.broadcast({ data, facet: slug })` after every HTTP write: `{type:'pies', change:'added'|'edited'|'removed'|'restored', from, who, items, ids, pies}`, `{type:'event', event, pies?}`, `{type:'presence', people}`. Broadcast errors are caught so writes never fail because of realtime. Payloads over ~48 KB drop `pies` and set `refetch: true`; clients then GET `/api/e/:slug/pies`. - `from` is the first 8 chars of the sender tab id (`x-pie-tab` header) so the sender does not toast itself. Live clients poll every 60s and heartbeat every 25s as a safety net.