# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-commit-a-speculatively-executed-cross-organizational-effect.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-commit-a-speculatively-executed-cross-organizational-effect.warrant.json
# Markdown SHA-256:   3a15d04e539eb9e63ecebea901351d12c625a2cab51b9d8825ea06f4489cbaa8
# Code AST SHA-256:   0667f30290123dd10bc8baaf77b311ad3d50630908528b33a3831a41139a12bc
# Verified Classes:   SpeculativeCommitRefused, LeaseView, CommitGate
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class SpeculativeCommitRefused(RuntimeError):
    """Raised when a cross-org speculative effect cannot be safely committed."""
    pass


@dataclass
class LeaseView:
    lease_id: str
    issued_at: float
    ttl: float
    revoked_at: float | None
    freshness_proof_at: float | None  # signed assertion timestamp from origin org


@dataclass
class CommitGate:
    commit_at: float
    clock_skew_bound: float
    revocation_propagation_delay: float  # d_prop; None-like if unbounded


def classify_commit_branch(lease: LeaseView, gate: CommitGate, now: float) -> str:
    """
    Return one of: 'commit', 'defer', 'abort'.
    Refuses (raises) when the classification cannot be made safely.
    """
    if gate.revocation_propagation_delay is None:
        # No bounded synchronous revocation channel: cross-org commit unsafe.
        return "defer"

    if lease.freshness_proof_at is None:
        # Cannot prove the lease was live at commit authorization time.
        raise SpeculativeCommitRefused(
            f"lease {lease.lease_id} has no freshness proof; "
            "cannot classify speculative commit"
        )

    proof_age = gate.commit_at - lease.freshness_proof_at
    if proof_age > gate.clock_skew_bound + gate.revocation_propagation_delay:
        return "abort"

    if lease.revoked_at is not None and lease.revoked_at <= gate.commit_at - gate.revocation_propagation_delay:
        return "abort"

    safe_epsilon = gate.revocation_propagation_delay + gate.clock_skew_bound
    if gate.commit_at - now < safe_epsilon:
        # Commit gate fires before revocation could propagate: defer.
        return "defer"

    return "commit"


def redeem_speculative_effect(lease: LeaseView, gate: CommitGate, now: float) -> str:
    branch = classify_commit_branch(lease, gate, now)
    if branch == "commit":
        return f"committed:{lease.lease_id}"
    if branch == "defer":
        return f"deferred:{lease.lease_id}"
    return f"aborted:{lease.lease_id}"


if __name__ == "__main__":
    # Example: unbounded propagation -> defer, never commit.
    lease = LeaseView("L-1", issued_at=0.0, ttl=30.0, revoked_at=None,
                      freshness_proof_at=time.time())
    gate = CommitGate(commit_at=time.time() + 1.0, clock_skew_bound=0.5,
                      revocation_propagation_delay=None)
    print(redeem_speculative_effect(lease, gate, time.time()))

    # Example: missing freshness proof -> hard refusal.
    lease2 = LeaseView("L-2", issued_at=0.0, ttl=30.0, revoked_at=None,
                       freshness_proof_at=None)
    try:
        redeem_speculative_effect(lease2, gate, time.time())
    except SpeculativeCommitRefused as e:
        print("refused:", e)
