# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-capability-scoped-credential-as-evidence-of-intent-versus-evidence-of-authority.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-capability-scoped-credential-as-evidence-of-intent-versus-evidence-of-authority.warrant.json
# Markdown SHA-256:   8b03fb89697e101c21f237653523c982e861f740a86d6c3ceb37812819945846
# Code AST SHA-256:   ecdf9848bc91d3273ec0f268f6ca920150ad771642588b1ecf7b4ecbafe226a9
# Verified Classes:   IntentCoverageError
# Defensive Guards:   8 fail-closed invariants (zero external deps)

class IntentCoverageError(RuntimeError):
    """Raised when a credential's authority is valid but its intent caveat
    cannot be discharged by an attester trusted by the root issuer."""
    pass


TRUSTED_ATTESTERS = {
    # caveat_name -> set of signer identities the ROOT issuer will accept
    "purpose=research": {"org-a-irb", "org-a-data-steward"},
}


def admit_request(credential, request, discharge_assertions):
    # Authority check: mechanical, total.
    if not credential.verify_chain():
        raise IntentCoverageError("authority chain invalid; refuse")
    if credential.is_expired() or credential.is_revoked():
        raise IntentCoverageError("authority lapsed; refuse")
    if not credential.scope_covers(request.action):
        raise IntentCoverageError(
            f"request action {request.action!r} exceeds scope; refuse"
        )

    # Intent check: evidence must originate OUTSIDE the presenter.
    for caveat in credential.intent_caveats():
        trusted = TRUSTED_ATTESTERS.get(caveat.name)
        if trusted is None:
            # Caveat with no registered attester: cannot be soundly discharged.
            raise IntentCoverageError(
                f"intent caveat {caveat.name!r} has no trusted attester; refuse"
            )
        assertion = discharge_assertions.get(caveat.name)
        if assertion is None:
            raise IntentCoverageError(
                f"intent caveat {caveat.name!r} undischarged; refuse"
            )
        if assertion.signer == credential.presenter:
            # Presenter self-attesting intent is not evidence.
            raise IntentCoverageError(
                f"intent caveat {caveat.name!r} discharged by presenter; "
                "self-attestation is not admissible"
            )
        if assertion.signer not in trusted:
            raise IntentCoverageError(
                f"intent caveat {caveat.name!r} discharged by untrusted "
                f"signer {assertion.signer!r}; refuse"
            )
        if not assertion.verify():
            raise IntentCoverageError(
                f"intent caveat {caveat.name!r} discharge assertion invalid"
            )

    return "admit"
