Skip to content

fix(daemon): keep messages that arrive while the daemon is stopped - #31

Merged
neoromantic merged 2 commits into
mainfrom
fix/daemon-gap-15
Oct 21, 2026
Merged

neoromantic merged 2 commits into
mainfrom
fix/daemon-gap-15

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

Problem

If a chat got new messages while the daemon was stopped, they became a permanent gap in the cache:

  • The live handler moved the chat's forward_cursor to 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.
  • Forward catch-up fetched only the newest 100 messages above the cursor (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_cursor one meaning and page the catch-up from it:

  • forward_cursor means "every message up to here is cached". Live updates still upsert messages into messages_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.
  • Catch-up pages oldest first from the cursor. Each forward_catchup job fetches the page just above the cursor (offset_id = cursor + 1, add_offset = -limit, min_id = cursor, the same paging mtcute uses for reverse history), caches it and moves the cursor to its newest message. A full page returns hasMore, and the existing scheduler wiring queues the next page. That keeps one API call per job, so paging stays under canMakeApiCall, 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.
  • Messages at or below the cursor in a response are ignored, so the cursor can't move backwards or loop if the server returns more than asked. The cursor also moves past messageEmpty entries.
  • A chat with no cursor still gets its newest page, as before. Older history stays with the backward job.
  • Counters: the next catch-up fetches live messages again. The upsert doesn't duplicate them, and catch-up now counts only messages that weren't cached yet, so synced_messages and the daemon's "messages synced" don't count a message twice.
  • mtcute updates: { catchUp: true } is not enabled. Catch-up already covers missed messages. getDifference can'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 refreshQueues polls 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.ts or the flood-waiter settings. The daemon stays read-only.

Tests

  • src/__tests__/daemon-restart-gap.test.ts runs the issue's example through the real update handlers, scheduler, sync worker and processJobs against a fake messages.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 on main.
  • sync-worker-real-jobs.test.ts covers 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 on main with Reconnect doesn't restore real-time updates #16 and tg send doesn't add the sent message to the cache #19) and bun 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.ts mocks the real job handlers with mock.module(), which applies to the whole bun test process, and mock.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 existing sync-worker-real-jobs tests 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 in AGENTS.md, CHANGELOG.md and docs/testing.md.

Fixes #15

🤖 Generated with Claude Code

neoromantic and others added 2 commits October 21, 2026 16:15
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
neoromantic merged commit 3ebd01c into main Oct 21, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/daemon-gap-15 branch October 21, 2026 12:17
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.

Daemon loses messages that arrive while it is stopped

1 participant