UNPKG

@code-yeongyu/senpi

Version:

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

183 lines • 9.08 kB
import { formatSelector } from "./chains.js"; import { isHealthExhaustionFailure } from "./circuit-probes.js"; import { rateLimitModelFamily } from "../credential-pool/model-scope.js"; import { isBillingErrorMessage } from "./billing.js"; import { usageLimitScope } from "./usage-limit.js"; import { accountsWithCapacity } from "../subscription-limits/usage-capacity.js"; // A weekly window binds the account (seven_day) or one family (seven_day_opus/...); the text alone cannot tell. const WEEKLY_LIMIT = /\bweekly\b[^.]*\blimit\b/i; const MODEL_FAMILY = /(?:^|[^a-z])(opus|sonnet|haiku|fable|mythos)(?:[^a-z]|$)/i; /** The model family a model id belongs to, when it names one. */ function modelFamilyOf(modelId) { return MODEL_FAMILY.exec(modelId)?.[1]?.toLowerCase(); } /** * Chain entries this session has seen refuse to serve for a reason retrying cannot * fix within the cooldown: an account-wide usage limit or billing failure (every * model on that provider is out) or a model-scoped limit/exhaustion (that entry is * out). Backed by the session's selector cooldowns under namespaced keys, so it is * runtime-only and expires on the same injected clock as every other suppression. */ export class UnusableEntries { constructor(cooldowns) { this.accountErrors = new Map(); /** Wall-clock time each provider's account hold began: only usage reported after it can lift it (senpi#6536). */ this.accountHeldSince = new Map(); /** Providers whose limit named no family: the family it hit, awaiting one request on another family. */ this.pendingProbes = new Map(); this.widenedFamilies = new Map(); this.familyErrors = new Map(); this.cooldowns = cooldowns; } note(model, failure) { const scope = usageLimitScope(failure.errorMessage); const family = modelFamilyOf(model.id); // A subscription limit that names no family may bind only this family: hold it and let one request on // another family decide, instead of marking every model of the account spent (senpi#5596). if (scope === "account" && family !== undefined && failure.errorMessage !== undefined && WEEKLY_LIMIT.test(failure.errorMessage) && !isBillingErrorMessage(failure.errorMessage) && rateLimitModelFamily(failure.errorMessage) === undefined) { const held = this.pendingProbes.get(model.provider); if (held === undefined || held === family) { this.pendingProbes.set(model.provider, family); this.cooldowns.note(familyKey(model.provider, family), failure); this.familyErrors.set(familyKey(model.provider, family), failure.errorMessage); return undefined; } this.pendingProbes.delete(model.provider); this.widenedFamilies.set(model.provider, [held, family]); this.noteAccount(model.provider, failure); this.accountErrors.set(model.provider, failure.errorMessage); return { provider: model.provider, heldFamily: held, probeFamily: family, outcome: "widened" }; } if (scope === "account") { this.noteAccount(model.provider, failure); if (failure.errorMessage) this.accountErrors.set(model.provider, failure.errorMessage); } else if (scope === "model" || isHealthExhaustionFailure(failure.errorMessage)) { this.cooldowns.note(entryKey(model), failure); } return undefined; } /** A response served on `model`: a pending family probe on another family resolves as served. */ noteServed(model) { const held = this.pendingProbes.get(model.provider); const family = modelFamilyOf(model.id); if (held === undefined || family === undefined || family === held) return undefined; this.pendingProbes.delete(model.provider); return { provider: model.provider, heldFamily: held, probeFamily: family, outcome: "served" }; } /** The weekly-limit family hold on `model`, while it lasts (senpi#5596). */ heldFamily(model) { const family = modelFamilyOf(model.id); if (family === undefined) return undefined; const key = familyKey(model.provider, family); const errorMessage = this.familyErrors.get(key); return errorMessage !== undefined && this.cooldowns.isSuppressed(key) ? { family, errorMessage } : undefined; } /** The two families a no-family limit was confirmed on before it widened to the account. */ widenedFamiliesOf(provider) { return this.isProviderSpent(provider) ? this.widenedFamilies.get(provider) : undefined; } isProviderSpent(provider) { return this.cooldowns.isSuppressed(accountKey(provider)); } accountError(provider) { return this.isProviderSpent(provider) ? this.accountErrors.get(provider) : undefined; } /** Wall-clock time the provider said its account limit resets; undefined when the hold is a guessed default. */ accountResetAt(provider) { return this.cooldowns.statedResetAt(accountKey(provider)); } /** * Drops an account hold whose length was a guessed default, so the next request asks the provider again * (senpi#5568). A hold with a reset the provider stated is kept. Returns whether a hold was dropped. */ releaseGuessedAccountHold(provider) { if (this.accountResetAt(provider) !== undefined) return false; return this.releaseAccountHold(provider); } /** * Drops the provider's account hold when usage one of its accounts reported after the hold began shows it can * serve again: the provider lifted the limit before the reset it stated (senpi#6536). Returns whether one was dropped. */ releaseAccountHoldShownServable(provider, usage) { const since = this.accountHeldSince.get(provider); if (since === undefined || !this.isProviderSpent(provider)) return false; if (accountsWithCapacity(usage, provider, since, this.cooldowns.wallClock()).length === 0) return false; return this.releaseAccountHold(provider); } /** Drops the provider's account hold, whatever its length; returns whether one was held (senpi#5590). */ releaseAccountHold(provider) { if (!this.isProviderSpent(provider)) return false; this.cooldowns.clear(accountKey(provider)); this.accountErrors.delete(provider); this.accountHeldSince.delete(provider); this.clearFamilyHolds(provider); return true; } isUnusable(model) { const family = modelFamilyOf(model.id); return (this.isProviderSpent(model.provider) || this.cooldowns.isSuppressed(entryKey(model)) || (family !== undefined && this.cooldowns.isSuppressed(familyKey(model.provider, family)))); } clear(model) { // A deliberate model selection allows the user to probe after reauth/reset. this.cooldowns.clear(entryKey(model)); this.cooldowns.clear(accountKey(model.provider)); this.accountErrors.delete(model.provider); this.accountHeldSince.delete(model.provider); this.clearFamilyHolds(model.provider); } /** The provider's accounts changed (a login, an added or re-authorized account): every hold it earned is stale. */ clearProvider(provider, models) { this.cooldowns.clear(accountKey(provider)); this.accountErrors.delete(provider); this.accountHeldSince.delete(provider); for (const model of models) { if (model.provider === provider) this.cooldowns.clear(entryKey(model)); } this.clearFamilyHolds(provider); } noteAccount(provider, failure) { this.cooldowns.note(accountKey(provider), failure); this.accountHeldSince.set(provider, this.cooldowns.wallClock()); } /** A family hold and its pending probe go wherever the provider's own holds go (senpi#5596). */ clearFamilyHolds(provider) { const prefix = familyKey(provider, ""); for (const key of [...this.familyErrors.keys()]) { if (!key.startsWith(prefix)) continue; this.cooldowns.clear(key); this.familyErrors.delete(key); } this.pendingProbes.delete(provider); this.widenedFamilies.delete(provider); } } function accountKey(provider) { // This is a provider-name approximation, not resolved credential/account identity. // Gateways with distinct model accounts share a key; pooled slots can reset earlier // than the last surfaced account error. Explicit /model selection permits a probe. return `account:${provider}`; } function familyKey(provider, family) { return `family:${provider}:${family}`; } function entryKey(model) { return `exhausted:${formatSelector(model)}`; } //# sourceMappingURL=unusable.js.map