Skip to content

fix: backlog replay can drop messages and produce duplicate replies in bot.py #5

@coderabbitai

Description

@coderabbitai

Summary

The backlog replay logic in bot.py (on_ready) has two related problems that can cause missed messages and duplicate replies.

Problem 1 – History scan limit drops older messages

At line 114, only the last 50 messages are scanned to find the bot's last reply cursor (last_bot_msg). If the bot was offline long enough that its last reply is beyond the 50-message window, last_bot_msg is None and the fallback (lines 126–129) processes only the last 5 user messages, silently dropping any older unprocessed ones.

Problem 2 – Duplicate replies when on_message is active during replay

The on_message event handler is active while the backlog is being replayed in on_ready. If a new message arrives (or if history iteration yields a message that also triggers on_message), the same message can be forwarded to Ollama and replied to twice.

Suggested fix

  1. Persist a durable checkpoint — store last_processed_message_id to disk (e.g., a small JSON/SQLite file) after each successful process_message call instead of relying on scanning chat history for the last bot message.
  2. Use the checkpoint for history fetch — call channel.history(after=last_processed_message_id, oldest_first=True) (or from the beginning if no checkpoint exists) with no artificial small limit.
  3. Add message-id deduplication in process_message (maintain a set of recently processed IDs) or temporarily gate on_message while the replay loop runs to prevent double-processing.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions