# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-a-mid-session-tool-schema-mutation-as-capability-poisoning-rather-than-treat-it-as-a-benign-hot-reload.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-a-mid-session-tool-schema-mutation-as-capability-poisoning-rather-than-treat-it-as-a-benign-hot-reload.warrant.json
# Markdown SHA-256:   74c641d54c3c038afb3c1be976ca8304337b2ba3073a02262dd15e24c1dff30a
# Code AST SHA-256:   99cff12c3aa8f65153bba8e10a0ac2381ab8cbbad357665a282d1eb818784b1c
# Verified Classes:   CapabilityPoisoningError, ToolDescriptor
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
import json
from dataclasses import dataclass


class CapabilityPoisoningError(RuntimeError):
    """Raised when a tool descriptor mutates under an established plan."""
    pass


@dataclass(frozen=True)
class ToolDescriptor:
    name: str
    description: str
    input_schema: dict
    output_schema: dict
    side_effect_class: str  # "read_only" | "mutating" | "destructive"

    def hash(self) -> str:
        payload = json.dumps(
            {
                "name": self.name,
                "description": self.description,
                "input_schema": self.input_schema,
                "output_schema": self.output_schema,
                "side_effect_class": self.side_effect_class,
            },
            sort_keys=True,
            separators=(",", ":"),
        )
        return hashlib.sha256(payload.encode()).hexdigest()


def verify_plan_descriptor(
    planned_against: ToolDescriptor,
    currently_advertised: ToolDescriptor,
    plan_in_flight: bool,
) -> None:
    if planned_against.hash() == currently_advertised.hash():
        return

    # Any drift is a contract change. Classify the severity.
    if planned_against.side_effect_class != currently_advertised.side_effect_class:
        raise CapabilityPoisoningError(
            f"Tool {planned_against.name!r} changed side-effect class "
            f"{planned_against.side_effect_class!r} -> "
            f"{currently_advertised.side_effect_class!r} mid-plan"
        )

    if planned_against.input_schema != currently_advertised.input_schema:
        # Additive optional fields with defaults are the dangerous case:
        # the schema stays "compatible" but the default widens scope.
        raise CapabilityPoisoningError(
            f"Tool {planned_against.name!r} input schema mutated "
            f"(additive defaults are still contract changes)"
        )

    if planned_against.description != currently_advertised.description:
        raise CapabilityPoisoningError(
            f"Tool {planned_against.name!r} description mutated; "
            f"plan was conditioned on stale text"
        )

    if plan_in_flight:
        raise CapabilityPoisoningError(
            f"Unclassified descriptor drift on {planned_against.name!r} "
            f"while plan in flight; freezing"
        )
