# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-delegated-credential-whose-scope-was-narrowed-by-an-upstream-agent-rather-than-by-the-issuing-authority.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-delegated-credential-whose-scope-was-narrowed-by-an-upstream-agent-rather-than-by-the-issuing-authority.warrant.json
# Markdown SHA-256:   2ed2ac2e72fde3d74bdb1f4e1be28107fe62f37438ec1b66ad8096dfb4c4551b
# Code AST SHA-256:   8ab5fef3fe6c8dae7e4d7c0064bc823d3f165e45b6d49584b294169dd22c2761
# Verified Classes:   UnattestedNarrowingError, Credential
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet, Optional


class UnattestedNarrowingError(RuntimeError):
    """Raised when a delegated credential's scope is a strict subset of an
    ancestor's but carries no signed narrowing attestation, making it
    impossible to distinguish authority-withheld claims from
    intermediate-dropped claims."""
    pass


@dataclass(frozen=True)
class Credential:
    cred_id: str
    parent_id: Optional[str]
    claims: FrozenSet[str]
    narrowing_provenance: Optional[str]  # "deterministic" | "probabilistic" | None
    narrowing_attestation: Optional[str] = None  # signed scope_diff ref, if any


def classify_missing_claim(
    child: Credential,
    ancestor: Credential,
    required_claim: str,
) -> str:
    """Return one of: 'present', 'authority_withheld', 'intermediate_dropped',
    or raise UnattestedNarrowingError if the distinction is unrecoverable."""
    if required_claim in child.claims:
        return "present"

    if required_claim not in ancestor.claims:
        # Ancestor never had it either: genuine authority boundary.
        return "authority_withheld"

    # Claim is in ancestor but absent in child: a narrowing occurred.
    if child.narrowing_attestation is not None:
        # Signed scope_diff lets us confirm the drop was policy-driven.
        return "authority_withheld"

    if child.narrowing_provenance == "deterministic":
        # Re-running the pinned narrowing function would confirm; here we
        # treat a deterministic marker as authoritative for the drop.
        return "authority_withheld"

    # Probabilistic or unattested narrowing: unrecoverable from credential.
    raise UnattestedNarrowingError(
        f"credential {child.cred_id} drops claim {required_claim!r} present in "
        f"ancestor {ancestor.cred_id} without attestation "
        f"(provenance={child.narrowing_provenance!r}); cannot distinguish "
        f"authority-withheld from intermediate-dropped. Escalate to issuer "
        f"{child.parent_id} with scope_gap request."
    )


def execute_step(child: Credential, ancestor: Credential, required_claim: str) -> None:
    verdict = classify_missing_claim(child, ancestor, required_claim)
    if verdict == "authority_withheld":
        raise PermissionError(
            f"authority denied {required_claim!r}; subtask impossible under "
            f"{child.cred_id}"
        )
    if verdict == "intermediate_dropped":
        # Should be unreachable given the raise above, but kept explicit.
        raise UnattestedNarrowingError(
            f"intermediate dropped {required_claim!r}; re-request from issuer"
        )
    # verdict == "present": proceed with execution.
