# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-cross-issuer-trust-anchoring-from-silently-collapsing-under-transitive-credential-delegation.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-cross-issuer-trust-anchoring-from-silently-collapsing-under-transitive-credential-delegation.warrant.json
# Markdown SHA-256:   c6e7fcad4654b81113789d34fe2403334b185266513676ce8762a4de74bf9eab
# Code AST SHA-256:   7dfe158387f4eaca9ff83769998eb121021cd38c3e1ed3763ea58b892ec6cd40
# Verified Classes:   TrustAnchorCollapseError
# Defensive Guards:   7 fail-closed invariants (zero external deps)

class TrustAnchorCollapseError(RuntimeError):
    """Raised when a credential path reaches a trust anchor but fails
    per-hop authorization (claim scope, depth, subject scope, or revocation)."""
    pass


MAX_DEPTH_DEFAULT = 1


def resolve_trust(path, asserted_claim, subject, framework_policy):
    """
    path: ordered list of hops from leaf issuer to anchor.
          Each hop: {"issuer": str, "accredits": str, "claim_scope": set,
                     "subject_scope": set, "revoked": bool, "expired": bool}
    framework_policy: {"max_depth": int}
    Returns True only if every hop authorizes the asserted claim for the subject.
    """
    if not path:
        raise TrustAnchorCollapseError("empty trust path: cannot anchor")

    max_depth = framework_policy.get("max_depth", MAX_DEPTH_DEFAULT)
    if len(path) - 1 > max_depth:
        raise TrustAnchorCollapseError(
            f"delegation depth {len(path) - 1} exceeds permitted {max_depth}"
        )

    seen = set()
    for hop in path:
        issuer = hop["issuer"]
        if issuer in seen:
            raise TrustAnchorCollapseError(f"delegation loop at issuer {issuer}")
        seen.add(issuer)

        if hop.get("revoked"):
            raise TrustAnchorCollapseError(f"accreditation revoked at {issuer}")
        if hop.get("expired"):
            raise TrustAnchorCollapseError(f"accreditation expired at {issuer}")
        if asserted_claim not in hop["claim_scope"]:
            raise TrustAnchorCollapseError(
                f"issuer {issuer} not scoped for claim {asserted_claim}"
            )
        if subject not in hop["subject_scope"] and "*" not in hop["subject_scope"]:
            raise TrustAnchorCollapseError(
                f"issuer {issuer} not scoped for subject {subject}"
            )

    return True
