# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-taint-aware-sandbox-denial-as-evidence-of-an-exfiltration-attempt-rather-than-a-benign-policy-miss.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-taint-aware-sandbox-denial-as-evidence-of-an-exfiltration-attempt-rather-than-a-benign-policy-miss.warrant.json
# Markdown SHA-256:   076deec6f567c9b421080fd1e9bf7ab3cdcc0a66ebf686d67c1a1c53db4f146f
# Code AST SHA-256:   7decbc022973211aee7523852a7dcad46008d1c8d8b2038508a23243b51b773d
# Verified Classes:   TaintBoundaryViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class TaintBoundaryViolation(RuntimeError):
    """Raised when a denial cannot be classified as benign capability miss."""


def classify_denial(denial, manifest, session_sources, turn_calls):
    tool = denial.get("tool")
    violated_label = denial.get("label")
    source = denial.get("source")

    if tool not in manifest:
        return "capability_miss"

    if violated_label is None:
        raise TaintBoundaryViolation(
            f"denial for permitted tool {tool!r} carries no label; "
            "cannot distinguish policy miss from exfiltration"
        )

    if source not in session_sources:
        return "false_positive_label"

    transform_paths = [
        c for c in turn_calls
        if c["tool"] != tool and c.get("consumes") == source
    ]
    declared = [c for c in transform_paths if c.get("declares_declassifier")]

    if transform_paths and not declared:
        raise TaintBoundaryViolation(
            f"exfiltration path: source {source!r} reaches sink {tool!r} "
            f"via {[c['tool'] for c in transform_paths]} with no declassifier"
        )

    if len(declared) < len(transform_paths):
        raise TaintBoundaryViolation(
            f"partial declassification on path to {tool!r}; "
            "unlabeled transform present"
        )

    return "incident_confirmed"
