mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-09-05 08:10:14 +00:00
2.9 KiB
2.9 KiB
Description
Fixes #5496
This PR enhances the ToolRepetitionDetector to detect non-continuous repetitive tool call patterns (e.g., ABABAB) to prevent the AI from getting stuck in alternating tool call loops.
Changes Made
- Enhanced ToolRepetitionDetector: Added pattern detection algorithm that identifies repeating sequences of tool calls beyond just consecutive repetitions
- Pattern Detection Algorithm: Implements detection for patterns of length 2-6 with configurable minimum repetitions (default: 2)
- History Tracking: Maintains a rolling history of the last 20 tool calls for pattern analysis
- Backward Compatibility: Preserves existing consecutive repetition detection behavior
- Comprehensive Testing: Added 15+ new test cases covering various pattern scenarios
Technical Implementation
Key Features:
- Detects patterns like AB-AB, ABC-ABC, ABCD-ABCD, etc.
- Configurable pattern length (2-6 tools) and minimum repetitions (default: 2)
- Efficient pattern matching using sliding window approach
- Proper serialization ensures tools with different parameters are treated as distinct
- State reset after detection to allow recovery
Algorithm:
- Maintains history of serialized tool calls
- For each new tool call, checks for repeating patterns of various lengths
- Uses backward pattern matching from the end of history
- Triggers when minimum repetitions threshold is reached
Testing
- All existing tests pass: Ensures backward compatibility
- New pattern detection tests: Cover ABAB, ABCABC, and complex patterns
- Edge cases: Different parameters, insufficient repetitions, mixed patterns
- Integration tests: Verify both consecutive and pattern detection work together
Verification of Acceptance Criteria
- Pattern Detection: Successfully detects non-continuous repetitions like ABABAB
- Configurable: Pattern length and repetition thresholds are configurable
- Performance: Efficient algorithm with bounded history (20 calls max)
- No False Positives: Different tools/parameters don't trigger false detections
- Recovery: State resets after detection to allow user guidance
Example Scenarios Detected
// These patterns will now be detected:
toolA -> toolB -> toolA -> toolB // ABAB pattern
read_file(file1) -> write_file(file2) -> read_file(file1) -> write_file(file2) // Real-world example
toolA -> toolB -> toolC -> toolA -> toolB -> toolC // ABCABC pattern
Files Changed
src/core/tools/ToolRepetitionDetector.ts- Enhanced with pattern detectionsrc/core/tools/__tests__/ToolRepetitionDetector.spec.ts- Added comprehensive tests
Checklist
- Code follows project style guidelines
- Self-review completed
- Comments added for complex logic
- All existing tests pass
- New tests added for pattern detection
- No breaking changes
- Performance considerations addressed
- Backward compatibility maintained