examples/emergency_freeze.py
| 1 | """Example: emergency freeze of all agent actions. |
| 2 | |
| 3 | All five governance nodes approve an ``EMERGENCY_FREEZE`` proposal. A signed |
| 4 | ``ConsensusResult`` is produced and its ML-DSA signature is independently |
| 5 | verified. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | from quantumshield.identity.agent import AgentIdentity |
| 11 | |
| 12 | from pqc_ai_governance import ( |
| 13 | ConsensusRound, |
| 14 | GovernanceNode, |
| 15 | GovernanceProposal, |
| 16 | NodeRegistry, |
| 17 | ProposalKind, |
| 18 | QuorumPolicy, |
| 19 | VoteDecision, |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | def main() -> None: |
| 24 | print("== PQC AI Governance: Emergency Freeze ==\n") |
| 25 | |
| 26 | alice = GovernanceNode(identity=AgentIdentity.create("alice"), name="alice", weight=1) |
| 27 | bob = GovernanceNode(identity=AgentIdentity.create("bob"), name="bob", weight=1) |
| 28 | carol = GovernanceNode(identity=AgentIdentity.create("carol"), name="carol", weight=1) |
| 29 | dave = GovernanceNode(identity=AgentIdentity.create("dave"), name="dave", weight=2) |
| 30 | eve = GovernanceNode(identity=AgentIdentity.create("eve"), name="eve", weight=1) |
| 31 | |
| 32 | registry = NodeRegistry() |
| 33 | for node in (alice, bob, carol, dave, eve): |
| 34 | registry.register(node) |
| 35 | |
| 36 | proposal = GovernanceProposal.create( |
| 37 | kind=ProposalKind.EMERGENCY_FREEZE, |
| 38 | subject_id="*", |
| 39 | title="EMERGENCY: halt all agent actions", |
| 40 | description="Suspected credential compromise; freeze all agents immediately.", |
| 41 | proposer_did=alice.did, |
| 42 | parameters={"severity": "critical", "reason": "suspected-compromise"}, |
| 43 | ttl_seconds=3600, |
| 44 | ) |
| 45 | alice.sign_proposal(proposal) |
| 46 | print(f"Proposal {proposal.proposal_id}") |
| 47 | print(f" title : {proposal.title}") |
| 48 | print(f" kind : {proposal.kind.value}") |
| 49 | |
| 50 | rnd = ConsensusRound(proposal=proposal, registry=registry, policy=QuorumPolicy()) |
| 51 | for voter in (alice, bob, carol, dave, eve): |
| 52 | signed = voter.cast_vote( |
| 53 | proposal, |
| 54 | VoteDecision.APPROVE, |
| 55 | rationale=f"{voter.name} approves emergency freeze", |
| 56 | ) |
| 57 | rnd.cast(signed) |
| 58 | print(f" {voter.name:5s} -> APPROVE (weight {voter.weight})") |
| 59 | |
| 60 | result = rnd.finalize(coordinator=alice) |
| 61 | print("\nConsensus:") |
| 62 | print(f" decision : {result.decision}") |
| 63 | print(f" reason : {result.reason}") |
| 64 | print(f" approve : {result.approve_weight}/{result.total_weight}") |
| 65 | |
| 66 | ok = ConsensusRound.verify_result(result) |
| 67 | print(f" signature: {'OK (ML-DSA verified)' if ok else 'INVALID'}") |
| 68 | assert ok, "result signature failed to verify" |
| 69 | |
| 70 | print("\n[FREEZE IN EFFECT] All agents must halt.") |
| 71 | print(f" signed-by : {result.signer_did[:22]}... ({result.algorithm})") |
| 72 | |
| 73 | |
| 74 | if __name__ == "__main__": |
| 75 | main() |
| 76 | |