fix: prevent duplicate outgoing messages in chat#2104
Conversation
When sending a message, the device echoes the packet back causing processTextMessageProtobuf to emit a second message:new WebSocket event. Make insertMessage() return a boolean indicating whether a row was actually inserted, and gate the WebSocket emit, push notifications, auto-responder, auto-acknowledge, and auto-ping on that result. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude finished @Yeraze's task —— View job Code Review: Fix Duplicate Outgoing MessagesTodo List:
SummaryThis PR successfully addresses the duplicate message issue by implementing a clean solution: making Code Quality ✅ EXCELLENTStrengths:
Code Quality Notes:
Potential Issues & Recommendations1. Race Conditions (Minor Risk)Issue: For PostgreSQL/MySQL, there's a small window between cache check and database insert where another thread could insert the same message. Location: // Check cache for duplicate before inserting
const existsInCache = this._messagesCache.some(m => m.id === messageData.id);
if (existsInCache) {
return false;
}
// Race window here - another thread could insert between check and insertRecommendation: This is likely acceptable given the context (single-threaded Node.js event loop), but consider adding cache insertion to be atomic with the database check. 2. MySQL onDuplicateKeyUpdate LogicIssue: Line 66 uses Location: Recommendation: This might cause 3. Cache Size ManagementThe 500-message cache limit is reasonable, but consider:
Performance Impact ✅ POSITIVEImprovements:
Minimal Overhead:
Security Considerations ✅ SECURE
Test Coverage AssessmentCurrent Coverage: ✅ ADEQUATE
Recommendations for Enhanced Testing:
Final Verdict: ✅ APPROVE WITH MINOR SUGGESTIONSThis is a well-engineered solution that addresses the root cause effectively. The implementation is clean, follows established patterns, and provides measurable improvements to system behavior. Pre-merge Checklist:
The core fix is solid and ready for production. The minor suggestions above are optimizations rather than blockers. |
- Repository test: insertMessage returns true on insert, false on duplicate - DatabaseService test: update helper to return boolean, verify return values - Manager integration test: verify WebSocket emit is gated on insertMessage result Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude finished @Yeraze's task —— View job Code Review: Fix Duplicate Outgoing Messages ✅Todo List:
SummaryThis PR successfully addresses the duplicate message issue by implementing a clean solution: making Code Quality ✅ EXCELLENTStrengths:
Code Quality Notes:
Potential Issues & Recommendations1. Race Conditions (Minor Risk)Issue: For PostgreSQL/MySQL, there's a small window between cache check and database insert where another thread could insert the same message. Location: // Check cache for duplicate before inserting
const existsInCache = this._messagesCache.some(m => m.id === messageData.id);
if (existsInCache) {
return false;
}
// Race window here - another thread could insert between check and insertRecommendation: This is likely acceptable given the context (single-threaded Node.js event loop), but consider adding cache insertion to be atomic with the database check. 2. MySQL onDuplicateKeyUpdate LogicIssue: Line 66 uses Location: Recommendation: This might cause 3. Cache Size ManagementThe 500-message cache limit is reasonable, but consider:
Performance Impact ✅ POSITIVEImprovements:
Minimal Overhead:
Security Considerations ✅ SECURE
Test Coverage AssessmentCurrent Coverage: ✅ ADEQUATE
Recommendations for Enhanced Testing:
Final Verdict: ✅ APPROVE WITH MINOR SUGGESTIONSThis is a well-engineered solution that addresses the root cause effectively. The implementation is clean, follows established patterns, and provides measurable improvements to system behavior. Pre-merge Checklist:
The core fix is solid and ready for production. The minor suggestions above are optimizations rather than blockers. |
Summary
processTextMessageProtobufunconditionally emits a secondmessage:newWebSocket eventinsertMessage()now returns a boolean indicating whether a row was actually inserted (not a duplicate)Test plan
tests/system-tests.sh)🤖 Generated with Claude Code