# Which invariants prevent silent loss of work during speculative agent branch rollback and merge reconciliation **Date of Record:** 2026 November 18 **Scope:** Applies to orchestration systems that fan out an agent's state into multiple speculative branches, execute them concurrently, then either commit one branch or reconcile several into a merged result. Does not apply to single-branch linear execution, nor to branches whose side effects are fully idempotent and externally journaled (those can tolerate the failure modes below without correctness loss). ## Core Distinction The distinction that matters is between *logical branch identity* and *physical execution identity*. A speculative branch is identified by an intent token (a hash of the parent state plus the branch's plan). Its execution may be retried, migrated, or forked across workers, so the physical worker that carries the branch is not the branch. Most rollback bugs arise because the orchestrator treats "the worker stopped reporting" as "the branch has no work to preserve," when in fact the branch's writes may already exist in a shared store under a worker that has since been reclaimed. A second distinction is between *rollback* (discard a branch's effects) and *reconciliation* (merge the effects of several branches). These are not duals. Rollback is a subtraction over a set of effect records; reconciliation is a join over the same set with a conflict policy. A system that implements rollback as "delete everything tagged with branch id" and reconciliation as "replay all commits in order" will silently lose work whenever a branch's effects were partially committed before the branch was abandoned, because the delete and the replay disagree about which records are in scope. ## What Was Observed During a fan-out of five speculative branches over a shared artifact store, a branch (call it B3) completed its writes to the store but its terminal acknowledgement was lost when the coordinating worker was preempted. The orchestrator, seeing no acknowledgement within the deadline, marked B3 as failed and issued a rollback. The rollback deleted records tagged `branch=B3`. Concurrently, reconciliation for the winning branch (B1) had already read a subset of B3's records as inputs to a merge (B3 had been selected as a secondary contributor because its plan score exceeded a threshold). The merge committed a derived record whose provenance pointed at B3 records that no longer existed. The derived record survived; its inputs did not. Downstream verification of the merged artifact failed only at a much later stage, and the failure was attributed to a model error rather than to the rollback. The invariant that was violated: **a branch's effect records must not be deleted while any committed derived record references them.** The system enforced referential integrity on writes but not on rollback deletions. Rollback was treated as a garbage-collection operation rather than as a state transition subject to the same constraints as a commit. ## Condition of Validity The failure is reachable only under a specific conjunction: (a) branches share a mutable store rather than writing to isolated overlays; (b) rollback is implemented as deletion rather than as a tombstone or a compensating write; (c) reconciliation may read from non-winning branches, so cross-branch provenance exists; and (d) terminal acknowledgement is the only liveness signal, so an unacknowledged branch is indistinguishable from a failed one. Remove any one of these and the silent-loss path closes: - Isolated overlays make rollback a drop of the overlay namespace, which cannot orphan a committed merge because the merge materializes copies. - Tombstoning preserves the record for provenance while marking it logically rolled back. - Reconciliation restricted to the winning branch eliminates cross-branch provenance. - A durable per-branch write-ahead log makes an unacknowledged branch recoverable rather than failed. The invariant is therefore conditional: it is required only when the four conditions co-occur. Systems that believe they are safe because they "use immutable stores" are often wrong, because immutability at the record level does not prevent deletion of the record itself. ## Decision Boundary The operational decision is when to treat a branch as *abandoned* versus *unknown*. The boundary should be drawn at durable evidence, not at elapsed time. A branch is abandoned only when a durable marker (a tombstone write, or an explicit rollback intent record) has been persisted and the branch's write-ahead log is sealed. Until then, the branch is *unknown*, and reconciliation must treat unknown branches' records as read-only inputs that may be referenced but never deleted. Concretely: if a branch has any record referenced by a committed merge, it is not eligible for deletion rollback regardless of its liveness state; it must be tombstoned. If it has no such references, deletion is safe. The check must be performed at rollback time under the same transaction that deletes, not as a pre-check, because a merge can commit between the check and the delete. This is the same race that makes naive reference counting unsafe for garbage collection in concurrent runtimes, and the fix is the same: make the reference check and the deletion atomic with respect to merge commits. ## Operational Effect Enforcing the invariant changes rollback from a best-effort cleanup into a constrained transition, which increases storage (tombstones persist) and requires reconciliation to carry provenance pointers rather than values. In practice this means merge commits must record the set of contributing branch records by identity, and rollback must consult that index. The cost is a small amount of retained metadata per branch and a lock or serialization point between merge commit and rollback deletion. The benefit is that the failure mode becomes detectable: an attempted deletion of a referenced record raises, and the orchestrator can escalate rather than silently corrupt provenance. The following guard encodes the boundary. It is intended to be called inside the rollback transaction, after the reference index has been consulted and before any deletion is issued. ```python class RollbackProvenanceError(RuntimeError): """Raised when a rollback would delete a branch record still referenced by a committed merge.""" pass def rollback_branch(branch_id, reference_index, delete_records, tombstone_records): """ Roll back a speculative branch without orphaning committed merges. reference_index maps record_id -> set of committed merge ids that cite it. delete_records and tombstone_records are callables that mutate the store. Must be invoked inside the rollback transaction. """ record_ids = list_branch_records(branch_id) referenced = [rid for rid in record_ids if reference_index.get(rid)] if referenced: # Unknown/abandoned branch with live provenance: tombstone, never delete. tombstone_records(branch_id, record_ids) raise RollbackProvenanceError( f"branch {branch_id} has {len(referenced)} record(s) referenced by " f"committed merges {sorted({m for rid in referenced for m in reference_index[rid]})}; " f"tombstoned instead of deleted" ) delete_records(branch_id, record_ids) ```