recommend.js: probe fresh when all cached providers are invalid_key

invalid_key (HTTP 401) can be transient during key rotation or
temporary API issues — unlike rejected/exhausted which are stable
budget states. When cache shows all chain providers as invalid_key,
bypass cache and probe fresh so recovery is immediate instead of
waiting for the 20-minute TTL to expire.
This commit is contained in:
Vigilio Desto 2026-04-07 15:55:32 +00:00
parent 6e6d93f3bf
commit ab35cc8346
Signed by: vigilio
GPG key ID: 159D6AD58C8E55E9

View file

@ -50,8 +50,19 @@ async function getProviderData() {
// Try fresh cache first (within 20 minutes from monitor.js runs)
const cached = getCachedRun(20);
if (cached && cached.providers) {
// Cache hit — but check if all chain members are invalid_key
// If so, probe fresh: invalid_key can be transient (API 401s during key rotation)
// vs. rejected/exhausted which are stable budget states worth caching
const chainProviders = PROVIDER_CHAIN
.map(name => cached.providers[name])
.filter(Boolean);
const allInvalidOrMissing = chainProviders.length === 0 ||
chainProviders.every(p => p.status === 'invalid_key' || p.status === 'error');
if (!allInvalidOrMissing) {
return { source: 'cache', providers: cached.providers };
}
// All invalid — fall through to fresh probe
}
// No cache — probe Teams providers directly (no full monitor run, targeted probes only)
const allProviders = getProviders();