Skip to content

fix(sync): cache history senders and counts, show progress, count the last minute - #134

Merged
neoromantic merged 5 commits into
mainfrom
fix/history-senders-progress
Oct 30, 2026
Merged

neoromantic merged 5 commits into
mainfrom
fix/history-senders-progress

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Fixes #126
Fixes #127

Problem

  • Sync jobs stored only the messages of a messages.getHistory page: its senders never reached users_cache (search showed no sender name, --sender missed them) and its count was dropped, so chat_sync_state.total_messages stayed NULL and tg status couldn't say how much history is left.
  • tg status api_calls_last_minute was the current calendar-minute window, and the daemon never logged its calls to api_activity; the CLI's rows had timestamp in seconds although the docs say ms.

Change

  • trackApiCall (src/utils/telegram-rate-limits.ts) counts a call in its rate window and logs it to api_activity: start in ms, outcome, Telegram's error code, duration. The CLI wrapper, the daemon's history pages (context daemon:sync) and contacts checks (daemon:contacts) use it.
  • RateLimitsService.getStatus counts the last 60 seconds from api_activity; the service takes an injectable clock.
  • The prune cuts api_activity in 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).
  • Sync jobs cache the page's full (non-min) users before the messages, through the helper tg messages list uses, and store the chat's count in total_messages (newest wins; a catch-up page bounded by minId gives none; a whole-history messages.messages answer counts its length). Chats a page names are not cached: chats_cache is the account's own chat list, and the sender index reads only users_cache.
  • tg status adds progress.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, and messages_per_minute/eta_seconds measured from the history jobs completed in the last 15 minutes (null without two). Pretty output shows History Cached, History Left and the chats.
  • Docs: 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_messages set and kept newest, none from a minId page, whole-history length; a daemon call (success and RPC error) in api_activity in 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.ts and status.e2e.test.ts: progress figures, pace and time left, JSON and pretty output.
  • history-progress.test.ts also covers a complete chat without a count (synced by an older version; catch-up pages give none): its cached messages count as known, and chats_without_count keeps only pending chats.
  • multi-account.e2e.test.ts seeds its calls in api_activity, where tg status counts them now.
  • bun run check; bun run test 2116 pass, bun run test:e2e 358 pass.

🤖 Generated with Claude Code

@neoromantic
neoromantic force-pushed the fix/history-senders-progress branch from 50df704 to cbdba15 Compare October 30, 2026 16:10
neoromantic and others added 5 commits October 30, 2026 20:20
…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
neoromantic force-pushed the fix/history-senders-progress branch from cbdba15 to bbacdf2 Compare October 30, 2026 16:27
@neoromantic
neoromantic merged commit f676b6d into main Oct 30, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/history-senders-progress branch October 30, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant