|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Parse + harden duplicate/related triage model output. |
| 5 | + * Related matches are high-noise; prefer empty over weak overlap. |
| 6 | + * Each related entry must carry its own concrete shared-failure reason. |
| 7 | + */ |
| 8 | + |
| 9 | +const WEAK_RELATED_REASON_RE = |
| 10 | + /\b(?:somewhat|broadly|loosely|vaguely)\s+related\b|\bboth\s+(?:issues?\s+)?pertain\s+to\s+errors?\b|\bsame\s+(?:client|app)\b|\berrors?\s+in\s+general\b|\bgeneral\s+proxy\s+errors?\b|\bHTTP\s+error\b/i; |
| 11 | + |
| 12 | +/** Well-known errno / syscall failure tokens (allowlist only — never E[A-Z]{4,}). */ |
| 13 | +const KNOWN_ERRNO_RE = |
| 14 | + /\b(?:ECONNRESET|ECONNREFUSED|ETIMEDOUT|ENOTFOUND|EPIPE|EAI_AGAIN|ECONNABORTED|EHOSTUNREACH|ENETUNREACH|EADDRINUSE)\b/; |
| 15 | + |
| 16 | +/** |
| 17 | + * HTTP statuses require status-indicating context so bare issue/PR numbers |
| 18 | + * (e.g. 410, 503 as ticket ids) are not treated as failure evidence. |
| 19 | + */ |
| 20 | +const HTTP_STATUS_RE = |
| 21 | + /\b(?:(?:HTTP(?:\s+status)?(?:\s+code)?|status(?:\s+code)?)\s+|(?:returns?|returning|got|getting|receive[ds]?|reports?|reporting|code)\s+)([1-5]\d\d)\b/gi; |
| 22 | + |
| 23 | +/** One-sided attribution of a concrete failure to only one issue. */ |
| 24 | +const ONE_SIDED_ATTRIBUTION_RE = |
| 25 | + /\b(?:only\s+(?:the\s+)?(?:new\s+)?(?:issue|one|first|second|other)|only\s+one|just\s+the\s+(?:first|second|new|other)|(?:issue\s+#?\d+\s+)?alone|appearing\s+only(?:\s+in)?|appears?\s+only(?:\s+in)?|exclusive\s+to|the\s+(?:other|existing(?:\s+issue)?)\s+does\s+not|(?:does|do)\s+not\s+(?:show|report|include|return|have))\b/i; |
| 26 | + |
| 27 | +/** |
| 28 | + * Issue-role attribution between a shared binder and a concrete token means the |
| 29 | + * token is not shared evidence (e.g. "both fail: issue 410 returns HTTP 500"). |
| 30 | + */ |
| 31 | +const ISSUE_SPECIFIC_ATTR_RE = |
| 32 | + /\b(?:issue\s+#?\d+|the\s+(?:new|current|present)\s+issue|this\s+issue|the\s+(?:first|second|other)(?:\s+issue)?)\b/i; |
| 33 | + |
| 34 | +const BOTH_FAILURE_VERB_RE = |
| 35 | + /\bboth(?:\s+issues?)?(?:\s+\w+){0,6}\s+(?:return|report|show|have|hit|fail|reproduce|receive|share)\b/i; |
| 36 | + |
| 37 | +function extractHttpStatuses(text) { |
| 38 | + const found = []; |
| 39 | + const re = new RegExp(HTTP_STATUS_RE.source, "gi"); |
| 40 | + let match; |
| 41 | + while ((match = re.exec(String(text || ""))) !== null) { |
| 42 | + found.push(match[1]); |
| 43 | + } |
| 44 | + return [...new Set(found)]; |
| 45 | +} |
| 46 | + |
| 47 | +function extractErrnoTokens(text) { |
| 48 | + const found = []; |
| 49 | + const copy = new RegExp(KNOWN_ERRNO_RE.source, "g"); |
| 50 | + let match; |
| 51 | + while ((match = copy.exec(String(text || ""))) !== null) { |
| 52 | + found.push(match[0].toUpperCase()); |
| 53 | + } |
| 54 | + return [...new Set(found)]; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Locate concrete failure signature spans inside a clause. |
| 59 | + * @returns {{ start: number, end: number, text: string }[]} |
| 60 | + */ |
| 61 | +function findSignatureSpans(text) { |
| 62 | + const spans = []; |
| 63 | + const pushMatches = (re) => { |
| 64 | + const copy = new RegExp(re.source, re.flags.includes("g") ? re.flags : `${re.flags}g`); |
| 65 | + let match; |
| 66 | + while ((match = copy.exec(text)) !== null) { |
| 67 | + spans.push({ |
| 68 | + start: match.index, |
| 69 | + end: match.index + match[0].length, |
| 70 | + text: match[0], |
| 71 | + }); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + pushMatches(KNOWN_ERRNO_RE); |
| 76 | + pushMatches(HTTP_STATUS_RE); |
| 77 | + pushMatches(/\bField required\b/gi); |
| 78 | + pushMatches(/\bcontent\[\d+\]/g); |
| 79 | + pushMatches(/\b[\w]+\.[\w.]+\.(?:text|content|type)\b/g); |
| 80 | + |
| 81 | + if (/\b(?:taskkill|ghost\s+LISTEN|listen(?:-|\s)?port)\b/i.test(text) && /\b\d{2,5}\b/.test(text)) { |
| 82 | + const m = text.match(/\b(?:taskkill|ghost\s+LISTEN|listen(?:-|\s)?port)\b/i); |
| 83 | + if (m && m.index != null) { |
| 84 | + spans.push({ start: m.index, end: m.index + m[0].length, text: m[0] }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + spans.sort((a, b) => a.start - b.start || a.end - b.end); |
| 89 | + return spans; |
| 90 | +} |
| 91 | + |
| 92 | +function hasConcreteFailureToken(text) { |
| 93 | + return findSignatureSpans(String(text || "")).length > 0 |
| 94 | + || (/\breproduc(?:e|es|ed|tion)\b/i.test(text) && /\b(?:when|if|after|on)\b/i.test(text)); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * True when the reason attributes distinct concrete failures to different issues. |
| 99 | + */ |
| 100 | +function hasDistinctFailureSignatures(text) { |
| 101 | + const statuses = extractHttpStatuses(text); |
| 102 | + if (statuses.length > 1) return true; |
| 103 | + |
| 104 | + const errnos = extractErrnoTokens(text); |
| 105 | + if (errnos.length > 1) return true; |
| 106 | + |
| 107 | + // Mixed concrete failure types (HTTP status + errno) are not a shared signature. |
| 108 | + if (statuses.length >= 1 && errnos.length >= 1) return true; |
| 109 | + |
| 110 | + if (/\b(?:the\s+first|one)\b[\s\S]{0,100}\b(?:the\s+second|the\s+other)\b/i.test(text)) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + if (/\bwhereas\b/i.test(text)) return true; |
| 114 | + if (/\brespectively\b/i.test(text)) return true; |
| 115 | + if (/\bwhile\s+(?:the\s+)?(?:other|second|issue)\b/i.test(text)) return true; |
| 116 | + if (/\bone\b[^.]{0,80}\band\s+the\s+other\b/i.test(text)) return true; |
| 117 | + if (/\balone\s+reports\b/i.test(text)) return true; |
| 118 | + if (/\bseparate\s+\d{3}\s+problem\b/i.test(text)) return true; |
| 119 | + if (/\b(?:failures?|root\s+causes?|status(?:es)?|errors?|symptoms?)\s+differ\b/i.test(text)) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + if (/\bdifferent\s+(?:failure|root\s+cause|status|error|problem|symptom)s?\b/i.test(text)) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + return false; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Require a shared quantifier in the same clause as the concrete signature, |
| 130 | + * with no one-sided attribution between the binder and the token (or immediately |
| 131 | + * after the token). Generic "both fail" + a later one-issue token does not pass. |
| 132 | + */ |
| 133 | +function clauseBindsSharedToSignature(clause) { |
| 134 | + const signatures = findSignatureSpans(clause); |
| 135 | + if (!signatures.length) { |
| 136 | + // Reproduction phrases count as signatures when shared-bound. |
| 137 | + if (!(/\breproduc(?:e|es|ed|tion)\b/i.test(clause) && /\b(?:when|if|after|on)\b/i.test(clause))) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + const spans = signatures.length |
| 143 | + ? signatures |
| 144 | + : [{ start: clause.search(/\breproduc/i), end: clause.length, text: "repro" }]; |
| 145 | + |
| 146 | + for (const sig of spans) { |
| 147 | + if (sig.start < 0) continue; |
| 148 | + |
| 149 | + const binders = []; |
| 150 | + const binderRe = /\b(?:both(?:\s+issues?)?|(?:the\s+)?issues?\s+share|share|shared|same|identical(?:ly)?)\b/gi; |
| 151 | + let match; |
| 152 | + while ((match = binderRe.exec(clause)) !== null) { |
| 153 | + if (match.index < sig.start) binders.push(match); |
| 154 | + } |
| 155 | + |
| 156 | + for (const binder of binders) { |
| 157 | + const between = clause.slice(binder.index, sig.start); |
| 158 | + const trail = clause.slice(sig.end, Math.min(clause.length, sig.end + 72)); |
| 159 | + if (ONE_SIDED_ATTRIBUTION_RE.test(between) || ONE_SIDED_ATTRIBUTION_RE.test(trail)) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + const binderText = binder[0]; |
| 164 | + if (/^both\b/i.test(binderText)) { |
| 165 | + if (!BOTH_FAILURE_VERB_RE.test(clause.slice(binder.index, sig.end))) continue; |
| 166 | + // Generic "both issues fail/have/show …" must not validate a later |
| 167 | + // issue-specific token (e.g. "issue 410 returns HTTP 500"). |
| 168 | + if (ISSUE_SPECIFIC_ATTR_RE.test(between)) continue; |
| 169 | + } else if (/^same$/i.test(binderText)) { |
| 170 | + // "same adapter" is not enough; require same … error/failure/Field required/status. |
| 171 | + if (!/\bsame\b[\s\S]{0,80}\b(?:error|failure|status|fault|exception|signature|Field required|(?:HTTP(?:\s+status)?(?:\s+code)?|status(?:\s+code)?|returns?|got|code)\s+[1-5]\d\d|ECONNRESET|ECONNREFUSED|ETIMEDOUT|ENOTFOUND|EPIPE|EAI_AGAIN|ECONNABORTED|EHOSTUNREACH|ENETUNREACH|EADDRINUSE)\b/i |
| 172 | + .test(clause.slice(binder.index))) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + } else if (/share/i.test(binderText)) { |
| 176 | + if (!/\b(?:share|shared)\b/i.test(between + clause.slice(sig.start, sig.end))) continue; |
| 177 | + } |
| 178 | + |
| 179 | + return true; |
| 180 | + } |
| 181 | + } |
| 182 | + return false; |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * Positive evidence that two issues share a concrete failure signature. |
| 187 | + * The shared comparison must bind directly to the concrete token/description |
| 188 | + * in the same clause; component overlap alone is never enough. |
| 189 | + */ |
| 190 | +function hasConcreteRelatedSignature(reason) { |
| 191 | + const text = String(reason || ""); |
| 192 | + if (!text) return false; |
| 193 | + if (hasDistinctFailureSignatures(text)) return false; |
| 194 | + |
| 195 | + const clauses = text.split(/[.;]+/).map((part) => part.trim()).filter(Boolean); |
| 196 | + return clauses.some((clause) => clauseBindsSharedToSignature(clause)); |
| 197 | +} |
| 198 | + |
| 199 | +function sanitizeReason(raw) { |
| 200 | + return String(raw || "") |
| 201 | + .replace(/[\u0000-\u001f\u007f]/g, " ") |
| 202 | + .replace(/@/g, "\0AT\0") |
| 203 | + .replace(/[`*_~<>[\]()#|]/g, "") |
| 204 | + .replace(/\0AT\0/g, "(at)") |
| 205 | + .replace(/\s+/g, " ") |
| 206 | + .trim() |
| 207 | + .slice(0, 240); |
| 208 | +} |
| 209 | + |
| 210 | +function normalizeIssueNumber(entry, { currentNumber, knownNumbers }) { |
| 211 | + const cur = String(currentNumber); |
| 212 | + const known = knownNumbers instanceof Set |
| 213 | + ? knownNumbers |
| 214 | + : new Set((knownNumbers || []).map(String)); |
| 215 | + const match = String(entry ?? "").trim().match(/^#?(\d+)$/); |
| 216 | + if (!match) return ""; |
| 217 | + const number = match[1]; |
| 218 | + if (!number || number === cur || !known.has(number)) return ""; |
| 219 | + return number; |
| 220 | +} |
| 221 | + |
| 222 | +function normalizeIssueNumbers(value, { currentNumber, knownNumbers }) { |
| 223 | + return [...new Set( |
| 224 | + (Array.isArray(value) ? value : []) |
| 225 | + .map((entry) => normalizeIssueNumber(entry, { currentNumber, knownNumbers })) |
| 226 | + .filter(Boolean), |
| 227 | + )]; |
| 228 | +} |
| 229 | + |
| 230 | +/** |
| 231 | + * Prefer per-entry {number, reason}. Bare issue-number strings are ignored |
| 232 | + * (shared top-level reasons are ambiguous across multiple related IDs). |
| 233 | + */ |
| 234 | +function normalizeRelatedEntries(value, { currentNumber, knownNumbers }) { |
| 235 | + if (!Array.isArray(value)) return []; |
| 236 | + const out = []; |
| 237 | + const seen = new Set(); |
| 238 | + for (const entry of value) { |
| 239 | + let number = ""; |
| 240 | + let reason = ""; |
| 241 | + if (entry && typeof entry === "object" && !Array.isArray(entry)) { |
| 242 | + number = normalizeIssueNumber(entry.number ?? entry.issue ?? entry.id, { |
| 243 | + currentNumber, |
| 244 | + knownNumbers, |
| 245 | + }); |
| 246 | + reason = sanitizeReason(entry.reason ?? entry.why ?? ""); |
| 247 | + } else { |
| 248 | + // Legacy string / number forms have no per-entry reason — drop them. |
| 249 | + continue; |
| 250 | + } |
| 251 | + if (!number || seen.has(number)) continue; |
| 252 | + seen.add(number); |
| 253 | + out.push({ number, reason }); |
| 254 | + } |
| 255 | + return out; |
| 256 | +} |
| 257 | + |
| 258 | +function parseAiJson(raw) { |
| 259 | + const text = String(raw || "").trim(); |
| 260 | + if (!text) return null; |
| 261 | + try { |
| 262 | + return JSON.parse(text); |
| 263 | + } catch { |
| 264 | + try { |
| 265 | + return JSON.parse( |
| 266 | + text.replace(/^```(?:json)?\s*/i, "").replace(/\s*```\s*$/, "").trim(), |
| 267 | + ); |
| 268 | + } catch { |
| 269 | + return null; |
| 270 | + } |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +/** |
| 275 | + * Validate each related entry independently. |
| 276 | + * Weak shared-client wording without a concrete shared failure is dropped. |
| 277 | + */ |
| 278 | +function hardenRelatedMatches({ duplicates, related }) { |
| 279 | + const dupes = Array.isArray(duplicates) ? duplicates : []; |
| 280 | + const dupeSet = new Set(dupes.map(String)); |
| 281 | + const relatedOut = []; |
| 282 | + |
| 283 | + for (const entry of Array.isArray(related) ? related : []) { |
| 284 | + const number = String(entry?.number || ""); |
| 285 | + if (!number || dupeSet.has(number)) continue; |
| 286 | + const safeReason = sanitizeReason(entry?.reason); |
| 287 | + if (!safeReason || safeReason.length < 24) continue; |
| 288 | + // WEAK_RELATED_REASON_RE alone is insufficient: concrete-signature gating |
| 289 | + // already rejects weak overlap, so a separate weak+!concrete branch is dead. |
| 290 | + if (!hasConcreteRelatedSignature(safeReason)) continue; |
| 291 | + relatedOut.push({ number, reason: safeReason }); |
| 292 | + if (relatedOut.length >= 3) break; |
| 293 | + } |
| 294 | + |
| 295 | + return { |
| 296 | + duplicates: dupes, |
| 297 | + related: relatedOut, |
| 298 | + }; |
| 299 | +} |
| 300 | + |
| 301 | +function parseTriageMatches(raw, { currentNumber, knownNumbers }) { |
| 302 | + const parsed = parseAiJson(raw); |
| 303 | + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { |
| 304 | + return null; |
| 305 | + } |
| 306 | + |
| 307 | + const duplicates = normalizeIssueNumbers(parsed.duplicates ?? parsed.issues, { |
| 308 | + currentNumber, |
| 309 | + knownNumbers, |
| 310 | + }).slice(0, 5); |
| 311 | + |
| 312 | + const relatedEntries = normalizeRelatedEntries(parsed.related, { |
| 313 | + currentNumber, |
| 314 | + knownNumbers, |
| 315 | + }).filter((entry) => !duplicates.includes(entry.number)); |
| 316 | + |
| 317 | + const hardened = hardenRelatedMatches({ |
| 318 | + duplicates, |
| 319 | + related: relatedEntries, |
| 320 | + }); |
| 321 | + |
| 322 | + const overallReason = sanitizeReason(parsed.reason); |
| 323 | + |
| 324 | + if (!hardened.duplicates.length && !hardened.related.length) { |
| 325 | + return null; |
| 326 | + } |
| 327 | + |
| 328 | + return { |
| 329 | + duplicates: hardened.duplicates, |
| 330 | + related: hardened.related, |
| 331 | + reason: overallReason || "Potential matches returned without a reason.", |
| 332 | + }; |
| 333 | +} |
| 334 | + |
| 335 | +module.exports = { |
| 336 | + WEAK_RELATED_REASON_RE, |
| 337 | + ONE_SIDED_ATTRIBUTION_RE, |
| 338 | + hasDistinctFailureSignatures, |
| 339 | + hasConcreteRelatedSignature, |
| 340 | + hasConcreteFailureToken, |
| 341 | + clauseBindsSharedToSignature, |
| 342 | + findSignatureSpans, |
| 343 | + extractHttpStatuses, |
| 344 | + extractErrnoTokens, |
| 345 | + sanitizeReason, |
| 346 | + normalizeIssueNumber, |
| 347 | + normalizeIssueNumbers, |
| 348 | + normalizeRelatedEntries, |
| 349 | + parseAiJson, |
| 350 | + hardenRelatedMatches, |
| 351 | + parseTriageMatches, |
| 352 | +}; |
0 commit comments