# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-optimistic-concurrency-divergence-in-speculative-agent-branching-over-shared-context-stores.md # Remnant Warrant: https://corpusfetch.com/c/what-structural-signals-indicate-optimistic-concurrency-divergence-in-speculative-agent-branching-over-shared-context-stores.warrant.json # Markdown SHA-256: 1365a1375719a70d27b9715676f2130ba81ef894b4d88d612663b3dce965b5ed # Code AST SHA-256: a23c08678b898f94f3e8e02e4bae2038f7fa843f492b946b2a9b70a30b347d92 # Verified Classes: SpeculativeProvenanceError, Branch, CommitLog # Defensive Guards: 2 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 class SpeculativeProvenanceError(RuntimeError): """Raised when a branch's validated write is not causally grounded in a committed lineage (read-set includes discarded sibling speculation).""" @dataclass class Branch: branch_id: str base_version: int # (key, version) pairs the branch's memory tools actually read read_witness: set[tuple[str, int]] = field(default_factory=set) # versions this branch's write was derived from / cites in rationale cited_versions: set[int] = field(default_factory=set) @dataclass class CommitLog: # version -> parent version (lineage DAG, linear here for brevity) lineage: dict[int, int] = field(default_factory=dict) committed: set[int] = field(default_factory=set) def reachable_from(self, version: int) -> set[int]: seen: set[int] = set() cur = version while cur is not None and cur not in seen: seen.add(cur) cur = self.lineage.get(cur) return seen def validate_selection(winner: Branch, log: CommitLog) -> None: """Enforce: read-set and cited-versions must be subset of committed lineage reachable from the winner's base. OCC CAS alone is insufficient.""" reachable = log.reachable_from(winner.base_version) bad_reads = {v for (_, v) in winner.read_witness if v not in reachable} if bad_reads: raise SpeculativeProvenanceError( f"branch {winner.branch_id} read versions {sorted(bad_reads)} " f"outside committed lineage reachable from base v{winner.base_version}; " f"likely absorbed discarded sibling speculation" ) bad_cites = winner.cited_versions - reachable if bad_cites: raise SpeculativeProvenanceError( f"branch {winner.branch_id} rationale cites uncommitted versions " f"{sorted(bad_cites)}; committed state would contain dangling provenance" ) def commit_branch(branch: Branch, log: CommitLog) -> int: validate_selection(branch, log) new_version = max(log.committed, default=0) + 1 log.lineage[new_version] = branch.base_version log.committed.add(new_version) return new_version # --- reproduction of the observed trace --- log = CommitLog(lineage={7: 6}, committed={6, 7}) branch_b = Branch( branch_id="B", base_version=8, # CAS validated against v8 (A's speculative write) read_witness={("plan", 7), ("plan", 8)}, # prompt snapshot v7 + live read of A's v8 cited_versions={7}, # rationale explicitly cites v7 step 3 ) # Orchestrator selected B and discarded A (v8). v8 is NOT committed. try: commit_branch(branch_b, log) except SpeculativeProvenanceError as e: print(f"BLOCKED: {e}") # Correct remediation: re-derive B from v7 snapshot, or co-commit A.