instinct-cli 0.2.1

Help AI agents learn from outcomes and make better next decisions. Portable agent skill and Rust CLI for Claude Code, Codex and MCP, powered by Helix DB.
// generated by hxc 1.0.0-acgt-compiler from src/eligibility.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:3
#[derive(Clone, Default, Debug)]
// hlx:4
pub struct Meta {
    // hlx:5
    pub source: String,
    // hlx:6
    pub state: String,
    // hlx:7
    pub evidence_status: String,
    // hlx:8
    pub project: String,
    // hlx:9
    pub persona: String,
    // hlx:10
    pub harness: String,
    // hlx:11
    pub version: String,
    // hlx:12
    pub machine: String,
    // hlx:13
    pub valid_until: u64,
    // hlx:14
}
// hlx:15
#[derive(Clone, Default, Debug)]
// hlx:16
pub struct Scope {
    // hlx:17
    pub project: String,
    // hlx:18
    pub persona: String,
    // hlx:19
    pub harness: String,
    // hlx:20
    pub version: String,
    // hlx:21
    pub machine: String,
    // hlx:22
}
// hlx:23
pub fn env_scope() -> Scope {
    // hlx:24
    let v = |k: &str| std::env::var(k).unwrap_or_default();
    // hlx:25
    Scope {
        project: v("INSTINCT_PROJECT"),
        persona: v("INSTINCT_PERSONA"),
        harness: v("INSTINCT_HARNESS"),
        version: v("INSTINCT_HARNESS_VERSION"),
        machine: v("INSTINCT_MACHINE"),
    }
    // hlx:26
}
// hlx:27
pub fn scope_check(m: &Meta, s: &Scope, now: u64) -> Result<(), &'static str> {
    // hlx:28
    if m.state == "quarantined" || m.evidence_status == "contradicted" {
        // hlx:29
        return Err("quarantined");
        // hlx:30
    }
    // hlx:31
    if m.valid_until != 0 && now > m.valid_until {
        // hlx:32
        return Err("expired");
        // hlx:33
    }
    // hlx:34
    for (required, actual, reason) in [
        (&m.project, &s.project, "project"),
        (&m.persona, &s.persona, "persona"),
        (&m.harness, &s.harness, "harness"),
        (&m.version, &s.version, "version"),
        (&m.machine, &s.machine, "machine"),
    ] {
        // hlx:35
        if !required.is_empty() && required != actual {
            // hlx:36
            return Err(reason);
            // hlx:37
        }
        // hlx:38
    }
    // hlx:39
    Ok(())
    // hlx:40
}
// hlx:41
pub fn eligible(
    m: &Meta,
    s: &Scope,
    now: u64,
    sim: Option<f32>,
    hits: u32,
    quiet: bool,
) -> Result<(), &'static str> {
    // hlx:42
    scope_check(m, s, now)?;
    // hlx:43
    if quiet {
        // hlx:44
        return Err("quiet");
        // hlx:45
    }
    // hlx:46
    if (m.source == "mined" || m.source == "distilled" || m.state == "candidate")
        && hits == 0
        && sim.map_or(true, |x| !x.is_finite() || x < 0.74)
    {
        // hlx:47
        return Err("candidate_match_unproven");
        // hlx:48
    }
    // hlx:49
    Ok(())
    // hlx:50
}
// hlx:51
pub fn quiet(useful: u32, irrelevant: u32) -> bool {
    // hlx:52
    (irrelevant >= 2 && useful == 0)
        || (irrelevant >= 3 && (irrelevant as u64) * 4 >= (useful as u64 + irrelevant as u64) * 3)
    // hlx:53
}
// hlx:54
pub fn unverified(m: &Meta) -> bool {
    // hlx:55
    m.evidence_status != "verified"
    // hlx:56
}
// hlx:57
pub fn preservation_check(source: &str, candidate: &str) -> Result<(), &'static str> {
    // hlx:59
    let numbers = |s: &str| -> std::collections::BTreeSet<String> {
        s.split(|c: char| !c.is_ascii_digit() && c != '.' && c != '%' && c != '/')
            .filter(|x| x.chars().any(|c| c.is_ascii_digit()))
            .map(|x| x.trim_matches('.').to_string())
            .collect()
    };
    // hlx:60
    if !numbers(source).is_subset(&numbers(candidate)) {
        // hlx:61
        return Err("numeric_evidence_changed");
        // hlx:62
    }
    // hlx:63
    let a = source.to_lowercase();
    // hlx:64
    let b = candidate.to_lowercase();
    // hlx:65
    for q in [
        "not established",
        "not significant",
        "inconclusive",
        "uncertain",
        "unverified",
        "not proven",
        "shown subset",
        "early stop",
    ] {
        // hlx:66
        if a.contains(q) && !b.contains(q) {
            // hlx:67
            return Err("qualifier_removed");
            // hlx:68
        }
        // hlx:69
    }
    // hlx:70
    if candidate.contains("[REDACTED]") && !source.contains("[REDACTED]") {
        // hlx:71
        return Err("evidence_redacted");
        // hlx:72
    }
    // hlx:73
    Ok(())
    // hlx:74
}
// hlx:75
pub fn strength(hits: u32, misses: u32) -> f32 {
    // hlx:76
    (hits as f32 + 1.0) / (u64::from(hits) + u64::from(misses) + 2) as f32
    // hlx:77
}
// hlx:78
pub fn promoted(hits: u32, misses: u32, min_hits: u32) -> bool {
    // hlx:79
    hits >= min_hits && strength(hits, misses) >= 0.6
    // hlx:80
}
// hlx:83
pub fn prompt_scope(explicit_project: Option<&str>, cwd: Option<&str>) -> Scope {
    // hlx:84
    let mut scope = env_scope();
    // hlx:85
    if let Some(project) = explicit_project.filter(|name| !name.is_empty()).or(cwd) {
        // hlx:86
        scope.project = project.to_owned();
        // hlx:87
    }
    // hlx:88
    scope
    // hlx:89
}