PHASE A: Safe predicate evaluator (glyphos/control/predicate.py) - AST-based safe expression evaluation - Supports comparisons, boolean ops, attribute access - Helper function: dominant_contains() - Protected against code injection attacks PHASE B: XICContext queue helpers - enqueue_chain(label) for FIFO chain scheduling - pop_next_chain() to get next scheduled chain - jump_to(label) for immediate destination changes PHASE C: Control flow operations (xic_ops.py) - op_IF: Conditional branching with optional else - op_MATCH: Pattern matching against fused fields - op_LOOP: Iterative execution with guardrails - Added to OP_TABLE for operation dispatch PHASE D: Execution loop enhancement (xic_vm.py) - Chain queue scheduling with label matching - Total steps tracking for guardrail enforcement - max_total_steps limit across all operations - Graceful execution stop on guardrail trigger PHASE E: Comprehensive test suite (tests/test_control_flow.py) - 14 unit tests covering all operations - Predicate evaluator tests - IF/MATCH/LOOP operation tests - Queue helper and guardrail tests - All tests passing (14/14) PHASE F: Example programs - demo_control_flow_if.gx.json: IF branching example - demo_control_flow_loop.gx.json: LOOP iteration example PHASE G: Complete documentation - XIC_V2_CONTROL_FLOW_SUMMARY.md: Technical guide - XIC_V2_QUICK_REFERENCE.md: Developer quick reference - FedMart UI and integration documentation Integration points: - FedMart telemetry captures control flow steps - UI dashboard displays control branching - Symbolic pipeline predicate evaluation - 100% backward compatible with XIC v1.5 Test results: 36/36 passing (14 control flow + 12 FedMart + 10 UI) Status: Production ready
9.8 KiB
XIC v2 Control Flow Implementation - Complete Summary
Date: 2026-05-21
Status: ✅ COMPLETE & TESTED
Test Results: 36/36 tests passing (FedMart + UI + Control Flow)
Overview
Successfully implemented XIC v2 control flow with IF, MATCH, and LOOP operations. The system adds conditional branching, pattern matching, and iterative execution to XIC v1.5 while maintaining full backward compatibility.
What Was Implemented
1. Safe Predicate Evaluator (glyphos/control/predicate.py)
- Safe AST-based expression evaluation
- Prevents dangerous operations (imports, system calls)
- Supports:
- Comparisons:
>,<,>=,<=,==,!= - Boolean operators:
and,or,not - Attribute access:
fused.global_resonance_score - Helper functions:
dominant_contains('glyph://id')
- Comparisons:
Example Predicates:
"fused.global_resonance_score > 0.8"
"dominant_contains('glyph://entropy') and fused.global_resonance_score > 0.5"
"fused.glyph_count >= 3"
2. XICContext Queue Helpers
Three new methods added to XICContext class:
def enqueue_chain(self, label: str):
"""Schedule a chain/label to run next (FIFO)."""
def pop_next_chain(self):
"""Get next scheduled chain (FIFO). Returns None if queue empty."""
def jump_to(self, label: str):
"""Immediate jump: clear queue and run label next."""
3. Control Flow Operations
IF - Conditional Branching
IF <predicate> <then_label> [<else_label>]
- Evaluates predicate against last symbolic pipeline result
- Enqueues then_label if true, else_label if false (optional)
- Logs control steps for observability
Example:
{"op": "IF", "args": ["fused.global_resonance_score > 0.8", "high_resonance", "low_resonance"]}
MATCH - Pattern Matching
MATCH <path> <pattern> <then_label>
- Pattern matches against fused_symbol fields
- Currently supports
fused.glyph_ids(checks if pattern is in list) - Enqueues then_label if pattern matches
Example:
{"op": "MATCH", "args": ["fused.glyph_ids", "glyph://entropy", "found_entropy"]}
LOOP - Iterative Execution
LOOP <predicate> <body_label> [max_iter]
- Repeatedly enqueues body_label while predicate is true
- Enforces guardrails:
max_loop_iterations: max iterations per LOOP (default: 50)max_total_steps: max total steps for entire program (default: 1000)
- Emits symbolic steps for each iteration
Example:
{
"op": "SET_PARAM",
"args": ["max_loop_iterations", 5]
},
{
"op": "LOOP",
"args": ["fused.global_resonance_score > 0.6", "body_chain", 5]
}
4. Modified Execution Loop (xic_vm.py)
Enhanced run_xic_program() to:
- Handle chain queue scheduling with
pop_next_chain() - Track
total_stepsfor guardrail enforcement - Find and jump to CHAIN instructions by label
- Enforce
max_total_stepslimit - Stop execution if guardrails are triggered
File Summary
| File | Type | Change | Status |
|---|---|---|---|
glyphos/control/predicate.py |
New | Safe predicate evaluator | ✅ 78 LOC |
glyphos/control/__init__.py |
New | Package init | ✅ Empty |
xic_ops.py |
Modified | +Queue helpers, +3 control ops | ✅ 608 → 773 LOC |
xic_vm.py |
Modified | +Chain queue handling | ✅ 31 → 60 LOC |
tests/test_control_flow.py |
New | 14 unit tests | ✅ 377 LOC |
programs/demo_control_flow_if.gx.json |
New | IF demo program | ✅ Created |
programs/demo_control_flow_loop.gx.json |
New | LOOP demo program | ✅ Created |
Test Results
Control Flow Tests (14 passing)
✅ Predicate: simple comparison
✅ Predicate: false comparison
✅ Predicate: AND operator
✅ Predicate: dominant_contains
✅ IF: then branch
✅ IF: else branch
✅ IF: no else
✅ MATCH: pattern found
✅ MATCH: pattern not found
✅ LOOP: iterations
✅ LOOP: false condition
✅ LOOP: max iterations guardrail
✅ Queue: FIFO order
✅ Queue: jump_to
Full Test Suite (36/36 passing)
- FedMart Integration: 12/12 ✅
- UI Integration: 10/10 ✅
- Control Flow: 14/14 ✅
Usage Guide
1. IF Control Flow Example
{
"magic": "GXIC1",
"version": 1,
"entrypoint": "main",
"symbols": {
"main": 0,
"high_resonance": 5,
"low_resonance": 8,
"end": 10
},
"instructions": [
{"op": "SET_MODE", "args": ["symbolic"]},
{"op": "SET_CONTEXT", "args": ["domain", "analysis"]},
{"op": "PUSH_GLYPH_CONTEXT", "args": ["glyph://a"]},
{"op": "PUSH_GLYPH_CONTEXT", "args": ["glyph://b"]},
{"op": "RUN_PROMPT", "args": ["Analyze the relationship"]},
{"op": "IF", "args": ["fused.global_resonance_score > 0.8", "high_resonance", "low_resonance"]},
{"op": "CHAIN", "args": ["high_resonance"]},
{"op": "LOG", "args": ["High resonance path"]},
{"op": "CHAIN", "args": ["end"]},
{"op": "CHAIN", "args": ["low_resonance"]},
{"op": "LOG", "args": ["Low resonance path"]},
{"op": "CHAIN", "args": ["end"]},
{"op": "CHAIN", "args": ["end"]},
{"op": "LOG", "args": ["Done"]}
]
}
2. LOOP Control Flow Example
{
"instructions": [
{"op": "SET_MODE", "args": ["symbolic"]},
{"op": "SET_PARAM", "args": ["max_loop_iterations", 5]},
{"op": "LOOP", "args": ["fused.global_resonance_score > 0.6", "body", 5]},
{"op": "CHAIN", "args": ["body"]},
{"op": "RUN_PROMPT", "args": ["Refine analysis"]},
{"op": "CHAIN", "args": ["end"]},
{"op": "CHAIN", "args": ["end"]},
{"op": "LOG", "args": ["Complete"]}
]
}
3. Using Predicates
Simple comparisons:
"fused.global_resonance_score > 0.8"
"fused.glyph_count >= 2"
Boolean operators:
"fused.global_resonance_score > 0.8 and fused.glyph_count > 1"
"fused.global_resonance_score > 0.7 or fused.global_resonance_score < 0.3"
Helper functions:
"dominant_contains('glyph://compression')"
"dominant_contains('glyph://entropy') and fused.global_resonance_score > 0.5"
Backward Compatibility
✅ 100% Backward Compatible
- No changes to .gx binary format
- No changes to glyph ontology
- New operations are optional
- Existing XIC v1.5 programs run unchanged
- New operations integrate seamlessly
Guardrails & Safety
Built-in Guardrails
-
max_loop_iterations (default: 50)
- Prevents infinite loops
- Configurable via
SET_PARAM
-
max_total_steps (default: 1000)
- Limits total program execution
- Enforced across IF/LOOP/RUN_PROMPT
- Prevents resource exhaustion
-
Predicate Safety
- AST validation prevents code injection
- No system calls, imports, or builtins
- Only safe comparisons and helpers allowed
Guardrail Triggering
When a guardrail is triggered:
- Logged to
ctx._state["guardrails"] - Emitted as SymbolicStep with kind="guardrail"
- Execution stops gracefully
- FedMart telemetry captures event
Integration Points
With FedMart Telemetry
- Control flow steps logged as SymbolicStep objects
- Guardrail events captured in telemetry
- Dashboard shows control flow execution in timeline
With UI Dashboard
- Timeline displays IF/MATCH/LOOP steps
- Control flow branching visible in step sequence
- Guardrail enforcement shown in alerts
With Symbolic Pipeline
- Predicates evaluated against last pipeline result
- Fused symbol fields accessible in all predicates
- Dominant glyphs helper function built-in
Advanced Features
Custom Predicate Evaluation
from glyphos.control.predicate import eval_predicate
result = eval_predicate(
"fused.global_resonance_score > 0.7 and dominant_contains('glyph://entropy')",
fused={"global_resonance_score": 0.85},
dominant=[("glyph://entropy", 0.95), ("glyph://compression", 0.8)]
)
# Returns: True
Queue Management
ctx = XICContext()
ctx.enqueue_chain("analysis_1")
ctx.enqueue_chain("analysis_2")
next_chain = ctx.pop_next_chain() # Returns: "analysis_1"
# Jump immediately to a different chain
ctx.jump_to("emergency_shutdown")
Future Enhancements
Recommended for v3.0
- Extended Pattern Matching - Support more complex path expressions
- Custom Predicates - Register custom predicate functions
- Loop Optimization - Cache predicate results within iterations
- Control Flow Visualization - Graph rendering in dashboard
- Debugging Support - Breakpoints in control flow
- Performance Profiling - Time each control branch
Verification Checklist
- Predicate evaluator is secure (AST validation)
- Queue helpers work correctly (FIFO, jump)
- IF operation branches properly
- MATCH operation pattern matches
- LOOP operation iterates and respects limits
- Execution loop handles chain scheduling
- Guardrails are enforced
- Symbolic steps are emitted
- FedMart telemetry integration works
- All 36 tests passing
- Backward compatibility maintained
- Example programs created
- Documentation complete
Files Modified/Created
New Files
glyphos/control/predicate.py (78 lines)
glyphos/control/__init__.py (0 lines)
tests/test_control_flow.py (377 lines)
programs/demo_control_flow_if.gx.json (example)
programs/demo_control_flow_loop.gx.json (example)
Modified Files
xic_ops.py (165 lines added)
xic_vm.py (29 lines modified)
Conclusion
XIC v2 control flow is complete, tested, and production-ready. The implementation provides:
✅ Safe predicate evaluation with AST validation
✅ Three control flow operations (IF, MATCH, LOOP)
✅ Queue-based chain scheduling
✅ Comprehensive guardrail enforcement
✅ Full integration with FedMart telemetry
✅ Real-time UI visualization
✅ 100% backward compatibility
✅ 36/36 tests passing
Ready for immediate use in XIC programs.
Status: ✅ PRODUCTION READY
Version: XIC v2.0
Date: 2026-05-21