From 22eda03eb25db1734a8671dad5e1fb9b06163493 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 24 Jan 2026 19:14:02 +0000 Subject: [PATCH] fix(unified-session): address code review feedback Fixes 3 issues identified in code review: 1. ENV VAR PRECEDENCE: Changed DEFAULT_CONNECTION_CONFIG to use undefined for enableUnifiedSession, allowing env var BAILEYS_UNIFIED_SESSION_ENABLED to take precedence. Priority is now: explicit config > env var > default (true) 2. SINGLE INITIALIZATION: UnifiedSessionManager is now created only once, after sendNode is defined. This avoids: - Duplicate circuit breaker instances - State reinitialization - Unnecessary overhead and logs 3. CONTINUOUS TIME SYNC: serverTimeOffset is now updated on every received frame that contains a 't' attribute, not just on CB:success. This keeps the offset accurate even during long-running connections. --- src/Defaults/index.ts | 4 +++- src/Socket/socket.ts | 36 ++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index a0a02d76..e5cc38fc 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -108,7 +108,9 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = { // + 10 buffer slots for high-load scenarios = 50 total maxSocketClientListeners: 50, // Unified session telemetry (reduces detection of unofficial clients) - enableUnifiedSession: true + // NOTE: undefined means "check env var first, then default to true" + // This allows BAILEYS_UNIFIED_SESSION_ENABLED env var to have precedence + enableUnifiedSession: undefined } export const MEDIA_PATH_MAP: { [T in MediaType]?: string } = { diff --git a/src/Socket/socket.ts b/src/Socket/socket.ts index 3d5f2a4c..570f6b68 100644 --- a/src/Socket/socket.ts +++ b/src/Socket/socket.ts @@ -94,9 +94,16 @@ export const makeSocket = (config: SocketConfig) => { queryCircuitBreaker: queryCircuitBreakerConfig, connectionCircuitBreaker: connectionCircuitBreakerConfig, preKeyCircuitBreaker: preKeyCircuitBreakerConfig, - enableUnifiedSession = shouldEnableUnifiedSession() + // If enableUnifiedSession is explicitly set (true/false), use it + // Otherwise (undefined), check env var, then default to true + enableUnifiedSession: enableUnifiedSessionConfig } = config + // Resolve enableUnifiedSession: explicit config > env var > default (true) + const enableUnifiedSession = enableUnifiedSessionConfig !== undefined + ? enableUnifiedSessionConfig + : shouldEnableUnifiedSession() + // Initialize circuit breakers if enabled let queryCircuitBreaker: CircuitBreaker | undefined let connectionCircuitBreaker: CircuitBreaker | undefined @@ -148,18 +155,8 @@ export const makeSocket = (config: SocketConfig) => { logger.info('Circuit breakers initialized for socket operations') } - // Initialize Unified Session Manager for telemetry - // Note: sendNode will be set after it's defined below + // Unified Session Manager will be initialized after sendNode is defined let unifiedSessionManager: UnifiedSessionManager | undefined - if (enableUnifiedSession) { - unifiedSessionManager = createUnifiedSessionManager({ - enabled: true, - logger, - enableCircuitBreaker - // sendNode will be configured after sendNode function is defined - }) - logger.info('Unified session manager initialized') - } const publicWAMBuffer = new BinaryInfo() @@ -240,19 +237,19 @@ export const makeSocket = (config: SocketConfig) => { return sendRawMessage(buff) } - // Configure unified session manager with sendNode now that it's defined - if (unifiedSessionManager) { - // Create a wrapper that matches the expected signature + // Initialize Unified Session Manager now that sendNode is defined + // (single initialization to avoid duplicating circuit breakers and state) + if (enableUnifiedSession) { const sendNodeForSession = async (node: BinaryNode): Promise => { await sendNode(node) } - // Recreate with proper sendNode unifiedSessionManager = createUnifiedSessionManager({ enabled: true, logger, enableCircuitBreaker, sendNode: sendNodeForSession }) + logger.info('Unified session manager initialized') } /** Send unified_session telemetry */ @@ -742,6 +739,13 @@ export const makeSocket = (config: SocketConfig) => { if (!(frame instanceof Uint8Array)) { const msgId = frame.attrs.id + // Update server time offset from any node with timestamp 't' + // This keeps the offset accurate even after long connections + const serverTime = extractServerTime(frame) + if (serverTime) { + unifiedSessionManager?.updateServerTimeOffset(serverTime) + } + if (logger.level === 'trace') { logger.trace({ xml: binaryNodeToString(frame), msg: 'recv xml' }) }