# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reconcile-taint-label-replay-against-genai-span-ordering-before-causal-trace-reconstruction-can-be-trusted.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reconcile-taint-label-replay-against-genai-span-ordering-before-causal-trace-reconstruction-can-be-trusted.warrant.json
# Markdown SHA-256:   2deec316f7ddecc1e4ccb1e767fd5f504e952b23bbd5af3a25bd8eebfe7fa02e
# Code AST SHA-256:   b8d68312e8f1e1194a1419e54de3c5c65ab75f60c04577ab91d9dcafb4ba05aa
# Verified Classes:   CausalOrderViolation
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class CausalOrderViolation(RuntimeError):
    """Raised when taint-label replay cannot establish a valid causal order."""
    pass


def replay_taint_label(label: dict, sink_span: dict, predecessor_spans: list[dict]) -> bool:
    """
    Replay a single taint label onto a candidate sink span.

    label: {"id": str, "source_span_id": str, "provenance_step": int}
    sink_span: {"span_id": str, "logical_step": int | None, "start_time_unix_nano": int}
    predecessor_spans: ordered list of spans whose outputs fed the sink's decision,
                       each {"span_id": str, "logical_step": int | None}
    """
    if label.get("provenance_step") is None:
        raise CausalOrderViolation(
            f"label {label.get('id')} has no provenance_step; "
            "cannot order against sink without re-instrumentation"
        )
    if sink_span.get("logical_step") is None:
        raise CausalOrderViolation(
            f"sink span {sink_span.get('span_id')} has no logical_step; "
            "timestamp-only ordering is inadmissible for retry-wrapped spans"
        )

    pred_ids = {p["span_id"] for p in predecessor_spans}
    if label["source_span_id"] not in pred_ids:
        # Label did not flow through the recorded join; ancestry-only path is not influence.
        return False

    # Enforce the ordering invariant: label must precede the sink's decision point.
    if label["provenance_step"] >= sink_span["logical_step"]:
        raise CausalOrderViolation(
            f"causal inversion: label {label['id']} at step {label['provenance_step']} "
            f"cannot influence sink {sink_span['span_id']} at step {sink_span['logical_step']}"
        )

    # Guard against the retry-reset trap: a predecessor whose start_time precedes
    # the label source's completion is suspect and must be flagged, not silently accepted.
    for p in predecessor_spans:
        if p.get("logical_step") is None:
            raise CausalOrderViolation(
                f"predecessor {p['span_id']} lacks logical_step; "
                "fan-out join is under-specified, replay is underdetermined"
            )
        if p["logical_step"] >= sink_span["logical_step"]:
            raise CausalOrderViolation(
                f"predecessor {p['span_id']} at step {p['logical_step']} "
                f"does not precede sink at step {sink_span['logical_step']}"
            )

    return True
