# Clone and synchronize ledgers

Clone and synchronization are implemented in the source checkout. It is not a released installer or a
production deployment. Direct hosted access still needs no replica: select a
connection and use the ordinary ledger operations.

A replica retains authorized accepted history. It preserves ledger IDs, record
IDs, attachments and the original acceptance, including its principal, authority,
epoch, position and trust. Receiving history does not append it again or validate
it under today's admission rules. Synchronization alone never transfers authority. `ledgers publish` explicitly
composes synchronization and authority transfer in its default shared mode.

The ledger remains the public resource. Clone retains the same identity and history;
it does not create an independently governed fork. Direct hosted use needs no clone.

## Publish for shared contributions

Run `op-sys ledgers publish --endpoint https://records.example.com` for your
operator-provided service, or `op-sys ledgers publish` to reuse a destination.
Publishing guides setup, remembers `origin`, moves authority to the service and
retains your local copy. It selects local work after completion. Repeat bounded
calls until `state=published`; sign-in and unresolved grants may need input.
See the [local-to-shared quickstart](/docs/raw/manuals/ledger-quickstart.md).

The returned `url` is usable by another authorized participant:

```sh
op-sys ledgers clone "<ledger-url>"
op-sys records list
op-sys ledgers sync
```

URL cloning configures the destination and selects the local copy automatically.
No secret is included in the URL; recipients authenticate with their own identity.
Ambiguous accounts and conflicting organization or remote selections fail with
instructions to choose explicitly. Clone is not an invitation and grants no rights.

## Clone with an explicit existing connection

Configure and authenticate a named connection using the
[connection guide](/docs/raw/manuals/client-connections.md), then:

```sh
op-sys ledgers clone --connection work --ledger-id <ledger-id>
op-sys --connection local records list --ledger-id <ledger-id>
op-sys ledgers status --ledger-id <ledger-id>
op-sys ledgers sync --ledger-id <ledger-id>
```

The source requires `ledger.read`, `records.read` and `changes.read`. Contract,
policy and file access follow their own permissions. Clone creates an `origin`
remote if none exists; it does not change the selected connection. `--connection
local` reads the retained copy. Select `work` for a live hosted read. A local
replica requested with `freshness=current` fails `authority_unavailable`; it never
silently substitutes cached data or switches destinations.

```python
from op_sys import OpSys

client = OpSys()
progress = client.ledgers.clone(connection="work", ledger_id=ledger_id)
progress = client.ledgers.sync(ledger_id=ledger_id)
status = client.ledgers.status(ledger_id=ledger_id)

local = OpSys(connection="local")
records = local.records.list(ledger_id=ledger_id)
```

MCP exposes `op_sys_ledgers_clone`, `op_sys_ledgers_sync` and
`op_sys_ledgers_status` with the same arguments/results. All ordinary retained
reads use the existing ledger, record, contract and file capabilities.

## Advanced: retain local authority

```sh
op-sys ledgers publish --ledger-id <ledger-id> --connection work --mode copy
op-sys --connection work records list --ledger-id <ledger-id>
```

The connection must authenticate a principal permitted to create a hosted ledger.
The destination must be empty or an existing replica bound to that publisher.
The local ledger remains authoritative. Hosted `records.append` is fenced and its
`records.outcome` cannot resolve a source-side append. Inspect the original local
authority for those outcomes. Local acceptance remains `trust=local` after upload;
a publisher cannot assert new authenticated hosted acceptance.

A context binds one logical ledger, source/destination and verified principal.
`--context-id` (default `default`) selects saved progress. Use a separate context
for publication to a different configured remote. Synchronization cannot rename a
ledger into a different remote ledger ID or replace another authority. Changing
connection credentials or endpoints requires deliberate context reconciliation;
a changed identity is not silently treated as the previous one.

## Progress, files and interruption

