File size: 34,177 Bytes
3f5fadf
e9fdc7c
859f566
 
5aa5b79
3f5fadf
 
 
3e4331a
 
859f566
 
5aa5b79
 
3e4331a
5aa5b79
7f3d172
859f566
3e4331a
 
 
 
859f566
3e4331a
 
 
fef95f5
 
5aa5b79
859f566
 
5aa5b79
 
 
 
 
 
 
 
 
 
3e4331a
5aa5b79
3e4331a
859f566
5aa5b79
 
859f566
3e4331a
859f566
5aa5b79
859f566
5aa5b79
 
3e4331a
5aa5b79
 
 
 
 
 
 
 
 
 
 
3e4331a
5aa5b79
 
 
 
 
 
 
3e4331a
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e4331a
5aa5b79
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
859f566
5aa5b79
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
859f566
5aa5b79
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
859f566
5aa5b79
 
859f566
5aa5b79
859f566
5aa5b79
 
 
 
 
 
 
 
859f566
 
5aa5b79
 
 
 
 
 
859f566
 
5aa5b79
 
 
 
 
 
859f566
 
5aa5b79
859f566
5aa5b79
 
859f566
5aa5b79
859f566
 
 
 
5aa5b79
 
859f566
5aa5b79
 
859f566
5aa5b79
859f566
5aa5b79
 
859f566
 
 
 
5aa5b79
 
859f566
5aa5b79
859f566
 
 
 
5aa5b79
 
859f566
 
 
 
5aa5b79
 
859f566
5aa5b79
859f566
5aa5b79
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
859f566
 
5aa5b79
 
 
 
 
 
 
 
859f566
 
5aa5b79
 
 
3e4331a
5aa5b79
 
 
 
 
 
3e4331a
5aa5b79
3e4331a
5aa5b79
 
3e4331a
d265a89
5aa5b79
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
859f566
5aa5b79
 
859f566
5aa5b79
d265a89
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
d265a89
5aa5b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d265a89
5aa5b79
 
 
 
d265a89
5aa5b79
 
 
 
 
d265a89
 
 
5aa5b79
 
 
3e4331a
5aa5b79
859f566
 
 
 
 
 
 
 
 
 
5aa5b79
 
 
859f566
5aa5b79
859f566
5aa5b79
 
 
 
 
 
 
 
 
 
 
d265a89
5aa5b79
 
 
d265a89
3e4331a
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
848
849
850
851
852
"""
πŸš€ ARF Ultimate Investor Demo v3.8.0 - ENTERPRISE EDITION
Main entry point with comprehensive 5-tab interface
Uses actual ARF OSS v3.3.6 framework
FULLY CORRECTED VERSION - Integrated with all components
"""

import logging
import sys
import traceback
import json
import datetime
import asyncio
import time
from pathlib import Path
from typing import Dict, List, Any, Optional, Tuple, Union

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout),
        logging.FileHandler('arf_demo.log')
    ]
)
logger = logging.getLogger(__name__)

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent))

# ===========================================
# ARF FRAMEWORK IMPORT - CORRECTED BASED ON PACKAGE STRUCTURE
# ===========================================
ARF_OSS_AVAILABLE = False
OSS_VERSION = "3.3.6"
HealingIntent = None
IntentSource = None
IntentStatus = None
OSSMCPClient = None

