Files
OBLITERATUS/obliteratus/distributed/consensus.py
Joseph MaglyandClaude Opus 5.5 8dd6add995 fix(distributed): TP2 hardening before two-GPU qualification
Fixes eight reported defects in the native tensor-parallel path:

- #278 VERIFY admits host memory in a symmetric vote before allocating
  and broadcasting baseline input tensors.
- #280 require_record_consensus canonicalizes inside the poison-protected
  exchange and compares canonical payloads.
- #282 EXCISE votes on fallible digest preparation before the next
  collective.
- #285 preflight, model session, PROBE and EXCISE vote on rank-local
  preparation failures before the next collective, including the
  evidence-directory preparation that previously allocated before its
  all_reduce.
- #275 postcondition and VERIFY digest sets stream the full declared
  operation range (up to MAX_EXCISE_OPERATIONS) while preserving
  existing identities.
- #281 preflight source identity digests stream inventory records
  instead of using one 64 KiB control record.
- #286 the evidence checker requires an integer schema_version and
  checkout/source inventories are bounded and streamed.
- #276 standalone operation planning uses bounded chunked finite checks.

Each fix has CPU/Gloo regression tests, including one-rank fault cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 20:53:20 -04:00

215 lines
8.8 KiB
Python

"""Bounded Gloo consensus primitives for the CPU protocol test lane."""
from __future__ import annotations
import hashlib
import json
from collections.abc import Sequence
import torch
import torch.distributed as dist
from obliteratus.distributed.contracts import (
MAX_CONSENSUS_BYTES,
MAX_WORLD_SIZE,
ContractError,
RuntimeContractError,
canonical_record,
)
_LENGTH_BYTES = 4
# Fixed-size bounded buffer allocation and deterministic computation over identical
# gathered bytes are the irreducible process-fault boundary. Bounded timeout/rank
# loss fails closed (EXCISE: LMS_EXCISE_STATE_UNCERTAIN), never as success.
def encode_frame(payload: bytes, *, capacity: int = MAX_CONSENSUS_BYTES) -> torch.Tensor:
"""Encode bytes into one fixed-size uint8 frame with zero padding."""
if isinstance(capacity, bool) or not isinstance(capacity, int) or not 1 <= capacity <= MAX_CONSENSUS_BYTES:
raise ContractError(f"capacity must be between 1 and {MAX_CONSENSUS_BYTES}")
if not isinstance(payload, bytes):
raise ContractError("frame payload must be bytes")
if len(payload) > capacity:
raise ContractError(f"frame payload exceeds {capacity} bytes")
frame = bytearray(_LENGTH_BYTES + capacity)
frame[:_LENGTH_BYTES] = len(payload).to_bytes(_LENGTH_BYTES, byteorder="big")
frame[_LENGTH_BYTES : _LENGTH_BYTES + len(payload)] = payload
return torch.frombuffer(frame, dtype=torch.uint8).clone()
def decode_frame(frame: torch.Tensor) -> bytes:
"""Decode a canonical fixed-size frame and reject non-zero padding."""
if not isinstance(frame, torch.Tensor) or frame.dtype != torch.uint8 or frame.ndim != 1:
raise ContractError("frame must be a one-dimensional uint8 tensor")
capacity = frame.numel() - _LENGTH_BYTES
if not 1 <= capacity <= MAX_CONSENSUS_BYTES:
raise ContractError("frame has an invalid capacity")
raw = bytes(frame.detach().cpu().tolist())
size = int.from_bytes(raw[:_LENGTH_BYTES], byteorder="big")
if size > capacity:
raise ContractError("frame length exceeds its capacity")
if any(raw[_LENGTH_BYTES + size :]):
raise ContractError("frame padding must be zero")
return raw[_LENGTH_BYTES : _LENGTH_BYTES + size]
def _require_gloo(group: dist.ProcessGroup | None) -> tuple[int, int]:
if not dist.is_available() or not dist.is_initialized():
raise ContractError("a Gloo process group must be initialized")
backend = str(dist.get_backend(group)).lower()
if backend != "gloo":
raise ContractError("CPU protocol consensus requires the Gloo backend")
rank, world_size = dist.get_rank(group), dist.get_world_size(group)
if not 2 <= world_size <= MAX_WORLD_SIZE:
raise ContractError(f"Gloo world_size must be between 2 and {MAX_WORLD_SIZE}")
return rank, world_size
def gloo_all_gather_records(
record: object,
*,
group: dist.ProcessGroup | None = None,
capacity: int = MAX_CONSENSUS_BYTES,
) -> tuple[bytes, ...]:
"""Gather bounded canonical records without Python object collectives."""
_rank, world_size = _require_gloo(group)
# Capacity defines the wire shape and must be valid and identical on all ranks.
if isinstance(capacity, bool) or not isinstance(capacity, int) or not 1 <= capacity <= MAX_CONSENSUS_BYTES:
raise ContractError(f"capacity must be between 1 and {MAX_CONSENSUS_BYTES}")
frame = torch.zeros(_LENGTH_BYTES + capacity, dtype=torch.uint8)
gathered = [torch.zeros_like(frame) for _ in range(world_size)]
preparation_error: BaseException | None = None
try:
payload = canonical_record(record, max_bytes=capacity)
frame.copy_(encode_frame(payload, capacity=capacity))
except BaseException as exc:
preparation_error = exc
frame.zero_()
frame[:_LENGTH_BYTES] = 255
dist.all_gather(gathered, frame, group=group)
if preparation_error is not None:
raise preparation_error
if any(bool(torch.all(item[:_LENGTH_BYTES] == 255)) for item in gathered):
raise ContractError("a peer rank failed to prepare its consensus payload")
return tuple(decode_frame(item) for item in gathered)
def require_consensus_digest(
digest: str,
*,
group: dist.ProcessGroup | None = None,
) -> str:
"""Require every rank to supply the same lowercase SHA-256 digest."""
_rank, world_size = _require_gloo(group)
local = torch.zeros(33, dtype=torch.uint8)
gathered = [torch.zeros_like(local) for _ in range(world_size)]
preparation_error: BaseException | None = None
try:
if not isinstance(digest, str) or len(digest) != 64:
raise ContractError("digest must be 64 lowercase hexadecimal characters")
try:
payload = bytes.fromhex(digest)
except ValueError as exc:
raise ContractError("digest must be 64 lowercase hexadecimal characters") from exc
if digest != digest.lower() or payload.hex() != digest:
raise ContractError("digest must be 64 lowercase hexadecimal characters")
local[1:].copy_(torch.tensor(tuple(payload), dtype=torch.uint8))
except BaseException as exc:
preparation_error = exc
local.zero_()
local[0] = 1
dist.all_gather(gathered, local, group=group)
if preparation_error is not None:
raise preparation_error
if any(int(item[0]) != 0 for item in gathered):
raise ContractError("a peer rank failed to prepare its consensus payload")
if any(not torch.equal(local, item) for item in gathered):
raise ContractError("rank digests disagree")
return digest
def require_record_consensus(
record: object,
*,
group: dist.ProcessGroup | None = None,
) -> str:
"""Require every rank to present the same bounded canonical record."""
payloads = gloo_all_gather_records(record, group=group)
if any(payload != payloads[0] for payload in payloads):
raise ContractError("rank digests disagree")
return hashlib.sha256(payloads[0]).hexdigest()
def unanimous_vote(
sequence: int,
accepted: bool,
*,
group: dist.ProcessGroup | None = None,
) -> bool:
"""Return true only when every rank votes yes for the same sequence."""
if isinstance(sequence, bool) or not isinstance(sequence, int) or sequence < 0:
raise ContractError("vote sequence must be a non-negative integer")
if not isinstance(accepted, bool):
raise ContractError("accepted must be a boolean")
_rank, world_size = _require_gloo(group)
local = torch.zeros(2, dtype=torch.int64)
gathered = [torch.zeros_like(local) for _ in range(world_size)]
preparation_error: BaseException | None = None
try:
local.copy_(torch.tensor((sequence, int(accepted)), dtype=torch.int64))
except BaseException as exc:
preparation_error = exc
local.fill_(-1)
dist.all_gather(gathered, local, group=group)
if preparation_error is not None:
raise preparation_error
if any(int(item[0]) < 0 for item in gathered):
raise ContractError("a peer rank failed to prepare its consensus payload")
if any(int(item[0]) != sequence for item in gathered):
raise ContractError("rank vote sequences disagree")
return all(bool(int(item[1])) for item in gathered)
def gather_failure_codes(
sequence: int,
error_code: str | None,
*,
group: dist.ProcessGroup | None = None,
) -> tuple[str, ...]:
"""Gather actual preparation refusals in rank order, without exception text."""
payloads = gloo_all_gather_records(
{"sequence": sequence, "error_code": error_code}, group=group, capacity=256,
)
codes = []
for payload in payloads:
try:
record = json.loads(payload)
except (UnicodeError, ValueError) as exc:
raise ContractError("preparation vote is not JSON") from exc
if (
not isinstance(record, dict)
or set(record) != {"sequence", "error_code"}
or isinstance(record["sequence"], bool)
or not isinstance(record["sequence"], int)
or not 0 <= record["sequence"] <= 2**63 - 1
or record["sequence"] != sequence
or payload != canonical_record(record)
):
raise ContractError("rank preparation vote sequences or records disagree")
code = record["error_code"]
if code is not None:
RuntimeContractError(code, "preparation failed")
codes.append(code)
return tuple(codes)
def assert_rank_order(records: Sequence[object], *, world_size: int) -> None:
"""Reject missing, duplicate, or reordered rank-bearing records."""
if len(records) != world_size:
raise ContractError("record count does not match world_size")
ranks = [getattr(record, "rank", None) for record in records]
if ranks != list(range(world_size)):
raise ContractError("records must appear once in global-rank order")