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.
// generated by hxc 1.0.0-acgt-compiler from src/journal.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::{self, File, OpenOptions};
// hlx:3
use std::io::{BufRead, BufReader, ErrorKind, Read, Seek, SeekFrom, Write};
// hlx:4
use std::path::Path;
// hlx:6
pub fn private_options() -> OpenOptions {
    // hlx:7
    #[cfg(unix)]
    // hlx:8
    {
        use std::os::unix::fs::OpenOptionsExt;
        let mut opts = OpenOptions::new();
        opts.mode(0o600);
        opts
    }
    // hlx:9
    #[cfg(not(unix))]
    // hlx:10
    {
        OpenOptions::new()
    }
    // hlx:11
}
// hlx:13
pub fn read(path: &Path) -> Result<String, String> {
    // hlx:14
    if super::helix_db::is_native(path) {
        // hlx:15
        return super::helix_db::read(path);
        // hlx:16
    }
    // hlx:17
    let mut file = match File::open(path) {
        // hlx:18
        Ok(file) => file,
        // hlx:19
        Err(e) if e.kind() == ErrorKind::NotFound => return Ok(String::new()),
        // hlx:20
        Err(e) => return Err(format!("cannot read {}: {e}", path.display())),
        // hlx:21
    };
    // hlx:22
    // hlx:23
    file.lock_shared()
        .map_err(|e| format!("cannot lock {}: {e}", path.display()))?;
    // hlx:24
    let mut text = String::new();
    // hlx:25
    file.read_to_string(&mut text)
        .map_err(|e| format!("cannot read {}: {e}", path.display()))?;
    // hlx:26
    Ok(text)
    // hlx:27
}
// hlx:29
pub fn append(path: &Path, line: &str) -> Result<(), String> {
    // hlx:30
    if super::helix_db::is_native(path) {
        // hlx:31
        return super::helix_db::append(path, line);
        // hlx:32
    }
    // hlx:33
    if let Some(dir) = path.parent() {
        // hlx:34
        fs::create_dir_all(dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?;
        // hlx:35
    }
    // hlx:36
    let mut file = private_options()
        .create(true)
        .read(true)
        .append(true)
        .open(path)
        .map_err(|e| format!("cannot open {}: {e}", path.display()))?;
    // hlx:37
    let error = |e: std::io::Error| format!("cannot append to {}: {e}", path.display());
    // hlx:38
    file.lock().map_err(error)?;
    // hlx:39
    let mut record = String::with_capacity(line.len() + 2);
    // hlx:40
    if file.metadata().map_err(error)?.len() > 0 {
        // hlx:41
        file.seek(SeekFrom::End(-1)).map_err(error)?;
        // hlx:42
        let mut last = [0];
        // hlx:43
        file.read_exact(&mut last).map_err(error)?;
        // hlx:44
        if last[0] != b'\n' {
            // hlx:46
            file.rewind().map_err(error)?;
            // hlx:47
            let tail = BufReader::new(&mut file)
                .lines()
                .last()
                .transpose()
                .map_err(error)?
                .unwrap_or_default();
            // hlx:48
            if !tail.trim().is_empty() {
                // hlx:49
                super::parse_json(&tail)
                    .map_err(|e| format!("invalid final event in {}: {e}", path.display()))?;
                // hlx:50
            }
            // hlx:51
            record.push('\n');
            // hlx:52
        }
        // hlx:53
    }
    // hlx:54
    record.push_str(line);
    // hlx:55
    record.push('\n');
    // hlx:56
    file.write_all(record.as_bytes()).map_err(error)?;
    // hlx:57
    file.sync_data().map_err(error)
    // hlx:58
}
// hlx:61
pub fn identity_lock(path: &Path) -> Result<File, String> {
    // hlx:62
    if let Some(parent) = path.parent() {
        // hlx:63
        fs::create_dir_all(parent).map_err(|e| e.to_string())?;
        // hlx:64
    }
    // hlx:65
    let mut lock_path = path.as_os_str().to_owned();
    // hlx:66
    lock_path.push(".lock");
    // hlx:67
    let lock = private_options()
        .create(true)
        .truncate(false)
        .read(true)
        .write(true)
        .open(lock_path)
        .map_err(|e| e.to_string())?;
    // hlx:68
    lock.lock().map_err(|e| e.to_string())?;
    // hlx:69
    Ok(lock)
    // hlx:70
}