omega-cognitive-kernel 0.10.0

Online strategy-patch runtime: training-free RuleEffect adaptation with audit and rollback
Documentation
"""Guard an LLM agent tool-calling loop with Omega's `control_plane` preset
and a bounded multi-parameter `PolicySchema`.

Run after `pip install omega-cognitive-kernel` (or `maturin develop`):
    python examples/python/llm_agent_guard.py
"""

from __future__ import annotations

import json
import tempfile
from pathlib import Path

import omega_cognitive_kernel_python as ok


POLICY_SCHEMA = {
    "params": [
        {
            "name": "temperature",
            "min": 0.1,
            "max": 0.9,
            "initial": 0.7,
            "max_step": 0.2,
            "direction": "decrease_on_error",
        },
        {
            "name": "max_concurrency",
            "min": 1.0,
            "max": 8.0,
            "initial": 6.0,
            "max_step": 2.0,
            "direction": "decrease_on_error",
        },
        {
            "name": "retry_backoff_ms",
            "min": 200.0,
            "max": 5000.0,
            "initial": 500.0,
            "max_step": 800.0,
            "direction": "increase_on_error",
        },
    ]
}


def main() -> None:
    with tempfile.TemporaryDirectory(prefix="omega-llm-guard-") as tmp:
        state_path = str(Path(tmp) / "agent_state.json")
        session = ok.ClosedLoopSession.from_preset(
            "control_plane",
            state_path,
            policy_schema_json=json.dumps(POLICY_SCHEMA),
        )

        # (step_id, tool_error_rate, action_risk, previous_outcome)
        workload = [
            (1, 0.05, 0.10, None),        # Steady state: L2 blocks L1 writes
            (2, 0.72, 0.22, None),        # Tool errors spike: L1 adjusts all 3 params
            (3, 0.78, 0.25, "degraded"),  # Patch degraded: kernel auto-restores params
            (4, 0.11, 0.12, "improved"),  # Recovered: L2 re-enters steady gate
        ]

        for step_id, err, risk, outcome in workload:
            directive = json.loads(
                session.step_external_json(step_id, err, risk, None, outcome)
            )
            params = directive.get("active_params") or {}
            l2 = directive.get("l2_prior") or {}
            print(
                f"step={step_id} gate={directive['l1_gate']} "
                f"temp={params.get('temperature', 0.0):.2f} "
                f"conc={params.get('max_concurrency', 0.0):.1f} "
                f"backoff_ms={params.get('retry_backoff_ms', 0.0):.0f} "
                f"rollback={directive.get('rollback_to_checkpoint')} "
                f"lyapunov={l2.get('lyapunov_potential', 0.0):.4f}"
            )


if __name__ == "__main__":
    main()