Expand description
G6 — quota keeper: pure scheduler state machine, private persisted state and singleton/attempt locking.
The keeper watches each kept account’s weekly window (identified by its
provider-reported duration — never by the name primary) and tracks the
provider-reported reset instant as the authoritative reset generation.
When a generation’s reset has passed and fresh usage still shows no newer
window, the account is due for activation. In read-only mode (the
default) the keeper only records and alerts. Only for accounts the
operator explicitly opted in, the runner performs one bounded,
tool-free request per generation; the keeper never declares the window
active until a fresh usage read shows a strictly later reset.
§Invariants
- No I/O, no clock, no network in the state machine: every function takes
now_ms. Persistence and locking are separate helpers. - State is keyed by broker source + provider seat identity. For an opted-in account the identity is a strong seat fingerprint (digest of the full provider account id carried by the credential itself), so the key is canonical across aliases: two labels for one seat share one attempt ledger and a sequential alias switch cannot spend twice. A read-only account may carry only a weak metadata preview (label-scoped key). A metadata prefix is not an identity: activation is refused without a strong fingerprint, and the runner re-verifies the seat from the freshly vended token immediately before the request. Re-login of the same alias with a different seat never inherits a ticket.
- An attempt is recorded and must be persisted before the request is
sent (
begin_attempt); ambiguous outcomes (timeout, crash between begin and finish, 5xx) consume the per-generation budget of exactly one. Only a definitive not sent (typed pre-flight stage) or a whitelisted pre-inference 4xx is refunded, and those are bounded separately with backoff. A second attempt for the same generation requires an explicit operatorrearm. - A newly observed window is labelled verified; correlation with a keeper attempt is recorded, but never claimed as causal.
- Natural rollover is never assumed: with no evidence the phase is
Unknown/AwaitingActivation, neverActive. - Proven capacity before any inference. An activation attempt is
authorized only when the latest poll succeeded, is fresh, carries no
overall
limit_reachedassertion, and every window applicable to the activation model (5h, weekly, model-scoped) shows headroom. A passed reset while the provider still asserts 100 % /limit_reachedisExhausted(reset passed), never due.
Structs§
- Account
Identity - Namespace of one kept account. Every field is non-secret display text.
- Account
State - Attempt
Record - One activation attempt, for the operator record.
- Attempt
Ticket - Ticket for one attempt. The caller MUST persist the state between
begin_attemptand the request, so a crash leaves a pending record. - Banked
Resets - Banked reset inventory (Codex
rate_limit_reset_credits). Alert-only. - Keeper
Config - Keeper
Lock - Exclusive advisory
flockheld for the process lifetime (released on drop or process death). Used for the state-dir singleton and for one lock per kept account in the canonical keeper dir, so a second keeper with a different--state-dirstill cannot act on the same account. - Keeper
State - State
Store - Private, atomic JSON state (dir
0700, file0600, tmp+rename). - Usage
Observation - What one usage poll returned. Produced by the runner’s usage adapter.
Enums§
- Activation
Decision - Runner-facing decision after a poll.
- Attempt
Outcome - Terminal classification of one activation attempt, as reported by the
runner. Only a typed
NotSentand a whitelistedRejectedstatus are definitive “no inference” outcomes; everything else may have spent quota. - Attribution
- How a verified window relates to keeper activity. Correlation is recorded; causation is never claimed (an external request or a provider-side reset could equally have anchored the window).
- Keeper
Error - Keeper
Event - Operator-visible outcome of a state-machine step. Secret-free.
- NotSent
Stage - Where a request definitively stopped before leaving the process or
before reaching the provider. Only these stages may be reported as
NotSent; anything after the request was written to the socket isAmbiguous. - Observation
Outcome - Phase
Constants§
- ACCOUNT_
LOCK_ DIR - Sub-directory (of the canonical keeper dir) holding per-account locks.
- DEFAULT_
ATTEMPT_ TIMEOUT_ MS - DEFAULT_
BANKED_ EXPIRY_ ALERT_ MS - DEFAULT_
MAX_ ATTEMPTS - Attempts per reset generation. Fixed at one: a second attempt for the
same generation only ever happens through an explicit operator
rearm, never automatically. - DEFAULT_
MAX_ BACKOFF_ MS - DEFAULT_
POLL_ INTERVAL_ MS - DEFAULT_
RESET_ GRACE_ MS - Poll shortly after the reported reset instead of waiting a full interval.
- DEFAULT_
STALE_ AFTER_ MS - DEFAULT_
USAGE_ TIMEOUT_ MS - DEFAULT_
VERIFY_ POLL_ MS - Re-poll cadence while an attempt awaits verification.
- DEFAULT_
VERIFY_ WINDOW_ MS - How long an attempt may stay pending before it is labelled unverified.
- MAX_
ATTEMPT_ TIMEOUT_ MS - MAX_
HISTORY - Attempt history retained per account.
- MAX_
MAX_ ATTEMPTS - MAX_
MAX_ BACKOFF_ MS - MAX_
NOT_ SENT_ PER_ GENERATION - Definitive not-sent/rejected outcomes tolerated per generation.
- MAX_
POLL_ INTERVAL_ MS - MAX_
RESET_ GRACE_ MS - MAX_
STALE_ AFTER_ MS - MAX_
VERIFY_ WINDOW_ MS - MIN_
ATTEMPT_ TIMEOUT_ MS - MIN_
POLL_ INTERVAL_ MS - MIN_
STALE_ AFTER_ MS - REFUNDABLE_
REJECT_ STATUSES - HTTP statuses that prove the provider rejected the request before any inference (malformed request, auth, missing route, payload shape). Everything else — notably 429, 402, 408, 409 — is not refunded.
- SEAT_
FP_ PREFIX - Prefix of a strong seat fingerprint (see
AccountIdentity::seat_fingerprint). - SINGLETON_
LOCK_ NAME - File name of the singleton lock inside the state dir.
- STATE_
FILE_ NAME - File name of the persisted state inside the state dir.
- STATE_
VERSION
Functions§
- account_
lock_ name - File-name-safe form of a storage key for per-account locks.
- activation_
decision - Whether the runner may start an activation attempt now.
- begin_
attempt - Record an attempt as started. Consumes one unit of the generation budget.
- capacity_
view - Capacity view of the last successful poll, for
quota_policy. - finish_
attempt - Record the outcome of an attempt. Refundable outcomes return the budget unit and apply backoff; all others stay pending until verified/expired.
- observe
- Feed one fresh usage observation for
keyinto the state machine. - rearm
- Explicit operator re-arm: allow exactly one more attempt for the
generation currently in
Unverified. Never automatic. ReturnsOk(None)when the account is not unverified (nothing to re-arm). - recover_
after_ restart - Housekeeping on startup: a pending attempt with no recorded outcome means the previous process died between begin and finish — mark it ambiguous.
- windows_
with_ overall_ flag - Fold a provider-asserted overall
limit_reached: trueinto the account-wide windows so every policy consumer honours it. With no account-wide window present a syntheticoverallwindow is added.