Repository navigation
fix(daemon): apply deletes without chat context in private chats and basic groups - #66
Merged
Merged
Conversation
neoromantic
force-pushed
the
fix/per-account-cache-17
branch
from
October 22, 2026 09:48
66cb137 to
3c13b0f
Compare
neoromantic
force-pushed
the
fix/delete-without-chat-7b
branch
from
October 22, 2026 10:10
a74989c to
e07935e
Compare
…context A delete in a private chat or basic group names only message IDs, so the daemon looks each ID up in messages_cache without a chat. No index covered message_id, and the lookup read every cached message for each ID. Existing caches get the index on their next open: CREATE INDEX IF NOT EXISTS is idempotent, as idx_sync_jobs_lane already relies on. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…chats and basic groups Telegram names no chat when a message is deleted in a private chat or basic group, so the daemon looks the message ID up in the account's cache. It matched every cached chat, channels included, and skipped an ID that two chats had. Private chats and basic groups share one message-ID sequence per account while each channel and supergroup numbers its own, so a channel post with the same ID made such a delete look ambiguous and it was never marked. The lookup now ignores channel and supergroup rows. An ID that two private chats or basic groups still have live is left unmarked as before, and the daemon now logs it instead of skipping it silently. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…id index is built The first open of a cache without idx_messages_cache_message_id builds it under the write lock. That reads every page of messages_cache, raw_json included, and on a cache of a million or more messages read cold from disk it can take longer than the 5-second busy_timeout. A second process opening the same cache meanwhile then failed with "database is locked". A command would retry on its next open, but the daemon does not: started next to an agent's tg command, a single-account daemon found no cache it could open and exited. When the index is missing, its build now waits up to 60 seconds for the lock, as the peer ID migration does, and restores the busy timeout after. A process that waited finds the index built and creates nothing. A cache that has the index is still only read, so opening it never waits. A new test holds the lock for 6 seconds while a cache without the index opens; it failed with SQLITE_BUSY before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s IDs Every ambiguous-ID test had no unique match, so none reached the main return of markDeletedByMessageIds. Dropping the ambiguous list there, or the Set that counts a repeated ID once, or the five-ID limit of the warning, or warning on every delete, left the suite green. Telegram batches delete IDs, so a delete such as [7, 8] where only 8 is unique is the common case, and losing the warning for 7 would break the promise to log what is left unmarked. New cases: a delete of an ambiguous and a unique ID marks the unique one and reports the other, in the cache and through onDeleteMessage; a repeated ambiguous ID is reported once; six ambiguous IDs show five and "1 more"; deletes with nothing ambiguous log no warning. Each of those four regressions now fails a test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Before the fix, a delete without chat context whose message wasn't cached marked the one channel or supergroup post with that ID as deleted. The existing channel-only test cached the ID in two channels, which the old lookup already skipped as ambiguous, so bringing that bug back left the suite green. Cover one channel post alone, and one next to an already marked private message, in the cache and through the daemon's handler. Co-Authored-By: Claude Opus 5.5 <[email protected]>
neoromantic
force-pushed
the
fix/delete-without-chat-7b
branch
from
October 22, 2026 10:36
e07935e to
0d4e42f
Compare
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.
Rebased onto main after #59 merged.
Problem
Telegram names no chat when a message is deleted in a private chat or basic group (
updateDeleteMessages), so the daemon looks each message ID up in the receiving account's cache. The lookup matched every cached chat, channels and supergroups included, and each channel and supergroup numbers its own messages. That went wrong two ways:is_deleted = 1. It then dropped out oftg messages listandmessages search, andmessages getreturned it withisDeleted: true.The lookup also read every cached message for each ID, because nothing indexed
message_id.Fix
idx_messages_cache_message_idonmessages_cache(message_id). Existing caches get it on their next open. When it is missing, the build waits up to 60 seconds for the write lock (as the peer ID migration does) and restores the busy timeout after: building it reads the whole table, which on a large cache read cold from disk can outlast the 5-second timeout, and the daemon does not retry a cache it fails to open. A cache that has the index is only read.tg sql print-schemadescribes it.sharesAccountMessageIdsinsrc/utils/telegram-peer-ids.ts: true for users and basic groups, whose messages share one ID sequence per account.findChatIdsByMessageIds/markDeletedByMessageIdskeep only those chats among the live rows of each ID. One candidate is markedis_deleted = 1. None (not cached, or channels only) is skipped, so a channel post is never marked. Two or more are left unmarked, as before, and are returned asambiguous; a repeated ID counts once.onDeleteMessagelogs a warning for ambiguous IDs (at most five shown) instead of skipping them silently.messages searchhides them unless deleted messages are requested.docs/real-time.md(Deletes),docs/daemon.md(Update Handling),docs/database-schema.md(index list, Migrations).Tests
messages-cache.test.ts: a DM and a channel with the same ID mark only the DM; a basic group and a supergroup mark only the basic group; a channel post that is the only cached message with the ID is left, and so is one next to an already marked DM message (both marked the channel post before); an ID only channels have is left; a DM and a basic group with the same ID are kept and reported; a delete of an ambiguous and a unique ID marks the unique one and reports the other; a repeated ambiguous ID is reported once.daemon-accounts.connect.test.ts: the same cases throughonDeleteMessage(including a lone channel post, left with no warning), a channel delete that marks only the channel row, the warning (none when nothing is ambiguous; six ambiguous IDs show five and "1 more").sync-schema.test.ts: the index exists and the lookup's query plan uses it; a cache without it gets it on reopen with its rows unchanged, and a secondinitSyncSchemaleaves the schema as it is.cache-db-lock.test.ts: four processes migrating a 0.1.4 database at once also create the index; a cache without the index opens while another process holds the write lock for 6 seconds (failed with SQLITE_BUSY before).telegram-peer-ids.test.ts,update-handlers.test.ts,messages-search.test.tscover the predicate, the handler result and search.bun run check,bun run testandbun run test:e2epass.messages searchstopped returning it,--includeDeletedshowsisDeleted: true, no warning logged). A delete sent through the daemon's own session reached it only with the next update, when the gap was filled, and was marked too.Closes #7
🤖 Generated with Claude Code