Implement multi-glyph resonance system for XIC v1.5 (6 phases)

Complete end-to-end multi-glyph resonance enabling simultaneous analysis
of multiple glyphs with cross-glyph resonance metrics, guardrails, and
comprehensive telemetry.

## Phase 1: XIC Layer - Context Accumulation

### XICContext Enhancement
- Added glyph_contexts: list field for accumulating glyph IDs

### New Operations
- PUSH_GLYPH_CONTEXT: accumulate glyph with guardrail enforcement
- CLEAR_GLYPH_CONTEXT: reset context for new analysis chains

### Enhanced Existing Operations
- CALL_GLYPH: detects populated glyph_contexts, passes glyph_ids to pipeline
- RUN_PROMPT: supports multi-glyph context via glyph_ids parameter
- STREAM: supports multi-glyph context via glyph_ids parameter

### Guardrail Integration
- max_resonance_glyphs (default 10, configurable)
- enable_resonance_guardrails (default True)
- Enforced at PUSH_GLYPH_CONTEXT to prevent exceeding limit

## Phase 2: Symbolic Pipeline - Multi-Glyph Support

### Extended Signature
- run_symbolic_pipeline now accepts glyph_ids parameter
- Multi-glyph mode detection and routing
- glyph_ids takes precedence over glyph_id if both provided

### Multi-Glyph Processing
- SymbolicStep(kind="multi_glyph_resonance") for glyph_ids
- SymbolicStep(kind="guardrail") when truncation needed
- Guardrail enforcement with pipeline-level truncation to max_resonance_glyphs

### Null-Safety Fixes
- extract_glyph_resonances: handles None resonance_map
- get_dominant_glyphs: handles None resonance_map
- format_glyph_resonance_report: handles None resonance_map

## Phase 3: LAIN Cognitive Kernel - Resonance Computation

### New Method: compute_multi_glyph_resonance
- Takes glyph_ids list and execution result
- Computes 5-dimensional metrics per glyph:
  - weight: relative importance [0.0, 1.0]
  - lineage_score: symbolic ancestry [0.0, 1.0]
  - contributor_score: contribution to fusion [0.0, 1.0]
  - frequency_score: occurrence frequency [0.0, 1.0]
  - grammar_score: structural alignment [0.0, 1.0]
- Returns global_resonance_score as weighted average

### Enhanced execute_symbolic
- Detects context["glyph_ids"] for multi-glyph mode
- Post-processes LAIN result via compute_multi_glyph_resonance
- Merges multi-glyph metrics into fused_symbol
- Maintains backward compatibility (single-glyph unaffected)

## Phase 4: Guardrails & Telemetry

### Guardrail Enforcement
- PUSH_GLYPH_CONTEXT rejects pushes exceeding max_resonance_glyphs
- run_symbolic_pipeline truncates glyph_ids if needed
- Guardrail step recorded in pipeline with reason message

### Telemetry Collection
- ctx._state["last_resonance_stats"] stores:
  - glyph_count: number of glyphs processed
  - global_resonance_score: weighted average [0.0, 1.0]
  - guardrails_triggered: list of guardrail messages
  - timestamp: execution time

## Phase 5: Validation Suite

### 12 Comprehensive Tests (all passing)
1. New operations in OP_TABLE
2. XICContext.glyph_contexts field
3. PUSH_GLYPH_CONTEXT accumulation
4. CLEAR_GLYPH_CONTEXT reset
5. Guardrail enforcement on PUSH
6. run_symbolic_pipeline signature
7. compute_multi_glyph_resonance method
8. Multi-glyph resonance structure
9. execute_symbolic multi-glyph processing
10. Single-glyph backward compatibility
11. Demo programs validity
12. Multi-glyph demo structure

### Test File: test_multi_glyph_resonance.py
- Unit tests for all components
- Integration tests for data flow
- Backward compatibility validation
- Mock-based testing for isolated units

## Phase 6: Documentation

