# Schema-Entscheidungen für den Agenten festhalten

> Verknüpfe Anforderung und Entscheidung hinter einer Tabelle mit ihrer Migration, damit dein Coding-Agent sie kennt, bevor er das Schema ändert.

Quelle: https://facet-framework.com/de/guides/schema-decisions-your-agent-keeps/  
Zuletzt geändert: 2026-11-23T17:33:23+01:00

**Das Problem:** Dein Agent räumt die Rechnungstabelle auf und ersetzt fünf kopierte Adressspalten durch eine `billing_address_id`. Die Kopie war aber gewollt: Eine ausgestellte Rechnung darf sich nicht ändern. Jetzt zeigt jede alte Rechnung die neue Adresse des Kunden.

**Wie FACET hilft:** Ein Record hält die Anforderung fest und verweist auf die Entscheidung. Ein Anker verbindet ihn mit der Migration. Das Agent-Plugin zeigt dem Agenten die Anforderung, sobald er die Datei öffnet, und hält ihn zur Prüfung an, wenn er den Code trotzdem ändert.

**Beispiel:** der Shop aus den [FACET-Beispielen](https://github.com/facet-framework/examples/tree/main/shop), so wie ihn [Eine Antwort statt drei](https://facet-framework.com/de/guides/write-down-what-a-feature-does/) hinterlässt:

```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
```

Der Branch `guide/schema-decisions-your-agent-keeps` enthält das Ergebnis.

## Die Anforderung aufschreiben und die Entscheidung verknüpfen

Speichere als `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`: das Versprechen, die erste Zeile, die der Agent sieht.
- `contract.scenarios`: die Anforderung als prüfbares Verhalten.
- `intent.decision_records`: der Verweis auf das ADR, das die Kopie begründet.

## Die Tabelle als Datenobjekt beschreiben

Optional. Speichere als `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
```

Der Feature-Record verweist über `lineage.related_data_entities` hierher.

## Den Record in Migration und Code verankern

Oben in `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
  -- …
);
```

Dieselben zwei Zeilen kommen über `issueInvoice` in `src/billing/invoice.ts`. Dann:

```sh
npx facet lint
```

```text
3 record(s), 3 anchor(s) checked — 0 error(s), 1 warning(s) (unclaimed-tests 1)
```

## Die Records mit dem Schema committen

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

Drift vergleicht mit dem letzten Commit. Committe deshalb, bevor du dem Agenten die Tabelle überlässt.

## Sehen, was der Agent beim Öffnen der Migration bekommt

Bitte den Agenten, die Tabelle zu normalisieren. Liest er die Migration, ergänzt das Plugin:

```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
```

Den ganzen Record samt ADR-Pfad liest er mit einem `read_facet`-Aufruf.

## Ändert der Agent es trotzdem, muss er den Record prüfen

Ändert der Agent `issueInvoice` trotzdem, hält das Plugin ihn vor dem Abschluss an:

```text
FACET: source implementing these features changed, but their records did not:

- billing/invoicing/issue-invoice — changed: src/billing/invoice.ts
```

Er muss die Änderung zurücknehmen oder den Record anpassen und begründen. Dasselbe zeigt dir `npx facet drift`.

## Jede Migration verankern, die die Tabelle ändert

Drift sieht nur verankerte Dateien. Eine neue Migration ohne Anker ergibt `No drift found`. Verankere deshalb jede:

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

> **Was das nicht tut**
>
> Das Plugin zeigt Zusammenfassung und Szenario-IDs, nicht den Text des ADR. `facet lint` prüft nicht, ob
> `decision_records` oder `related_data_entities` existieren. Drift bittet um Prüfung, ein Build schlägt nie fehl.

### Quellen

- [FACET Core auf GitHub](https://github.com/facet-framework/core)
- [Einen Record schreiben](https://github.com/facet-framework/core/blob/main/docs/AUTHORING.md)
- [Record-Schema](https://facet-framework.com/de/docs/schema/)
