|
| 1 | +## Description |
| 2 | + |
| 3 | +Fixes #5496 |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Changes Made |
| 8 | + |
| 9 | +- **Enhanced ToolRepetitionDetector**: Added pattern detection algorithm that identifies repeating sequences of tool calls beyond just consecutive repetitions |
| 10 | +- **Pattern Detection Algorithm**: Implements detection for patterns of length 2-6 with configurable minimum repetitions (default: 2) |
| 11 | +- **History Tracking**: Maintains a rolling history of the last 20 tool calls for pattern analysis |
| 12 | +- **Backward Compatibility**: Preserves existing consecutive repetition detection behavior |
| 13 | +- **Comprehensive Testing**: Added 15+ new test cases covering various pattern scenarios |
| 14 | + |
| 15 | +## Technical Implementation |
| 16 | + |
| 17 | +### Key Features: |
| 18 | + |
| 19 | +- Detects patterns like AB-AB, ABC-ABC, ABCD-ABCD, etc. |
| 20 | +- Configurable pattern length (2-6 tools) and minimum repetitions (default: 2) |
| 21 | +- Efficient pattern matching using sliding window approach |
| 22 | +- Proper serialization ensures tools with different parameters are treated as distinct |
| 23 | +- State reset after detection to allow recovery |
| 24 | + |
| 25 | +### Algorithm: |
| 26 | + |
| 27 | +1. Maintains history of serialized tool calls |
| 28 | +2. For each new tool call, checks for repeating patterns of various lengths |
| 29 | +3. Uses backward pattern matching from the end of history |
| 30 | +4. Triggers when minimum repetitions threshold is reached |
| 31 | + |
| 32 | +## Testing |
| 33 | + |
| 34 | +- **All existing tests pass**: Ensures backward compatibility |
| 35 | +- **New pattern detection tests**: Cover ABAB, ABCABC, and complex patterns |
| 36 | +- **Edge cases**: Different parameters, insufficient repetitions, mixed patterns |
| 37 | +- **Integration tests**: Verify both consecutive and pattern detection work together |
| 38 | + |
| 39 | +## Verification of Acceptance Criteria |
| 40 | + |
| 41 | +- [x] **Pattern Detection**: Successfully detects non-continuous repetitions like ABABAB |
| 42 | +- [x] **Configurable**: Pattern length and repetition thresholds are configurable |
| 43 | +- [x] **Performance**: Efficient algorithm with bounded history (20 calls max) |
| 44 | +- [x] **No False Positives**: Different tools/parameters don't trigger false detections |
| 45 | +- [x] **Recovery**: State resets after detection to allow user guidance |
| 46 | + |
| 47 | +## Example Scenarios Detected |
| 48 | + |
| 49 | +```typescript |
| 50 | +// These patterns will now be detected: |
| 51 | +toolA -> toolB -> toolA -> toolB // ABAB pattern |
| 52 | +read_file(file1) -> write_file(file2) -> read_file(file1) -> write_file(file2) // Real-world example |
| 53 | +toolA -> toolB -> toolC -> toolA -> toolB -> toolC // ABCABC pattern |
| 54 | +``` |
| 55 | + |
| 56 | +## Files Changed |
| 57 | + |
| 58 | +- `src/core/tools/ToolRepetitionDetector.ts` - Enhanced with pattern detection |
| 59 | +- `src/core/tools/__tests__/ToolRepetitionDetector.spec.ts` - Added comprehensive tests |
| 60 | + |
| 61 | +## Checklist |
| 62 | + |
| 63 | +- [x] Code follows project style guidelines |
| 64 | +- [x] Self-review completed |
| 65 | +- [x] Comments added for complex logic |
| 66 | +- [x] All existing tests pass |
| 67 | +- [x] New tests added for pattern detection |
| 68 | +- [x] No breaking changes |
| 69 | +- [x] Performance considerations addressed |
| 70 | +- [x] Backward compatibility maintained |
0 commit comments