instinct-cli 0.3.9

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/rehearsal.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::{append, decision_packet, eligibility, journal, json_out, load, now_secs, Args, Json};
// hlx:3
use std::path::PathBuf;
// hlx:5
#[derive(Clone, Debug, PartialEq)]
// hlx:6
pub(super) struct StandingInstruction {
    // hlx:7
    pub(super) text: String,
    // hlx:8
    pub(super) reference: String,
    // hlx:9
}
// hlx:11
pub(super) fn set_default(
    path: &PathBuf,
    args: &Args,
    scope: &eligibility::Scope,
) -> Result<String, String> {
    // hlx:12
    let id = args.pos.first().ok_or("default requires a lesson ID")?;
    // hlx:13
    let project = args
        .opt("project")
        .filter(|p| !p.trim().is_empty())
        .ok_or("default requires an explicit --project")?;
    // hlx:14
    if args.pos.len() != 1 || args.opt("clear").is_some_and(|s| !s.is_empty()) {
        // hlx:15
        return Err("supply one lesson ID and use --clear without a value".into());
        // hlx:16
    }
    // hlx:17
    let clear = args.flag("clear");
    // hlx:18
    let instruction = match (clear, args.opt("instruction"), args.opt("source-ref")) {
        // hlx:19
        (true, None, None) => None,
        // hlx:20
        (false, Some(text), Some(reference))
            if !text.trim().is_empty() && !reference.trim().is_empty() =>
        // hlx:21
        {
            Some(StandingInstruction {
                text: text.trim().into(),
                reference: reference.trim().into(),
            })
        }
        // hlx:22
        _ => return Err("supply --instruction TEXT --source-ref REF together, or --clear".into()),
        // hlx:23
    };
    // hlx:24
    let _lock = journal::identity_lock(path)?;
    // hlx:25
    let lesson = load(path)?
        .into_iter()
        .find(|d| {
            d.id == *id && d.meta.project == project && d.meta.source == "banked"
    // hlx:26
    && eligibility::scope_check(&d.meta, scope, now_secs()).is_ok()
        })
        // hlx:27
        .ok_or("no eligible directly banked lesson in this project and scope")?;
    // hlx:28
    if lesson.standing == instruction {
        // hlx:29
        return Ok(format!("standing instruction already matches for {id}"));
        // hlx:30
    }
    // hlx:31
    let mut fields = vec![
        // hlx:32
        ("kind".into(), Json::Str("standing_instruction".into())),
        // hlx:33
        ("id".into(), Json::Str(id.clone())),
        ("ts".into(), Json::Num(now_secs() as f64)),
        // hlx:34
        ("scope_project".into(), Json::Str(lesson.meta.project)),
        ("scope_persona".into(), Json::Str(lesson.meta.persona)),
        // hlx:35
        (
            "action".into(),
            Json::Str(if clear { "clear" } else { "set" }.into()),
        ),
        // hlx:36
    ];
    // hlx:37
    if let Some(v) = instruction {
        // hlx:38
        fields.push(("instruction".into(), Json::Str(v.text)));
        // hlx:39
        fields.push(("source_ref".into(), Json::Str(v.reference)));
        // hlx:40
    }
    // hlx:41
    append(path, &json_out(&Json::Obj(fields)))?;
    // hlx:42
    Ok(format!(
        "standing instruction {} for {id}",
        if clear { "cleared" } else { "recorded" }
    ))
    // hlx:43
}
// hlx:46
pub(super) fn dream(
    path: &PathBuf,
    usual: &str,
    changed: &str,
    scope: &eligibility::Scope,
) -> Result<String, String> {
    // hlx:47
    if usual.trim().is_empty() || changed.trim().is_empty() || usual.trim() == changed.trim() {
        // hlx:48
        return Err("dream requires two different situations via --q and --changed".into());
        // hlx:49
    }
    // hlx:50
    let all = super::load_scoped(path, scope, now_secs())?;
    // hlx:51
    let docs: Vec<_> = all.iter().map(super::Discovery::lex_text).collect();
    // hlx:52
    let mut cases = Vec::new();
    // hlx:53
    for (name, q) in [("usual", usual), ("changed", changed)] {
        // hlx:54
        let methods =
            super::rank_methods(path, q, 2, scope, &all, |action| super::bm25(action, &docs))?;
        // hlx:55
        cases.push(Json::Obj(vec![
            // hlx:56
            ("case".into(), Json::Str(name.into())),
            ("situation".into(), Json::Str(q.into())),
            // hlx:57
            ("context".into(), decision_packet(&methods)),
            // hlx:58
        ]));
        // hlx:59
    }
    // hlx:60
    Ok(json_out(&Json::Obj(vec![
        // hlx:61
        ("schema".into(), Json::Str("instinct-rehearsal/1".into())),
        ("hypothetical".into(), Json::Bool(true)),
        // hlx:62
        ("cases".into(), Json::Arr(cases)),
        // hlx:63
    ])))
    // hlx:64
}