# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-lossy-summarization-checkpoints-have-silently-invalidated-an-agent-s-tool-call-preconditions.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-lossy-summarization-checkpoints-have-silently-invalidated-an-agent-s-tool-call-preconditions.warrant.json
# Markdown SHA-256:   d5fae3756579158cb59898e0374e0948610ec879a0288f209ace768bce9b676e
# Code AST SHA-256:   fc9a39cfffd8c5a220eeb97ad7db3726949e6c225e5819401aaf14f94be52590
# Verified Classes:   PreconditionInvalidated, PreconditionLedger
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class PreconditionInvalidated(RuntimeError):
    """Raised when a tool call's arguments diverge from a pinned precondition
    that was established before a lossy compaction checkpoint."""
    pass


class PreconditionLedger:
    def __init__(self):
        self._pins = {}  # name -> (value, guard)

    def pin(self, name, value, guard=None):
        # guard is an optional predicate over candidate values, e.g. lambda v: v != "v13"
        self._pins[name] = (value, guard)

    def validate_call(self, tool_name, kwargs, observed_in_live_context):
        for name, (pinned, guard) in self._pins.items():
            if name not in kwargs:
                continue
            candidate = kwargs[name]
            if not observed_in_live_context.get(name, False):
                # value came from a summary, not the live window: it is a claim
                if candidate != pinned:
                    raise PreconditionInvalidated(
                        f"{tool_name}: argument {name}={candidate!r} derived from "
                        f"summary, but pinned value is {pinned!r}; re-verify against "
                        f"external state before executing."
                    )
            if guard is not None and not guard(candidate):
                raise PreconditionInvalidated(
                    f"{tool_name}: argument {name}={candidate!r} violates pinned "
                    f"guard; precondition was established pre-checkpoint and must "
                    f"not be relaxed by post-checkpoint observation."
                )

    def reconcile_verification(self, name, observed):
        pinned, guard = self._pins[name]
        if observed != pinned and (guard is None or not guard(observed)):
            raise PreconditionInvalidated(
                f"verification for {name} returned {observed!r}, contradicting "
                f"pinned {pinned!r}; halt trajectory, do not reconcile in favor "
                f"of the summary."
            )


def execute_tool(tool_name, kwargs, ledger, live_context, verification=None):
    ledger.validate_call(tool_name, kwargs, live_context)
    if verification is not None:
        for name, observed in verification.items():
            ledger.reconcile_verification(name, observed)
    return {"tool": tool_name, "args": kwargs, "status": "executed"}