try:
    # ATTEMPT 1: Import from public package (correct based on pyproject.toml)
    try:
        from agentic_reliability_framework import __version__ as arf_version
        from agentic_reliability_framework.models import HealingIntent, IntentSource, IntentStatus
        from agentic_reliability_framework.engine import OSSMCPClient
        
        ARF_OSS_AVAILABLE = True
        OSS_VERSION = arf_version
        logger.info(f"βœ… Successfully imported ARF OSS v{OSS_VERSION} from public package")
        
    except ImportError as e1:
        logger.debug(f"Attempt 1 failed: {e1}")
        
        # ATTEMPT 2: Import from modules (development structure)
        try:
            # For Hugging Face demo, use enhanced mock classes
            logger.info("Using enhanced mock classes for demo environment")
            
            # Define enums matching the actual structure
            class IntentSource:
                OSS_ANALYSIS = "oss_analysis"
                RAG_SIMILARITY = "rag_similarity"
                HUMAN_OVERRIDE = "human_override"
                AUTOMATED_LEARNING = "automated_learning"
                
            class IntentStatus:
                CREATED = "created"
                OSS_ADVISORY_ONLY = "oss_advisory_only"
                PENDING_EXECUTION = "pending_execution"
                EXECUTING = "executing"
                COMPLETED = "completed"
                FAILED = "failed"
                
            # Enhanced HealingIntent based on actual structure
            class HealingIntent:
                def __init__(self, action: str, component: str, parameters: Dict, **kwargs):
                    self.action = action
                    self.component = component
                    self.parameters = parameters
                    self.justification = kwargs.get('justification', '')
                    self.confidence = kwargs.get('confidence', 0.85)
                    self.similar_incidents = kwargs.get('similar_incidents', [])
                    self.rag_similarity_score = kwargs.get('rag_similarity_score')
                    self.source = kwargs.get('source', IntentSource.OSS_ANALYSIS)
                    self.status = kwargs.get('status', IntentStatus.CREATED)
                    self.intent_id = kwargs.get('intent_id', f"intent_{int(time.time())}")
                    self.oss_edition = "oss"
                    self.requires_enterprise = True
                    self.execution_allowed = False
                    self.created_at = time.time()
                    
                def to_enterprise_request(self) -> Dict:
                    return {
                        'action': self.action,
                        'component': self.component,
                        'parameters': self.parameters,
                        'justification': self.justification,
                        'confidence': self.confidence,
                        'requires_enterprise': self.requires_enterprise,
                        'oss_metadata': {
                            'similar_incidents_count': len(self.similar_incidents),
                            'rag_used': self.rag_similarity_score is not None,
                            'source': self.source,
                            'oss_edition': self.oss_edition,
                            'execution_allowed': self.execution_allowed
                        },
                        'intent_id': self.intent_id,
                        'created_at': self.created_at
                    }
                
                def mark_as_oss_advisory(self):
                    self.status = IntentStatus.OSS_ADVISORY_ONLY
                    self.execution_allowed = False
                    return self
                
                @classmethod
                def from_analysis(cls, action: str, component: str, parameters: Dict, 
                                justification: str, confidence: float, **kwargs):
                    return cls(
                        action=action,
                        component=component,
                        parameters=parameters,
                        justification=justification,
                        confidence=confidence,
                        similar_incidents=kwargs.get('similar_incidents'),
                        rag_similarity_score=kwargs.get('rag_similarity_score'),
                        source=kwargs.get('source', IntentSource.OSS_ANALYSIS)
                    )
            
            class OSSMCPClient:
                def __init__(self):
                    self.mode = "advisory"
                    
                async def analyze_and_recommend(self, tool_name: str, component: str, 
                                              parameters: Dict, context: Optional[Dict] = None) -> HealingIntent:
                    similar_incidents = [
                        {"id": "inc_001", "similarity": 0.78, "resolution": "scaled_out", 
                         "component": "redis", "success": True, "timestamp": "2024-01-15T10:30:00"},
                        {"id": "inc_045", "similarity": 0.65, "resolution": "restarted", 
                         "component": "database", "success": True, "timestamp": "2024-01-10T14:20:00"},
                        {"id": "inc_089", "similarity": 0.59, "resolution": "circuit_breaker", 
                         "component": "api", "success": False, "timestamp": "2024-01-05T09:15:00"}
                    ]
                    
                    rag_score = sum(inc["similarity"] for inc in similar_incidents[:3]) / 3 if similar_incidents else 0.67
                    
                    return HealingIntent.from_analysis(
                        action=tool_name,
                        component=component,
                        parameters=parameters,
                        justification=f"OSS Analysis: Based on {len(similar_incidents)} similar historical incidents with {rag_score:.0%} average similarity",
                        confidence=0.82 + (len(similar_incidents) * 0.01),
                        similar_incidents=similar_incidents,
                        rag_similarity_score=rag_score,
                        source=IntentSource.RAG_SIMILARITY
                    ).mark_as_oss_advisory()
            
            # Set module-level variables
            IntentSource = IntentSource
            IntentStatus = IntentStatus
            OSSMCPClient = OSSMCPClient
            logger.info("βœ… Using enhanced mock classes for demo")
            
        except Exception as e2:
            logger.warning(f"Enhanced mock creation failed: {e2}")
            
            # Minimal fallback
            class HealingIntent:
                def __init__(self, **kwargs):
                    pass
                def to_enterprise_request(self):
                    return {"error": "ARF not available"}
                @classmethod
                def from_analysis(cls, **kwargs):
                    return cls()
                def mark_as_oss_advisory(self):
                    return self
            
            class OSSMCPClient:
                def __init__(self):
                    self.mode = "advisory"
                async def analyze_and_recommend(self, **kwargs):
                    return HealingIntent()

