# `LlmCall.PoolRegistry`
[🔗](https://github.com/gilbertwong96/llm_call/blob/main/lib/llm_call/pool_registry.ex#L1)

Records which `pool:` options configured each origin, and reports when a declaration did
not take effect.

## The rule

A record exists only if this library configured the pool that is running now.

`Finch.start_pool/3` returns `:ok` without touching an origin that already has a pool, so
writing a record after calling it says nothing about which options are in force. Every
function here keeps that distinction: the observation (`pool?`) and the record are read
together, and a record is only written when this declaration is the one that started the
pool.

## Why it is its own module

The rule was spread across `LlmCall.Provider` (start, claim, warn) and `LlmCall.Protocol`
(forget), and each addition to it interacted with the previous one: `LlmCall.Protocol.downgrade/2`
forgetting a record reopened a window that the claim-first ordering had opened, and neither
change was wrong on its own. `decide/2` is a pure function over the six states that can
occur, so the answers are enumerable and testable without a running Finch instance.

## Not public

`LlmCall.Provider` uses `register/3`; `LlmCall.Protocol` uses `forget/2`. Nothing else
should touch the table.

# `decision`

```elixir
@type decision() :: :silent | {:start, :report | :quiet} | {:warn, :once | :always}
```

What to do about it.

  * `:silent` — nothing to do and nothing to say.
  * `{:start, :report}` — the pool is absent and this declaration may start it; say that it
    did not configure the pool only if a *different* declaration had claimed it.
  * `{:start, :quiet}` — as above, first claim, so nothing to report.
  * `{:warn, :once}` — the pool exists and was not configured here; say so once per origin.
  * `{:warn, :always}` — the pool exists and a different declaration configured it.

# `observation`

```elixir
@type observation() :: %{pool?: boolean(), record: map() | nil}
```

What is known about one origin, before deciding anything.

`pool?` is whether Finch has an entry; `record` is what this library wrote, or `nil`.

# `decide`

```elixir
@spec decide(observation(), map()) :: decision()
```

The decision for one observation and one declaration. Pure.

# `forget`

```elixir
@spec forget(atom(), Finch.Pool.t()) :: :ok
```

Clears what is recorded for `origin`, because the pool that is running is no longer this
library's declaration — `LlmCall.Protocol.downgrade/2` replaced it.

# `put_record`

```elixir
@spec put_record(atom(), Finch.Pool.t(), map()) :: :ok
```

Seeds a record without starting a pool, so a test can set up the state it wants to reason
about. Not for library code: the only writers are `register/3` and `forget/2`.

# `recorded`

```elixir
@spec recorded(atom(), Finch.Pool.t()) :: map() | nil
```

The record for `origin`, or `nil`. Used by tests to assert the rule without reaching into
the table.

# `register`

```elixir
@spec register(atom(), Finch.Pool.t(), map()) :: :ok
```

Registers `opts` for `origin` on `instance`, returning the decision it made.

`opts` is the normalized declaration. `pool?` is passed in because Finch is the authority
and a fake would let the two disagree.

# `table`

```elixir
@spec table() :: atom()
```

The ETS table. The application creates it at boot.

---

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