(events: FailureEvent[], opts: DetectOpts = DEFAULT_DETECT)
| 70 | |
| 71 | /** Cluster events by signature and return only the patterns that RECUR enough to learn. */ |
| 72 | export function detectFailurePatterns(events: FailureEvent[], opts: DetectOpts = DEFAULT_DETECT): FailurePattern[] { |
| 73 | const by = new Map<string, { tool: string; count: number; tasks: Set<string>; sample: string }>(); |
| 74 | for (const e of events) { |
| 75 | let g = by.get(e.signature); |
| 76 | if (!g) { g = { tool: e.tool, count: 0, tasks: new Set(), sample: e.sample }; by.set(e.signature, g); } |
| 77 | g.count++; |
| 78 | g.tasks.add(e.task); |
| 79 | } |
| 80 | const out: FailurePattern[] = []; |
| 81 | for (const [signature, g] of by) { |
| 82 | if (g.count >= opts.minOccurrences && g.tasks.size >= opts.minDistinctTasks) { |
| 83 | out.push({ signature, tool: g.tool, count: g.count, distinctTasks: g.tasks.size, sample: g.sample }); |
| 84 | } |
| 85 | } |
| 86 | // Most-repeated first. |
| 87 | return out.sort((a, b) => b.count - a.count); |
| 88 | } |
| 89 | |
| 90 | /** Templated, DETERMINISTIC caution for a recurring failure. Never LLM-phrased. */ |
| 91 | export function buildLesson(p: FailurePattern): string { |
no test coverage detected