except Exception as e:
    logger.error(f"❌ CRITICAL: Failed to initialize ARF framework: {e}")
    logger.error(traceback.format_exc())
    # Provide absolute minimal fallbacks
    class HealingIntent:
        def __init__(self, **kwargs):
            pass
        def to_enterprise_request(self):
            return {"error": "ARF not available"}
        @classmethod
        def from_analysis(cls, **kwargs):
            return cls()
    class OSSMCPClient:
        async def analyze_and_recommend(self, **kwargs):
            return HealingIntent()

# ===========================================
# IMPORT SUPPORTING MODULES
# ===========================================
try:
    # Import UI components
    from ui.components import (
        create_header, create_status_bar, create_tab1_incident_demo,
        create_tab2_business_roi, create_tab3_audit_trail,
        create_tab4_enterprise_features, create_tab5_learning_engine,
        create_footer
    )
    
    # Import demo orchestrator
    from demo.orchestrator import DemoOrchestrator
    
    # Import visualizations
    from core.visualizations import EnhancedVisualizationEngine
    
    logger.info("βœ… Successfully imported all supporting modules")
    
except ImportError as e:
    logger.warning(f"Could not import some modules: {e}")
    # We'll define minimal versions inline if needed

# ===========================================
# INCIDENT SCENARIOS (from original app.py)
# ===========================================
INCIDENT_SCENARIOS = {
    "Cache Miss Storm": {
        "description": "Redis cluster experiencing 80% cache miss rate causing database overload",
        "severity": "CRITICAL",
        "metrics": {
            "Cache Hit Rate": "18.5% (Critical)",
            "Database Load": "92% (Overloaded)", 
            "Response Time": "1850ms (Slow)",
            "Affected Users": "45,000",
            "Eviction Rate": "125/sec"
        },
        "impact": {
            "Revenue Loss": "$8,500/hour",
            "Page Load Time": "+300%",
            "Users Impacted": "45,000",
            "SLA Violation": "Yes",
            "Customer Sat": "-40%"
        },
        "component": "redis_cache",
        "oss_analysis": {
            "status": "βœ… ARF OSS Analysis Complete",
            "recommendations": [
                "Increase Redis cache memory allocation",
                "Implement cache warming strategy",
                "Optimize key patterns",
                "Add circuit breaker for database fallback"
            ],
            "estimated_time": "60+ minutes",
            "engineers_needed": "2-3 SREs + 1 DBA",
            "manual_effort": "High",
            "total_cost": "$8,500"
        },
        "enterprise_results": {
            "actions_completed": [
                "βœ… Auto-scaled Redis cluster: 4GB β†’ 8GB",
                "βœ… Deployed intelligent cache warming",
                "βœ… Optimized 12 key patterns with ML",
                "βœ… Implemented circuit breaker"
            ],
            "metrics_improvement": {
                "Cache Hit Rate": "18.5% β†’ 72%",
                "Response Time": "1850ms β†’ 450ms",
                "Database Load": "92% β†’ 45%"
            },
            "business_impact": {
                "Recovery Time": "60 min β†’ 12 min",
                "Cost Saved": "$7,200",
                "Users Impacted": "45,000 β†’ 0",
                "Revenue Protected": "$1,700",
                "MTTR Improvement": "80% reduction"
            }
        }
    },
    "Database Connection Pool Exhaustion": {
        "description": "Database connection pool exhausted causing API timeouts and user failures",
        "severity": "HIGH",
        "metrics": {
            "Active Connections": "98/100 (Critical)",
            "API Latency": "2450ms",
            "Error Rate": "15.2%",
            "Queue Depth": "1250",
            "Connection Wait": "45s"
        },
        "impact": {
            "Revenue Loss": "$4,200/hour",
            "Affected Services": "API Gateway, User Service, Payment",
            "SLA Violation": "Yes",
            "Partner Impact": "3 external APIs"
        },
        "component": "database"
    },
    "Memory Leak in Production": {
        "description": "Java service memory leak causing gradual performance degradation",
        "severity": "HIGH",
        "metrics": {
            "Memory Usage": "96% (Critical)",
            "GC Pause Time": "4500ms",
            "Error Rate": "28.5%",
            "Restart Frequency": "12/hour",
            "Heap Fragmentation": "42%"
        },
        "impact": {
            "Revenue Loss": "$5,500/hour",
            "Session Loss": "8,500 users",
            "Customer Impact": "High",
            "Support Tickets": "+300%"
        },
        "component": "java_service"
    }
}

