Architecture boundaries
Scope: every change to the repository, the Worker, or a deployed surface.
Rule. Keep the boundary that owns a decision. A public page does not import product code. A product page does not publish internal records. An internal tool does not become a public route. Operational tooling does not ship as application code.
| Boundary | Owns | Location | Rule |
|---|---|---|---|
| Public | Marketing, institutional pages, and the public developer reference | apps/public-site/, apps/dev-site/, generated public/ |
Every page is public. Generated HTML is never hand-edited. |
| Product | Mind Platform, welcome and account surfaces, API documentation | apps/mind-platform/, apps/worker/src/, public/signup/ |
Product behavior stays in the Worker and its owning page source. Do not duplicate it on a public page. |
| Internal | Staff console, internal login, doctrine, audits, work notes | apps/worker/src/pages/internal/, public/internal/, internal/ |
Staff material is authenticated and excluded from public builds, indexes, and machine-readable files. |
| Operations | Migrations, schema, deploy gates, health checks, recovery | migrations/, db/, scripts/, wrangler.toml |
Production changes go through a numbered migration and the deploy gate. No ad-hoc command. |
The root package remains the @mpalo/worker workspace and owns the single Cloudflare Worker configuration and deployment boundary. Worker runtime source lives under apps/worker/src/. The root config owns deployment, build, asset-bucket, and D1 migration paths. See AGENTS.md and ARCHITECTURE.md.
| Do | Do not |
|---|---|
Put a new public page in apps/public-site/pages/ and build it. | Edit the generated HTML in public/. |
Keep an internal record in internal/docs/. | Render an internal document on a public developer page. |
Add a Worker route in apps/worker/src/stw.js and a page in the owning app. | Create a second deployment or a second Worker. |
Change schema through migrations/NN_*.sql. | Run d1 execute --remote with a file or a fixture. |
API and error envelopes
A failure carries its text in error. A success returns payload keys.
Rule. Return a real HTTP status code. A failure body is { "error": "A human readable sentence." }. Optional details carries field-level extras. Never return a stack trace, a SQL fragment, an internal identifier, or a secret in the body. Log the diagnostic detail and return the sentence.
{
"error": "The API key has expired. Create a new key to continue.",
"details": { "field": "authorization" }
}
A success may include message as an action confirmation. A failure never puts its text in message. The central frontend reader tolerates both, but a consumer that checks .error misses a message-only failure. Auth endpoints additionally pin exact neutral bodies; do not soften them.
Do: name the observed condition and the next step.
Do not: write "Something went wrong", "It looks like", "Oops", or an ellipsis in a failure body. Do not invent a diagnosis the system cannot verify.
Check the envelope with node scripts/error-envelope-check.mjs. Check the v1 contract with npm run check:api-contract. See API design notes.
Auth and security
Access is a staff flag. Secrets never live in SQL, a page, or a repository fixture.
Rule. Staff access is is_global_admin or is_support_user on users. Create staff accounts with scripts/create-staff-account.mjs, which takes the password interactively and hashes it locally. Never create a staff account from a committed SQL file. MFA is enabled through the application flow; users.mfa_secret is an AES-GCM blob, so a plaintext secret written by SQL locks the account out.
Rule. Every response carries the surface's security headers. The Worker sets a Content Security Policy, X-Content-Type-Options, X-Frame-Options, and a referrer policy. Scripts come from the page or the approved challenge origin. Do not add an inline handler or a third-party script without updating the policy and the check.
| Do | Do not |
|---|---|
| Hash a password on the server with the approved function. | Commit a password, a token, a TOTP secret, or a bcrypt hash. |
| Use the shared auth middleware and role check. | Repeat an authorization check in a page script. |
| Validate input at the boundary and bind SQL parameters. | Build a query by string concatenation. |
| Log the diagnostic detail without personal data. | Return a stack trace or a raw provider error. |
Run node scripts/auth-contract-tests.mjs after touching auth. It starts a local Wrangler server and pins statuses and neutral bodies. Read the security overview before changing a trust boundary.
Database and migrations
Production is only ever changed by a numbered migration.
Rule. A production schema change is a numbered forward migration in migrations/NN_*.sql, applied with wrangler d1 migrations apply. Never use d1 execute --remote with a file, an ad-hoc ALTER, a fixture, or a seed. This rule exists because it was broken: a committed test fixture was applied to production and published a live administrator's credentials. Read DATABASE.md before touching D1.
- Every file that writes fabricated rows opens with the production guard. The guard makes D1 roll back the whole file when the database is marked production.
- Wrangler applies every top-level
.sqlfile inmigrations/. Only numbered forward migrations belong there. History and one-off repairs belong inmigrations/archive/; diagnostics belong inscripts/db-queries/. - Seeds under
apps/worker/src/mind/db/are local development only and stay guarded. - Regenerate
schema.sqlwithnode scripts/db-export-schema.mjsafter a migration. - Rehearse a destructive change against a real snapshot with
sqlite3 -bailandPRAGMA foreign_keys=ONin the same stream. Without-bail, the rehearsal lies to you. - Capture a Time Travel bookmark before destructive work. Run
npm run db:backupand verify the dump restores. Runnpm run db:healthafter the change. - The deploy gate refuses to ship with an unapplied migration or schema drift. Fix the cause; do not bypass the gate.
Do not apply a file from apps/worker/src/mind/db/ to production. Do not edit schema.sql by hand. Do not mark a database as production by guessing; the sentinel is written once, deliberately, after migration 58.
Run npm run check:sqlsafety and node scripts/db-drift-guard.mjs before a database change ships. The operations runbook is database operations.
Deployment
One Worker, one gate, one command.
Rule. Deploy with npm run deploy. Do not run a bare npx wrangler deploy. The script refuses to ship when production has an unapplied migration, when a table, column, index, or trigger is missing from schema.sql, when the API contract and routes disagree, when generated validators are stale, or when an external font dependency exists.
- Build the static output with
npm run build:static. The public site and developer site write generated files intopublic/. - Run the checks. A failing check is a reason to fix the cause, not to skip the gate.
- Apply any numbered migration before deploying code that depends on it.
- Deploy with
npm run deploy. - Verify the deployed surface with the runbook in
internal/docs/operations/verify-deployment.md.
SKIP_DEPLOY_GATE=1 npm run deploy exists for a genuine emergency. It prints that the gate was skipped and leaves a record. It is not a shortcut for a failing check.
Do not deploy generated files that were hand-edited. Do not change the Worker name, route, binding, or asset bucket as part of an unrelated change. Do not deploy from an uncommitted tree unless the change is a deliberate recovery action.
Testing and observability
A change is not finished when it renders. It is finished when its contract and its failure path are checked.
| Change | Required check |
|---|---|
| Any source change | npm run check |
| Auth, session, or MFA | node scripts/auth-contract-tests.mjs |
| SQL, schema, or a query column | npm run check:sqlsafety, node scripts/db-drift-guard.mjs, npm run db:health |
| Worker routes or the v1 API | npm run check:worker, npm run check:api-contract |
| Response shapes | node scripts/error-envelope-check.mjs |
| Developer site | npm run build:dev-site, npm run check:dev-site |
| Inline scripts or CSP | node scripts/check-inline-js.mjs, node scripts/check-csp.js |
Rule. Log what the system observed, not what the person should feel. Do not put a password, token, MFA secret, full email body, or memory content in a log. A failure log names the operation, the status, and a correlation identifier that is not a personal identifier.
Use the health check and the incident runbook before a release that changes a critical path. An incident is handled in internal/docs/operations/incident-response.md: contain, record, notify the owner, then fix the cause. Do not publish incident detail on a public surface.
Enforcement
These gates are the engineering rule.
npm run checkcombines the dependency audit, Worker checks, Mind Platform check, public-site check, developer-site check, token checks, design checks, and icon check.npm run deployis the only deploy path. The gate is part of the command.node scripts/error-envelope-check.mjsflags ambiguous failure bodies.node scripts/auth-contract-tests.mjspins auth behavior against a local Worker.node scripts/db-drift-guard.mjscompares code column references with the schema, and--remotecomparesschema.sqlwith production.npm run db:healthchecks the sentinel, referential integrity, orphans, migration ledger, schema baseline, invariants, and capacity.
Owner: the Engineering owner for the Worker and operations; the Database owner for migrations; the Security owner for auth and trust boundaries. Review: before merge and before deploy.