# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-mints-a-delegated-aaif-credential-whose-scope-is-broader-than-the-warrant-it-received.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-mints-a-delegated-aaif-credential-whose-scope-is-broader-than-the-warrant-it-received.warrant.json
# Markdown SHA-256:   20be262bbf76f7482f9988bf61682e41d924fdf9bc62bcf15758ca395b82a528
# Code AST SHA-256:   13e89f1afcaecf246fc5ccab0c60e5c8c8413a44163fd4a4c2e438afc3a637cf
# Verified Classes:   ScopeGrammarMismatch
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class ScopeGrammarMismatch(RuntimeError):
    """Raised when a delegated credential's scope grammar cannot be soundly
    evaluated against the verifying grammar. Fallback is forbidden because
    unknown-grammar fallback is a scope-widening operation."""
    pass


KNOWN_GRAMMARS = {"aaif-scope/v2"}


def verify_delegated_credential(credential: dict, parent_scope: dict, pinned_grammar: str) -> dict:
    declared = credential.get("scope_grammar")
    if declared is None:
        raise ScopeGrammarMismatch("credential omits scope_grammar; absence is not a default")
    if declared not in KNOWN_GRAMMARS:
        # Do NOT fall back to a nearest grammar. Reject.
        raise ScopeGrammarMismatch(
            f"declared grammar {declared!r} not implemented; refusing lenient fallback"
        )
    if declared != pinned_grammar:
        raise ScopeGrammarMismatch(
            f"grammar drift across delegation edge: root pinned {pinned_grammar!r}, "
            f"child declared {declared!r}; subset relation is unsound"
        )
    child_scope = credential.get("scope", {})
    if not is_subset(child_scope, parent_scope, grammar=declared):
        raise ScopeGrammarMismatch(
            f"child scope {child_scope!r} is not a subset of parent {parent_scope!r} "
            f"under {declared!r}"
        )
    return child_scope


def is_subset(child: dict, parent: dict, grammar: str) -> bool:
    # Placeholder for the pinned-grammar predicate evaluator.
    # Must evaluate both scopes under the SAME grammar, never the child's declared one alone.
    raise NotImplementedError
