Skip to content

fix(daemon): apply deletes without chat context in private chats and basic groups - #66

Merged
neoromantic merged 5 commits into
mainfrom
fix/delete-without-chat-7b
Oct 22, 2026
Merged

neoromantic merged 5 commits into
mainfrom
fix/delete-without-chat-7b

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

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:

  • Skipped delete. An ID that more than one chat had was skipped, so any channel or supergroup post with the same ID made the delete look ambiguous and the private message stayed unmarked.
  • Wrong message marked. When the deleted message wasn't cached (or was already marked) and exactly one channel or supergroup had a live post with that ID, the daemon marked that post is_deleted = 1. It then dropped out of tg messages list and messages search, and messages get returned it with isDeleted: true.

The lookup also read every cached message for each ID, because nothing indexed message_id.

Fix

  • idx_messages_cache_message_id on messages_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-schema describes it.
  • sharesAccountMessageIds in src/utils/telegram-peer-ids.ts: true for users and basic groups, whose messages share one ID sequence per account.
  • findChatIdsByMessageIds / markDeletedByMessageIds keep only those chats among the live rows of each ID. One candidate is marked is_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 as ambiguous; a repeated ID counts once.
  • onDeleteMessage logs a warning for ambiguous IDs (at most five shown) instead of skipping them silently.
  • Rows are still only marked, never removed; messages search hides them unless deleted messages are requested.
  • Docs: 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 through onDeleteMessage (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 second initSyncSchema leaves 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.ts cover the predicate, the handler result and search.
  • bun run check, bun run test and bun run test:e2e pass.
  • Live check with the daemon running from this branch: a message sent to Saved Messages from another session was cached, and deleting it from that session marked the cached row deleted within seconds (messages search stopped returning it, --includeDeleted shows isDeleted: 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

@neoromantic
neoromantic force-pushed the fix/per-account-cache-17 branch from 66cb137 to 3c13b0f Compare October 22, 2026 09:48
Base automatically changed from fix/per-account-cache-17 to main October 22, 2026 09:51
@neoromantic
neoromantic changed the base branch from main to stack/pr59-base October 22, 2026 09:54
@neoromantic
neoromantic force-pushed the fix/delete-without-chat-7b branch from a74989c to e07935e Compare October 22, 2026 10:10
neoromantic and others added 5 commits October 22, 2026 14:32
…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
neoromantic force-pushed the fix/delete-without-chat-7b branch from e07935e to 0d4e42f Compare October 22, 2026 10:36
@neoromantic
neoromantic changed the base branch from stack/pr59-base to main October 22, 2026 10:36
@neoromantic
neoromantic merged commit 1f48ccf into main Oct 22, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/delete-without-chat-7b branch October 22, 2026 10:46
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.

Delete events without chat context can skip valid deletions

1 participant