# Conditional record acceptance

A ledger can enforce consumer-defined state rules as part of ordinary record
acceptance. State reconstruction is internal: there is no separate public view
resource, mutation API or rebuild operation.

## Configure the rules, then append records

```python
from op_sys import OpSys

client = OpSys(connection="local")
ledger = client.ledgers.create(name="Example")
ledger_id = ledger["ledger_id"]
contract = ledger["default_contract"]
rule = {
    "contract": contract,
    "key_field": "item",
    "state_field": "value",
    "version_field": "expected",
    "transitions": [],
}
client.ledgers.update(
    ledger_id=ledger_id,
    expected_revision=ledger["revision"],
    changes={"state_rules": [rule]},
)
client.records.append(
    ledger_id=ledger_id,
    contract=contract,
    payload={"item": "example", "value": {"your_field": "your value"}, "expected": 0},
    operation_id="first-change",
)
current = client.records.state(ledger_id=ledger_id, contract=contract, key="example")
print(current["current"])  # version, state and the originating record ID
```

These field names and values belong to the consumer. Unconfigured ledgers accept
ordinary independent records under their contracts; they do not infer state or
reject records simply because their subjects overlap. State rules replace the
whole state for a key; they do not execute consumer code or merge partial values.
The exact contract reference selects a rule; no extra rule ID is needed.

Rules are configured through `ledgers.update`, CLI `ledgers update`, MCP
`op_sys_ledgers_update`, or PATCH `/v1/ledgers/{ledger_id}`. Supply the complete
`changes.state_rules` list and expected ledger revision. Configuration requires
`contracts.manage` and `records.read`, plus permissions for any other changes in
the request. Definitions are immutable per contract: retain existing rules when
adding another, and publish another contract version for different rules. There
can be at most one rule per contract and 32 rules per ledger. Existing history is
validated before the entire configuration commits. Incompatible history or an
invalid rule leaves metadata, permissions, rules and revision unchanged.

## Read state and contribute through your preferred interface

Use `records.state`, CLI `records state`, MCP `op_sys_records_state`, or GET
`/v1/ledgers/{ledger_id}/state` with `contract` and `key` to retrieve current state.
Reads require `records.read` and an authoritative connection; a local retained
copy cannot promise current authority state. The result includes an observation.
A key with no accepted state has version zero, null state and no record ID.

Write through ordinary `records.append` in every interface. Include the expected
version in the configured payload field. The authority validates the contract,
permissions, state version and permitted transitions in one transaction. Two
writers cannot advance the same key from the same version. A failed check leaves
no accepted record, state change or reserved operation ID. Retry an uncertain
write with its original operation ID and identical input; use `records.outcome`
before deciding it failed. Revised intent gets a new operation ID.

## Optional transitions

A rule can additionally restrict a top-level string field inside the state:

```json
{"field":"status","initial":["draft"],"edges":[["draft","approved"],["approved","archived"]]}
```

This example permits new keys in draft and the listed subsequent transitions.
Keeping the same status is also permitted. The states are consumer-defined,
not a built-in document or task workflow. The existing record contract still
validates the submitted payload independently of these state checks.

## Actionable rejection and bounded history

- `state_conflict`: read `records.state`, reconsider the change and submit new
  intent at the observed version. Do not blindly replace a stale version.
- `state_transition_invalid`: inspect the configured rule and current state;
  choose a permitted transition.
- `state_payload_invalid`: supply the configured key, state and nonnegative
  expected-version fields.
- `state_rule_conflict`: preserve immutable rules; use a new contract version
  for different rules.
- `state_rule_not_found`: select an exact configured contract reference.
- `record_history_unavailable`: required retained history is incomplete.

State reconstruction is bounded to 10,000 retained records and 16 MiB per ledger.
A state-history limit or erased history cannot silently yield partial state.
Rules follow the ledger through synchronization, export/import and explicit
authority transfer. Grant changes and removal safeguards continue to apply.
