File size: 36,532 Bytes
98f3108
4b27602
 
8773365
e93eabb
4b27602
1737ba1
9dc97a5
4b27602
1737ba1
 
 
8773365
 
 
5b9eca0
 
d9d84fd
8c057f3
99c36b7
1737ba1
a6671a5
 
 
 
 
 
 
 
 
 
1737ba1
 
8773365
1737ba1
 
 
 
 
 
8773365
 
 
 
 
 
 
 
 
 
 
 
 
 
41c3ec6
 
8773365
1737ba1
8773365
1737ba1
8773365
 
 
 
 
 
 
 
 
 
 
 
d9d84fd
1737ba1
 
8773365
1737ba1
 
8773365
 
 
1737ba1
 
 
 
 
 
 
 
 
 
 
8c057f3
 
 
 
 
1737ba1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9d84fd
1737ba1
 
 
 
 
 
 
 
 
 
 
 
8773365
3854e5f
8773365
a6671a5
3854e5f
 
 
 
a6671a5
3854e5f
 
 
 
8773365
1737ba1
d9d84fd
1737ba1
 
 
 
8773365
1737ba1
 
 
 
 
 
 
 
 
 
a6671a5
3854e5f
 
1737ba1
 
 
 
 
 
 
 
 
 
8c057f3
 
1737ba1
 
d9d84fd
41c3ec6
4b27602
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1737ba1
8773365
1737ba1
 
 
 
 
 
 
 
8c057f3
 
 
 
a3ed1dc
1080e05
d9d84fd
1080e05
3b9c14e
8c057f3
3b9c14e
 
8c057f3
d9d84fd
8c057f3
 
3b9c14e
 
8c057f3
 
 
 
 
 
 
 
3b9c14e
 
 
 
 
4b27602
 
 
6b1b475
1080e05
d9d84fd
1080e05
3b9c14e
5b9eca0
 
3b9c14e
 
 
 
 
 
 
 
 
 
d9d84fd
3b9c14e
1737ba1
8773365
1737ba1
8773365
 
 
d9d84fd
8773365
1737ba1
 
 
 
 
 
 
 
8c057f3
 
1737ba1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c057f3
 
1737ba1
 
 
 
 
 
 
 
 
 
 
 
a6671a5
3854e5f
1737ba1
 
 
 
4b27602
3b9c14e
 
8c057f3
6b1b475
 
3b9c14e
 
 
8c057f3
6b1b475
3b9c14e
6b1b475
 
1080e05
d9d84fd
3b9c14e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1737ba1
 
 
 
a6671a5
d9d84fd
1737ba1
d9d84fd
1737ba1
 
 
d9d84fd
1737ba1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2deb84
1080e05
d9d84fd
1080e05
8773365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9d84fd
8773365
 
a3ed1dc
1737ba1
 
 
 
a3ed1dc
 
 
 
1080e05
 
 
 
1737ba1
 
 
1080e05
 
a3ed1dc
 
8773365
a3ed1dc
1737ba1
 
 
 
 
a3ed1dc
 
 
 
8773365
a3ed1dc
1737ba1
 
 
 
 
a3ed1dc
 
 
 
 
5b9eca0
 
 
 
 
 
 
 
 
 
 
 
 
 
66ca8ba
434be37
 
 
 
 
 
 
 
 
 
 
66ca8ba
 
 
 
5b9eca0
 
66ca8ba
 
5b9eca0
 
 
 
 
 
 
 
 
d9d84fd
 
 
 
 
 
 
5b9eca0
d9d84fd
 
 
 
 
 
 
 
 
5b9eca0
 
 
 
 
 
 
 
 
 
 
d9d84fd
5b9eca0
 
 
 
 
 
 
 
 
 
 
 
 
 
d9d84fd
 
 
 
5b9eca0
 
d9d84fd
5b9eca0
 
d9d84fd
5b9eca0
 
 
 
 
 
 
 
 
 
 
9dc97a5
d9d84fd
1080e05
3b9c14e
 
8773365
3b9c14e
 
 
8773365
 
 
 
 
 
 
 
3b9c14e
1080e05
8773365
 
 
 
 
 
 
 
 
 
 
 
 
1737ba1
 
 
 
 
 
 
 
 
 
8773365
5b9eca0
8773365
 
1737ba1
 
4b27602
1080e05
 
11c85bb
5b9eca0
1737ba1
8773365
3b9c14e
 
 
1737ba1
3b9c14e
 
4b27602
6b1b475
b2f7452
11c85bb
d9d84fd
6b1b475
3b9c14e
1e5a278
 
6b1b475
1080e05
 
 
3b9c14e
1737ba1
 
6b1b475
1e5a278
6b1b475
 
 
 
 
 
a3ed1dc
 
 
 
 
5b9eca0
 
 
 
d9d84fd
5b9eca0
 
 
 
d9d84fd
5b9eca0
 
66ca8ba
5b9eca0
a3ed1dc
5b9eca0
6b1b475
d9d84fd
79db0ae
3b9c14e
8b3c90f
79db0ae
 
 
 
 
5b9eca0
 
