Skip to content

fix(daemon): fetch senders missing from the cache - #152

Merged
neoromantic merged 2 commits into
mainfrom
bb/fetch-senders-missing-from-the-cache-143-thr_9ahjhd7wr7
Nov 13, 2026
Merged

neoromantic merged 2 commits into
mainfrom
bb/fetch-senders-missing-from-the-cache-143-thr_9ahjhd7wr7

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

Fixes #143

Problem

A cached message whose sender has no users_cache row shows only the sender's ID, and sender:name misses it. Caches synced before 0.9.0 (#126) have many (about 3% of the messages of a 1.16M-message cache, from about 3,000 senders), and a live message still leaves one when the sender arrives as a "min" user.

Change

  • New table sender_lookups (src/db/sender-lookups.ts): each user with cached messages and no users_cache row, with their newest cached message Telegram can be asked for (not deleted; private chat, basic group, or a channel/supergroup with a cached access hash). The scheduler fills it on start and once a day. The query walks idx_messages_cache_from from one sender to the next (recursive CTE): about 6 ms for 40,000 senders of 1.16M messages, against 200 ms for a DISTINCT over the messages.
  • New job type fetch_senders, one per chat with senders waiting, priority 4, in any chat mode (kept by tg chats sync and the startup cleanup, like refetch_messages). It asks for up to 100 of those messages by ID (channels.getMessages / messages.getMessages, the existing fetchMessagesByIdRaw) and caches the full users of the reply (cachePageUsers); the users_cache insert re-indexes their messages through the Search: re-index older messages after a person or chat is renamed #93 queue. Messages aren't rewritten.
  • A fourth lane, senders, with no slot in the cycle: it only gets a slot when catch-up, first pages and history have nothing due, at the calm pace (a job every 3 s).
  • Senders Telegram doesn't return (deleted message, left/private chat, a "min" record), and the senders of a chat that fails for good, get asked_at; the daily scan lists them again 30 days later with their newest message then.
  • tg status: progress.senders { missing, not_found }, in total and per account; pretty output shows a Senders Missing row (and N senders missing in the per-account line with several accounts).
  • Channels posting as themselves are not fetched or counted: chats_cache keeps only the account's own chats (sync-strategy.md).
  • Docs: daemon.md (Missing senders), sync-strategy.md, database-schema.md, cli-commands.md, rate-limiting.md, schema annotations, CHANGELOG.

Why by ID

Made-up cache shaped like the issue (1.16M messages; 2,960 senders without a row, 37,193 messages = 3.2%, in 341 of 400 supergroups): 349 calls, 0.12 per missing sender, about 17 minutes at the calm pace. One getHistory page around each sender's message would take 588 calls even with this generator's clustered messages, and about one per sender when they are spread. users.getUsers needs an access hash the cache doesn't have.

Tests

  • src/__tests__/fetch-senders.test.ts (fake client): what the scan lists and skips; a job caches the sender and sender:alicia then finds all three older messages after the re-index queue drains; messages.getMessages for a basic group; 30-day retry with the newest message; CHANNEL_PRIVATE marks the senders asked; a retried error keeps them waiting; 100 per request; one job per chat, picked only after history, calm pace, kept when the chat is turned off; scan on start and once a day.
  • src/__e2e__/status.e2e.test.ts: progress.senders in JSON (total and per account) and the pretty row.
  • bun run check; targeted unit/e2e suites (478 pass). No live Telegram in this change.

🤖 Generated with Claude Code

neoromantic and others added 2 commits November 13, 2026 19:54
Messages whose sender has no users_cache row (history synced before
0.9.0, live messages from "min" users) show only the sender's ID and
`sender:name` misses them. The scheduler now lists such senders in
sender_lookups on start and once a day (an index walk, ~6 ms on 1.16M
messages), and a fetch_senders job per chat asks Telegram for one
message of each of up to 100 senders by ID; the reply caches the
senders, which re-indexes their messages. The jobs run in a fourth lane
only when catch-up, first pages and history are empty. A sender
Telegram doesn't return is asked again after 30 days. tg status shows
progress.senders { missing, not_found } in total and per account.

Fixes #143

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ers cached since

History is written newest first, so the row cached last was often the
oldest message; if Telegram had deleted it, the sender stayed missing
after every retry. A chat whose waiting senders were cached by another
path no longer gets an empty job every refresh.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit 3a5f82f into main Nov 13, 2026
5 checks passed
@neoromantic
neoromantic deleted the bb/fetch-senders-missing-from-the-cache-143-thr_9ahjhd7wr7 branch November 13, 2026 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fetch senders missing from the cache

1 participant