# Move a local ledger's authority to hosting

The ledger keeps its identity, records, contracts and original acceptance facts.
After an explicit handoff the hosted service accepts new records, and your local
copy remains a replica. Merely configuring a connection or publishing history does
not transfer authority.

This source implementation supports local authority → Veylar-hosted or
customer-hosted authority. Reverse transfers and transfers between hosted
services are not supported. Existing release packages have not been qualified
for this change.

## Start and resume

Configure and authenticate a named connection using the connection/identity
operations. Publish the ledger to a replica at that connection first; see
[replica synchronization](/docs/raw/manuals/ledger-replicas.md). Then use the same capability
through SDK, CLI, MCP or HTTP:

```python
from op_sys import OpSys

local = OpSys(connection="local")
ledger_id = "ledger_..."
admin = local.identity.associate(connection="work")["hosted"]
ledger = local.ledgers.get(ledger_id=ledger_id)

result = local.authority.transfer(
    ledger_id=ledger_id,
    transfer_id="move-to-work-001",  # retain this ID for every retry
    destination="work",
    destination_admin=admin,
    expected_revision=ledger["revision"],
)
# If next_action is resume, call this again within your own progress budget:
result = local.authority.resume(
    ledger_id=ledger_id, transfer_id="move-to-work-001", max_items=32,
)
```

```sh
op-sys --connection local authority status --ledger-id <ledger-id> --transfer-id move-to-work-001
op-sys --connection local authority resume --ledger-id <ledger-id> --transfer-id move-to-work-001
```

MCP exposes `op_sys_authority_transfer`, `op_sys_authority_status` and
`op_sys_authority_resume`. Their arguments and results follow the same catalog.
A call advances at most `max_items` history items (default 32, maximum 100), in
chunks of at most 16 and an 8 MiB encoded batch budget. A file item carries at
most 1 MiB. Private acknowledged-prefix checkpoints avoid rereading earlier
file ranges during an ordinary resume. Computing the frozen
manifest inspects the retained history and files; its time depends on ledger size.

Status is read-only and never releases a fence. Local status reports the retained
observation; hosted status checks the destination directly. Resume resolves an
interrupted operation, including a lost response after activation.

## Authority and grants

The source requires `authority.transfer` and permission to read the records,
contracts, files and access policy needed for the complete handoff. The destination
connection must authenticate as the explicitly selected administrator and have
creation authority. Its publisher identity must match the existing replica.

The source principal is explicitly associated with this verified administrator.
Other local grants appear in `unresolved_grants`. Resolve each deliberately:

```python
local.authority.resume(
    ledger_id=ledger_id, transfer_id="move-to-work-001",
    grant_mappings=[{"source": original_local_principal, "target": None}],
)
```

`target=None` drops that grant at the destination. A non-null target must be the
principal verified by the destination connection. This first handoff supports
mapping to that verified administrator, not guessing or verifying other accounts
from their names or emails. Grant other participants access afterwards through
`access.update`. Already resolved mappings cannot silently change during a retry.
Records retain their original principal and `trust="local"`; transferring
authority does not retroactively authenticate local acceptance.

Preparation leaves record contributions possible, but holds the selected
governance revision stable. Once grant resolution and destination control are
established, the source fences all new authoritative mutations and fixes the final
history boundary. It checks and transfers retained definitions, contracts, records,
required file bytes, removals and retry reservations. Erased files require only
their tombstone. Partial uploads are retained as partial. Accepted record
attachments require complete or explicitly erased bytes. Missing attachment bytes require
repair before handoff.

The destination remains a replica until the complete manifest and fence
attestation match. Activation advances the authority epoch once. An interruption
never grants authority back to the source, and there is no automatic unfence timer.
The authenticated publisher attests local provenance; this is not proof against
an owner modifying their own database or replacing the software.

## After the handoff

Ordinary append from the retained copy uses the verified hosted connection.
Retained reads still read the local replica; synchronize to refresh it, or select
the hosted connection for current reads. Resume first if activation succeeded but
local completion failed. Do not create another transfer to recover one already
in progress.

A retry of a pre-transfer accepted operation returns its original acceptance,
including an originally selected default contract that has since been retired.
Current outcome permissions still apply. Operation identities for erased records
remain reserved. New records are authenticated hosted acceptances.

The handoff does not relabel pending contributions or change their bound account.
An account or endpoint change fails rather than draining those attempts under a
new identity. Old read continuations require a new snapshot after the authority
change. Copying a source database creates a recovery replica, not another authority.

## Direct HTTP exchange

The public API exposes the same transfer, status and resume operations at
`/v1/ledgers/{ledger_id}/authority/transfers/{transfer_id}` and its `/resume`
subpath. Custom clients supply the documented `offer`, explicit grant mappings,
frozen `barrier`, and bounded `chunk` values in the canonical schema. The supplied
CLI/SDK/MCP build these exchange values from local storage.

The barrier binds the source position, item count and a rolling SHA-256 digest.
Start with the canonical hash of `[]`; for each ordered item hash
`[previous_digest, item]`. The order is contracts, definitions, file chunks,
minimal erased-operation reservations, retained records in acceptance-position
order, then removal facts. Each record includes its original retry binding and
resolved contract. Resume checks offset and prior digest transactionally. Repeating
the latest identical chunk is safe; inspect status before retrying an older chunk.
The final activation is atomic. No local HTTP daemon is needed.
