Feature: Predictive Error Prevention — Layer 4 Error Pattern Database
  As a plan executor
  I want historical failure patterns to be consulted before execution
  So that preventive guidance prevents recurring errors

  Scenario: pep-record Recording a new failure pattern creates an entry
    Given a fresh error pattern service
    When I record a failure with pattern "Race condition in payment module" and failure "Payment confirmation timeout"
    Then the error pattern database should contain 1 pattern
    And the pattern should have frequency 1

  Scenario: pep-record Recording the same pattern again increments frequency
    Given a fresh error pattern service
    When I record a failure with pattern "Race condition in payment module" and failure "Payment confirmation timeout"
    And I record a failure with pattern "Race condition in payment module" and failure "Idempotency key collision"
    Then the error pattern database should contain 1 pattern
    And the pattern should have frequency 2
    And the pattern should have 2 historical failures

  Scenario: pep-match Matching context text against known patterns returns guidance
    Given a fresh error pattern service
    And a recorded pattern "Async conversion" with preventive check "Add explicit transaction boundaries"
    When I match context "We need to perform async conversion in the payment module"
    Then preventive guidance should contain "Add explicit transaction boundaries"

  Scenario: pep-match No matching patterns returns empty guidance
    Given a fresh error pattern service
    And a recorded pattern "Async conversion" with preventive check "Add transaction boundaries"
    When I match context "Simple refactoring of the UI component"
    Then preventive guidance should be empty

  Scenario: pep-format Preventive guidance formats for actor context
    Given a fresh error pattern service
    And a recorded pattern "Memory leak" with preventive check "Check resource cleanup"
    And a recorded pattern "Memory leak" with preventive check "Verify dispose patterns"
    When I match context "Fix the memory leak in the cache service"
    Then the formatted guidance should contain "PREVENTIVE GUIDANCE"
    And the formatted guidance should contain "Check resource cleanup"

  Scenario: pep-stats Statistics report pattern counts
    Given a fresh error pattern service
    And a recorded pattern "Race condition" with preventive check "Add locks"
    And a recorded pattern "Memory leak" with preventive check "Check cleanup"
    When I request error pattern statistics
    Then the statistics should show 2 total patterns

  Scenario: pep-extract Keywords are automatically extracted from pattern text
    Given a fresh error pattern service
    When I record a failure with pattern "Race condition in payment processing module" and failure "Timeout"
    Then the recorded pattern should have keywords including "race"
    And the recorded pattern should have keywords including "payment"

  Scenario: pep-multi Multiple patterns match and checks are deduplicated
    Given a fresh error pattern service
    And a recorded pattern "async conversion" with preventive check "Add transaction boundaries" and keywords "async,conversion"
    And a recorded pattern "async timeout" with preventive check "Add retry logic" and keywords "async,timeout"
    When I match context "Handle async conversion with proper timeout handling"
    Then preventive guidance should contain "Add transaction boundaries"
    And preventive guidance should contain "Add retry logic"

  Scenario: pep-repo-get Repository get returns pattern by ID
    Given a fresh error pattern service
    When I record a failure with pattern "Test pattern" and failure "Test failure"
    Then the recorded pattern can be retrieved by its ID

  Scenario: pep-repo-delete Repository delete removes a pattern
    Given a fresh error pattern service
    When I record a failure with pattern "Deletable pattern" and failure "Will be deleted"
    And I delete the last recorded pattern
    Then the error pattern database should contain 0 patterns

  Scenario: pep-fallback-match Pattern without keywords matches by description
    Given a fresh error pattern service
    And a recorded pattern with no keywords for "timeout error" with check "Add timeout handling"
    When I match context "We encountered a timeout error in production"
    Then preventive guidance should contain "Add timeout handling"
