Skip to content

feat!: cache a group's new messages only while the account takes part - #155

Merged
neoromantic merged 13 commits into
mainfrom
feat/group-participation
Nov 18, 2026
Merged

neoromantic merged 13 commits into
mainfrom
feat/group-participation

Conversation

@neoromantic

@neoromantic neoromantic commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

What

A group (basic group or supergroup) adds new messages to the cache only while the account sent an ordinary message there in the last 90 days. Reading a group doesn't count. The rule holds in every path and every tg chats sync mode; cached messages stay; private chats and channels are unchanged.

  • One writer: MessagesCache.upsert/upsertBatch count own ordinary messages first (oldest first), then insert a new row only if GroupParticipation.canInsert allows it; cached rows are always updated. Rejected messages write nothing (no attachment row, no search index row). The write returns { accepted, inserted, skipped }, and counters use inserted.
  • State (per account cache): group_participation (newest own message, first check) and group_capture_gaps (pauses [start, end)). Merges never lower the date or reopen a pause. An older cache gets its own messages as first facts, once.
  • Checks: after each dialog pass, one check_group_participation job per group (archived ones too, not channels) runs messages.search with fromId: inputPeerSelf, minDate: now - 90 days, in the catch-up lane at P4 with the usual retries and FLOOD_WAIT handling. An error records nothing. The dialog list's own newest message counts at once.
  • Queue and cursors: no catch-up, first page or history for a group that isn't active; each refresh drops such pending jobs (retries included). A job that starts after expiry ends without a request; a page whose group expired in flight moves no cursor and isn't retried. Cursors move past paused messages, but a page with any message kept out records no history range. A group coming back gets top_message_id = NULL for a catch-up from its old cursor.
  • CLI: tg messages list/get build API pages in memory, so they show Telegram's page whether the cache took it or not (same output). tg send resumes a group without a daemon update. tg chats list/sync show syncReason: group_participation_required | group_participation_unknown and lastOwnMessageAt; syncEnabled reflects participation. tg status leaves paused groups out of its counts and history progress, and ignores Telegram's count for a group with a pause.

Docs: README, docs/sync-strategy.md (new section), real-time.md, caching.md, database-schema.md, cli-commands.md, daemon.md, AGENTS project rules, CHANGELOG Unreleased.

Checks

Latest head: merged with main, then simplified after a review of the whole PR. The simplifications cut about 210 lines: a shorter GroupParticipation, CacheWriteResult.written, page rows from the cache getters, one test per behaviour, and docs/sync-strategy.md#group-participation as the one full statement of the rule.

  • bun run check: pass
  • bun run test: 2270 pass, 0 fail
  • bun run test:e2e: 376 pass, 0 fail, including the replay test for messages list --fresh on a group without participation
  • bun run build: pass
  • bun run test:integration: not verified. The test session answers 401 AUTH_KEY_UNREGISTERED, and its re-login is in progress separately.

Cleanup of pre-existing code is stacked on this PR in #174.

🤖 Generated with Claude Code

neoromantic and others added 3 commits November 14, 2026 04:51
A group (basic group or supergroup) adds new messages to the cache only
while the account sent an ordinary message there in the last 90 days.
The rule lives in one place, MessagesCache, so live updates, the dialog
pass, every sync job, tg messages list/get (--fresh too) and tg send all
follow it, whatever the tg chats sync mode. Cached messages stay and are
still updated; private chats and channels are unchanged.

- group_participation keeps the newest own message per group;
  group_capture_gaps keeps the pauses, so catch-up and history after a
  return skip them. An older cache gets its own messages as first facts.
- After each dialog pass, one check_group_participation job per group
  searches for the newest own message of the last 90 days (catch-up
  lane, P4, usual retries and FLOOD_WAIT handling).
- The scheduler queues no work for a group that isn't active and drops
  its pending jobs; a page whose group went off in flight moves nothing.
  A page with messages kept out records no history range.
- tg messages list/get build API pages in memory, so they show
  Telegram's page whether the cache took it or not.
- tg chats list/sync show group_participation_required/_unknown and
  lastOwnMessageAt; tg status leaves paused groups out of its progress.

BREAKING CHANGE: groups without an own message in the last 90 days no
longer add messages to the cache, in any sync mode.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rticipation rule

