@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
112 lines • 5.61 kB
JavaScript
import { MAX_SLOT_BLOCK_MS } from "@earendil-works/pi-ai/auth/pool/failover";
import { stripProviderRequestIds } from "@earendil-works/pi-ai/utils/retry";
import { isValidThinkingLevel } from "../../cli/args.js";
import { usageLimitResetMs } from "../credential-pool/reset-time.js";
import { isTransportFailureText } from "./transient-network.js";
/**
* Runtime-only selector suppression. A SelectorCooldowns instance belongs to one
* AgentSession and is deliberately never persisted to settings or session files.
* Keys are canonical base selectors (`provider/id`), or a rung (`provider/id:level`) for a
* hard error that parks only the level that failed (senpi#3311).
*/
export class SelectorCooldowns {
constructor(now, random = Math.random, wallNow = now) {
this.expiresAtBySelector = new Map();
/**
* The same deadline on the wall clock. The monotonic clock stops while the machine sleeps, so a limit that
* reset during a sleep would stay held for its whole length again after waking (senpi#4085).
*/
this.wallExpiresAtBySelector = new Map();
/** Wall-clock end of a suppression whose length the provider stated (a reset or retry-after), not a default. */
this.statedResetAtBySelector = new Map();
this.now = now;
this.random = random;
this.wallNow = wallNow;
}
note(baseSelector, failure) {
const { ms, stated } = this.durationFor(failure);
this.expiresAtBySelector.set(baseSelector, this.now() + ms);
this.wallExpiresAtBySelector.set(baseSelector, this.wallNow() + ms);
if (stated)
this.statedResetAtBySelector.set(baseSelector, this.wallNow() + ms);
else
this.statedResetAtBySelector.delete(baseSelector);
}
isSuppressed(baseSelector) {
const expiresAt = this.expiresAtBySelector.get(baseSelector);
if (expiresAt === undefined)
return false;
if (this.now() < expiresAt && this.wallNow() < (this.wallExpiresAtBySelector.get(baseSelector) ?? Infinity))
return true;
this.clear(baseSelector);
return false;
}
/** When a live suppression ends, if the provider stated it; undefined for a guessed default or no suppression. */
statedResetAt(baseSelector) {
return this.isSuppressed(baseSelector) ? this.statedResetAtBySelector.get(baseSelector) : undefined;
}
clear(baseSelector) {
this.expiresAtBySelector.delete(baseSelector);
this.wallExpiresAtBySelector.delete(baseSelector);
this.statedResetAtBySelector.delete(baseSelector);
}
/** Clears `baseSelector` and every rung of it (`baseSelector:<thinking level>`), never another model. */
clearModel(baseSelector) {
for (const key of this.expiresAtBySelector.keys()) {
// Only a rung of this model: a model id may itself contain ':' (openrouter's `x:batch`).
const rung = key.startsWith(`${baseSelector}:`) && isValidThinkingLevel(key.slice(baseSelector.length + 1));
if (key === baseSelector || rung)
this.clear(key);
}
}
/** The wall clock the stated resets are read on. */
wallClock() {
return this.wallNow();
}
clearAll() {
this.expiresAtBySelector.clear();
this.wallExpiresAtBySelector.clear();
this.statedResetAtBySelector.clear();
}
durationFor(failure) {
const stated = this.statedDurationMs(failure);
return stated === undefined
? { ms: this.defaultDurationMs(failure.errorMessage), stated: false }
: { ms: stated, stated: true };
}
statedDurationMs({ retryAfterMs, errorMessage, }) {
// Reset hints are wall-clock times; the stored deadline remains monotonic.
const resetMs = errorMessage === undefined ? undefined : usageLimitResetMs(errorMessage, this.wallNow());
const retryMs = retryAfterMs !== undefined && Number.isFinite(retryAfterMs) && retryAfterMs > 0 ? retryAfterMs : undefined;
const hintedMs = Math.max(resetMs ?? 0, retryMs ?? 0);
return hintedMs > 0 ? Math.min(MAX_SLOT_BLOCK_MS, hintedMs) : undefined;
}
defaultDurationMs(errorMessage) {
const message = stripProviderRequestIds(errorMessage ?? "").toLowerCase();
const defaultMs = /usage[- ]limit|quota|insufficient_quota|billing|credits[-_ ]required|credits are required/.test(message)
? 30 * 60_000
: /rate[ -]?limit|429|too many requests/.test(message)
? 30_000
: /overloaded|capacity/.test(message)
? 45_000 + this.capacityJitterMs()
: /5xx|\b5\d\d\b|server|internal error/.test(message)
? 20_000
: undefined;
if (defaultMs !== undefined)
return defaultMs;
// A reason-less forbidden rejection is a per-request blip on a healthy credential (senpi#2376).
if (/"type"\s*:\s*"forbidden"[^}]*request not allowed|request not allowed[^}]*"type"\s*:\s*"forbidden"/.test(message)) {
return 60_000;
}
// Transport blips (timeouts, DNS, socket drops) say nothing about model
// health; the 5-minute default parked the primary and blocked revert-to-primary.
if (isTransportFailureText(message)) {
return 60_000;
}
return 5 * 60_000;
}
capacityJitterMs() {
return Math.round(Math.min(1, Math.max(0, this.random())) * 30_000);
}
}
//# sourceMappingURL=cooldown.js.map