2026-05-21 01:27:49 -04:00
|
|
|
"""Symbolic Pipeline Abstraction for XIC.
|
|
|
|
|
|
|
|
|
|
Provides a structured, glyph-aware pipeline for symbolic cognition execution.
|
2026-05-21 02:21:44 -04:00
|
|
|
Routes prompts through the LAIN 8-lane cognition kernel with explicit step tracking
|
|
|
|
|
and comprehensive glyph resonance metrics.
|
2026-05-21 01:27:49 -04:00
|
|
|
"""
|
|
|
|
|
|
2026-07-09 12:54:44 -04:00
|
|
|
import logging
|
2026-05-21 01:27:49 -04:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
2026-07-09 12:54:44 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-05-21 01:27:49 -04:00
|
|
|
|
2026-05-21 02:21:44 -04:00
|
|
|
@dataclass
|
|
|
|
|
class GlyphResonanceMetrics:
|
|
|
|
|
"""Glyph resonance metrics from LAIN cognition layer."""
|
|
|
|
|
weight: float
|
|
|
|
|
lineage_score: float
|
|
|
|
|
contributor_score: float
|
|
|
|
|
frequency_score: float
|
|
|
|
|
grammar_score: float
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class GlyphResonanceMap:
|
|
|
|
|
"""Maps glyph IDs to their resonance metrics."""
|
|
|
|
|
resonances: Dict[str, GlyphResonanceMetrics] = field(default_factory=dict)
|
|
|
|
|
global_resonance_score: float = 0.0
|
|
|
|
|
|
|
|
|
|
def get_glyph_resonance(self, glyph_id: str) -> Optional[GlyphResonanceMetrics]:
|
|
|
|
|
"""Get resonance metrics for a specific glyph."""
|
|
|
|
|
return self.resonances.get(glyph_id)
|
|
|
|
|
|
|
|
|
|
def get_top_glyphs(self, n: int = 5) -> List[tuple[str, GlyphResonanceMetrics]]:
|
|
|
|
|
"""Get top N glyphs by weight."""
|
|
|
|
|
sorted_glyphs = sorted(
|
|
|
|
|
self.resonances.items(),
|
|
|
|
|
key=lambda x: x[1].weight,
|
|
|
|
|
reverse=True
|
|
|
|
|
)
|
|
|
|
|
return sorted_glyphs[:n]
|
|
|
|
|
|
|
|
|
|
def get_average_resonance(self) -> float:
|
|
|
|
|
"""Get average resonance across all glyphs."""
|
|
|
|
|
if not self.resonances:
|
|
|
|
|
return 0.0
|
|
|
|
|
total = sum(m.weight for m in self.resonances.values())
|
|
|
|
|
return total / len(self.resonances)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class FusedSymbol:
|
|
|
|
|
"""Fused symbolic representation from LAIN cognition."""
|
|
|
|
|
summary: str
|
|
|
|
|
glyph_ids: List[str] = field(default_factory=list)
|
|
|
|
|
resonance_map: GlyphResonanceMap = field(default_factory=GlyphResonanceMap)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def from_lain_result(cls, lain_fused_symbol: Dict[str, Any]) -> "FusedSymbol":
|
|
|
|
|
"""Parse fused_symbol dict from LAIN result."""
|
|
|
|
|
summary = lain_fused_symbol.get("summary", "")
|
|
|
|
|
glyph_ids = lain_fused_symbol.get("glyph_ids", [])
|
|
|
|
|
|
|
|
|
|
resonance_map = GlyphResonanceMap(
|
|
|
|
|
global_resonance_score=lain_fused_symbol.get("global_resonance_score", 0.0)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
raw_resonance = lain_fused_symbol.get("resonance_map", {})
|
|
|
|
|
for glyph_id, metrics_dict in raw_resonance.items():
|
|
|
|
|
if isinstance(metrics_dict, dict):
|
|
|
|
|
resonance_map.resonances[glyph_id] = GlyphResonanceMetrics(
|
|
|
|
|
weight=metrics_dict.get("weight", 0.0),
|
|
|
|
|
lineage_score=metrics_dict.get("lineage_score", 0.0),
|
|
|
|
|
contributor_score=metrics_dict.get("contributor_score", 0.0),
|
|
|
|
|
frequency_score=metrics_dict.get("frequency_score", 0.0),
|
|
|
|
|
grammar_score=metrics_dict.get("grammar_score", 0.0),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return cls(summary=summary, glyph_ids=glyph_ids, resonance_map=resonance_map)
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 01:27:49 -04:00
|
|
|
@dataclass
|
|
|
|
|
class SymbolicStep:
|
|
|
|
|
"""A single step in the symbolic pipeline execution."""
|
|
|
|
|
name: str
|
|
|
|
|
kind: str # "prompt", "glyph_call", "fused_symbol"
|
|
|
|
|
payload: Any
|
|
|
|
|
context: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class SymbolicPipelineResult:
|
|
|
|
|
"""Result of a symbolic pipeline execution."""
|
|
|
|
|
steps: List[SymbolicStep]
|
|
|
|
|
output_text: str
|
2026-05-21 02:21:44 -04:00
|
|
|
fused_symbol: Optional[FusedSymbol] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_glyph_resonances(
|
|
|
|
|
pipeline_result: "SymbolicPipelineResult",
|
|
|
|
|
) -> Dict[str, Dict[str, Any]]:
|
|
|
|
|
"""Extract glyph resonance metrics from a pipeline result.
|
|
|
|
|
|
|
|
|
|
Returns dict mapping glyph_id → resonance metrics dict.
|
|
|
|
|
"""
|
|
|
|
|
if not pipeline_result.fused_symbol:
|
|
|
|
|
return {}
|
|
|
|
|
|
2026-05-21 02:29:22 -04:00
|
|
|
if not pipeline_result.fused_symbol.resonance_map:
|
|
|
|
|
return {}
|
|
|
|
|
|
2026-05-21 02:21:44 -04:00
|
|
|
result = {}
|
|
|
|
|
for glyph_id, metrics in pipeline_result.fused_symbol.resonance_map.resonances.items():
|
|
|
|
|
result[glyph_id] = {
|
|
|
|
|
"weight": metrics.weight,
|
|
|
|
|
"lineage_score": metrics.lineage_score,
|
|
|
|
|
"contributor_score": metrics.contributor_score,
|
|
|
|
|
"frequency_score": metrics.frequency_score,
|
|
|
|
|
"grammar_score": metrics.grammar_score,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_dominant_glyphs(
|
|
|
|
|
pipeline_result: "SymbolicPipelineResult",
|
|
|
|
|
n: int = 3,
|
|
|
|
|
) -> List[tuple[str, float]]:
|
|
|
|
|
"""Get top N glyphs by resonance weight from a pipeline result.
|
|
|
|
|
|
|
|
|
|
Returns list of (glyph_id, weight) tuples sorted by weight descending.
|
|
|
|
|
"""
|
|
|
|
|
if not pipeline_result.fused_symbol:
|
|
|
|
|
return []
|
|
|
|
|
|
2026-05-21 02:29:22 -04:00
|
|
|
if not pipeline_result.fused_symbol.resonance_map:
|
|
|
|
|
return []
|
|
|
|
|
|
2026-05-21 02:21:44 -04:00
|
|
|
return [
|
|
|
|
|
(glyph_id, metrics.weight)
|
|
|
|
|
for glyph_id, metrics in pipeline_result.fused_symbol.resonance_map.get_top_glyphs(n)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_glyph_resonance_report(
|
|
|
|
|
pipeline_result: "SymbolicPipelineResult",
|
|
|
|
|
) -> str:
|
|
|
|
|
"""Format a human-readable glyph resonance report."""
|
|
|
|
|
if not pipeline_result.fused_symbol:
|
|
|
|
|
return "No glyph resonance data."
|
|
|
|
|
|
2026-05-21 02:29:22 -04:00
|
|
|
if not pipeline_result.fused_symbol.resonance_map:
|
|
|
|
|
return "No resonance map available."
|
|
|
|
|
|
2026-05-21 02:21:44 -04:00
|
|
|
resonance = pipeline_result.fused_symbol.resonance_map
|
|
|
|
|
lines = [
|
|
|
|
|
f"Global Resonance Score: {resonance.global_resonance_score:.3f}",
|
|
|
|
|
f"Glyphs Engaged: {len(resonance.resonances)}",
|
|
|
|
|
"",
|
|
|
|
|
"Top Glyphs by Weight:",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for glyph_id, metrics in resonance.get_top_glyphs(5):
|
|
|
|
|
lines.append(
|
|
|
|
|
f" {glyph_id}: weight={metrics.weight:.3f}, "
|
|
|
|
|
f"lineage={metrics.lineage_score:.3f}, "
|
|
|
|
|
f"contributor={metrics.contributor_score:.3f}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return "\n".join(lines)
|
2026-05-21 01:27:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_symbolic_pipeline(
|
|
|
|
|
prompt: str,
|
|
|
|
|
context: Optional[Dict[str, Any]] = None,
|
|
|
|
|
glyph_id: Optional[str] = None,
|
2026-05-21 02:29:22 -04:00
|
|
|
glyph_ids: Optional[List[str]] = None,
|
2026-05-21 01:27:49 -04:00
|
|
|
) -> SymbolicPipelineResult:
|
|
|
|
|
"""
|
|
|
|
|
High-level symbolic pipeline entrypoint for XIC.
|
|
|
|
|
|
|
|
|
|
Accepts a prompt and optional symbolic/glyph context, routes through
|
|
|
|
|
the LAIN 8-lane cognition kernel via CognitiveKernel.execute_symbolic(),
|
|
|
|
|
and returns a structured SymbolicPipelineResult with execution steps,
|
|
|
|
|
final output text, and fused symbolic representation.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
prompt: User or system prompt text.
|
|
|
|
|
context: Optional dict of symbolic/cognitive context metadata.
|
2026-05-21 02:29:22 -04:00
|
|
|
glyph_id: Optional glyph identifier for single-glyph cognition.
|
|
|
|
|
glyph_ids: Optional list of glyph identifiers for multi-glyph resonance.
|
2026-05-21 01:27:49 -04:00
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
SymbolicPipelineResult with:
|
|
|
|
|
- steps: List of SymbolicStep objects tracking execution flow.
|
|
|
|
|
- output_text: Final text result from cognition layer.
|
|
|
|
|
- fused_symbol: Fused symbolic representation (if produced by LAIN).
|
2026-05-21 02:29:22 -04:00
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
If both glyph_id and glyph_ids are provided, glyph_ids takes precedence
|
|
|
|
|
for multi-glyph resonance computation.
|
2026-05-21 01:27:49 -04:00
|
|
|
"""
|
|
|
|
|
from gx_compiler.compressor import GXCompressor
|
|
|
|
|
from .cognitive_kernel import get_kernel
|
|
|
|
|
|
|
|
|
|
steps: List[SymbolicStep] = []
|
|
|
|
|
kernel = get_kernel()
|
|
|
|
|
prompt_bytes = prompt.encode("utf-8")
|
|
|
|
|
|
|
|
|
|
# Step 1: Initial prompt
|
|
|
|
|
steps.append(SymbolicStep(
|
|
|
|
|
name="initial_prompt",
|
|
|
|
|
kind="prompt",
|
|
|
|
|
payload=prompt,
|
|
|
|
|
context=dict(context or {})
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
# Step 2: Prepare context for glyph-aware processing
|
|
|
|
|
exec_context = dict(context or {})
|
2026-05-21 02:29:22 -04:00
|
|
|
guardrails_triggered = []
|
|
|
|
|
|
|
|
|
|
# Multi-glyph resonance takes precedence
|
|
|
|
|
if glyph_ids:
|
|
|
|
|
# Apply guardrails
|
|
|
|
|
max_glyphs = exec_context.get("max_resonance_glyphs", 10)
|
|
|
|
|
if len(glyph_ids) > max_glyphs:
|
|
|
|
|
glyph_ids = glyph_ids[:max_glyphs]
|
|
|
|
|
guardrails_triggered.append(f"Truncated glyph list to {max_glyphs}")
|
|
|
|
|
|
|
|
|
|
exec_context["glyph_ids"] = glyph_ids
|
|
|
|
|
steps.append(SymbolicStep(
|
|
|
|
|
name="multi_glyph_resonance",
|
|
|
|
|
kind="multi_glyph_resonance",
|
|
|
|
|
payload={"glyph_ids": glyph_ids, "count": len(glyph_ids)},
|
|
|
|
|
context=exec_context
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
# Record guardrail step if triggered
|
|
|
|
|
if guardrails_triggered:
|
|
|
|
|
steps.append(SymbolicStep(
|
|
|
|
|
name="guardrail_enforcement",
|
|
|
|
|
kind="guardrail",
|
|
|
|
|
payload={"guardrails": guardrails_triggered},
|
|
|
|
|
context={"max_resonance_glyphs": max_glyphs}
|
|
|
|
|
))
|
|
|
|
|
elif glyph_id:
|
2026-05-21 01:27:49 -04:00
|
|
|
exec_context["glyph_id"] = glyph_id
|
|
|
|
|
steps.append(SymbolicStep(
|
|
|
|
|
name=f"glyph:{glyph_id}",
|
|
|
|
|
kind="glyph_call",
|
|
|
|
|
payload=prompt,
|
|
|
|
|
context=exec_context
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
# Step 3: Compress prompt and build manifest
|
|
|
|
|
try:
|
|
|
|
|
payload = GXCompressor.compress(prompt)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return SymbolicPipelineResult(
|
|
|
|
|
steps=steps,
|
|
|
|
|
output_text=f"[Pipeline Error] Compression failed: {e}",
|
|
|
|
|
fused_symbol=None
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
manifest = {
|
|
|
|
|
"source_file": "<symbolic>",
|
|
|
|
|
"source_type": "symbolic",
|
|
|
|
|
"version": "1.0.0",
|
|
|
|
|
"contributor": "XIC-symbolic",
|
|
|
|
|
"segments": [{"id": "seg_0", "start": 0, "end": 1,
|
|
|
|
|
"start_byte": 0, "end_byte": len(prompt_bytes)}],
|
|
|
|
|
}
|
|
|
|
|
segments = [{"id": "seg_0", "start": 0, "end": 1,
|
|
|
|
|
"start_byte": 0, "end_byte": len(prompt_bytes)}]
|
|
|
|
|
|
|
|
|
|
# Step 4: Execute through LAIN cognition pipeline
|
|
|
|
|
result = kernel.execute_symbolic(
|
|
|
|
|
manifest=manifest,
|
|
|
|
|
segments=segments,
|
|
|
|
|
payload=payload,
|
|
|
|
|
mode="symbolic",
|
|
|
|
|
context=exec_context,
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-21 02:21:44 -04:00
|
|
|
# Step 5: Extract and parse results
|
|
|
|
|
lain_fused_symbol = result.get("fused_symbol")
|
|
|
|
|
fused_symbol = None
|
|
|
|
|
|
|
|
|
|
if lain_fused_symbol:
|
|
|
|
|
fused_symbol = FusedSymbol.from_lain_result(lain_fused_symbol)
|
|
|
|
|
output_text = result.get("output_text") or fused_symbol.summary
|
|
|
|
|
else:
|
|
|
|
|
output_text = result.get("output_text") or prompt
|
2026-05-21 01:27:49 -04:00
|
|
|
|
|
|
|
|
# Step 6: Record fusion step if fused_symbol present
|
|
|
|
|
if fused_symbol:
|
|
|
|
|
steps.append(SymbolicStep(
|
|
|
|
|
name="fusion",
|
|
|
|
|
kind="fused_symbol",
|
2026-05-21 02:21:44 -04:00
|
|
|
payload={
|
|
|
|
|
"summary": fused_symbol.summary,
|
|
|
|
|
"glyph_ids": fused_symbol.glyph_ids,
|
|
|
|
|
"global_resonance_score": fused_symbol.resonance_map.global_resonance_score,
|
|
|
|
|
},
|
2026-05-21 01:27:49 -04:00
|
|
|
context={}
|
|
|
|
|
))
|
|
|
|
|
|
2026-05-21 02:40:10 -04:00
|
|
|
# Build telemetry for FedMart integration
|
|
|
|
|
try:
|
|
|
|
|
from integrations.fedmart.xic_adapter import emit_telemetry
|
2026-07-09 12:54:44 -04:00
|
|
|
from integrations.fedmart.glyph_telemetry import emit_glyph_activation
|
2026-05-21 02:40:10 -04:00
|
|
|
import time
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
top_glyphs = []
|
|
|
|
|
avg_resonance = 0.0
|
|
|
|
|
|
|
|
|
|
if fused_symbol and fused_symbol.resonance_map:
|
|
|
|
|
top_glyphs = [
|
|
|
|
|
{"glyph_id": glyph_id, "weight": metrics.weight}
|
|
|
|
|
for glyph_id, metrics in fused_symbol.resonance_map.get_top_glyphs(5)
|
|
|
|
|
]
|
|
|
|
|
avg_resonance = fused_symbol.resonance_map.get_average_resonance()
|
|
|
|
|
|
2026-07-09 12:54:44 -04:00
|
|
|
# Emit standard XIC telemetry
|
2026-05-21 02:40:10 -04:00
|
|
|
telemetry = {
|
|
|
|
|
"event_type": "symbolic_pipeline_run",
|
|
|
|
|
"timestamp": datetime.utcnow().isoformat() + "Z",
|
|
|
|
|
"program": exec_context.get("program", "<unknown>"),
|
|
|
|
|
"chain_label": exec_context.get("chain_label"),
|
|
|
|
|
"glyph_ids": fused_symbol.glyph_ids if fused_symbol else [],
|
|
|
|
|
"glyph_count": len(fused_symbol.glyph_ids) if fused_symbol else 0,
|
|
|
|
|
"global_resonance_score": fused_symbol.resonance_map.global_resonance_score
|
|
|
|
|
if (fused_symbol and fused_symbol.resonance_map)
|
|
|
|
|
else 0.0,
|
|
|
|
|
"steps_executed": len(steps),
|
|
|
|
|
"guardrails_triggered": guardrails_triggered,
|
|
|
|
|
"resonance_map_summary": {
|
|
|
|
|
"top_glyphs": top_glyphs,
|
|
|
|
|
"average_resonance": avg_resonance,
|
|
|
|
|
},
|
|
|
|
|
"raw_payload": {
|
|
|
|
|
"output_text": output_text,
|
|
|
|
|
"fused_symbol_summary": (
|
|
|
|
|
{"summary": fused_symbol.summary, "glyph_ids": fused_symbol.glyph_ids}
|
|
|
|
|
if fused_symbol
|
|
|
|
|
else None
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit_telemetry(telemetry)
|
2026-07-09 12:54:44 -04:00
|
|
|
|
|
|
|
|
# Emit glyph activation telemetry for each engaged glyph
|
|
|
|
|
if fused_symbol and fused_symbol.glyph_ids:
|
|
|
|
|
from glyphs.super_registry import get_super
|
|
|
|
|
for glyph_id in fused_symbol.glyph_ids:
|
|
|
|
|
glyph = get_super(glyph_id)
|
|
|
|
|
if glyph:
|
|
|
|
|
superpower_ids = glyph.get("superpowers", [])
|
|
|
|
|
specialized_type = glyph.get("specialized_type", "")
|
|
|
|
|
metrics = glyph.get("originalMetrics", {})
|
|
|
|
|
|
|
|
|
|
emit_glyph_activation(
|
|
|
|
|
glyph_id=glyph_id,
|
|
|
|
|
superpower_ids=superpower_ids,
|
|
|
|
|
specialized_type=specialized_type,
|
|
|
|
|
metrics=metrics,
|
|
|
|
|
context={"run_id": telemetry.get("run_id")}
|
|
|
|
|
)
|
2026-05-21 02:40:10 -04:00
|
|
|
except ImportError:
|
2026-07-09 12:54:44 -04:00
|
|
|
logger.debug("FedMart integration not available — telemetry emission skipped")
|
2026-05-21 02:40:10 -04:00
|
|
|
|
2026-05-21 01:27:49 -04:00
|
|
|
return SymbolicPipelineResult(
|
|
|
|
|
steps=steps,
|
|
|
|
|
output_text=output_text,
|
|
|
|
|
fused_symbol=fused_symbol
|
|
|
|
|
)
|