Synchronization is explicit. Connecting, status, ordinary reads and restored
connectivity do not trigger it. A call processes at most `max_pages` history pages
(default 10, maximum 100) and `max_file_chunks` byte chunks (default 16, maximum
1,024). File availability checks consider at most the larger of 16 and the chunk
budget in distinct file IDs per call; cached files do not cause an unbounded network scan. Each
history page is independently bounded by the read contract. Re-run
`ledgers sync` to continue. The client persists protocol checkpoints; consumers do
not assemble batches or supply sequence numbers.

The shared result reports:

- `state`: `snapshot`, `changes`, `files`, `caught_up` or recovery state.
- `direction`: `pull` from authority or `push` to hosted replica.
- `records_retained`, `pages_processed`, `files_remaining` and `next_action`.
- `remaining`: `null` while the history backlog is unknown, zero once traversed.
- `observation`: the source observation time, authority epoch and coverage.

`caught_up` describes the last observed source boundary, not future writes. Record
acceptance and byte availability remain separate. The default `files=referenced`
hydrates referenced files when authorized; `files=none` retains records without
fetching bytes. `files_remaining` can be nonzero when hydration was deliberately
omitted. A known erased file is terminal unavailability, not an endlessly retried
transfer. File reads expose missing, partial, available or erased availability.

An interrupted snapshot stays private until complete. An existing active snapshot
remains readable during its replacement. Applying a page and recording its
checkpoint is atomic. Lost responses can cause replay or a new snapshot; neither
creates new acceptance. Incremental changes include permitted definitions and
removals. Expired or incompatible continuations restart a replacement snapshot
within the caller's page budget. Status never performs delivery.

A replacement snapshot evicts unverified cached bytes and rehydrates its referenced
files. This prevents a lost change interval from leaving already-erased source
files readable. Known removal reservations remain. Hosted legal holds can block
replacement/removal; resolve the hold rather than bypassing it. A current,
authorized deleted-ledger observation also reconciles an expired replica.

## Authorization and retained copies

Every source request and hosted exchange rechecks authentication and permissions.
Learned denial closes supported local replica reads. Permission changes invalidate
old continuations and may require a new authorized snapshot. `observed_at` on
retained reads describes the source observation, not the time the cache was read.

Disconnected clients cannot learn a new revocation or deletion until they contact
the source. This is not DRM: independently held files, exported data and owner-
controlled storage cannot be retroactively revoked. Local replicas do not gain a
universal `operator` bypass into the core's authorization.

## HTTP integrations

HTTP consumers retrieve source snapshots/changes through the existing operations.
The hosted receiver exposes:

- `POST /v1/ledgers/{ledger_id}/synchronization` (`ledgers.receive`): bounded `snapshot`,
  `changes` or `file` exchange, with a nullable initial `checkpoint`.
- `GET /v1/ledgers/{ledger_id}/synchronization` (`ledgers.checkpoint`): the original publisher's
  read-only destination checkpoint, useful after a lost response.

The receiver serializes publication, stages snapshots and preserves acceptance.
`restart=true` starts a replacement snapshot using its current checkpoint. A stale
checkpoint returns `revision_conflict`. The first request requires creation
permission; later exchanges remain bound to the original authenticated publisher.
The receiver accepts local provenance, not publisher-invented authenticated
acceptance. See the canonical catalog/OpenAPI for the complete request schemas.

These exchanges are also available through CLI/SDK/MCP. Publish/clone/synchronize/status
are supplied-client orchestration over those operations. A hosted HTTP endpoint
cannot inspect another device's saved progress; no local HTTP server is required.

## Pre-launch cutover

The documented ledger operations are the supported contract. Legacy replica adoption is
not a supported product workflow: clone has no `adopt_legacy` option. Old
endpoints, receipts and delivery protocols are being retired rather than carried
forward through a compatibility layer.

Ordinary core replicas, pending contributions and authority transfer remain
supported. Original acceptance and stable identities are preserved within these
current-contract workflows. Refusing to replace an unrelated existing store is
a data-integrity boundary, not a promise to support its obsolete format.

Existing development stores are not reset automatically. Use an isolated current
store for development and verification; any required handling of existing data is
separate from the supported public interface.
