# Rules

A decision table is a record type with kind `decision_table`. Each row is one
version of a rule: input cells with conditions and output cells with values.
`rules.evaluate` finds the rules that match an input on a key date. A release
freezes the rows after their tests pass, so later readers can rely on exactly
that state.

## Reserved fields

Creating the type with `kind: "decision_table"` adds these fields. They are
locked: their key, type and settings cannot change.

| Field | Type | Meaning |
|---|---|---|
| `rule_key` | text, required | Stable name of the rule across its versions. |
| `valid_from` | date, required | First day the version applies. |
| `valid_to` | date | First day it no longer applies; empty means open-ended. |
| `priority` | number | Higher wins; empty counts as 0. |
| `enabled` | boolean | `false` switches the version off; empty counts as on. |
| `supersedes` | text | Comma-separated rule keys this rule replaces for the inputs it matches. |
| `source` | text | Where the rule comes from, free text. |

`(rule_key, valid_from)` is unique. Add your own text fields for inputs and
any simple field (text, number, money, date, boolean, select) for outputs.

## kind_config

Set it with records.types.update and publish the type; rules always read the
published schema.

```json
{
  "hit_policy": "first",
  "inputs": [
    {"field": "region", "type": "text"},
    {"field": "weight", "type": "number"}
  ],
  "outputs": [{"field": "rate"}],
  "tests": [
    {
      "name": "heavy parcel north",
      "at": "2026-10-01",
      "input": {"region": "north", "weight": "12"},
      "expect": [{"rule_key": "north_heavy", "outputs": {"rate": "18"}}],
      "forbid": ["north_light"]
    }
  ]
}
```

- `inputs`: text fields holding conditions. `type` says how the input value is
  compared: `text`, `number`, `date` or `boolean`.
- `outputs`: fields returned for a match, in their field format.
- `tests`: the cases a release must pass (see Tests).

## Conditions in input cells

| Cell | Matches |
|---|---|
| `-` or empty | any value |
| `"north"`, `12`, `2026-01-01`, `true` | equal value |
| `"north","south"` | any value in the list |
| `< 10`, `<= 10`, `> 10`, `>= 10` | comparison (number and date) |
| `[5..10)` | range; `[`/`]` include the bound, `(`/`)` exclude it |
| `not("north","south")` | anything except the listed values |

Quote text that contains spaces, commas or brackets. A cell that does not parse is reported by rules.test
as `cell_invalid` with the position.

## Evaluation

`rules.evaluate` with `record_type_id`, `input` (a value per input field) and
`at` (the key date):

1. Versions with `valid_from <= at < valid_to` and `enabled` take part.
2. A taking-part row matches when all its input cells match.
3. A matching row that is not itself superseded removes the matching rows it
   names in `supersedes`; their keys are listed in `superseded`. A superseded
   row removes nothing: with `a` superseding `b` and `b` superseding `c`, all
   three matching, `a` and `c` remain. This happens before the hit policy.
4. `first`: the highest priority wins, ties by `rule_key`; two matches with the
   same top priority fail with `ambiguous_match`. `collect`: every match,
   sorted the same way.

Example: `standard` (`region` `-`, rate 10) applies everywhere;
`partner_north` (`region` `"north"`, rate 7, `supersedes` `standard`) replaces
it only for `north`. Input `north` returns `partner_north` alone, input
`south` returns `standard`, even with `collect`.

No match returns an empty `matches` list; the caller decides whether that is
an error. Without `release_id` the current rows are read (`source.kind:
"working"`); with `release_id` the frozen release.

## Tests

Each test sets `at` and `input`. `expect` lists exactly the matches in order
with the outputs to compare; `"expect": []` requires that nothing matches.
`forbid` lists rule keys that must not match. Leave `expect` out to check only
`forbid`, other matches are then allowed; a test needs `expect` or `forbid`.

```json
{"name": "south never gets the partner rate", "at": "2026-10-01",
 "input": {"region": "south", "weight": "3"}, "forbid": ["partner_north"]}
```

`rules.test` runs all tests against the current rows and also checks the table: `valid_to` after `valid_from`, no
overlapping versions of a rule, known `supersedes` keys without cycles, every
cell parses. It returns `passed`, per-test `results`, `issues` and the
`content_hash` of the state it checked.

## Releases

1. `rules.test` until `passed` is true and `issues` is empty.
2. `rules.release` with `expected_content_hash` (from step 1) and an optional
   `note`. It needs space administration rights. In the chat it runs only
   after a person confirms it; the confirmation is bound to that content hash.
3. The release gets the next `number` and keeps the rows, the schema version
   and the test report. Releases never change and cannot be deleted.

If nothing changed since the latest release, the call returns that release
with `created: false`. `rules.releases.list` lists releases newest first;
`rules.releases.get` returns one with `snapshot` and `test_report`.

A decision table with releases cannot be archived or deleted on its own.

## Diff

`rules.diff` compares two states by `(rule_key, valid_from)`: `base` and
`target` are each `"working"` or a release id. By default base is the latest
release and target the working rows. The answer lists `added`, `removed`,
`changed` (with before and after), `config_changed` for inputs, outputs or hit
policy, and `tests_changed` by test name.

## Errors

| Code | Next step |
|---|---|
| `not_decision_table` | Use a type with kind `decision_table`. |
| `record_type_not_published` | Publish the type first. |
| `input_missing` | Pass a value for every field in `details.inputs`. |
| `input_invalid` | Send the value in the input's `type`. |
| `ambiguous_match` | Change priorities or conditions of `details.rule_keys`. |
| `table_invalid` | Fix the rows in `details.issues` (or archive rows if the table is too large). |
| `unary_syntax` | Fix the cell at the reported position. |
| `release_stale` | Rows changed after the test: run rules.test again and use its hash. |
| `release_tests_failed` | Add tests or fix the failing ones in `details.report`. |
| `release_not_found` | The release id does not belong to this table. |
