* chore(coherence): adopt Coherence with specs, invariants and a PR report Install @posthog/coherence as a frontend dev dependency and wire its Claude Code hooks into the committed .claude/settings.json, so every agent session in the repo gets the vocabulary, specs, journal and checks. The hooks call the binary under frontend/ through $CLAUDE_PROJECT_DIR. Declare 23 components and the entrances that carry outside trust, and six invariants, each refuted with a deliberate break before it counted: - every /api route requires auth and its scopes, except a named public list (new route sweep in tests/endpoints/test_route_guards.py) - a hidden rom, platform or firmware answers every id route exactly as a missing id would (new sweep over every method of every id route) - the unscoped save, state and screenshot fetches by id are reached only from their own routers (chokepoints) - validate_path never resolves outside its base folder The PR workflow runs in comment mode: one report comment, failing only when Coherence itself breaks. Trunk skips Prettier and markdownlint on *.spec.md and *.practice.md, since Prettier escapes their globs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(routes): satisfy mypy in the route guard sweep Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(coherence): record the worktree test database enactment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(roms): answer a hidden rom like a missing one on file routes; tighten route sweeps Addresses Greptile's review on #5584. The rom file delete and reading-progress routes answered "File not found" for a hidden rom but "Rom with id N not found" for a missing one, so a caller could tell them apart. They now raise the same not-found. The route sweeps now reach the checks they claim to test: a protected route with no scopes counts as public (its guard admits anyone), write requests carry a body and upload name so validation can't answer first, RomFile-keyed routes get a real file id, error bodies are compared with the id normalized, and the rom routes are swept with the rom hidden directly, through its platform, and by the unrated filter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(coherence): tighten route guard sweeps and isolate oracle test databases (code-review) - Count a route as protected only when its endpoint is the scope-checking wrapper with non-empty scopes, not just when it declares the bearer scopes. - Treat 401 and 403 like 422 as never reaching the visibility check, and sweep /api/streaming/states/{rom_id}. - Run pytest oracles with a per-run ROMM_TEST_DB_TAG unless one is set. - Use the shared setup-frontend action and ubuntu-24.04 in the workflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(tests): simplify route guard sweeps and path tests (simplify) - Mark scope-guard wrappers with required_scopes so the auth sweep reads a public attribute instead of the private closure. - Derive the entity regex from one segment list, ask each missing id once, and run the three hide scenarios through one sweep helper. - Fold the new validate_path inputs into the existing valid, traversal and absolute path tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(coherence): trim workflow comments (review-polish) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(filesystem): restore the validate_path property test the paths invariant cites Filesystem.spec.md names test_validate_path_never_escapes_its_base as the invariant's evidence, and Coherence selects it by name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(coherence): run hooks from the local binary instead of npx Each hook calls frontend/node_modules/.bin/coherence directly and exits 0 when it isn't installed, so sessions without frontend deps (fresh worktrees, backend-only setups, cloud setup still installing) don't report a hook error on every tool call, and no hook pays npx's startup. Also commits the journal and run records left by earlier sessions, which .coherence/.gitignore keeps as durable records. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(tests): share the bearer, user token and hide helpers Tests built the same Authorization headers and HiddenEntity rows in about 40 local copies, and two modules imported private helpers from other test files. They now use bearer, user_headers and hide from tests/auth_helpers.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(endpoints): one 404 for a missing or hidden rom, platform or firmware Each route wrote the missing-row 404 and the hidden-row 404 separately and had to keep them identical by hand. visible_rom_or_404, visible_platform_or_404 and visible_firmware_or_404 take the lookup result and raise one answer for both, either the standard not-found or a bespoke detail. Re-reads after a write keep a plain not-found check, so an edit that hides the row doesn't turn into a 404 after it succeeded. The RomFile-keyed routes (/roms/{file_id}/files, .../files/content/{name}, /roms/{file_id}/patch) name their path parameter file_id instead of id, so the route sweep reads which id a route takes from its path and drops its hand-kept list. URLs are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(setup): develop inside WSL on Windows @posthog/coherence installs only on Linux and macOS, so npm install in frontend/ fails on native Windows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(coherence): record the whole-repo chokepoint run Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(types): regenerate API types for the file_id patch route Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
9.6 KiB
RomM - Repository Guide for Contributors & Agents
RomM is a self-hosted ROM manager and player: scan a game library off disk, enrich it with metadata from 10+ providers, browse it in a web UI, and play in the browser.
The frontend talks to the backend over /api/* (REST) and /ws (Socket.IO). TypeScript types are generated from the backend's OpenAPI schema into frontend/src/__generated__/ - the backend is the single source of truth for API shapes.
Deep-dive references
docs/BACKEND_ARCHITECTURE.md- layers, request flow, auth and visibility model, scanning, sockets, tasks. Routes, columns and env vars live in the code it points to.docs/FRONTEND_ARCHITECTURE.md- routing, stores, services, theming, build tooling.DEVELOPER_SETUP.md- Docker and manual local setup (mock library,.env, services).CONTRIBUTING.md- contribution flow, AI-assistance disclosure, translations.
Stack-specific guidance
frontend/AGENTS.md (frontend layout and commands) and backend/AGENTS.md (backend commands) load when you work in those directories. The UI lives in frontend/src/.
Skills - load the focused guide for your task
These live in .claude/skills/ and carry the detailed rules. Invoke the one that matches what you're doing. pr-ready, address-bot-reviews, and draft-announcement are run by the user, not auto-invoked.
Repo-wide rules
Disclose AI assistance in the PR. RomM requires it (see CONTRIBUTING.md): state that AI was used and to what extent. This is mandatory and non-negotiable for agent-written contributions.
Branch off master; open PRs against master. Fork → feature branch → PR. Don't push to master.
Linting is via Trunk (trunk fmt && trunk check), which wraps ruff, black, isort, ESLint, Prettier, and more, and runs in CI on every PR. Never commit with --no-verify.
The backend owns the API contract. Changed a response schema or route? Regenerate frontend types (npm run generate) and re-typecheck.
Tests travel with code. New logic gets a test; new endpoints get endpoint tests; new v2 primitives get a Storybook story (+ play() if interactive).
Verify before handoff. Don't say "done" on UI work without testing it in the browser in both themes and all input modalities. See review-polish.
English first. Outside of language files, all code, comments, identifiers, .md files, and commit/PR messages are in English.
No em-dashes. Never use em-dashes (U+2014) when writing comments or text. Use commas, parentheses, or separate sentences instead. An em-dash string literal is code (an empty-value glyph) and stays allowed.
Prefer a check to a model review. A mechanical rule (an import, a character, a CSS pattern, an SFC shape) belongs in ESLint, tsconfig, or Trunk. When a review finding repeats, propose a check as a named follow-up; see review-polish.
Keep comments short. Comments should be concise, and focus on the "why" rather than the "what" (the code itself is the "what"). A comment is one or two lines; a docstring is one sentence plus Args:/Returns: when the signature needs it. Three or more lines of prose means you are explaining, not commenting. See review-polish for what to cut.
Don't restate the code. The code is self-documenting, so skip comments that narrate what a reader can already see (which button sits where, that something is disabled when empty, obvious ordering). Comment only non-obvious "why" that the code can't convey on its own.
One home per value. Before declaring a constant, type, limit, regex, or store getter, search for an existing one and import it. Validation limits live on the model and are imported by endpoints; a type lives in the component that owns it and is exported. Don't add a near-duplicate that differs only by sorting, and don't invent an identity scheme where a foreign key already exists.
ROM and platform visibility is access control. A ROM or platform hidden from a user, directly or by age limit, must be unreachable for them, not only absent from lists. Any route, task or socket event that reaches a ROM-, platform- or firmware-linked resource checks visibility: visible_rom_or_404/visible_platform_or_404/visible_firmware_or_404 on a single fetch (one 404 for missing and hidden), a visibility filter in SQL (before any limit) on a list, and the ROM's audience on a broadcast. Handler queries take visibility as a required argument, and a system caller passes UNRESTRICTED explicitly. A hidden resource answers like a missing one. Files under /assets/romm/resources/ are served by nginx without auth and are out of scope.
Never commit secrets. Never commit secrets (API keys, passwords, tokens, etc.) to the repo. Use environment variables or secret management tools instead.
Don't explain a change. Avoid comments that explain why a change was made to the code, or that record what the code used to do. Focus instead on the current behaviour of the code and how it works. Reasons for a change go in the commit message and the PR body.
Python tools live in backend/tools/. Standalone dev/test utilities and scripts (not part of the app runtime) go in backend/tools/, not scattered across backend/.
Link PRs to issues. In the PR description, use Fixes #XXXX for issue/bug fixes and Closes #XXXX for feature implementations.
Use the PR template. Base every PR description on .github/PULL_REQUEST_TEMPLATE.md.
Show UI changes in the PR. A PR that touches anything visible ships with at least one screenshot under the template's Screenshots heading, captured from the running app during the review-polish browser pass. Enough shots to convey what changed, not a catalogue of every state. Save the files outside the repo and never commit them, and upload them with gh pr create --attach (or gh pr edit --attach on an existing PR) rather than handing the paths to the user. --attach needs gh 2.99.0 or later (check gh --version). Claude Code cloud sessions block the GraphQL API that gh pr create and gh pr edit use, so there --attach fails whatever the version: send the files to the user to attach instead.
Diagram architectural changes in the PR. When a change moves a boundary (a new service, task, or provider in a path, a model relationship, a different call path across layers, an auth or socket flow), the PR description carries a mermaid block showing it. GitHub renders them. Draw what changed, not the whole system. See review-polish.
Never run the full backend test suite locally
A bare uv run pytest (or pytest -vv) over backend/ runs serially, takes 20+ minutes, and
burns a huge number of tokens on output. Don't do it, even to "double check" at the end.
Instead, select the tests affected by the change and run only those. Add -n auto when the
selection spans a directory or more: the suite is set up for pytest-xdist (one database per
worker).
cd backend
uv run pytest tests/path/to/test_file.py # one file
uv run pytest tests/path/to/test_file.py::test_name # one test
uv run pytest -n auto tests/handler/ tests/endpoints/ # affected areas, in parallel
uv run pytest -k "scan or queue" # by name pattern
It gains nothing on a single file or test, and --pdb and -s don't work under it, so drop it
when debugging.
Pick the targets from the diff: the test file mirroring each changed module
(backend/<area>/x.py → backend/tests/<area>/test_x.py), plus the tests of the
callers of anything whose signature or behavior you changed (grep for the symbol).
CI runs the whole suite on the PR (pytest.yml, MariaDB + PostgreSQL). That is the
place for full-suite coverage; local runs stay scoped.
In a git worktree, run tests against a temporary database. Every checkout shares
romm_test, and one on another branch can migrate it to a revision yours lacks, so
every test errors in setup with Can't locate revision. Set ROMM_TEST_DB_TAG to a
tag unique to the worktree: the run creates and migrates romm_test_tmp_<tag> (one per
xdist worker), then drops it at the end. Never stamp or downgrade the shared romm_test.
ROMM_TEST_DB_TAG=my_worktree uv run pytest -n auto tests/handler/
Environment setup
Each tool version is pinned in one file: Python in .python-version, uv as ARG UV_VERSION in docker/Dockerfile, and Node in frontend/.nvmrc. Don't install these by hand. Run scripts/dev-setup.sh, which installs the pinned toolchain, the backend and frontend dependencies, and the Trunk launcher, and skips anything already in place. Add --db to also install and start MariaDB with the pytest database (Debian/Ubuntu only). Rerun it whenever a version is wrong or a dependency is missing.
In Claude Code cloud sessions, .claude/hooks/cloud-setup.sh runs scripts/dev-setup.sh --db at session start. Setting the environment's setup script to bash /home/user/romm/scripts/dev-setup.sh --db || true gets the install cached in the environment snapshot, so the hook only has to restart MariaDB. The setup script doesn't run from the repo root and CLAUDE_PROJECT_DIR isn't set yet, so the path must be absolute. The cloud clone lives at /home/user/<repo-name>; adjust the path for a renamed fork. || true keeps a failed install from blocking the session (the hook retries it). Other agents should run scripts/dev-setup.sh --db from the repo root as their setup step.
Quick command reference
Setup: see DEVELOPER_SETUP.md. Docker path is cp env.template .env → docker compose build → docker compose up -d (app at http://localhost:3000).
Lint (both stacks): trunk fmt && trunk check.