|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +PATTERNS = Path("failure_similarity/failure_patterns.json") |
| 7 | +REPORT_JSON = Path("failure_similarity/failure_similarity_report.json") |
| 8 | +REPORT_MD = Path("failure_similarity/failure_similarity_report.md") |
| 9 | + |
| 10 | + |
| 11 | +def text_for(path): |
| 12 | + return Path(path).read_text().lower() |
| 13 | + |
| 14 | + |
| 15 | +def score_pattern(trace_text, pattern): |
| 16 | + hits = [signal for signal in pattern["signals"] if signal.lower() in trace_text] |
| 17 | + score = round(len(hits) / len(pattern["signals"]), 2) |
| 18 | + confidence = round(min(0.99, 0.60 + score * 0.35), 2) if hits else 0.0 |
| 19 | + return { |
| 20 | + "pattern_id": pattern["id"], |
| 21 | + "failure_family": pattern["family"], |
| 22 | + "similarity": score, |
| 23 | + "confidence": confidence, |
| 24 | + "matched_evidence": hits, |
| 25 | + "likely_root_cause": pattern["likely_root_cause"] |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | +def main(): |
| 30 | + parser = argparse.ArgumentParser(description="Search similar DetTrace replay failure patterns") |
| 31 | + parser.add_argument("trace", help="Path to trace or replay artifact") |
| 32 | + args = parser.parse_args() |
| 33 | + |
| 34 | + trace_text = text_for(args.trace) |
| 35 | + patterns = json.loads(PATTERNS.read_text())["patterns"] |
| 36 | + |
| 37 | + matches = [score_pattern(trace_text, p) for p in patterns] |
| 38 | + matches = [m for m in matches if m["similarity"] > 0] |
| 39 | + matches.sort(key=lambda m: (m["confidence"], m["similarity"], len(m["matched_evidence"])), reverse=True) |
| 40 | + |
| 41 | + best = matches[0] if matches else { |
| 42 | + "pattern_id": "none", |
| 43 | + "failure_family": "unknown", |
| 44 | + "similarity": 0.0, |
| 45 | + "confidence": 0.0, |
| 46 | + "matched_evidence": [], |
| 47 | + "likely_root_cause": "unknown" |
| 48 | + } |
| 49 | + |
| 50 | + report = { |
| 51 | + "input_trace": args.trace, |
| 52 | + "safe_claim": "heuristic replay failure-similarity search; not ML or production incident ranking", |
| 53 | + "most_similar_failure": best, |
| 54 | + "top_matches": matches[:5] |
| 55 | + } |
| 56 | + |
| 57 | + REPORT_JSON.write_text(json.dumps(report, indent=2)) |
| 58 | + REPORT_MD.write_text( |
| 59 | + "# Failure Similarity Search Report\n\n" |
| 60 | + "## Safe claim\n\n" |
| 61 | + f"{report['safe_claim']}\n\n" |
| 62 | + "## Most similar failure\n\n" |
| 63 | + f"- family: `{best['failure_family']}`\n" |
| 64 | + f"- similarity: `{best['similarity']}`\n" |
| 65 | + f"- confidence: `{best['confidence']}`\n" |
| 66 | + f"- likely root cause: `{best['likely_root_cause']}`\n" |
| 67 | + f"- evidence: `{best['matched_evidence']}`\n\n" |
| 68 | + "## Top matches\n\n" |
| 69 | + + "\n".join( |
| 70 | + f"- `{m['failure_family']}` similarity=`{m['similarity']}` confidence=`{m['confidence']}` evidence=`{m['matched_evidence']}` root_cause=`{m['likely_root_cause']}`" |
| 71 | + for m in report["top_matches"] |
| 72 | + ) |
| 73 | + + "\n" |
| 74 | + ) |
| 75 | + |
| 76 | + print(json.dumps(report, indent=2)) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + main() |
0 commit comments