Repository navigation
fix: give history messages without fromId the sender live updates store (#44) - #57
Merged
Merged
Conversation
Telegram leaves fromId out of an incoming private message, a channel post and a message in Saved Messages. Live updates store mtcute's msg.sender.id, which falls back to the chat (peerId) in that case, but the raw-message mapper behind history pages, tg messages list/get, the daemon's initial load, catch-up and backfill, and tg send's full-updates path read only fromId and stored NULL. So a private message found in search had no sender name, --sender missed it, and a catch-up or a --fresh page overwrote the sender a live update had stored. toSenderIdFromRawMessage in telegram-peer-ids.ts applies the same rule (fromId, else peerId), and both raw parsers use it. A message that names neither still gets NULL. A new test feeds real mtcute Message objects through the live handler and the same raw messages through cacheHistoryResponse and checks that both rows match msg.sender.id. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit stops new NULL senders, but caches written by 0.3.0
and older still hold NULL in messages_cache.from_id for every incoming
private message, channel post and Saved Messages message that came from
a history page. Search shows them without a name and --sender misses
them until each one is fetched again.
Step 3 of the peer ID migration (user_version 3) gives the rows with a
NULL sender, no fromId and a known peerId the chat as the sender by the
same rule. The update trigger re-indexes each row. Rows whose raw_json
is '{}' or malformed stay NULL. It runs once, in the existing
transaction, because a NULL scan can't use the partial from_id index.
Those rows can be most of a cache, since private chats get full
history, so the step reads only fromId and peerId out of raw_json.
Loading every whole raw_json at once inside the write lock could run a
small host out of memory, and a killed process would roll back and die
the same way on every later open. json_quote keeps a fromId that is not
an object parseable, so it can't throw either. On 100k synthetic
messages with 1.1 KB of raw_json each, all to fill, the first open
takes about 7 s (1.3 s scan, 1.1 s update, the rest the search
re-index) and peaks at 125 MB RSS, against 375 MB when it read whole
rows; with nothing to fill it takes about 80 ms.
On a big cache that holds the write lock longer than the 5 s an older
daemon waits, so the docs say to stop the daemon before upgrading.
It is a separate commit so it can be dropped on its own.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
neoromantic
force-pushed
the
fix/history-sender-44
branch
from
October 21, 2026 22:00
fa1f7dd to
82c9361
Compare
The second initSyncSchema in the #44 migration test found user_version 3 and no bare chat rows, so migratePeerIds returned before the `version < 3` guard and the test passed even with the guard removed. A 0.2.0 daemon that keeps running after the upgrade writes bare chat rows, which reruns the migration on every open; the guard is what keeps that from scanning every message's raw_json each time. The test now writes such a row too, checks it was re-keyed, and checks that a message cached since keeps a NULL sender. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
What and why
Fixes #44.
Telegram leaves
fromIdout of an incoming private message, a channel post and a message in Saved Messages. Live updates store mtcute'smsg.sender.id, which in that case falls back to the chat:fromId, elsepeerId. The raw-message mapper behind history pages read onlyfromIdand storedNULL. That mapper also servestg messages list/get(including--fresh), the daemon's initial load, catch-up and backfill, andtg send's full-updates path. As a result:tg messages searchhad no sender name, and--sendermissed it;--freshpage overwrote the sender a live update had stored withNULL, because the upsert replacesfrom_id.Commit 1 adds
toSenderIdFromRawMessagetosrc/utils/telegram-peer-ids.ts, which applies mtcute'sMessage.senderrule, and both raw parsers now use it. The result is the other user for an incoming private message, your own user in Saved Messages, the channel's marked ID (-100…) for a channel post and-Nfor a basic-group service message. A message that names neitherfromIdnorpeerIdstill getsNULL. The live path indaemon-accounts.tsis unchanged.Commit 2 is a one-time backfill that can be dropped on its own. It adds step 3 of the peer ID migration (
user_version3), which runs in the existingBEGIN IMMEDIATEtransaction. A row with aNULLsender, nofromIdinraw_jsonand a knownpeerIdgets the chat as the sender, and the update trigger re-indexes it. Rows whoseraw_jsonis{}or malformed stayNULL. It runs once, because aNULLscan can't use the partialfrom_idindex.These rows can be most of a cache (private chats get full history), so the step reads only
fromIdandpeerIdout ofraw_jsonwithjson_extract, never whole rows: an out-of-memory kill inside the write lock would roll back and repeat on every later open.json_quotekeeps afromIdthat is not an object parseable. Measured cost on 100k synthetic messages with 1.1 KB ofraw_jsoneach, all to fill:On a big cache the fill holds the write lock longer than the 5 s a 0.3.0 daemon waits, and that daemon keeps storing
NULLsenders until it restarts; those rows heal when the message is fetched again.docs/database-schema.mdsays so and tells users to stop the daemon before upgrading and start it after; the CHANGELOG entry for the release PR says the same.How I checked it
daemon-accounts.connect.test.ts: real mtcuteMessageobjects go throughonNewMessage, and the same raw messages go through the realcacheHistoryResponse. For an incoming DM, a channel post, Saved Messages, an outgoing DM and a basic-group service message, the livefrom_id, the historyfrom_idandmsg.sender.idare equal.messages-history-command.test.ts: a history DM withoutfromIdis found bysearch('Alice')withfrom_idandsender_first_name.messages.e2e.test.ts: the DM row is seeded withoutfromId. The realtg messages searchgives both Alice hits her name, and the channel hit has the channel asfromId.sync-worker-real-helpers.test.ts,message-parser.test.ts,send-cache.test.tsandsql-commands.test.tsare updated to the new rule and annotation.peer-id-migration.test.ts: step 3 fills the DM, channel post and Saved Messages rows. It leaves alone the rows with a sender, with{}or malformedraw_json, with an unknownfromIdtype, or with afromIdthat is a string (withoutjson_quote,JSON.parsewould throw on it).chats_cachestays byte-identical, search finds the DM by name, and aNULLrow added later staysNULL.The integration suite asserts
fromIdequals the own user on the Saved Messages send andlist --freshpaths. I didn't run it for this PR.Without the
src/utilsandsrc/daemonchanges, the parity, history-DM and e2e tests fail, and the migration test fails without step 3.bun run check,bun run test(1559 pass) andbun run test:e2e(193 pass) are clean.Tests pass locally and use made-up data only
README, docs and affected demos follow the product content standard
Title follows the repo's commit style: Conventional Commits (
feat: …,fix: …) unlessAGENTS.mdsays otherwiseThe CHANGELOG entry is left to the 0.3.1 release PR.
🤖 Generated with Claude Code