#!/usr/bin/env python3
"""Reduce a creds-matrix run to its FINDING CLASSES, one per line, sorted+unique.

Used by the harness's expected-findings baseline. Classes, not instances: the baseline
must survive a row being added or renamed, but must CHANGE when a class of defect is
fixed or introduced -- so that remediating a defect requires updating the baseline in the
same commit, rather than turning the gauntlet red for the person who fixed it.
"""
import sys, re
KEYS = ("EXPECTED-BUT-ABSENT", "UNEXPECTED-EXTRA", "FIELD DRIFT", "ASYMMETRY",
        "IDENTITY CONFLATION", "UNDECLARED", "UNCHECKABLE", "WORLD-READABLE",
        "MODE", "UNREADABLE", "ESCALATION FAILED", "FLOOR")
out = set()
for line in sys.stdin:
    if "[FAIL]" not in line:
        continue
    body = line.split("[FAIL]", 1)[1].strip()
    code = re.match(r"([SE]\d)\b", body)
    key = next((k for k in KEYS if k in body), None)
    if code and key:
        out.add("%s %s" % (code.group(1), key))
    elif code:
        out.add(code.group(1) + " other")
    else:
        out.add("schema/row defect")
for c in sorted(out):
    print(c)
