Open the context
Choose the project folder and name the files or behavior involved.
Decide token-bucket admission from an explicit timestamp and return the next state. Check exact token exhaustion and capped refill, rejecting backward time, invalid costs and non-finite state values.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from math import isfinite
def admit(tokens, last, now, capacity, rate, cost):
values = (tokens, last, now, capacity, rate, cost)
if not all(type(x) in (int, float) and isfinite(x) for x in values):
raise ValueError("finite numbers required")
if not (capacity > 0 and rate > 0 and 0 < cost <= capacity
and 0 <= tokens <= capacity and now >= last):
raise ValueError("invalid bucket state")
refill = min(capacity, tokens + (now - last) * rate)
allowed = refill >= cost
return (refill - cost if allowed else refill, now), allowedThe example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.