#[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 std::process::Command;
use std::path::{Path,PathBuf};
use std::time::Instant;
fn run(cmd:&mut Command)->String {
let out=cmd.output().expect("start command");
assert!(out.status.success(),"command failed: {}",String::from_utf8_lossy(&out.stderr));
String::from_utf8(out.stdout).expect("UTF-8 output").trim().to_owned()
}
fn field(path:&Path,filter:&str)->String {
run(Command::new("jq").args(["-er",filter]).arg(path))
}
fn ask(route:&str,model:&str,work:&Path,prompt:&str)->String {
let mut cmd=Command::new("timeout");
cmd.arg("90").arg(route).current_dir(&work);
if route=="claude" {cmd.args(["-p","--model",model,"--tools","","--settings","{\"disableAllHooks\":true}","--setting-sources","","--strict-mcp-config","--no-session-persistence"]);}else{cmd.args(["exec","--ephemeral","--ignore-user-config","--skip-git-repo-check","-s","read-only" ,"-m",model,"-c","features.shell_tool=false","-o","answer.txt"]);};
let output=cmd.arg(prompt).output().expect("run harness");
let answer=if !output.status.success(){{eprintln!("{}",String::from_utf8_lossy(&output.stderr)); format!("ERROR exit {}",output.status)}}else if route=="codex"{std::fs::read_to_string(work.join("answer.txt")).unwrap()}else{String::from_utf8_lossy(&output.stdout).to_string()};
let answer=answer.trim().replace(['\n','\t']," ");
answer
}
fn main() {
let root=PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let engine=std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().join("instinct");
let route=std::env::args().nth(1).unwrap_or_else(||"claude".into());
assert!(matches!(route.as_str(),"claude"|"codex"));
let model=std::env::args().nth(2).unwrap_or_else(||if route=="claude"{"haiku".into()}else{"gpt-6.2-sol".into()});
let work=PathBuf::from(run(Command::new("mktemp").arg("-d")));
let mode=std::env::args().nth(3).unwrap_or_else(||"framed".into());
assert!(matches!(mode.as_str(),"raw"|"framed"|"prepared"));
let cases=root.join(if mode=="prepared"{"bench/fresh-cases.json"}else{"bench/cases.json"});
let discovery=root.join("bench/discovery.json");
let bank=|args:&[String]|run(Command::new(&engine).env_clear().env("INSTINCT_STORE",work.join("bank.jsonl")).args(args));
let mut args=vec!["bank".to_string()];
for key in ["title","when","found","how","evidence"] {args.extend([format!("--{key}"),field(&discovery,&format!(".{key}"))]);};
if mode=="prepared" {args.extend(["--tags".to_string(),std::fs::read_to_string(root.join("bench/transfer-cues.txt")).unwrap()]);};
bank(&args);
let static_method=field(&discovery,".how");
println!("route\tmodel\tcase\tarm\trecall_ms\tcontext_bytes\tdecision_ms\tfirst_correct\texact_pass\tanswer");
for (index,arm) in [(0,"base"),(1,"static"),(0,"live"),(1,"base"),(0,"static"),(1,"live")] {
let question=field(&cases,&format!(".[{index}].question"));
let expected=field(&cases,&format!(".[{index}].key"));
let name=field(&cases,&format!(".[{index}].name"));
let start=Instant::now();
let cue=if arm=="live" && mode=="framed"{ask(&route,&model,&work,&format!("Describe the underlying decision situation in one sentence and six reusable search terms from other domains. Include any condition that changes the appropriate method. Do not solve the case. Case: {question}"))}else{String::new()};
assert!(!cue.starts_with("ERROR"),"situation extraction failed");
let lookup=Instant::now();
let context=match arm {"live"=>bank(&["recall".into(),"--q".into(),format!("{cue} {question}"),"--k".into(),"1".into()]),"static"=>static_method.clone(),_=>String::new()};
let retrieval=lookup.elapsed().as_millis();
let prompt=format!("Use only the supplied case. Optional previous experience: {context}\nCase: {question}\nReturn only the extract letter and the integer, separated by one space.");
let answer=ask(&route,&model,&work,&prompt);
println!("{route}\t{model}\t{name}\t{arm}\t{retrieval}\t{}\t{}\t{}\t{}\t{}",context.len(),start.elapsed().as_millis(),answer.split_whitespace().next()==expected.split_whitespace().next(),answer==expected,answer);
};
std::fs::remove_dir_all(work).unwrap();
}