File size: 4,240 Bytes
a0e9e53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Deterministic taxonomy for FrontierLab blocking and failure signals.

The taxonomy separates expected governance gates from runtime failures so that a
human review requirement is never counted as a software/model error.
"""
from __future__ import annotations

from collections import Counter

from .models import Run

SCHEMA = "frontierlab.error-taxonomy.v1"

GOVERNANCE = "GOVERNANCE_GATE"
EVIDENCE = "EVIDENCE_INSUFFICIENT"
CITATION = "CITATION_OR_SOURCE_FAILURE"
RESOURCE = "RESOURCE_NOT_CONNECTED"
MODEL_RUNTIME = "MODEL_RUNTIME_FAILURE"
MODEL_OUTPUT = "MODEL_OUTPUT_FAILURE"
CLAIM_REJECTED = "CLAIM_REJECTED"
SCIENTIFIC_TENSION = "SCIENTIFIC_TENSION"


def _classify_log(item: dict) -> str | None:
    if not isinstance(item, dict):
        return None
    role = str(item.get("role", ""))
    status = str(item.get("status", ""))
    note = str(item.get("note", "")).lower()

    if role == "Human Claim Review" and status == "BLOCKED":
        return GOVERNANCE
    if role == "Audit Gate" and status == "BLOCKED":
        return EVIDENCE
    if role == "Citation Gate" and status == "BLOCKED":
        return CITATION
    if role == "Meta AI" and status == "BLOCKED":
        return RESOURCE
    if status == "FAILED_NO_RETRY":
        return MODEL_RUNTIME
    if status == "BLOCKED" and ("model" in note or "backend" in note):
        return RESOURCE
    return None


def classify_run(run: Run) -> dict:
    events: list[dict] = []

    for index, item in enumerate(run.log):
        category = _classify_log(item)
        if category:
            events.append({
                "source": "run.log",
                "index": index,
                "category": category,
                "role": item.get("role"),
                "status": item.get("status"),
                "note": item.get("note", ""),
            })

    for index, rejected in enumerate(run.audit.rejected_claims):
        if not isinstance(rejected, dict):
            continue
        reason = str(rejected.get("reason", "")).lower()
        category = CITATION if ("citation" in reason or "source" in reason or "passage" in reason) else CLAIM_REJECTED
        events.append({
            "source": "audit.rejected_claims",
            "index": index,
            "category": category,
            "reason": rejected.get("reason", ""),
        })

    for index, conflict in enumerate(run.audit.conflicts):
        events.append({
            "source": "audit.conflicts",
            "index": index,
            "category": SCIENTIFIC_TENSION,
            "claim_key": conflict.get("claim_key") if isinstance(conflict, dict) else None,
        })

    if run.backend == "nvidia-nim" and isinstance(run.inference, dict):
        receipts = run.inference.get("receipts", [])
        if isinstance(receipts, list):
            for index, receipt in enumerate(receipts):
                if not isinstance(receipt, dict):
                    continue
                if receipt.get("status") == "FAILED_NO_RETRY":
                    # Avoid double counting only if the same failure was already explicitly
                    # represented in run.log; current pipeline usually does not log it.
                    events.append({
                        "source": "inference.receipts",
                        "index": index,
                        "category": MODEL_RUNTIME,
                        "role": receipt.get("role"),
                        "status": "FAILED_NO_RETRY",
                    })

    counts = Counter(event["category"] for event in events)
    runtime_failures = counts[MODEL_RUNTIME] + counts[MODEL_OUTPUT]
    non_runtime_blocks = counts[GOVERNANCE] + counts[EVIDENCE] + counts[CITATION] + counts[RESOURCE]

    return {
        "schema": SCHEMA,
        "events": events,
        "counts": dict(sorted(counts.items())),
        "runtime_failures": runtime_failures,
        "non_runtime_blocks": non_runtime_blocks,
        "governance_or_evidence_blocks": non_runtime_blocks,
        "scientific_tensions": counts[SCIENTIFIC_TENSION],
        "note": (
            "Governance/evidence blocks are expected safety workflow outcomes and are "
            "reported separately from runtime/model-output failures."
        ),
    }