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 bench/capability/subset.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 std::process::{Command, Stdio};
// hlx:3
use std::io::Write;
// hlx:4
use std::time::Instant;
// hlx:5
fn members(mask: u8) -> String {
    // hlx:6
    (0..3)
        .filter(|i| mask & (1 << i) != 0)
        .map(|i| format!("\"item{}\"", i))
        .collect::<Vec<_>>()
        .join(",")
    // hlx:7
}
// hlx:8
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // hlx:9
    let binary = std::env::args()
        .nth(1)
        .ok_or("usage: capability-subset /absolute/path/to/instinct")?;
    // hlx:10
    let root =
        std::env::temp_dir().join(format!("instinct-capability-subset-{}", std::process::id()));
    // hlx:11
    std::fs::create_dir(&root)?;
    // hlx:12
    let mut total = 0u64;
    // hlx:13
    let mut failures = 0u64;
    // hlx:14
    let mut elapsed = Vec::new();
    // hlx:15
    for actual in 0u8..8 {
        // hlx:16
        for allowed in 0u8..8 {
            // hlx:17
            let expected = if actual & !allowed == 0 {
                "pass"
            } else {
                "fail"
            };
            // hlx:18
            let request = format!(
                r#"{{"check":{{"schema":"instinct.check/v1","id":"subset-oracle","revision":1,"inputs":{{"actual":"set<string>","allowed":"set<string>"}},"claim":{{"op":"subset","left":{{"fact":"actual"}},"right":{{"fact":"allowed"}}}}}},"facts":{{"actual":[{}],"allowed":[{}]}}}}"#,
                members(actual),
                members(allowed)
            );
            // hlx:19
            let start = Instant::now();
            // hlx:20
            let mut command = Command::new(&binary);
            // hlx:21
            for (key, _) in std::env::vars_os() {
                // hlx:22
                if key.to_string_lossy().starts_with("INSTINCT_") {
                    // hlx:23
                    command.env_remove(key);
                    // hlx:24
                }
                // hlx:25
            }
            // hlx:26
            let mut child = command
                .args(["check", "run", "--input", "-"])
                .env("HOME", &root)
                .env("USERPROFILE", &root)
                .env("XDG_DATA_HOME", &root)
                .env("LOCALAPPDATA", &root)
                .current_dir(&root)
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()?;
            // hlx:27
            child
                .stdin
                .take()
                .ok_or("missing child stdin")?
                .write_all(request.as_bytes())?;
            // hlx:28
            let output = child.wait_with_output()?;
            // hlx:29
            elapsed.push(start.elapsed().as_micros());
            // hlx:30
            let text = String::from_utf8(output.stdout)?;
            // hlx:31
            let verdict = format!("\"verdict\":\"{}\"", expected);
            // hlx:32
            let spaced = format!("\"verdict\": \"{}\"", expected);
            // hlx:33
            if !output.status.success()
                || !(text.contains(&verdict) || text.contains(&spaced))
                || !text.contains("not_evaluated")
                || !text.contains("caller_reported")
            {
                // hlx:34
                failures += 1;
                // hlx:35
                eprintln!("FAIL actual={actual} allowed={allowed} expected={expected} output={text} stderr={}", String::from_utf8_lossy(&output.stderr));
                // hlx:36
            }
            // hlx:37
            total += 1;
            // hlx:38
        }
        // hlx:39
    }
    // hlx:40
    elapsed.sort_unstable();
    // hlx:41
    println!("pairs={total} failures={failures} cli_median_us={} cli_p90_us={} (process startup included; no speedup claim)", elapsed[elapsed.len()/2], elapsed[(elapsed.len()*9/10).min(elapsed.len()-1)]);
    // hlx:42
    let unexpected_files = std::fs::read_dir(&root)?.count();
    // hlx:43
    std::fs::remove_dir(&root)?;
    // hlx:44
    if failures != 0 || unexpected_files != 0 {
        // hlx:45
        return Err(format!(
            "oracle failures={failures}, unexpected home entries={unexpected_files}"
        )
        .into());
        // hlx:46
    }
    // hlx:47
    Ok(())
    // hlx:48
}