# ===========================================
# AUDIT TRAIL MANAGER (from original app.py)
# ===========================================
class AuditTrailManager:
    """Manage audit trail and execution history"""
    
    def __init__(self):
        self.execution_history = []
        self.incident_history = []
        self._initialize_sample_data()
    
    def _initialize_sample_data(self):
        """Initialize with sample historical data"""
        base_time = datetime.datetime.now() - datetime.timedelta(hours=2)
        
        sample_executions = [
            self._create_execution_entry(
                base_time - datetime.timedelta(minutes=90),
                "Cache Miss Storm", 4, 7200, "βœ… Executed", "Auto-scaled cache"
            ),
            self._create_execution_entry(
                base_time - datetime.timedelta(minutes=75),
                "Memory Leak", 3, 5200, "βœ… Executed", "Fixed memory leak"
            ),
            self._create_execution_entry(
                base_time - datetime.timedelta(minutes=60),
                "API Rate Limit", 4, 2800, "βœ… Executed", "Increased rate limits"
            ),
        ]
        
        self.execution_history = sample_executions
        
        services = ["API Gateway", "Database", "Redis Cache", "Auth Service"]
        
        for i in range(8):
            incident_time = base_time - datetime.timedelta(minutes=i * 15)
            self.incident_history.append({
                "timestamp": incident_time,
                "time_str": incident_time.strftime("%H:%M"),
                "service": services[i % len(services)],
                "type": "Cache Miss Storm" if i % 3 == 0 else "Memory Leak",
                "severity": 3 if i % 3 == 0 else 2,
                "description": f"High latency on {services[i % len(services)]}",
                "id": f"inc_{i:03d}"
            })
    
    def _create_execution_entry(self, timestamp, scenario, actions, savings, status, details):
        return {
            "timestamp": timestamp,
            "time_str": timestamp.strftime("%H:%M"),
            "scenario": scenario,
            "actions": str(actions),
            "savings": f"${savings:,}",
            "status": status,
            "details": details,
            "id": f"exec_{len(self.execution_history):03d}"
        }
    
    def add_execution(self, scenario: str, actions: List[str], 
                     savings: int, approval_required: bool, details: str = ""):
        entry = self._create_execution_entry(
            datetime.datetime.now(),
            scenario,
            len(actions),
            savings,
            "βœ… Approved & Executed" if approval_required else "βœ… Auto-Executed",
            details
        )
        self.execution_history.insert(0, entry)
        return entry
    
    def add_incident(self, scenario_name: str, metrics: Dict):
        entry = {
            "timestamp": datetime.datetime.now(),
            "time_str": datetime.datetime.now().strftime("%H:%M"),
            "service": "Demo System",
            "type": scenario_name,
            "severity": 3,
            "description": f"Demo incident: {scenario_name}",
            "id": f"inc_{len(self.incident_history):03d}"
        }
        self.incident_history.insert(0, entry)
        return entry
    
    def get_execution_history_table(self, limit: int = 10) -> List[List]:
        return [
            [entry["time_str"], entry["scenario"], entry["actions"], 
             entry["status"], entry["savings"], entry["details"]]
            for entry in self.execution_history[:limit]
        ]
    
    def get_incident_history_table(self, limit: int = 15) -> List[List]:
        return [
            [entry["time_str"], entry["service"], entry["type"], 
             f"{entry['severity']}/3", entry["description"]]
            for entry in self.incident_history[:limit]
        ]
    
    def export_audit_trail(self) -> str:
        total_savings = sum(
            int(e["savings"].replace("$", "").replace(",", ""))
            for e in self.execution_history
            if "$" in e["savings"]
        )
        
        return json.dumps({
            "executions": self.execution_history,
            "incidents": self.incident_history,
            "exported_at": datetime.datetime.now().isoformat(),
            "total_executions": len(self.execution_history),
            "total_incidents": len(self.incident_history),
            "total_savings": total_savings,
            "arf_version": OSS_VERSION,
            "oss_available": ARF_OSS_AVAILABLE
        }, indent=2, default=str)

