# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-abandon-speculative-replay-as-an-arbitration-oracle-in-a-byzantine-tool-chain.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-abandon-speculative-replay-as-an-arbitration-oracle-in-a-byzantine-tool-chain.warrant.json
# Markdown SHA-256:   198ad75a2e941a2d3ac849b0bf926505879648809c7413f381404b28c4f8ed14
# Code AST SHA-256:   a3ed563ec3f2ae76adec667ac5ec6c63e2436afbb74f8e657ad9b6197f9be48b
# Verified Classes:   NonAdjudicativeTraceError, TraceCapture
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass, field
from typing import Optional


class NonAdjudicativeTraceError(RuntimeError):
    """Raised when a trace cannot be used as a Byzantine arbitration oracle."""


@dataclass
class TraceCapture:
    external_reads_captured: bool = False
    clock_and_entropy_captured: bool = False
    replay_env_isolated: bool = False
    provenance_attested: bool = False
    captured_artifacts: dict = field(default_factory=dict)


def classify_trace_for_arbitration(trace: TraceCapture, trace_id: str) -> str:
    missing = []
    if not trace.external_reads_captured:
        missing.append("external_reads_captured")
    if not trace.clock_and_entropy_captured:
        missing.append("clock_and_entropy_captured")
    if not trace.replay_env_isolated:
        missing.append("replay_env_isolated")
    if not trace.provenance_attested:
        missing.append("provenance_attested")

    if missing:
        raise NonAdjudicativeTraceError(
            f"Trace {trace_id} is not adjudicative; missing capture conditions: "
            f"{', '.join(missing)}. Route to quorum or signed-receipt oracle, "
            f"not to replay-as-adjudication."
        )
    return "ADJUDICATIVE"


def arbitrate(agent_traces: dict[str, TraceCapture]) -> str:
    # Replay-as-adjudication is only sound if EVERY candidate trace is adjudicative.
    # A single non-adjudicative trace poisons the comparison, because the arbiter
    # would be comparing one honest trace against a fresh execution.
    classifications = {}
    for agent_id, trace in agent_traces.items():
        classifications[agent_id] = classify_trace_for_arbitration(trace, agent_id)
    return "replay-oracle-permitted"
