# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-rollback-divergence-in-speculative-multi-agent-branch-execution.md # Remnant Warrant: https://corpusfetch.com/c/what-structural-signals-indicate-rollback-divergence-in-speculative-multi-agent-branch-execution.warrant.json # Markdown SHA-256: 47a45eeee84247c0e83172982fb5f2df05d2f7297fd179595d2407b69ea3a825 # Code AST SHA-256: 8b12c9e76672a538e54647a8bd6310b74ccfa7d170df309c5a14a2309e05c9c8 # Verified Classes: RollbackDivergenceError, ToolClass, ToolCall, Branch # Defensive Guards: 3 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) from dataclasses import dataclass, field from enum import Enum from typing import Callable class RollbackDivergenceError(RuntimeError): """Raised when a speculative branch's rollback leaves external residue that the restored context cannot reconcile.""" class ToolClass(Enum): READ_ONLY = "read_only" IDEMPOTENT_WRITE = "idempotent_write" NON_IDEMPOTENT_WRITE = "non_idempotent_write" @dataclass class ToolCall: tool: str tool_class: ToolClass nonce: str | None compensation: Callable[[], None] | None = None @dataclass class Branch: branch_id: str ledger: list[ToolCall] = field(default_factory=list) def audit_pre_fork(reachable_tools: dict[str, ToolClass]) -> None: """Refuse to speculate if any non-idempotent write lacks a compensation path.""" unsafe = [ name for name, cls in reachable_tools.items() if cls is ToolClass.NON_IDEMPOTENT_WRITE ] if unsafe: raise RollbackDivergenceError( f"Speculation unsafe: non-idempotent writes reachable without " f"compensation: {unsafe}. Disable write-class tools for speculative " f"branches or abort the fork." ) def rollback_branch(branch: Branch) -> list[str]: """Attempt to roll back a losing branch. Returns list of orphaned effects that could not be compensated and must be surfaced to the survivor.""" orphans: list[str] = [] for call in branch.ledger: if call.tool_class is ToolClass.READ_ONLY: continue if call.tool_class is ToolClass.IDEMPOTENT_WRITE and call.nonce: if call.compensation is None: raise RollbackDivergenceError( f"Branch {branch.branch_id}: idempotent write {call.tool} " f"has nonce but no compensation callable; cannot cleanly " f"roll back." ) call.compensation() continue # Non-idempotent or nonce-less write: cannot be undone. orphans.append(f"{call.tool}(nonce={call.nonce})") if orphans: raise RollbackDivergenceError( f"Branch {branch.branch_id} rollback left {len(orphans)} orphaned " f"external effects: {orphans}. Survivor must be re-prompted with " f"explicit attribution or the fork tree must be aborted." ) return orphans