|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test Approval Gates - Interactive Demo |
| 3 | +
|
| 4 | +This script creates approval requests and waits for you to approve/reject them |
| 5 | +via the dashboard UI at http://localhost:8000 |
| 6 | +
|
| 7 | +Usage: |
| 8 | + python scripts/test_approval_gates.py |
| 9 | +""" |
| 10 | +import time |
| 11 | +from datetime import datetime |
| 12 | + |
| 13 | +from empathy_os.telemetry import ApprovalGate |
| 14 | + |
| 15 | + |
| 16 | +def create_test_approvals(): |
| 17 | + """Create several test approval requests for demonstration.""" |
| 18 | + gate = ApprovalGate(agent_id="demo-workflow") |
| 19 | + |
| 20 | + print("=" * 60) |
| 21 | + print("🚦 APPROVAL GATES DEMO") |
| 22 | + print("=" * 60) |
| 23 | + print("\nThis demo will create 3 approval requests.") |
| 24 | + print("Open the dashboard at: http://localhost:8000") |
| 25 | + print("\nYou can approve or reject each request in the dashboard UI.") |
| 26 | + print("=" * 60) |
| 27 | + |
| 28 | + # Approval 1: Deploy to Production |
| 29 | + print("\n📋 Request 1: Deploy to Production") |
| 30 | + print(" Creating approval request...") |
| 31 | + |
| 32 | + response = gate.request_approval( |
| 33 | + approval_type="deploy_to_production", |
| 34 | + context={ |
| 35 | + "version": "5.0.0", |
| 36 | + "changes": 60, |
| 37 | + "risk": "medium", |
| 38 | + "timestamp": datetime.utcnow().isoformat(), |
| 39 | + }, |
| 40 | + timeout=60.0, # Wait 60 seconds |
| 41 | + ) |
| 42 | + |
| 43 | + print(f" ✅ Response received!") |
| 44 | + print(f" Approved: {response.approved}") |
| 45 | + print(f" Responder: {response.responder}") |
| 46 | + print(f" Reason: {response.reason}") |
| 47 | + |
| 48 | + if not response.approved: |
| 49 | + print("\n❌ Deployment blocked by human review") |
| 50 | + return |
| 51 | + |
| 52 | + # Approval 2: Delete Resources |
| 53 | + print("\n📋 Request 2: Delete Resources") |
| 54 | + print(" Creating approval request...") |
| 55 | + |
| 56 | + response = gate.request_approval( |
| 57 | + approval_type="delete_resources", |
| 58 | + context={ |
| 59 | + "resource_type": "database_records", |
| 60 | + "count": 1000, |
| 61 | + "impact": "high", |
| 62 | + }, |
| 63 | + timeout=60.0, |
| 64 | + ) |
| 65 | + |
| 66 | + print(f" ✅ Response received!") |
| 67 | + print(f" Approved: {response.approved}") |
| 68 | + print(f" Responder: {response.responder}") |
| 69 | + print(f" Reason: {response.reason}") |
| 70 | + |
| 71 | + # Approval 3: Refactor Code |
| 72 | + print("\n📋 Request 3: Refactor Code") |
| 73 | + print(" Creating approval request...") |
| 74 | + |
| 75 | + response = gate.request_approval( |
| 76 | + approval_type="refactor_code", |
| 77 | + context={ |
| 78 | + "files": 50, |
| 79 | + "lines_changed": 5000, |
| 80 | + "risk": "low", |
| 81 | + }, |
| 82 | + timeout=60.0, |
| 83 | + ) |
| 84 | + |
| 85 | + print(f" ✅ Response received!") |
| 86 | + print(f" Approved: {response.approved}") |
| 87 | + print(f" Responder: {response.responder}") |
| 88 | + print(f" Reason: {response.reason}") |
| 89 | + |
| 90 | + print("\n" + "=" * 60) |
| 91 | + print("✅ DEMO COMPLETE") |
| 92 | + print("=" * 60) |
| 93 | + |
| 94 | + |
| 95 | +def create_background_approval(): |
| 96 | + """Create a single approval request that stays pending.""" |
| 97 | + gate = ApprovalGate(agent_id="background-agent") |
| 98 | + |
| 99 | + print("\n🔄 Creating a persistent approval request...") |
| 100 | + print("This will stay in your dashboard until you approve/reject it.") |
| 101 | + |
| 102 | + gate.request_approval( |
| 103 | + approval_type="run_expensive_operation", |
| 104 | + context={ |
| 105 | + "operation": "Train ML model", |
| 106 | + "estimated_cost": "$50", |
| 107 | + "estimated_time": "2 hours", |
| 108 | + }, |
| 109 | + timeout=600.0, # 10 minutes |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + import sys |
| 115 | + |
| 116 | + if len(sys.argv) > 1 and sys.argv[1] == "--background": |
| 117 | + # Create one that stays pending |
| 118 | + create_background_approval() |
| 119 | + else: |
| 120 | + # Interactive demo |
| 121 | + create_test_approvals() |
0 commit comments