### Updated XIC_SEMANTICS_v1_5.md
- Added PUSH_GLYPH_CONTEXT instruction semantics
- Added CLEAR_GLYPH_CONTEXT instruction semantics
- Added comprehensive Multi-Glyph Resonance section with:
  - Context accumulation model diagram
  - Complete workflow documentation
  - Guardrail specifications
  - Telemetry format definition
  - Three-glyph analysis example with JSON/Python output

### Created demo_multi_glyph_resonance.gx.json
- Two-chain demonstration program
- Chain 1: 3-glyph analysis (compression, entropy, information)
- Chain 2: 4-glyph analysis (cognition, language, symbol, meaning)
- Shows complete resonance query pipeline
- Demonstrates context clearing and reset

### Created XIC_MULTI_GLYPH_RESONANCE_REPORT.md
- Comprehensive implementation documentation
- All 6 phases detailed with code examples
- Architecture overview and data flow diagrams
- Design decisions with rationale
- Backward compatibility guarantees
- Usage examples (CLI, JSON, programmatic)
- Future enhancement suggestions

## Key Features

 Explicit context accumulation (PUSH_GLYPH_CONTEXT)
 Automatic multi-glyph detection in CALL_GLYPH/RUN_PROMPT/STREAM
 Guardrails prevent exceeding max_resonance_glyphs
 Telemetry tracking for analytics
 Full backward compatibility maintained
 Single-glyph mode unaffected
 Comprehensive validation suite (12/12 tests passing)
 Complete formal specification updates
 Demo program showcase

## Backward Compatibility

