# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-delegated-credential-chain-has-silently-widened-scope-through-per-tool-attestation-re-binding.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-delegated-credential-chain-has-silently-widened-scope-through-per-tool-attestation-re-binding.warrant.json
# Markdown SHA-256:   8a91a4f2a044ea78658a9fa14fb5a3dc07c3643c26437edbd84866b2ffada841
# Code AST SHA-256:   d9dd3ced6d3c76e0a349dd7f9c94fe4ed8f656303b9bf75c7ee99238304e7a53
# Verified Classes:   ScopeContainmentViolation, DelegatedCredential, ToolAttestation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class ScopeContainmentViolation(RuntimeError):
    """Raised when an attestation's scope is not contained in the subject's delegated scope set."""


@dataclass(frozen=True)
class DelegatedCredential:
    subject: str
    delegated_scope: frozenset[str]
    parent_scope: frozenset[str] = field(default_factory=frozenset)

    def __post_init__(self) -> None:
        # Monotonic narrowing at mint time: a credential may not exceed its parent.
        if self.parent_scope and not self.delegated_scope <= self.parent_scope:
            raise ScopeContainmentViolation(
                f"delegation to {self.subject} widens scope: "
                f"{sorted(self.delegated_scope - self.parent_scope)} not in parent set"
            )


@dataclass(frozen=True)
class ToolAttestation:
    subject: str
    tool: str
    scope: frozenset[str]
    signature_valid: bool


def admit_invocation(
    credential: DelegatedCredential,
    attestation: ToolAttestation,
) -> None:
    """Gate a tool invocation on scope containment, not merely signature validity.

    The invariant enforced here is:
        attestation.scope subset-of credential.delegated_scope
    Signature validity is necessary but not sufficient; it is checked only to
    avoid admitting forged attestations, never as a substitute for containment.
    """
    if not attestation.signature_valid:
        raise ScopeContainmentViolation(
            f"attestation for {attestation.tool} by {attestation.subject} failed signature check"
        )
    if attestation.subject != credential.subject:
        raise ScopeContainmentViolation(
            f"attestation subject {attestation.subject} does not match credential subject "
            f"{credential.subject}"
        )
    excess = attestation.scope - credential.delegated_scope
    if excess:
        # This is the silent-widening case: a valid attestation carrying scope
        # the subject was never delegated. Deny before the tool executes.
        raise ScopeContainmentViolation(
            f"attestation for tool {attestation.tool} widens scope for "
            f"{attestation.subject}: {sorted(excess)} not in delegated set "
            f"{sorted(credential.delegated_scope)}"
        )
