instinct-cli 0.3.6

Local memory for AI coding agents: an MCP server and CLI that gives Claude Code, Codex, Cursor and any MCP client the one lesson that fits. Lessons that keep failing go quiet.
// generated by hxc 1.0.0-acgt-compiler from src/check/mod.hlx -- do not edit by hand

#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
    let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
    if !ok {
        eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
    }
    ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
        std::process::exit(13);
    }
    use std::os::windows::process::CommandExt;
    c.creation_flags(0x08000000);
    c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
        std::process::exit(13);
    }
    c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
        std::process::exit(13);
    }
    use std::os::windows::process::CommandExt;
    c.creation_flags(0x08000000);
    c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
        std::process::exit(13);
    }
    c
}

// hlx:2
// `instinct check`: scoped deterministic checks. The pure kernel (`kernel`, `json`) is Helix-authored
// hlx:3
// (`*.hlx`, exported to the sibling `.rs` files); this file is only the CLI adapter, the one place that reads input.
// hlx:4
// Pure operations open no bank. Explicit attach writes only its private sidecar; no operation grants authority.
// hlx:5
#[allow(dead_code)]
// hlx:6
pub(crate) mod json;
// hlx:7
#[allow(dead_code)]
// hlx:8
mod kernel;
// hlx:9
mod observe;
// hlx:10
#[allow(dead_code)]
// hlx:11
mod digest;
// hlx:12
#[allow(dead_code)]
// hlx:13
mod receipt;
// hlx:14
mod compose;
// hlx:15
pub(crate) mod feedback;
// hlx:16
pub(crate) mod mcp;
// hlx:17
pub use kernel::{evaluate, Mode};
// hlx:18
use std::io::Read;
// hlx:19
const USAGE: &str = "usage: instinct check validate|run|explain|receipt|replay|compose --input FILE|-\n  Reads one JSON request (at most 1 MiB) and prints one JSON result. Spec: docs/checks.md.\n  receipt prints a byte-identity receipt for a request; replay recomputes {request, receipt}; neither authenticates or authorizes.\n  observe --root ABS --input FILE|- observes a trusted staging inventory read-only; see check observe --help.\n  attach --lesson ID --store BANK --input FILE|- explicitly attaches a policy.\n  detach --lesson ID --store BANK withdraws a policy without deleting history.\n  Pure operations open no bank; attach/detach explicitly write their sidecar. Exit 0 for pass, fail and abstain, exit 2 for an error result.";
// hlx:20
fn usage_error(why: &str) -> String {
    // hlx:21
    kernel::error_json(&format!("usage: {why}"))
    // hlx:22
}
// hlx:23
fn read_input(path: &str) -> Result<Vec<u8>, String> {
    // hlx:24
    read_input_capped(path, json::MAX_BYTES)
    // hlx:25
}
// hlx:26
fn read_input_capped(path: &str, max: usize) -> Result<Vec<u8>, String> {
    // hlx:27
    let cap = (max as u64) + 1;
    // hlx:28
    let mut buf = Vec::new();
    // hlx:29
    let got = if path == "-" {
        // hlx:30
        std::io::stdin().lock().take(cap).read_to_end(&mut buf)
    // hlx:31
    } else {
        // hlx:32
        match std::fs::File::open(path) {
            // hlx:33
            Ok(f) => f.take(cap).read_to_end(&mut buf),
            // hlx:34
            Err(e) => return Err(format!("input_io: cannot open input ({})", e.kind())),
            // hlx:35
        }
        // hlx:36
    };
    // hlx:37
    if let Err(e) = got {
        // hlx:38
        return Err(format!("input_io: cannot read input ({})", e.kind()));
        // hlx:39
    }
    // hlx:40
    if buf.len() > max {
        // hlx:41
        return Err(if max == json::MAX_BYTES {
            "input_too_large: more than 1 MiB".to_string()
        } else {
            "input_too_large: replay envelope exceeds 1 MiB plus 4096 bytes".to_string()
        });
        // hlx:42
    }
    // hlx:43
    Ok(buf)
    // hlx:44
}
// hlx:45
/// Runs the subcommand and returns the process exit code. Output goes to stdout only, as one JSON object.
// hlx:46
pub fn cli(args: &[String]) -> i32 {
    // hlx:47
    if args.first().map(String::as_str) == Some("attach") {
        // hlx:48
        return match feedback::attach_cli(&args[1..]) {
            Ok(s) => {
                println!("{s}");
                0
            }
            Err(e) => fail(&kernel::error_json(&e)),
        };
        // hlx:49
    }
    // hlx:50
    if args.first().map(String::as_str) == Some("detach") {
        // hlx:51
        return match feedback::detach_cli(&args[1..]) {
            Ok(s) => {
                println!("{s}");
                0
            }
            Err(e) => fail(&kernel::error_json(&e)),
        };
        // hlx:52
    }
    // hlx:53
    // Observation requires an explicit --root grant.
    // hlx:54
    if args.first().map(String::as_str) == Some("observe") {
        // hlx:55
        return observe::cli(&args[1..]);
        // hlx:56
    }
    // hlx:57
    let is_help = |a: &String| a == "--help" || a == "-h";
    // hlx:58
    let sub_ok = |a: &String| {
        matches!(
            a.as_str(),
            "validate" | "run" | "explain" | "receipt" | "replay" | "compose"
        )
    };
    // hlx:59
    if (args.len() == 1 && is_help(&args[0]))
        || (args.len() == 2 && sub_ok(&args[0]) && is_help(&args[1]))
    {
        // hlx:60
        println!("{USAGE}");
        // hlx:61
        return 0;
        // hlx:62
    }
    // hlx:63
    let mode = match args.first().map(String::as_str) {
        // hlx:64
        Some("validate") => Mode::Validate,
        // hlx:65
        Some("run") => Mode::Run,
        // hlx:66
        Some("explain") => Mode::Explain,
        // hlx:67
        Some("receipt") | Some("replay") | Some("compose") => Mode::Run,
        // hlx:68
        Some(other) if other.starts_with('-') => {
            return fail(&usage_error("expected validate, run or explain first"))
        }
        // hlx:69
        Some(_) => {
            return fail(&usage_error(
                "unknown subcommand; expected validate, run or explain",
            ))
        }
        // hlx:70
        None => {
            return fail(&usage_error(
                "missing subcommand; expected validate, run or explain",
            ))
        } // hlx:71
    };
    // hlx:72
    let mut input: Option<String> = None;
    // hlx:73
    let mut i = 1;
    // hlx:74
    while i < args.len() {
        // hlx:75
        let a = &args[i];
        // hlx:76
        let value = if a == "--input" {
            // hlx:77
            i += 1;
            // hlx:78
            match args.get(i) {
                // hlx:79
                Some(v) if v == "-" || !v.starts_with("--") => v.clone(),
                // hlx:80
                _ => return fail(&usage_error("--input needs a file path or -")),
                // hlx:81
            }
        // hlx:82
        } else if let Some(v) = a.strip_prefix("--input=") {
            // hlx:83
            v.to_string()
        // hlx:84
        } else if a.starts_with('-') && a != "-" {
            // hlx:85
            return fail(&usage_error("unknown option (only --input is accepted; --help is only accepted as `check --help` or `check SUBCOMMAND --help`)"));
        // hlx:86
        } else {
            // hlx:87
            return fail(&usage_error("unexpected positional argument"));
            // hlx:88
        };
        // hlx:89
        if input.is_some() {
            // hlx:90
            return fail(&usage_error("--input given more than once"));
            // hlx:91
        }
        // hlx:92
        input = Some(value);
        // hlx:93
        i += 1;
        // hlx:94
    }
    // hlx:95
    let path = match input {
        // hlx:96
        Some(p) if !p.is_empty() => p,
        // hlx:97
        _ => return fail(&usage_error("--input FILE|- is required")),
        // hlx:98
    };
    // hlx:99
    let max = if args[0] == "replay" {
        json::MAX_BYTES + json::REPLAY_EXTRA_BYTES
    } else {
        json::MAX_BYTES
    };
    // hlx:100
    let bytes = match read_input_capped(&path, max) {
        // hlx:101
        Ok(b) => b,
        // hlx:102
        Err(e) => return fail(&kernel::error_json(&e)),
        // hlx:103
    };
    // hlx:104
    let (out, is_error) = match args[0].as_str() {
        // hlx:105
        "receipt" => receipt::create(&bytes),
        // hlx:106
        "replay" => receipt::replay(&bytes),
        // hlx:107
        "compose" => compose::compose(&bytes),
        // hlx:108
        _ => evaluate(mode, &bytes),
        // hlx:109
    };
    // hlx:110
    println!("{out}");
    // hlx:111
    if is_error {
        // hlx:112
        2
    // hlx:113
    } else {
        // hlx:114
        0
        // hlx:115
    }
    // hlx:116
}
// hlx:117
fn fail(json: &str) -> i32 {
    // hlx:118
    println!("{json}");
    // hlx:119
    2
    // hlx:120
}