From cf979d6b6d75d14247ca3e377fbffeba5a1a184b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Jan 2026 14:17:18 +0000 Subject: [PATCH 1/3] fix(retry): resolve RETRY_BACKOFF_DELAYS initialization order error - Remove duplicate RETRY_BACKOFF_DELAYS and RETRY_JITTER_FACTOR from Defaults/index.ts - Export constants from retry-utils.ts as single source of truth - Add 'as const' for better type inference - Fixes "Cannot access 'RETRY_BACKOFF_DELAYS' before initialization" error --- src/Defaults/index.ts | 16 ++++------------ src/Utils/retry-utils.ts | 6 +++--- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index 9116ff43..5f22f2d5 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -169,15 +169,7 @@ export const DEFAULT_CACHE_MAX_KEYS = { LID_GLOBAL: 10_000 } -/** - * Retry configuration with custom progressive backoff - * Fixed delay steps in milliseconds: 1s → 2s → 5s → 10s → 20s - * Use with 'stepped' backoff strategy in retry-utils.ts - */ -export const RETRY_BACKOFF_DELAYS = [1000, 2000, 5000, 10000, 20000] - -/** - * Jitter factor for retry delays (0.15 = ±15% randomization) - * Helps prevent thundering herd problem - */ -export const RETRY_JITTER_FACTOR = 0.15 +// NOTE: RETRY_BACKOFF_DELAYS and RETRY_JITTER_FACTOR are defined in retry-utils.ts +// to avoid circular dependency and initialization order issues. +// Import from '@whiskeysockets/baileys' Utils if needed: +// import { getRetryDelayWithJitter, getAllRetryDelaysWithJitter } from './Utils/retry-utils' diff --git a/src/Utils/retry-utils.ts b/src/Utils/retry-utils.ts index 66594804..61affb88 100644 --- a/src/Utils/retry-utils.ts +++ b/src/Utils/retry-utils.ts @@ -20,15 +20,15 @@ import type { CircuitBreaker } from './circuit-breaker.js' /** * Retry configuration with custom progressive backoff * Fixed delay steps in milliseconds: 1s → 2s → 5s → 10s → 20s - * Defined locally to avoid circular dependency with Defaults + * Exported for external use (e.g., custom retry logic) */ -const RETRY_BACKOFF_DELAYS = [1000, 2000, 5000, 10000, 20000] +export const RETRY_BACKOFF_DELAYS = [1000, 2000, 5000, 10000, 20000] as const /** * Jitter factor for retry delays (0.15 = ±15% randomization) * Helps prevent thundering herd problem */ -const RETRY_JITTER_FACTOR = 0.15 +export const RETRY_JITTER_FACTOR = 0.15 /** * Backoff strategies From 890a80da4878bd9bcb390367df7ce177d616a87c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Jan 2026 14:25:56 +0000 Subject: [PATCH 2/3] fix(retry): use literal values in retryConfigs to fix ESM initialization Hardcode values in retryConfigs.rsocket instead of referencing RETRY_BACKOFF_DELAYS and RETRY_JITTER_FACTOR to avoid "Cannot access before initialization" error in ESM module loading. --- src/Utils/retry-utils.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Utils/retry-utils.ts b/src/Utils/retry-utils.ts index 61affb88..672f0838 100644 --- a/src/Utils/retry-utils.ts +++ b/src/Utils/retry-utils.ts @@ -656,14 +656,14 @@ export const retryConfigs = { /** * RSocket-style retry with stepped delays * Uses fixed delay array: 1s, 2s, 5s, 10s, 20s (with ±15% jitter) - * Unlike exponential, this uses exact delays from RETRY_BACKOFF_DELAYS + * Values hardcoded to avoid initialization order issues in ESM */ rsocket: { - maxAttempts: RETRY_BACKOFF_DELAYS.length, - baseDelay: RETRY_BACKOFF_DELAYS[0], - maxDelay: RETRY_BACKOFF_DELAYS[RETRY_BACKOFF_DELAYS.length - 1], + maxAttempts: 5, // RETRY_BACKOFF_DELAYS.length + baseDelay: 1000, // RETRY_BACKOFF_DELAYS[0] + maxDelay: 20000, // RETRY_BACKOFF_DELAYS[4] backoffStrategy: 'stepped' as const, - jitter: RETRY_JITTER_FACTOR, + jitter: 0.15, // RETRY_JITTER_FACTOR }, } From baf9123d0cb1b97c883a949d42cf05337d820f4a Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Jan 2026 14:34:47 +0000 Subject: [PATCH 3/3] fix(retry): address PR #15 code review feedback - Re-export RETRY_BACKOFF_DELAYS and RETRY_JITTER_FACTOR from Defaults for backwards compatibility with existing imports - Add 'as const' to RETRY_JITTER_FACTOR for type consistency - Improve documentation explaining why values are hardcoded in retryConfigs (ESM initialization order issues with indirect circular imports) --- src/Defaults/index.ts | 7 +++---- src/Utils/retry-utils.ts | 17 +++++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index 5f22f2d5..78cce3b7 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -169,7 +169,6 @@ export const DEFAULT_CACHE_MAX_KEYS = { LID_GLOBAL: 10_000 } -// NOTE: RETRY_BACKOFF_DELAYS and RETRY_JITTER_FACTOR are defined in retry-utils.ts -// to avoid circular dependency and initialization order issues. -// Import from '@whiskeysockets/baileys' Utils if needed: -// import { getRetryDelayWithJitter, getAllRetryDelaysWithJitter } from './Utils/retry-utils' +// Re-export retry constants for backwards compatibility +// Actual definitions are in retry-utils.ts to avoid ESM initialization order issues +export { RETRY_BACKOFF_DELAYS, RETRY_JITTER_FACTOR } from '../Utils/retry-utils' diff --git a/src/Utils/retry-utils.ts b/src/Utils/retry-utils.ts index 672f0838..e3be6ce0 100644 --- a/src/Utils/retry-utils.ts +++ b/src/Utils/retry-utils.ts @@ -28,7 +28,7 @@ export const RETRY_BACKOFF_DELAYS = [1000, 2000, 5000, 10000, 20000] as const * Jitter factor for retry delays (0.15 = ±15% randomization) * Helps prevent thundering herd problem */ -export const RETRY_JITTER_FACTOR = 0.15 +export const RETRY_JITTER_FACTOR = 0.15 as const /** * Backoff strategies @@ -656,14 +656,19 @@ export const retryConfigs = { /** * RSocket-style retry with stepped delays * Uses fixed delay array: 1s, 2s, 5s, 10s, 20s (with ±15% jitter) - * Values hardcoded to avoid initialization order issues in ESM + * + * NOTE: Values are hardcoded instead of referencing RETRY_BACKOFF_DELAYS/RETRY_JITTER_FACTOR + * to prevent "Cannot access before initialization" errors in ESM environments. + * This occurs when modules are loaded in specific orders due to indirect circular imports + * (e.g., via prometheus-metrics.ts -> circuit-breaker.ts chain). + * Keep these values in sync with the constants above (lines 25, 31). */ rsocket: { - maxAttempts: 5, // RETRY_BACKOFF_DELAYS.length - baseDelay: 1000, // RETRY_BACKOFF_DELAYS[0] - maxDelay: 20000, // RETRY_BACKOFF_DELAYS[4] + maxAttempts: 5, // = RETRY_BACKOFF_DELAYS.length + baseDelay: 1000, // = RETRY_BACKOFF_DELAYS[0] + maxDelay: 20000, // = RETRY_BACKOFF_DELAYS[4] backoffStrategy: 'stepped' as const, - jitter: 0.15, // RETRY_JITTER_FACTOR + jitter: 0.15, // = RETRY_JITTER_FACTOR }, }