# `Spectre.GAM.Kernel.Meter`
[🔗](https://github.com/elchemista/spectre/blob/0.4.0/lib/spectre/gam/kernel/meter.ex#L1)

Pure, conservative accounting for Mandate resource limits.

Every operation consumes a typed `Meter.Account` and preserves:

    ceiling = available + reserved + suspended + spent + delegated

Quantities move between buckets; they are never inferred or created. A
parent's delegated quantity becomes a child's ceiling, so delegation is
subtractive rather than a second issuance of authority.

`suspended` is never released by time or restart. It moves only through an
explicit Duty disposition or definitive late Outcome.

# `accounts`

```elixir
@type accounts() :: %{optional(String.t()) =&gt; Spectre.GAM.Kernel.Meter.Account.t()}
```

# `reason`

```elixir
@type reason() :: term()
```

# `available?`

```elixir
@spec available?(Spectre.GAM.Kernel.Meter.Account.t(), non_neg_integer()) :: boolean()
```

Returns whether quantity can currently be reserved.

# `delegate`

```elixir
@spec delegate(
  Spectre.GAM.Kernel.Meter.Account.t(),
  Spectre.GAM.Kernel.Meter.Account.t(),
  non_neg_integer()
) ::
  {:ok, Spectre.GAM.Kernel.Meter.Account.t(),
   Spectre.GAM.Kernel.Meter.Account.t()}
  | {:error, reason()}
```

Transfers quantity subtractively from a parent to a child allocation.

Both accounts must represent the same Meter. The parent moves
`available -> delegated`; the child's `ceiling` and `available` grow by the
same quantity.

# `devolve`

```elixir
@spec devolve(
  Spectre.GAM.Kernel.Meter.Account.t(),
  Spectre.GAM.Kernel.Meter.Account.t()
) ::
  {:ok, Spectre.GAM.Kernel.Meter.Account.t(),
   Spectre.GAM.Kernel.Meter.Account.t()}
  | {:error, reason()}
```

Returns all currently free child quantity to its parent.

# `devolve`

```elixir
@spec devolve(
  Spectre.GAM.Kernel.Meter.Account.t(),
  Spectre.GAM.Kernel.Meter.Account.t(),
  non_neg_integer()
) ::
  {:ok, Spectre.GAM.Kernel.Meter.Account.t(),
   Spectre.GAM.Kernel.Meter.Account.t()}
  | {:error, reason()}
```

Returns only free child quantity to its parent.

Reserved and suspended quantities remain bound to their Attempt or Duty, and
spent remains historical. Only `available` can therefore be devolved.

# `plan_reservations`

```elixir
@spec plan_reservations(Spectre.GAM.Kernel.Meter.Amounts.t(), accounts()) ::
  {:ok, Spectre.GAM.Kernel.Meter.Amounts.t()} | {:error, reason()}
```

Validates a canonical reservation request against current balances.

# `recontain_many`

```elixir
@spec recontain_many(Spectre.GAM.Kernel.Meter.Amounts.t(), accounts()) ::
  {:ok, accounts(), Spectre.GAM.Kernel.Meter.Amounts.t(),
   Spectre.GAM.Kernel.Meter.Amounts.t()}
  | {:error, reason()}
```

Conservatively takes back released quantities that remain available.

The returned maps partition every request into `recontained` and `deficits`.
A deficit is reported without inventing quantity or violating conservation.

# `release`

```elixir
@spec release(Spectre.GAM.Kernel.Meter.Account.t(), non_neg_integer()) ::
  {:ok, Spectre.GAM.Kernel.Meter.Account.t()} | {:error, reason()}
```

Returns `reserved` quantity only after definitive no-effect Evidence.

# `reserve`

```elixir
@spec reserve(Spectre.GAM.Kernel.Meter.Account.t(), non_neg_integer()) ::
  {:ok, Spectre.GAM.Kernel.Meter.Account.t()} | {:error, reason()}
```

Moves quantity from `available` to `reserved`.

# `reserve_many`

```elixir
@spec reserve_many(Spectre.GAM.Kernel.Meter.Amounts.t(), accounts()) ::
  {:ok, accounts(), Spectre.GAM.Kernel.Meter.Amounts.t()} | {:error, reason()}
```

Plans and applies several reservations as one pure transition.

# `resolve_suspended`

```elixir
@spec resolve_suspended(
  Spectre.GAM.Kernel.Meter.Account.t(),
  non_neg_integer(),
  :settle | :release
) ::
  {:ok, Spectre.GAM.Kernel.Meter.Account.t()} | {:error, reason()}
```

Resolves suspended quantity under an explicit durable disposition.

`:settle` acknowledges it as spent; `:release` makes it available again.

# `settle`

```elixir
@spec settle(Spectre.GAM.Kernel.Meter.Account.t(), non_neg_integer()) ::
  {:ok, Spectre.GAM.Kernel.Meter.Account.t()} | {:error, reason()}
```

Moves a definitive effected quantity from `reserved` to `spent`.

# `suspend`

```elixir
@spec suspend(Spectre.GAM.Kernel.Meter.Account.t(), non_neg_integer()) ::
  {:ok, Spectre.GAM.Kernel.Meter.Account.t()} | {:error, reason()}
```

Moves an ambiguous reservation from `reserved` to `suspended`.

# `validate`

```elixir
@spec validate(Spectre.GAM.Kernel.Meter.Account.t()) :: :ok | {:error, reason()}
```

Validates a typed account and its conservation equation.

---

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