Developer tools dump stack traces, exit codes, and jargon. Figuring out *what to do next* often means searching the same Stack Overflow answers again.
| 1 | """Deterministic rule matching engine.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import re |
| 6 | from functools import lru_cache |
| 7 | |
| 8 | from why_failed.models import ( |
| 9 | AnalysisResult, |
| 10 | ErrorMatch, |
| 11 | MatchCandidate, |
| 12 | Pattern, |
| 13 | Rule, |
| 14 | ) |
| 15 | from why_failed.normalize import NormalizedInput, normalize |
| 16 | from why_failed.registry import get_all_rules |
| 17 | |
| 18 | |
| 19 | @lru_cache(maxsize=512) |
| 20 | def _compile_regex(source: str) -> re.Pattern[str]: |
| 21 | return re.compile(source, re.IGNORECASE | re.MULTILINE) |
| 22 | |
| 23 | |
| 24 | def _pattern_matches(text: str, text_lower: str, pattern: Pattern) -> bool: |
| 25 | if pattern.kind == "regex": |
| 26 | return _compile_regex(pattern.value).search(text) is not None |
| 27 | return pattern.value.lower() in text_lower |
| 28 | |
| 29 | |
| 30 | def score_rule(text: str, rule: Rule) -> int | None: |
| 31 | """Score a rule against normalized text. |
| 32 | |
| 33 | Returns None if required patterns are missing or score is zero. |
| 34 | """ |
| 35 | text_lower = text.lower() |
| 36 | score = 0 |
| 37 | matched_any = False |
| 38 | |
| 39 | for pattern in rule.patterns: |
| 40 | if _pattern_matches(text, text_lower, pattern): |
| 41 | score += pattern.score_value() |
| 42 | matched_any = True |
| 43 | elif pattern.required: |
| 44 | return None |
| 45 | |
| 46 | if not matched_any: |
| 47 | return None |
| 48 | |
| 49 | for signal in rule.secondary: |
| 50 | if signal.lower() in text_lower: |
| 51 | score += 2 |
| 52 | |
| 53 | return score if score > 0 else None |
| 54 | |
| 55 | |
| 56 | def find_candidates( |
| 57 | text: str, |
| 58 | rules: list[Rule] | None = None, |
| 59 | *, |
| 60 | min_score: int = 3, |
| 61 | ) -> list[MatchCandidate]: |
| 62 | """Return all matching candidates sorted by score descending, then rule id.""" |
| 63 | if rules is None: |
| 64 | rules = get_all_rules() |
| 65 | |
| 66 | candidates: list[MatchCandidate] = [] |
| 67 | for rule in rules: |
| 68 | scored = score_rule(text, rule) |
| 69 | if scored is not None and scored >= min_score: |
| 70 | candidates.append(MatchCandidate(rule=rule, score=scored)) |
| 71 | |
| 72 | candidates.sort(key=lambda c: (-c.score, c.rule.id)) |
| 73 | return candidates |
| 74 | |
| 75 | |
| 76 | def candidate_to_match(candidate: MatchCandidate) -> ErrorMatch: |
| 77 | rule = candidate.rule |
| 78 | return ErrorMatch( |
| 79 | rule_id=rule.id, |
| 80 | tool=rule.tool, |
| 81 | title=rule.title, |
| 82 | why=rule.why, |
| 83 | checks=list(rule.checks), |
| 84 | fixes=list(rule.fixes), |
| 85 | confidence=rule.confidence, |
| 86 | references=list(rule.references), |
| 87 | score=candidate.score, |
| 88 | ) |
| 89 | |
| 90 | |
| 91 | def analyze( |
| 92 | raw: str, |
| 93 | *, |
| 94 | rules: list[Rule] | None = None, |
| 95 | ) -> AnalysisResult: |
| 96 | """Full pipeline: normalize → match → structured result.""" |
| 97 | normalized: NormalizedInput = normalize(raw) |
| 98 | if not normalized.text: |
| 99 | return AnalysisResult( |
| 100 | matched=False, |
| 101 | normalized_preview="", |
| 102 | truncated=normalized.truncated, |
| 103 | ) |
| 104 | |
| 105 | candidates = find_candidates(normalized.text, rules) |
| 106 | preview = normalized.text |
| 107 | if len(preview) > 200: |
| 108 | preview = preview[:200] + "…" |
| 109 | |
| 110 | if not candidates: |
| 111 | return AnalysisResult( |
| 112 | matched=False, |
| 113 | alternatives=[], |
| 114 | normalized_preview=preview, |
| 115 | truncated=normalized.truncated, |
| 116 | ) |
| 117 | |
| 118 | best = candidates[0] |
| 119 | alternatives = candidates[1:4] |
| 120 | return AnalysisResult( |
| 121 | matched=True, |
| 122 | match=candidate_to_match(best), |
| 123 | alternatives=alternatives, |
| 124 | normalized_preview=preview, |
| 125 | truncated=normalized.truncated, |
| 126 | ) |
| 127 | |
| 128 | |
| 129 | class LocalRulesProvider: |
| 130 | """Default ErrorProvider backed by the local rule registry.""" |
| 131 | |
| 132 | def analyze(self, error: str) -> ErrorMatch | None: |
| 133 | result = analyze(error) |
| 134 | return result.match |