omega-cognitive-kernel 1.0.0

Online strategy-patch runtime: training-free RuleEffect adaptation with audit and rollback
Documentation
use clap::{Parser, Subcommand, ValueEnum};
use omega_cognitive_kernel::api::{ExternalOutcome, LoopBenchmarkScenario};
use omega_cognitive_kernel::config::LoopPreset;
use omega_cognitive_kernel::task::{
    L2_GAIN_DEFAULT_EPISODES, L2_GAIN_DEFAULT_RUNS, L2_GAIN_DEFAULT_STEPS,
};
use std::path::PathBuf;

#[derive(Copy, Clone, Debug, ValueEnum)]
pub(crate) enum CliLoopBenchmarkScenario {
    Steady,
    Shift,
    Noisy,
}

impl From<CliLoopBenchmarkScenario> for LoopBenchmarkScenario {
    fn from(scenario: CliLoopBenchmarkScenario) -> Self {
        match scenario {
            CliLoopBenchmarkScenario::Steady => Self::Steady,
            CliLoopBenchmarkScenario::Shift => Self::Shift,
            CliLoopBenchmarkScenario::Noisy => Self::Noisy,
        }
    }
}
#[derive(Copy, Clone, Debug, ValueEnum)]
pub(crate) enum CliLoopPreset {
    Deterministic,
    HotPatcher,
    MetaGuard,
    FastAdapt,
    SteadyPrior,
    ControlPlane,
    FullKernel,
}

impl From<CliLoopPreset> for LoopPreset {
    fn from(preset: CliLoopPreset) -> Self {
        match preset {
            CliLoopPreset::Deterministic => Self::Deterministic,
            CliLoopPreset::HotPatcher => Self::HotPatcher,
            CliLoopPreset::MetaGuard => Self::MetaGuard,
            CliLoopPreset::FastAdapt => Self::FastAdapt,
            CliLoopPreset::SteadyPrior => Self::SteadyPrior,
            CliLoopPreset::ControlPlane => Self::ControlPlane,
            CliLoopPreset::FullKernel => Self::FullKernel,
        }
    }
}

#[derive(Copy, Clone, Debug, ValueEnum)]
pub(crate) enum CliExternalOutcome {
    Improved,
    Degraded,
    Timeout,
}

impl From<CliExternalOutcome> for ExternalOutcome {
    fn from(outcome: CliExternalOutcome) -> Self {
        match outcome {
            CliExternalOutcome::Improved => Self::AcceptedAndImproved,
            CliExternalOutcome::Degraded => Self::Degraded,
            CliExternalOutcome::Timeout => Self::TimeoutOrSkipped,
        }
    }
}

#[derive(Parser)]
#[command(
    name = "omega-kernel",
    version,
    about = "Omega Cognitive Kernel — self-improving cognitive runtime (L0/L1/L2)"
)]
pub(crate) struct Cli {
    /// Path to omega.toml (optional). Applied after open; --state overrides config state_path.
    #[arg(long, global = true)]
    pub(crate) config: Option<PathBuf>,
    #[arg(long, global = true, conflicts_with = "store")]
    pub(crate) state: Option<PathBuf>,
    /// State store URI (default: file storage). Use `sqlite://<path>` with the sqlite feature.
    /// Relative paths resolve from the current working directory; use `sqlite:///path` for absolute paths.
    /// This replaces state_path from --config and cannot be combined with --state.
    #[arg(long, global = true, value_name = "URI")]
    pub(crate) store: Option<String>,
    /// Closed-loop layer preset (deterministic, hot-patcher, meta-guard, fast-adapt, steady-prior, control-plane, full-kernel).
    #[arg(long, global = true, value_enum)]
    pub(crate) preset: Option<CliLoopPreset>,
    #[command(subcommand)]
    pub(crate) command: Commands,
}

