* ✨ Enable closed schemas for RPC methods * 🐛 Fix duplicate make-dummy-request test helper definition The branch added a variadic DummyRequest/make-dummy-request pair but left the pre-existing single-arg definition in place. Because it was loaded last, zero-arg (make-dummy-request) calls added by prepare-rpc-params and rpc-nitrate-test threw ArityException, which broke 384 tests and caused 14 downstream assertion failures. Remove the stale duplicate so the variadic definition is the only one, and drop the now-unused yrq alias and duplicate yres alias. AI-assisted-by: deepseek-v4.1-flash * ✨ Add focused tests for make-dummy-request helper Pin the call contract of make-dummy-request, which the suite uses in three styles: no arguments, a single options map, and keyword arguments. The helper's redefinition shadowing in 8ca95adb98 was only caught by a full-suite run with hundreds of unrelated errors; these tests fail locally in a focused --focus run. Cover the zero-arg defaults, map and keyword overrides, the :body-bytes -> ByteArrayInputStream wrapping, :body-stream precedence, and cookie readback. Also clarify the docstring to list all supported call styles. AI-assisted-by: deepseek-v4.1-flash * 🚑 Prevent RPC client params from overriding auth context Strip qualified keys from decoded request params before merging them with the server-built auth context, so transit bodies can no longer override ::profile-id, ::auth-type or ::token-perms. Adds a regression test proving the override and the fix. AI-assisted-by: muse-spark-1.3-contributor * 📚 Merge backend subtleties memories under generic name Rename rpc-db-worker-subtleties to subtleties and fold in http-storage-filedata-subtleties, so the name no longer enumerates topics. Update all mem: references accordingly. AI-assisted-by: muse-spark-1.3-contributor * ✨ Add realistic tests for RPC auth override Cover the transit wire vector and the real wrapped :get-profile method with two database profiles, proving a session cannot read another profile by smuggling :app.rpc/profile-id in the body. AI-assisted-by: muse-spark-1.3-contributor * ✨ Add e2e test for RPC auth context override Parametrize rpcPost with contentType, accept and query so e2e can send hand-written transit bodies without new dependencies. The new test proves a transit-smuggled :app.rpc/profile-id no longer overrides the session in get-profile. Also fix the demo email assertion in auth-flow to the current uuid format. AI-assisted-by: muse-spark-1.3-contributor
6.7 KiB
Backend Architecture and Workflow
Backend: JVM Clojure; Integrant; PostgreSQL; Redis/Valkey; RPC; HTTP; storage; mail; audit/logging; workers.
Focused memories
- Cross-cutting backend subtleties (RPC, DB, workers, cron, HTTP/sessions, storage, file data):
mem:backend/subtleties - Storage abstraction, logical buckets, object lifecycle, deduplication, access, and garbage collection:
mem:backend/storage. - Embedded Ladybug graph experiment, projection, incremental sync, console, and risks:
mem:backend/graph-experiment - Auth flows, permission model, teams, projects, invitations, comments, webhooks, audit:
mem:backend/auth-permissions-product-domains - Services, task-queue/Pub-Sub topology constraints ->
mem:prod-infra/core.
Stable namespace map
app.rpc.commands.*: RPC command implementations exposed under/api/rpc/command/<cmd-name>.app.rpc.permissions: permission predicate/check helper factories.app.http.*: HTTP routes and middleware.app.auth.*: provider-specific authentication helpers such as LDAP/OIDC.app.loggers.*: audit, webhook, database, and external log integrations.app.db.*/app.db: next.jdbc wrapper and SQL helpers.app.tasks.*: background task handlers.app.worker: task execution/cron plumbing.app.main: Integrant system map and component wiring.app.config:PENPOT_*env config and feature flags.app.srepl.*: development REPL helpers for manual backend operations (data inspection, migration helpers, one-off admin tasks).app.nitrate,app.rpc.commands.nitrate, andapp.rpc.management.nitrate: external Nitrate subscription/organization integration, gated by the:nitratefeature flag and shared-key HTTP calls.
RPC conventions
RPC commands are defined with app.util.services/defmethod and schemas. Use get- prefixes for read operations. Command metadata usually includes auth, docs version, params schema, and result schema. Return plain maps/vectors or raise structured exceptions from app.common.exceptions.
Backend RPC command areas without focused memories include access tokens, binfile, demo, feedback, file snapshots, fonts, management, Nitrate, and webhooks beyond the notes in mem:backend/auth-permissions-product-domains; inspect nearby command tests and command metadata before changing them.
DB conventions
app.db helpers accept cfg, pool, or conn in most places and convert kebab-case to snake_case:
db/get,db/get*,db/query,db/insert!,db/update!,db/delete!.- Use
db/run!for multiple operations on one connection. - Use
db/tx-run!for transactions.
Database migrations live in backend/src/app/migrations/; pure SQL migrations are under backend/src/app/migrations/sql/. SQL filenames conventionally start with a sequence and verb/table description, e.g. 0026-mod-profile-table-add-is-active-field. Applied migrations are tracked in the migrations table.
For interactive PostgreSQL access with correct dev defaults, use scripts/psql; to dump
the current DDL schema, use scripts/db-schema (see mem:scripts/psql).
For deeper details on transaction semantics, advisory locks, Transit vs JSON helpers, and dev/test DB URLs: mem:backend/subtleties.
Background tasks
A task handler is an Integrant component with ig/assert-key, ig/expand-key, and ig/init-key, returning the function run by the worker. New tasks also need wiring in app.main: handler config, worker registry entry, and cron entry if scheduled.
For worker dispatch, cron, retry semantics, deduplication, and queue internals: mem:backend/subtleties.
REPL
In devenv, backend nREPL is exposed on port 6064.
Non-interactive eval (preferred for agents)
./scripts/nrepl-eval.mjs connects to an already-running nREPL server and evaluates code. Session state (defs, in-ns) persists across invocations via a stored session ID in /tmp/penpot-nrepl-session-<host>-<port>.
./scripts/nrepl-eval.mjs '(+ 1 2)' # single expression
./scripts/nrepl-eval.mjs "(require '[my.ns :as ns] :reload)" # reload after edits
./scripts/nrepl-eval.mjs -e # inspect last exception (*e)
./scripts/nrepl-eval.mjs --reset-session '(def x 0)' # discard session, start fresh
./scripts/nrepl-eval.mjs <<'EOF' # multi-expression heredoc
(def x 10)
(+ x 20)
EOF
Default port is 6064. Use -p <PORT> for a different port. Use -t <MS> to override the 120s timeout. Do not start the nREPL server — assume it is already running.
Interactive REPL
backend/scripts/nrepl starts a REPLy client connected to the running nREPL.
For an in-process backend REPL (where you control the JVM lifecycle), stop the running backend first so port 9090 is free, then run backend/scripts/repl. Useful top-level helpers include (start), (stop), (restart), (run-tests), and (repl/refresh-all). Many app.srepl.main helpers accept the global system var, e.g. manual email or maintenance operations.
Fixtures
Fixtures can populate local data for manual testing/perf work. From the backend REPL, run (app.cli.fixtures/run {:preset :small}); fixture users conventionally look like profileN@example.com with password 123123. Standalone fixture aliases may exist, but check current backend/deps.edn before relying on old command names.
Performance
- Type Hinting: Use explicit JVM type hints (e.g.
^String,^long) in performance-critical paths to avoid reflection overhead. - Batch inserts: Use
db/insert-many!for bulk row inserts — generates a single SQL with multiple parameter tuples. Avoid on very large datasets (SQL length / parameter count limits). - Server-side cursors: Use
db/plan(fetch-size 1000, forward-only, read-only) ordb/cursorfor large result sets. Never fetch large collections into memory at once. - Transaction discipline: Use
tx-run!for writes (opens a transaction),run!for reads (single connection, no transaction). Set:read-onlyontx-run!when applicable to let PostgreSQL optimize.
Lint and Format
IMPORTANT: all CLI commands must be executed from the backend/ subdirectory.
- Linting:
pnpm run lint:clj. - Formatting:
pnpm run check-fmt:cljto check,pnpm run fmt:cljto fix. After runningfmt:clj,check-fmt:cljis redundant. Avoid unrelated whitespace diffs.
Before linting: if delimiter errors are suspected (after LLM edits), run
scripts/paren-repair on the affected files first. Delimiter errors produce
misleading linter/compiler output. See mem:scripts/paren-repair.
Testing
Backend test commands, coverage rules, and conventions: mem:backend/testing.
Cross-cutting testing principles, anti-patterns, and verification checklist: mem:testing.