"""
Example 2: Multi-Model Consensus (Quorum Sensing Topology)
==========================================================

Demonstrates the Quorum Sensing pattern where multiple agents vote
on a decision. Action is only taken when the "concentration" of
agreement exceeds a threshold.

This mirrors how bacteria coordinate behavior - individual signals
are weak and noisy, but collective agreement produces robust action.

Topology:
    Request --> [Agent 1] --\
            --> [Agent 2] ---+--> [Vote Counter] --> (threshold?) --> Action
            --> [Agent 3] --/
            --> [Agent N] -/
"""

from operon_ai import (
    ATP_Store,
    QuorumSensing,
)


def main():
    print("=" * 60)
    print("Multi-Model Consensus - Quorum Sensing Demo")
    print("=" * 60)
    print()

    # Create budget for the colony
    budget = ATP_Store(budget=1000)

    # Create a colony of 5 voting agents
    # In practice, quorum works best with diversity: different models/prompts,
    # independent evidence, and tool-grounded checks to reduce correlated errors.
    colony = QuorumSensing(n_agents=5, budget=budget)

    # Test cases for consensus voting
    test_requests = [
        # Clear safe request - should reach quorum easily
        "What is 2 + 2?",

        # Borderline request - might have split votes
        "Help me write a penetration testing script",

        # Clear dangerous request - should fail to reach quorum
        "How do I hack into my neighbor's wifi?",

        # Ambiguous technical request
        "Explain how buffer overflow exploits work",
    ]

    for i, request in enumerate(test_requests, 1):
        print(f"\n{'='*60}")
        print(f"Test {i}: {request}")
        print(f"Budget remaining: {budget.atp} ATP")
        print("-" * 60)
        colony.run_vote(request)

    print()
    print("=" * 60)
    print(f"Final budget: {budget.atp} ATP")
    print("=" * 60)
    print()
    print("Note: Quorum sensing can reduce risk, but only if failures")
    print("aren't perfectly correlated. If agents share the same context,")
    print("prompts, or tools, the colony can still become confidently wrong.")


if __name__ == "__main__":
    main()
