# Conditional acceptance

Configure ledger rules, submit ordinary records and resolve state conflicts.

Canonical page: /docs/state-rules

## Choose when prior state matters

An ordinary record contract validates each submitted payload. State rules additionally compare a submission with earlier accepted records for the same consumer-defined key. For example, accept an update only if its expected version still matches, or permit only specified state transitions.

These rules are optional. Independent observations need no state tracking. Veylar does not infer your workflow or run arbitrary consumer code.


## Configure the ledger

Use ledgers.update with changes.state_rules and the expected ledger revision. Each rule names its exact contract and the payload fields for key, state and expected version, plus optional transitions. No view ID, handler or rebuild operation is required.

Configuration requires contracts.manage and records.read. Supply the complete rule list. Existing rules are immutable; preserve them and use a new contract version for different rules. The authority validates existing history before committing all requested changes.

```python
client.ledgers.update(
    ledger_id=ledger["ledger_id"],
    expected_revision=ledger["revision"],
    changes={"state_rules": [{
        "contract": ledger["default_contract"],
        "key_field": "item", "state_field": "value",
        "version_field": "expected", "transitions": [],
    }]},
)
```

- [Configure a ledger](/docs/reference/op_sys_ledgers_update)

## Append an ordinary record

Supply the configured fields in your payload. An unseen key starts at version zero. The service atomically checks permissions, the record contract, the expected version and permitted transitions. Concurrent submissions cannot both advance the same version.

The record and its acceptance follow the ordinary append and retry contract. A state value replaces the entire previous value; it is not an automatic merge.

```python
client.records.append(
    ledger_id=ledger["ledger_id"],
    payload={"item": "example", "value": {"message": "Hello"}, "expected": 0},
    operation_id="first-change",
)
```


## Understand rejection and recover

Read records.state with the ledger, contract reference and key for the current version, state and originating record. A state_conflict means your expected version is stale; read and reconsider before submitting revised intent with a new operation ID. A state_transition_invalid error means the configured rule does not permit the requested transition.

For an uncertain write, use records.outcome and preserve its original operation ID and input. Do not assume it was rejected. State reads require an authoritative connection and records.read; they never substitute stale local state.

The current profile bounds reconstruction to 10,000 retained records and 16 MiB per ledger. Missing or erased history cannot silently yield partial current state.

- [Read current state](/docs/reference/op_sys_records_state)
- [Complete examples and transition rules](/docs/raw/manuals/record-views.md)

## Related guides

- [Contracts & rules](/docs/raw/contracts.md)
- [Ledgers & records](/docs/raw/ledgers.md)
- [API conventions](/docs/raw/api-conventions.md)
