Skip to content

Commit 6f75944

Browse files
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.
1 parent 1242baa commit 6f75944

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,11 @@ export class BaileysStartupService extends ChannelStartupService {
252252
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
253253
private eventProcessingQueue: Promise<void> = Promise.resolve();
254254

255-
// Cumulative history sync counters (reset on sync completion)
255+
// Cumulative history sync counters (reset on new sync or completion)
256256
private historySyncMessageCount = 0;
257257
private historySyncChatCount = 0;
258258
private historySyncContactCount = 0;
259+
private historySyncLastProgress = -1;
259260

260261
// Cache TTL constants (in seconds)
261262
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
@@ -945,6 +946,14 @@ export class BaileysStartupService extends ChannelStartupService {
945946
syncType?: proto.HistorySync.HistorySyncType;
946947
}) => {
947948
try {
949+
// Reset counters when a new sync starts (progress resets or decreases)
950+
if (progress <= this.historySyncLastProgress) {
951+
this.historySyncMessageCount = 0;
952+
this.historySyncChatCount = 0;
953+
this.historySyncContactCount = 0;
954+
}
955+
this.historySyncLastProgress = progress ?? -1;
956+
948957
if (syncType === proto.HistorySync.HistorySyncType.ON_DEMAND) {
949958
console.log('received on-demand history sync, messages=', messages);
950959
}
@@ -1093,6 +1102,7 @@ export class BaileysStartupService extends ChannelStartupService {
10931102
this.historySyncMessageCount = 0;
10941103
this.historySyncChatCount = 0;
10951104
this.historySyncContactCount = 0;
1105+
this.historySyncLastProgress = -1;
10961106
}
10971107

10981108
contacts = undefined;

0 commit comments

Comments
 (0)