"""LAIN ↔ Supercharged Glyph Bridge Connects the Supercharged Glyph Registry (LedoGlyph600) to the LAIN cognition engine, injecting glyph metadata, frequency signatures, lineage, activation envelopes, and resonance profiles into the cognition process. """ from typing import Optional, Dict, Any, List from glyphs.super_registry import get_super, search_super, list_super_ids def load_glyph_context(manifest: dict, context: dict) -> dict: """Load glyph context relevant to the GX file. Determines which glyph(s) are relevant by: 1. Checking manifest["glyph_id"] if present 2. Searching glyphs by manifest["glyphs"] if present 3. Searching glyphs by manifest["tags"] if present 4. Falling back to default context if no glyph found Args: manifest: GX manifest dict context: Execution context dict Returns: Dict with glyph metadata and context fields: { "id": str, "name": str, "category": str, "score": int, "praw": dict, "originalMetrics": dict, "activation": dict, "lineage": dict, "found": bool, "_raw": dict, } """ # Try to find glyph by explicit ID glyph_id = manifest.get("glyph_id") if glyph_id: glyph = get_super(glyph_id) if glyph: return _build_glyph_context(glyph, found=True) # Try to find by "glyphs" field in manifest glyphs_list = manifest.get("glyphs") if glyphs_list and isinstance(glyphs_list, list) and glyphs_list: glyph_id = glyphs_list[0] glyph = get_super(glyph_id) if glyph: return _build_glyph_context(glyph, found=True) # Try to find by tags tags = manifest.get("tags", []) if tags: for tag in tags: results = search_super(tag, limit=1) if results: return _build_glyph_context(results[0], found=True) # Return default context if no glyph found return _build_glyph_context(None, found=False) def _build_glyph_context(glyph: Optional[dict], found: bool) -> dict: """Build a normalized glyph context dict. Args: glyph: Glyph dict from registry, or None found: Whether a glyph was found Returns: Normalized glyph context dict """ if glyph and found: return { "id": glyph.get("id", "unknown"), "name": glyph.get("name", "unknown"), "category": glyph.get("category", "unknown"), "score": glyph.get("score", 0), "praw": glyph.get("praw", {}), "originalMetrics": glyph.get("originalMetrics", {}), "activation": glyph.get("activation", {}), "lineage": glyph.get("lineage", {}), "routing": glyph.get("routing", {}), "storage": glyph.get("storage", {}), "governance": glyph.get("governance", {}), "period": glyph.get("period"), "band": glyph.get("band"), "found": True, "_raw": glyph, } else: return { "id": "none", "name": "none", "category": "none", "score": 0, "praw": {}, "originalMetrics": {}, "activation": {}, "lineage": {}, "routing": {}, "storage": {}, "governance": {}, "period": None, "band": None, "found": False, "_raw": None, } def inject_glyph_metadata_into_lane( lane_result: dict, glyph_context: dict, ) -> dict: """Inject glyph metadata into a lane result. Adds glyph fields to the lane result without overwriting existing fields. Extends lane_result with: - glyph_id - glyph_name - glyph_category - glyph_frequency_signature (praw) - glyph_activation_mode - glyph_lineage_signature - glyph_symbolic_anatomy (originalMetrics) Args: lane_result: Lane result dict from lane processor glyph_context: Glyph context from load_glyph_context() Returns: Extended lane_result dict with glyph metadata """ if not glyph_context.get("found"): # No glyph context, return unchanged return lane_result # Create extended result extended = dict(lane_result) # Add glyph metadata extended["glyph_id"] = glyph_context["id"] extended["glyph_name"] = glyph_context["name"] extended["glyph_category"] = glyph_context["category"] extended["glyph_score"] = glyph_context["score"] # Add frequency signature praw = glyph_context.get("praw", {}) if praw: extended["glyph_frequency_signature"] = praw # Add activation mode and score activation = glyph_context.get("activation", {}) if activation: extended["glyph_activation_mode"] = activation.get("currentMode", "unknown") extended["glyph_activation_score"] = activation.get("score", 0) # Add lineage signature lineage = glyph_context.get("lineage", {}) if lineage: extended["glyph_lineage_signature"] = lineage.get("signature", "unknown") extended["glyph_inheritance_weight"] = lineage.get("inheritanceWeight", 0) # Add symbolic anatomy metrics = glyph_context.get("originalMetrics", {}) if metrics: extended["glyph_symbolic_anatomy"] = metrics return extended def compute_glyph_resonance(glyph_context: dict) -> dict: """Compute glyph-level resonance metrics. Combines multiple glyph measurements into unified resonance metrics: - activation_resonance: from activation.score - frequency_resonance: from praw vector magnitude - symbolic_resonance: from originalMetrics resonance field - overall_resonance: combined normalized value Args: glyph_context: Glyph context from load_glyph_context() Returns: Dict with resonance metrics """ if not glyph_context.get("found"): return { "activation_resonance": 0.0, "frequency_resonance": 0.0, "symbolic_resonance": 0.0, "overall_resonance": 0.0, "glyph_found": False, } # Activation resonance from activation.score (0-100 scale) activation = glyph_context.get("activation", {}) activation_score = activation.get("score", 0) activation_resonance = min(1.0, activation_score / 100.0) # Frequency resonance from praw vector magnitude praw = glyph_context.get("praw", {}) praw_values = [float(praw.get(k, 0)) for k in ["P", "R", "A", "W"]] praw_magnitude = (sum(v * v for v in praw_values) ** 0.5) / 100.0 frequency_resonance = min(1.0, praw_magnitude) # Symbolic resonance from originalMetrics metrics = glyph_context.get("originalMetrics", {}) symbolic_resonance_val = metrics.get("resonance", 50) symbolic_resonance = min(1.0, symbolic_resonance_val / 100.0) # Combined overall resonance overall_resonance = ( activation_resonance * 0.4 + frequency_resonance * 0.3 + symbolic_resonance * 0.3 ) return { "activation_resonance": round(activation_resonance, 4), "frequency_resonance": round(frequency_resonance, 4), "symbolic_resonance": round(symbolic_resonance, 4), "overall_resonance": round(overall_resonance, 4), "glyph_found": True, "glyph_id": glyph_context["id"], "glyph_score": glyph_context["score"], } def augment_fused_symbol_with_glyphs( fused_symbol: dict, glyph_context: dict, ) -> dict: """Augment fused symbol with glyph metadata. Adds glyph context fields to the final fused symbol: - glyph_id - glyph_name - glyph_category - glyph_score - glyph_activation_mode - glyph_resonance - glyph_lineage_signature Args: fused_symbol: Fused symbol from fuse_lanes() glyph_context: Glyph context from load_glyph_context() Returns: Augmented fused_symbol dict """ # Create extended symbol augmented = dict(fused_symbol) if glyph_context.get("found"): # Add glyph metadata augmented["glyph_id"] = glyph_context["id"] augmented["glyph_name"] = glyph_context["name"] augmented["glyph_category"] = glyph_context["category"] augmented["glyph_score"] = glyph_context["score"] # Add activation mode activation = glyph_context.get("activation", {}) if activation: augmented["glyph_activation_mode"] = activation.get("currentMode", "unknown") # Add lineage signature lineage = glyph_context.get("lineage", {}) if lineage: augmented["glyph_lineage_signature"] = lineage.get("signature", "unknown") # Add key glyph points to key_points if "key_points" in augmented: glyph_key_points = [ f"glyph:{glyph_context['id']}", f"category:{glyph_context['category']}", ] augmented["key_points"] = augmented.get("key_points", []) + glyph_key_points else: # Mark that no glyph was found augmented["glyph_id"] = "none" augmented["glyph_name"] = "none" augmented["glyph_category"] = "none" augmented["glyph_score"] = 0 augmented["glyph_found"] = False return augmented