# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-federated-capability-attestation-as-non-transferable-across-an-a2a-handoff-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-federated-capability-attestation-as-non-transferable-across-an-a2a-handoff-boundary.warrant.json
# Markdown SHA-256:   619d157a3ba1ef9d885154c18984244ac61e85776b264d457e8dff7a1cd313f0
# Code AST SHA-256:   d28ba014c32c1310137d32214fbb0e0f7de1bf5cbfc032f07697680f249bb807
# Verified Classes:   AttestationLaunderingError
# Defensive Guards:   7 fail-closed invariants (zero external deps)

class AttestationLaunderingError(RuntimeError):
    """Raised when a forwarded capability attestation would extend authority
    beyond what the root principal explicitly granted at this hop."""
    pass


def verify_forwarded_attestation(
    child: dict,
    parent: dict,
    root_policy: dict,
    is_revoked,
    now: float,
) -> str:
    """Verify a delegated attestation chain at a receiving domain.

    Returns the root principal under which the action must be recorded.
    Raises AttestationLaunderingError on any conjunction failure.
    """
    # 1. Explicit re-delegation grant required on the parent's policy.
    if not root_policy.get("subdelegate", False):
        raise AttestationLaunderingError(
            f"policy {root_policy.get('id')} does not grant re-delegation; "
            f"delegate {child.get('delegate')} cannot forward to {child.get('subdelegate')}"
        )

    # 2. Principal preserved end-to-end.
    if child.get("principal") != parent.get("principal"):
        raise AttestationLaunderingError(
            f"principal drift: parent={parent.get('principal')} "
            f"child={child.get('principal')} — authority laundering"
        )

    # 3. Full chain presented and linked.
    if child.get("parent_attestation_id") != parent.get("id"):
        raise AttestationLaunderingError(
            f"broken chain: child references {child.get('parent_attestation_id')}, "
            f"parent is {parent.get('id')}"
        )

    # 4. Scope monotonically narrowing.
    child_scope = set(child.get("scope", []))
    parent_scope = set(parent.get("scope", []))
    permitted = set(root_policy.get("permitted_actions", []))
    if not child_scope <= (parent_scope & permitted):
        raise AttestationLaunderingError(
            f"scope widening: child={child_scope} not subset of "
            f"parent∩policy={parent_scope & permitted}"
        )

    # 5. Depth bound and revocation freshness.
    if child.get("delegation_depth", 0) > root_policy.get("max_delegation_depth", 1):
        raise AttestationLaunderingError(
            f"delegation depth {child.get('delegation_depth')} exceeds "
            f"max {root_policy.get('max_delegation_depth')}"
        )
    if is_revoked(parent.get("id")):
        raise AttestationLaunderingError(
            f"ancestor attestation {parent.get('id')} is revoked"
        )
    if child.get("expires_at", 0) < now:
        raise AttestationLaunderingError(
            f"attestation expired at {child.get('expires_at')}, now={now}"
        )

    # Record under the ROOT principal, not the immediate sender.
    return parent["principal"]
