Files
puter/src/backend
Daniel Salazar bc456982e9 feat(metering): events for an actor's balance and for a refusal a listener can make good (#4643)
* feat(metering): report an actor's balance each time it is worked out

`metering.balance` carries what an actor on a metered plan has left to
spend and the plan it was worked out under. It is emitted when usage
settles and when a credit gate re-reads the balance, so an extension can
act on a low balance without reading it itself.

The balance a settling batch reports now counts the purchased credit
that batch drew, so the credit gate and the exhausted notice follow the
batch that spends the last of it instead of the one after. A batch that
settles right after a credits change still reports the credit as read,
since that read may predate the change.

`MeteringService.isUnmetered` is public, for callers that need to tell a
plan with nothing to run out of from one that has run out.

* feat(metering): let a listener top an actor up at the moment of refusal

`metering.out-of-credits` is emitted, and waited on, when an actor is
about to be refused for lack of credit. A listener that adds credit sets
`refilled`, and the refused work is tried once more instead of failing.

It is asked from the three places a refusal is decided: the credit gate
on routes, the shared credit reservation the AI drivers use, and the
chat driver's own gate. Each tries again once, after the listener
answers, and refuses as before when nothing was added.

* fix(metering): count a flush's write once in a read with no cached base

An increment for a counter with no cached base read the store after its
buffer snapshot with nothing ordering the two, so a flush landing in
between was counted from both, and that store read was then kept as the
base. It now reads the store the way other reads do, starting from the
increment's own snapshot.

A write that has reached the store but is not yet applied to the buffer
looks the same from the buffer as one still on its way. `kv.get` can now
answer with the last fence token a writer applied to a key, and a read
counts such a write's amounts from one side only. A read that sees a
write sent between its snapshots is retried.

A base is laid from a store read only if no write was out when it was
read and none has been sent since.
2026-11-14 12:43:02 -07:00
..
…
…
…
…