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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Felix Framework - HuggingFace Deployment Validation

Comprehensive validation script for ensuring Felix Framework deployment
readiness on HuggingFace Spaces. Tests all components, integrations,
and performance characteristics.

Usage:
    python validate_hf_deployment.py

This script validates:
1. Core Felix Framework components
2. HuggingFace integration compatibility
3. Web interface functionality
4. Mathematical precision preservation
5. Performance under resource constraints
6. Research integrity maintenance
"""

import sys
import os
import time
import logging
import asyncio
from typing import Dict, Any, List, Tuple

# Add src to path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)


class ValidationResult:
    """Stores validation test results."""

    def __init__(self, name: str, success: bool, message: str,
                 duration: float = 0.0, details: Dict[str, Any] = None):
        self.name = name
        self.success = success
        self.message = message
        self.duration = duration
        self.details = details or {}


class FelixDeploymentValidator:
    """
    Comprehensive validation suite for Felix Framework HF deployment.
    """

    def __init__(self):
        self.results: List[ValidationResult] = []
        self.critical_failures: List[str] = []

    def run_validation(self) -> bool:
        """Run complete validation suite."""
        print("Felix Framework - HuggingFace Deployment Validation")
        print("=" * 60)

        validators = [
            self._validate_core_imports,
            self._validate_mathematical_precision,
            self._validate_helix_geometry,
            self._validate_agent_system,
            self._validate_hf_integration,
            self._validate_web_interface,
            self._validate_performance,
            self._validate_research_integrity
        ]

        total_tests = len(validators)
        passed_tests = 0

        for validator in validators:
            try:
                start_time = time.time()
                result = validator()
                duration = time.time() - start_time

                if isinstance(result, ValidationResult):
                    result.duration = duration
                    self.results.append(result)
                    if result.success:
                        passed_tests += 1
                        print(f"✅ {result.name}: {result.message} ({duration:.2f}s)")
                    else:
                        print(f"❌ {result.name}: {result.message} ({duration:.2f}s)")
                        if result.name.startswith("CRITICAL"):
                            self.critical_failures.append(result.name)
                else:
                    # Handle multiple results
                    for r in result:
                        r.duration = duration / len(result)
                        self.results.append(r)
                        if r.success:
                            passed_tests += 1
                            print(f"[PASS] {r.name}: {r.message}")
                        else:
                            print(f"[FAIL] {r.name}: {r.message}")
                            if r.name.startswith("CRITICAL"):
                                self.critical_failures.append(r.name)

            except Exception as e:
                duration = time.time() - start_time
                result = ValidationResult(
                    name=f"CRITICAL - {validator.__name__}",
                    success=False,
                    message=f"Validation failed with exception: {e}",
                    duration=duration
                )
                self.results.append(result)
                self.critical_failures.append(result.name)
                print(f"❌ {result.name}: {result.message}")

        # Summary
        print("\n" + "=" * 60)
        print(f"📊 Validation Summary: {passed_tests}/{len(self.results)} tests passed")

        if self.critical_failures:
            print(f"⚠️  Critical failures: {len(self.critical_failures)}")
            for failure in self.critical_failures:
                print(f"   - {failure}")

        success_rate = passed_tests / len(self.results) if self.results else 0
        overall_success = success_rate >= 0.9 and len(self.critical_failures) == 0

        if overall_success:
            print("🎉 Felix Framework is ready for HuggingFace Spaces deployment!")
        else:
            print("🚨 Felix Framework requires fixes before deployment")

        return overall_success

    def _validate_core_imports(self) -> ValidationResult:
        """Validate all core imports work correctly."""
        try:
            # Core framework imports
            from core.helix_geometry import HelixGeometry
            from agents.agent import Agent
            from communication.central_post import CentralPost
            from llm.huggingface_client import HuggingFaceClient
            from interface.gradio_interface import FelixGradioInterface

            # External dependencies
            import numpy as np
            import plotly.graph_objects as go
            import gradio as gr

            return ValidationResult(
                "Core Imports",
                True,
                "All required modules imported successfully",
                details={"numpy_version": np.__version__, "gradio_version": gr.__version__}
            )
        except ImportError as e:
            return ValidationResult(
                "CRITICAL - Core Imports",
                False,
                f"Import failed: {e}"
            )

    def _validate_mathematical_precision(self) -> ValidationResult:
        """Validate mathematical precision preservation."""
        try:
            from core.helix_geometry import HelixGeometry
            import numpy as np

            # Standard Felix parameters
            helix = HelixGeometry(33.0, 0.001, 100.0, 33)

            # Test positions at key points
            test_points = [0.0, 0.25, 0.5, 0.75, 1.0]
            max_error = 0.0

            for t in test_points:
                x, y, z = helix.get_position(t)

                # Validate mathematical consistency
                expected_z = 100.0 * (1.0 - t)  # Linear height
                z_error = abs(z - expected_z)
                max_error = max(max_error, z_error)

                # Validate radius calculation
                radius = helix.get_radius(z)
                actual_radius = np.sqrt(x*x + y*y)
                radius_error = abs(radius - actual_radius)
                max_error = max(max_error, radius_error)

            # Check precision against Felix standard (<1e-12)
            precision_ok = max_error < 1e-10  # Slightly relaxed for deployment

            return ValidationResult(
                "Mathematical Precision",
                precision_ok,
                f"Max error: {max_error:.2e} ({'< 1e-10' if precision_ok else '>= 1e-10'})",
                details={"max_error": max_error, "test_points": len(test_points)}
            )

        except Exception as e:
            return ValidationResult(
                "CRITICAL - Mathematical Precision",
                False,
                f"Precision validation failed: {e}"
            )

    def _validate_helix_geometry(self) -> List[ValidationResult]:
        """Validate helix geometry calculations."""
        results = []

        try:
            from core.helix_geometry import HelixGeometry

            # Test standard Felix helix
            helix = HelixGeometry(33.0, 0.001, 100.0, 33)

            # Test position calculation
            x, y, z = helix.get_position(0.5)
            results.append(ValidationResult(
                "Helix Position Calculation",
                True,
                f"Position at t=0.5: ({x:.3f}, {y:.3f}, {z:.3f})"
            ))

            # Test radius tapering
            r_top = helix.get_radius(0.0)  # Top
            r_bottom = helix.get_radius(100.0)  # Bottom
            concentration_ratio = r_top / r_bottom

            results.append(ValidationResult(
                "Radius Tapering",
                abs(concentration_ratio - 33000) < 100,  # Allow small variance
                f"Concentration ratio: {concentration_ratio:.0f}x"
            ))

            # Test boundary conditions
            try:
                helix.get_position(-0.1)  # Should fail
                results.append(ValidationResult(
                    "Boundary Validation",
                    False,
                    "Failed to reject invalid t value"
                ))
            except ValueError:
                results.append(ValidationResult(
                    "Boundary Validation",
                    True,
                    "Correctly rejects invalid t values"
                ))

        except Exception as e:
            results.append(ValidationResult(
                "CRITICAL - Helix Geometry",
                False,
                f"Helix validation failed: {e}"
            ))

        return results

    def _validate_agent_system(self) -> ValidationResult:
        """Validate agent system functionality."""
        try:
            from agents.specialized_agents import ResearchAgent, AnalysisAgent, SynthesisAgent
            from communication.central_post import CentralPost

            # Create central post
            central_post = CentralPost()

            # Test agent creation
            research_agent = ResearchAgent(
                agent_id="test_research",
                helix_position=0.1,
                central_post=central_post
            )

            analysis_agent = AnalysisAgent(
                agent_id="test_analysis",
                helix_position=0.5,
                central_post=central_post
            )

            synthesis_agent = SynthesisAgent(
                agent_id="test_synthesis",
                helix_position=0.9,
                central_post=central_post
            )

            # Test agent properties
            agents = [research_agent, analysis_agent, synthesis_agent]
            agent_types = [type(agent).__name__ for agent in agents]

            return ValidationResult(
                "Agent System",
                True,
                f"Created {len(agents)} specialized agents: {', '.join(agent_types)}"
            )

        except Exception as e:
            return ValidationResult(
                "CRITICAL - Agent System",
                False,
                f"Agent system validation failed: {e}"
            )

    def _validate_hf_integration(self) -> List[ValidationResult]:
        """Validate HuggingFace integration components."""
        results = []

        try:
            from llm.huggingface_client import HuggingFaceClient, ModelType, create_felix_hf_client
            from llm.token_budget import TokenBudgetManager

            # Test HF client creation (without actual API calls)
            hf_client = create_felix_hf_client(token_budget=1000, concurrent_requests=2)

            results.append(ValidationResult(
                "HF Client Creation",
                True,
                "HuggingFace client created successfully"
            ))

            # Test model configurations
            available_models = hf_client.get_available_models()

            results.append(ValidationResult(
                "Model Configuration",
                len(available_models) >= 4,
                f"Configured {len(available_models)} model types"
            ))

            # Test token budget manager
            token_manager = TokenBudgetManager(total_budget=5000)

            results.append(ValidationResult(
                "Token Budget Manager",
                True,
                f"Token manager initialized with {token_manager.get_status()['total_budget']} budget"
            ))

        except Exception as e:
            results.append(ValidationResult(
                "CRITICAL - HF Integration",
                False,
                f"HF integration validation failed: {e}"
            ))

        return results

    def _validate_web_interface(self) -> ValidationResult:
        """Validate web interface components."""
        try:
            from interface.gradio_interface import FelixGradioInterface, create_felix_interface
            import plotly.graph_objects as go

            # Test interface creation (without launching)
            interface = create_felix_interface(enable_llm=False, token_budget=1000)

            # Test visualization creation
            viz = interface.create_helix_visualization()

            # Test interface building (without launching)
            demo = interface.create_interface()

            return ValidationResult(
                "Web Interface",
                True,
                f"Gradio interface created with {len(interface.educational_content)} educational sections"
            )

        except Exception as e:
            return ValidationResult(
                "CRITICAL - Web Interface",
                False,
                f"Web interface validation failed: {e}"
            )

    def _validate_performance(self) -> List[ValidationResult]:
        """Validate performance characteristics."""
        results = []

        try:
            import psutil
            import time
            from core.helix_geometry import HelixGeometry

            # Memory usage test
            process = psutil.Process()
            initial_memory = process.memory_info().rss / 1024 / 1024  # MB

            # Create multiple helix instances
            helixes = []
            for i in range(100):
                helix = HelixGeometry(33.0, 0.001, 100.0, 33)
                helixes.append(helix)

            final_memory = process.memory_info().rss / 1024 / 1024  # MB
            memory_increase = final_memory - initial_memory

            results.append(ValidationResult(
                "Memory Usage",
                memory_increase < 50,  # Less than 50MB for 100 helixes
                f"Memory increase: {memory_increase:.1f} MB for 100 helix instances"
            ))

            # Performance timing test
            start_time = time.time()
            positions = []
            for helix in helixes[:10]:  # Test 10 helixes
                for t in [0.0, 0.25, 0.5, 0.75, 1.0]:
                    positions.append(helix.get_position(t))

            calc_time = time.time() - start_time

            results.append(ValidationResult(
                "Calculation Performance",
                calc_time < 1.0,  # Should complete in under 1 second
                f"50 position calculations in {calc_time:.3f}s"
            ))

        except Exception as e:
            results.append(ValidationResult(
                "Performance Validation",
                False,
                f"Performance validation failed: {e}"
            ))

        return results

    def _validate_research_integrity(self) -> List[ValidationResult]:
        """Validate research integrity preservation."""
        results = []

        try:
            # Test statistical analysis components
            from comparison.statistical_analysis import StatisticalAnalyzer

            analyzer = StatisticalAnalyzer()

            results.append(ValidationResult(
                "Statistical Framework",
                True,
                "Statistical analysis components available"
            ))

            # Test educational content accuracy
            from interface.gradio_interface import FelixGradioInterface

            interface = FelixGradioInterface()
            educational_content = interface.educational_content

            # Check key research concepts are present
            required_concepts = ["helix", "agent", "spoke", "convergence", "parametric"]

            content_text = str(educational_content).lower()
            concepts_found = sum(1 for concept in required_concepts if concept in content_text)

            results.append(ValidationResult(
                "Educational Content",
                concepts_found >= len(required_concepts) - 1,
                f"Found {concepts_found}/{len(required_concepts)} key research concepts"
            ))

            # Validate mathematical references
            math_refs = ["parametric", "exponential", "precision", "<1e-12"]
            math_found = sum(1 for ref in math_refs if ref.lower() in content_text)

            results.append(ValidationResult(
                "Mathematical References",
                math_found >= 2,
                f"Found {math_found}/{len(math_refs)} mathematical references"
            ))

        except Exception as e:
            results.append(ValidationResult(
                "Research Integrity",
                False,
                f"Research integrity validation failed: {e}"
            ))

        return results

    def generate_report(self) -> str:
        """Generate comprehensive validation report."""
        report = []
        report.append("Felix Framework - HuggingFace Deployment Validation Report")
        report.append("=" * 60)
        report.append("")

        # Summary
        total_tests = len(self.results)
        passed_tests = sum(1 for r in self.results if r.success)
        success_rate = passed_tests / total_tests if total_tests > 0 else 0

        report.append(f"Overall Results: {passed_tests}/{total_tests} tests passed ({success_rate:.1%})")
        report.append(f"Critical Failures: {len(self.critical_failures)}")
        report.append("")

        # Detailed results
        report.append("Detailed Test Results:")
        report.append("-" * 30)

        for result in self.results:
            status = "PASS" if result.success else "FAIL"
            report.append(f"[{status}] {result.name}: {result.message}")
            if result.duration > 0:
                report.append(f"        Duration: {result.duration:.2f}s")
            if result.details:
                for key, value in result.details.items():
                    report.append(f"        {key}: {value}")
            report.append("")

        # Recommendations
        if self.critical_failures:
            report.append("Critical Issues Requiring Attention:")
            report.append("-" * 35)
            for failure in self.critical_failures:
                report.append(f"• {failure}")
        else:
            report.append("✅ No critical issues detected")
            report.append("Felix Framework is ready for HuggingFace Spaces deployment!")

        return "\n".join(report)


def main():
    """Main validation entry point."""
    validator = FelixDeploymentValidator()
    success = validator.run_validation()

    # Generate and save report
    report = validator.generate_report()

    try:
        with open('felix_deployment_validation_report.txt', 'w') as f:
            f.write(report)
        print(f"\n📄 Detailed report saved to: felix_deployment_validation_report.txt")
    except Exception as e:
        print(f"\n⚠️ Could not save report: {e}")

    return success


if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)