Repository navigation
fix(sync): cache history senders and counts, show progress, count the last minute - #134
Merged
Merged
Conversation
neoromantic
force-pushed
the
fix/history-senders-progress
branch
from
October 30, 2026 16:10
50df704 to
cbdba15
Compare
…aemon's tg status took api_calls_last_minute from the current calendar-minute rate window, and the daemon's calls never reached api_activity. Calls now go through trackApiCall, which counts the call in its window and logs it to api_activity with its start in ms, outcome, Telegram's error code and duration; the daemon's history pages and contacts checks use it too. getStatus counts the last 60 seconds from api_activity, with an injectable clock. The prune cuts api_activity in ms and first turns the rows older versions wrote in seconds into ms. Fixes #127 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y progress Sync jobs stored only the messages of a getHistory page. They now cache the page's full (non-min) users before the messages, through the same helper as tg messages list, so the search index has the sender's name, and store the chat's message count on the server in chat_sync_state.total_messages (the newest count wins; a catch-up page bounded by minId gives none). tg status reports progress.history per account and in total: messages cached of those known, messages and chats left, the 5 pending chats with the most left, and a time left from the pace of the last 15 minutes of history pages, null without one. Fixes #126 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…calls in api_activity A complete chat that an older version synced gets no count: catch-up pages are bounded by minId. It now counts its cached messages as known instead of staying under chats_without_count, which keeps only pending chats. The multi-account status e2e tests seed their calls in api_activity, where tg status counts them since #127. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ting them A user record on a history page without an access hash (not min) replaced the cached row, erasing its username, access hash and contact flag, and the new messages lost the sender's username in the search index. Page users now go through UsersCache.mergeMany: a field the record lacks keeps its stored value, the access hash changes only when the record has one, the contact flag is only ever raised, and a record without an access hash keeps the stored raw_json. The rate-limiting docs say how a recent seconds row is left out of the last minute until the next prune converts it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The daemon logged only its sync pages and contacts checks: dialog passes, the joined-communities check, getMe at start, reconnect and health checks, and mtcute's own requests reached neither the rate windows nor api_activity, so tg status undercounted the last minute. A network middleware of the daemon's clients, inside mtcute's flood waiter, now counts and logs each request to the account's cache (start in ms, outcome, Telegram's error code, duration); a write that fails never fails the request. The sync jobs and the contacts check no longer track their calls themselves, so nothing is counted twice. Co-Authored-By: Claude Opus 5.5 <[email protected]>
neoromantic
force-pushed
the
fix/history-senders-progress
branch
from
October 30, 2026 16:27
cbdba15 to
bbacdf2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #126
Fixes #127
Problem
messages.getHistorypage: its senders never reachedusers_cache(search showed no sender name,--sendermissed them) and itscountwas dropped, sochat_sync_state.total_messagesstayed NULL andtg statuscouldn't say how much history is left.tg statusapi_calls_last_minutewas the current calendar-minute window, and the daemon never logged its calls toapi_activity; the CLI's rows hadtimestampin seconds although the docs say ms.Change
trackApiCall(src/utils/telegram-rate-limits.ts) counts a call in its rate window and logs it toapi_activity: start in ms, outcome, Telegram's error code, duration. The CLI wrapper, the daemon's history pages (contextdaemon:sync) and contacts checks (daemon:contacts) use it.RateLimitsService.getStatuscounts the last 60 seconds fromapi_activity; the service takes an injectable clock.api_activityin ms and first turns rows older versions wrote in seconds into ms (a value below 10^11 can only be seconds; a week of rows, found through the timestamp index, so one short update rather than a background migration).tg messages listuses, and store the chat's count intotal_messages(newest wins; a catch-up page bounded byminIdgives none; a whole-historymessages.messagesanswer counts its length). Chats a page names are not cached:chats_cacheis the account's own chat list, and the sender index reads onlyusers_cache.tg statusaddsprogress.history(and per account, without the list): messages cached of those known, messages and chats left, chats without a count, the 5 pending chats with the most left, andmessages_per_minute/eta_secondsmeasured from the history jobs completed in the last 15 minutes (null without two). Pretty output shows History Cached, History Left and the chats.cli-commands.md,database-schema.md,rate-limiting.md,sync-strategy.md, schema annotations, CHANGELOG.Tests
rate-limits.test.ts: the 60-second figure across a rate-window boundary, with an injected clock.sync-worker-real-jobs.test.ts: a page's users cached (min user skipped) and found by search on the sender's name,total_messagesset and kept newest, none from aminIdpage, whole-history length; a daemon call (success and RPC error) inapi_activityin ms with its duration.cache-maintenance.test.ts: seconds rows turned into ms before the prune;daemon-contacts.test.ts: the contacts call logged.history-progress.test.tsandstatus.e2e.test.ts: progress figures, pace and time left, JSON and pretty output.history-progress.test.tsalso covers a complete chat without a count (synced by an older version; catch-up pages give none): its cached messages count as known, andchats_without_countkeeps only pending chats.multi-account.e2e.test.tsseeds its calls inapi_activity, wheretg statuscounts them now.bun run check;bun run test2116 pass,bun run test:e2e358 pass.🤖 Generated with Claude Code