instinct-cli 0.3.9

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 tests/check_rational.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
fn request(op: &str, a: &str, b: &str) -> String {
    format!(
        r#"{{"check":{{"schema":"instinct.check/v1","id":"fraction","revision":1,"inputs":{{"a":"rational","b":"rational"}},"claim":{{"op":"{op}","left":{{"fact":"a"}},"right":{{"fact":"b"}}}}}},"facts":{{"a":"{a}","b":"{b}"}}}}"#
    )
}
// hlx:5
fn call(mode: &str, input: &str) -> (bool, String) {
    let mut c = Command::new(env!("CARGO_BIN_EXE_instinct"))
        .args(["check", mode, "--input", "-"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    c.stdin.take().unwrap().write_all(input.as_bytes()).ok();
    let out = c.wait_with_output().unwrap();
    (out.status.success(), String::from_utf8(out.stdout).unwrap())
}
// hlx:6
#[test]
fn exact_fraction_comparisons() {
    for (op, a, b, verdict) in [
        ("eq", "1/2", "2/4", "pass"),
        ("lt", "-1/2", "-1/3", "pass"),
        ("lte", "0/17", "0/1", "pass"),
        ("lt", "9007199254740993/1", "9007199254740992/1", "fail"),
        (
            "lt",
            "170141183460469231731687303715884105727/170141183460469231731687303715884105726",
            "170141183460469231731687303715884105726/170141183460469231731687303715884105725",
            "pass",
        ),
    ] {
        let (ok, out) = call("run", &request(op, a, b));
        assert!(ok, "{out}");
        assert!(out.contains(&format!("\"verdict\":\"{verdict}\"")), "{out}");
    }
}
// hlx:7
#[test]
fn malformed_fractions_never_pass() {
    for bad in [
        "1/0", "1/-2", "01/2", "-0/1", "1/+2", "1/02", "1/2/3", "1.0/2", "1e2/3",
    ] {
        let (ok, out) = call("run", &request("eq", bad, bad));
        assert!(!ok, "{bad}: {out}");
        assert!(out.contains("invalid_number"), "{out}");
    }
}
// hlx:8
#[test]
fn missing_fraction_abstains() {
    let input = request("eq", "1/2", "1/2").replace("\"b\":\"1/2\"", "");
    let input = input.replace(",}", "}");
    let (ok, out) = call("run", &input);
    assert!(ok, "{out}");
    assert!(out.contains("\"verdict\":\"abstain\""), "{out}");
}
// hlx:9
#[test]
fn integer_rational_mixing_is_not_coerced() {
    let input = request("lt", "1/1", "2/1").replace("\"b\":\"rational\"", "\"b\":\"int\"");
    let (ok, out) = call("validate", &input);
    assert!(!ok);
    assert!(out.contains("type_mismatch"), "{out}");
}
// hlx:10
#[test]
fn difficult_fractions_exhaust_fuel_instead_of_passing() {
    let (mut a, mut b) = (1i128, 1i128);
    while let Some(next) = a.checked_add(b) {
        (a, b) = (b, next);
    }
    let leaf = r#"{"op":"eq","left":{"fact":"a"},"right":{"fact":"a"}}"#;
    for (count, expected) in [(100, "pass"), (1000, "abstain")] {
        let claim = format!(
            "{{\"op\":\"all\",\"args\":[{}]}}",
            vec![leaf; count].join(",")
        );
        let input = format!(
            r#"{{"check":{{"schema":"instinct.check/v1","id":"budget","revision":1,"inputs":{{"a":"rational"}},"claim":{claim}}},"facts":{{"a":"{a}/{b}"}}}}"#
        );
        let (ok, out) = call("run", &input);
        assert!(ok, "{out}");
        assert!(
            out.contains(&format!("\"verdict\":\"{expected}\"")),
            "{out}"
        );
        if count == 1000 {
            assert!(out.contains("resource_limit"), "{out}");
        }
    }
}
// hlx:11
#[test]
fn rational_receipts_replay() {
    let input = request("lte", "-5/7", "2/9");
    let (ok, receipt) = call("receipt", &input);
    assert!(ok, "{receipt}");
    let (ok, out) = call(
        "replay",
        &format!("{{\"request\":{input},\"receipt\":{receipt}}}"),
    );
    assert!(ok, "{out}");
    assert!(out.contains("pass"), "{out}");
}