# Python SDK

CLI, SDK and MCP use the same ledger-core operations. The selected connection
chooses local or hosted execution; record methods and permissions stay the same.

```python
from op_sys import OpSys

client = OpSys()
client.start()  # Prepare and select a local ledger on first use.
inspection = client.ledgers.inspect()
accepted = client.records.append(payload={"message": "Hello"}, operation_id="first-record")
if accepted["state"] == "accepted":
    record = client.records.get(record_id=accepted["acceptance"]["record_id"])
    assert record["record"]["payload"] == {"message": "Hello"}
```

Inspect the selected ledger's admitted contracts before contributing. Records
are arbitrary consumer-defined JSON. `start()` installs a permissive contract
only when creating a new local ledger; it never changes an existing contract.
Other operations do not implicitly create ledgers.

Use `OpSys(state_root="/absolute/path")` for another local store, or
`OpSys(connection="work")` for an authenticated named connection. Constructors
resolve configuration without contacting the service. A hosted `start()` reports
saved selection without creating or uploading a ledger.

## Resources and operations

- `ledgers`: create, list, get, inspect, configure, lifecycle, export/import.
- `contracts`: publish immutable versions, list and get them. Admission changes
  are explicit ledger configuration changes.
- `records`: append, get, list, search, feed, outcome and erase. `pages()` composes
  bounded single-ledger reads; `search_pages()` and `feed_pages()` compose local
  or hosted retrieval without changing the public search/feed contracts.
- `files` and `references`: upload/download bytes, inspect availability, resolve
  authorized references. Attachment references accompany record submissions.
- `access`: inspect and update issuer-qualified ledger permissions.
- `invitations`: create a workspace membership offer with `workspace_id` and
  `email`, or a ledger access offer with `ledger_id` and `permissions`.
  Use `inspect(invitation=link)` and `accept(invitation=link)` for either.
  Use `status()` to resume an interrupted ledger invitation.
  [Ledger sharing](/docs/raw/manuals/ledger-core.md#invite-a-participant-to-a-ledger)
- `workspaces` and `memberships`: discover workspaces and manage membership.
  [Workspace onboarding](/docs/raw/manuals/team-onboarding.md)
- `ledgers.clone/sync/status`, `contributions` and `authority`: explicit synchronization, contribution
  recovery and fenced authority transfer.
- `records.state`, `collections` and `groups`: conditional state, saved selections and
  authorization in the selected local store or hosted tenant.

Record calls accept `ledger_id`, or use the saved selection. Append preserves
`None`, booleans and strings without coercion. Save `operation_id` before sending;
retry it with identical input. An uncertain response is not a rejection:
`records.outcome` reads the outcome without resubmitting.

Read pages until `next` is null. Preserve the snapshot's `changes_from` checkpoint
only after completing every page. `records.pages(max_pages=...)` may stop at its
budget before completion; inspect the last page rather than assuming completeness.

For service retrieval, `records.search_pages(max_pages=...)` yields full pages until
`next_cursor` is null or the budget is reached. `records.feed_pages(max_pages=...)`
yields full pages until `caught_up` or its budget; it does not poll indefinitely.
Both preserve empty pages, coverage and tokens, and propagate errors without
automatic restart. Save progress only after processing each page. See
[retrieval and resume examples](/docs/raw/manuals/record-collections.md#read-multiple-pages-and-resume-later).

See [core contracts](/docs/raw/manuals/ledger-core.md), [connections](/docs/raw/manuals/client-connections.md),
[files](/docs/raw/manuals/ledger-files.md), [reads](/docs/raw/manuals/ledger-reads.md), [replication](/docs/raw/manuals/ledger-replicas.md)
and [authority transfer](/docs/raw/manuals/ledger-authority.md) for complete semantics.
