fix(history-sync): reset cumulative counters on new sync start and abort

Detect new sync runs by tracking lastProgress — when progress resets or
decreases, counters are zeroed before accumulating. This prevents stale
counts from aborted syncs leaking into subsequent runs.

Addresses Sourcery review feedback on PR #2442.
This commit is contained in:
Alexandre Reyes Martins
2026-02-24 14:06:14 +00:00
parent 1242baa5a4
commit 6f759443b0
@@ -252,10 +252,11 @@ export class BaileysStartupService extends ChannelStartupService {
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
private eventProcessingQueue: Promise<void> = Promise.resolve();
// Cumulative history sync counters (reset on sync completion)
// Cumulative history sync counters (reset on new sync or completion)
private historySyncMessageCount = 0;
private historySyncChatCount = 0;
private historySyncContactCount = 0;
private historySyncLastProgress = -1;
// Cache TTL constants (in seconds)
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
@@ -945,6 +946,14 @@ export class BaileysStartupService extends ChannelStartupService {
syncType?: proto.HistorySync.HistorySyncType;
}) => {
try {
// Reset counters when a new sync starts (progress resets or decreases)
if (progress <= this.historySyncLastProgress) {
this.historySyncMessageCount = 0;
this.historySyncChatCount = 0;
this.historySyncContactCount = 0;
}
this.historySyncLastProgress = progress ?? -1;
if (syncType === proto.HistorySync.HistorySyncType.ON_DEMAND) {
console.log('received on-demand history sync, messages=', messages);
}
@@ -1093,6 +1102,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.historySyncMessageCount = 0;
this.historySyncChatCount = 0;
this.historySyncContactCount = 0;
this.historySyncLastProgress = -1;
}
contacts = undefined;