instinct-cli 0.3.8

Shared local memory for coding agents. Save a fix once and find it again in a new session or another connected tool.
// generated by hxc 1.0.0-acgt-compiler from src/check/mcp.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
use super::{
    json::{self, J},
    kernel::{self, Mode},
    receipt,
};
// hlx:3
pub const SCHEMA: &str = r#"{"name":"instinct_check","description":"Evaluate or replay a bounded check over explicitly supplied facts. No filesystem or bank access. Results do not authorize actions or establish world truth.","inputSchema":{"type":"object","additionalProperties":false,"properties":{"operation":{"type":"string","enum":["validate","run","explain","receipt","replay"]},"request_json":{"type":"string","description":"Raw JSON check request, or replay envelope. Preserves exact integers and duplicate-key validation; never a file path."}},"required":["operation","request_json"]},"annotations":{"readOnlyHint":true,"destructiveHint":false,"openWorldHint":false}}"#;
// hlx:4
fn field<'a>(v: &'a J, k: &str) -> Option<&'a J> {
    if let J::Obj(o) = v {
        o.iter().find(|(n, _)| n == k).map(|(_, v)| v)
    } else {
        None
    }
}
// hlx:5
fn error(reason: &str) -> String {
    kernel::error_json(reason)
}
// hlx:6
pub fn call_wire(line: &str) -> Result<(String, Vec<String>), String> {
    // hlx:7
    // Outer JSON may escape every inner byte as \uXXXX; this bounded allowance is transport only.
    // hlx:8
    let envelope = json::parse_with_limits(
        line.as_bytes(),
        6 * (json::MAX_BYTES + json::REPLAY_EXTRA_BYTES) + 4096,
        16,
    )
    .map_err(|e| error(&e))?;
    // hlx:9
    let args = field(&envelope, "params")
        .and_then(|p| field(p, "arguments"))
        .ok_or_else(|| error("check requires arguments"))?;
    // hlx:10
    let J::Obj(fields) = args else {
        return Err(error("check arguments must be an object"));
    };
    // hlx:11
    if fields.len() != 2
        || fields
            .iter()
            .any(|(k, _)| !matches!(k.as_str(), "operation" | "request_json"))
    {
        return Err(error("check requires exactly operation and request_json"));
    }
    // hlx:12
    let Some(J::Str(op)) = field(args, "operation") else {
        return Err(error("operation must be a string"));
    };
    // hlx:13
    let Some(J::Str(raw)) = field(args, "request_json") else {
        return Err(error("request_json must be a string"));
    };
    // hlx:14
    let (out, failed) = match op.as_str() {
        "validate" => kernel::evaluate(Mode::Validate, raw.as_bytes()),
        "run" => kernel::evaluate(Mode::Run, raw.as_bytes()),
        "explain" => kernel::evaluate(Mode::Explain, raw.as_bytes()),
        "receipt" => receipt::create(raw.as_bytes()),
        "replay" => receipt::replay(raw.as_bytes()),
        _ => {
            return Err(error(
                "unsupported check operation; observation and mutation are not exposed",
            ))
        }
    };
    // hlx:15
    if failed {
        Err(out)
    } else {
        Ok((out, vec![]))
    }
    // hlx:16
}