# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-federated-capability-contract-that-is-syntactically-valid-but-semantically-unenforceable.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-federated-capability-contract-that-is-syntactically-valid-but-semantically-unenforceable.warrant.json
# Markdown SHA-256:   010edc1a4e12847084562ede3a5b6f96248b8b63d1608f12653191aa009ae0b2
# Code AST SHA-256:   813643bccc409cc131c00c8c35b0e586969a3b40f5ad6f20014dec774fb85099
# Verified Classes:   UnenforceableClauseError, Clause
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Callable, Iterable


class UnenforceableClauseError(RuntimeError):
    """Raised when a syntactically valid contract clause cannot be guarded at runtime."""


@dataclass(frozen=True)
class Clause:
    name: str
    observable: bool          # condition 1: names a runtime-observable quantity
    guard_at_effect_site: bool  # condition 2: guard installed at lowest effect site
    total_over_effects: bool  # condition 3: no ungoverned effect path
    shared_semantics: bool    # condition 4: units/semantics pinned


def _violations(clause: Clause) -> list[str]:
    reasons = []
    if not clause.observable:
        reasons.append("not runtime-observable (emergent/inferred property)")
    if not clause.guard_at_effect_site:
        reasons.append("guard not at lowest effect site")
    if not clause.total_over_effects:
        reasons.append("effect vocabulary not fully covered (spawn/batch/replay path)")
    if not clause.shared_semantics:
        reasons.append("units/semantics not pinned by shared operational definition")
    return reasons


def accept_contract(
    clauses: Iterable[Clause],
    *,
    peer_signature_valid: bool,
) -> list[Clause]:
    """Accept a federated capability contract only if every clause is enforceable.

    Signature validity is necessary but NOT sufficient; it is checked and then
    deliberately set aside so it cannot mask semantic unenforceability.
    """
    if not peer_signature_valid:
        raise UnenforceableClauseError("contract signature failed verification")

    accepted: list[Clause] = []
    for clause in clauses:
        reasons = _violations(clause)
        if reasons:
            raise UnenforceableClauseError(
                f"rejecting clause {clause.name!r}: " + "; ".join(reasons)
            )
        accepted.append(clause)
    return accepted


# Example: the observed failure — a syntactically valid but unenforceable clause.
try:
    accept_contract(
        [
            Clause(
                name="max_reasoning_depth",
                observable=False,          # depth is emergent, not a counter
                guard_at_effect_site=False,
                total_over_effects=False,
                shared_semantics=False,
            )
        ],
        peer_signature_valid=True,
    )
except UnenforceableClauseError as err:
    # Correct behavior: reject and emit a renegotiation report.
    print(f"negotiation rejected -> {err}")
