# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-bound-speculative-rollback-when-a-parallel-agent-branch-s-side-effects-have-already-escaped-the-sandbox.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-bound-speculative-rollback-when-a-parallel-agent-branch-s-side-effects-have-already-escaped-the-sandbox.warrant.json
# Markdown SHA-256:   10611c8add7e23764e45d5b3831743329d5d61c37ac9b8ca409a6fd8c26aba0c
# Code AST SHA-256:   27184cdfac3e6d41a6943047a5b51ad55e72924f4163f889f959d66cbf92b426
# Verified Classes:   IrrevocableEffectLeak
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class IrrevocableEffectLeak(RuntimeError):
    """Raised when a speculative branch has already produced an external,
    non-reversible effect and the orchestrator is asked to roll it back."""


EFFECT_CLASS = {"read": 1, "write_scratch": 2, "send_email": 3, "charge": 3, "deny_ticket": 3}


def rollback_branch(branch, journal):
    for entry in reversed(journal):
        cls = EFFECT_CLASS.get(entry.tool, 3)  # unknown tools default to irrevocable
        if cls == 3 and entry.effect_emitted:
            raise IrrevocableEffectLeak(
                f"branch {branch.id} emitted irrevocable effect via {entry.tool!r} "
                f"at {entry.timestamp}; rollback would leave external state diverged. "
                f"Promote branch or escalate as split-brain commit."
            )
        if cls == 1 and entry.inverse is None:
            raise IrrevocableEffectLeak(
                f"reversible tool {entry.tool!r} in branch {branch.id} has no journaled inverse"
            )
    branch.discard()
    return True


def propose_class3(branch, call):
    if EFFECT_CLASS.get(call.tool, 3) == 3:
        # branch never holds the credential; it only emits a proposal
        return {"proposal": call, "branch": branch.id, "executed": False}
    return call.execute()
