# 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.

Source: https://facet-framework.com/en/guides/schema-decisions-your-agent-keeps/  
Last updated: 2026-11-23T17:33:23+01:00

**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 record holds the requirement and links the decision. An anchor 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](https://github.com/facet-framework/examples/tree/main/shop), as [One answer instead of three](https://facet-framework.com/en/guides/write-down-what-a-feature-does/) 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.

## Write 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-invoice
lifecycle: active
intent:
  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.md
contract:
  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 2
verification:
  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.

## Describe the table as a data entity

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

```yaml
schema_version: "0.1"
id: data/billing/invoice
canonical_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: P10Y
ownership: { team: billing }
read_by:
  - billing/invoicing/issue-invoice
mutated_by:
  - billing/invoicing/issue-invoice
```

The feature record points here via `lineage.related_data_entities`.

## Anchor 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: primary
CREATE 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)
```

## Commit 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"
```

Drift compares against the last commit, so commit before you hand the table to the agent.

## See 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.

## If 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`.

## Anchor 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: related
ALTER TABLE invoices ADD COLUMN billing_address_id UUID REFERENCES customer_addresses(id);
```

> **What this does not do**
>
> The plugin shows the summary and scenario ids, not the ADR text. `facet lint` does not check that `decision_records`
> or `related_data_entities` exist. Drift asks for a review; it never fails a build.

### Sources

- [FACET Core on GitHub](https://github.com/facet-framework/core)
- [How to write a record](https://github.com/facet-framework/core/blob/main/docs/AUTHORING.md)
- [Record schema](https://facet-framework.com/en/docs/schema/)
