# `H.Storage`
[🔗](https://github.com/parlant/harness/blob/v0.3.0/lib/h/storage.ex#L1)

Pluggable persistence boundary for a single `H` session.

Every backend (`H.Storage.Memory`, `H.Storage.File`, and later `H.Storage.SQLite`)
implements atomic multi-batch commits with per-batch `seq` revisions and
prefix-refusal semantics: if batch `k` fails a domain check, batches `0..(k - 1)`
remain durably stored and `refused` reports `{k, reason}`; any storage I/O error
returns `{:error, reason}` without advancing committed state.

# `check`

```elixir
@type check() ::
  {:unique_request_id, request_id :: String.t(), fingerprint :: String.t()}
  | {:conversation_exists, conversation_id :: id()}
```

# `commit_result`

```elixir
@type commit_result() :: %{
  stored: non_neg_integer(),
  seqs: [seq()],
  refused: nil | {non_neg_integer(), term()}
}
```

# `doc_address`

```elixir
@type doc_address() ::
  {:session, kind :: String.t()}
  | {:conversation, conversation_id :: id(), kind :: String.t()}
  | {:task, task_id :: id(), kind :: String.t()}
```

# `entry_query`

```elixir
@type entry_query() :: %{
  :conversation_id =&gt; id(),
  optional(:cutoff) =&gt; nil | id(),
  optional(:order) =&gt; :asc | :desc,
  optional(:cursor) =&gt; nil | id(),
  optional(:limit) =&gt; pos_integer()
}
```

# `id`

```elixir
@type id() :: pos_integer()
```

# `seq`

```elixir
@type seq() :: non_neg_integer()
```

# `t`

```elixir
@type t() :: {module(), state :: term()}
```

# `write`

```elixir
@type write() ::
  {:check, check()}
  | {:put_session_meta, map()}
  | {:put_conversation, map()}
  | {:append_entry, map()}
  | {:put_submission, map()}
  | {:put_task, map()}
  | {:put_doc, doc_address(), map()}
  | {:retire_doc, doc_address()}
```

# `close`

```elixir
@callback close(state :: term()) :: :ok
```

# `commit`

```elixir
@callback commit(state :: term(), batches :: [[write()]]) ::
  {:ok, commit_result(), state :: term()} | {:error, term()}
```

# `get_doc`

```elixir
@callback get_doc(state :: term(), doc_address(), opts :: keyword()) ::
  {:ok, nil | map(), state :: term()} | {:error, term()}
```

# `get_entry`

```elixir
@callback get_entry(state :: term(), entry_id :: id()) ::
  {:ok, nil | map(), state :: term()} | {:error, term()}
```

# `get_submission`

```elixir
@callback get_submission(state :: term(), request_id :: String.t()) ::
  {:ok, nil | map(), state :: term()} | {:error, term()}
```

# `list_entries`

```elixir
@callback list_entries(state :: term()) ::
  {:ok, [map()], state :: term()} | {:error, term()}
```

# `mint_id`

```elixir
@callback mint_id(state :: term()) :: {:ok, id(), state :: term()} | {:error, term()}
```

# `open`

```elixir
@callback open(keyword()) :: {:ok, state :: term()} | {:error, term()}
```

# `read_bootstrap`

```elixir
@callback read_bootstrap(state :: term()) ::
  {:ok, map(), state :: term()} | {:error, term()}
```

# `scan_entries`

```elixir
@callback scan_entries(state :: term(), entry_query()) ::
  {:ok, %{items: [map()], next_cursor: nil | id()}, state :: term()}
  | {:error, term()}
```

# `close`

```elixir
@spec close(t()) :: :ok
```

# `commit`

```elixir
@spec commit(t(), [[write()]]) :: {:ok, commit_result(), t()} | {:error, term()}
```

# `get_doc`

```elixir
@spec get_doc(t(), doc_address(), keyword()) ::
  {:ok, nil | map(), t()} | {:error, term()}
```

# `get_entry`

```elixir
@spec get_entry(t(), id()) :: {:ok, nil | map(), t()} | {:error, term()}
```

# `get_submission`

```elixir
@spec get_submission(t(), String.t()) :: {:ok, nil | map(), t()} | {:error, term()}
```

# `list_entries`

```elixir
@spec list_entries(t()) :: {:ok, [map()], t()} | {:error, term()}
```

Lists all persisted session entries across conversations in ascending ID order.

# `mint_id`

```elixir
@spec mint_id(t()) :: {:ok, id(), t()} | {:error, term()}
```

# `open`

```elixir
@spec open(module(), keyword()) :: {:ok, t()} | {:error, term()}
```

# `read_bootstrap`

```elixir
@spec read_bootstrap(t()) :: {:ok, map(), t()} | {:error, term()}
```

# `scan_entries`

```elixir
@spec scan_entries(t(), entry_query()) ::
  {:ok, %{items: [map()], next_cursor: nil | id()}, t()} | {:error, term()}
```

---

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