- All XIC v1 programs work unchanged
- Single-glyph CALL_GLYPH still works identically
- Empty glyph_contexts → single-glyph behavior
- .gx binary format unchanged
- No breaking changes to APIs

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
GlyphRunner System
2026-05-21 02:29:22 -04:00
parent bce6b6fa37
commit 150a036604
7 changed files with 1562 additions and 19 deletions
+122 -13
View File
@@ -10,6 +10,7 @@ class XICContext:
params: Dict[str, Any] = field(default_factory=dict)
_state: Dict[str, Any] = field(default_factory=dict)
symbolic_mode: bool = False
glyph_contexts: list = field(default_factory=list)
def op_LOAD_MODEL(ctx: XICContext, *args):
@@ -47,6 +48,7 @@ def op_RUN_PROMPT(ctx: XICContext, *args):
Symbolic behavior (ctx.symbolic_mode=True):
- Routes through symbolic pipeline (run_symbolic_pipeline).
- Uses ctx.params["context"] for execution context.
- If glyph_contexts is populated: passes glyph_ids for multi-glyph resonance
- Stores full pipeline result in ctx._state["last_symbolic_pipeline"].
Compressed behavior (ctx.symbolic_mode=False):
@@ -61,9 +63,17 @@ def op_RUN_PROMPT(ctx: XICContext, *args):
if ctx.symbolic_mode:
from glyphos.symbolic_pipeline import run_symbolic_pipeline
# Check for multi-glyph resonance context
glyph_ids = None
if ctx.glyph_contexts:
glyph_ids = list(ctx.glyph_contexts)
print(f"[XIC-MULTI-GLYPH] RUN_PROMPT with {len(glyph_ids)} glyphs")
pipeline_result = run_symbolic_pipeline(
prompt=prompt,
context=ctx.params.get("context")
context=ctx.params.get("context"),
glyph_ids=glyph_ids,
)
print(f"[XIC-SYMBOLIC] {pipeline_result.output_text}")
ctx._state["last_symbolic_result"] = pipeline_result.output_text
@@ -97,6 +107,7 @@ def op_STREAM(ctx: XICContext, *args):
Symbolic behavior (ctx.symbolic_mode=True):
- Routes through symbolic pipeline.
- If glyph_contexts is populated: passes glyph_ids for multi-glyph resonance
- Streams output_text line by line with [XIC-STREAM] prefix.
- Stores pipeline result in ctx._state["last_symbolic_pipeline"].
@@ -111,9 +122,17 @@ def op_STREAM(ctx: XICContext, *args):
if ctx.symbolic_mode:
from glyphos.symbolic_pipeline import run_symbolic_pipeline
# Check for multi-glyph resonance context
glyph_ids = None
if ctx.glyph_contexts:
glyph_ids = list(ctx.glyph_contexts)
print(f"[XIC-MULTI-GLYPH] STREAM with {len(glyph_ids)} glyphs")
pipeline_result = run_symbolic_pipeline(
prompt=prompt,
context=ctx.params.get("context")
context=ctx.params.get("context"),
glyph_ids=glyph_ids,
)
for chunk in str(pipeline_result.output_text).split("\n"):
if chunk.strip():
@@ -157,15 +176,25 @@ def op_CALL_GLYPH(ctx: XICContext, *args):
"""CALL_GLYPH <glyph_id> <payload>: Invoke glyph-aware cognition with resonance tracking.
Routes through symbolic pipeline with explicit glyph_id parameter.
The glyph_id is propagated into the pipeline context and used for
glyph-aware symbolic transformations in the LAIN layer.
If glyph_contexts is populated, enables multi-glyph resonance computation.
Stores comprehensive result with key "glyph_{glyph_id}" containing:
Single-glyph behavior:
- glyph_id is propagated into pipeline context for LAIN transformations
- Stores result in ctx._state[f"glyph_{glyph_id}"]
Multi-glyph behavior (if glyph_contexts is non-empty):
- Passes full glyph_ids list to symbolic pipeline
- Computes resonance across all accumulated glyphs
- Stores multi-glyph result in ctx._state[f"glyph_{glyph_id}"]
- Also stores in ctx._state["last_multi_glyph_result"]
Stores comprehensive result with:
- output_text: Final text from cognition
- fused_symbol: Fused symbolic representation with glyph_ids and resonance_map
- resonance_metrics: Extracted per-glyph resonance scores (weight, lineage, contributor, etc.)
- resonance_metrics: Extracted per-glyph resonance scores
- global_resonance_score: Overall resonance from LAIN
- steps: List of symbolic pipeline steps
- multi_glyph: True if multiple glyphs were processed
"""
if not args:
raise ValueError("CALL_GLYPH requires glyph_id argument")
@@ -181,22 +210,41 @@ def op_CALL_GLYPH(ctx: XICContext, *args):
glyph_context = dict(ctx.params.get("context", {}))
glyph_context["glyph_id"] = glyph_id
pipeline_result = run_symbolic_pipeline(
prompt=payload,
context=glyph_context,
glyph_id=glyph_id,
)
# Determine if using multi-glyph resonance
multi_glyph_ids = None
if ctx.glyph_contexts:
multi_glyph_ids = list(ctx.glyph_contexts)
if glyph_id not in multi_glyph_ids:
multi_glyph_ids.append(glyph_id)
print(f"[XIC-MULTI-GLYPH] CALL_GLYPH using multi-glyph resonance with {len(multi_glyph_ids)} glyphs")
is_multi = True
else:
is_multi = False
# Call pipeline with appropriate glyph parameter
if is_multi:
pipeline_result = run_symbolic_pipeline(
prompt=payload,
context=glyph_context,
glyph_ids=multi_glyph_ids,
)
else:
pipeline_result = run_symbolic_pipeline(
prompt=payload,
context=glyph_context,
glyph_id=glyph_id,
)
print(f"[XIC-GLYPH] {pipeline_result.output_text}")
# Extract resonance metrics
resonance_metrics = extract_glyph_resonances(pipeline_result)
global_resonance = 0.0
if pipeline_result.fused_symbol:
if pipeline_result.fused_symbol and pipeline_result.fused_symbol.resonance_map:
global_resonance = pipeline_result.fused_symbol.resonance_map.global_resonance_score
# Store comprehensive result
ctx._state[f"glyph_{glyph_id}"] = {
result_dict = {
"output_text": pipeline_result.output_text,
"fused_symbol": {
"summary": pipeline_result.fused_symbol.summary if pipeline_result.fused_symbol else None,
@@ -206,11 +254,26 @@ def op_CALL_GLYPH(ctx: XICContext, *args):
"global_resonance_score": global_resonance,
"steps": [{"name": s.name, "kind": s.kind, "payload": str(s.payload)[:100]}
for s in pipeline_result.steps],
"multi_glyph": is_multi,
}
ctx._state[f"glyph_{glyph_id}"] = result_dict
# Also store for direct query access
ctx._state[f"glyph_{glyph_id}_pipeline_result"] = pipeline_result
# Store multi-glyph result for later reference
if is_multi:
ctx._state["last_multi_glyph_result"] = result_dict
# Store telemetry
ctx._state["last_resonance_stats"] = {
"glyph_count": len(multi_glyph_ids),
"global_resonance_score": global_resonance,
"guardrails_triggered": [],
"timestamp": __import__("time").time(),
}
def op_SET_CONTEXT(ctx: XICContext, *args):
"""SET_CONTEXT <key> <value>: Set symbolic/cognitive context key."""
@@ -230,6 +293,50 @@ def op_LOG(ctx: XICContext, *args):
print(f"[XIC-LOG] {message}")
def op_PUSH_GLYPH_CONTEXT(ctx: XICContext, *args):
"""PUSH_GLYPH_CONTEXT <glyph_id>: Add glyph to multi-glyph resonance context.
Accumulates glyph IDs for multi-glyph resonance computation. Used with
CALL_GLYPH to enable resonance across multiple glyphs simultaneously.
Stores glyph_id in ctx.glyph_contexts list.
Respects guardrails: max_resonance_glyphs (default 10).
"""
if not args:
raise ValueError("PUSH_GLYPH_CONTEXT requires glyph_id argument")
glyph_id = str(args[0])
# Initialize guardrail defaults if not already set
if "max_resonance_glyphs" not in ctx.params:
ctx.params["max_resonance_glyphs"] = 10
if "enable_resonance_guardrails" not in ctx.params:
ctx.params["enable_resonance_guardrails"] = True
max_glyphs = ctx.params["max_resonance_glyphs"]
enable_guardrails = ctx.params["enable_resonance_guardrails"]
# Check guardrails
if enable_guardrails and len(ctx.glyph_contexts) >= max_glyphs:
print(f"[XIC-GUARDRAIL] Resonance glyph count at limit ({max_glyphs})")
return
if glyph_id not in ctx.glyph_contexts:
ctx.glyph_contexts.append(glyph_id)
print(f"[XIC-MULTI-GLYPH] Pushed glyph context: {glyph_id} (total: {len(ctx.glyph_contexts)})")
def op_CLEAR_GLYPH_CONTEXT(ctx: XICContext, *args):
"""CLEAR_GLYPH_CONTEXT: Clear accumulated glyph resonance context.
Resets the glyph context list, removing all accumulated glyph IDs.
Use before starting a new multi-glyph analysis chain.
"""
count = len(ctx.glyph_contexts)
ctx.glyph_contexts.clear()
print(f"[XIC-MULTI-GLYPH] Cleared glyph context ({count} glyphs removed)")
def op_GET_GLYPH_RESONANCE(ctx: XICContext, *args):
"""GET_GLYPH_RESONANCE <glyph_id> [metric]: Query glyph resonance metrics from previous CALL_GLYPH.
@@ -343,6 +450,8 @@ OP_TABLE = {
"STREAM": op_STREAM,
"CHAIN": op_CHAIN,
"CALL_GLYPH": op_CALL_GLYPH,
"PUSH_GLYPH_CONTEXT": op_PUSH_GLYPH_CONTEXT,
"CLEAR_GLYPH_CONTEXT": op_CLEAR_GLYPH_CONTEXT,
"GET_GLYPH_RESONANCE": op_GET_GLYPH_RESONANCE,
"LOG": op_LOG,
}