instinct-cli 0.3.10

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/pack_fixtures.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::J,
    kernel::{self, Mode},
    receipt,
};
// hlx:3
#[cfg(test)]
#[path = "pack_fixtures_tests.rs"]
mod tests;
// hlx:4
fn object(v: &J) -> Result<&[(String, J)], String> {
    if let J::Obj(v) = v {
        Ok(v)
    } else {
        Err("fixture requires object".into())
    }
}
// hlx:5
fn field<'a>(o: &'a [(String, J)], k: &str) -> Option<&'a J> {
    o.iter().find(|(key, _)| key == k).map(|(_, v)| v)
}
// hlx:6
fn text(v: &J) -> Result<&str, String> {
    if let J::Str(s) = v {
        Ok(s)
    } else {
        Err("fixture requires string".into())
    }
}
// hlx:7
fn ser(v: &J) -> String {
    let mut out = String::new();
    receipt::ser(v, &mut out);
    out
}
// hlx:8
pub(super) fn requests(checks: &[J], fixtures: &J) -> Result<Vec<(J, String)>, String> {
    let J::Arr(items) = fixtures else {
        return Err("fixtures must be an array".into());
    };
    if items.is_empty() || items.len() > 64 {
        return Err("expected 1 to 64 synthetic fixtures".into());
    }
    let mut ids = std::collections::BTreeSet::new();
    let mut out = Vec::new();
    for item in items {
        let f = object(item)?;
        if f.iter().any(|(k, _)| {
            !matches!(
                k.as_str(),
                "id" | "check_id"
                    | "evidence_origin"
                    | "expected"
                    | "facts"
                    | "conditions"
                    | "scope"
            )
        }) {
            return Err("unknown synthetic fixture field".into());
        }
        let id = text(field(f, "id").ok_or("fixture id required")?)?;
        if id.is_empty() || id.len() > 128 || !ids.insert(id) {
            return Err("fixture IDs must be unique nonempty strings".into());
        }
        if field(f, "evidence_origin") != Some(&J::Str("synthetic".into())) {
            return Err("fixtures require explicit synthetic evidence_origin; real observations are not pack fixtures".into());
        }
        let check_id = text(field(f, "check_id").ok_or("fixture check_id required")?)?;
        let check = checks
            .iter()
            .find(|c| object(c).ok().and_then(|o| field(o, "id")) == Some(&J::Str(check_id.into())))
            .ok_or("fixture references unknown check")?;
        let expected = text(field(f, "expected").ok_or("fixture expected required")?)?;
        if !matches!(expected, "pass" | "fail" | "abstain") {
            return Err("fixture expected must be pass, fail or abstain".into());
        }
        let mut request = vec![("check".into(), check.clone())];
        for key in ["facts", "conditions", "scope"] {
            if let Some(v) = field(f, key) {
                request.push((key.into(), v.clone()));
            }
        }
        let request = J::Obj(request);
        if kernel::evaluate(Mode::Validate, ser(&request).as_bytes()).1 {
            return Err("invalid synthetic fixture; validate locally for details".into());
        }
        out.push((request, expected.into()));
    }
    Ok(out)
}
// hlx:9
pub(super) fn verify(requests: &[(J, String)]) -> Result<J, String> {
    let (mut matches, mut mismatches, mut exhausted) = (0, 0, 0);
    let mut rows = Vec::new();
    for (i, (request, expected)) in requests.iter().enumerate() {
        let (raw, bad) = kernel::evaluate(Mode::Run, ser(request).as_bytes());
        if bad {
            return Err("fixture evaluation failed".into());
        }
        let result = super::json::parse(raw.as_bytes())?;
        let o = object(&result)?;
        let verdict = text(field(o, "verdict").ok_or("missing verdict")?)?;
        let limited = field(o, "reason")
            .and_then(|v| text(v).ok())
            .is_some_and(|s| s.starts_with("resource_limit"));
        let agrees = !limited && verdict == expected;
        matches += agrees as usize;
        mismatches += (!agrees) as usize;
        exhausted += limited as usize;
        rows.push(J::Obj(vec![
            ("index".into(), J::Num(i.to_string())),
            ("verdict".into(), J::Str(verdict.into())),
            ("expected".into(), J::Str(expected.clone())),
            ("matches".into(), J::Bool(agrees)),
        ]));
    }
    Ok(J::Obj(vec![
        (
            "schema".into(),
            J::Str("instinct.check-pack-fixtures-result/v1".into()),
        ),
        (
            "status".into(),
            J::Str(
                if mismatches == 0 {
                    "matches_supplied_labels"
                } else {
                    "fixture_mismatch"
                }
                .into(),
            ),
        ),
        ("matches".into(), J::Num(matches.to_string())),
        ("mismatches".into(), J::Num(mismatches.to_string())),
        ("exhausted".into(), J::Num(exhausted.to_string())),
        ("fixtures".into(), J::Arr(rows)),
        (
            "evidence_origin".into(),
            J::Str("caller_declared_synthetic".into()),
        ),
        ("promoted".into(), J::Bool(false)),
        ("authorization".into(), J::Str("not_evaluated".into())),
    ]))
}