Implement XIC v2 control flow with IF, MATCH, LOOP operations
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
This commit is contained in:
@@ -11,20 +11,59 @@ def run_xic_program(prog: XICProgram) -> XICContext:
|
||||
|
||||
Creates a context, iterates through instructions, dispatches
|
||||
each operation to the handler in OP_TABLE, and returns the final context.
|
||||
|
||||
Supports control flow via chain queue (_chain_queue):
|
||||
- When a control op (IF, MATCH, LOOP) enqueues a label,
|
||||
the executor searches for a CHAIN instruction with that label
|
||||
and continues execution from there.
|
||||
- Tracks total_steps for guardrail enforcement.
|
||||
- Stops if guardrails are triggered (max_total_steps, etc).
|
||||
"""
|
||||
ctx = XICContext()
|
||||
max_total_steps = int(ctx.params.get("max_total_steps", 10000))
|
||||
instr_ptr = 0
|
||||
total_steps = 0
|
||||
|
||||
for i, instr in enumerate(prog.instructions):
|
||||
while instr_ptr < len(prog.instructions) and total_steps < max_total_steps:
|
||||
# Check for guardrails
|
||||
if "guardrails" in ctx._state and ctx._state["guardrails"]:
|
||||
print(f"[XIC-VM] Guardrail triggered, stopping execution")
|
||||
break
|
||||
|
||||
# Check for scheduled chain
|
||||
next_chain = ctx.pop_next_chain()
|
||||
if next_chain:
|
||||
# Find CHAIN instruction with matching label
|
||||
found = False
|
||||
for i, instr in enumerate(prog.instructions):
|
||||
if instr.op == "CHAIN" and instr.args and str(instr.args[0]) == next_chain:
|
||||
instr_ptr = i
|
||||
found = True
|
||||
print(f"[XIC-VM] Jumping to chain: {next_chain}")
|
||||
break
|
||||
if not found:
|
||||
print(f"[XIC-VM] Warning: chain '{next_chain}' not found, continuing")
|
||||
|
||||
# Execute current instruction
|
||||
instr = prog.instructions[instr_ptr]
|
||||
op_name = instr.op
|
||||
|
||||
if op_name not in OP_TABLE:
|
||||
raise XICRuntimeError(f"Unknown operation at instruction {i}: {op_name}")
|
||||
raise XICRuntimeError(f"Unknown operation at instruction {instr_ptr}: {op_name}")
|
||||
|
||||
op_handler = OP_TABLE[op_name]
|
||||
|
||||
try:
|
||||
op_handler(ctx, *instr.args)
|
||||
total_steps += 1
|
||||
ctx._state["total_steps"] = total_steps
|
||||
except Exception as e:
|
||||
raise XICRuntimeError(f"Operation {op_name} failed at instruction {i}: {e}")
|
||||
raise XICRuntimeError(f"Operation {op_name} failed at instruction {instr_ptr}: {e}")
|
||||
|
||||
instr_ptr += 1
|
||||
|
||||
if total_steps >= max_total_steps:
|
||||
ctx._state.setdefault("guardrails", []).append("max_total_steps_exceeded")
|
||||
print(f"[XIC-VM] Max total steps exceeded: {total_steps}")
|
||||
|
||||
return ctx
|
||||
|
||||
Reference in New Issue
Block a user