# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-peer-passport-as-still-trustworthy-during-in-flight-a2a-handoffs-and-when-must-it-hard-fail-the-tra.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-peer-passport-as-still-trustworthy-during-in-flight-a2a-handoffs-and-when-must-it-hard-fail-the-tra.warrant.json
# Markdown SHA-256:   35eb1da8ba45f13671f0c6d4a17574ca11ad162e3a292ad98c442f9adb4d8fe1
# Code AST SHA-256:   5f0aca3ed1f248194408dfdce08f40eb2bca7b4135a42cbee2c2fc75e6c76346
# Verified Classes:   RevocationAttributionError, SideEffectClass, PassportEpoch, InFlightTransaction
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional


class RevocationAttributionError(RuntimeError):
    """Raised when a side effect cannot be safely attributed or safely aborted
    under a revoked or unverifiable peer passport."""
    pass


class SideEffectClass(Enum):
    REVERSIBLE = "reversible"      # e.g. inventory reservation, draft write
    IRREVERSIBLE = "irreversible"  # e.g. fund transfer, external monetary call


@dataclass
class PassportEpoch:
    """Broker-signed monotonic revocation sequence observed at handshake."""
    sequence: int
    broker_signature: bytes
    observed_at: float


@dataclass
class InFlightTransaction:
    passport_fingerprint: str
    epoch: Optional[PassportEpoch]
    side_effects: list = field(default_factory=list)


def commit_side_effect(
    txn: InFlightTransaction,
    effect_class: SideEffectClass,
    revocation_known: bool,
    broker_propagation_sla: float,
    last_broker_check: float,
) -> str:
    """Decide whether an in-flight side effect may commit, must be quarantined,
    or must hard-fail. Returns a disposition string."""
    now = time.monotonic()

    # Irreversible effects require a fresh broker check within the SLA window.
    if effect_class is SideEffectClass.IRREVERSIBLE:
        if revocation_known:
            raise RevocationAttributionError(
                f"irreversible effect blocked: passport {txn.passport_fingerprint} "
                f"revoked mid-transaction"
            )
        if (now - last_broker_check) > 2 * broker_propagation_sla:
            raise RevocationAttributionError(
                f"irreversible effect blocked: broker check stale by "
                f"{now - last_broker_check:.1f}s, exceeds 2x SLA "
                f"{2 * broker_propagation_sla:.1f}s"
            )
        return "commit-with-fresh-epoch"

    # Reversible effects: distinguish attributable vs unattributable.
    if revocation_known:
        if txn.epoch is not None and txn.epoch.broker_signature:
            # Past attribution preserved; future authorization denied.
            return "quarantine-attributed-to-revoked-principal"
        raise RevocationAttributionError(
            f"reversible effect under revoked passport {txn.passport_fingerprint} "
            f"has no broker-attested epoch; state is unattributable and must be "
            f"quarantined without attribution, not rolled back"
        )

    if txn.epoch is None:
        # No epoch: allow commit but mark as unattributable for later audit.
        return "commit-unattributed"

    return "commit-attributed"
