LlmCall.PoolRegistry (llm_call v0.4.0)

Copy Markdown View Source

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.

Summary

Types

What to do about it.

What is known about one origin, before deciding anything.

Functions

The decision for one observation and one declaration. Pure.

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.

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.

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

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

The ETS table. The application creates it at boot.

Types

decision()

@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()

@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.

Functions

decide(map, declared)

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

The decision for one observation and one declaration. Pure.

forget(instance, origin)

@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(instance, origin, declared)

@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(instance, origin)

@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(instance, origin, opts)

@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()

@spec table() :: atom()

The ETS table. The application creates it at boot.