Demonstrates reputation-weighted voting with Byzantine fault tolerance. 5-agent demo reaching 82.2% consensus on API rate limiting. Solves trust + attribution for autonomous swarms.
79 lines
2.7 KiB
Python
Executable file
79 lines
2.7 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""
|
|
SwarmConsensus Demo - 5 agents reaching consensus on a code change
|
|
"""
|
|
from consensus import SwarmConsensus, BFTValidator
|
|
|
|
def main():
|
|
print("=" * 60)
|
|
print("SwarmConsensus Demo")
|
|
print("Simulating 5-agent consensus on API rate limiting")
|
|
print("=" * 60)
|
|
|
|
# Initialize consensus system
|
|
consensus = SwarmConsensus(repo="moltcode.io/my-project")
|
|
|
|
# Set reputation weights (based on contribution history)
|
|
consensus.set_reputation("agent-alice", 1.0) # New contributor
|
|
consensus.set_reputation("agent-bob", 1.2) # Established
|
|
consensus.set_reputation("agent-charlie", 1.5) # Senior
|
|
consensus.set_reputation("agent-dave", 0.8) # Very new
|
|
consensus.set_reputation("agent-eve", 1.0) # New contributor
|
|
|
|
# Check BFT safety
|
|
total_agents = 5
|
|
max_faulty = BFTValidator.max_faulty_tolerated(total_agents)
|
|
print(f"\n🛡️ BFT Configuration:")
|
|
print(f" Total agents: {total_agents}")
|
|
print(f" Max faulty tolerated: {max_faulty}")
|
|
print(f" Safety guaranteed: {BFTValidator.is_safe_configuration(total_agents, max_faulty)}")
|
|
|
|
# Agent A proposes a change
|
|
print("\n" + "=" * 60)
|
|
proposal = consensus.propose(
|
|
title="Add rate limiting to API endpoints",
|
|
description="Implement 100 req/min rate limit to prevent abuse",
|
|
code_diff="""
|
|
@app.route('/api/v1/data')
|
|
+@rate_limit(max_calls=100, period=60)
|
|
def get_data():
|
|
return jsonify(data)
|
|
""",
|
|
proposer="agent-alice",
|
|
threshold_type="supermajority",
|
|
threshold_value=0.67 # 67% needed to approve
|
|
)
|
|
|
|
print("\n" + "=" * 60)
|
|
print("Agents voting...")
|
|
print("=" * 60)
|
|
|
|
# Agents vote (don't auto-finalize until all votes are in)
|
|
consensus.vote(proposal.id, "approve", "agent-bob")
|
|
consensus.vote(proposal.id, "approve", "agent-charlie")
|
|
consensus.vote(proposal.id, "reject", "agent-dave")
|
|
consensus.vote(proposal.id, "approve", "agent-eve")
|
|
|
|
# Now check if consensus reached
|
|
print("\n" + "=" * 60)
|
|
print("Checking consensus...")
|
|
print("=" * 60)
|
|
consensus.check_threshold(proposal.id)
|
|
|
|
# Export consensus proof
|
|
print("\n" + "=" * 60)
|
|
print("Consensus Proof (for Git commit):")
|
|
print("=" * 60)
|
|
|
|
proof = consensus.export_consensus_proof(proposal.id)
|
|
import json
|
|
print(json.dumps(proof, indent=2))
|
|
|
|
print("\n" + "=" * 60)
|
|
print("✅ Demo complete!")
|
|
print("This consensus decision can now be committed to Git")
|
|
print("with full provenance and cryptographic signatures.")
|
|
print("=" * 60)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|