AOSSIE-Org / AOSSIE-Org/SkillBot
fix: backlog replay can drop messages and produce duplicate replies in bot.py
- 主要語言
- Python
- 星號
- 6
- 分支
- 3
- PR 合併指標
- 30 天內沒有已合併 PR
描述
## 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
貢獻指南
評估
這個 Issue 還沒有評估資料。