Skip to content

fix: give history messages without fromId the sender live updates store (#44) - #57

Merged
neoromantic merged 3 commits into
mainfrom
fix/history-sender-44
Oct 21, 2026
Merged

neoromantic merged 3 commits into
mainfrom
fix/history-sender-44

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

What and why

Fixes #44.

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 in that case falls back to the chat: fromId, else peerId. The raw-message mapper behind history pages read only fromId and stored NULL. That mapper also serves tg messages list/get (including --fresh), the daemon's initial load, catch-up and backfill, and tg send's full-updates path. As a result:

  • a private message found by tg messages search had no sender name, and --sender missed it;
  • a daemon catch-up or a --fresh page overwrote the sender a live update had stored with NULL, because the upsert replaces from_id.

Commit 1 adds toSenderIdFromRawMessage to src/utils/telegram-peer-ids.ts, which applies mtcute's Message.sender rule, 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 -N for a basic-group service message. A message that names neither fromId nor peerId still gets NULL. The live path in daemon-accounts.ts is 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_version 3), which runs in the existing BEGIN IMMEDIATE transaction. A row with a NULL sender, no fromId in raw_json and a known peerId gets the chat as the sender, and the update trigger re-indexes it. Rows whose raw_json is {} or malformed stay NULL. It runs once, because a NULL scan can't use the partial from_id index.

These rows can be most of a cache (private chats get full history), so the step reads only fromId and peerId out of raw_json with json_extract, never whole rows: an out-of-memory kill inside the write lock would roll back and repeat on every later open. json_quote keeps a fromId that is not an object parseable. Measured cost on 100k synthetic messages with 1.1 KB of raw_json each, all to fill:

  • first open: about 7 s (1.3 s scan, 1.1 s update, the rest the search re-index), peak RSS 125 MB, against 375 MB when it read whole rows;
  • nothing to fill: about 80 ms;
  • later opens: none.

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 NULL senders until it restarts; those rows heal when the message is fetched again. docs/database-schema.md says 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 mtcute Message objects go through onNewMessage, and the same raw messages go through the real cacheHistoryResponse. For an incoming DM, a channel post, Saved Messages, an outgoing DM and a basic-group service message, the live from_id, the history from_id and msg.sender.id are equal.

  • messages-history-command.test.ts: a history DM without fromId is found by search('Alice') with from_id and sender_first_name.

  • messages.e2e.test.ts: the DM row is seeded without fromId. The real tg messages search gives both Alice hits her name, and the channel hit has the channel as fromId.

  • sync-worker-real-helpers.test.ts, message-parser.test.ts, send-cache.test.ts and sql-commands.test.ts are 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 malformed raw_json, with an unknown fromId type, or with a fromId that is a string (without json_quote, JSON.parse would throw on it). chats_cache stays byte-identical, search finds the DM by name, and a NULL row added later stays NULL.

  • The integration suite asserts fromId equals the own user on the Saved Messages send and list --fresh paths. I didn't run it for this PR.

  • Without the src/utils and src/daemon changes, the parity, history-DM and e2e tests fail, and the migration test fails without step 3.

  • bun run check, bun run test (1559 pass) and bun 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: …) unless AGENTS.md says otherwise

The CHANGELOG entry is left to the 0.3.1 release PR.

🤖 Generated with Claude Code

neoromantic and others added 2 commits October 22, 2026 01:30
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
neoromantic force-pushed the fix/history-sender-44 branch from fa1f7dd to 82c9361 Compare October 21, 2026 22:00
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]>
@neoromantic
neoromantic merged commit 0637c03 into main Oct 21, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/history-sender-44 branch October 21, 2026 22:21
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.

History pages store no sender for incoming DMs and channel posts

1 participant