Backend¶
src/retrospeedlab/backend/ is the FastAPI service at api.retrospeedlab.com. It stores what the search relays, serves the website and the stream, replays every human highscore, and runs the bots that talk to Discord, Twitch and Bluesky.
uv run uvicorn retrospeedlab.backend.main:app
The database tables are created at start-up, and GET /health answers once it is up.
API areas¶
| Area | Routes | What it does |
|---|---|---|
| Live run | /live, /games |
The run the search relays, snapshots, movies, videos and knowledge |
| Lab | /lab-runs, /reports |
Lab sessions, their timelines and reports |
| Highscores | /highscores, /ghosts, /seasons |
Boards, replay verification, ghosts, deaths, seasons and the weekly challenge |
| Ratings | /games/{slug}/ratings |
Players' one-to-five-star ratings per game, version and category |
| Players | /auth/discord/*, /players/me |
Discord sign-in, your account and its deletion |
| Rotation and engagement | /rotation, /engagement |
The stream schedule, featured game, A/B variants and exposures |
| Lakehouse | /lakehouse |
Read-only numbers from the Delta tables |
| Promotion and sharing | /promotion, /share, /articles |
Post results, share cards, technical article drafts |
| Chat and Twitch | /chat, /live/clips |
Chat commands, predictions and clips |
| Operations | /alerts, /hosts, /maintainer, /github/webhook, /kuma/webhook |
Alert inputs, host metrics, the owner's decision page and signed approvals |
| Releases | /games/releases |
The latest announced game versions |
Machine clients authenticate with an API key header; players with a signed session cookie. With STAGE=development the interactive OpenAPI docs are at /docs.
Sign-in with Discord¶
Players sign in with Discord (scope identify only). The backend keeps a signed, HTTP-only session cookie for 180 days, renewed while you play, and checks the origin of every write. DELETE /players/me deletes the account.
The owner's tools sit behind a separate single sign-on on the same Discord app that lets exactly one Discord account in. It works as a forward-auth check for the self-hosted dashboards.
Highscores and replay verification¶
When a human wins in the browser, the page sends the version, the frame count and every input since power-on. The backend:
- refuses a run of another version than the board's, more than ten runs an hour from one address and more than sixty an hour from everyone, and answers 503 while three runs already wait for the replay;
- replays the inputs with the engine's verifier in a subprocess, one at a time, with a fresh scratch folder, 60 s of CPU, 2 GiB of memory, no new processes, a minimal environment and an audit hook that refuses sockets, process spawning and writes outside the scratch folder;
- accepts the run only when the replay played that version and
won_atequals the run's length.
A run that fails is rejected and never stored. A run saved while signed out is kept for 30 minutes until you sign in with Discord. Each board belongs to a minor version and shows the AI's verified record beside the humans.
Alerts¶
backend/alerts.yaml holds the rules, checked every minute: a silent runner or host, stream lag or a frozen stream, a stopped relay, failed Airflow tasks or quality gates, a monitor down, a lab cost spike, a search crash loop, failed CI and security alerts. Every firing rule is a Bugsink issue that resolves itself once its signal clears; a merged pull request resolves the issues it fixes. A full disk is reported by each server's own disk watch (deploy/disk/).
Community¶
- Seasons: every calendar month (UTC) is a season; the finished month's boards are archived.
- Weekly challenge: each ISO week from Monday 00:00 UTC features the next game; the fastest verified human run by Sunday 23:59 UTC wins, announced in Discord.
- Twitch: channel-point rewards (name a worker, pick the game on stream, emote rain, ask the AI), predictions on the live run, clips and chat commands such as
!stats,!recordand!vs. - Discord: record and lab posts, release announcements for every new game version and Beat-the-AI roles. A release post is the CHANGELOG entry's first sentence and the play link; a patch keeps its board, since every old run still replays on it.
- Daily recap: the day's news in at most four lines (levels, bests, the proven run, near misses, lab runs, human runs and the best human against the AI), written by the model from those facts and checked against them, with a fixed template as the fallback. A day with nothing new is skipped. A day after each recap the bot reads its reactions and replies, and the recap learns from them which sections lead, bullets or one story line, emoji or plain, and the hour (16:00, 18:00 or 20:00 UTC).
Promotion¶
- Bluesky posts are drafted with Claude for records, wins, new games, lab sessions and the weekly recap, at most two a day. They publish themselves at the hour that earned the most engagement, and the post style (hook, length, media, tags, call to action) is learned from what performed.
- YouTube Shorts are rendered with music; the learner draws their hook style and weights it by the views each Short earns. A Claude critic reviews the copy and a still against the brand rule (nothing that imitates an existing game, console, character or brand), the facts and the tone; a Short it passes uploads through the YouTube Data API, scheduled for the best free slot. At most five Shorts upload per YouTube quota day; a spent quota holds the rest until the next day.
- Technical articles publish to the blog, LinkedIn and dev.to once the same critic passes their text and cover, at most one a week; a failed step retries every hour.
- Pause: one card in the owner's console holds every upload, post, article and A/B winner until the owner presses Resume. The backend puts it there through the console's producer API with
CONSOLE_API_KEYand long-polls the owner's presses. - A/B winners ship on their own: the backend records each winner the lake names, serves it from then on, rolls it back when the data turns, and says so on a card in the owner's console until the owner presses Seen. A winner whose guardrail goal got worse is not shipped.
promotion.yaml and engagement.yaml hold the posting and experiment settings.
Experiments¶
engagement.yaml declares the surfaces and their switches; the experiments, their variants, the holdout share and the block length are constants in src/retrospeedlab/experiments.py, shared with the lake and the site. Visitors get a variant by a stable hash; time experiments switch variants in 30-minute blocks. A holdout share sees a random order instead of the learned one, so the lake can measure every learned ranking against it. The Twitch title loop picks the channel title through the same mechanism.
Configuration¶
Environment variables only, read once at start-up; a missing required one stops the start. The required names are:
STAGE, DATABASE_URL, VIDEO_DIR, UPLOAD_API_KEY, STREAM_API_KEY, CHAT_COMMAND_KEY, BUGSINK_API_BASE, BUGSINK_API_TOKEN, BUGSINK_ALERTS_DSN, UPTIME_URL, STREAM_ELEMENTS_JWT, GITHUB_TOKEN, GITHUB_WEBHOOK_SECRET, OPS_SIGNING_KEY, PLAYER_SESSION_KEY, LAKE_URL, LAKE_ENDPOINT, LAKE_ACCESS_KEY_ID, LAKE_SECRET_ACCESS_KEY, DISCORD_GUILD_ID, DISCORD_BEAT_AI_ROLE. deploy/backend/.env.example lists them with the optional feature groups; the public addresses and the Twitch channel are constants in src/retrospeedlab/addresses.py.
Optional groups are all or nothing: Discord bot, Discord sign-in, owner single sign-on, the owner's decision page (DISCORD_OWNER_ID with the Discord sign-in), Twitch, Restream, OpenRouter, articles, and promotion (ANTHROPIC_API_KEY and CONSOLE_API_KEY) with its Bluesky and YouTube credentials. Deploy lists where each value comes from.