"""Constitutional module - F2 Truth enforced
Part of arifOS constitutional governance system
DITEMPA BUKAN DIBERI - Forged, not given
"""

#!/usr/bin/env python3
"""
Codex Constitutional Skills for arifOS MCP
Implements coding-specific skills with full constitutional governance
"""

import asyncio
import re
import ast
from typing import Dict, List, Optional, Any, Union
import logging

# arifOS constitutional components
from arifos_core.mcp.unified_server import ConstitutionalMCPClient
from arifos_core.enforcement.metrics import ConstitutionalMetrics
from arifos_core.system.apex_prime import apex_review, Verdict
from arifos_core.memory.vault999 import vault999_query, vault999_store
from arifos_core.trinity.coordinator import TrinityCoordinator


class CodeVerdict(Enum):
    """Verdicts specific to code operations"""
    CODE_SEAL = "CODE_SEAL"
    CODE_PARTIAL = "CODE_PARTIAL"
    CODE_VOID = "CODE_VOID"
    CODE_SABAR = "CODE_SABAR"


@dataclass
class ConstitutionalCodeAnalysis:
    """Result of constitutional code analysis"""
    verdict: Union[Verdict, CodeVerdict]
    security_score: float
    performance_score: float
    architectural_score: float
    maintainability_score: float
    constitutional_compliance: Dict[str, bool]
    agi_insights: List[str]
    asi_validation: Dict[str, Any]
    apex_verdict: Dict[str, Any]
    recommendations: List[str]
    metrics: Dict[str, float]


@dataclass
class ConstitutionalCodeGeneration:
    """Result of constitutional code generation"""
    verdict: Union[Verdict, CodeVerdict]
    generated_code: str
    constitutional_headers: List[str]
    complexity_score: float
    clarity_score: float
    trinity_validation: Dict[str, Any]
    constraints_applied: List[str]
    generation_metrics: Dict[str, float]


