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 src/demo.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 super::{parse_json, Json};
// hlx:3
use std::{
    env, fs,
    path::Path,
    process::Command,
    time::{SystemTime, UNIX_EPOCH},
};
// hlx:4
fn invoke(store: &Path, args: &[String]) -> Result<String, String> {
    // hlx:5
    let exe = env::current_exe().map_err(|e| e.to_string())?;
    // hlx:6
    let result = Command::new(exe)
        .env_clear()
        .env("INSTINCT_STORE", store)
        .args(args)
        .output()
        .map_err(|e| e.to_string())?;
    // hlx:7
    if !result.status.success() {
        return Err(String::from_utf8_lossy(&result.stderr).trim().to_owned());
    };
    // hlx:8
    String::from_utf8(result.stdout).map_err(|e| e.to_string())
    // hlx:9
}
// hlx:10
fn display(store: &Path, situation: &str) -> Result<usize, String> {
    // hlx:11
    let packet = parse_json(&invoke(
        store,
        &["decide".into(), "--q".into(), situation.into()],
    )?)?;
    // hlx:12
    let Some(Json::Arr(matches)) = packet.get("experiences") else {
        return Err("invalid decision response".into());
    };
    // hlx:13
    println!("\n{situation}");
    // hlx:14
    for experience in matches {
        // hlx:15
        println!(
            "  Try first: {}",
            experience
                .get("method")
                .and_then(Json::as_str)
                .ok_or("missing method")?
        );
        // hlx:16
        if let Some(boundary) = experience.get("reconsider_if").and_then(Json::as_str) {
            println!("  Reconsider: {boundary}");
        };
        // hlx:17
    }
    // hlx:18
    if matches.is_empty() {
        println!("  No applicable experience. Start from the current evidence.");
    };
    // hlx:19
    Ok(matches.len())
    // hlx:20
}
// hlx:21
fn seed(store: &Path, fixture: &Json) -> Result<(), String> {
    // hlx:22
    let Some(Json::Arr(bank)) = fixture.get("bank") else {
        return Err("missing example discoveries".into());
    };
    // hlx:23
    for item in bank {
        // hlx:24
        let mut args = vec!["bank".into()];
        // hlx:25
        for key in ["title", "when", "found", "how", "evidence", "reconsider"] {
            args.extend([
                format!("--{key}"),
                item.get(key)
                    .and_then(Json::as_str)
                    .ok_or("invalid example discovery")?
                    .to_string(),
            ]);
        }
        // hlx:26
        invoke(store, &args)?;
        // hlx:27
    }
    // hlx:28
    Ok(())
    // hlx:29
}
// hlx:30
fn play(store: &Path, query: Option<&str>) -> Result<(), String> {
    // hlx:31
    let learned = parse_json(include_str!("../bench/first-use/discoveries.json"))?;
    // hlx:32
    seed(store, &learned)?;
    // hlx:33
    if let Some(q) = query {
        // hlx:34
        let everyday = parse_json(include_str!("../bench/everyday/fixtures.json"))?;
        // hlx:35
        seed(store, &everyday)?;
        // hlx:36
        let original = parse_json(include_str!("../bench/backtest/fixtures.json"))?;
        // hlx:37
        seed(store, &original)?;
        // hlx:38
        display(store, q)?;
        // hlx:39
        return Ok(());
        // hlx:40
    }
    // hlx:41
    println!("Why am I explaining this again?\nTess already showed her last assistant which client update worked. A fresh agent can use that discovery when the next update comes along.");
    // hlx:42
    let hits = display(
        store,
        "Write my usual client update, please. This is Tess in a new chat.",
    )?;
    // hlx:43
    let empty = display(store, "Write a toast for my sister's birthday.")?;
    // hlx:44
    if hits == 0 || empty != 0 {
        return Err("demo failed its retrieval checks".into());
    };
    // hlx:45
    Ok(())
    // hlx:46
}
// hlx:47
pub fn run(query: Option<&str>) -> Result<(), String> {
    // hlx:48
    let nonce = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|e| e.to_string())?
        .as_nanos();
    // hlx:49
    let dir = env::temp_dir().join(format!("instinct-demo-{}-{nonce}", std::process::id()));
    // hlx:50
    fs::create_dir(&dir).map_err(|e| e.to_string())?;
    // hlx:51
    let result = play(&dir.join("examples.hxdb"), query);
    // hlx:52
    let cleanup = fs::remove_dir_all(&dir).map_err(|e| e.to_string());
    // hlx:53
    result.and(cleanup)
    // hlx:54
}