# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-a-speculative-tool-call-prefetch-has-already-crossed-its-side-effect-commit-point-before-the-abort-signal-lands.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-a-speculative-tool-call-prefetch-has-already-crossed-its-side-effect-commit-point-before-the-abort-signal-lands.warrant.json
# Markdown SHA-256:   ca9c01a485278f8ced23759b6ee45c422e9249570760a719e86c152c0da2bccf
# Code AST SHA-256:   2fca898f5e41eddfd8a9e6b35dd5ca4b34fd22de1692a0c96c6501b88e752156
# Verified Classes:   SpeculativeAbortStateViolation, SpeculativeExecutor
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class SpeculativeAbortStateViolation(RuntimeError):
    """Raised when a speculative branch is discarded after a possible commit
    with no tombstone or commit-status confirmation, orphaning a side effect."""
    pass


class SpeculativeExecutor:
    def __init__(self, tool_registry, checkpoint_store):
        self.tools = tool_registry
        self.checkpoints = checkpoint_store

    def abort_speculative(self, branch):
        tool = self.tools[branch.tool_name]
        if tool.idempotent:
            # Safe: duplicate commit is a no-op; rewind is clean.
            self.checkpoints.rewind(branch.checkpoint_id)
            return

        # Non-idempotent: we cannot observe t_commit, only t_response.
        # The abort may have landed inside [t_commit, t_commit + t_response].
        commit_confirmed = tool.query_commit_status(branch.idempotency_key)
        if commit_confirmed is True:
            # Side effect exists. Do not rewind as if clean; record tombstone
            # so the retry path is forced to reconcile before re-issuing.
            self.checkpoints.record_tombstone(
                branch.checkpoint_id,
                intent_key=branch.idempotency_key,
                status="committed_during_abort",
            )
            self.checkpoints.rewind(branch.checkpoint_id)
            return

        if commit_confirmed is False:
            # Tool can prove non-commit; clean rewind is sound.
            self.checkpoints.rewind(branch.checkpoint_id)
            return

        # commit_confirmed is None: tool cannot answer. We are in the
        # dangerous regime by default. Refuse to discard silently.
        if not branch.idempotency_key:
            raise SpeculativeAbortStateViolation(
                f"abort of non-idempotent tool {branch.tool_name!r} with no "
                f"intent-bound idempotency key and no commit-status; side "
                f"effect state is unknown and unreconcilable"
            )

        self.checkpoints.record_tombstone(
            branch.checkpoint_id,
            intent_key=branch.idempotency_key,
            status="possibly_committed",
        )
        self.checkpoints.rewind(branch.checkpoint_id)
        # Retry path is now obligated to reconcile the tombstone before
        # re-issuing the logical intent.
