# `Condukt.SessionStore.ObjectStore`
[🔗](https://github.com/tuist/tuist/blob/condukt@3.0.0/condukt/lib/condukt/session_store/object_store.ex#L1)

A `Condukt.SessionStore` with a journal, on object storage such as S3 or
Tigris, that fences out a writer that lost its session.

    session_store:
      {Condukt.SessionStore.ObjectStore,
       bucket: {Condukt.SessionStore.ObjectStore.S3, endpoint: ..., bucket: ..., ...},
       key: "thread-123",
       epoch: 7}

## Options

  * `:bucket` - required, `{module, config}` for a
    `Condukt.SessionStore.ObjectStore.Bucket`.
  * `:key` - the session's key, falling back to the session's `:id`. Letters,
    digits, `.`, `_`, `:`, and `-`.
  * `:prefix` - where sessions live in the bucket, `"condukt/sessions"` by
    default.
  * `:epoch` - the writer's epoch, a non-negative integer, `0` by default.
    Whatever hands a session to a process, such as a lease, gives each new
    owner a higher epoch than the last.
  * `:encrypt` and `:decrypt` - functions from binary to binary applied to
    every object, the identity by default.

## Layout

Every object is created once and never overwritten; S3 refuses a create
whose key exists (`If-None-Match: *`), and that refusal is the only
coordination the store needs. Under `<prefix>/<key>/`:

  * `manifest/<n>` - the session's state at version `n`: the writer epoch,
    the snapshot without its messages, the segments holding the messages,
    and `wal_from`, where the journal starts. The highest `n` is current.
  * `segments/<first message>-<epoch>-<random>` - a run of messages. A save
    adds one segment with the messages after the stored ones, so a run costs
    what it added, not the whole history. A history that no longer starts
    with the stored messages, as after a compaction, or one spread over too
    many segments, is written as a single segment again.
  * `wal/<n>` - journal records, one object per append, from `wal_from` on.
    Clearing the journal moves `wal_from` past them.

## Fencing

A process writing with a higher epoch than the current manifest's first
creates the next manifest with its epoch, then creates an empty fence
object at the next free `wal/<n>`, and reads the journal only after that.
A writer with an older epoch is refused from then on:

  * its next manifest is taken, so a save or clear fails with
    `{:error, :fenced}`;
  * its next journal object is the fence, so an append fails the same way;
  * after every append and every manifest it creates, it checks that no
    manifest follows its own. That catches the cases where a newer writer
    has since cleared the journal, deleting the fence, or deleted the
    manifest the old writer's next one would have collided with, which
    would let its creates succeed where nobody reads them. A manifest that
    turns out to be superseded refuses the save or clear, which then
    deletes nothing.

A record the old writer created before the fence is read by the new writer,
so a record the old writer was told is durable is never lost. One it
created after the new writer's manifest but before the fence is read too,
though the old writer is told the append failed: the new writer then holds
a tool call's intent without its result, which a resumed run already treats
as an outcome it cannot know.

An append costs one create and one listing. A save costs a segment create,
a manifest create, and a listing.

`load/1` and `load_journal/1` take the session over first when `:epoch` is
given, as a write would, and are plain reads of the current state otherwise,
for readers that never write. A read at an epoch must not run alongside a
write at the same epoch from another process: its fence object takes the
writer's next journal position. Read before writing, or from the writer.

The writer's position (current manifest and next journal object) is cached
in the writing process, which is the session, and only by writes. A write
from a stale position is refused like a stale writer's, and the cache is
dropped after any failed write.

A writer that takes the session over deletes the manifests before its
claim, and later each one it replaced but the last two; the check above
needs only that some manifest after an old writer's own exists. Objects a fenced writer created before it found out,
the journal's fence objects until the journal is cleared, and anything a
failed delete left behind are not referenced and stay in the bucket.

---

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