UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

41 lines • 1.44 kB
/** * Per-monitor wake pacing: a token bucket of `rate` tokens refilled at `rate` an hour, one token * per wake. Monitors without a rate are unpaced and always ready. */ const HOUR_MS = 3_600_000; export class WakePacer { #rateOf; #buckets = new Map(); constructor(rateOf) { this.#rateOf = rateOf; } /** When `id` next holds a whole token: `now` for an unpaced monitor or one with a token left. */ readyAt(id, now) { const paced = this.#refill(id, now); if (paced === undefined || paced.bucket.tokens >= 1) return now; return now + Math.ceil(((1 - paced.bucket.tokens) * HOUR_MS) / paced.rate); } spend(id, now) { const paced = this.#refill(id, now); if (paced !== undefined) paced.bucket.tokens = Math.max(0, paced.bucket.tokens - 1); } forget(id) { this.#buckets.delete(id); } clear() { this.#buckets.clear(); } #refill(id, now) { const rate = this.#rateOf(id); if (rate === undefined || !Number.isFinite(rate) || rate < 1) return undefined; const bucket = this.#buckets.get(id) ?? { tokens: rate, at: now }; bucket.tokens = Math.min(rate, bucket.tokens + ((now - bucket.at) * rate) / HOUR_MS); bucket.at = now; this.#buckets.set(id, bucket); return { bucket, rate }; } } //# sourceMappingURL=monitor-wake-pacing.js.map