Facet
DeutschInstall
Work with AI agents

Keep your agent from undoing a schema decision

Link the requirement and decision behind a table to its migration, so your coding agent knows them before it edits the schema.

  • 5 min read
  • 7 steps
  • Claude CodeCodexMCP serverMCP is the standard way AI assistants call outside tools. FACET runs an MCP server from your project with facet mcp, so an assistant can look up records and the code anchored to them.In the glossary
  • Your coding agent

The problem: Your agent cleans up the invoices table and replaces five copied address columns with one billing_address_id. But the copy was deliberate: an issued invoice may not change. Now every old invoice shows the customer's new address.

How FACET helps: A FACET recordA FACET record is a short, checkable description of one feature, kept as a YAML file next to the code. It states why the feature exists, the behaviour it promises as scenarios, and the tests that check it.In the glossary holds the requirement and links the decision. An AnchorAn anchor is a comment in the source code, starting with @facet:, that names the record the code belongs to. FACET reads these comments to know where each feature is implemented.In the glossary ties it to the migration. The agent plugin shows the agent the requirement when it opens the file, and stops it for a review if it changes the code anyway.

Example: the shop from the FACET examples, as One answer instead of three leaves it:

sh
  • git clone --depth 1 --branch guide/write-down-what-a-feature-does https://github.com/facet-framework/examples.git
  • cp -r examples/shop shop && cd shop
  • git init && git add -A && git commit -m "Example shop"
  • npm install

Branch guide/schema-decisions-your-agent-keeps holds the result.

Step 1Write the requirement and link the decision

Save as docs/facets/features/billing/invoicing/issue-invoice.yaml:

yaml
schema_version: "0.1"id: billing/invoicing/issue-invoicelifecycle: activeintent:  summary: An order is billed with an invoice that keeps the billing address it was issued to, because an issued invoice may not change.  decision_records:    - docs/adr/0004-invoice-address-snapshot.mdcontract:  scenarios:    - id: address-stays-as-issued      statement: |        Given an invoice issued to Old Street 1        When the customer changes the billing address to New Street 2        Then the invoice still shows Old Street 1    - id: new-invoice-uses-current-address      statement: |        Given a customer who changed the billing address to New Street 2        When the next invoice is issued        Then that invoice shows New Street 2verification:  unit_tests:    - id: tests/invoice.test.ts      proves: [address-stays-as-issued, new-invoice-uses-current-address]lineage:  owner:    team: billing  created_at: "2026-11-23"  related_data_entities:    - data/billing/invoice
  • intent.summary: the promise, the first line the agent sees.
  • contract.scenarios: the requirement as testable behavior.
  • intent.decision_records: the link to the ADR that explains the copy.

Step 2Describe the table as a data entity

Optional. Save as docs/facets/data-entities/billing/invoice.yaml:

yaml
schema_version: "0.1"id: data/billing/invoicecanonical_schema:  fields:    - { name: customer_id, type: uuid, references: data/customers/customer }    - { name: billing_street, type: string, constraints: [copied-at-issue, immutable] }    - { name: billing_city, type: string, constraints: [copied-at-issue, immutable] }retention:  policy: Retain for 10 years after issue, unchanged  duration: P10Yownership: { team: billing }read_by:  - billing/invoicing/issue-invoicemutated_by:  - billing/invoicing/issue-invoice

The feature record points here via lineage.related_data_entities.

Step 3Anchor the record in the migration and the code

At the top of db/migrations/0007_invoices.sql:

sql
-- @facet: billing/invoicing/issue-invoice-- @facet-role: primaryCREATE TABLE invoices (  id                  UUID PRIMARY KEY,  customer_id         UUID NOT NULL REFERENCES customers(id),  billing_name        TEXT NOT NULL,  billing_street      TEXT NOT NULL,  billing_postal_code TEXT NOT NULL,  billing_city        TEXT NOT NULL,  billing_country     CHAR(2) NOT NULL  -- …);

The same two lines go above issueInvoice in src/billing/invoice.ts. Then:

sh
  • npx facet lint
text
3 record(s), 3 anchor(s) checked — 0 error(s), 1 warning(s) (unclaimed-tests 1)

Step 4Commit the records with the schema

sh
  • git add docs db/migrations src/billing/invoice.ts
  • git commit -m "Record why invoices copy the billing address"

DriftDrift is when code changes and the record describing it does not follow, so the two no longer match. facet drift lists the features whose anchored code changed since a chosen baseline.In the glossary compares against the last commit, so commit before you hand the table to the agent.

Step 5See what the agent is given when it opens the migration

Ask the agent to normalize the table. When it reads the migration, the plugin adds:

text
FACET claims for db/migrations/0007_invoices.sql, before editing:billing/invoicing/issue-invoice  promises: An order is billed with an invoice that keeps the billing address it was issued to, because an issued invoice may not change.  contract: address-stays-as-issued, new-invoice-uses-current-address

The full record, including the ADR path, is one read_facet call away.

Step 6If the agent changes it anyway, it has to review the record

If the agent changes issueInvoice anyway, the plugin stops it before it finishes:

text
FACET: source implementing these features changed, but their records did not:- billing/invoicing/issue-invoice — changed: src/billing/invoice.ts

It must undo the change or update the record and say why. You see the same with npx facet drift.

Step 7Anchor every migration that changes the table

Drift only sees anchored files. A new migration without an anchor reports No drift found. So anchor each one:

sql
-- @facet: billing/invoicing/issue-invoice-- @facet-role: relatedALTER TABLE invoices ADD COLUMN billing_address_id UUID REFERENCES customer_addresses(id);

Sources