# Engineering Defaults — Migrations, Trust Boundaries, and Scope Discipline
<!--
ref: @protocol/engineering-defaults
tags: [migrations, schema, security, trust-boundary, scope, defaults]
triggers: [writing a migration, backfilling data, adding a column default, checkout accepts a price from the client, client sends the amount, designing a phased rollout, should this be a new feature or a default change]
trigger_prompt: When deciding what belongs in a schema migration versus application code, how much the client is trusted to send, or whether a problem warrants a new lifecycle rather than a changed default.
category: core
audience: [agent-author, claude-code-user]
version: 9
updated_at: 2026-11-15T05:33:51.326Z
-->
Three defaults that prevent recurring classes of mistake: migrations carry structure only, the client supplies intent and never the product, and a changed default beats a designed lifecycle.
## Migrations are for schema, not data
**Migrations describe structure. Anything that determines row *values* belongs in application code.**
Structural changes — `ADD`/`DROP TABLE`/`COLUMN`/`INDEX`, column type changes — go in migrations. Column `DEFAULT`s, backfills, and data transformations do not, even though the tooling will happily let you write them there.
**Why:**
- A default set in application code is visible to whoever reads the model. A default buried in migration history is not.
- Mixing data changes into migrations conflates *what the schema is* with *what the rollout history was*, which makes "is this environment consistent with this code" much harder to answer.
- A column `DEFAULT` shapes every future insert. That is data behavior wearing schema clothing.
**How to apply.** Need a default? Set it as a field initializer on the model, where a reader will find it:
```ts
@Column({ name: 'retry_mode', default: 'never' })
retryMode: RetryMode = 'never'; // ← the initializer is what actually populates new rows
```
The column option becomes documentary; the initializer is what runs on `new Model()` → `save()`. Need a backfill? Write it as an idempotent, re-runnable service routine, not a one-shot migration — you will want to run it again.
**Write migrations in plain SQL.** The operator's ruling (2026-11-02, Reforge M2): ORM-driven migration tools such as Alembic feel productive while you pack changes together, then carry maintenance overhead (his words: "It seems good at first as you are packing stuff together, but there is maintenance overhead"; "pure SQL is golden"). Pure SQL migrations, Flyway-style, are the default for new work. On a client already running an ORM tool, stay on it and raise the switch as a future consideration at a natural break, never mid-milestone (Reforge keeps Alembic through M2).
## The client supplies intent, the server supplies the product
**Never let a client tell the server what something costs, what it is, or what it is worth.** The client sends a small, closed-vocabulary *intent*; the server resolves that into the real object from its own source of truth.
The failure shape is a checkout flow that accepts a price, product id, or amount from the browser. It is trivially tampered with, and it usually ships because the client "already knows" the number it just displayed.
**How to apply.** Constrain the input to an enum the server fully controls, then look everything else up:
```ts
// client sends: { tier: "pro" } ← intent, closed set
// server does: lookupPlan("pro") ← canonical price, id, entitlements
// client NEVER sends: price, amount, productId, currency
```
Displaying a price is not the same as authorizing one. The same rule covers entitlements, discounts, and anything else the client can see but must not decide.
## A changed default beats a designed lifecycle
**When asked to fix a problem, audit the controls that already exist before designing new machinery.** Flipping a default is almost always better than introducing a lifecycle, a phase, or a mode.
The failure shape: a problem arrives, and the response is a multi-phase feature with options, states, and a rollout plan — when a one-line change to an existing default would have resolved it. Phased designs feel rigorous and are mostly deferred work that never arrives.
**How to apply.** In order:
1. List the controls that already exist in this area. Is one of them simply set wrong?
2. If yes, change it. That is the whole fix.
3. Only if no existing control can express the desired behavior, add one — the smallest one.
4. Treat "Phase 2 / Phase 3" in your own proposal as a signal you are designing past the actual ask. Cut it.
The bias holds hardest under launch pressure, which is exactly when elaborate designs are most tempting and least affordable.
*When code, scripts or conventions here are what feels rough — burning a session, doing two jobs, or asking a reader to remember what a check could enforce — groom them: `@protocol/grooming` is the general entry point.*
## One codebase; a pod's role is a flag
**A backend is small enough that workers are not a separate codebase.** Every pod runs the same image and code. A flag (e.g. `APP_ROLE=api|worker|all`, default `all`) decides what each pod does: serve requests, run the scheduler, work the queue.
**Why:**
- Staging and local run `all`, so one pod exercises every code path. A specialized worker repo or entrypoint is code that only runs in prod.
- Scaling becomes a Deployment's replica count, not a new service: for example two pods serving the API and five crawling during a burst, each Deployment with its own resources.
**How to apply.** Gate behaviour on the role flag at startup, never by forking code. Make background work claimable per unit (`FOR UPDATE SKIP LOCKED` plus a lease that expires) so any number of worker pods can share it, including zero extra pods. Build the flag and the claim when they're cheap. Add the extra pods only when load asks for them.
## Small transactions; a collision is a retry
**Every write is a small transaction, and a collision is an ordinary retry.** On a busy database, deadlocks and serialization failures (`40P01`, `40001`) are par for the course. They are not a reason to cut concurrency.
**How to apply.**
- Never hold a transaction across a network call, a whole job, or a crawl. One batch, one transaction.
- Take row locks in a stable order (sort by key before a multi-row upsert), so two writers rarely collide.
- Wrap the transaction boundary in one shared helper that retries `40P01` and `40001` with jittered backoff, a few attempts, then fails only that unit.
- Once writes are small and retried, throughput should be bounded by the real external limit (a rate limiter, a pool), not by the database or memory. If it isn't, find out why before turning concurrency down.
## Compose existing pieces before adding schema
When a design proposes a feature, first try to build it from pieces that already exist (throwaway repos, copies of entries, scoped or read-only tokens) before adding tables, columns or a new code path. Lead the design with the change count: new tables, new columns, new endpoints, and lines touched in core read paths. The target is zero, and each item above zero needs its own justification. Design for the core use case only. A list of nice-to-haves (drift reports, delete guards, unifying for its own sake) is cost, not thoroughness.
**Why:** the operator ruled this on 2026-11-15. A design for version-pinned protocol sets came back with two tables, a token column, a guard layer and eight questions. The same goal (reproducible eval reads) fits a throwaway repo holding copies, read through an existing repo-pinned token, with no schema change. Every column added to core code makes it harder to read, for every later reader.
---
*Related: `@content/web/github/2026-11-29-convex-backend-skill-issue-64` · `@content/youtube/tiago-forte/i-connected-claude-to-everything-then-i-saw-the-gap` · `@content/youtube/indy-dev-dan/mac-mini-agents-openclaw-is-a-nightmare-use-these-skills-instead` · `@content/youtube/cole-medin/watch-this-if-your-coding-agent-is-ignoring-your-rules-you-need-hooks` · `@content/youtube/aie/the-one-thing-every-ai-agent-framework-agrees-on-mislav-ivanda-daytona`*
---
*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*
*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*