# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-its-authorization-warrant-at-the-aaif-credential-exchange-boundary-when-the-issuing-agent-s-context-has-been-compacted.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-its-authorization-warrant-at-the-aaif-credential-exchange-boundary-when-the-issuing-agent-s-context-has-been-compacted.warrant.json
# Markdown SHA-256:   5ac87d8304a6de9d871c9e4a309c6ce69258c20a3c659f83c48f5937d8cc2f9e
# Code AST SHA-256:   5e132ee04178739ec605c567d85b4f1373f6cab14ce0b696096dbd37ebada4d5
# Verified Classes:   WarrantConstraintLoss
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class WarrantConstraintLoss(RuntimeError):
    """Raised when an issuer cannot resolve a constraint reference it is
    expected to attest, and reconstruction would fail open."""
    pass


def introspect_chain(issuer_state, presented_warrant):
    """
    Fail-closed introspection for AAIF delegation chains.

    issuer_state.durable_constraints maps claim_id -> inline constraint set,
    persisted outside the LLM context so compaction cannot evict it.
    """
    attestation = []
    for hop in presented_warrant.delegation_chain:
        claim_id = hop.constraint_ref
        if claim_id is None:
            # Constraint was inlined at mint time; safe under any compaction.
            attestation.append(hop.inline_constraints)
            continue

        resolved = issuer_state.durable_constraints.get(claim_id)
        if resolved is None:
            # Do NOT reconstruct from memory. Reconstruction drifts toward
            # permissiveness and produces a signed-but-wrong attestation.
            raise WarrantConstraintLoss(
                f"constraint_ref {claim_id!r} unresolvable in durable issuer "
                f"state; refusing to attest chain element for subject "
                f"{hop.subject!r}. Warrant must be treated as revoked."
            )
        attestation.append(resolved)

    return attestation
