# `Repo`

A class in `fastatp.repo.repo` ([source](https://github.com/jacoblapenna/fastatp/blob/main/fastatp/repo/repo.py#L131)).

```python
Repo(
    did: str,
    signing_key: PrivateKey | Signer | None,
    store: BlockStore | None = None,
    clock: TidClock | None = None,
    *,
    head_cid: CID | None = None,
    head_store: HeadStore | None = None,
    on_commit: Callable[[CommitEvent], None] | None = None,
    blobs: BlobStore | None = None,
    storage: Storage | None = None,
)
```

A DID's signed, present-state repository over a block store.

## Attributes

- `did`
- `head_store: HeadStore`
- `signer: Signer | None`
- `store: BlockStore`
- `blobs`
- `clock`
- `mst`
- `seq: int`: The last firehose seq used on this repo (0 before any).
- `frozen: bool`: True when this host holds no pen for the repo: reads and imports work, every commit is refused.
- `commit: Commit`
- `commit_cid: CID`
- `rev: str`
- `root: CID`

## Methods

### `create`

```python
async create(
    did: str,
    signing_key: PrivateKey | Signer,
    store: BlockStore | None = None,
    clock: TidClock | None = None,
    *,
    head_store: HeadStore | None = None,
    on_commit: Callable[[CommitEvent], None] | None = None,
    blobs: BlobStore | None = None,
    storage: Storage | None = None,
) -> Repo
```

A new repo with its genesis commit (a signed empty tree). With a
head store that already holds a head for `did` this fails:
`open` that one instead.

### `open`

```python
async open(
    did: str,
    signing_key: PrivateKey | Signer | None,
    store: BlockStore | None = None,
    head_store: HeadStore | None = None,
    clock: TidClock | None = None,
    *,
    on_commit: Callable[[CommitEvent], None] | None = None,
    blobs: BlobStore | None = None,
    storage: Storage | None = None,
) -> Repo
```

Load `did`'s current head from the head store (`head_store`, or
`storage.heads`), its blocks from the block store. `signing_key=None`
opens it frozen.

### `advance_seq`

```python
async advance_seq() -> int
```

Take the next firehose seq for a frame that is not a commit (an
account, identity or sync announcement): the head stays, the seq
moves, through the same swap, so the number is durable before the
frame it stamps is delivered. Returns the seq taken.

### `record_count`

```python
record_count(collection: str | None = None) -> int
```

### `list_records`

```python
list_records(collection: str) -> list[tuple[str, CID]]
```

### `get_record`

```python
get_record(collection: str, rkey: str) -> dict[str, Any] | None
```

### `put_record`

```python
async put_record(collection: str, rkey: str, record: RecordLike) -> CommitEvent | None
```

### `delete_record`

```python
async delete_record(collection: str, rkey: str) -> CommitEvent | None
```

### `recommit`

```python
async recommit() -> CommitEvent
```

Re-sign the current state under the current `signer` (no data
change). Used after a key rotation so the persisted head verifies under
the new key on the next load.

### `apply_writes`

```python
async apply_writes(writes: list[WriteOp]) -> CommitEvent | None
```

Commit `writes` as one signed change.

Writers sharing this object take turns: validation, signing, the head
update and garbage collection happen under one lock, so concurrent
writes land in sequence and each builds on the previous. A sync
`import_car` that lands while a commit is being signed is caught
after the signature and refused ([`RepoError`](https://fastatp.com/api/repoerror.md)), with nothing staged
left behind.

Across processes the head store decides: the commit lands only if the
store's head is still the one it was built on. When it is not, the
repo reloads the new head and retries the writes on it, a few times,
re-validating each time; a write the other process already made byte
for byte is dropped as done, and if nothing is left this returns
`None` (the repo already holds exactly this). A `create` whose
path now holds different bytes fails as it always has. Contention
beyond the retries surfaces as [`HeadMoved`](https://fastatp.com/api/headmoved.md).

### `add_blob`

```python
add_blob(data: bytes, mime_type: str) -> Blob
```

Store blob bytes; returns the palette `Blob` object to embed in a
record. The CID is raw codec (0x55) + sha-256 — never dag-cbor.

### `get_blob`

```python
get_blob(cid: CID) -> bytes | None
```

### `blob_info`

```python
blob_info(cid: CID) -> BlobInfo | None
```

### `list_blobs`

```python
list_blobs() -> list[BlobInfo]
```

All stored blobs, sorted by CID string (deterministic cursoring).

### `blob_refs`

```python
blob_refs() -> set[CID]
```

Blob CIDs referenced by the CURRENT records (walks every record).

### `gc_blobs`

```python
gc_blobs() -> int
```

Delete blobs no current record references; returns the count.

Deliberately explicit rather than per-commit: blob
retention is application policy, like record retention.

### `commit_car`

```python
commit_car() -> bytes
```

CAR carrying the current commit block only (the #sync event body).

### `export_car`

```python
export_car() -> bytes
```

Current-state CAR: commit root + every reachable block.

### `read_car`

```python
read_car(
    data: bytes,
    verify_key: PublicKey | None = None,
) -> tuple[Commit, dict[str, CID], dict[CID, bytes]]
```

Parse a repo CAR into (commit, key->record CID map, blocks).

Integrity comes from content addressing: every CAR block is checked
against its CID on decode, and the entry map is read by walking only
CIDs reachable from the signed commit's `data` root (missing or
substituted blocks fail the walk). Pass `verify_key` to also check
the commit signature.

### `import_car`

```python
async import_car(
    data: bytes,
    verify_key: PublicKey | None = None,
    *,
    gc: bool = True,
    newer_than: str | None = None,
) -> None
```

Replace this repo's state with a CAR's contents (checkpoint load).

`newer_than` is a rev (normally this repo's current `rev`): a CAR
whose head rev is not strictly greater raises `StaleCheckpoint`
before any state changes, so a router that re-hosts pushed checkpoints
can refuse a replayed or older copy in the same parse that verifies it.

A checkpoint load is not a commit: no `on_commit` fires (there is
no honest CommitEvent to announce — no ops, no valid `since`). A
caller that tracks a durable head pointer must therefore pass
`gc=False`, persist its pointer, and then call
`collect_garbage` — the same persist-head-before-GC ordering
`_commit_now` enforces, so a crash mid-import never leaves the
pointer naming a deleted block (see [`Identity.import_repo_car`](https://fastatp.com/api/identity.md#import_repo_car)).

### `collect_garbage`

```python
collect_garbage() -> None
```

Sweep the store: drop every block the current head does not reach.

Commits and imports already collect what they orphan, per repo. The
sweep is for a store this repo has to itself: it also removes blocks
a crash left staged, and what an `import_car(gc=False)` deferred.
Never run it over a store shared with other repos or other processes
on this repo; there, anything beyond the per-commit collection is the
database's own maintenance job.

### `prove`

```python
prove(collection: str, rkey: str) -> bytes
```

A standalone record proof: CAR of commit + MST path + record block.

Anyone holding this slice can verify — with nothing but a DID
resolver — that this exact record is present in this repo's signed
current state. This is the atproto-native "signed message": emit it
over any transport, verify with [`Peer.verify()`](https://fastatp.com/api/peer.md#verify).
