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.
//! `instinct connect claude`: a stand-in `claude` on PATH records its arguments; without one, the hooks land in
//! the project's `.claude/settings.local.json`.
#![cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::process::Command;

fn tmp(name: &str) -> PathBuf {
    let d = std::env::temp_dir().join(format!("instinct-connect-{}-{name}", std::process::id()));
    let _ = std::fs::remove_dir_all(&d);
    std::fs::create_dir_all(&d).unwrap();
    d
}

fn run(dir: &PathBuf, path: &str, args: &[&str]) -> (i32, String) {
    let o = Command::new(env!("CARGO_BIN_EXE_instinct"))
        .args(["connect"])
        .args(args)
        .current_dir(dir)
        .env("PATH", path)
        .env("HOME", dir.join("home"))
        .output()
        .unwrap();
    (o.status.code().unwrap(), String::from_utf8_lossy(&o.stdout).into_owned() + &String::from_utf8_lossy(&o.stderr))
}

fn fake_claude(dir: &PathBuf, exit: i32) -> (String, PathBuf) {
    let bin = dir.join("bin");
    std::fs::create_dir_all(&bin).unwrap();
    let log = dir.join("calls.log");
    let p = bin.join("claude");
    std::fs::write(&p, format!("#!/bin/sh\necho \"$@\" >> {}\nexit {exit}\n", log.display())).unwrap();
    std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
    (bin.display().to_string(), log)
}

#[test]
fn with_the_claude_cli_it_installs_both_plugins_at_local_scope_by_default() {
    let d = tmp("cli");
    let (bin, log) = fake_claude(&d, 0);
    let (code, out) = run(&d, &format!("{bin}:/usr/bin:/bin"), &["claude"]);
    assert_eq!(code, 0, "{out}");
    let calls = std::fs::read_to_string(&log).unwrap();
    assert!(calls.contains("plugin marketplace add koscak-labs/instinct --scope local"), "{calls}");
    assert!(calls.contains("plugin install instinct@koscak-labs --scope local"), "{calls}");
    assert!(calls.contains("plugin install instinct-autorecall@koscak-labs --scope local"), "{calls}");
    assert!(!calls.contains("--scope user"));
    assert!(!d.join(".claude").exists(), "no settings file is written when the CLI does the work");
    std::fs::remove_file(&log).unwrap();
    let (code, _) = run(&d, &format!("{bin}:/usr/bin:/bin"), &["claude", "--user"]);
    assert_eq!(code, 0);
    assert!(std::fs::read_to_string(&log).unwrap().contains("plugin install instinct@koscak-labs --scope user"));
}

#[test]
fn a_failing_claude_step_is_an_error() {
    let d = tmp("fail");
    let (bin, _) = fake_claude(&d, 1);
    let (code, out) = run(&d, &format!("{bin}:/usr/bin:/bin"), &["claude"]);
    assert_ne!(code, 0, "{out}");
}

#[test]
fn without_the_cli_hooks_go_into_settings_local_json_and_the_mcp_command_is_printed() {
    let d = tmp("nocli");
    let empty = d.join("empty");
    std::fs::create_dir_all(&empty).unwrap();
    let (code, out) = run(&d, &empty.display().to_string(), &["claude"]);
    assert_eq!(code, 0, "{out}");
    let s = std::fs::read_to_string(d.join(".claude/settings.local.json")).unwrap();
    assert!(s.contains("instinct guard") && s.contains("instinct prompt") && s.contains("\"matcher\":\"Bash\""), "{s}");
    assert!(out.contains("claude mcp add --transport stdio --scope local instinct -- instinct mcp"), "{out}");
    // Again: nothing to change.
    let (_, again) = run(&d, &empty.display().to_string(), &["claude"]);
    assert!(again.contains("already has both"), "{again}");
}