Resolving a @username kept only the input peer, so a broadcast channel new
to the cache had no type, counted as a group and its first page was not
cached. Cache the resolved chat (not a min one) before its messages are
written, as `tg chats get` does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
contacts.resolveUsername carries no last message and often no member
count, so the cache upsert cleared what the dialog pass had stored.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic marked this pull request as ready for review November 14, 2026 01:10
neoromantic and others added 9 commits November 17, 2026 23:50
…ps, ID offset, header)

The merge already runs under the write lock, so the upsert is a plain
INSERT OR REPLACE. One schema check helper, the shared channel ID offset,
and history progress reads pauses in its own query instead of hasGaps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…worker gate, accurate PAUSED comment

CacheWriteResult counts written messages instead of listing them; every
caller used only the length. The worker gate reuses
CHAT_INDEPENDENT_JOB_TYPES instead of a second list of capture jobs, and
the PAUSED comment no longer claims what the write did.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…le-row MessagesHistory.get, inline withSync

cacheHistoryResponse builds page rows with ChatsCache.getById (one lookup)
and UsersCache.getById instead of ad-hoc queries. MessagesHistory.getMany
becomes get(chatId, messageId), its only use. chats list inlines withSync
into toItem.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…se fixture helpers

The e2e now checks the sender and that no history range is recorded, then
caches the page once the account took part, in one test. The unit copy of
the page test goes. Chat and photo fixtures use fakeDialog,
dialogToCacheInput, photoMessage and takePartIn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop takePartIn calls that changed nothing (repeated after a schema init,
or before writes to DMs only) and trim the rest to the groups whose
messages each test caches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ement of the rule

Other docs, the changelog entry and daemon-bootstrap comments link to the
section instead of restating the 90-day rule, its writers and scheduling.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…articipation

The Pauses bullet says again that a pause ends at the newest own message the
search finds, and the date source says nothing of the search is cached.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… in the changelog

The Unreleased entry lists the commands the rule covers instead of "every
mode", and says the daemon sends one search per group after each dialog pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keep the participation rule in the new paths: the chats list output declaration gets the group_participation reasons and lastOwnMessageAt, live messages still note the chat's newest message, and wait/watch docs say a paused group shows nothing. New tests that write group messages take part in those groups.
@neoromantic
neoromantic merged commit 528f6ae into main Nov 18, 2026
5 checks passed
@neoromantic
neoromantic deleted the feat/group-participation branch November 18, 2026 10:33
neoromantic added a commit that referenced this pull request Nov 18, 2026
#155 landed as 528f6ae, a squash of df2ad48, which this branch already holds, plus 6e2e551, picked above.
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. fetch.ts: the batch fetch is built
on main's shared fetchMessagesByIdRaw, with the peer check kept. README
and CHANGELOG: both sides' entries kept.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. README and CHANGELOG: both sides kept. main dropped MessagesHistory.getMany (neoromantic#174): the item is read with get. Fixture groups take part in the tests: main caches a group's new messages only while the account writes there (neoromantic#155).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. README and CHANGELOG: both sides kept. Fixture groups take part in the tests: main caches a group's new messages only while the account writes there (neoromantic#155).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. README and CHANGELOG: both sides kept. main dropped MessagesHistory.getMany (neoromantic#174): the item is read with get. Fixture groups take part in the tests: main caches a group's new messages only while the account writes there (neoromantic#155).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. README and CHANGELOG: both sides kept. main dropped MessagesHistory.getMany (neoromantic#174): the item is read with get. Fixture groups take part in the tests: main caches a group's new messages only while the account writes there (neoromantic#155).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. history.ts and two test files: imports of both sides. CHANGELOG: both sides kept. Fixture groups take part in the tests: main caches a group's new messages only while the account writes there (neoromantic#155).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. skill.ts: main's shared getErrorMessage, the manifest stays removed. CHANGELOG: both sides kept. Fixture groups take part in the tests: main caches a group's new messages only while the account writes there (neoromantic#155).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. helpers.ts: the item reader takes main's raw string, with the transcript kept. sql.test.ts uses main's cache helper. CHANGELOG: both sides kept.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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.

1 participant