Skip to content

fix(errors): use bounded exponential backoff in withRetry (#30) - #41

Open
aswadi wants to merge 1 commit into
HyperDevTools:mainfrom
aswadi:fix/bounded-exponential-backoff-retry-30
Open

aswadi wants to merge 1 commit into
HyperDevTools:mainfrom
aswadi:fix/bounded-exponential-backoff-retry-30

Conversation

@aswadi

@aswadi aswadi commented Nov 15, 2026

Copy link
Copy Markdown

Closes #30

Replaces the linear baseDelayMs * attempt sleep in withRetry (src/errors.ts) with a deterministic, bounded exponential backoff schedule and upfront retry-option validation:

  • Bounded exponential backoff schedule (src/errors.ts):
    • Adds calculateRetryDelayMs(attempt, baseDelayMs, maxDelayMs) computing min(maxDelayMs, MAX_TIMER_DELAY_MS, baseDelayMs * 2^(attempt - 1)) (defaults: maxAttempts = 3, baseDelayMs = 50, maxDelayMs = 30_000).
    • Supports both the existing positional call signature (operation, maxAttempts?, baseDelayMs?, maxDelayMs?) and a RetryOptions object overload (operation, { maxAttempts?, baseDelayMs?, maxDelayMs? }).
  • Upfront option validation & timer-overflow safety:
    • Validates operation, maxAttempts (safe integer >= 1), baseDelayMs (finite >= 0), and maxDelayMs (finite >= 0, >= baseDelayMs when explicitly supplied), throwing ValidationError immediately without invoking operation on invalid settings.
    • Bounds large exponents and delays to MAX_TIMER_DELAY_MS (2_147_483_647 ms, Node.js setTimeout signed 32-bit ceiling) and guards 0 delays before exponentiation so Infinity, NaN, or 1ms 32-bit timer wraparounds cannot occur.
  • Immediate rethrow for non-retryable errors:
    • Rethrows non-retryable ToolkitError instances and standard Error values immediately on the first failure without scheduling a timer.
  • Deterministic fake-timer tests & docs (src/__tests__/errors.test.ts, docs/api/error-handling.md):
    • Adds jest.useFakeTimers() unit tests covering exponential progression, maxDelayMs capping, RetryOptions overload, immediate non-retryable failure, invalid retry settings, and overflow-sized delays (100% statement & line coverage on src/errors.ts), and documents the retry contract in docs/api/error-handling.md.

Local Verification

  • npx prettier --check "src/**/*.ts" -> all matched files formatted cleanly
  • npm run lint -> 0 warnings, 0 errors
  • npm test -- --coverage --runInBand -> 7 suites passed (75 / 75 tests, 100% line coverage on src/errors.ts)
  • npm run build && npx tsc --noEmit -> 0 errors
  • npm audit --audit-level=moderate -> 0 moderate/high/critical vulnerabilities

@aswadi
aswadi requested a review from Code-Kaybee as a code owner November 15, 2026 02:49

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use bounded exponential backoff for retryable toolkit errors

1 participant