PR #772: Fix Infinite Retry Loop
📌 Overview
Upstream PR: clusterzx/paperless-ai#772
Status: ✅ Merged into fork
Integration Date: 2025-12-03
Upstream Status: ⏳ Pending review
🐛 Problem
Documents with very short content (< 25 characters) would trigger an infinite retry loop: - AI returns content too short - System retries indefinitely - Logs fill up with retry attempts - No error handling or circuit breaker
✅ Solution
- Added
MIN_CONTENT_LENGTHconstant (25 characters) - Retry tracking - Tracks number of retries per document
- Circuit breaker - Stops after 3 failed attempts
- Better error messages - Clear logging when content is too short
- Graceful degradation - Marks document as failed instead of infinite loop
📝 Changes
Modified Files
services/chatService.js- Added retry tracking and MIN_CONTENT_LENGTH checkservices/documentsService.js- Improved error handling
Key Code Changes
// Added constant
const MIN_CONTENT_LENGTH = 25;
// Retry tracking
if (retryCount >= 3) {
loggerService.log('error', `Document ${docId} failed after 3 retries`);
return null;
}
// Content validation
if (content.length < MIN_CONTENT_LENGTH) {
loggerService.log('warn', `Content too short for document ${docId}`);
retryCount++;
// ... retry logic
}
🧪 Testing
Tested with: - ✅ Short documents (< 25 chars) - ✅ Empty documents - ✅ Very long documents - ✅ Retry counter behavior - ✅ Log output
Test file: test-pr772-fix.js (6/6 tests passing)
📊 Impact
Before: - Infinite loops on short documents - System hangs - Logs fill up - Manual intervention required
After: - Graceful failure after 3 retries - Clear error messages - System continues processing other documents - No manual intervention needed
🔗 Related
- Upstream PR: https://github.com/clusterzx/paperless-ai/pull/772
- Test File:
../../test-pr772-fix.js - Commit: See git log for integration commit
👥 Credits
- Original Author: Community contributor (upstream PR #772)
- Integrated By: Admonstrator
- Tested By: Admonstrator with AI assistance