# ===========================================
# CREATE DEMO INTERFACE - CORRECTED
# ===========================================
def create_demo_interface():
    """Create the 5-tab demo interface - CORRECTED VERSION"""
    
    # Import gradio here to avoid issues
    import gradio as gr
    
    # Initialize components
    audit_manager = AuditTrailManager()
    
    # Create OSS client
    oss_client = OSSMCPClient() if OSSMCPClient else None
    
    # Create orchestrator
    orchestrator = DemoOrchestrator(arf_client=oss_client)
    
    # Create visualization engine
    try:
        viz_engine = EnhancedVisualizationEngine()
    except:
        # Fallback to simple visualizations
        class SimpleVizEngine:
            def create_interactive_timeline(self, scenario):
                import plotly.graph_objects as go
                fig = go.Figure()
                fig.add_trace(go.Scatter(x=[1,2,3], y=[1,2,1]))
                fig.update_layout(title="Timeline", height=400)
                return fig
            def create_executive_dashboard(self, roi=None):
                import plotly.graph_objects as go
                fig = go.Figure()
                fig.add_trace(go.Bar(x=['A','B','C'], y=[1,2,3]))
                fig.update_layout(title="Dashboard", height=400)
                return fig
        viz_engine = SimpleVizEngine()
    
    # Custom CSS
    custom_css = """
    .gradio-container {
        max-width: 1800px !important;
        margin: auto !important;
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif !important;
    }
    h1 {
        background: linear-gradient(90deg, #1a365d 0%, #2d3748 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
        font-weight: 800 !important;
        font-size: 2.5rem !important;
        margin-bottom: 0.5rem !important;
    }
    .critical { color: #FF6B6B !important; font-weight: 900 !important; }
    .success { color: #4ECDC4 !important; font-weight: 900 !important; }
    .tab-nav { background: linear-gradient(90deg, #f8fafc 0%, #ffffff 100%) !important; }
    .metric-card {
        background: white !important;
        border-radius: 10px !important;
        padding: 20px !important;
        box-shadow: 0 2px 8px rgba(0,0,0,0.06) !important;
        border-left: 4px solid #4ECDC4 !important;
        margin-bottom: 15px !important;
    }
    """
    
    with gr.Blocks(
        title=f"πŸš€ ARF Investor Demo v3.8.0",
        theme=gr.themes.Soft(
            primary_hue="blue",
            secondary_hue="teal"
        ),
        css=custom_css
    ) as demo:
        
        # ============ HEADER ============
        create_header(OSS_VERSION, ARF_OSS_AVAILABLE)
        
        # ============ STATUS BAR ============
        create_status_bar()
        
        # ============ 5 TABS ============
        with gr.Tabs():
            
            # TAB 1: LIVE INCIDENT DEMO
            with gr.TabItem("πŸ”₯ Live Incident Demo"):
                # Get components from UI module
                (scenario_dropdown, scenario_description, metrics_display, impact_display,
                 timeline_output, oss_btn, enterprise_btn, approval_toggle, demo_btn,
                 approval_display, config_display, results_display) = create_tab1_incident_demo(
                    INCIDENT_SCENARIOS, "Cache Miss Storm"
                )
            
            # TAB 2: BUSINESS IMPACT & ROI
            with gr.TabItem("πŸ’° Business Impact & ROI"):
                (dashboard_output, monthly_slider, impact_slider, team_slider,
                 calculate_btn, roi_output) = create_tab2_business_roi()
            
            # TAB 3: AUDIT TRAIL & HISTORY
            with gr.TabItem("πŸ“œ Audit Trail & History"):
                (refresh_btn, clear_btn, export_btn, execution_table, savings_chart,
                 incident_table, memory_graph, export_text) = create_tab3_audit_trail()
            
            # TAB 4: ENTERPRISE FEATURES
            with gr.TabItem("🏒 Enterprise Features"):
                (license_display, validate_btn, trial_btn, upgrade_btn, features_table,
                 compliance_display, integrations_table, mcp_mode) = create_tab4_enterprise_features()
            
            # TAB 5: LEARNING ENGINE
            with gr.TabItem("🧠 Learning Engine"):
                (learning_graph, graph_type, show_labels, search_query, search_btn,
                 clear_btn_search, search_results, stats_display, patterns_display,
                 performance_display) = create_tab5_learning_engine()
        
        # ============ FOOTER ============
        create_footer()
        
        # ============ EVENT HANDLERS ============
        
        # Scenario dropdown change
        def update_scenario(scenario_name):
            scenario = INCIDENT_SCENARIOS.get(scenario_name, {})
            timeline = viz_engine.create_interactive_timeline(scenario)
            return (
                f"### {scenario_name}\n{scenario.get('description', 'No description')}",
                scenario.get("metrics", {}),
                scenario.get("impact", {}),
                timeline
            )
        
        scenario_dropdown.change(
            fn=update_scenario,
            inputs=[scenario_dropdown],
            outputs=[scenario_description, metrics_display, impact_display, timeline_output]
        )
        
        # OSS Analysis button
        async def run_oss_analysis(scenario_name):
            scenario = INCIDENT_SCENARIOS.get(scenario_name, {})
            analysis = await orchestrator.analyze_incident(scenario_name, scenario)
            
            # Add to audit trail
            audit_manager.add_incident(scenario_name, scenario.get("metrics", {}))
            
            # Update tables
            incident_table_data = audit_manager.get_incident_history_table()
            
            return analysis, incident_table_data
        
        oss_btn.click(
            fn=run_oss_analysis,
            inputs=[scenario_dropdown],
            outputs=[results_display, incident_table]
        )
        
        # Enterprise Healing button
        def execute_healing(scenario_name, approval_required):
            scenario = INCIDENT_SCENARIOS.get(scenario_name, {})
            
            # Create mock healing intent
            healing_intent = {
                "action": "scale_out",
                "component": scenario.get("component", "unknown"),
                "parameters": {"scale_factor": 2},
                "justification": f"Healing {scenario_name}"
            }
            
            # Execute through orchestrator
            execution = orchestrator.execute_healing(
                scenario_name, 
                healing_intent,
                mode="approval" if approval_required else "autonomous"
            )
            
            # Add to audit trail
            audit_manager.add_execution(
                scenario_name,
                ["scale_out", "circuit_breaker", "monitoring"],
                7200,
                approval_required,
                f"Healed {scenario_name}"
            )
            
            # Create approval HTML
            if approval_required:
                approval_html = """
                <div style='padding: 20px; background: #e8f5e8; border-radius: 10px; border-left: 4px solid #28a745;'>
                    <div style='display: flex; align-items: center; gap: 10px; margin-bottom: 15px;'>
                        <div style='background: #28a745; color: white; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold;'>
                            βœ…
                        </div>
                        <h4 style='margin: 0; color: #1a365d;'>Approved & Executed</h4>
                    </div>
                    <div style='color: #2d3748;'>
                        Action approved by system administrator and executed successfully.
                    </div>
                </div>
                """
            else:
                approval_html = """
                <div style='padding: 20px; background: #e3f2fd; border-radius: 10px; border-left: 4px solid #2196f3;'>
                    <div style='display: flex; align-items: center; gap: 10px; margin-bottom: 15px;'>
                        <div style='background: #2196f3; color: white; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold;'>
                            ⚑
                        </div>
                        <h4 style='margin: 0; color: #1a365d;'>Auto-Executed</h4>
                    </div>
                    <div style='color: #2d3748;'>
                        Action executed autonomously by ARF Enterprise.
                    </div>
                </div>
                """
            
            # Update execution table
            execution_table_data = audit_manager.get_execution_history_table()
            
            return approval_html, execution, execution_table_data
        
        enterprise_btn.click(
            fn=execute_healing,
            inputs=[scenario_dropdown, approval_toggle],
            outputs=[approval_display, results_display, execution_table]
        )
        
        # Quick Demo button
        async def run_quick_demo():
            # Run OSS analysis
            scenario = INCIDENT_SCENARIOS["Cache Miss Storm"]
            analysis = await orchestrator.analyze_incident("Cache Miss Storm", scenario)
            
            # Execute healing
            execution = orchestrator.execute_healing(
                "Cache Miss Storm",
                {"action": "scale_out", "component": "redis_cache"},
                mode="autonomous"
            )
            
            # Update audit trail
            audit_manager.add_incident("Cache Miss Storm", scenario.get("metrics", {}))
            audit_manager.add_execution(
                "Cache Miss Storm",
                ["scale_out", "circuit_breaker"],
                7200,
                False,
                "Quick demo execution"
            )
            
            # Update all tables
            execution_table_data = audit_manager.get_execution_history_table()
            incident_table_data = audit_manager.get_incident_history_table()
            
            # Create approval HTML
            approval_html = """
            <div style='padding: 20px; background: #fff3cd; border-radius: 10px; border-left: 4px solid #ffc107;'>
                <div style='display: flex; align-items: center; gap: 10px; margin-bottom: 15px;'>
                    <div style='background: #ffc107; color: white; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold;'>
                        ⚑
                    </div>
                    <h4 style='margin: 0; color: #1a365d;'>Quick Demo Completed</h4>
                </div>
                <div style='color: #2d3748;'>
                    OSS analysis β†’ Enterprise execution completed successfully.
                </div>
            </div>
            """
            
            return (
                analysis,
                approval_html,
                execution,
                execution_table_data,
                incident_table_data,
                gr.Checkbox.update(value=False)
            )
        
        demo_btn.click(
            fn=run_quick_demo,
            outputs=[
                results_display,
                approval_display,
                results_display,
                execution_table,
                incident_table,
                approval_toggle
            ]
        )
        
        # ROI Calculator
        def calculate_roi(monthly, impact, team):
            company_data = {
                "monthly_incidents": monthly,
                "avg_cost_per_incident": impact,
                "team_size": team
            }
            roi_result = orchestrator.calculate_roi(company_data)
            
            # Format for display
            formatted_result = {
                "annual_impact": f"${roi_result['annual_impact']:,.0f}",
                "team_cost": f"${roi_result['team_cost']:,.0f}",
                "potential_savings": f"${roi_result['potential_savings']:,.0f}",
                "roi_multiplier": f"{roi_result['roi_multiplier']:.1f}Γ—",
                "payback_months": f"{roi_result['payback_months']:.1f} months",
                "recommendation": roi_result['recommendation']
            }
            
            # Update dashboard with user-specific ROI
            dashboard = viz_engine.create_executive_dashboard(roi_result)
            
            return formatted_result, dashboard
        
        calculate_btn.click(
            fn=calculate_roi,
            inputs=[monthly_slider, impact_slider, team_slider],
            outputs=[roi_output, dashboard_output]
        )
        
        # Audit Trail Refresh
        def refresh_audit_trail():
            execution_table_data = audit_manager.get_execution_history_table()
            incident_table_data = audit_manager.get_incident_history_table()
            export_data = audit_manager.export_audit_trail()
            return execution_table_data, incident_table_data, export_data
        
        refresh_btn.click(
            fn=refresh_audit_trail,
            outputs=[execution_table, incident_table, export_text]
        )
        
        # Clear History
        def clear_audit_trail():
            audit_manager.execution_history = []
            audit_manager.incident_history = []
            audit_manager._initialize_sample_data()
            return refresh_audit_trail()
        
        clear_btn.click(
            fn=clear_audit_trail,
            outputs=[execution_table, incident_table, export_text]
        )
        
        # Export Audit Trail
        def update_export():
            return audit_manager.export_audit_trail()
        
        export_btn.click(
            fn=update_export,
            outputs=[export_text]
        )
        
        # Search similar incidents
        def search_incidents(query):
            if not query.strip():
                return []
            results = orchestrator.get_similar_incidents(query)
            return [[r["id"], f"{r['similarity']:.0%}", r["scenario"], r["resolution"]] 
                    for r in results]
        
        search_btn.click(
            fn=search_incidents,
            inputs=[search_query],
            outputs=[search_results]
        )
        
        clear_btn_search.click(
            fn=lambda: [],
            outputs=[search_results]
        )
        
        # Initialize dashboard on load
        demo.load(
            fn=lambda: viz_engine.create_executive_dashboard(),
            outputs=[dashboard_output]
        )
    
    return demo

