#[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, Stdio};
use std::io::Write;
use std::time::Instant;
fn members(mask: u8) -> String {
(0..3)
.filter(|i| mask & (1 << i) != 0)
.map(|i| format!("\"item{}\"", i))
.collect::<Vec<_>>()
.join(",")
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let binary = std::env::args()
.nth(1)
.ok_or("usage: capability-subset /absolute/path/to/instinct")?;
let root =
std::env::temp_dir().join(format!("instinct-capability-subset-{}", std::process::id()));
std::fs::create_dir(&root)?;
let mut total = 0u64;
let mut failures = 0u64;
let mut elapsed = Vec::new();
for actual in 0u8..8 {
for allowed in 0u8..8 {
let expected = if actual & !allowed == 0 {
"pass"
} else {
"fail"
};
let request = format!(
r#"{{"check":{{"schema":"instinct.check/v1","id":"subset-oracle","revision":1,"inputs":{{"actual":"set<string>","allowed":"set<string>"}},"claim":{{"op":"subset","left":{{"fact":"actual"}},"right":{{"fact":"allowed"}}}}}},"facts":{{"actual":[{}],"allowed":[{}]}}}}"#,
members(actual),
members(allowed)
);
let start = Instant::now();
let mut command = Command::new(&binary);
for (key, _) in std::env::vars_os() {
if key.to_string_lossy().starts_with("INSTINCT_") {
command.env_remove(key);
}
}
let mut child = command
.args(["check", "run", "--input", "-"])
.env("HOME", &root)
.env("USERPROFILE", &root)
.env("XDG_DATA_HOME", &root)
.env("LOCALAPPDATA", &root)
.current_dir(&root)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
child
.stdin
.take()
.ok_or("missing child stdin")?
.write_all(request.as_bytes())?;
let output = child.wait_with_output()?;
elapsed.push(start.elapsed().as_micros());
let text = String::from_utf8(output.stdout)?;
let verdict = format!("\"verdict\":\"{}\"", expected);
let spaced = format!("\"verdict\": \"{}\"", expected);
if !output.status.success()
|| !(text.contains(&verdict) || text.contains(&spaced))
|| !text.contains("not_evaluated")
|| !text.contains("caller_reported")
{
failures += 1;
eprintln!("FAIL actual={actual} allowed={allowed} expected={expected} output={text} stderr={}", String::from_utf8_lossy(&output.stderr));
}
total += 1;
}
}
elapsed.sort_unstable();
println!("pairs={total} failures={failures} cli_median_us={} cli_p90_us={} (process startup included; no speedup claim)", elapsed[elapsed.len()/2], elapsed[(elapsed.len()*9/10).min(elapsed.len()-1)]);
let unexpected_files = std::fs::read_dir(&root)?.count();
std::fs::remove_dir(&root)?;
if failures != 0 || unexpected_files != 0 {
return Err(format!(
"oracle failures={failures}, unexpected home entries={unexpected_files}"
)
.into());
}
Ok(())
}