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 | """Structured data models for error analysis.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from dataclasses import dataclass, field |
| 6 | from enum import StrEnum |
| 7 | from typing import Literal |
| 8 | |
| 9 | Confidence = Literal["high", "medium", "low"] |
| 10 | |
| 11 | |
| 12 | class ConfidenceLevel(StrEnum): |
| 13 | """Confidence levels for rule matches.""" |
| 14 | |
| 15 | HIGH = "high" |
| 16 | MEDIUM = "medium" |
| 17 | LOW = "low" |
| 18 | |
| 19 | |
| 20 | @dataclass(frozen=True, slots=True) |
| 21 | class Pattern: |
| 22 | """A single matching signal within a rule. |
| 23 | |
| 24 | Attributes: |
| 25 | value: Literal fragment or regex source. |
| 26 | kind: How the pattern is applied. |
| 27 | weight: Score contribution on match. |
| 28 | required: If True, the rule cannot match without this signal. |
| 29 | """ |
| 30 | |
| 31 | value: str |
| 32 | kind: Literal["exact", "regex", "tool", "generic"] = "exact" |
| 33 | weight: int | None = None |
| 34 | required: bool = False |
| 35 | |
| 36 | def score_value(self) -> int: |
| 37 | if self.weight is not None: |
| 38 | return self.weight |
| 39 | defaults = { |
| 40 | "exact": 5, |
| 41 | "regex": 3, |
| 42 | "tool": 2, |
| 43 | "generic": 1, |
| 44 | } |
| 45 | return defaults[self.kind] |
| 46 | |
| 47 | |
| 48 | @dataclass(frozen=True, slots=True) |
| 49 | class Rule: |
| 50 | """A deterministic local error-explanation rule.""" |
| 51 | |
| 52 | id: str |
| 53 | tool: str |
| 54 | title: str |
| 55 | why: str |
| 56 | checks: tuple[str, ...] |
| 57 | fixes: tuple[str, ...] |
| 58 | patterns: tuple[Pattern, ...] |
| 59 | confidence: Confidence = "high" |
| 60 | references: tuple[str, ...] = () |
| 61 | secondary: tuple[str, ...] = () |
| 62 | """Extra literal fragments that boost score (+2 each) when present.""" |
| 63 | |
| 64 | |
| 65 | @dataclass(frozen=True, slots=True) |
| 66 | class ErrorMatch: |
| 67 | """Result of matching input against a rule.""" |
| 68 | |
| 69 | rule_id: str |
| 70 | tool: str |
| 71 | title: str |
| 72 | why: str |
| 73 | checks: list[str] |
| 74 | fixes: list[str] |
| 75 | confidence: Confidence |
| 76 | references: list[str] = field(default_factory=list) |
| 77 | score: int = 0 |
| 78 | |
| 79 | |
| 80 | @dataclass(frozen=True, slots=True) |
| 81 | class MatchCandidate: |
| 82 | """Internal scored candidate before selecting the best match.""" |
| 83 | |
| 84 | rule: Rule |
| 85 | score: int |
| 86 | |
| 87 | |
| 88 | @dataclass(frozen=True, slots=True) |
| 89 | class AnalysisResult: |
| 90 | """Full analysis outcome for formatting / JSON.""" |
| 91 | |
| 92 | matched: bool |
| 93 | match: ErrorMatch | None = None |
| 94 | alternatives: list[MatchCandidate] = field(default_factory=list) |
| 95 | normalized_preview: str = "" |
| 96 | truncated: bool = False |