From ab35cc83461623d88512cc829cda9770eb29c200 Mon Sep 17 00:00:00 2001 From: Vigilio Desto Date: Tue, 7 Apr 2026 15:55:32 +0000 Subject: [PATCH] recommend.js: probe fresh when all cached providers are invalid_key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- recommend.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/recommend.js b/recommend.js index cdf907d..8615dfa 100644 --- a/recommend.js +++ b/recommend.js @@ -50,7 +50,18 @@ async function getProviderData() { // Try fresh cache first (within 20 minutes from monitor.js runs) const cached = getCachedRun(20); if (cached && cached.providers) { - return { source: 'cache', providers: 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)