"""Ratchet comparison: new violations fail, violations that disappeared must be removed from the baseline (they cannot return)."""


def diff(current, baseline):
    """(new, stale).

    Set contracts (iterables of strings): new = in current but not in baseline; stale = in baseline but gone from current.
    Count contracts ({file: count}): new = files whose count rose above the baseline (or that are not in it);
    stale = files whose count fell below the baseline (the baseline must be lowered so the old level cannot come back).
    """
    if isinstance(current, dict) or isinstance(baseline, dict):
        current, baseline = dict(current or {}), dict(baseline or {})
        new = {f: (baseline.get(f, 0), n) for f, n in current.items() if n > baseline.get(f, 0)}
        stale = {f: (n, current.get(f, 0)) for f, n in baseline.items() if current.get(f, 0) < n}
        return new, stale
    current, baseline = set(current), set(baseline)
    return sorted(current - baseline), sorted(baseline - current)
