Compare commits

..

2 Commits

Author SHA1 Message Date
Renato Alcara 50226ae389 fix(decrypt): correct 1-based attempt check for unknown-error retry
Codex P2 review caught: `retry()` in retry-utils.ts iterates with
`for (let attempt = 1; ...)`, so the `attempt` passed to `shouldRetry` on
the first failure is 1, not 0. The previous `attempt < 1` was therefore
always false → no retry on unknown errors, contradicting the inline policy
comment ("one retry in case it was a transient blip").

Use `attempt < 2` so the SECOND pass happens (initial + 1 retry = 2 total
attempts, which matches `maxAttempts: 2`) and the third pass is refused.

The session-record / corrupted-session branches above already return false
unconditionally and are not affected.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 10:34:04 -03:00
Renato Alcara 56ca70bd75 perf(decrypt): fail-fast on session-record errors to clear pipeline backpressure
PRODUCTION BUG (still present after PR #396): inbound messages from the
smartphone take ~60s to surface in the consumer (zpro). PR #396 fixed the
PN/LID lock-vs-storage drift but did NOT clear the latency. Diff against the
known-good Pedro snapshot pinpointed the residual cause: this retry wrapper.

ROOT CAUSE:
`DECRYPTION_RETRY_OPTIONS.shouldRetry` retries 3× with exponential backoff
(200 ms → 400 ms → 800 ms ≈ 1.4 s per failed message) on
`'No matching sessions found'` and friends. After WhatsApp's LID/DSM rollout
own DSM messages flood in for sessions stored under the legacy `_1.0` format
that no longer matches the LID-addressed envelope, so EVERY DSM hits this
path. At ~30 DSM/min the accumulated backoff is ~42 s — enough to block
real-contact messages from reaching the buffer flush.

The Pedro snapshot (Feb 6 2026, 484 commits behind, confirmed fast in prod
on the same auth state) has NO retry wrapper at all: try once → fail → send
retry receipt → phone re-sends as `pkmsg` → fresh session → next message
decrypts cleanly. Total recovery ~300 ms, no pipeline backpressure.

WHY THE RETRIES WERE USELESS:
1. libsignal already scanned every stored session for the JID before throwing
   `No matching sessions found`. Re-running the same lookup 200 ms later
   gives the same answer — no new session record materialises in that window.
2. Bad MAC / counter errors mean the keys are simply wrong; retry doesn't
   regenerate keys. (This branch was already correctly returning false.)

THIS PATCH:
- `sessionRecordErrors` now also returns `false` from `shouldRetry` (matches
  the existing `corruptedSessionErrors` policy).
- Unknown errors retry exactly once (was twice) — quick blip recovery only.
- `maxAttempts` lowered to 2 to match.
- Big block comment captures the rationale so the next person doesn't add
  retries back hoping it'll help.

Recovery still happens — just upstream, via the retry-receipt → pkmsg flow,
which is what WhatsApp protocol intends and what Pedro's working snapshot
relies on.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 10:30:22 -03:00
16 changed files with 154 additions and 17 deletions
+6
View File
@@ -343,11 +343,13 @@ export const extractGroupMetadata = (result: BinaryNode) => {
let descId: string | undefined let descId: string | undefined
let descOwner: string | undefined let descOwner: string | undefined
let descOwnerPn: string | undefined let descOwnerPn: string | undefined
let descOwnerUsername: string | undefined
let descTime: number | undefined let descTime: number | undefined
if (descChild) { if (descChild) {
desc = getBinaryNodeChildString(descChild, 'body') desc = getBinaryNodeChildString(descChild, 'body')
descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined
descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined
descOwnerUsername = descChild.attrs.participant_username || undefined
descTime = +descChild.attrs.t! descTime = +descChild.attrs.t!
descId = descChild.attrs.id descId = descChild.attrs.id
} }
@@ -362,16 +364,19 @@ export const extractGroupMetadata = (result: BinaryNode) => {
subject: group.attrs.subject!, subject: group.attrs.subject!,
subjectOwner: group.attrs.s_o, subjectOwner: group.attrs.s_o,
subjectOwnerPn: group.attrs.s_o_pn, subjectOwnerPn: group.attrs.s_o_pn,
subjectOwnerUsername: group.attrs.s_o_username,
subjectTime: +(group.attrs.s_t ?? '0'), subjectTime: +(group.attrs.s_t ?? '0'),
size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length, size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
creation: +(group.attrs.creation ?? '0'), creation: +(group.attrs.creation ?? '0'),
owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined, owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined, ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
ownerUsername: group.attrs.creator_username || undefined,
owner_country_code: group.attrs.creator_country_code, owner_country_code: group.attrs.creator_country_code,
desc, desc,
descId, descId,
descOwner, descOwner,
descOwnerPn, descOwnerPn,
descOwnerUsername,
descTime, descTime,
linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined, linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
restrict: !!getBinaryNodeChild(group, 'locked'), restrict: !!getBinaryNodeChild(group, 'locked'),
@@ -386,6 +391,7 @@ export const extractGroupMetadata = (result: BinaryNode) => {
id: attrs.jid!, id: attrs.jid!,
phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined, phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined, lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
username: attrs.participant_username || attrs.username || undefined,
admin: (attrs.type || null) as GroupParticipant['admin'] admin: (attrs.type || null) as GroupParticipant['admin']
} }
}), }),
+5
View File
@@ -2240,6 +2240,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
fromMe, fromMe,
participant: node.attrs.participant, participant: node.attrs.participant,
participantAlt, participantAlt,
participantUsername: node.attrs.participant_username,
addressingMode, addressingMode,
id: node.attrs.id, id: node.attrs.id,
...(msg.key || {}) ...(msg.key || {})
@@ -2248,6 +2249,10 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
msg.messageTimestamp = +node.attrs.t! msg.messageTimestamp = +node.attrs.t!
const fullMsg = proto.WebMessageInfo.fromObject(msg) as WAMessage const fullMsg = proto.WebMessageInfo.fromObject(msg) as WAMessage
// Preserve custom WAMessageKey fields (participantAlt, participantUsername,
// addressingMode) that proto.WebMessageInfo.fromObject strips because
// they aren't part of the proto.MessageKey schema.
fullMsg.key = msg.key
await upsertMessage(fullMsg, 'append') await upsertMessage(fullMsg, 'append')
} }
}) })
+2
View File
@@ -9,6 +9,8 @@ export interface Contact {
name?: string name?: string
/** name of the contact, the contact has set on their own on WA */ /** name of the contact, the contact has set on their own on WA */
notify?: string notify?: string
/** username associated with this contact, when provided by WA */
username?: string
/** I have no idea */ /** I have no idea */
verifiedName?: string verifiedName?: string
// Baileys Added // Baileys Added
+2
View File
@@ -81,6 +81,7 @@ export type BaileysEventMap = {
id: string id: string
author: string author: string
authorPn?: string authorPn?: string
authorUsername?: string
participants: GroupParticipant[] participants: GroupParticipant[]
action: ParticipantAction action: ParticipantAction
} }
@@ -88,6 +89,7 @@ export type BaileysEventMap = {
id: string id: string
author: string author: string
authorPn?: string authorPn?: string
authorUsername?: string
participant: string participant: string
participantPn?: string participantPn?: string
action: RequestJoinAction action: RequestJoinAction
+4
View File
@@ -20,17 +20,20 @@ export interface GroupMetadata {
addressingMode?: WAMessageAddressingMode addressingMode?: WAMessageAddressingMode
owner: string | undefined owner: string | undefined
ownerPn?: string | undefined ownerPn?: string | undefined
ownerUsername?: string | undefined
owner_country_code?: string | undefined owner_country_code?: string | undefined
subject: string subject: string
/** group subject owner */ /** group subject owner */
subjectOwner?: string subjectOwner?: string
subjectOwnerPn?: string subjectOwnerPn?: string
subjectOwnerUsername?: string
/** group subject modification date */ /** group subject modification date */
subjectTime?: number subjectTime?: number
creation?: number creation?: number
desc?: string desc?: string
descOwner?: string descOwner?: string
descOwnerPn?: string descOwnerPn?: string
descOwnerUsername?: string
descId?: string descId?: string
descTime?: number descTime?: number
/** if this group is part of a community, it returns the jid of the community to which it belongs */ /** if this group is part of a community, it returns the jid of the community to which it belongs */
@@ -56,6 +59,7 @@ export interface GroupMetadata {
/** the person who added you to group or changed some setting in group */ /** the person who added you to group or changed some setting in group */
author?: string author?: string
authorPn?: string authorPn?: string
authorUsername?: string
} }
export interface WAGroupCreateResponse { export interface WAGroupCreateResponse {
+2
View File
@@ -19,7 +19,9 @@ export type WAContactMessage = proto.Message.IContactMessage
export type WAContactsArrayMessage = proto.Message.IContactsArrayMessage export type WAContactsArrayMessage = proto.Message.IContactsArrayMessage
export type WAMessageKey = proto.IMessageKey & { export type WAMessageKey = proto.IMessageKey & {
remoteJidAlt?: string remoteJidAlt?: string
remoteJidUsername?: string
participantAlt?: string participantAlt?: string
participantUsername?: string
server_id?: string server_id?: string
addressingMode?: string addressingMode?: string
isViewOnce?: boolean // TODO: remove out of the message key, place in WebMessageInfo isViewOnce?: boolean // TODO: remove out of the message key, place in WebMessageInfo
+1
View File
@@ -1083,6 +1083,7 @@ export const processSyncAction = (
action.lidContactAction.firstName || action.lidContactAction.firstName ||
action.lidContactAction.username || action.lidContactAction.username ||
undefined, undefined,
username: action.lidContactAction.username || undefined,
lid: id!, lid: id!,
phoneNumber: undefined phoneNumber: undefined
} }
+44 -12
View File
@@ -67,33 +67,61 @@ export const DECRYPTION_RETRY_CONFIG = {
} }
/** /**
* Retry options for decryption operations * Retry options for decryption operations.
* Uses exponential backoff with jitter to handle transient failures *
* IMPORTANT — fail-fast policy for decryption:
* Both `sessionRecordErrors` ('No matching sessions found', etc.) and
* `corruptedSessionErrors` ('Bad MAC', 'MessageCounterError', missing keys)
* return `false` from `shouldRetry`. Rationale:
*
* 1. libsignal already scanned ALL stored sessions for the JID before
* throwing — retrying immediately gives the SAME result (no new session
* record materialises in the 200-800ms backoff window).
* 2. The real recovery flow is upstream: failed decrypt → retry receipt to
* WA → phone re-sends as `pkmsg` → libsignal builds a fresh session →
* next message decrypts cleanly. That handshake takes ~300ms total.
* 3. Wrapping decrypt in 3 attempts × exponential backoff (200ms→400ms→800ms
* ≈ 1.4 s per failed message) just blocks the inbound buffer pipeline. At
* the rate own DSM messages flood in after a LID/PN mismatch, this
* compounds to tens of seconds of accumulated delay before live messages
* from real contacts can even reach the consumer.
*
* Only truly *unknown* errors get a single retry — those might be transient
* (network blip, unexpected exception) and a quick 200ms retry is cheap.
*
* If we ever need transient-error retries again (e.g. the storage layer adds
* an async race that benefits from re-reading), set `sessionRecordErrors` to
* `attempt < 1` here, NOT `attempt < 3` — one extra read at most.
*/ */
export const DECRYPTION_RETRY_OPTIONS: RetryOptions = { export const DECRYPTION_RETRY_OPTIONS: RetryOptions = {
maxAttempts: 3, maxAttempts: 2,
baseDelay: 200, // 200ms base delay baseDelay: 200, // 200ms base delay (only used for unknown errors below)
maxDelay: 2000, // 2s max delay maxDelay: 2000,
backoffStrategy: 'exponential', backoffStrategy: 'exponential',
backoffMultiplier: 2, backoffMultiplier: 2,
jitter: 0.2, // 20% jitter jitter: 0.2,
collectMetrics: false, // No Prometheus metrics collectMetrics: false,
operationName: 'message_decryption', operationName: 'message_decryption',
shouldRetry: (error: Error, attempt: number) => { shouldRetry: (error: Error, attempt: number) => {
const errorMsg = error?.message || '' const errorMsg = error?.message || ''
// Always retry on session record errors (session might be syncing) // Session record errors: libsignal already exhausted all stored sessions.
// Retrying immediately gives the same result; the real recovery path
// is the upstream retry-receipt → pkmsg flow. Fail fast.
if (DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some(err => errorMsg.includes(err))) { if (DECRYPTION_RETRY_CONFIG.sessionRecordErrors.some(err => errorMsg.includes(err))) {
return attempt < 3 // Retry up to 3 times return false
} }
// Don't retry on corrupted session errors (need cleanup first) // Corrupted session errors: Bad MAC / counter errors. Same reasoning —
// the keys are wrong and won't right themselves on retry.
if (DECRYPTION_RETRY_CONFIG.corruptedSessionErrors.some(err => errorMsg.includes(err))) { if (DECRYPTION_RETRY_CONFIG.corruptedSessionErrors.some(err => errorMsg.includes(err))) {
return false return false
} }
// Retry other transient errors // Unknown errors: one retry in case it was a transient blip.
return attempt < 2 // Retry up to 2 times for unknown errors // `attempt` is 1-based (retry-utils starts the loop at 1), so `attempt < 2`
// allows the second pass and returns false on the third.
return attempt < 2
} }
} }
@@ -246,10 +274,14 @@ export function decodeMessageNode(stanza: BinaryNode, meId: string, meLid: strin
const key: WAMessageKey = { const key: WAMessageKey = {
remoteJid: chatId, remoteJid: chatId,
remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined, remoteJidAlt: !isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
remoteJidUsername: !isJidGroup(chatId)
? stanza.attrs.peer_recipient_username || stanza.attrs.recipient_username
: undefined,
fromMe, fromMe,
id: msgId, id: msgId,
participant, participant,
participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined, participantAlt: isJidGroup(chatId) ? addressingContext.senderAlt : undefined,
participantUsername: stanza.attrs.participant ? stanza.attrs.participant_username : undefined,
addressingMode: addressingContext.addressingMode, addressingMode: addressingContext.addressingMode,
...(msgType === 'newsletter' && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {}) ...(msgType === 'newsletter' && stanza.attrs.server_id ? { server_id: stanza.attrs.server_id } : {})
} }
+1
View File
@@ -315,6 +315,7 @@ export const processHistoryMessage = (item: proto.IHistorySync, logger?: ILogger
contacts.push({ contacts.push({
id: chatId, id: chatId,
name: chat.displayName || chat.name || chat.username || undefined, name: chat.displayName || chat.name || chat.username || undefined,
username: chat.username || undefined,
lid: chat.lidJid || chat.accountLid || undefined, lid: chat.lidJid || chat.accountLid || undefined,
phoneNumber: chat.pnJid || undefined phoneNumber: chat.pnJid || undefined
}) })
+9 -1
View File
@@ -950,12 +950,19 @@ const processMessage = async (
id: jid, id: jid,
author: message.key.participant!, author: message.key.participant!,
authorPn: message.key.participantAlt!, authorPn: message.key.participantAlt!,
authorUsername: message.key.participantUsername!,
participants, participants,
action action
}) })
const emitGroupUpdate = (update: Partial<GroupMetadata>) => { const emitGroupUpdate = (update: Partial<GroupMetadata>) => {
ev.emit('groups.update', [ ev.emit('groups.update', [
{ id: jid, ...update, author: message.key.participant ?? undefined, authorPn: message.key.participantAlt } {
id: jid,
...update,
author: message.key.participant ?? undefined,
authorPn: message.key.participantAlt,
authorUsername: message.key.participantUsername
}
]) ])
} }
@@ -964,6 +971,7 @@ const processMessage = async (
id: jid, id: jid,
author: message.key.participant!, author: message.key.participant!,
authorPn: message.key.participantAlt!, authorPn: message.key.participantAlt!,
authorUsername: message.key.participantUsername!,
participant: participant.lid, participant: participant.lid,
participantPn: participant.pn, participantPn: participant.pn,
action, action,
+1
View File
@@ -46,6 +46,7 @@ export const processContactAction = (
{ {
id, id,
name: action.fullName || action.firstName || action.username || undefined, name: action.fullName || action.firstName || action.username || undefined,
username: action.username || undefined,
lid: lidJid || undefined, lid: lidJid || undefined,
phoneNumber phoneNumber
} }
+27 -1
View File
@@ -13,7 +13,7 @@ export class USyncContactProtocol implements USyncQueryProtocol {
} }
getUserElement(user: USyncUser): BinaryNode { getUserElement(user: USyncUser): BinaryNode {
//TODO: Implement type / username fields (not yet supported) if (user.phone) {
return { return {
tag: 'contact', tag: 'contact',
attrs: {}, attrs: {},
@@ -21,6 +21,32 @@ export class USyncContactProtocol implements USyncQueryProtocol {
} }
} }
if (user.username) {
return {
tag: 'contact',
attrs: {
username: user.username,
...(user.usernameKey ? { pin: user.usernameKey } : {}),
...(user.lid ? { lid: user.lid } : {})
}
}
}
if (user.type) {
return {
tag: 'contact',
attrs: {
type: user.type
}
}
}
return {
tag: 'contact',
attrs: {}
}
}
parser(node: BinaryNode): boolean { parser(node: BinaryNode): boolean {
if (node.tag === 'contact') { if (node.tag === 'contact') {
assertNodeErrorFree(node) assertNodeErrorFree(node)
@@ -0,0 +1,28 @@
import type { USyncQueryProtocol } from '../../Types/USync'
import { assertNodeErrorFree, type BinaryNode } from '../../WABinary'
import { USyncUser } from '../USyncUser'
export class USyncUsernameProtocol implements USyncQueryProtocol {
name = 'username'
getQueryElement(): BinaryNode {
return {
tag: 'username',
attrs: {}
}
}
getUserElement(user: USyncUser): BinaryNode | null {
void user
return null
}
parser(node: BinaryNode): string | null {
if (node.tag === 'username') {
assertNodeErrorFree(node)
return typeof node.content === 'string' ? node.content : null
}
return null
}
}
+1
View File
@@ -2,3 +2,4 @@ export * from './USyncDeviceProtocol'
export * from './USyncContactProtocol' export * from './USyncContactProtocol'
export * from './USyncStatusProtocol' export * from './USyncStatusProtocol'
export * from './USyncDisappearingModeProtocol' export * from './USyncDisappearingModeProtocol'
export * from './USyncUsernameProtocol'
+7 -1
View File
@@ -6,7 +6,8 @@ import {
USyncContactProtocol, USyncContactProtocol,
USyncDeviceProtocol, USyncDeviceProtocol,
USyncDisappearingModeProtocol, USyncDisappearingModeProtocol,
USyncStatusProtocol USyncStatusProtocol,
USyncUsernameProtocol
} from './Protocols' } from './Protocols'
import { USyncUser } from './USyncUser' import { USyncUser } from './USyncUser'
@@ -137,4 +138,9 @@ export class USyncQuery {
this.protocols.push(new USyncLIDProtocol()) this.protocols.push(new USyncLIDProtocol())
return this return this
} }
withUsernameProtocol() {
this.protocols.push(new USyncUsernameProtocol())
return this
}
} }
+12
View File
@@ -2,6 +2,8 @@ export class USyncUser {
id?: string id?: string
lid?: string lid?: string
phone?: string phone?: string
username?: string
usernameKey?: string
type?: string type?: string
personaId?: string personaId?: string
@@ -20,6 +22,16 @@ export class USyncUser {
return this return this
} }
withUsername(username: string) {
this.username = username
return this
}
withUsernameKey(usernameKey: string) {
this.usernameKey = usernameKey
return this
}
withType(type: string) { withType(type: string) {
this.type = type this.type = type
return this return this