From a6cf9b0b26cbbfb2fe1b59b9e3b05d26d19de4d0 Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Sun, 26 Apr 2026 22:48:23 -0300 Subject: [PATCH] refactor(signal): replace circuit-breaker with bounded-retry in libsignal wrapper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The preKeyCircuitBreaker was passed into signalStorage() as an optional parameter and was used in only ONE place: .reset() after detecting an identity-key change (line 391, the 'contact may have reinstalled WhatsApp' recovery path). Bounded-retry is stateless — each retry is independent — so the equivalent of "forget the past failures" happens automatically on the next operation. There is no state to reset. Changes: - Drop CircuitBreaker import. - Remove preKeyCircuitBreaker field from LibSignalRepositoryOptions. - Remove preKeyCircuitBreaker parameter from signalStorage(). - Remove the .reset() call in the identity-key-change recovery path (replaced with explanatory comment). Build passes. --- src/Signal/libsignal.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/Signal/libsignal.ts b/src/Signal/libsignal.ts index 8ef48a8f..26b6e8e9 100644 --- a/src/Signal/libsignal.ts +++ b/src/Signal/libsignal.ts @@ -6,7 +6,6 @@ import type { LIDMapping, SignalAuthState, SignalKeyStoreWithTransaction } from import type { BaileysEventEmitter } from '../Types/Events' import type { SignalRepositoryWithLIDStore } from '../Types/Signal' import { generateSignalPubKey } from '../Utils' -import { CircuitBreaker } from '../Utils/circuit-breaker.js' import type { ILogger } from '../Utils/logger' import { metrics } from '../Utils/prometheus-metrics.js' import { isAnyLidUser, isAnyPnUser, jidDecode, transferDevice, WAJIDDomains } from '../WABinary' @@ -67,8 +66,6 @@ export interface IdentitySaveResult { export interface LibSignalRepositoryOptions { /** Event emitter for broadcasting identity changes */ ev?: BaileysEventEmitter - /** Circuit breaker for prekey operations (optional) */ - preKeyCircuitBreaker?: CircuitBreaker } // ============================================ @@ -256,7 +253,7 @@ export function makeLibSignalRepository( pnToLIDFunc?: (jids: string[]) => Promise, options?: LibSignalRepositoryOptions ): SignalRepositoryWithLIDStore { - const { ev, preKeyCircuitBreaker } = options || {} + const { ev } = options || {} const lidMapping = new LIDMappingStore(auth.keys as SignalKeyStoreWithTransaction, logger, pnToLIDFunc) // Identity key cache to avoid repeated storage reads @@ -277,7 +274,7 @@ export function makeLibSignalRepository( cacheMetricsInterval.unref() } - const storage = signalStorage(auth, lidMapping, identityKeyCache, ev, preKeyCircuitBreaker, logger) + const storage = signalStorage(auth, lidMapping, identityKeyCache, ev, logger) const parsedKeys = auth.keys as SignalKeyStoreWithTransaction const migratedSessionCache = new LRUCache({ @@ -385,13 +382,9 @@ export function makeLibSignalRepository( 'Identity key changed - contact may have reinstalled WhatsApp, session will be re-established' ) - // Reset prekey circuit breaker since we identified the cause - // Reset regardless of state (could be open, half-open, or closed with accumulated failures) - // eslint-disable-next-line max-depth - if (preKeyCircuitBreaker) { - preKeyCircuitBreaker.reset() - logger.debug({ jid }, 'Reset prekey circuit breaker after identity key change detection') - } + // (No circuit breaker to reset — bounded-retry is stateless, + // each retry is independent so identity-change recovery + // happens automatically on the next operation.) } else if (saveResult.isNew) { logger.debug( { jid, addr: addrStr, fingerprint: saveResult.currentFingerprint }, @@ -769,7 +762,6 @@ function signalStorage( lidMapping: LIDMappingStore, identityKeyCache: LRUCache, ev?: BaileysEventEmitter, - preKeyCircuitBreaker?: CircuitBreaker, logger?: ILogger ): ExtendedSignalStorage { // Shared function to resolve PN signal address to LID if mapping exists