#[derive(Subcommand)]
pub(crate) enum Commands {
    Run {
        #[arg(
            long,
            default_value = "maintain coherent self-model while acting in world"
        )]
        goal: String,
        #[arg(long, default_value_t = 12)]
        steps: u32,
        #[arg(long)]
        no_save: bool,
    },
    /// Print kernel status (human text or --json).
    Status {
        /// Emit KernelStatus as JSON (for scripts / HTTP shims).
        #[arg(long)]
        json: bool,
    },
    Reset {
        #[arg(long)]
        yes: bool,
    },
    Demo {
        #[arg(long, default_value_t = 12)]
        steps: u32,
    },
    /// Show audit trail (tail) or operator summary panel.
    Audit {
        #[arg(long, default_value_t = 20)]
        tail: usize,
        /// Print rollup panel instead of raw events.
        #[arg(long)]
        summary: bool,
    },
    /// Offline replay of trajectory.jsonl (no world re-execution).
    Replay {
        /// Explicit trajectory path; default is trajectory.jsonl beside --state.
        #[arg(long)]
        path: Option<PathBuf>,
        #[arg(long, default_value_t = 20)]
        tail: usize,
    },
    Checkpoints,
    Checkpoint {
        #[arg(long, default_value = "manual")]
        label: String,
    },
    Rollback {
        id: String,
    },
    Task {
        #[arg(long, default_value_t = 6)]
        episodes: u32,
        #[arg(long, default_value_t = 4)]
        steps: u32,
    },
    /// Compare identical seeded runs with L2 interventions disabled/enabled.
    BenchmarkL2 {
        /// stable, nonstationary, degraded, fixedpoint, regime_shift, or cyclic_shift
        #[arg(long, default_value = "nonstationary")]
        scenario: String,
        #[arg(long, default_value_t = 5)]
        runs: u32,
        #[arg(long, default_value_t = 8)]
        episodes: u32,
        #[arg(long, default_value_t = 8)]
        steps: u32,
        #[arg(long, default_value_t = 20261115)]
        seed: u64,
        /// L2 commit score threshold used by the treatment arm.
        #[arg(long)]
        commit_threshold: Option<f64>,
        /// Cooldown steps after a committed modulation.
        #[arg(long)]
        commit_cooldown: Option<u64>,
        /// Consecutive negative rewards required for reward_change signal.
        #[arg(long)]
        negative_reward_required: Option<u32>,
        /// Opt-in spectral experiment; default benchmark remains the baseline.
        #[arg(long)]
        spectral_control: bool,
        #[arg(long, requires = "spectral_control")]
        no_purity_prior: bool,
        #[arg(long, requires = "spectral_control")]
        no_reverse_brake: bool,
        #[arg(long, requires = "spectral_control")]
        no_history_discount: bool,
        /// Include per-step diagnostics in each treatment run.
        #[arg(long)]
        diagnostics: bool,
        /// Emit machine-readable JSON.
        #[arg(long)]
        json: bool,
    },
    /// Compare L0, L1-only, L2-only, and L1+L2 on the same seeded task.
    BenchmarkL1l2 {
        /// stable, nonstationary, degraded, fixedpoint, regime_shift, or cyclic_shift
        #[arg(long, default_value = "regime_shift")]
        scenario: String,
        #[arg(long, default_value_t = 5)]
        runs: u32,
        #[arg(long, default_value_t = 8)]
        episodes: u32,
        #[arg(long, default_value_t = 8)]
        steps: u32,
        #[arg(long, default_value_t = 20261115)]
        seed: u64,
        #[arg(long)]
        commit_threshold: Option<f64>,
        #[arg(long)]
        commit_cooldown: Option<u64>,
        #[arg(long)]
        negative_reward_required: Option<u32>,
        /// Emit machine-readable JSON with all four per-seed reports.
        #[arg(long)]
        json: bool,
    },
    /// Run the fixed four-scenario, discovery/validation L2 gain matrix.
    BenchmarkL2Gain {
        /// Paired seeds per scenario; must be at least two for intervals.
        #[arg(long, default_value_t = L2_GAIN_DEFAULT_RUNS)]
        runs: u32,
        #[arg(long, default_value_t = L2_GAIN_DEFAULT_EPISODES)]
        episodes: u32,
        #[arg(long, default_value_t = L2_GAIN_DEFAULT_STEPS)]
        steps: u32,
        /// Run a quick two-seed preflight; not effectiveness evidence.
        #[arg(long, conflicts_with_all = ["runs", "episodes", "steps"])]
        smoke: bool,
        /// Emit all eight factorial reports and the preregistered assessment.
        #[arg(long)]
        json: bool,
    },
    /// Operator nudge: set strategy weight (0.1–10).
    StrategyWeight {
        /// Strategy name (e.g. observe, explore).
        name: String,
        /// New weight in [0.1, 10].
        weight: f64,
    },
    /// Incremental audit poll (byte cursor). Prints new events and next offset.
    AuditPoll {
        /// Byte offset from a previous poll (0 = from start / resync).
        #[arg(long, default_value_t = 0)]
        offset: u64,
    },
    /// Run a single external-telemetry step for L0-off presets (hot-patcher, meta-guard, control-plane).
    StepExternal {
        /// Monotonically increasing step identifier.
        #[arg(long, default_value_t = 1)]
        step_id: u64,
        /// Observed prediction or policy error.
        #[arg(long)]
        error: f64,
        /// Observed action risk in [0, 1].
        #[arg(long, default_value_t = 0.0)]
        risk: f64,
        /// Outcome of the previous directive, if any.
        #[arg(long, value_enum)]
        outcome: Option<CliExternalOutcome>,
        /// Run L1 in shadow-only mode without mutating cooldown state.
        #[arg(long)]
        shadow: bool,
        /// Emit ControlDirective as JSON.
        #[arg(long)]
        json: bool,
        /// Do not persist updated external closed-loop state after stepping.
        #[arg(long)]
        no_save: bool,
    },
    /// Benchmark all 7 (L0, L1, L2) closed-loop presets under steady, shift, or noisy workloads.
    BenchmarkLoops {
        /// Workload scenario (steady, shift, noisy).
        #[arg(long, value_enum, default_value_t = CliLoopBenchmarkScenario::Shift)]
        scenario: CliLoopBenchmarkScenario,
        /// Steps to execute per preset.
        #[arg(long, default_value_t = 24)]
        steps: u32,
        /// Emit machine-readable JSON report.
        #[arg(long)]
        json: bool,
    },
    /// Benchmark true closed-loop multi-parameter adaptation (baseline vs L1 vs L1+L2).
    BenchmarkClosedLoop {
        /// Total steps per arm (default 150: steady -> drift -> noisy).
        #[arg(long, default_value_t = 150)]
        steps: u32,
        /// Emit machine-readable JSON report.
        #[arg(long)]
        json: bool,
    },
    /// Run the preregistered Adaptive Kernel Protocol v1 Phase 3 Shadow vs Active evaluation.
    AdaptiveEval {
        /// Emit machine-readable JSON instead of a human-readable summary table.
        #[arg(long)]
        json: bool,
    },
}