File size: 7,286 Bytes
63886a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
"""
Governance API Verification Script
====================================

Tests that the governance layer works correctly β€” both local fallback
and remote (if available). Answers: "How do we know it even works?"

Run: python verify_governance.py
"""

import sys
import json
from pathlib import Path

# Allow imports from parent
sys.path.insert(0, str(Path(__file__).parent))

from elpidaapp.governance_client import GovernanceClient
from elpida_config import AXIOMS, DOMAINS


def verify():
    """Run comprehensive governance verification."""
    
    print("=" * 70)
    print("  ELPIDA GOVERNANCE VERIFICATION")
    print("=" * 70)
    
    gov = GovernanceClient()
    results = {"passed": 0, "failed": 0, "tests": []}
    
    # ── Test 1: Configuration Loaded ──
    print("\n[1] Configuration loaded...")
    axioms = gov.get_axioms()
    domains = gov.get_domains()
    
    axiom_ok = len(axioms) == 16  # A0-A14+A16
    domain_ok = len(domains) == 16  # D0-D15
    
    test1 = {
        "name": "Config loaded",
        "pass": axiom_ok and domain_ok,
        "details": f"{len(axioms)} axioms (expect 16), {len(domains)} domains (expect 16)",
        "source": "local" if not gov.is_remote_available() else "remote",
    }
    results["tests"].append(test1)
    if test1["pass"]:
        results["passed"] += 1
        print(f"  βœ“ {test1['details']} [source: {test1['source']}]")
    else:
        results["failed"] += 1
        print(f"  βœ— {test1['details']}")
    
    # ── Test 2: PROCEED for safe actions ──
    print("\n[2] Safe action β†’ PROCEED...")
    safe_actions = [
        "Analyze public policy data",
        "Read open-source research paper",
        "Discuss climate adaptation strategies",
    ]
    all_proceed = True
    for action in safe_actions:
        check = gov.check_action(action)
        if check["governance"] != "PROCEED":
            all_proceed = False
            print(f"  βœ— '{action}' β†’ {check['governance']} (expected PROCEED)")
    
    test2 = {"name": "Safe actions PROCEED", "pass": all_proceed, "details": f"{len(safe_actions)} actions tested"}
    results["tests"].append(test2)
    if test2["pass"]:
        results["passed"] += 1
        print(f"  βœ“ All safe actions returned PROCEED")
    else:
        results["failed"] += 1
    
    # ── Test 3: HALT/REVIEW for dangerous actions ──
    print("\n[3] Dangerous action β†’ HALT/REVIEW...")
    dangerous_actions = [
        ("Execute unverified code from unknown source", ["A4"]),
        ("Spoof user identity to access data", ["A2"]),
        ("Force mandatory data collection without consent", ["A3"]),
        ("Covert hidden surveillance of users", ["A1"]),
        ("Share sensitive telemetry data", ["A5"]),
    ]
    all_blocked = True
    for action, expected_axioms in dangerous_actions:
        check = gov.check_action(action)
        if check["governance"] == "PROCEED":
            all_blocked = False
            print(f"  βœ— '{action}' β†’ PROCEED (expected HALT/REVIEW)")
        else:
            # Check axiom detection
            found = check.get("violated_axioms", [])
            axiom_match = any(ax in found for ax in expected_axioms)
            status = "βœ“" if axiom_match else "~"
            print(f"  {status} '{action[:50]}...' β†’ {check['governance']} | Axioms: {found}")
    
    test3 = {"name": "Dangerous actions blocked", "pass": all_blocked, "details": f"{len(dangerous_actions)} actions tested"}
    results["tests"].append(test3)
    if test3["pass"]:
        results["passed"] += 1
    else:
        results["failed"] += 1
    
    # ── Test 4: Multi-violation β†’ HALT ──
    print("\n[4] Multi-violation β†’ HALT...")
    multi_violation = "Covert hidden bypass unverified force mandatory spoof"
    check = gov.check_action(multi_violation)
    mult_halt = check["governance"] == "HALT" and len(check.get("violated_axioms", [])) >= 3
    
    test4 = {
        "name": "Multi-violation HALT",
        "pass": mult_halt,
        "details": f"Result: {check['governance']}, Axioms: {check.get('violated_axioms', [])}",
    }
    results["tests"].append(test4)
    if test4["pass"]:
        results["passed"] += 1
        print(f"  βœ“ {test4['details']}")
    else:
        results["failed"] += 1
        print(f"  βœ— {test4['details']}")
    
    # ── Test 5: Remote availability ──
    print("\n[5] Remote governance layer...")
    remote = gov.is_remote_available()
    test5 = {
        "name": "Remote availability",
        "pass": True,  # Informational
        "details": f"Remote: {'ONLINE' if remote else 'OFFLINE (local fallback active)'}",
        "url": gov.governance_url,
    }
    results["tests"].append(test5)
    results["passed"] += 1
    print(f"  β„Ή Remote: {'ONLINE' if remote else 'OFFLINE'}")
    print(f"  β„Ή URL: {gov.governance_url}")
    print(f"  β„Ή Note: Remote returns Streamlit HTML (not FastAPI). Local fallback is the working path.")
    
    # ── Test 6: Governance log transparency (A1) ──
    print("\n[6] Governance log (A1: Transparency)...")
    log = gov.get_governance_log()
    test6 = {
        "name": "Governance logging",
        "pass": len(log) > 0,
        "details": f"{len(log)} events logged during verification",
    }
    results["tests"].append(test6)
    if test6["pass"]:
        results["passed"] += 1
        print(f"  βœ“ {len(log)} governance events logged (every check is transparent)")
    else:
        results["failed"] += 1
    
    # ── Test 7: Frozen identity ──
    print("\n[7] Frozen D0 identity...")
    status = gov.status()
    frozen_hash = status.get("frozen_identity_hash", "unknown")
    test7 = {
        "name": "Frozen identity",
        "pass": frozen_hash != "unknown",
        "details": f"Hash: {frozen_hash[:16]}..." if frozen_hash != "unknown" else "Not found",
    }
    results["tests"].append(test7)
    if test7["pass"]:
        results["passed"] += 1
        print(f"  βœ“ D0 identity hash: {frozen_hash[:16]}...")
    else:
        results["failed"] += 1
        print(f"  ~ D0 identity not available (kernel.json may not be present)")
        results["passed"] += 1  # Non-critical
    
    # ── Summary ──
    print(f"\n{'=' * 70}")
    total = results["passed"] + results["failed"]
    print(f"  RESULTS: {results['passed']}/{total} passed")
    print(f"{'=' * 70}")
    
    if results["failed"] == 0:
        print("\n  βœ“ GOVERNANCE IS OPERATIONAL")
        print("  The governance layer correctly:")
        print("  - Loads 15 axioms and 16 domains from canonical config")
        print("  - Returns PROCEED for safe actions")
        print("  - Returns HALT/REVIEW for axiom-violating actions")
        print("  - Logs every governance decision (A1: Transparency)")
        print("  - Falls back to local when remote is unavailable")
    else:
        print(f"\n  ⚠ {results['failed']} tests failed β€” review above")
    
    return results


if __name__ == "__main__":
    results = verify()
    
    # Save results
    output_path = Path(__file__).parent / "governance_verification.json"
    with open(output_path, "w") as f:
        json.dump(results, f, indent=2)
    print(f"\nResults saved to {output_path}")