class CodexConstitutionalSkills:
    """Constitutional coding skills with Trinity governance"""
    
    def __init__(self, user_id: str = "codex_user"):
        self.user_id = user_id
        self.metrics = ConstitutionalMetrics()
        self.trinity_coordinator = TrinityCoordinator()
        
        # Code analysis patterns
        self.security_patterns = [
            r'eval\s*\(',
            r'exec\s*\(',
            r'__import__',
            r'subprocess\.call',
            r'os\.system',
            r'input\s*\(',
            r'raw_input\s*\('
        ]
        
        self.performance_patterns = [
            r'for.*range\(',
            r'while.*True',
            r'list\(',
            r'dict\(',
            r'recursive',
            r'global'
        ]
        
        self.architectural_patterns = [
            r'class\s+\w+',
            r'def\s+\w+',
            r'import\s+',
            r'from\s+\w+\s+import',
            r'__init__',
            r'self\.'
        ]
        
        # Constitutional code templates
        self.constitutional_templates = {
            "function_header": '"""Constitutional function - F{floor} {description}\n{DITEMPA} - Forged with constitutional governance\n"""\n',
            "class_header": '"""Constitutional class - F{floor} {description}\n{DITEMPA} - Forged with constitutional governance\n"""\n',
            "module_header": '"""Constitutional module - F{floor} {description}\nPart of arifOS constitutional governance system\n{DITEMPA} - Forged, not given\n"""\n'
        }
    
    @constitutional_tool(name="codex_code_analysis")
    async def analyze_code(self, code: str, analysis_type: str, user_id: str, context: Optional[Dict] = None) -> Dict:
        """Analyze code with AGI/ASI/APEX constitutional validation"""
        
        logging.info(f"Constitutional code analysis requested: {analysis_type}")
        
        # F6 Clarity: Pre-analysis complexity check
        clarity_score = self._calculate_code_clarity(code)
        if clarity_score < 0.2:
            return {
                "verdict": CodeVerdict.CODE_VOID.value,
                "reason": "F6 Clarity violation: Code too complex for constitutional analysis",
                "constitutional_compliance": {"f6_clarity": False},
                "recommendations": ["Simplify code structure", "Reduce nesting levels", "Improve naming clarity"]
            }
        
        # Phase 1: AGI Analysis (Architectural Perspective)
        logging.info("Phase 1: AGI architectural analysis")
        agi_analysis = await self._agi_architectural_analysis(code, analysis_type, context)
        
        # Phase 2: ASI Validation (Safety & Empathy)
        logging.info("Phase 2: ASI safety validation")
        asi_validation = await self._asi_safety_validation(code, agi_analysis, context)
        
        # Phase 3: APEX Judgment (Final Constitutional Verdict)
        logging.info("Phase 3: APEX final judgment")
        apex_verdict = await self._apex_constitutional_judgment(code, agi_analysis, asi_validation, analysis_type)
        
        # Synthesize final analysis
        final_analysis = ConstitutionalCodeAnalysis(
            verdict=apex_verdict["verdict"],
            security_score=asi_validation["security_score"],
            performance_score=agi_analysis["performance_score"],
            architectural_score=agi_analysis["architectural_score"],
            maintainability_score=self._calculate_maintainability_score(code, agi_analysis),
            constitutional_compliance=apex_verdict["constitutional_compliance"],
            agi_insights=agi_analysis["insights"],
            asi_validation=asi_validation,
            apex_verdict=apex_verdict,
            recommendations=self._generate_recommendations(agi_analysis, asi_validation, apex_verdict),
            metrics=apex_verdict["metrics"]
        )
        
        # Store analysis in VAULT-999 if sealed
        if final_analysis.verdict in [Verdict.SEAL, CodeVerdict.CODE_SEAL]:
            await self._store_analysis_in_vault(code, final_analysis, user_id)
        
        return {
            "verdict": final_analysis.verdict.value if hasattr(final_analysis.verdict, 'value') else str(final_analysis.verdict),
            "security_score": final_analysis.security_score,
            "performance_score": final_analysis.performance_score,
            "architectural_score": final_analysis.architectural_score,
            "maintainability_score": final_analysis.maintainability_score,
            "constitutional_compliance": final_analysis.constitutional_compliance,
            "agi_insights": final_analysis.agi_insights,
            "asi_validation": final_analysis.asi_validation,
            "apex_verdict": final_analysis.apex_verdict,
            "recommendations": final_analysis.recommendations,
            "metrics": final_analysis.metrics,
            "constitutional_valid": final_analysis.verdict in [Verdict.SEAL, Verdict.PARTIAL, CodeVerdict.CODE_SEAL, CodeVerdict.CODE_PARTIAL]
        }
    
    async def _agi_architectural_analysis(self, code: str, analysis_type: str, context: Optional[Dict]) -> Dict:
        """AGI architectural analysis (The Mind - orthogonal crystal)"""
        
        # Parse code AST for architectural insights
        try:
            tree = ast.parse(code)
            architectural_insights = self._extract_architectural_patterns(tree)
        except SyntaxError:
            architectural_insights = {"error": "Invalid Python syntax", "patterns": []}
        
        # Performance analysis
        performance_score = self._analyze_performance_characteristics(code)
        
        # Architectural pattern recognition
        architectural_score = self._analyze_architectural_quality(code, tree if 'tree' in locals() else None)
        
        # Generate AGI insights
        insights = [
            f"AGI Architectural Analysis: {analysis_type}",
            f"Code complexity: {architectural_insights.get('complexity', 'unknown')}",
            f"Architectural patterns detected: {len(architectural_insights.get('patterns', []))}",
            f"Performance characteristics: {performance_score:.2f}/1.0",
            f"Architectural quality: {architectural_score:.2f}/1.0"
        ]
        
        # Add specific insights based on analysis type
        if analysis_type == "architecture":
            insights.extend(self._generate_architectural_insights(architectural_insights))
        elif analysis_type == "performance":
            insights.extend(self._generate_performance_insights(code, performance_score))
        elif analysis_type == "security":
            insights.extend(self._generate_security_insights(code))
        elif analysis_type == "maintainability":
            insights.extend(self._generate_maintainability_insights(code))
        
        return {
            "performance_score": performance_score,
            "architectural_score": architectural_score,
            "insights": insights,
            "patterns": architectural_insights.get('patterns', []),
            "complexity_analysis": architectural_insights.get('complexity', 'moderate'),
            "recommendations": self._generate_agi_recommendations(architectural_insights, performance_score)
        }
    
    async def _asi_safety_validation(self, code: str, agi_analysis: Dict, context: Optional[Dict]) -> Dict:
        """ASI safety validation (The Heart - fractal spiral)"""
        
        # Security vulnerability detection
        security_issues = self._detect_security_vulnerabilities(code)
        security_score = max(0.0, 1.0 - (len(security_issues) * 0.2))
        
        # Empathy analysis - who might be affected by this code?
        stakeholder_impact = self._analyze_stakeholder_impact(code, context)
        
        # Safety pattern validation
        safety_patterns = self._validate_safety_patterns(code)
        
        # Weakest stakeholder protection (κᵣ formula)
        weakest_protection = self._calculate_weakest_stakeholder_protection(code, stakeholder_impact)
        
        return {
            "security_score": security_score,
            "security_issues": security_issues,
            "stakeholder_impact": stakeholder_impact,
            "safety_patterns_valid": safety_patterns,
            "weakest_stakeholder_protected": weakest_protection["protected"],
            "kappa_r": weakest_protection["kappa_r"],
            "empathy_score": weakest_protection["empathy_score"],
            "safety_recommendations": self._generate_safety_recommendations(security_issues, stakeholder_impact)
        }
    
    async def _apex_constitutional_judgment(self, code: str, agi_analysis: Dict, asi_validation: Dict, analysis_type: str) -> Dict:
        """APEX final constitutional judgment (The Soul - toroidal manifold)"""
        
        # Compile constitutional metrics
        constitutional_metrics = {
            "f2_truth": self._validate_code_truthfulness(code),
            "f3_peace": self._validate_code_peacefulness(code, asi_validation),
            "f4_clarity": self._validate_code_clarity(code),
            "f6_clarity": self._validate_code_complexity(code),
            "f8_tri_witness": self._validate_tri_witness_compliance(code, agi_analysis, asi_validation),
            "f9_anti_hantu": self._validate_anti_hantu_compliance(code),
            "f10_symbolic": self._validate_symbolic_meaning(code),
            "f11_command_auth": self._validate_command_authority(code),
            "f12_injection": self._validate_injection_defense(code)
        }
        
        # Calculate overall constitutional score
        constitutional_score = sum(constitutional_metrics.values()) / len(constitutional_metrics)
        
        # Determine final verdict
        if constitutional_score >= 0.85:
            verdict = Verdict.SEAL
        elif constitutional_score >= 0.70:
            verdict = Verdict.PARTIAL
        elif constitutional_score >= 0.50:
            verdict = CodeVerdict.CODE_SABAR
        else:
            verdict = CodeVerdict.CODE_VOID
        
        return {
            "verdict": verdict,
            "constitutional_compliance": constitutional_metrics,
            "constitutional_score": constitutional_score,
            "metrics": {
                "overall_score": constitutional_score,
                "agi_contribution": agi_analysis["architectural_score"],
                "asi_contribution": asi_validation["empathy_score"],
                "trinity_synthesis": (agi_analysis["architectural_score"] + asi_validation["empathy_score"] + constitutional_score) / 3
            },
            "final_recommendations": self._generate_apex_recommendations(constitutional_metrics, agi_analysis, asi_validation)
        }
    
    @constitutional_tool(name="codex_code_generation")
    async def generate_code(self, requirements: str, constraints: List[str], user_id: str, 
                          language: str = "python", complexity_level: str = "moderate", 
                          context: Optional[Dict] = None) -> Dict:
        """Generate code with constitutional constraints and trinity validation"""
        
        logging.info(f"Constitutional code generation requested: {complexity_level} {language}")
        
        # F1 Amanah: Requirements validation
        validated_requirements = await self._validate_requirements_constitutionally(requirements)
        if not validated_requirements["valid"]:
            return {
                "verdict": CodeVerdict.CODE_VOID.value,
                "reason": f"F1 Amanah violation: {validated_requirements['reason']}",
                "constitutional_compliance": {"f1_amanah": False}
            }
        
        # F2 Truth: Requirements truth validation
        truth_validation = await self._validate_requirements_truth(validated_requirements["requirements"])
        if not truth_validation["valid"]:
            return {
                "verdict": CodeVerdict.CODE_VOID.value,
                "reason": f"F2 Truth violation: {truth_validation['reason']}",
                "constitutional_compliance": {"f2_truth": False}
            }
        
        # F3 Peace: Constraint analysis
        peaceful_constraints = await self._analyze_constraints_peacefully(constraints)
        if not peaceful_constraints["peaceful"]:
            return {
                "verdict": CodeVerdict.CODE_VOID.value,
                "reason": f"F3 Peace violation: {peaceful_constraints['reason']}",
                "constitutional_compliance": {"f3_peace": False}
            }
        
        # Phase 1: AGI Architectural Foundation
        logging.info("Phase 1: AGI architectural foundation")
        agi_foundation = await self._agi_architectural_foundation(
            truth_validation["requirements"], 
            peaceful_constraints["constraints"], 
            language, 
            complexity_level
        )
        
        # Phase 2: ASI Empathetic Constraints
        logging.info("Phase 2: ASI empathetic constraints")
        asi_constraints = await self._asi_empathetic_constraints(
            agi_foundation, 
            peaceful_constraints["constraints"], 
            context
        )
        
        # Phase 3: Codex Generation with APEX Judgment
        logging.info("Phase 3: Codex generation with APEX judgment")
        codex_generation = await self._codex_generation_with_apex_judgment(
            agi_foundation,
            asi_constraints,
            language,
            complexity_level
        )
        
        # Store generation in VAULT-999 if sealed
        if codex_generation.verdict in [Verdict.SEAL, CodeVerdict.CODE_SEAL]:
            await self._store_generation_in_vault(codex_generation, user_id)
        
        return {
            "verdict": codex_generation.verdict.value if hasattr(codex_generation.verdict, 'value') else str(codex_generation.verdict),
            "generated_code": codex_generation.generated_code,
            "constitutional_headers": codex_generation.constitutional_headers,
            "complexity_score": codex_generation.complexity_score,
            "clarity_score": codex_generation.clarity_score,
            "trinity_validation": codex_generation.trinity_validation,
            "constraints_applied": codex_generation.constraints_applied,
            "generation_metrics": codex_generation.generation_metrics,
            "constitutional_valid": codex_generation.verdict in [Verdict.SEAL, Verdict.PARTIAL, CodeVerdict.CODE_SEAL, CodeVerdict.CODE_PARTIAL]
        }
    
    async def _agi_architectural_foundation(self, requirements: str, constraints: List[str], language: str, complexity_level: str) -> Dict:
        """AGI architectural foundation for code generation"""
        
        # Analyze requirements architecturally
        architectural_requirements = self._extract_architectural_requirements(requirements)
        
        # Design constitutional architecture
        architecture = self._design_constitutional_architecture(architectural_requirements, constraints, language)
        
        # Generate architectural blueprint
        blueprint = self._generate_architectural_blueprint(architecture, complexity_level)
        
        return {
            "architectural_requirements": architectural_requirements,
            "architecture": architecture,
            "blueprint": blueprint,
            "modularity_score": architecture.get("modularity_score", 0.8),
            "scalability_score": architecture.get("scalability_score", 0.7),
            "agi_insights": [
                f"AGI Architectural Foundation: {language}",
                f"Complexity level: {complexity_level}",
                f"Modularity score: {architecture.get('modularity_score', 0.8):.2f}",
                f"Scalability score: {architecture.get('scalability_score', 0.7):.2f}"
            ]
        }
    
    async def _asi_empathetic_constraints(self, agi_foundation: Dict, constraints: List[str], context: Optional[Dict]) -> Dict:
        """ASI empathetic constraints application"""
        
        # Analyze stakeholder impact of architecture
        stakeholder_impact = self._analyze_architecture_stakeholder_impact(agi_foundation["architecture"])
        
        # Apply empathy-driven constraints
        empathetic_constraints = self._apply_empathetic_constraints(constraints, stakeholder_impact)
        
        # Validate weakest stakeholder protection
        weakest_protection = self._validate_weakest_stakeholder_in_architecture(agi_foundation["architecture"], stakeholder_impact)
        
        return {
            "stakeholder_impact": stakeholder_impact,
            "empathetic_constraints": empathetic_constraints,
            "weakest_stakeholder_protected": weakest_protection["protected"],
            "kappa_r": weakest_protection["kappa_r"],
            "empathy_score": weakest_protection["empathy_score"],
            "safety_constraints": empathetic_constraints.get("safety_constraints", []),
            "accessibility_constraints": empathetic_constraints.get("accessibility_constraints", [])
        }
    
    async def _codex_generation_with_apex_judgment(self, agi_foundation: Dict, asi_constraints: Dict, language: str, complexity_level: str) -> ConstitutionalCodeGeneration:
        """Codex generation with final APEX constitutional judgment"""
        
        # Generate code with constitutional constraints
        generated_code = await self._generate_constitutional_code(
            agi_foundation,
            asi_constraints,
            language,
            complexity_level
        )
        
        # Apply constitutional headers and documentation
        constitutional_headers = self._apply_constitutional_headers(generated_code, language)
        
        # Calculate complexity and clarity scores
        complexity_score = self._calculate_generated_code_complexity(generated_code)
        clarity_score = self._calculate_generated_code_clarity(generated_code)
        
        # Trinity validation
        trinity_validation = await self._validate_trinity_synthesis(agi_foundation, asi_constraints, generated_code)
        
        # APEX final judgment
        apex_judgment = await self._apex_final_judgment_on_code(
            generated_code,
            complexity_score,
            clarity_score,
            trinity_validation
        )
        
        return ConstitutionalCodeGeneration(
            verdict=apex_judgment["verdict"],
            generated_code=generated_code,
            constitutional_headers=constitutional_headers,
            complexity_score=complexity_score,
            clarity_score=clarity_score,
            trinity_validation=trinity_validation,
            constraints_applied=asi_constraints["empathetic_constraints"],
            generation_metrics=apex_judgment["metrics"]
        )
    
    # Helper methods for constitutional validation
    
    def _calculate_code_clarity(self, code: str) -> float:
        """Calculate F6 Clarity score for code"""
        if not code.strip():
            return 0.0
        
        lines = code.split('\n')
        
        # Clarity factors
        comment_ratio = len([line for line in lines if line.strip().startswith('#')]) / max(1, len(lines))
        docstring_ratio = len(re.findall(r'""".*?"""', code, re.DOTALL)) / max(1, len(lines))
        
        # Complexity factors (negative)
        nesting_depth = self._calculate_nesting_depth(code)
        line_length_avg = sum(len(line) for line in lines) / max(1, len(lines))
        
        # Clarity formula (0.0 to 1.0, higher is better)
        clarity = (
            comment_ratio * 0.3 +
            docstring_ratio * 0.2 +
            (1.0 - min(1.0, nesting_depth / 10)) * 0.3 +
            (1.0 - min(1.0, (line_length_avg - 80) / 80)) * 0.2
        )
        
        return max(0.0, min(1.0, clarity))
    
    def _calculate_nesting_depth(self, code: str) -> int:
        """Calculate maximum nesting depth of code"""
        try:
            tree = ast.parse(code)
            max_depth = 0
            
            for node in ast.walk(tree):
                if isinstance(node, (ast.If, ast.For, ast.While, ast.Try, ast.With, ast.FunctionDef, ast.ClassDef)):
                    depth = self._get_node_depth(node)
                    max_depth = max(max_depth, depth)
            
            return max_depth
        except SyntaxError:
            return 10  # High depth for invalid syntax
    
    def _get_node_depth(self, node) -> int:
        """Get nesting depth of AST node"""
        depth = 0
        current = node
        while hasattr(current, 'parent') and current.parent:
            depth += 1
            current = current.parent
        return depth
    
    def _detect_security_vulnerabilities(self, code: str) -> List[Dict]:
        """Detect security vulnerabilities for ASI validation"""
        vulnerabilities = []
        
        for pattern in self.security_patterns:
            matches = re.finditer(pattern, code, re.IGNORECASE)
            for match in matches:
                vulnerabilities.append({
                    "type": "security_vulnerability",
                    "pattern": pattern,
                    "location": match.span(),
                    "severity": "high",
                    "recommendation": f"Avoid using {pattern.strip()} for security reasons"
                })
        
        return vulnerabilities
    
    def _analyze_stakeholder_impact(self, code: str, context: Optional[Dict]) -> Dict:
        """Analyze who might be affected by this code"""
        # Simple stakeholder analysis
        stakeholders = {
            "end_users": 0.8,  # High impact - code affects users
            "developers": 0.6,  # Medium impact - other developers working with code
            "maintainers": 0.7,  # Medium-high impact - future maintainers
            "system": 0.5,  # Medium impact - system resources
            "data": 0.9  # High impact - data handling
        }
        
        # Adjust based on code content
        if "user" in code.lower() or "input" in code.lower():
            stakeholders["end_users"] = 0.95
        
        if "database" in code.lower() or "sql" in code.lower():
            stakeholders["data"] = 0.98
        
        if "api" in code.lower() or "network" in code.lower():
            stakeholders["system"] = 0.8
        
        return stakeholders
    
    def _calculate_weakest_stakeholder_protection(self, code: str, stakeholder_impact: Dict) -> Dict:
        """Calculate κᵣ (empathy conductance) for weakest stakeholder protection"""
        
        # Find weakest stakeholder (highest vulnerability)
        weakest_stakeholder = min(stakeholder_impact.items(), key=lambda x: x[1])
        
        # Calculate protection measures
        protection_measures = self._assess_protection_measures(code, weakest_stakeholder[0])
        
        # κᵣ formula: empathy conductance = protection_score * stakeholder_vulnerability
        kappa_r = protection_measures["score"] * (1.0 - weakest_stakeholder[1])
        
        # Empathy score: how well we protect the most vulnerable
        empathy_score = min(1.0, kappa_r * 2.0)  # Scale to reasonable range
        
        return {
            "protected": kappa_r >= 0.5,
            "kappa_r": kappa_r,
            "empathy_score": empathy_score,
            "weakest_stakeholder": weakest_stakeholder[0],
            "protection_measures": protection_measures
        }
    
    def _assess_protection_measures(self, code: str, stakeholder: str) -> Dict:
        """Assess protection measures for specific stakeholder"""
        
        protection_score = 0.0
        measures = []
        
        if stakeholder == "end_users":
            # Input validation
            if "validate" in code.lower() or "sanitize" in code.lower():
                protection_score += 0.3
                measures.append("Input validation detected")
            
            # Error handling
            if "try:" in code or "except" in code:
                protection_score += 0.2
                measures.append("Error handling present")
            
            # User feedback
            if "user" in code.lower() and ("message" in code.lower() or "feedback" in code.lower()):
                protection_score += 0.2
                measures.append("User feedback mechanisms")
        
        elif stakeholder == "data":
            # Data validation
            if "validate" in code.lower():
                protection_score += 0.3
                measures.append("Data validation present")
            
            # Encryption/Hashing
            if any(word in code.lower() for word in ["encrypt", "hash", "secure"]):
                protection_score += 0.3
                measures.append("Security measures present")
        
        elif stakeholder == "developers":
            # Documentation
            if '"""' in code or "'''" in code:
                protection_score += 0.2
                measures.append("Documentation present")
            
            # Clear naming
            if self._has_clear_naming(code):
                protection_score += 0.2
                measures.append("Clear naming conventions")
        
        return {"score": min(1.0, protection_score), "measures": measures}
    
    def _has_clear_naming(self, code: str) -> bool:
        """Check if code has clear naming conventions"""
        # Simple heuristic - check for meaningful variable/function names
        meaningful_names = len(re.findall(r'\b[a-z_]+\b', code)) > len(re.findall(r'\b[a-z]\b', code))
        return meaningful_names
    
    def _validate_code_truthfulness(self, code: str) -> bool:
        """F2 Truth: Validate code truthfulness"""
        # Check for misleading comments
        comments = re.findall(r'#.*$', code, re.MULTILINE)
        
        # Simple check - comments should not contradict code
        for comment in comments:
            if any(misleading in comment.lower() for misleading in ["always", "never", "impossible"]):
                return False  # Potentially misleading
        
        return True
    
    def _validate_code_peacefulness(self, code: str, asi_validation: Dict) -> bool:
        """F3 Peace: Validate code peacefulness"""
        # Check stakeholder protection from ASI validation
        return asi_validation.get("weakest_stakeholder_protected", False)
    
    def _validate_code_clarity(self, code: str) -> bool:
        """F4 Clarity: Validate code clarity (DS >= 0.0)"""
        clarity_score = self._calculate_code_clarity(code)
        return clarity_score >= 0.3  # Minimum clarity threshold
    
    def _validate_code_complexity(self, code: str) -> bool:
        """F6 Clarity: Validate code complexity"""
        complexity_factors = self._calculate_complexity_factors(code)
        return complexity_factors["overall_complexity"] <= 0.8  # Reasonable complexity
    
    def _calculate_complexity_factors(self, code: str) -> Dict:
        """Calculate various complexity factors"""
        lines = code.split('\n')
        
        # Cyclomatic complexity approximation
        branch_points = len(re.findall(r'\b(if|elif|else|for|while|except|finally)\b', code))
        
        # Nesting complexity
        nesting_complexity = self._calculate_nesting_complexity(code)
        
        # Length complexity
        length_complexity = len(code) / 1000  # Normalize
        
        # Overall complexity (0-1 scale)
        overall_complexity = min(1.0, (branch_points * 0.1 + nesting_complexity * 0.3 + length_complexity * 0.2))
        
        return {
            "branch_points": branch_points,
            "nesting_complexity": nesting_complexity,
            "length_complexity": length_complexity,
            "overall_complexity": overall_complexity
        }
    
    def _calculate_nesting_complexity(self, code: str) -> float:
        """Calculate nesting complexity"""
        try:
            tree = ast.parse(code)
            max_nesting = 0
            
            for node in ast.walk(tree):
                if isinstance(node, (ast.If, ast.For, ast.While, ast.Try)):
                    nesting = self._calculate_node_nesting(node)
                    max_nesting = max(max_nesting, nesting)
            
            return min(1.0, max_nesting / 10)  # Normalize
        except SyntaxError:
            return 1.0  # High complexity for invalid syntax
    
    def _calculate_node_nesting(self, node) -> int:
        """Calculate nesting level of AST node"""
        nesting = 0
        current = node
        
        # Walk up the tree to count nesting
        while hasattr(current, 'parent') and current.parent:
            if isinstance(current.parent, (ast.If, ast.For, ast.While, ast.Try)):
                nesting += 1
            current = current.parent
        
        return nesting
    
    def _validate_tri_witness_compliance(self, code: str, agi_analysis: Dict, asi_validation: Dict) -> bool:
        """F8 Tri-Witness: Validate three-witness consensus"""
        # Check that AGI, ASI, and code itself are in consensus
        agi_score = agi_analysis.get("architectural_score", 0)
        asi_score = asi_validation.get("empathy_score", 0)
        code_clarity = self._calculate_code_clarity(code)
        
        # Consensus threshold
        consensus_score = (agi_score + asi_score + code_clarity) / 3
        return consensus_score >= 0.6
    
    def _validate_anti_hantu_compliance(self, code: str) -> bool:
        """F9 Anti-Hantu: Validate no circular dependencies"""
        # Check for circular import patterns
        circular_patterns = [
            r'from \. import',  # Relative imports
            r'import.*\n.*from.*import.*\n.*import',  # Complex import chains
        ]
        
        for pattern in circular_patterns:
            if re.search(pattern, code, re.MULTILINE | re.IGNORECASE):
                return False
        
        return True
    
    def _validate_symbolic_meaning(self, code: str) -> bool:
        """F10 Symbolic: Validate symbolic meaning"""
        # Check for meaningful variable/function names
        # Extract all identifiers
        identifiers = re.findall(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b', code)
        
        # Filter out Python keywords and built-ins
        python_keywords = {'def', 'class', 'if', 'else', 'for', 'while', 'try', 'except', 'import', 'from', 'return', 'pass', 'break', 'continue'}
        meaningful_identifiers = [id for id in identifiers if id not in python_keywords and len(id) > 1]
        
        # Check for meaningful names (not just a, b, c, etc.)
        meaningful_names = [id for id in meaningful_identifiers if len(id) > 2 and not id.islower()]
        
        return len(meaningful_names) > len(identifiers) * 0.3  # At least 30% meaningful names
    
    def _validate_command_authority(self, code: str) -> bool:
        """F11 Command Auth: Validate command authority"""
        # Check for dangerous system commands
        dangerous_commands = [
            r'os\.system\s*\(',
            r'subprocess\.call\s*\(',
            r'eval\s*\(',
            r'exec\s*\('
        ]
        
        for pattern in dangerous_commands:
            if re.search(pattern, code):
                return False  # Dangerous command usage detected
        
        return True
    
    def _validate_injection_defense(self, code: str) -> bool:
        """F12 Injection: Validate injection defense"""
        # Check for SQL injection vulnerabilities
        sql_patterns = [
            r'\.execute\s*\(\s*["\'].*%.*["\']',  # String formatting in SQL
            r'\.execute\s*\(\s*.*\+.*',  # String concatenation in SQL
        ]
        
        for pattern in sql_patterns:
            if re.search(pattern, code, re.IGNORECASE):
                return False  # Potential SQL injection vulnerability
        
        return True
    
    def _store_analysis_in_vault(self, code: str, analysis: ConstitutionalCodeAnalysis, user_id: str):
        """Store constitutional code analysis in VAULT-999"""
        try:
            asyncio.create_task(self._async_store_analysis(code, analysis, user_id))
        except Exception as e:
            logging.error(f"Failed to schedule analysis storage: {e}")
    
    async def _async_store_analysis(self, code: str, analysis: ConstitutionalCodeAnalysis, user_id: str):
        """Async storage of analysis in VAULT-999"""
        try:
            insight_text = f"Constitutional code analysis: {analysis_type} analysis with verdict {analysis.verdict}"
            
            vault_result = await vault999_store(
                insight_text=insight_text,
                structure=f"Code analysis with constitutional scores: security={analysis.security_score:.2f}, performance={analysis.performance_score:.2f}, architectural={analysis.architectural_score:.2f}",
                truth_boundary=f"Code constitutional compliance verified with overall score: {analysis.metrics.get('overall_score', 0):.2f}",
                scar="Code required full trinity validation before constitutional sealing",
                vault_target="BBB",  # Memory band
                user_id=user_id
            )
            
            logging.info(f"Stored code analysis in VAULT-999: {vault_result}")
            
        except Exception as e:
            logging.error(f"Failed to store analysis in VAULT-999: {e}")


# Decorator for constitutional tool registration
# Function constitutional_tool breakdown suggested - F6 Clarity
def constitutional_tool(*args, **kwargs):
    """Constitutional function - F2 Truth enforced"""
    """Constitutional function - F6 Clarity enforced"""
    """Constitutional function - F2 Truth enforced"""
    """Constitutional function - F6 Clarity enforced"""
    return self._broken_down_function(*args, **kwargs)
    asyncio.run(test_codex_skills())