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 | """Input normalization and secret redaction.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import re |
| 6 | from dataclasses import dataclass |
| 7 | |
| 8 | # Default max input size (100 KB). |
| 9 | DEFAULT_MAX_BYTES = 100 * 1024 |
| 10 | # When truncating, keep this many bytes from each end. |
| 11 | TRUNCATION_KEEP = 40 * 1024 |
| 12 | |
| 13 | # CSI / OSC and other common ANSI escape sequences. |
| 14 | _ANSI_RE = re.compile( |
| 15 | r"(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F])|" # Fe / C1 |
| 16 | r"(?:\x1B\[[0-?]*[ -/]*[@-~])|" # CSI |
| 17 | r"(?:\x1B\][^\x07\x1B]*(?:\x07|\x1B\\))|" # OSC |
| 18 | r"(?:\x1B[PX^_][^\x1B]*\x1B\\)" # DCS/PM/APC/SOS |
| 19 | ) |
| 20 | |
| 21 | # Common prompt / shell prefixes (line-start). |
| 22 | _PROMPT_RE = re.compile( |
| 23 | r"^(?:" |
| 24 | r"\$\s+|" |
| 25 | r"#\s+|" |
| 26 | r">\s+|" |
| 27 | r"PS\s+[A-Z]:[^>]*>\s*|" |
| 28 | r"[A-Za-z]:\\[^>]*>\s*|" |
| 29 | r"[\w.-]+@[\w.-]+:[~/\w.-]*[$#]\s+" |
| 30 | r")", |
| 31 | re.MULTILINE, |
| 32 | ) |
| 33 | |
| 34 | # Timestamps like [2024-01-15 12:34:56] or 2024-01-15T12:34:56Z |
| 35 | _TIMESTAMP_RE = re.compile( |
| 36 | r"(?:" |
| 37 | r"\[\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?\]\s*|" |
| 38 | r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?\s+" |
| 39 | r")" |
| 40 | ) |
| 41 | |
| 42 | # Secret patterns — value groups are redacted. |
| 43 | _SECRET_PATTERNS: list[tuple[re.Pattern[str], str]] = [ |
| 44 | ( |
| 45 | re.compile( |
| 46 | r"(?i)(Authorization:\s*Bearer\s+)(\S+)", |
| 47 | ), |
| 48 | r"\1[REDACTED]", |
| 49 | ), |
| 50 | ( |
| 51 | re.compile( |
| 52 | r"(?i)((?:OPENAI|ANTHROPIC|GEMINI|GOOGLE|AWS|AZURE|GITHUB|GITLAB|" |
| 53 | r"STRIPE|TWILIO|SENDGRID|SLACK|DISCORD|NPM|HUGGINGFACE)" |
| 54 | r"[_-]?(?:API[_-]?KEY|SECRET|TOKEN|ACCESS[_-]?KEY|SECRET[_-]?ACCESS[_-]?KEY)" |
| 55 | r"\s*[=:]\s*)([^\s\"']+)", |
| 56 | ), |
| 57 | r"\1[REDACTED]", |
| 58 | ), |
| 59 | ( |
| 60 | re.compile( |
| 61 | r"(?i)((?:AWS_SECRET_ACCESS_KEY|AWS_ACCESS_KEY_ID)\s*[=:]\s*)([^\s\"']+)", |
| 62 | ), |
| 63 | r"\1[REDACTED]", |
| 64 | ), |
| 65 | ( |
| 66 | re.compile( |
| 67 | r"(?i)((?:password|passwd|pwd|secret|token|api[_-]?key|access[_-]?token)" |
| 68 | r"\s*[=:]\s*)([^\s\"']+)", |
| 69 | ), |
| 70 | r"\1[REDACTED]", |
| 71 | ), |
| 72 | ( |
| 73 | re.compile( |
| 74 | r"(?i)((?:mongodb|postgres|postgresql|mysql|redis|amqp|https?)://)" |
| 75 | r"([^/\s:@]+):([^/\s@]+)@", |
| 76 | ), |
| 77 | r"\1\2:[REDACTED]@", |
| 78 | ), |
| 79 | ( |
| 80 | re.compile(r"(?i)(sk-[a-zA-Z0-9]{20,})"), |
| 81 | "[REDACTED]", |
| 82 | ), |
| 83 | ( |
| 84 | re.compile(r"(?i)(ghp_[a-zA-Z0-9]{20,})"), |
| 85 | "[REDACTED]", |
| 86 | ), |
| 87 | ( |
| 88 | re.compile(r"(?i)(xox[baprs]-[a-zA-Z0-9-]{10,})"), |
| 89 | "[REDACTED]", |
| 90 | ), |
| 91 | ] |
| 92 | |
| 93 | _MULTI_BLANK_RE = re.compile(r"\n{3,}") |
| 94 | _MULTI_SPACE_RE = re.compile(r"[^\S\n]{2,}") |
| 95 | |
| 96 | |
| 97 | @dataclass(frozen=True, slots=True) |
| 98 | class NormalizedInput: |
| 99 | """Normalized error text ready for matching.""" |
| 100 | |
| 101 | text: str |
| 102 | truncated: bool = False |
| 103 | original_length: int = 0 |
| 104 | |
| 105 | |
| 106 | def redact_secrets(text: str) -> str: |
| 107 | """Redact common secret patterns from text.""" |
| 108 | result = text |
| 109 | for pattern, replacement in _SECRET_PATTERNS: |
| 110 | result = pattern.sub(replacement, result) |
| 111 | return result |
| 112 | |
| 113 | |
| 114 | def strip_ansi(text: str) -> str: |
| 115 | """Remove ANSI escape sequences.""" |
| 116 | return _ANSI_RE.sub("", text) |
| 117 | |
| 118 | |
| 119 | def _truncate(text: str, max_bytes: int) -> tuple[str, bool]: |
| 120 | encoded = text.encode("utf-8", errors="replace") |
| 121 | if len(encoded) <= max_bytes: |
| 122 | return text, False |
| 123 | |
| 124 | keep = min(TRUNCATION_KEEP, max_bytes // 2) |
| 125 | head = encoded[:keep].decode("utf-8", errors="replace") |
| 126 | tail = encoded[-keep:].decode("utf-8", errors="replace") |
| 127 | marker = "\n\n[... truncated ...]\n\n" |
| 128 | return head + marker + tail, True |
| 129 | |
| 130 | |
| 131 | def normalize( |
| 132 | raw: str, |
| 133 | *, |
| 134 | max_bytes: int = DEFAULT_MAX_BYTES, |
| 135 | redact: bool = True, |
| 136 | ) -> NormalizedInput: |
| 137 | """Normalize raw error input for deterministic matching. |
| 138 | |
| 139 | Steps: |
| 140 | 1. Replace null bytes and normalize newlines |
| 141 | 2. Optionally truncate oversized input |
| 142 | 3. Strip ANSI codes |
| 143 | 4. Redact secrets |
| 144 | 5. Strip prompt / timestamp prefixes where safe |
| 145 | 6. Collapse excessive whitespace / blank lines |
| 146 | """ |
| 147 | original_length = len(raw.encode("utf-8", errors="replace")) |
| 148 | |
| 149 | text = raw.replace("\x00", "") |
| 150 | text = text.replace("\r\n", "\n").replace("\r", "\n") |
| 151 | |
| 152 | text, truncated = _truncate(text, max_bytes) |
| 153 | |
| 154 | text = strip_ansi(text) |
| 155 | |
| 156 | if redact: |
| 157 | text = redact_secrets(text) |
| 158 | |
| 159 | text = _PROMPT_RE.sub("", text) |
| 160 | text = _TIMESTAMP_RE.sub("", text) |
| 161 | |
| 162 | text = _MULTI_SPACE_RE.sub(" ", text) |
| 163 | text = _MULTI_BLANK_RE.sub("\n\n", text) |
| 164 | text = text.strip() |
| 165 | |
| 166 | return NormalizedInput( |
| 167 | text=text, |
| 168 | truncated=truncated, |
| 169 | original_length=original_length, |
| 170 | ) |