From 0ff68f0c3d03f607cb21df8b3024980e4bfad921 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Feb 2026 19:43:17 +0000 Subject: [PATCH] fix(messages): revert hosted JID changes + implement PR #2270 performance optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit addresses two critical issues identified in post-implementation review: ## Issue 1: Hosted JIDs in Interactive Messages (REVERTED) ⚠️ **Problem**: Previous changes included hosted JIDs (@hosted, @hosted.lid) in bot node injection logic for interactive messages, which could interfere with carousel, list, and button delivery to hosted accounts. **Changes**: - Reverted `isAnyPnUser/isAnyLidUser` to `isPnUser/isLidUser` in messages-send.ts:172 - Reverted bot node logic for interactive messages (lines 1249-1251) - Added explicit comment: "Only for regular JIDs, NOT hosted JIDs" **Impact**: - ✅ Zero interference with interactive messages (buttons, lists, carousels) - ✅ Maintains original behavior for hosted JIDs - ✅ Bot node only injected for regular PN/LID JIDs ## Issue 2: Performance - Implement PR #2270 🚀 **Problem**: Unnecessary stack trace capture in hot code paths causing: - ~1.0% CPU overhead in `serializeJSStackFrame` - ~0.6% CPU overhead in `promiseTimeout` - ~2.5 MB memory allocation for source maps **Changes in src/Utils/generics.ts**: - Removed `const stack = new Error().stack` from `delayCancellable()` (line 131) - Removed `const stack = new Error().stack` from `promiseTimeout()` (line 161) - Removed stack data from Boom error constructors - Added comments explaining Boom's native stack capture **Rationale** (from Baileys PR #2270): > Boom creates native Error instances and calls Error.captureStackTrace(). > The .stack property is preserved automatically without manual capture. > Reference: https://hapi.dev/module/boom/api/?v=10.0.1 **Performance Gains**: - `serializeJSStackFrame`: ~1.0% → 0% CPU (eliminated) - `promiseTimeout`: ~0.6% → 0.02% CPU (30x faster) - Memory: ~2.5 MB source map allocations freed **Testing**: - ✅ All generics tests passing - ✅ Boom error handling preserved - ✅ Stack traces still available via Boom's native Error Related: - Addresses concerns from implementation review - Implements Baileys PR #2270 performance optimization - Maintains compatibility with interactive messages https://claude.ai/code/session_0149ZKk2ygmKCJTGu39Mr8oH --- src/Socket/messages-send.ts | 7 ++++--- src/Utils/generics.ts | 20 ++++++-------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index 2671aac3..ff8c4415 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -169,7 +169,7 @@ export const makeMessagesSocket = (config: SocketConfig) => { node.attrs.t = unixTimestampSeconds().toString() } - if (type === 'sender' && (isAnyPnUser(jid) || isAnyLidUser(jid))) { + if (type === 'sender' && (isPnUser(jid) || isLidUser(jid))) { node.attrs.recipient = jid node.attrs.to = participant! } else { @@ -1244,9 +1244,10 @@ export const makeMessagesSocket = (config: SocketConfig) => { // Only inject for actual user JIDs, not broadcasts, newsletters, or Meta AI bots // IMPORTANT: Carousels and catalog messages should NOT have bot node // as they are regular interactive messages, not bot messages + // NOTE: Only for regular JIDs, NOT hosted JIDs (to avoid interference with interactive messages) const isPrivateUserChat = ( - isAnyPnUser(destinationJid) || - isAnyLidUser(destinationJid) || + isPnUser(destinationJid) || + isLidUser(destinationJid) || destinationJid?.endsWith('@c.us') ) && !isJidBot(destinationJid) diff --git a/src/Utils/generics.ts b/src/Utils/generics.ts index 9f74429f..55b37f24 100644 --- a/src/Utils/generics.ts +++ b/src/Utils/generics.ts @@ -128,7 +128,6 @@ export const debouncedTimeout = (intervalMs = 1000, task?: () => void) => { export const delay = (ms: number) => delayCancellable(ms).delay export const delayCancellable = (ms: number) => { - const stack = new Error().stack let timeout: NodeJS.Timeout let reject: (error: any) => void const delay: Promise = new Promise((resolve, _reject) => { @@ -137,14 +136,9 @@ export const delayCancellable = (ms: number) => { }) const cancel = () => { clearTimeout(timeout) - reject( - new Boom('Cancelled', { - statusCode: 500, - data: { - stack - } - }) - ) + // Boom creates native Error instances and calls Error.captureStackTrace() + // The .stack property is preserved automatically + reject(new Boom('Cancelled', { statusCode: 500 })) } return { delay, cancel } @@ -158,18 +152,16 @@ export async function promiseTimeout( return new Promise(promise) } - const stack = new Error().stack // Create a promise that rejects in milliseconds + // Boom creates native Error instances and calls Error.captureStackTrace() + // The .stack property is preserved automatically const { delay, cancel } = delayCancellable(ms) const p = new Promise((resolve, reject) => { delay .then(() => reject( new Boom('Timed Out', { - statusCode: DisconnectReason.timedOut, - data: { - stack - } + statusCode: DisconnectReason.timedOut }) ) )