# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-revokes-a-memory-it-already-read-into-its-working-context.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-revokes-a-memory-it-already-read-into-its-working-context.warrant.json
# Markdown SHA-256:   a1541ffd2019f00cfd259f35cc1f9dcd38ebffd250264bba05968dc7486e0d71
# Code AST SHA-256:   20386f9fe9f9506c2a22d0b24e513130a30625b5144c69baa0e2153710deceb5
# Verified Classes:   RevocationState, StaleMemoryActedUponError, HeldRecord
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Optional


class RevocationState(Enum):
    VALID = "valid"
    REVOKED = "revoked"
    DECAYED = "decayed"
    UNKNOWN = "unknown"


class StaleMemoryActedUponError(RuntimeError):
    """Raised when an instance attempts to act on a held memory record
    whose provenance chain is no longer valid at composition time."""
    pass


@dataclass(frozen=True)
class HeldRecord:
    provenance_id: str
    revocation_token: Optional[str]
    decay_weight: float
    content: str


DECAY_THRESHOLD = 0.35


def validate_at_composition(record: HeldRecord, store) -> RevocationState:
    """Validate a held record's provenance chain at the moment an outbound
    argument is composed. Must be called per decision boundary, not per read."""
    if record.revocation_token is None:
        # No token means the store cannot vouch for this record's current
        # validity. Treat as unknown, never as valid.
        return RevocationState.UNKNOWN

    if record.decay_weight < DECAY_THRESHOLD:
        return RevocationState.DECAYED

    # Store must walk the full provenance chain; leaf-only checks miss
    # transitive revocation through derived records.
    chain_state = store.validate_provenance_chain(record.revocation_token)
    if chain_state == "revoked":
        return RevocationState.REVOKED
    if chain_state == "valid":
        return RevocationState.VALID
    return RevocationState.UNKNOWN


def compose_tool_call(record: HeldRecord, store, tool: str, args: dict) -> dict:
    state = validate_at_composition(record, store)
    if state in (RevocationState.REVOKED, RevocationState.DECAYED):
        raise StaleMemoryActedUponError(
            f"Refusing to compose {tool!r}: record {record.provenance_id!r} "
            f"is {state.value} at composition time. Held content is a "
            f"hypothesis, not a fact; re-read or drop the derived argument."
        )
    if state is RevocationState.UNKNOWN:
        raise StaleMemoryActedUponError(
            f"Refusing to compose {tool!r}: record {record.provenance_id!r} "
            f"has no verifiable revocation token. Cannot prove current validity."
        )
    return {
        "tool": tool,
        "args": args,
        "provenance": {
            "provenance_id": record.provenance_id,
            "revocation_token": record.revocation_token,
            "decay_weight_at_composition": record.decay_weight,
        },
    }
