policy/usage.bend checks
raw source on the hub · import cswap-bend-cli@0.1.0.0/policy/usage.bend as Usage
2 imports
import Base import ./types.bend as T
Definitions
def pct.two source · line 8 · raw
@+t:Nat -> @+o:Nat -> String
def pct.frac.pair source · line 15 · raw
@qr:Pair(Nat, Nat) -> String
def pct.frac source · line 19 · raw
@+q:Nat -> @+r:Nat -> String
def pct.label.pair source · line 26 · raw
@qr:Pair(Nat, Nat) -> String
def pct.label source · line 31 · raw
@+c:Nat -> String
4000 -> "40", 8550 -> "85.5": the reference's %.10g for two-digit inputs.
def win.opt source · line 36 · raw
@+p:Maybe<&2, Nat> -> @+r:Maybe<&2, Nat> -> @+rest:List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win> -> List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win>
def win.keep source · line 43 · raw
@+hit:Bool -> @+w:0x0717344745a30ebad1f19045228e8f18/policy/types.Win -> @+rest:List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win> -> List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win>
def model.wanted source · line 51 · raw
@+n:String -> @+ms:List<&2, String> -> Bool
n is lowercase; the configured name is lowercased here. all matches any.
def win.scoped.one source · line 58 · raw
@+s:0x0717344745a30ebad1f19045228e8f18/policy/types.Scoped -> @+ms:List<&2, String> -> @+rest:List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win> -> List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win>
def win.scoped source · line 63 · raw
@+ss:List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Scoped> -> @+ms:List<&2, String> -> List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win>
def windows source · line 72 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> @+ms:List<&2, String> -> List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win>
The relevant windows, in API order: 5h, 7d, then the wanted scoped ones. Never spend.
def win.max.step source · line 77 · raw
@+acc:Maybe<&2, Nat> -> @+p:Nat -> Maybe<&2, Nat>
def win.max source · line 84 · raw
@+ws:List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win> -> @+acc:Maybe<&2, Nat> -> Maybe<&2, Nat>
def head.of source · line 93 · raw
@+m:Maybe<&2, Nat> -> Maybe<&2, Nat>
def headroom.raw source · line 101 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> @+ms:List<&2, String> -> Maybe<&2, Nat>
100 - max(pct) over the relevant windows; None with no windows.
def err.is_none source · line 106 · raw
@+e:0x0717344745a30ebad1f19045228e8f18/policy/types.ErrKind -> Bool
def before.opt source · line 113 · raw
@+now:Nat -> @+m:Maybe<&2, Nat> -> Bool
def fresh.ext source · line 122 · raw
@+now:Nat -> @+nf:Nat -> @+np:Maybe<&2, Nat> -> @+hu:Nat -> @+cu:Nat -> Bool
Trust is extended while the row is failing (consecutive failures > 0, as the reference), planned, held or claimed.
def win.reset source · line 125 · raw
@+w:0x0717344745a30ebad1f19045228e8f18/policy/types.Win -> Maybe<&2, Nat>
def soon.min source · line 130 · raw
@+a:Maybe<&2, Nat> -> @+b:Maybe<&2, Nat> -> Maybe<&2, Nat>
def win.soonest source · line 141 · raw
@+ws:List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Win> -> @+acc:Maybe<&2, Nat> -> Maybe<&2, Nat>
The soonest reset among the relevant windows (past ones included): once it passes, that window is zeroed and the snapshot is obsolete.
def soon.ok source · line 149 · raw
@+now:Nat -> @+soonest:Maybe<&2, Nat> -> Bool
No window reset known: only the age ceiling applies.
def within.ceiling source · line 160 · raw
@+e:0x0717344745a30ebad1f19045228e8f18/policy/types.ErrKind -> @+age:Nat -> @+now:Nat -> @+soonest:Maybe<&2, Nat> -> Bool
How long a failing row's reading may be trusted. After an http-429 the account's windows did not move, so the reading is a valid lower bound until the soonest window resets: now < min(earliest reset, fetched_at + 7200 s). Any other failure is no evidence the reading still holds: at most 3600 s.
def fresh.age source · line 167 · raw
@+age:Nat -> @+ext:Bool -> @+within:Bool -> Bool
def fresh.at source · line 173 · raw
@+fa:Maybe<&2, Nat> -> @+now:Nat -> @+e:0x0717344745a30ebad1f19045228e8f18/policy/types.ErrKind -> @+nf:Nat -> @+np:Maybe<&2, Nat> -> @+hu:Nat -> @+cu:Nat -> @+soonest:Maybe<&2, Nat> -> Bool
A reading is decision-grade when it is at most 300 s old, or its trust is
extended (the row is failing, planned, held or claimed) and within the
ceiling of within.ceiling.
def fresh source · line 180 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> @+now:Nat -> @+ms:List<&2, String> -> Bool
def sent.none source · line 185 · raw
@+s:0x0717344745a30ebad1f19045228e8f18/policy/types.Sentinel -> Bool
def trusted source · line 193 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> @+now:Nat -> @+ms:List<&2, String> -> Bool
A decision-grade reading: fresh, and not hidden behind a sentinel.
def head.if source · line 198 · raw
@+ok:Bool -> @+h:Maybe<&2, Nat> -> Maybe<&2, Nat>
def headroom source · line 207 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> @+now:Nat -> @+ms:List<&2, String> -> Maybe<&2, Nat>
The decision value of an account: None when there is no trusted reading or the reading carries no relevant window.
def usage.slot source · line 210 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> Nat
def usage.backoff_until source · line 215 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> Nat
def in_backoff source · line 220 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> @+now:Nat -> Bool
def usage.sentinel source · line 223 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> 0x0717344745a30ebad1f19045228e8f18/policy/types.Sentinel
def usage.of.pick source · line 228 · raw
@+hit:Bool -> @+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> @+rest:Maybe<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Usage> -> Maybe<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Usage>
def usage.of source · line 236 · raw
@+slot:Nat -> @+us:List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Usage> -> Maybe<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Usage>
The first usage row of a slot; None is "no reading".
def account.slot source · line 243 · raw
@+a:0x0717344745a30ebad1f19045228e8f18/policy/types.Account -> Nat
def account.of.pick source · line 248 · raw
@+hit:Bool -> @+a:0x0717344745a30ebad1f19045228e8f18/policy/types.Account -> @+rest:Maybe<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Account> -> Maybe<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Account>
def account.of source · line 255 · raw
@+slot:Nat -> @+as:List<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Account> -> Maybe<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Account>
def head.row source · line 263 · raw
@+m:Maybe<&2, 0x0717344745a30ebad1f19045228e8f18/policy/types.Usage> -> @+now:Nat -> @+ms:List<&2, String> -> Maybe<&2, Nat>
Headroom of a row, if there is a row.
def poll.stale source · line 272 · raw
@+fa:Maybe<&2, Nat> -> @+now:Nat -> @+ttl:Nat -> Bool
def poll.planned_due source · line 279 · raw
@+np:Maybe<&2, Nat> -> @+now:Nat -> Bool
def poll.overslept source · line 287 · raw
@+np:Maybe<&2, Nat> -> @+now:Nat -> @+pi:Nat -> Bool
next_poll_at lies further out than max(interval, 600) * 1.1 + 60 s.
def poll.unplanned source · line 294 · raw
@+np:Maybe<&2, Nat> -> Bool
def poll.ready source · line 301 · raw
@+np:Maybe<&2, Nat> -> @+now:Nat -> @+pi:Nat -> @+fa:Maybe<&2, Nat> -> @+ttl:Nat -> Bool
def poll.gate source · line 305 · raw
@+blocked:Bool -> @+ready:Bool -> Bool
def poll_due source · line 314 · raw
@+u:0x0717344745a30ebad1f19045228e8f18/policy/types.Usage -> @+now:Nat -> @+cfg:0x0717344745a30ebad1f19045228e8f18/policy/types.Config -> Bool
Is this row due a fetch on a scheduled auto tick? Never inside backoff, dead (one auth strike), held or claimed; then the plan or staleness decides.
def backoff.cap source · line 326 · raw
@+k:Nat -> Nat
def backoff.zero source · line 329 · raw
@+rl:Bool -> @+computed:Nat -> Nat
def backoff.bump source · line 336 · raw
@+big:Bool -> @+ra:Nat -> Nat
def backoff.asked source · line 343 · raw
@+rl:Bool -> @+ra:Nat -> Nat
def backoff.some source · line 350 · raw
@+rl:Bool -> @+ra:Nat -> @+computed:Nat -> Nat
def backoff.retry.some source · line 353 · raw
@+r:Nat -> @+rl:Bool -> @+computed:Nat -> Nat
def backoff.rate_limited source · line 360 · raw
@+e:0x0717344745a30ebad1f19045228e8f18/policy/types.ErrKind -> Bool
def backoff.retry source · line 367 · raw
@+rl:Bool -> @+ra:Maybe<&2, Nat> -> @+computed:Nat -> Nat
def backoff_for source · line 377 · raw
@+n:Nat -> @+e:0x0717344745a30ebad1f19045228e8f18/policy/types.ErrKind -> @+ra:Maybe<&2, Nat> -> Nat
Seconds of backoff after the n-th consecutive failure: 30, 60, 120, 240, 480, 600; a server's Retry-After raises it (Retry-After 0 on an http-429 means at least 300 s).