Naive fixed windowobserved
0admitted
counter resets at the boundaryLIMIT 5
A fixed-window rate limiter looks right and passes ordinary tests. At the boundary, it admits twice the advertised limit. Seven annotation lines turn the production TypeScript below into a universal claim.
Read the LemmaScript contractThe shipping implementation stays ordinary TypeScript. The //@ contract is the part a human reviews—and the part LemmaScript asks Dafny to prove for every valid input.
export function admit(log: number[], now: number, W: number, limit: number): AdmitResult { //@ requires W >= 1 //@ requires limit >= 0 //@ requires log.length <= limit //@ requires forall(k, 0 <= k && k < log.length ==> log[k] <= now) //@ ensures \result.log.length <= limit // WINDOW-FAITHFUL: //@ ensures forall(k, 0 <= k && k < \result.log.length ==> now - W < \result.log[k] && \result.log[k] <= now) // exact decision + non-restrictive: //@ ensures \result.ok === (activeCount(log, now, W) < limit) const active = pruneWindow(log, now, W); if (active.length < limit) { return { log: [...active, now], ok: true }; } return { log: active, ok: false };}npm run verify> lsc check --backend=dafny verified/core.verified.tsGenerated: verified/core.verified.dfy.genRunning dafny verify...Dafny program verifier finished with 24 verified, 0 errors
For every monotone request stream, every half-open window(s, s + W] contains at most limit admitted requests.
The clock is monotone across calls.
The per-key store updates atomically.
The verified core is the code imported by the middleware.
// BOUND: