mirror of
https://github.com/saymrwulf/crisis.git
synced 2026-05-14 20:37:54 +00:00
The previous design routed every Crisis message through a `Mothership` that
also held every agent's LamportGraph, ran the byzantine scan from a
privileged vantage, and built proofs from its own view. That made the
mothership a chokepoint — exactly what a BFT layer is supposed to remove.
This commit redistributes responsibility along the lines you'd expect from
a real open protocol:
Each `CrisisAgent` now owns:
- its own `LamportGraph` (the agent's view of the network)
- `emit_claim(claim) → Message`: wraps a Claim into a fully-valid Crisis
Message built from the agent's OWN graph state, with chain link + cross
references + mined PoW nonce
- `receive(message)`: extends my graph if integrity holds; idempotent
- `gossip_to(peer) → int`: shares everything I have with peer until
quiescence (Algorithm 4 in the paper, in-process flavor)
- `detect_mutations() → list[LocalAlarm]`: scans MY graph for same-id
spacelike vertex pairs via the existing
`LamportGraph.find_mutations`, filtered by application-layer
`statement_id` so cross-detector AlarmClaims canonicalize
The `Mothership` shrinks to coordinator-only:
- bootstrap (register honest agents; trigger boundary open with a joiner)
- clock (call each agent's `next_turn()` per turn)
- first-hop routing (sender's emission → declared target subset)
- all-pairs gossip rounds between turns
- emit_alarms_from_detectors(): poll each agent for its LocalAlarms,
wrap any returned alarms into AlarmClaim payloads, broadcast them as
Crisis Messages over the gossip layer
Gone (regression-tested in `test_no_chokepoint.py`):
- `Mothership._graphs`, `Mothership.all_graphs()`, `Mothership.graph_of()`
- `alarm.scan_for_mutations(mothership)`
- any path where the mothership reads an agent's internal state
New voting layer (`crisis_agents/vote.py`):
- `AlarmClaim`: a Crisis-payload dataclass discriminated by `kind="alarm"`.
Wraps the accused process_id, statement_id, witness_digests, and
detection turn. Round-trips through JSON same as Claim.
- `quorum_for(n) = ceil(2n/3)`: classic BFT threshold.
- `tally_alarms(graph, threshold)`: groups AlarmClaim vertices by
(accused, statement_id, witness_pair), counts unique signer
process_ids, ratifies groups meeting the threshold. Deterministic
ordering so two equal graphs produce equal `RatifiedAlarm` lists.
- `RatifiedAlarm`: the network-level consensus on byzantine behavior.
Multi-signer proofs (`crisis_agents/proof.py`):
- schema_version bumped 1 → 2.
- ProofDocument now embeds every signer's process_id_hex and the
quorum threshold that was met. Self-consistency check enforces
distinct signers, witness pairs, and signer count ≥ threshold.
Byzantine scenario rewrite:
- `MockByzantineAgent` now takes an `intro_claim` for its first turn (a
benign broadcast). The intro is technically necessary: the agent's two
contradictory variants both chain to the intro vertex, so they can
propagate through gossip — without it, the second variant would fail
the chain constraint in any graph already holding the first.
- `fact_check` scenario: closed phase still has 3 honest agents emitting
6 claims each into the closed log; Crisis phase grew to 2 turns (intro
+ equivocation) so the byzantine can establish its same-id anchor
before equivocating.
End-to-end CLI output reframed around six phases:
1. closed team (no Crisis)
2. boundary opens
3. emission + gossip
4. decentralized detection (each agent reports its own findings)
5. alarms emitted + gossiped + ratified by quorum
6. proof emission
Tests (51 fresh + 5 carried over for boundary):
- `test_mothership.py`: per-agent graph ownership, broadcast vs.
targeted delivery semantics, gossip propagation, regression guards
against the removed centralization attributes.
- `test_alarm.py`: every honest agent independently detects the same
mutation; the byzantine doesn't detect itself; witness pairs are
canonical across detectors.
- `test_vote.py`: AlarmClaim round-trip, quorum formulas, tally
determinism, mothership convenience method matches direct tallying.
- `test_proof.py`: build_proof from RatifiedAlarm; multi-signer JSON
round-trip; tampered-witness/below-quorum/duplicate-signer rejection.
- `test_no_chokepoint.py` (the centerpiece): after the full lifecycle,
every honest agent's ratified-alarm set is byte-identical. A single
byzantine accuser alone cannot ratify. Forbidden attributes don't
exist on Mothership.
Full suite: 163 tests, all green in 0.80s.
CrisisViz: untouched by this refactor. The `crisis_data.json` pipeline
the visualizer consumes is produced by the orthogonal
`crisis.demo.Simulation`, which this commit doesn't touch.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
97 lines
3.1 KiB
Python
97 lines
3.1 KiB
Python
"""Tests for ProofDocument + self-consistent verification."""
|
|
|
|
import json
|
|
from dataclasses import replace
|
|
|
|
import pytest
|
|
|
|
from crisis_agents.proof import (
|
|
ProofDocument,
|
|
VerificationResult,
|
|
build_proof,
|
|
verify_proof_self_consistent,
|
|
)
|
|
from crisis_agents.vote import RatifiedAlarm
|
|
|
|
|
|
def _sample_ratified() -> RatifiedAlarm:
|
|
return RatifiedAlarm(
|
|
accused_process_id_hex="76468f93" * 8,
|
|
statement_id="s03",
|
|
witness_digests=("a" * 64, "b" * 64),
|
|
signer_process_id_hexes=("11" * 32, "22" * 32, "33" * 32),
|
|
quorum_threshold=3,
|
|
)
|
|
|
|
|
|
class TestBuildProof:
|
|
|
|
def test_produces_well_formed_proof(self):
|
|
proof = build_proof(_sample_ratified())
|
|
assert proof.accused_process_id_hex.startswith("76468f93")
|
|
assert proof.statement_id == "s03"
|
|
assert proof.quorum_threshold == 3
|
|
assert len(proof.signer_process_id_hexes) == 3
|
|
assert proof.schema_version == 2
|
|
|
|
def test_summary_mentions_quorum(self):
|
|
proof = build_proof(_sample_ratified())
|
|
assert "quorum" in proof.summary.lower()
|
|
|
|
|
|
class TestRoundtripJSON:
|
|
|
|
def test_to_from_json(self):
|
|
original = build_proof(_sample_ratified())
|
|
roundtrip = ProofDocument.from_json(original.to_json())
|
|
assert roundtrip == original
|
|
|
|
def test_json_is_indented_and_sorted(self):
|
|
proof = build_proof(_sample_ratified())
|
|
text = proof.to_json()
|
|
parsed = json.loads(text)
|
|
# Sorted keys: schema_version after accused_process_id_hex alphabetically
|
|
# (just verify it's a dict with the expected keys)
|
|
assert set(parsed.keys()) == {
|
|
"accused_process_id_hex", "schema_version", "signer_process_id_hexes",
|
|
"statement_id", "quorum_threshold", "summary", "witness_digests",
|
|
}
|
|
|
|
|
|
class TestSelfConsistentVerification:
|
|
|
|
def test_valid_proof_passes(self):
|
|
proof = build_proof(_sample_ratified())
|
|
result = verify_proof_self_consistent(proof)
|
|
assert result.ok, result.reason
|
|
|
|
def test_duplicate_witnesses_fail(self):
|
|
proof = build_proof(_sample_ratified())
|
|
tampered = replace(proof, witness_digests=("a" * 64, "a" * 64))
|
|
assert not verify_proof_self_consistent(tampered).ok
|
|
|
|
def test_below_quorum_fails(self):
|
|
ra = _sample_ratified()
|
|
proof = build_proof(ra)
|
|
tampered = replace(
|
|
proof,
|
|
signer_process_id_hexes=("11" * 32, "22" * 32), # 2 < threshold 3
|
|
)
|
|
result = verify_proof_self_consistent(tampered)
|
|
assert not result.ok
|
|
assert "quorum" in result.reason.lower()
|
|
|
|
def test_duplicate_signers_fail(self):
|
|
proof = build_proof(_sample_ratified())
|
|
tampered = replace(
|
|
proof,
|
|
signer_process_id_hexes=("11" * 32, "11" * 32, "33" * 32),
|
|
)
|
|
assert not verify_proof_self_consistent(tampered).ok
|
|
|
|
def test_unsupported_schema_version_fails(self):
|
|
proof = build_proof(_sample_ratified())
|
|
tampered = replace(proof, schema_version=99)
|
|
result = verify_proof_self_consistent(tampered)
|
|
assert not result.ok
|
|
assert "schema" in result.reason.lower()
|