#[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::{parse_json, Json};
use std::{
env, fs,
path::Path,
process::Command,
time::{SystemTime, UNIX_EPOCH},
};
fn invoke(store: &Path, args: &[String]) -> Result<String, String> {
let exe = env::current_exe().map_err(|e| e.to_string())?;
let result = Command::new(exe)
.env_clear()
.env("INSTINCT_STORE", store)
.args(args)
.output()
.map_err(|e| e.to_string())?;
if !result.status.success() {
return Err(String::from_utf8_lossy(&result.stderr).trim().to_owned());
};
String::from_utf8(result.stdout).map_err(|e| e.to_string())
}
fn display(store: &Path, situation: &str) -> Result<usize, String> {
let packet = parse_json(&invoke(
store,
&["decide".into(), "--q".into(), situation.into()],
)?)?;
let Some(Json::Arr(matches)) = packet.get("experiences") else {
return Err("invalid decision response".into());
};
println!("\n{situation}");
for experience in matches {
println!(
" Try first: {}",
experience
.get("method")
.and_then(Json::as_str)
.ok_or("missing method")?
);
if let Some(boundary) = experience.get("reconsider_if").and_then(Json::as_str) {
println!(" Reconsider: {boundary}");
};
}
if matches.is_empty() {
println!(" No applicable experience. Start from the current evidence.");
};
Ok(matches.len())
}
fn seed(store: &Path, fixture: &Json) -> Result<(), String> {
let Some(Json::Arr(bank)) = fixture.get("bank") else {
return Err("missing example discoveries".into());
};
for item in bank {
let mut args = vec!["bank".into()];
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(),
]);
}
invoke(store, &args)?;
}
Ok(())
}
fn play(store: &Path, query: Option<&str>) -> Result<(), String> {
let learned = parse_json(include_str!("../bench/first-use/discoveries.json"))?;
seed(store, &learned)?;
if let Some(q) = query {
let everyday = parse_json(include_str!("../bench/everyday/fixtures.json"))?;
seed(store, &everyday)?;
let original = parse_json(include_str!("../bench/backtest/fixtures.json"))?;
seed(store, &original)?;
display(store, q)?;
return Ok(());
}
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.");
let hits = display(
store,
"Write my usual client update, please. This is Tess in a new chat.",
)?;
let empty = display(store, "Write a toast for my sister's birthday.")?;
if hits == 0 || empty != 0 {
return Err("demo failed its retrieval checks".into());
};
Ok(())
}
pub fn run(query: Option<&str>) -> Result<(), String> {
let nonce = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| e.to_string())?
.as_nanos();
let dir = env::temp_dir().join(format!("instinct-demo-{}-{nonce}", std::process::id()));
fs::create_dir(&dir).map_err(|e| e.to_string())?;
let result = play(&dir.join("examples.hxdb"), query);
let cleanup = fs::remove_dir_all(&dir).map_err(|e| e.to_string());
result.and(cleanup)
}