Development
This page is published from the server repository. Edit on GitHub View source
Setting up a working copy, the conventions every change follows, and what to touch when adding a route, a variable or a schema field. CONTRIBUTING.md has the pull request rules; Testing has the suites.
Set up
You need Node.js 20, Docker with Compose v2 (for the databases) and Git.
git clone https://github.com/synthaea-lab/server.git
cd server
npm ci
npx prisma generate
Two ways to run the application:
-
The Compose stack (README-SETUP.md): PostgreSQL, the application in development mode with the repository bind-mounted, and the mTLS proxy. Closest to how agents reach the server.
-
Directly on Node, against any PostgreSQL 15:
cp .env.example .env # set DATABASE_URL and the secrets npx prisma db push npm run dev # http://localhost:3000Without the proxy, agent routes refuse every request (no proxy secret, no certificate headers). Use the Compose stack, or call the handlers from tests, to exercise them.
| Command | Does |
|---|---|
npm run dev | Next.js development server |
npm run build, npm start | Production build, then the standalone server (Deployment) |
npm run lint | ESLint (.eslintrc.cjs) through next lint. The configuration uses @typescript-eslint/parser and @typescript-eslint/eslint-plugin, which package.json does not list yet; install them to run it. |
npx tsc --noEmit | Type check |
npx vitest run tests/unit | Unit tests |
scripts/run-tests.sh | Unit and integration tests against a throwaway database |
npm run db:push, npm run db:generate, npm run db:studio | Apply the schema, regenerate the client, browse the database |
Conventions
- Tenancy comes from the identity, never the request. A console route gets the tenant from
requireTenantMemberorrequireTenantAdmin; an agent route fromauthenticateAgent. Every query filters on it. Another tenant's record is a404. - Agent and console identities never mix. Agent routes authenticate with the certificate the proxy passed; console routes with a session. A route serves one or the other.
- The middleware runs on the Edge runtime.
middleware.tsand what it imports cannot use Prisma,lib/auth.tsornode:modules. Server components and route handlers run on Node. - Tenants and organizations share an id. Create tenants only with
provisionTenant. - Logic in
lib/, I/O at the edge. Keep decisions in pure functions that take plain data and a clock; keep Prisma calls in the route or a thin store module. Most of the unit suite depends on this split. - Errors. Wrap handlers in
routeand throwHttpErrorsubclasses (BadRequest,NotFound) for expected refusals; validate bodies with zod. Never put a driver message or stack in a response. - Audit in the same transaction. An administrative change writes its row with
recordAudit, passed the transaction the change runs in, and adds its action toAuditAction. - Agent data is hostile. Bound every agent-supplied string, array and depth. Cut text a
process on the host controls, refuse only what shipped content controls, and never answer
4xxfor something the agent would have to retry (Agent protocol). - Best effort after acceptance. Once an upload is stored or queued, secondary work
(counters,
last_seen) must not turn the answer into a5xx. - Shared wire formats follow their owner. The agent owns the event, detection, beacon and manifest formats. Change the server's validation with the owner's golden fixtures, and keep the contract tests passing (Testing).
- Code comments say why. State the invariant or the reason; the documentation explains the mechanism. When a change alters what a functional area does, update its module note and the page here that describes it.
Adding things
A console route
- Create
app/api/<resource>/route.ts; guard it withrequireTenantMemberorrequireTenantAdminfirst. - Scope every query to the caller's tenant.
- Add an integration test with another tenant's data, and extend
tests/integration/tenancy.test.ts.
Console routes need nothing in the middleware or nginx: every path not listed as public is session-protected by default.
An agent route
- Create the route and call
authenticateAgentfirst. - Add the exact path to the public list in
middleware.ts, or the middleware redirects the agent to/login. - Add an nginx location that requires a verified client certificate, unless the path is already
under
/api/ingest/(nginx.conf), and size its body limit. - Document the endpoint in Agent protocol.
A scheduled job
Create app/api/cron/<name>/route.ts, call verifyCronRequest first, bound the work per call,
make a rerun harmless, and list it in Scheduled jobs. /api/cron/* is
already public in the middleware and rate-limited in nginx.
An environment variable
Read it where it is used, refuse a set-but-invalid value rather than falling back to the default,
and add it to Configuration (and to .env.example if a
local run needs it).
A schema field
Edit prisma/schema.prisma, then npx prisma db push and
npx prisma generate. A new non-null column on a table with rows needs a default. There is no
migration file to write (Data model). In the Compose stack,
rebuild the server image afterwards.