ExAgent.UsageLimits.Budget.Adapter behaviour (ExAgent v3.0.0)

View Source

Transactional admission and settlement for a shared monetary account.

Stability — Stable.

The handle selects the account. Each fresh attempt reserves an integer amount in microcents. A successful acknowledgement represents a committed reservation. Refuse duplicate IDs so a lost acknowledgement can never authorize a repeated dispatch. A denial's finite delay comes from the backend's clock. Use :infinity for a quote that can never fit and :unknown when renewal alone cannot be shown to free enough balance. Return {:error, :spend_budget_policy_mismatch} when the handle's policy differs from the account's active policy.

The reservation remains held when the attempt has no known terminal price. A partial price raises the hold to at least the known subtotal. A full price replaces the hold. :unused releases it only when the attempt never started. The adapter accepts retried reports idempotently. A conflict with a closed reservation leaves the applied amount intact. The runtime can include bounded attribution in the candidate :labels request field. Receipts may omit unknown labels.

Summary

Callbacks

Reads one committed balance snapshot without taking the account's admission lock. Returns amounts in cents. An unavailable snapshot returns an error.

Commits a fresh hold or reports that this attempt may not start.

Applies the evidence to an existing reservation within the relative IO budget.

Types

labels()

(candidate API)
@type labels() :: %{required(String.t()) => String.t()}

request()

@type request() :: %{
  :id => String.t(),
  :request_id => String.t(),
  :amount => pos_integer(),
  optional(:labels) => labels()
}

settlement()

@type settlement() :: :unused | {:partial | :priced, non_neg_integer()}

settlement_error()

@type settlement_error() ::
  ExAgent.UsageLimits.Budget.unavailable()
  | :result_unconfirmed
  | :budget_reservation_not_found
  | :budget_settlement_conflict

Callbacks

balance(handle, timeout_ms)

(optional)
@callback balance(handle :: term(), timeout_ms :: pos_integer()) ::
  {:ok, ExAgent.UsageLimits.Budget.balance()}
  | {:error, ExAgent.UsageLimits.Budget.unavailable()}

Reads one committed balance snapshot without taking the account's admission lock. Returns amounts in cents. An unavailable snapshot returns an error.

reserve(handle, request, timeout_ms)

@callback reserve(handle :: term(), request(), timeout_ms :: pos_integer()) ::
  :ok
  | {:deny, pos_integer() | :infinity | :unknown}
  | {:error,
     ExAgent.UsageLimits.Budget.unavailable() | :spend_budget_policy_mismatch}

Commits a fresh hold or reports that this attempt may not start.

settle(handle, t, settlement, timeout_ms)

@callback settle(handle :: term(), String.t(), settlement(), timeout_ms :: pos_integer()) ::
  :ok | {:error, settlement_error()}

Applies the evidence to an existing reservation within the relative IO budget.