AOSSIE-Org / AOSSIE-Org/SkillBot

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

Open
#5 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
6
Forks
3
PR merge metrics
No merged PRs in 30d

Description

## 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

- PR: https://github.com/AOSSIE-Org/SkillBot/pull/4
- Review comment: https://github.com/AOSSIE-Org/SkillBot/pull/4#discussion_r3393419333
- Requested by: @kpj2006

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.