instinct-cli 0.3.6

Local memory for AI coding agents: an MCP server and CLI that gives Claude Code, Codex, Cursor and any MCP client the one lesson that fits. Lessons that keep failing go quiet.
//! Rare-word gate: with 20 or more lessons, two shared words that are both in more than 10% of the bank's
//! trigger texts do not open the gate. One rarer shared word does. `INSTINCT_RARE=0` turns the gate off, and
//! a bank under 20 lessons never uses it.
use std::path::PathBuf;
use std::process::Command;

fn bin() -> &'static str {
    env!("CARGO_BIN_EXE_instinct")
}

fn store(name: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("instinct-rare-{}-{}-{name}", std::process::id(), uniq()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    dir.join("bank.hxdb")
}

fn run(store: &PathBuf, rare_off: bool, args: &[&str]) -> String {
    let mut c = Command::new(bin());
    for (k, _) in std::env::vars_os().filter(|(k, _)| k.to_string_lossy().starts_with("INSTINCT_")) {
        c.env_remove(k);
    }
    c.env("INSTINCT_STORE", store).args(args);
    if rare_off {
        c.env("INSTINCT_RARE", "0");
    }
    let out = c.output().unwrap();
    assert!(out.status.success(), "{args:?}: {}", String::from_utf8_lossy(&out.stderr));
    String::from_utf8(out.stdout).unwrap()
}

fn bank(store: &PathBuf, n: usize) {
    for i in 0..n {
        // Every lesson shares "garden orchard"; only the first has "zeppelin".
        let when = if i == 0 {
            "garden orchard zeppelin".to_string()
        } else {
            format!("garden orchard filler{i:02}word")
        };
        run(
            store,
            false,
            &[
                "bank",
                &format!("--title=Lesson number {i:02}"),
                &format!("--when={when}"),
                "--found=f",
                "--how=h",
                "--evidence=e",
            ],
        );
    }
}

#[test]
fn two_common_words_stay_silent_one_rare_word_opens() {
    let s = store("gate");
    bank(&s, 21);
    let two_common = run(&s, false, &["recall", "--q=garden orchard", "--k=3"]);
    assert!(!two_common.contains("Lesson number"), "{two_common}");
    let with_rare = run(&s, false, &["recall", "--q=garden zeppelin", "--k=1"]);
    assert!(with_rare.contains("Lesson number 00"), "{with_rare}");
    let gate_off = run(&s, true, &["recall", "--q=garden orchard", "--k=3"]);
    assert!(gate_off.contains("Lesson number"), "gate off: {gate_off}");
}

#[test]
fn small_banks_do_not_use_the_gate() {
    let s = store("small");
    bank(&s, 5);
    let out = run(&s, false, &["recall", "--q=garden orchard", "--k=3"]);
    assert!(out.contains("Lesson number"), "{out}");
}

/// A number no other call in this test process has seen, so parallel tests never share a folder.
fn uniq() -> usize {
    static N: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
    N.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}