# records.state

Read the current state and version for a contract-governed key from authoritative accepted history.

Capability: op_sys_records_state
Protocol: op-sys.capabilities.v1
Runtime: 0.3.0
Availability: Local or hosted ledger operations, including cross-ledger discovery, search and feed.
Optional integration: false
Canonical page: /docs/reference/op_sys_records_state

## Authority

The authenticated issuer-qualified principal must hold the requested ledger permission.

## Invocation

CLI, SDK and MCP use the same operation and arguments; where a hosted HTTP binding exists, its route carries the same domain contract. Host paths and environment references refer to the runtime host. Request template: replace placeholder IDs and supply complete contract-specific objects before use. This is not a complete runnable workflow.

### CLI

```sh
op-sys --output json tools call op_sys_records_state \
  --arguments '{"ledger_id":"<ledger_id>","contract":{"contract_id":"<contract_id>","version":"<version>","digest":"<digest>"},"key":"<key>"}'
```

### Python SDK

```python
import json
from op_sys import OpSys

client = OpSys()
arguments = json.loads("{\"ledger_id\":\"<ledger_id>\",\"contract\":{\"contract_id\":\"<contract_id>\",\"version\":\"<version>\",\"digest\":\"<digest>\"},\"key\":\"<key>\"}")
result = client.capabilities.call(
    "op_sys_records_state", arguments
)
print(result)
```

### MCP (initialized connection)

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "op_sys_records_state",
    "arguments": {
      "ledger_id": "<ledger_id>",
      "contract": {
        "contract_id": "<contract_id>",
        "version": "<version>",
        "digest": "<digest>"
      },
      "key": "<key>"
    }
  }
}
```

### HTTP

```sh
curl --fail-with-body --request GET "$VEYLAR_ENDPOINT/v1/ledgers/$LEDGER_ID/state?contract=%7B%22contract_id%22%3A%22%3Ccontract_id%3E%22%2C%22version%22%3A%22%3Cversion%3E%22%2C%22digest%22%3A%22%3Cdigest%3E%22%7D&key=%3Ckey%3E" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "X-Veylar-Tenant: $TENANT_ID"
```

Named CLI command: op-sys records state (use --help for flags).

SDK convenience: client.records.state(…). The capability adapter may wrap the result.

## Parameters

### ledger_id

Required; string. Stable ledger identity, local or hosted. A display name is not interchangeable with this ID.

minLength: 1; maxLength: 128

### key

Required; string. Consumer-defined key tracked by the configured state rule for the selected contract.

minLength: 1; maxLength: 256

### contract

Required; object. Contract body when publishing/creating; immutable contract reference when selecting an admitted contract for a write.


```json
{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "contract_id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128
    },
    "version": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128
    },
    "digest": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    }
  },
  "required": [
    "contract_id",
    "version",
    "digest"
  ]
}
```


### Complete input schema

```json
{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "ledger_id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128
    },
    "key": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256
    },
    "contract": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "contract_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "digest": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      },
      "required": [
        "contract_id",
        "version",
        "digest"
      ]
    }
  },
  "required": [
    "ledger_id",
    "contract",
    "key"
  ]
}
```

## Behavior

- Requires records.read at the authority; retained copies cannot claim current state.
- An unseen key has version zero and null state. Submit changes through records.append using the configured payload fields.
- Reconstruction is internal and fails if retained history is incomplete or exceeds supported bounds.
- Inspect the admitted contract before writing.
- Accepted records retain immutable identity and acceptance facts.
- Pending contributions are separate from accepted history.

## Response

Returns RecordState with current state, version, originating record and authority observation.

### Complete result JSON Schema

```json
{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "ledger_id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128
    },
    "contract": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "contract_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "digest": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      },
      "required": [
        "contract_id",
        "version",
        "digest"
      ]
    },
    "current": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "key": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "version": {
          "type": "integer",
          "minimum": 0,
          "maximum": 9007199254740991
        },
        "state": {},
        "record_id": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ]
        },
        "sequence": {
          "anyOf": [
            {
              "type": "integer",
              "minimum": 0,
              "maximum": 9007199254740991
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "key",
        "version",
        "state",
        "record_id",
        "sequence"
      ]
    },
    "observation": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "source": {
          "type": "string",
          "enum": [
            "authority",
            "replica"
          ]
        },
        "authority_epoch": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "observed_at": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64
        },
        "coverage": {
          "type": "string",
          "minLength": 1,
          "maxLength": 4096
        },
        "complete": {
          "type": "boolean"
        },
        "provenance": {
          "const": "imported-unverified"
        }
      },
      "required": [
        "source",
        "authority_epoch",
        "observed_at",
        "coverage",
        "complete"
      ]
    }
  },
  "required": [
    "ledger_id",
    "contract",
    "current",
    "observation"
  ]
}
```


[Runtime output field catalog](/docs/output-contracts.json) (descriptive contracts, not JSON Schema validators).

CLI: use --output json. For tools call, read the operation result at payload.tool.result; payload.tool.name identifies the capability. The outer envelope is {schema_version: "v1", payload: {tool: {name, result}}}.

Python: client.capabilities.call(name, arguments) returns the operation result. SDK exceptions preserve reason and code.

MCP: tools/call returns JSON text and structuredContent. Application failures set isError; inspect reason/code instead of matching message text.

Related resource schemas (not necessarily a complete operation response):
- [ledger-core.v1.schema.json](/docs/schemas/ledger-core.v1.schema.json)

## Errors and recovery

[Ledger limits and operation catalog](/docs/ledger-core.json)

Canonical operation: records.state

Permissions: records.read

Retry: read_only; concurrency: none

Codes: invalid_input, contract_invalid, payload_invalid, contract_not_admitted, permission_denied, not_found, revision_conflict, operation_conflict, unsupported, authority_unavailable, outcome_uncertain, outcome_expired, validation_context_unavailable, file_unavailable, integrity_failure, ledger_archived, ledger_deleted, resnapshot_required, identity_changed, authority_fenced, withdrawal_requires_resolution, limit_exceeded, rate_limited, state_conflict, state_transition_invalid, state_rule_invalid, state_rule_conflict, state_payload_invalid, state_rule_not_found, state_history_limit, record_history_unavailable, payment_required

[HTTP status mappings and schemas](/docs/ledger-core.openapi.json)

- permission_denied: Inspect the selected principal and ledger grants.
- payload_invalid: Correct the input against the admitted contract.

Family reasons are representative, not exhaustive.

A timeout is not proof that a write failed. Reuse the documented retry identity, batch, offset or mutation ID. If this operation has no documented retry identity, inspect its state before repeating it.

On authorization failure, correct the configured identity or grants; do not change the local actor to impersonate a hosted principal. On validation failure, correct the input against the published schema and selected contract.

On a revision or state conflict, retrieve current state and reconcile intent. Do not automatically overwrite a competing update. Preserve unknown additive response fields when building consumers.

## Source manuals

- [local-records](/docs/raw/manuals/local-records.md)
- [public-capabilities](/docs/raw/manuals/public-capabilities.md)
- [command-output-schema](/docs/raw/manuals/command-output-schema.md)
