Files
cleveragents-core/robot/helper_predictive_error_prevention.py
freemo 583e6b7ea2
CI / benchmark-publish (pull_request) Has been skipped
CI / lint (pull_request) Successful in 14s
CI / build (pull_request) Successful in 16s
CI / quality (pull_request) Successful in 17s
CI / security (pull_request) Successful in 36s
CI / typecheck (pull_request) Successful in 54s
CI / unit_tests (pull_request) Successful in 2m39s
CI / integration_tests (pull_request) Successful in 3m10s
CI / docker (pull_request) Successful in 40s
CI / coverage (pull_request) Successful in 5m0s
CI / lint (push) Successful in 14s
CI / build (push) Successful in 15s
CI / quality (push) Successful in 16s
CI / security (push) Successful in 34s
CI / typecheck (push) Successful in 35s
CI / benchmark-regression (push) Has been skipped
CI / unit_tests (push) Successful in 2m23s
CI / docker (push) Successful in 38s
CI / integration_tests (push) Successful in 3m8s
CI / coverage (push) Successful in 5m10s
CI / benchmark-publish (push) Successful in 16m26s
CI / benchmark-regression (pull_request) Successful in 30m12s
feat(correcting-plans): implement Predictive Error Prevention (Layer 4) with Error Pattern Database
Implement spec-mandated Layer 4 Predictive Error Prevention system:

- ErrorPattern domain model with pattern text, historical failures,
  preventive checks, frequency tracking, and keyword-based matching.
- ErrorPatternRepository with in-memory CRUD + context-matching query.
- ErrorPatternService with record_failure(), match_patterns(), and
  get_statistics() methods.
- Wire into plan execution via plan_lifecycle_service pre-execution hook.
- Add error pattern statistics to CLI diagnostics output.

Behave BDD: 11 scenarios covering recording, matching, formatting, stats.
Robot Framework: 3 integration smoke tests.
ASV benchmarks: pattern matching performance.

ISSUES CLOSED: #571
2026-03-08 21:53:21 -04:00

100 lines
3.6 KiB
Python

"""Helper utilities for Predictive Error Prevention Robot integration tests.
Covers Layer 4: Error Pattern Database — recording, matching, and
preventive guidance generation.
"""
from __future__ import annotations
from cleveragents.application.services.error_pattern_service import (
ErrorPatternService,
)
from cleveragents.domain.models.core.error_pattern import ErrorPattern
def _record_and_verify_pattern() -> None:
"""Integration test: record a failure and verify pattern creation."""
service = ErrorPatternService()
pattern = service.record_failure(
pattern_text="Race condition in payment module",
failure_description="Payment confirmation timeout",
preventive_checks=("Add explicit locking",),
keywords=("race", "payment"),
)
assert isinstance(pattern, ErrorPattern), (
f"Expected ErrorPattern, got {type(pattern)}"
)
assert pattern.frequency == 1, f"Expected frequency 1, got {pattern.frequency}"
assert len(pattern.historical_failures) == 1, (
f"Expected 1 historical failure, got {len(pattern.historical_failures)}"
)
assert pattern.pattern == "Race condition in payment module"
stats = service.get_statistics()
assert stats["total_patterns"] == 1, (
f"Expected 1 pattern, got {stats['total_patterns']}"
)
print("record-and-verify-ok")
def _match_patterns_returns_guidance() -> None:
"""Integration test: match context against patterns returns guidance."""
service = ErrorPatternService()
service.record_failure(
pattern_text="Async conversion failure",
failure_description="Timeout during async conversion",
preventive_checks=("Add transaction boundaries", "Use retry logic"),
keywords=("async", "conversion"),
)
guidance = service.match_patterns(
"We need to perform async conversion in the payment module"
)
assert guidance.has_guidance, "Expected guidance to be non-empty"
assert len(guidance.preventive_checks) == 2, (
f"Expected 2 checks, got {len(guidance.preventive_checks)}"
)
assert "Add transaction boundaries" in guidance.preventive_checks
assert "Use retry logic" in guidance.preventive_checks
formatted = guidance.format_for_context()
assert "PREVENTIVE GUIDANCE" in formatted, (
f"Expected header in formatted output: {formatted}"
)
print("match-patterns-guidance-ok")
def _frequency_increments_on_duplicate() -> None:
"""Integration test: duplicate pattern recording increments frequency."""
service = ErrorPatternService()
service.record_failure(
pattern_text="Memory leak in cache",
failure_description="OOM after 24h",
preventive_checks=("Check dispose patterns",),
keywords=("memory", "leak", "cache"),
)
updated = service.record_failure(
pattern_text="Memory leak in cache",
failure_description="High memory usage after 12h",
)
assert updated.frequency == 2, f"Expected frequency 2, got {updated.frequency}"
assert len(updated.historical_failures) == 2, (
f"Expected 2 failures, got {len(updated.historical_failures)}"
)
stats = service.get_statistics()
assert stats["total_patterns"] == 1, (
f"Expected 1 pattern, got {stats['total_patterns']}"
)
assert stats["total_occurrences"] == 2, (
f"Expected 2 occurrences, got {stats['total_occurrences']}"
)
print("frequency-increments-ok")
if __name__ == "__main__":
_record_and_verify_pattern()
_match_patterns_returns_guidance()
_frequency_increments_on_duplicate()
print("all-predictive-error-prevention-tests-passed")