From 99ad15a37f62b49feded595cf5b0b6aafe56430e Mon Sep 17 00:00:00 2001 From: Austin Liu <193228693+Dingding-leo@users.noreply.github.com> Date: Thu, 23 Jul 2026 23:36:44 +0930 Subject: [PATCH] fix(resilience): skip terminal connections in token health check sweep (#8182) (#8286) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Background token health check sweep was probing connections with terminal statuses (credits_exhausted / banned / expired) on every cycle, wasting CPU and network. These connections can never self-heal via token refresh — they need manual re-auth or credit top-up. Add a terminal status guard in checkConnection() that mirrors the existing isTerminalConnectionStatus() in auth.ts and TERMINAL_CONNECTION_STATUSES in connectionRecovery.ts. Verified by reporter: after manually disabling 11 credits_exhausted connections, CPU dropped from ~53% to ~2.7%. This fix automates that skip so the sweep never touches terminal connections in the first place. Co-authored-by: Austin Liu --- src/lib/tokenHealthCheck.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib/tokenHealthCheck.ts b/src/lib/tokenHealthCheck.ts index 4007a5d60d..e7ad393a58 100644 --- a/src/lib/tokenHealthCheck.ts +++ b/src/lib/tokenHealthCheck.ts @@ -399,6 +399,17 @@ export async function checkConnection(conn) { const intervalMin = conn.healthCheckInterval ?? DEFAULT_HEALTH_CHECK_INTERVAL_MIN; if (intervalMin <= 0) return; if (!conn.isActive) return; + + // #8182: skip terminal connections (credits_exhausted / banned / expired). + // These can never self-heal via a token refresh — probing them wastes + // CPU and network on every sweep cycle. Mirrors isTerminalConnectionStatus + // in src/sse/services/auth.ts and TERMINAL_CONNECTION_STATUSES in + // src/lib/quota/connectionRecovery.ts. + const terminalStatuses = new Set(["credits_exhausted", "banned", "expired"]); + if (typeof conn.testStatus === "string" && terminalStatuses.has(conn.testStatus.toLowerCase())) { + return; + } + if (!conn.refreshToken || typeof conn.refreshToken !== "string") { if (isGitHubAccessTokenOnlyConnection(conn)) { const now = new Date().toISOString();