Skip to content

test: add unit tests for PatternDetector (zero coverage) #252

Description

@acailic

Problem

collector/patterns/pattern_detector.py (441 lines) has zero test coverage.

PatternDetector implements cross-session pattern detection with pure logic (no I/O, no async):

  • detect_all_patterns() — groups by agent, calls per-agent detection
  • detect_error_rate_trends() — compares baseline vs recent error rates
  • detect_tool_failure_frequency() — compares tool failure rates
  • detect_new_failure_modes() — detects new error session patterns
  • detect_confidence_drops() — replay value drop detection
  • _calculate_average_error_rate() / _calculate_tool_failure_rate() — metric helpers

Pattern dataclass and to_dict() are also untested.

Scope

Add tests/test_pattern_detector.py covering:

Pattern dataclass

  • to_dict() roundtrip has all expected keys

Helper methods

  • Empty session list returns 0.0
  • Error rate: sessions with errors / total sessions
  • Tool failure rate: total_errors / total_tool_calls

detect_error_rate_trends()

  • No pattern when recent rate <= baseline
  • Warning when increase exceeds threshold
  • Critical when increase exceeds 2x threshold
  • Handles zero baseline rate

detect_tool_failure_frequency()

  • No pattern below threshold
  • Pattern detected when rate spikes
  • Severity escalation to critical

detect_new_failure_modes()

  • No pattern when recent errors <= 2x baseline
  • Warning when recent exceeds 2x baseline
  • Critical when recent exceeds 3x baseline

detect_confidence_drops()

  • No pattern when drop is below threshold
  • Pattern when replay_value drops significantly

detect_all_patterns()

  • Empty list returns empty
  • Groups sessions by agent correctly
  • Sorts critical before warning
  • Returns no patterns when baseline too small (< 3 sessions)

Validation

python3 -m pytest -q tests/test_pattern_detector.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    auto-workAI auto-worker queueenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions