Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): stop caching a transient LID lookup failure
Yudhi Armyndharisb1192b91505c
diff --git a/src/modules/session/session-lid-resolver.service.spec.ts b/src/modules/session/session-lid-resolver.service.spec.tsindex 432f01c25..8c18c320f 100644--- a/src/modules/session/session-lid-resolver.service.spec.ts+++ b/src/modules/session/session-lid-resolver.service.spec.ts@@ -53,6 +53,14 @@ describe('SessionLidResolver', () => {await expect(resolver.resolveSenderPhone('s1', '111@lid')).resolves.toBeNull();});+ it('does not cache a transient failure, so the sender resolves once the engine answers', async () => {+ resolveContactPhone.mockRejectedValueOnce(new Error('Evaluation failed')).mockResolvedValueOnce('628111');++ await expect(resolver.resolveSenderPhone('s1', '111@lid')).resolves.toBeNull();+ await expect(resolver.resolveSenderPhone('s1', '111@lid')).resolves.toBe('628111');+ expect(resolveContactPhone).toHaveBeenCalledTimes(2);+ });+it('returns null when the session has no live engine', async () => {await expect(resolver.resolveSenderPhone('not-started', '111@lid')).resolves.toBeNull();});diff --git a/src/modules/session/session-lid-resolver.service.ts b/src/modules/session/session-lid-resolver.service.tsindex 92e45b083..6c1d480a9 100644--- a/src/modules/session/session-lid-resolver.service.ts+++ b/src/modules/session/session-lid-resolver.service.ts@@ -55,14 +55,17 @@ export class SessionLidResolver {} catch {phone = null;}- // Bounded FIFO eviction: Map preserves insertion order, so the first key is the oldest.- if (this.cache.size >= SessionLidResolver.CACHE_MAX) {- for (const oldest of this.cache.keys()) {- this.cache.delete(oldest);- break;+ // Only a definitive answer is cached: a transient null would otherwise stick for the process life.+ if (resolved) {+ // Bounded FIFO eviction: Map preserves insertion order, so the first key is the oldest.+ if (this.cache.size >= SessionLidResolver.CACHE_MAX) {+ for (const oldest of this.cache.keys()) {+ this.cache.delete(oldest);+ break;+ }}+ this.cache.set(key, phone);}- this.cache.set(key, phone);// Persist the resolution so the read-path can bridge this contact's `@lid` and `@c.us` rows even// when the operator never sent to them (#583 R3 Phase 2). A definitive null is persisted too, so// a phone that later becomes hidden overwrites its stale mapping instead of being served forever