# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-a-delegated-aaif-credential-whose-chain-is-valid-but-whose-attenuation-scope-silently-widened-at-the-hop-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-a-delegated-aaif-credential-whose-chain-is-valid-but-whose-attenuation-scope-silently-widened-at-the-hop-boundary.warrant.json
# Markdown SHA-256:   f338074d9574f88e5b475ebc1bb1a50dece00130bb49ed6c339b6b739ad28402
# Code AST SHA-256:   2865f8a759a7310fa04d8b064eaf8f6e7e61e6c295de94a6df20e7ae5bd56406
# Verified Classes:   AttenuationViolation, Credential
# Defensive Guards:   8 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional

class AttenuationViolation(RuntimeError):
    """Raised when a delegated credential's effective authority is not provably
    a subset of its delegator's, despite valid chain integrity."""

@dataclass
class Credential:
    scope: str
    scope_vocab_version: int
    resource_glob: str
    not_after: float
    budget: float
    parent_not_after: float
    parent_budget: float
    signature_valid: bool

# Pinned vocabularies: version -> scope -> canonical capability set.
VOCAB = {
    1: {"repo:read": {"read", "write"}},          # v1 conflated read/write
    2: {"repo:read": {"read"}},
}
KNOWN_VOCAB_VERSIONS = set(VOCAB)

def glob_subset(child: str, parent: str) -> Optional[bool]:
    """Return True/False if containment is decidable, None if not."""
    if parent.endswith("/**"):
        return child.startswith(parent[:-2])
    if parent.endswith("/*"):
        if child.endswith("/**"):
            return False
        return child.startswith(parent[:-1]) and "/" not in child[len(parent) - 1:]
    return child == parent

def verify_hop(child: Credential, parent_effective_caps: set,
               parent_glob: str, parent_not_after: float,
               parent_budget: float) -> set:
    if not child.signature_valid:
        raise AttenuationViolation("integrity failed before attenuation check")

    if child.scope_vocab_version not in KNOWN_VOCAB_VERSIONS:
        raise AttenuationViolation(
            f"undecidable: unknown scope vocab v{child.scope_vocab_version}")

    child_caps = VOCAB[child.scope_vocab_version].get(child.scope)
    if child_caps is None:
        raise AttenuationViolation(f"undecidable: unknown scope {child.scope!r}")

    if not child_caps.issubset(parent_effective_caps):
        raise AttenuationViolation(
            f"scope widening: {child_caps - parent_effective_caps} not held by delegator")

    contained = glob_subset(child.resource_glob, parent_glob)
    if contained is None:
        raise AttenuationViolation(
            f"undecidable: cannot decide containment of {child.resource_glob!r} "
            f"within {parent_glob!r}")
    if not contained:
        raise AttenuationViolation(
            f"resource widening: {child.resource_glob!r} escapes {parent_glob!r}")

    if child.not_after > parent_not_after:
        raise AttenuationViolation(
            f"time widening: {child.not_after} > {parent_not_after}")
    if child.budget > parent_budget:
        raise AttenuationViolation(
            f"budget widening: {child.budget} > {parent_budget}")

    return child_caps  # new accumulated effective set for the next hop
