# `SuperPi.Durable.Doc`
[🔗](https://github.com/oleksandr/super-pi-beam/blob/v0.1.0/lib/super_pi/durable/doc.ex#L1)

Scoped JSON document manager supporting `latest` and `rewindable` (`fork: "as_of"`) policies.

Rewindable documents store base snapshots and RFC 6902 (`Jsonpatch`) operational deltas
stamped with the commit `seq`, enabling exact historical reconstruction on conversation fork
via `snapshot_as_of/3`. Active snapshots are cached in `:ets` (`:super_pi_doc_cache`).

# `builtin_spec`

# `cache_invalidate`

Removes cache entries (another node wrote these documents).

# `ensure_ets!`

# `put_doc`

Creates or updates a scoped document at `seq` and caches the new immutable snapshot in ETS.

# `put_ets_overlay`

Writes a fast in-memory ETS overlay for deferred commits (`100ms` streaming updates).

# `snapshot`

Reads the latest snapshot of a document, checking ETS first before reconstructing from Ecto.

# `snapshot_as_of`

Reconstructs a rewindable conversation document (`pi.agent`) as of `at_seq`, walking
conversation fork ancestry when `at_seq` predates the fork point (`spec.md` §3.3).

# `snapshot_for_update`

Reads the latest value of a document from the database, never from the cache, and (on
PostgreSQL) locks its row until the transaction ends. Use it for a read-modify-write inside
`SuperPi.Durable.Storage.commit/1`: a cached copy may be older than what another node wrote.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
