# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-byzantine-committed-task-as-revocable-rather-than-durable.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-byzantine-committed-task-as-revocable-rather-than-durable.warrant.json
# Markdown SHA-256:   8f26fa044e01f62211f39836e3dddad12c6a5052a56ed2f46103b4562bcf4626
# Code AST SHA-256:   5355fcf2484bdfe9ed9322a06e7672f98740898fa9f7eb15ac785acdad3901ec
# Verified Classes:   NonPortableCommitError, CommitCertificate, AgentCommitState
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Set, Optional
import time


class NonPortableCommitError(RuntimeError):
    """Raised when an agent attempts to flush irreversible side effects
    against a BFT commit certificate that a fresh quorum would not honor."""
    pass


@dataclass
class CommitCertificate:
    task_id: str
    view: int
    signers: Set[str]
    equivocators: Set[str] = field(default_factory=set)

    def portable(self, current_quorum: Set[str], f: int) -> bool:
        # Certificate is portable iff it was signed by a quorum that excludes
        # known equivocators and whose signers intersect the current quorum
        # by at least f+1 (so a fresh quorum cannot be formed without them).
        honest_signers = self.signers - self.equivocators
        if len(honest_signers) < 2 * f + 1:
            return False
        overlap = honest_signers & current_quorum
        return len(overlap) >= f + 1


@dataclass
class AgentCommitState:
    task_id: str
    certificate: CommitCertificate
    flushed: bool = False
    journal: list = field(default_factory=list)

    def flush_side_effect(self, effect: str, current_quorum: Set[str], f: int) -> None:
        if not self.certificate.portable(current_quorum, f):
            # Downgrade to revocable: journal the effect, do not flush.
            self.journal.append((time.time(), effect))
            raise NonPortableCommitError(
                f"task {self.task_id} commit in view {self.certificate.view} "
                f"is non-portable to quorum {current_quorum}; "
                f"honest signers={self.certificate.signers - self.certificate.equivocators}, "
                f"required overlap>= {f + 1}. Effect journaled, not flushed."
            )
        # Portable: safe to flush irreversible side effect.
        self.flushed = True
        self.journal.append((time.time(), f"FLUSHED:{effect}"))


def execute_task_step(
    state: AgentCommitState,
    effect: str,
    current_quorum: Set[str],
    f: int,
) -> None:
    """Execute a single irreversible step of a committed task.
    Raises NonPortableCommitError if the commit cannot be proven durable
    to the current quorum, forcing the caller into revocable mode."""
    state.flush_side_effect(effect, current_quorum, f)