79db0ae
8c057f3
 
 
5b9eca0
8c057f3
 
 
 
5b9eca0
 
 
79db0ae
3b9c14e
5b9eca0
 
d9d84fd
 
5b9eca0
 
 
 
 
 
 
 
 
 
 
3b9c14e
5b9eca0
3b9c14e
 
a3ed1dc
 
3b9c14e
 
 
a3ed1dc
 
 
 
3b9c14e
5b9eca0
 
 
 
 
a3ed1dc
d9d84fd
6b1b475
3b9c14e
5b9eca0
 
 
 
 
3b9c14e
1737ba1
 
 
3b9c14e
5b9eca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1080e05
d9d84fd
1080e05
 
8773365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b9eca0
8773365
1e5a278
5b9eca0
d9d84fd
5b9eca0
d9d84fd
5b9eca0
 
b2f7452
a6671a5
 
 
aa09497
1737ba1
8c057f3
a6671a5
1737ba1
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
import gradio as gr
import json
import logging
import logging.handlers
import numpy as np
from datetime import datetime
from typing import Dict, Any, List, Optional, Tuple
import threading
import time
import os
import sqlite3
import contextlib
import signal
import sys
import functools
import csv
import io
import tempfile
from collections import deque
from scipy.stats import beta
import plotly.graph_objects as go

# ----------------------------------------------------------------------
# Prometheus metrics (optional)
# ----------------------------------------------------------------------
try:
    from prometheus_client import Counter, Histogram, Gauge, generate_latest, CONTENT_TYPE_LATEST
    PROMETHEUS_AVAILABLE = True
except ImportError:
    PROMETHEUS_AVAILABLE = False
    logging.warning("prometheus-client not installed, metrics endpoint disabled")

# ----------------------------------------------------------------------
# Configuration from environment variables with validation
# ----------------------------------------------------------------------
LOW_THRESHOLD = float(os.getenv("ARF_LOW_THRESHOLD", "0.2"))
HIGH_THRESHOLD = float(os.getenv("ARF_HIGH_THRESHOLD", "0.8"))
ALPHA_PRIOR = float(os.getenv("ARF_ALPHA_PRIOR", "1.0"))
BETA_PRIOR = float(os.getenv("ARF_BETA_PRIOR", "1.0"))
DB_PATH = os.getenv("ARF_DB_PATH", "/data/arf_decisions.db")
LOG_LEVEL = os.getenv("ARF_LOG_LEVEL", "INFO").upper()
VERSION = "4.2.0+oss-enhanced"

# Validate thresholds
if not (0 <= LOW_THRESHOLD < HIGH_THRESHOLD <= 1):
    logging.warning(f"Invalid thresholds: low={LOW_THRESHOLD}, high={HIGH_THRESHOLD}. Using defaults.")
    LOW_THRESHOLD = 0.2
    HIGH_THRESHOLD = 0.8

# Validate priors
if ALPHA_PRIOR <= 0 or BETA_PRIOR <= 0:
    logging.warning(f"Invalid priors: alpha={ALPHA_PRIOR}, beta={BETA_PRIOR}. Using defaults.")
    ALPHA_PRIOR = 1.0
    BETA_PRIOR = 1.0

# ----------------------------------------------------------------------
# Logging setup (file rotation + console)
# ----------------------------------------------------------------------
os.makedirs("/var/log/arf", exist_ok=True)
logger = logging.getLogger(__name__)
logger.setLevel(getattr(logging, LOG_LEVEL, logging.INFO))

file_handler = logging.handlers.RotatingFileHandler(
    "/var/log/arf/app.log", maxBytes=10_485_760, backupCount=5
)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))

console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))

logger.addHandler(file_handler)
logger.addHandler(console_handler)
logger.propagate = False

