Repository navigation
fix(daemon): keep messages that arrive while the daemon is stopped - #31
Merged
Merged
Conversation
The live handler moved a chat's forward cursor to every new message, and live updates start before the scheduler, so the first live message after a restart jumped the cursor over everything that arrived while the daemon was off. Forward catch-up also fetched only the newest 100 messages above the cursor and then moved it to the top, so a gap larger than one page lost its oldest part even without a live message. The forward cursor now means "every message up to here is cached". Live updates still cache messages but leave the cursor to the sync jobs. Forward catch-up fetches the oldest page above the cursor (offset_id = cursor + 1, add_offset = -limit, min_id = cursor), moves the cursor to its newest message and lets the scheduler queue the next page while pages are full, one API call per job. Only messages that were not cached yet count towards synced_messages, so live messages that catch-up fetches again are not counted twice. mtcute's catchUp option stays off: catch-up covers missed messages on its own, and getDifference can't replay long gaps. Fixes #15 Co-Authored-By: Claude Opus 5.5 <[email protected]>
mock.module() replaces a module for the whole bun test process, and mock.restore() doesn't undo it. sync-worker-real.test.ts mocks the real job handlers, so every file that ran after it got the stubs. Bun orders files differently on macOS and Linux: the new daemon restart test passed locally and failed in CI, and the existing sync-worker-real-jobs tests fail the same way when they happen to run later. --isolate gives each file its own module registry. The unit suite takes about 3.6s instead of 1.9s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
neoromantic
force-pushed
the
fix/daemon-gap-15
branch
from
October 21, 2026 12:15
18c486a to
ea1dfe0
Compare
This was referenced Oct 21, 2026
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.
Problem
If a chat got new messages while the daemon was stopped, they became a permanent gap in the cache:
forward_cursorto every new message ID, and live updates start before the scheduler. The first live message after a restart jumped the cursor over everything that arrived while the daemon was off.min_id = cursor) and then moved the cursor to the top. A gap larger than one page lost its oldest part even without a live message.Design
The smallest change that holds up on its own is to give
forward_cursorone meaning and page the catch-up from it:forward_cursormeans "every message up to here is cached". Live updates still upsert messages intomessages_cache, but they no longer move the cursor. Only sync jobs move it, after they have fetched the messages below. Message IDs can't show a gap (in DMs and basic groups they are account-wide), so the live handler can't know whether moving the cursor is safe.forward_catchupjob fetches the page just above the cursor (offset_id = cursor + 1,add_offset = -limit,min_id = cursor, the same paging mtcute uses forreversehistory), caches it and moves the cursor to its newest message. A full page returnshasMore, and the existing scheduler wiring queues the next page. That keeps one API call per job, so paging stays undercanMakeApiCall, the inter-job delay and the existing FLOOD_WAIT handling. If a page fails, the cursor stays at the last cached page and the next catch-up resumes from there.messageEmptyentries.synced_messagesand the daemon's "messages synced" don't count a message twice.updates: { catchUp: true }is not enabled. Catch-up already covers missed messages.getDifferencecan't replay a long gap (differenceTooLong), and the daemon doesn't close its client storage on stop, so replays could repeat. Turning it on would mainly bring offline edits and deletions, which is a separate gap.Because live updates never move the cursor, a gap from a dropped connection (#16) is covered the same way: the next catch-up after the reconnect pages from the cursor.
Trade-off: an active chat's cursor trails live messages until the next catch-up. Today that costs no extra calls, because
refreshQueuespolls every enabled chat anyway, and the response just isn't empty. If #6 moves catch-up to "on start and on reconnect", the first catch-up after a restart will fetch the messages received live since the previous one again.No changes to reconnect logic,
daemon-scheduler.ts,send.tsor the flood-waiter settings. The daemon stays read-only.Tests
src/__tests__/daemon-restart-gap.test.tsruns the issue's example through the real update handlers, scheduler, sync worker andprocessJobsagainst a fakemessages.getHistory. The cursor is at 1000, 150 messages arrive while the daemon is stopped, and live message 1151 lands before catch-up. All 151 end up cached, the cursor ends at 1151, every message counts once, and it takes 2 calls. Two more cases: live messages that arrive during catch-up, and a FLOOD_WAIT between pages that resumes from the cursor once the wait expires. All three fail onmain.sync-worker-real-jobs.test.tscovers paging boundaries: the request shape, a 150-message gap (full page, then short page), a gap of exactly one batch (one more empty page), nothing above the cursor, messages at or below the cursor,messageEmpty, counting only uncached messages, and a chat without a cursor.update-handlers.test.ts: a live message no longer moves the cursor or sets one for a new chat.bun run check,bun run test(1152 pass, rebased onmainwith Reconnect doesn't restore real-time updates #16 and tg send doesn't add the sent message to the cache #19) andbun run test:e2e(100 pass) are green locally. I didn't run the live suite (bun run test:integration). Please run it after merging, since this changes Telegram calls in the daemon.Second commit: the unit test scripts now pass
--isolate.sync-worker-real.test.tsmocks the real job handlers withmock.module(), which applies to the wholebun testprocess, andmock.restore()doesn't undo it. Every file that ran after it got the stubs: the new restart test passed on macOS and failed in CI's Linux file order, and the existingsync-worker-real-jobstests fail the same way when they run later. The unit suite takes about 3.6s instead of 1.9s.Docs:
docs/sync-strategy.md,docs/real-time.md, the sync sentence inAGENTS.md,CHANGELOG.mdanddocs/testing.md.Fixes #15
🤖 Generated with Claude Code