# ===========================================
# MAIN EXECUTION - CORRECTED
# ===========================================
def main():
    """Main entry point - CORRECTED"""
    print("πŸš€ Starting ARF Ultimate Investor Demo v3.8.0...")
    print("=" * 70)
    print("πŸ“Š Features:")
    print("  β€’ 5 Comprehensive Tabs with User-Focused Journey")
    print("  β€’ Live Incident Demo with OSS Analysis")
    print("  β€’ Business Impact & ROI Calculator")
    print("  β€’ Audit Trail & History with Memory Graph")
    print("  β€’ Enterprise Features & License Management")
    print("  β€’ Learning Engine with Pattern Detection")
    print("=" * 70)
    print(f"\nπŸ“¦ Using ARF OSS v{OSS_VERSION}")
    print(f"πŸ”§ ARF Available: {ARF_OSS_AVAILABLE}")
    print("🌐 Opening web interface...")
    
    # Create and launch the demo interface
    demo = create_demo_interface()
    
    # Launch configuration
    launch_config = {
        "server_name": "0.0.0.0",
        "server_port": 7860,
        "share": False,
        "debug": False,
        "show_error": True
    }
    
    demo.launch(**launch_config)

# ===========================================
# EXECUTION GUARD - CORRECTED
# ===========================================
if __name__ == "__main__":
    main()