# ----------------------------------------------------------------------
# SQLite persistence with secure permissions
# ----------------------------------------------------------------------
def init_db():
    db_dir = os.path.dirname(DB_PATH)
    if db_dir and not os.path.exists(db_dir):
        os.makedirs(db_dir, exist_ok=True)
    with contextlib.closing(sqlite3.connect(DB_PATH)) as conn:
        cursor = conn.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS decisions (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                timestamp TEXT NOT NULL,
                decision_json TEXT NOT NULL,
                risk REAL NOT NULL
            )
        ''')
        conn.commit()
    try:
        os.chmod(DB_PATH, 0o600)
    except Exception as e:
        logger.warning(f"Could not set secure permissions on DB: {e}")
    logger.info(f"Database initialized at {DB_PATH}")

def save_decision_to_db(decision: dict, risk: float):
    try:
        with contextlib.closing(sqlite3.connect(DB_PATH)) as conn:
            cursor = conn.cursor()
            cursor.execute(
                "INSERT INTO decisions (timestamp, decision_json, risk) VALUES (?, ?, ?)",
                (decision["timestamp"], json.dumps(decision), risk)
            )
            conn.commit()
    except Exception as e:
        logger.error(f"Failed to save decision to DB: {e}")

def load_recent_decisions(limit: int = 100) -> List[Tuple[str, dict, float]]:
    decisions = []
    try:
        with contextlib.closing(sqlite3.connect(DB_PATH)) as conn:
            cursor = conn.cursor()
            cursor.execute(
                "SELECT timestamp, decision_json, risk FROM decisions ORDER BY timestamp DESC LIMIT ?",
                (limit,)
            )
            rows = cursor.fetchall()
            for ts, json_str, risk in rows:
                decisions.append((ts, json.loads(json_str), risk))
        decisions.reverse()
    except Exception as e:
        logger.error(f"Failed to load decisions from DB: {e}")
    return decisions

def vacuum_db():
    try:
        with contextlib.closing(sqlite3.connect(DB_PATH)) as conn:
            conn.execute("VACUUM")
        logger.info("Database vacuumed")
    except Exception as e:
        logger.error(f"Vacuum failed: {e}")

# ----------------------------------------------------------------------
# Prometheus metrics (renamed to avoid conflict with Gradio components)
# ----------------------------------------------------------------------
if PROMETHEUS_AVAILABLE:
    prom_decisions_total = Counter('arf_decisions_total', 'Total decisions made', ['action'])
    prom_risk_gauge = Gauge('arf_current_risk', 'Current risk score')
    prom_decision_latency = Histogram('arf_decision_latency_seconds', 'Time to evaluate intent')
    prom_mcmc_runs = Counter('arf_mcmc_runs_total', 'Total MCMC runs')
else:
    prom_decisions_total = None
    prom_risk_gauge = None
    prom_decision_latency = None
    prom_mcmc_runs = None

# ----------------------------------------------------------------------
# Thread‑safe history
# ----------------------------------------------------------------------
decision_history = []
risk_history = []
history_lock = threading.Lock()
shutdown_event = threading.Event()

def update_dashboard_data(decision: dict, risk: float):
    with history_lock:
        decision_history.append((datetime.utcnow().isoformat(), decision, risk))
        risk_history.append((datetime.utcnow().isoformat(), risk))
        if len(decision_history) > 100:
            decision_history.pop(0)
        if len(risk_history) > 100:
            risk_history.pop(0)
    save_decision_to_db(decision, risk)
    if PROMETHEUS_AVAILABLE:
        prom_decisions_total.labels(action=decision.get("risk_level", "unknown")).inc()
        prom_risk_gauge.set(risk)

def refresh_history_from_db():
    global decision_history, risk_history
    decisions = load_recent_decisions(100)
    with history_lock:
        decision_history.clear()
        risk_history.clear()
        for ts, dec, risk in decisions:
            decision_history.append((ts, dec, risk))
            risk_history.append((ts, risk))
    if PROMETHEUS_AVAILABLE and risk_history:
        prom_risk_gauge.set(risk_history[-1][1])

# ----------------------------------------------------------------------
# Memory monitoring
# ----------------------------------------------------------------------
def get_memory_usage():
    try:
        import resource
        rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
        if rss < 1e9:
            return rss / 1024.0
        else:
            return rss / (1024.0 * 1024.0)
    except ImportError:
        try:
            with open("/proc/self/status") as f:
                for line in f:
                    if line.startswith("VmRSS:"):
                        parts = line.split()
                        if len(parts) >= 2:
                            return int(parts[1]) / 1024.0
        except Exception:
            pass
    return None

def memory_monitor_loop():
    while not shutdown_event.is_set():
        try:
            mem_mb = get_memory_usage()
            if mem_mb is not None:
                logger.info(f"Process memory: {mem_mb:.1f} MB")
            else:
                logger.info("Process memory: unknown")
        except Exception as e:
            logger.error(f"Memory logging error: {e}")
        for _ in range(60):
            if shutdown_event.is_set():
                break
            time.sleep(1)

# ----------------------------------------------------------------------
# Bayesian Risk Engine
# ----------------------------------------------------------------------
class BayesianRiskEngine:
    def __init__(self, alpha=ALPHA_PRIOR, beta=BETA_PRIOR, maxlen=None):
        self.alpha = alpha
        self.beta = beta
        self.maxlen = maxlen
        self.events = deque(maxlen=maxlen)
        self.total_failures = 0
        self.total_successes = 0

    def update(self, failures, successes):
        self.events.append((failures, successes))
        self.total_failures += failures
        self.total_successes += successes
        if self.maxlen is not None and len(self.events) == self.maxlen:
            self.total_failures = sum(f for f, _ in self.events)
            self.total_successes = sum(s for _, s in self.events)
        self.alpha = ALPHA_PRIOR + self.total_failures
        self.beta = BETA_PRIOR + self.total_successes

    def risk(self):
        return self.alpha / (self.alpha + self.beta)

    def risk_interval(self, prob=0.95):
        lo = beta.ppf((1 - prob) / 2, self.alpha, self.beta)
        hi = beta.ppf((1 + prob) / 2, self.alpha, self.beta)
        return lo, hi

# ----------------------------------------------------------------------
# Policy Engine
# ----------------------------------------------------------------------
class PolicyEngine:
    def __init__(self):
        self.thresholds = {"low": LOW_THRESHOLD, "high": HIGH_THRESHOLD}

    def evaluate(self, risk):
        if risk < self.thresholds["low"]:
            return "approve", "Risk within safe limits"
        elif risk > self.thresholds["high"]:
            return "deny", f"Risk exceeds high threshold ({self.thresholds['high']})"
        else:
            return "escalate", f"Risk in escalation zone ({self.thresholds['low']}-{self.thresholds['high']})"

# ----------------------------------------------------------------------
# Infrastructure analysis
# ----------------------------------------------------------------------
def handle_infra_with_governance(fault_type: str, context_window: int, session_state: dict):
    start_time = time.time()
    try:
        fault_type = fault_type.strip()
        if fault_type not in ["none", "switch_down", "server_overload", "cascade"]:
            fault_type = "none"
        context_window = max(0, min(1000, int(context_window)))

        fault_map = {
            "none": (1, 99),
            "switch_down": (20, 80),
            "server_overload": (35, 65),
            "cascade": (60, 40)
        }
        failures, successes = fault_map.get(fault_type, (1, 99))

        maxlen = context_window if context_window > 0 else None
        risk_engine = BayesianRiskEngine(maxlen=maxlen)
        risk_engine.update(failures, successes)
        risk = risk_engine.risk()
        ci_low, ci_high = risk_engine.risk_interval(0.95)

        policy_engine = PolicyEngine()
        action, reason = policy_engine.evaluate(risk)
        control_decision = autonomous_control_decision(risk, risk_engine, policy_engine)

        analysis_result = {
            "risk": risk,
            "risk_ci": [ci_low, ci_high],
            "decision": action,
            "justification": reason,
            "healing_actions": ["restart"] if action == "deny" else ["monitor"],
            "posterior_parameters": {
                "alpha": risk_engine.alpha,
                "beta": risk_engine.beta
            },
            "context_window": context_window
        }
        output = {
            **analysis_result,
            "governance": {
                "policy_evaluation": {
                    "action": action,
                    "reason": reason,
                    "thresholds": policy_engine.thresholds
                },
                "control_plane_decision": control_decision
            }
        }
        if PROMETHEUS_AVAILABLE:
            prom_decision_latency.observe(time.time() - start_time)
        return output, session_state
    except Exception as e:
        logger.exception("Error in handle_infra_with_governance")
        return {"error": str(e)}, session_state

def autonomous_control_decision(risk, risk_engine, policy_engine):
    action, reason = policy_engine.evaluate(risk)
    risk_level = "low" if risk < LOW_THRESHOLD else "medium" if risk < HIGH_THRESHOLD else "high"
    decision = {
        "timestamp": datetime.utcnow().isoformat(),
        "approved": action == "approve",
        "actions": ["escalate_human"] if action == "escalate" else [],
        "reason": reason,
        "risk_level": risk_level
    }
    update_dashboard_data(decision, risk)
    return decision

# ----------------------------------------------------------------------
# MCMC
# ----------------------------------------------------------------------
class MHMCMC:
    def __init__(self, log_target, proposal_sd=0.1):
        self.log_target = log_target
        self.proposal_sd = proposal_sd

    def sample(self, n_samples, initial_state, burn_in=0):
        samples = np.zeros((n_samples, len(initial_state)))
        current = np.array(initial_state)
        current_log = self.log_target(current)
        accepted = 0
        for i in range(n_samples + burn_in):
            proposal = current + np.random.normal(0, self.proposal_sd, size=len(current))
            proposal_log = self.log_target(proposal)
            accept_prob = min(1, np.exp(proposal_log - current_log))
            if np.random.rand() < accept_prob:
                current = proposal
                current_log = proposal_log
                accepted += 1
            if i >= burn_in:
                samples[i - burn_in] = current
        acceptance_rate = accepted / (n_samples + burn_in)
        return samples, acceptance_rate

def run_hmc_mcmc(samples: int, warmup: int):
    try:
        samples = max(500, min(10000, int(samples)))
        warmup = max(100, min(2000, int(warmup)))
        if PROMETHEUS_AVAILABLE:
            prom_mcmc_runs.inc()

        np.random.seed(42)
        data = np.random.normal(0.5, 0.2, 10)

        def log_prior(mu):
            return -0.5 * (mu ** 2)

        def log_likelihood(mu):
            return -0.5 * np.sum(((data - mu) / 0.2) ** 2)

        def log_posterior(mu):
            return log_prior(mu) + log_likelihood(mu)

        sampler = MHMCMC(log_posterior, proposal_sd=0.05)
        mu_samples, acceptance = sampler.sample(samples, initial_state=[0.0], burn_in=warmup)
        mu_samples = mu_samples.flatten()

        mean = np.mean(mu_samples)
        median = np.median(mu_samples)
        credible_interval = np.percentile(mu_samples, [2.5, 97.5])

        fig_trace = go.Figure()
        fig_trace.add_trace(go.Scatter(y=mu_samples, mode='lines', name='μ', line=dict(width=1)))
        fig_trace.update_layout(title="Trace of μ (Metropolis-Hastings)", xaxis_title="Iteration", yaxis_title="μ")

        fig_hist = go.Figure()
        fig_hist.add_trace(go.Histogram(x=mu_samples, nbinsx=50, name='Posterior'))
        fig_hist.update_layout(title="Posterior Distribution of μ", xaxis_title="μ", yaxis_title="Density")

        summary = {
            "mean": mean,
            "median": median,
            "credible_interval_95": f"[{credible_interval[0]:.3f}, {credible_interval[1]:.3f}]",
            "acceptance_rate": f"{acceptance:.2%}"
        }
        return summary, fig_trace, fig_hist
    except Exception as e:
        logger.exception("MCMC computation failed")
        return {"error": str(e)}, go.Figure(), go.Figure()

# ----------------------------------------------------------------------
# Dashboard plots
# ----------------------------------------------------------------------
class TTLCache:
    def __init__(self, ttl_seconds=5):
        self.ttl = ttl_seconds
        self.cache = {}
        self.lock = threading.Lock()

    def __call__(self, func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            key = (func.__name__, args, frozenset(kwargs.items()))
            now = time.time()
            with self.lock:
                if key in self.cache:
                    result, timestamp = self.cache[key]
                    if now - timestamp < self.ttl:
                        return result
            result = func(*args, **kwargs)
            with self.lock:
                self.cache[key] = (result, now)
            return result
        return wrapper

dashboard_cache = TTLCache(ttl_seconds=2)

@dashboard_cache
def generate_risk_gauge():
    with history_lock:
        if not risk_history:
            return go.Figure()
        latest_risk = risk_history[-1][1]
    fig = go.Figure(go.Indicator(
        mode="gauge+number",
        value=latest_risk,
        title={'text': "Current Risk"},
        gauge={
            'axis': {'range': [0, 1]},
            'bar': {'color': "darkblue"},
            'steps': [
                {'range': [0, LOW_THRESHOLD], 'color': "lightgreen"},
                {'range': [LOW_THRESHOLD, HIGH_THRESHOLD], 'color': "yellow"},
                {'range': [HIGH_THRESHOLD, 1], 'color': "red"}
            ]
        }))
    return fig

@dashboard_cache
def generate_decision_pie():
    with history_lock:
        if not decision_history:
            return go.Figure()
        approved = sum(1 for _, d, _ in decision_history if d.get("approved", False))
        blocked = len(decision_history) - approved
    fig = go.Figure(data=[go.Pie(labels=["Approved", "Blocked"], values=[approved, blocked])])
    fig.update_layout(title="Policy Decisions")
    return fig

@dashboard_cache
def generate_action_timeline():
    with history_lock:
        if not decision_history:
            return go.Figure()
        times = [d["timestamp"] for _, d, _ in decision_history]
        approvals = [1 if d.get("approved", False) else 0 for _, d, _ in decision_history]
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=times, y=approvals, mode='markers+lines', name='Approvals'))
    fig.update_layout(title="Autonomous Actions Timeline", xaxis_title="Time", yaxis_title="Approved (1) / Blocked (0)")
    return fig

@dashboard_cache
def generate_risk_trend():
    with history_lock:
        if not risk_history:
            return go.Figure()
        times = [ts for ts, _ in risk_history]
        risks = [r for _, r in risk_history]
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=times, y=risks, mode='lines+markers', name='Risk', line=dict(color='red', width=2)))
    fig.add_hline(y=LOW_THRESHOLD, line_dash="dash", line_color="green", annotation_text=f"Low ({LOW_THRESHOLD})")
    fig.add_hline(y=HIGH_THRESHOLD, line_dash="dash", line_color="orange", annotation_text=f"High ({HIGH_THRESHOLD})")
    fig.update_layout(title="Risk Trend", xaxis_title="Time", yaxis_title="Risk Score", yaxis_range=[0, 1])
    return fig

def refresh_dashboard():
    with history_lock:
        total = len(decision_history)
        approved = sum(1 for _, d, _ in decision_history if d.get("approved", False))
        blocked = total - approved
        avg_risk = np.mean([r for _, r in risk_history]) if risk_history else 0.5
        control_stats = {
            "total_decisions": total,
            "approved_actions": approved,
            "blocked_actions": blocked,
            "average_risk": float(avg_risk)
        }
    return (
        control_stats,
        generate_risk_gauge(),
        generate_decision_pie(),
        generate_action_timeline(),
        generate_risk_trend()
    )

# ----------------------------------------------------------------------
# Batch simulation
# ----------------------------------------------------------------------
def run_batch_simulation(context_window: int):
    fault_types = ["none", "switch_down", "server_overload", "cascade"]
    results = []
    for fault in fault_types:
        output, _ = handle_infra_with_governance(fault, context_window, {})
        if "error" in output:
            results.append([
                fault,
                "Error",
                output["error"],
                "N/A",
                "N/A"
            ])
        else:
            results.append([
                fault,
                f"{output['risk']:.4f}",
                output["decision"],
                output["governance"]["control_plane_decision"]["risk_level"],
                f"[{output['risk_ci'][0]:.3f}, {output['risk_ci'][1]:.3f}]"
            ])
    # Return as list of lists for gr.Dataframe
    return results

# ----------------------------------------------------------------------
# Data export
# ----------------------------------------------------------------------
def export_history_to_csv():
    try:
        with contextlib.closing(sqlite3.connect(DB_PATH)) as conn:
            cursor = conn.cursor()
            cursor.execute("SELECT timestamp, decision_json, risk FROM decisions ORDER BY timestamp")
            rows = cursor.fetchall()
        if not rows:
            return None
        output = io.StringIO()
        writer = csv.writer(output)
        writer.writerow(["Timestamp", "Decision", "Risk", "Approved", "Risk Level", "Reason"])
        for ts, json_str, risk in rows:
            dec = json.loads(json_str)
            writer.writerow([
                ts,
                json_str,
                risk,
                dec.get("approved", False),
                dec.get("risk_level", ""),
                dec.get("reason", "")
            ])
        output.seek(0)
        # Write to a temporary file
        with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
            f.write(output.getvalue())
            return f.name
    except Exception as e:
        logger.error(f"Export failed: {e}")
        return None

# ----------------------------------------------------------------------
# Update thresholds
# ----------------------------------------------------------------------
def update_thresholds(low: float, high: float):
    global LOW_THRESHOLD, HIGH_THRESHOLD
    if 0 <= low < high <= 1:
        LOW_THRESHOLD = low
        HIGH_THRESHOLD = high
        logger.info(f"Updated thresholds: low={low}, high={high}")
        return f"Thresholds updated: approve < {low}, escalate {low}-{high}, deny > {high}"
    else:
        return f"Invalid thresholds: low={low}, high={high}. Must satisfy 0 ≤ low < high ≤ 1."

# ----------------------------------------------------------------------
# OSS capabilities
# ----------------------------------------------------------------------
oss_caps = {
    "edition": "OSS (Demo)",
    "version": VERSION,
    "license": "Apache 2.0",
    "execution": {"modes": ["advisory"], "max_incidents": 100},
    "memory": {"type": "in-memory", "faiss_index_type": "flat", "max_incident_nodes": 100},
    "enterprise_features": [
        "Real-time HMC (using PyMC)",
        "Hyperpriors",
        "Decision Engine",
        "Full audit trails & compliance reporting",
        "Blast radius limits & automatic rollback",
        "Multi-cloud & hybrid deployment support"
    ]
}

# ----------------------------------------------------------------------
# Graceful shutdown
# ----------------------------------------------------------------------
def shutdown_handler(signum, frame):
    logger.info("Received shutdown signal, cleaning up...")
    shutdown_event.set()
    time.sleep(2)
    logger.info("Shutdown complete")
    sys.exit(0)

signal.signal(signal.SIGTERM, shutdown_handler)
signal.signal(signal.SIGINT, shutdown_handler)

# ----------------------------------------------------------------------
# Startup
# ----------------------------------------------------------------------
init_db()
refresh_history_from_db()

mem_thread = threading.Thread(target=memory_monitor_loop, daemon=True)
mem_thread.start()

def vacuum_scheduler():
    while not shutdown_event.is_set():
        time.sleep(86400)
        if not shutdown_event.is_set():
            vacuum_db()
vacuum_thread = threading.Thread(target=vacuum_scheduler, daemon=True)
vacuum_thread.start()

# ----------------------------------------------------------------------
# Gradio UI
# ----------------------------------------------------------------------
with gr.Blocks(title=f"ARF v{VERSION} – Bayesian Risk Scoring Demo", theme=gr.themes.Soft()) as demo:
    gr.Markdown(f"""
    # 🧠 ARF v{VERSION} – Bayesian Risk Scoring for AI Reliability (Demo)
    **Mathematically rigorous risk estimation using conjugate priors and MCMC**
    This demo showcases:
    - **Bayesian conjugate prior (Beta-Binomial)** – online risk update from observed failures/successes.
    - **Policy thresholds** – approve (<{LOW_THRESHOLD}), escalate ({LOW_THRESHOLD}{HIGH_THRESHOLD}), deny (>{HIGH_THRESHOLD}).
    - **Metropolis-Hastings MCMC** – sampling from a posterior distribution (simulating HMC concepts).
    - **Autonomous control decisions** – based on the current risk estimate.
    All components are implemented with only `numpy`, `scipy`, and standard libraries.
    """)

    with gr.Tabs():
        # Control Plane Dashboard
        with gr.TabItem("Control Plane Dashboard"):
            gr.Markdown("### 🎮 Control Plane")
            with gr.Row():
                with gr.Column():
                    system_status = gr.JSON(label="System Status", value={
                        "edition": oss_caps["edition"],
                        "version": oss_caps["version"],
                        "governance_mode": "advisory",
                        "policies_loaded": 2,
                        "risk_threshold_low": LOW_THRESHOLD,
                        "risk_threshold_high": HIGH_THRESHOLD
                    })
                with gr.Column():
                    control_stats = gr.JSON(label="Control Statistics", value={
                        "total_decisions": 0,
                        "approved_actions": 0,
                        "blocked_actions": 0,
                        "average_risk": 0.5
                    })
            with gr.Row():
                risk_gauge = gr.Plot(label="Current Risk Gauge")
                decision_pie = gr.Plot(label="Policy Decisions")
            with gr.Row():
                action_timeline = gr.Plot(label="Autonomous Actions Timeline")
                risk_trend = gr.Plot(label="Risk Trend")
            with gr.Row():
                auto_refresh = gr.Checkbox(label="Auto-refresh (3s)", value=False)
                refresh_btn = gr.Button("Refresh Now")
            timer = gr.Timer(value=3, active=False)
            def refresh_if_enabled(auto):
                if auto:
                    return refresh_dashboard()
                else:
                    return [gr.update() for _ in range(5)]
            timer.tick(refresh_if_enabled, inputs=[auto_refresh], outputs=[control_stats, risk_gauge, decision_pie, action_timeline, risk_trend])
            refresh_btn.click(
                fn=refresh_dashboard,
                outputs=[control_stats, risk_gauge, decision_pie, action_timeline, risk_trend]
            )
            auto_refresh.change(lambda v: gr.Timer(active=v), inputs=[auto_refresh], outputs=[timer])

        # Infrastructure Reliability
        with gr.TabItem("Infrastructure Reliability"):
            gr.Markdown("### 🏗️ Infrastructure Intent Evaluation with Bayesian Risk")
            infra_state = gr.State(value={})
            with gr.Row():
                with gr.Column():
                    infra_fault = gr.Dropdown(
                        ["none", "switch_down", "server_overload", "cascade"],
                        value="none",
                        label="Inject Fault",
                        info="Select a fault type to simulate infrastructure issues."
                    )
                    context_window_input = gr.Number(
                        value=50,
                        label="Context Window (number of recent events)",
                        minimum=0,
                        maximum=1000,
                        step=1,
                        info="How many past incidents to consider for risk calculation (0 = unlimited)"
                    )
                    with gr.Row():
                        infra_btn = gr.Button("Evaluate Intent")
                        batch_btn = gr.Button("Run Batch Simulation", variant="secondary")
                with gr.Column():
                    infra_output = gr.JSON(label="Analysis Result")
            batch_results = gr.Dataframe(
                headers=["Fault Type", "Risk", "Decision", "Risk Level", "Confidence Interval"],
                label="Batch Simulation Results",
                datatype=["str", "str", "str", "str", "str"]
            )
            infra_btn.click(
                fn=handle_infra_with_governance,
                inputs=[infra_fault, context_window_input, infra_state],
                outputs=[infra_output, infra_state]
            )
            batch_btn.click(
                fn=run_batch_simulation,
                inputs=[context_window_input],
                outputs=[batch_results]
            )

        # Deep Analysis (MCMC)
        with gr.TabItem("Deep Analysis (MCMC)"):
            gr.Markdown("### Markov Chain Monte Carlo (Metropolis‑Hastings)")
            with gr.Row():
                with gr.Column():
                    hmc_samples = gr.Slider(500, 10000, value=5000, step=500, label="Number of Samples")
                    hmc_warmup = gr.Slider(100, 2000, value=1000, step=100, label="Burn‑in Steps")
                    hmc_run_btn = gr.Button("Run MCMC")
                with gr.Column():
                    hmc_summary = gr.JSON(label="Posterior Summary")
            with gr.Row():
                hmc_trace_plot = gr.Plot(label="Trace Plot")
                hmc_pair_plot = gr.Plot(label="Posterior Histogram")
            hmc_run_btn.click(
                fn=run_hmc_mcmc,
                inputs=[hmc_samples, hmc_warmup],
                outputs=[hmc_summary, hmc_trace_plot, hmc_pair_plot]
            )

        # Policy Management
        with gr.TabItem("Policy Management"):
            gr.Markdown("### 📋 Execution Policies")
            with gr.Row():
                low_slider = gr.Slider(0, 1, value=LOW_THRESHOLD, step=0.01, label="Low Threshold (Approve <)")
                high_slider = gr.Slider(0, 1, value=HIGH_THRESHOLD, step=0.01, label="High Threshold (Deny >)")
                update_thresh_btn = gr.Button("Update Thresholds")
            thresh_status = gr.Markdown(f"Current: approve < {LOW_THRESHOLD}, escalate {LOW_THRESHOLD}-{HIGH_THRESHOLD}, deny > {HIGH_THRESHOLD}")
            policies_json = [
                {"name": "Low Risk Policy", "conditions": [f"risk < {LOW_THRESHOLD}"], "action": "approve", "priority": 1},
                {"name": "Medium Risk Policy", "conditions": [f"{LOW_THRESHOLD} ≤ risk ≤ {HIGH_THRESHOLD}"], "action": "escalate", "priority": 2},
                {"name": "High Risk Policy", "conditions": [f"risk > {HIGH_THRESHOLD}"], "action": "deny", "priority": 3}
            ]
            policy_display = gr.JSON(label="Active Policies", value=policies_json)
            update_thresh_btn.click(
                fn=update_thresholds,
                inputs=[low_slider, high_slider],
                outputs=[thresh_status]
            ).then(
                fn=lambda: [
                    [
                        {"name": "Low Risk Policy", "conditions": [f"risk < {LOW_THRESHOLD}"], "action": "approve", "priority": 1},
                        {"name": "Medium Risk Policy", "conditions": [f"{LOW_THRESHOLD} ≤ risk ≤ {HIGH_THRESHOLD}"], "action": "escalate", "priority": 2},
                        {"name": "High Risk Policy", "conditions": [f"risk > {HIGH_THRESHOLD}"], "action": "deny", "priority": 3}
                    ]
                ],
                outputs=[policy_display]
            )

        # Enterprise / OSS
        with gr.TabItem("Enterprise / OSS"):
            gr.Markdown(f"""
            <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 2rem; border-radius: 12px; margin-bottom: 2rem; text-align: center; color: white;">
                <h1 style="margin: 0; font-size: 2.5rem;">🚀 ARF {oss_caps['edition'].upper()} Edition</h1>
                <p style="font-size: 1.2rem; opacity: 0.9;">Version {oss_caps['version']} · Apache 2.0 License</p>
            </div>

            <div style="display: flex; gap: 1.5rem; margin-bottom: 2rem;">
                <div style="flex: 1; background: #f8f9fa; padding: 1.5rem; border-radius: 8px;">
                    <h3>📦 OSS Capabilities (Demo)</h3>
                    <ul>
                        <li>✅ Bayesian conjugate prior – Beta-Binomial risk scoring</li>
                        <li>✅ Policy thresholds – configurable approve/escalate/deny</li>
                        <li>✅ MCMC sampling – Metropolis-Hastings (simulates HMC concepts)</li>
                        <li>✅ In-memory storage – no persistence</li>
                        <li>✅ Full open-source transparency</li>
                    </ul>
                </div>
                <div style="flex: 1; background: #f8f9fa; padding: 1.5rem; border-radius: 8px;">
                    <h3>🏢 Enterprise Features</h3>
                    <ul>
                        <li>🔒 Real-time HMC (using PyMC) – Bayesian deep learning for risk</li>
                        <li>🔒 Hyperpriors – hierarchical models for better generalization</li>
                        <li>🔒 Decision Engine with full audit trails</li>
                        <li>🔒 Blast radius limits & automatic rollback</li>
                        <li>🔒 Multi-cloud & hybrid deployment support</li>
                        <li>🔒 Compliance reporting (SOC2, ISO 27001)</li>
                        <li>🔒 24/7 enterprise support & SLAs</li>
                    </ul>
                </div>
            </div>

            <div style="background: #e9ecef; padding: 1.5rem; border-radius: 8px; text-align: center;">
                <h3 style="margin-top: 0;">✨ Why Upgrade to Enterprise?</h3>
                <p>ARF Enterprise delivers the same mathematically rigorous foundation but with <strong>production‑grade reliability</strong> and <strong>governance controls</strong> that meet the strictest compliance requirements.</p>
                <ul style="display: inline-block; text-align: left; margin: 1rem auto;">
                    <li>📊 **Persistent storage** – every decision logged and queryable</li>
                    <li>⚙️ **Advanced risk fusion** – combine conjugate, hyperprior, and HMC estimates</li>
                    <li>🛡️ **Semantic memory** – FAISS vector search for context‑aware policies</li>
                    <li>📈 **Real‑time dashboards** with Grafana & Prometheus integration</li>
                </ul>
            </div>

            <div style="text-align: center; margin-top: 2rem;">
                <a href="https://calendly.com/petter2025us/30min" target="_blank" style="background: #764ba2; color: white; padding: 12px 24px; text-decoration: none; border-radius: 8px; font-weight: bold; margin-right: 1rem;">📅 Book a Demo</a>
                <a href="mailto:petter2025us@outlook.com" style="background: #667eea; color: white; padding: 12px 24px; text-decoration: none; border-radius: 8px; font-weight: bold;">📧 Contact Sales</a>
            </div>
            """)
            gr.Markdown("### 📥 Export Decision History")
            export_btn = gr.DownloadButton("Download CSV", variant="primary")
            export_btn.click(
                fn=export_history_to_csv,
                outputs=[gr.File(label="decision_history.csv")]
            )

# ----------------------------------------------------------------------
# Launch
# ----------------------------------------------------------------------
if __name__ == "__main__":
    demo.queue()
    if PROMETHEUS_AVAILABLE and hasattr(demo, 'app') and demo.app:
        demo.app.add_api_route("/metrics", lambda: (generate_latest(), 200, {"Content-Type": CONTENT_TYPE_LATEST}), methods=["GET"])
    demo.launch(theme="soft", server_name="0.0.0.0", server_port=7860)