Skip to content

fix(send): cache the sent message right away - #30

Merged
neoromantic merged 2 commits into
mainfrom
fix/send-cache-19
Oct 21, 2026
Merged

neoromantic merged 2 commits into
mainfrom
fix/send-cache-19

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

Problem

tg send returned the message ID but never wrote the message to messages_cache. Telegram pushes no update for a message to the session that sent it, and the daemon shares that session, so the message reached the cache only by accident (an unrelated update revealing a gap, or a later sync job). tg messages search missed it until then.

Fix

After a successful send, tg send upserts the message into messages_cache from the response (src/commands/send/sent-message-cache.ts):

  • updates / updatesCombined: each updateNewMessage / updateNewChannelMessage goes through the sync worker's parseRawMessage, with the chat ID from the message's peerId (toPeerIdFromRawPeer). raw_json is the raw message.
  • updateShortSentMessage: there is no message object, so the row is rebuilt from the request (text, reply-to) and the response (id, date, out, media), then mapped by the same parseRawMessage. The sender is the own user ID, and inputPeerSelf maps to it as well. The ID comes from the account row, or from getMe once if the row lacks it (it is then saved, as tg me does). raw_json is the short response.
  • Chat IDs are marked peer IDs (user ID, -chatId, -100…), the format the daemon uses for messages_cache.chat_id. The new toPeerIdFromInputPeer in src/utils/telegram-peer-ids.ts handles input peers. The messages/chats format mismatch (Messages and chats are cached with different peer ID formats #18) is untouched.
  • The FTS index (message_search) follows through the existing messages_cache triggers, so the message is searchable at once.
  • A cache failure never turns a successful send into an error: it is logged with verbose() and the send still reports success.

No daemon code changed: the command only imports the already exported parseRawMessage and bigIntReplacer. Docs (docs/cli-commands.md, docs/caching.md, docs/testing.md), CHANGELOG.md and ROADMAP.md are updated.

Tests

  • New src/__tests__/send-cache.test.ts (16 tests) runs the real module: both response shapes, inputPeerSelf / user / basic group / channel peers, link preview media, reply-to, FTS search, fetched_at and raw_json, the own user ID from the account row vs. getMe with save, and a failing cache write or failing user ID lookup that resolves without throwing.
  • bun run check, bun run test (1135 pass) and bun run test:e2e (100 pass) are green locally.
  • Live check: a new smoke test, "send to Saved Messages caches the message for search without a daemon", sends with tg send --to me (no daemon), then finds the message with tg messages search --query <marker> under the own user ID. It passed against the real API together with the existing Saved Messages test (2 pass), and both messages were deleted afterwards. The live run showed that Telegram answers a Saved Messages send with a full updates response (out=false, no fromId), not updateShortSentMessage, so the short path is covered by unit tests only.

Fixes #19

🤖 Generated with Claude Code

neoromantic and others added 2 commits October 21, 2026 16:11
tg send returned the message ID but never wrote the message to
messages_cache. Telegram pushes no update for a message to the session
that sent it, and the daemon shares that session, so the message only
reached the cache by accident: after an unrelated update revealed a gap,
or on a later sync job. tg messages search missed it until then.

Now send upserts the message from the response. A full updates response
goes through the sync worker's parseRawMessage with the chat ID taken
from the message peer. updateShortSentMessage has no message object, so
the row is rebuilt from the request, with the own user ID as sender and
as the Saved Messages chat; the ID comes from the account row, or from
getMe once if the row lacks it. Chat IDs are marked peer IDs, the format
the daemon uses for messages_cache. The FTS index follows through the
existing triggers. A cache failure is logged in verbose mode and the
send still reports success.

Fixes #19

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The new smoke test sends to Saved Messages with no daemon running and
finds the message with tg messages search, cached under the own user
ID. It is the "done when" check of #19 against the real API, where
Telegram answers a Saved Messages send with a full updates response
(out=false, no fromId) rather than updateShortSentMessage.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit c82489b into main Oct 21, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/send-cache-19 branch October 21, 2026 12:14
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.

tg send doesn't add the sent message to the cache

1 participant