# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reconcile-a-decayed-graph-edge-against-a-stale-vector-neighbor-before-retrieval-or-the-hybrid-index-silently-returns-a-phantom.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reconcile-a-decayed-graph-edge-against-a-stale-vector-neighbor-before-retrieval-or-the-hybrid-index-silently-returns-a-phantom.warrant.json
# Markdown SHA-256:   714fd2aa1185aeda106fa7e0bb691df1837f2b4e8a06c13c95a444bef4ff7348
# Code AST SHA-256:   06771d50de739e8b015bdecbf7876c80685e0d01c323472f0fff7348a63ae0bd
# Verified Classes:   DecaySkewPhantomError, DecayState
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import math
from dataclasses import dataclass


class DecaySkewPhantomError(RuntimeError):
    """Raised when a joined retrieval would emit a tuple whose liveness
    differs between the vector substrate and the graph substrate."""
    pass


@dataclass
class DecayState:
    weight: float          # current decayed weight / embedding liveness in [0,1]
    floor: float           # pruning floor for this substrate

    @property
    def live(self) -> bool:
        return self.weight >= self.floor


def reconcile_join(
    tuple_id: str,
    vec: DecayState,
    graph: DecayState,
    policy: str = "conservative",
) -> bool:
    """Decide whether a dual-written tuple may contribute to a joined result.

    Returns True if the tuple is admissible. Raises DecaySkewPhantomError if
    the two substrates disagree on liveness and the policy cannot resolve it
    without fabricating support.
    """
    if policy not in ("conservative", "authoritative-vector"):
        raise DecaySkewPhantomError(
            f"unknown reconciliation policy {policy!r} for tuple {tuple_id}"
        )

    if vec.live == graph.live:
        # Both agree (both live or both dead): no skew, admit iff live.
        return vec.live

    # Skew detected: one substrate has retired the tuple, the other has not.
    if policy == "conservative":
        # min-of-decay: suppress, because the stricter substrate said dead.
        return False

    # authoritative-vector: vector store gates recall; graph is nav hint only.
    # Graph may NOT resurrect a tuple the vector substrate retired.
    if not vec.live:
        raise DecaySkewPhantomError(
            f"tuple {tuple_id}: graph edge live (w={graph.weight:.3f}) but "
            f"vector embedding retired (w={vec.weight:.3f} < floor "
            f"{vec.floor:.3f}); refusing to emit phantom"
        )
    # Vector live, graph dead: admissible as a vector hit, but the caller
    # must re-fetch payload from the vector store, never from the edge record.
    return True


def apply_decay(weight: float, lam: float, delta_t: float) -> float:
    return weight * math.exp(-lam * delta_t)
