From 49df322b19c2bec16aa3395fc0f73c529e631bb5 Mon Sep 17 00:00:00 2001 From: Vlad Ra Date: Wed, 18 Mar 2026 19:15:16 +0000 Subject: [PATCH] fix: use consecutive-old-counter for telegram dialog cutoff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit iter_dialogs() doesn't sort strictly by last_message_date desc — there's a blurred zone where old and new chats are mixed. Replace the immediate break with a consecutive-old-counter (threshold=100) so we skip interleaved old dialogs and only stop after 100 consecutive old ones. Fixes #15 Co-Authored-By: Claude Opus 4.6 (1M context) --- scripts/tg_sync.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/scripts/tg_sync.py b/scripts/tg_sync.py index 549a2b0..e5c7b7d 100644 --- a/scripts/tg_sync.py +++ b/scripts/tg_sync.py @@ -163,12 +163,19 @@ async def cmd_list(limit: int, since: str = None): sys.stderr.flush() chats = [] count = 0 + consecutive_old = 0 + CONSECUTIVE_OLD_THRESHOLD = 100 # Telethon pages internally in 100s async for d in client.iter_dialogs(limit=limit): count += 1 if cutoff and d.date and d.date < cutoff: - sys.stderr.write(f" Stopped at {count} dialogs (reached cutoff {since})\n") - sys.stderr.flush() - break + consecutive_old += 1 + if consecutive_old >= CONSECUTIVE_OLD_THRESHOLD: + sys.stderr.write(f" Stopped at {count} dialogs ({CONSECUTIVE_OLD_THRESHOLD} consecutive old, cutoff {since})\n") + sys.stderr.flush() + break + continue # skip this old dialog but keep scanning + else: + consecutive_old = 0 # reset counter on any new dialog if count % 100 == 0: sys.stderr.write(f" {count} dialogs...\n") sys.stderr.flush()