Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): stop a history backfill once its engine is retired
Yudhi Armyndharis4c250425668c
diff --git a/src/modules/session/message-history-projector.ts b/src/modules/session/message-history-projector.tsindex b351d5e96..8fe94e426 100644--- a/src/modules/session/message-history-projector.ts+++ b/src/modules/session/message-history-projector.ts@@ -17,6 +17,7 @@ export async function persistHistoryMessages(id: string,messages: IncomingMessage[],logger: LoggerService,+ isLive: () => boolean,): Promise<void> {const storeEphemeralMessages = resolveFeatureFlags(configService).storeEphemeralMessages;const byId = new Map<string, IncomingMessage>();@@ -46,6 +47,10 @@ export async function persistHistoryMessages(where: { sessionId: id, waMessageId: In(chunkIds) },select: { waMessageId: true },});+ // A delete() can retire the engine while a large batch works through its chunks; its transaction+ // has then already cleared this session's messages, and a row inserted after it (no FK) would+ // never be reaped. Same re-check the live inbound path makes after its awaits.+ if (!isLive()) break;const seen = new Set(existing.map(r => r.waMessageId));const rows = chunkIds.filter(x => !seen.has(x))diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.tsindex 0b325b8d6..b208732b9 100644--- a/src/modules/session/message-projector.service.spec.ts+++ b/src/modules/session/message-projector.service.spec.ts@@ -245,7 +245,7 @@ describe('MessageProjector', () => {describe('persistHistoryMessages', () => {it('skips rows that cannot become a valid message row, and queries nothing when none survive', async () => {- await projector.persistHistoryMessages('s1', [+ await projector.persistHistoryMessages('s1', engine, [historyMessage({ id: '' }), // no id -> cannot de-duphistoryMessage({ isStatusBroadcast: true }), // a story, not a chathistoryMessage({ chatId: '' }), // chatId is NOT NULL@@ -261,7 +261,7 @@ describe('MessageProjector', () => {// filter rather than dragging the insert builder into a test about which rows qualify.messageRepository.find.mockResolvedValue([{ waMessageId: 'GOOD' }]);- await projector.persistHistoryMessages('s1', [+ await projector.persistHistoryMessages('s1', engine, [historyMessage({ id: 'GOOD' }),historyMessage({ id: 'BAD', from: '' }),]);@@ -273,7 +273,10 @@ describe('MessageProjector', () => {it('de-duplicates repeated ids within one batch', async () => {messageRepository.find.mockResolvedValue([{ waMessageId: 'DUP' }]);- await projector.persistHistoryMessages('s1', [historyMessage({ id: 'DUP' }), historyMessage({ id: 'DUP' })]);+ await projector.persistHistoryMessages('s1', engine, [+ historyMessage({ id: 'DUP' }),+ historyMessage({ id: 'DUP' }),+ ]);expect(dedupIds(messageRepository.find)).toEqual(['DUP']);});diff --git a/src/modules/session/message-projector.service.ts b/src/modules/session/message-projector.service.tsindex 08fccbd4f..299133f97 100644--- a/src/modules/session/message-projector.service.ts+++ b/src/modules/session/message-projector.service.ts@@ -633,8 +633,10 @@ export class MessageProjector {}/** History backfill persist, extracted to message-history-projector.ts (stateless function). */- persistHistoryMessages(id: string, messages: IncomingMessage[]): Promise<void> {- return persistHistoryMessages(this.messageRepository, this.configService, id, messages, this.logger);+ persistHistoryMessages(id: string, engine: IWhatsAppEngine, messages: IncomingMessage[]): Promise<void> {+ return persistHistoryMessages(this.messageRepository, this.configService, id, messages, this.logger, () =>+ this.engines.isLive(id, engine),+ );}/** Reaction apply, queued on the per-message mutation chain — see MessageMutationProjector. */diff --git a/src/modules/session/session-engine-event-wiring.ts b/src/modules/session/session-engine-event-wiring.tsindex ed771c175..fba34f34d 100644--- a/src/modules/session/session-engine-event-wiring.ts+++ b/src/modules/session/session-engine-event-wiring.ts@@ -227,7 +227,7 @@ export class SessionEngineEventWiring {if (!host.isLiveEngine(id, engine)) return;// Persist for the chat view only; no dispatch (these predate the live session).void host.messages- .persistHistoryMessages(id, messages)+ .persistHistoryMessages(id, engine, messages).catch(err => this.logger.error(`Failed to persist history messages for ${id}`, String(err)));},onMessageCreate: (message): void => host.messages.handleOwnSendEcho(id, engine, message),diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex 8c56e3fdc..403591a45 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -5919,6 +5919,43 @@ describe('SessionService', () => {expect(messageRepository.save).not.toHaveBeenCalled(); // no longer the throwing path});+ it('stops a history backfill whose engine retired mid-batch, so a session delete leaves no orphan rows', async () => {+ const callbacks = await startAndCaptureCallbacks();+ const execute = jest.fn().mockResolvedValue({ identifiers: [] });+ const qb = {+ insert: jest.fn().mockReturnThis(),+ values: jest.fn().mockReturnThis(),+ orIgnore: jest.fn().mockReturnThis(),+ execute,+ };+ (messageRepository.createQueryBuilder as jest.Mock) = jest.fn().mockReturnValue(qb);+ (messageRepository.create as jest.Mock).mockImplementation((data: Record<string, unknown>) => ({ ...data }));+ // delete() tears the engine down while the de-dup query is in flight.+ (messageRepository.find as jest.Mock).mockImplementation(() => {+ registry.delete('sess-uuid-1');+ return Promise.resolve([]);+ });++ callbacks.onHistoryMessages?.([+ {+ id: 'h1',+ from: '[email protected]',+ to: '[email protected]',+ chatId: '[email protected]',+ body: 'old',+ type: 'text',+ timestamp: 1,+ fromMe: false,+ isGroup: false,+ kind: 'individual',+ },+ ]);+ await flush();++ expect(messageRepository.find).toHaveBeenCalled();+ expect(execute).not.toHaveBeenCalled();+ });+it('persists author only for inbound history rows, never for the account’s own (fromMe) posts', async () => {// The Baileys history sync includes the account's own group messages (with author = self);// those must land with author NULL to keep the column's "null on outgoing" contract.