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 tests/check_hint.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::{
    fs,
    io::Write,
    process::{Command, Stdio},
    sync::atomic::{AtomicUsize, Ordering},
};
// hlx:3
static NEXT: AtomicUsize = AtomicUsize::new(0);
// hlx:4
struct Fixture(std::path::PathBuf);
impl Fixture {
    fn new() -> Self {
        let p = std::env::temp_dir().join(format!(
            "ichint-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        ));
        fs::create_dir(&p).unwrap();
        Self(p)
    }
    fn call(&self, args: &[&str], input: &str, hints: bool) -> String {
        let mut cmd = Command::new(env!("CARGO_BIN_EXE_instinct"));
        for (k, _) in std::env::vars_os() {
            if k.to_string_lossy().starts_with("INSTINCT_") {
                cmd.env_remove(k);
            }
        }
        let mut c = cmd
            .args(args)
            .current_dir(&self.0)
            .env("HOME", &self.0)
            .env("INSTINCT_STORE", self.0.join("bank.jsonl"))
            .env("INSTINCT_STARTER", "0")
            .env("INSTINCT_CHECK_HINTS", if hints { "1" } else { "0" })
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .unwrap();
        if !input.is_empty() {
            c.stdin.take().unwrap().write_all(input.as_bytes()).unwrap();
        }
        let out = c.wait_with_output().unwrap();
        assert!(
            out.status.success(),
            "{}",
            String::from_utf8_lossy(&out.stderr)
        );
        String::from_utf8(out.stdout).unwrap()
    }
    fn bank(&self) -> String {
        self.call(
            &[
                "bank",
                "--title",
                "Synthetic packing check",
                "--when",
                "synthetic packing flarble",
                "--found",
                "Use synthetic policy",
                "--how",
                "Inspect packing",
                "--evidence",
                "Fixture",
            ],
            "",
            false,
        )
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_owned()
    }
    fn attach(&self, id: &str) {
        self.call(&["check","attach","--lesson",id,"--store","bank.jsonl","--input","-"],r#"{"schema":"instinct.artifact-policy/v1","allowed":["PRIVATE_POLICY_NAME"],"assumptions":{"trusted_quiescent_staging":true}}"#,false);
    }
    fn prompt(&self, hints: bool) -> String {
        self.call(
            &["prompt"],
            r#"{"prompt":"synthetic packing flarble"}"#,
            hints,
        )
    }
}
impl Drop for Fixture {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.0);
    }
}
// hlx:5
#[test]
fn opt_in_hint_does_not_observe_or_write_feedback() {
    let f = Fixture::new();
    let id = f.bank();
    let ordinary = f.prompt(false);
    f.attach(&id);
    let bank = fs::read(f.0.join("bank.jsonl")).unwrap();
    let sidecar = fs::read(f.0.join("bank.jsonl.capabilities.hxdb")).unwrap();
    assert_eq!(ordinary, f.prompt(false));
    let hinted = f.prompt(true);
    assert!(
        hinted.contains("Attached check available; not evaluated"),
        "{hinted}"
    );
    assert!(hinted.contains("no action is authorized"));
    assert!(!hinted.contains("PRIVATE_POLICY_NAME"));
    assert_eq!(bank, fs::read(f.0.join("bank.jsonl")).unwrap());
    assert_eq!(
        sidecar,
        fs::read(f.0.join("bank.jsonl.capabilities.hxdb")).unwrap()
    );
}
// hlx:6
#[test]
fn detached_and_corrupt_attachments_do_not_hint() {
    let f = Fixture::new();
    let id = f.bank();
    f.attach(&id);
    assert!(f.prompt(true).contains("Attached check available"));
    f.call(
        &["check", "detach", "--lesson", &id, "--store", "bank.jsonl"],
        "",
        false,
    );
    assert!(!f.prompt(true).contains("Attached check available"));
    fs::write(f.0.join("bank.jsonl.capabilities.hxdb"), "bad").unwrap();
    assert!(!f.prompt(true).contains("Attached check available"));
}
// hlx:7
#[test]
fn copied_sidecar_cannot_attach_to_other_bank() {
    let a = Fixture::new();
    let b = Fixture::new();
    let id = a.bank();
    assert_eq!(id, b.bank());
    a.attach(&id);
    fs::copy(
        a.0.join("bank.jsonl.capabilities.hxdb"),
        b.0.join("bank.jsonl.capabilities.hxdb"),
    )
    .unwrap();
    assert!(!b.prompt(true).contains("Attached check available"));
}
// hlx:8
#[test]
fn native_bank_attachment_is_hinted_only_after_reattach() {
    let f = Fixture::new();
    let id = f.bank();
    f.call(
        &["migrate", "--store", "bank.jsonl", "--to", "bank.hxdb"],
        "",
        false,
    );
    let policy = r#"{"schema":"instinct.artifact-policy/v1","allowed":[],"assumptions":{"trusted_quiescent_staging":true}}"#;
    f.call(
        &[
            "check",
            "attach",
            "--lesson",
            &id,
            "--store",
            "bank.hxdb",
            "--input",
            "-",
        ],
        policy,
        false,
    );
    let out = f.call(
        &["prompt", "--store", "bank.hxdb"],
        r#"{"prompt":"synthetic packing flarble"}"#,
        true,
    );
    assert!(out.contains("Attached check available"), "{out}");
}
// hlx:9
#[test]
fn changed_lesson_fingerprint_suppresses_stale_hint() {
    let f = Fixture::new();
    let id = f.bank();
    f.attach(&id);
    let path = f.0.join("bank.jsonl");
    let before = fs::read_to_string(&path).unwrap();
    fs::write(
        &path,
        before.replace("Inspect packing", "Inspect packing again"),
    )
    .unwrap();
    let out = f.prompt(true);
    assert!(out.contains("Synthetic packing check"));
    assert!(!out.contains("Attached check available"));
}
// hlx:10
#[cfg(unix)]
#[test]
fn symlink_sidecar_is_ignored_without_disclosing_target() {
    use std::os::unix::fs::symlink;
    let f = Fixture::new();
    let _ = f.bank();
    symlink(
        "/unavailable/PRIVATE_TARGET",
        f.0.join("bank.jsonl.capabilities.hxdb"),
    )
    .unwrap();
    let out = f.prompt(true);
    assert!(!out.contains("Attached check available") && !out.contains("PRIVATE_TARGET"));
}
// hlx:11
#[test]
fn scoped_attachment_does_not_hint_in_another_project() {
    let f = Fixture::new();
    let out = f.call(
        &[
            "bank",
            "--title",
            "Scoped packing check",
            "--when",
            "synthetic packing flarble",
            "--found",
            "Fixture",
            "--how",
            "Inspect packing",
            "--evidence",
            "Fixture",
            "--project",
            "lab",
        ],
        "",
        false,
    );
    let id = out.split_whitespace().nth(1).unwrap();
    f.call(&["check","attach","--lesson",id,"--store","bank.jsonl","--input","-"],r#"{"schema":"instinct.artifact-policy/v1","allowed":[],"scope":{"project":"lab"},"assumptions":{"trusted_quiescent_staging":true}}"#,false);
    let event = r#"{"prompt":"synthetic packing flarble"}"#;
    let matched = f.call(&["prompt", "--project", "lab"], event, true);
    assert!(matched.contains("Attached check available"), "{matched}");
    let outside = f.call(&["prompt", "--project", "other"], event, true);
    assert!(
        !outside.contains("Attached check available") && !outside.contains("Scoped packing check")
    );
}
// hlx:12
#[test]
fn oversized_sidecar_is_ignored() {
    let f = Fixture::new();
    let _ = f.bank();
    fs::write(f.0.join("bank.jsonl.capabilities.hxdb"), vec![b'x'; 65537]).unwrap();
    assert!(!f.prompt(true).contains("Attached check available"));
}