Repository navigation
fix: shorten refresh backoff for transient failures and log the cause (#178) - #179
Merged
Merged
Conversation
…#178) A failed OAuth token refresh blocked further refreshes for a fixed hour regardless of cause, so a single transient network error, timeout, or 5xx could stop telemetry for up to 75 minutes. Classify the refresh failure: rejected credentials (invalid_grant and other 4xx) keep the hour-long backoff because retrying a dead refresh token cannot help, while transient failures back off only ~1 minute so the next poll can retry. Also surface the underlying refresh error once as a warning, since it was previously only written to status.lastError and immediately overwritten by the throttle message. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes part of #178.
Problem
A failed OAuth token refresh set a single, fixed one-hour throttle (
DEFAULT_REFRESH_RETRY_DELAY_MS) regardless of the cause. Combined with occasional packet loss on the reporter's line, one transient error during a refresh stopped telemetry for up to 75 minutes (60 min throttle + backoff). On short poll intervals this happened several times an hour.On top of that, the actual reason of a failed refresh was never logged —
onFailureonly wrotestatus.lastError, which the next poll's throttle message immediately overwrote, so the original error was invisible.Change
classifyRefreshFailure, duck-typed on theBluettiOAuthTokenClientErrorreason/httpStatusto avoid a circular import):auth—invalid_grant/oauth_errorand other 4xx (except 429): keep the full one-hour backoff, because retrying a rejected refresh token cannot succeed until re-auth (see Re-authentication has no effect until manual restart: running poll loop keeps the stale token #175).transient— network error, timeout, 5xx, 429, and anything unknown: back off only ~1 minute (DEFAULT_TRANSIENT_REFRESH_RETRY_DELAY_MS) so the next scheduled poll retries.onRefreshFailurecallback, wired tothis.log.warninmain.ts. It fires only on real attempts, not on throttled polls, so it does not spam.Not addressed here (needs reporter input)
Point 1 of the issue — "token refreshed on every poll". The code already stamps
created_atfromexpires_in(normalizeTokenResponse, fix for #46), so expiry should be computable and refresh should not run every poll. If it still does for the reporter, BLUETTI's refresh response likely omitsexpires_inentirely. I'll ask on the issue which expiry fields the stored token actually contains before guessing at a fallback lifetime.Tests
Added unit tests: transient failure retries after the short delay; rejected credentials keep the full backoff; the failure is reported exactly once per failure window (not on throttled polls); and the classifier mapping.
npm run check,lint,test:ts(99 passing) andtest:package(57 passing) all green.🤖 Generated with Claude Code