#[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
}
use super::{append, decision_packet, eligibility, journal, json_out, load, now_secs, Args, Json};
use std::path::PathBuf;
#[derive(Clone, Debug, PartialEq)]
pub(super) struct StandingInstruction {
pub(super) text: String,
pub(super) reference: String,
}
pub(super) fn set_default(path: &PathBuf, args: &Args) -> Result<String, String> {
let id = args.pos.first().ok_or("default requires a lesson ID")?;
let project = args
.opt("project")
.filter(|p| !p.trim().is_empty())
.ok_or("default requires an explicit --project")?;
if args.pos.len() != 1 || args.opt("clear").is_some_and(|s| !s.is_empty()) {
return Err("supply one lesson ID and use --clear without a value".into());
}
let clear = args.flag("clear");
let instruction = match (clear, args.opt("instruction"), args.opt("source-ref")) {
(true, None, None) => None,
(false, Some(text), Some(reference))
if !text.trim().is_empty() && !reference.trim().is_empty() =>
{
Some(StandingInstruction {
text: text.trim().into(),
reference: reference.trim().into(),
})
}
_ => return Err("supply --instruction TEXT --source-ref REF together, or --clear".into()),
};
let _lock = journal::identity_lock(path)?;
let scope = eligibility::env_scope();
let lesson = load(path)?
.into_iter()
.find(|d| {
d.id == *id && d.meta.project == project && d.meta.source == "banked"
&& eligibility::scope_check(&d.meta, &scope, now_secs()).is_ok()
})
.ok_or("no eligible directly banked lesson in this project and scope")?;
if lesson.standing == instruction {
return Ok(format!("standing instruction already matches for {id}"));
}
let mut fields = vec![
("kind".into(), Json::Str("standing_instruction".into())),
("id".into(), Json::Str(id.clone())),
("ts".into(), Json::Num(now_secs() as f64)),
("scope_project".into(), Json::Str(lesson.meta.project)),
("scope_persona".into(), Json::Str(lesson.meta.persona)),
(
"action".into(),
Json::Str(if clear { "clear" } else { "set" }.into()),
),
];
if let Some(v) = instruction {
fields.push(("instruction".into(), Json::Str(v.text)));
fields.push(("source_ref".into(), Json::Str(v.reference)));
}
append(path, &json_out(&Json::Obj(fields)))?;
Ok(format!(
"standing instruction {} for {id}",
if clear { "cleared" } else { "recorded" }
))
}
pub(super) fn dream(
path: &PathBuf,
usual: &str,
changed: &str,
scope: &eligibility::Scope,
) -> Result<String, String> {
if usual.trim().is_empty() || changed.trim().is_empty() || usual.trim() == changed.trim() {
return Err("dream requires two different situations via --q and --changed".into());
}
let all = super::load_scoped(path, scope, now_secs())?;
let docs: Vec<_> = all.iter().map(super::Discovery::lex_text).collect();
let mut cases = Vec::new();
for (name, q) in [("usual", usual), ("changed", changed)] {
let methods =
super::rank_methods(path, q, 2, scope, &all, |action| super::bm25(action, &docs))?;
cases.push(Json::Obj(vec![
("case".into(), Json::Str(name.into())),
("situation".into(), Json::Str(q.into())),
("context".into(), decision_packet(&methods)),
]));
}
Ok(json_out(&Json::Obj(vec![
("schema".into(), Json::Str("instinct-rehearsal/1".into())),
("hypothetical".into(), Json::Bool(true)),
("cases".into(), Json::Arr(cases)),
])))
}