#[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::{
io::Write,
process::{Command, Output, Stdio},
sync::atomic::{AtomicUsize, Ordering},
};
static NEXT: AtomicUsize = AtomicUsize::new(0);
fn run(op: &str, raw: &str) -> Output {
let home = std::env::temp_dir().join(format!(
"iccompose-{}-{}",
std::process::id(),
NEXT.fetch_add(1, Ordering::Relaxed)
));
std::fs::create_dir(&home).unwrap();
let mut c = Command::new(env!("CARGO_BIN_EXE_instinct"));
for (k, _) in std::env::vars_os() {
if k.to_string_lossy().starts_with("INSTINCT_") {
c.env_remove(k);
}
}
let mut child = c
.args(["check", op, "--input", "-"])
.env("HOME", &home)
.env("INSTINCT_STORE", home.join("absent.hxdb"))
.current_dir(&home)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
child
.stdin
.take()
.unwrap()
.write_all(raw.as_bytes())
.unwrap();
let out = child.wait_with_output().unwrap();
assert_eq!(
std::fs::read_dir(&home).unwrap().count(),
0,
"pure composition wrote files"
);
std::fs::remove_dir(home).unwrap();
out
}
fn child(ty: &str, a: &str, b: &str, scope: &str, condition: bool) -> String {
format!(
r#"{{"check":{{"schema":"instinct.check/v1","id":"child","revision":1,"inputs":{{"a":"{ty}","b":"{ty}"}},"required_conditions":["ready"],"scope":{scope},"claim":{{"op":"eq","left":{{"fact":"a"}},"right":{{"fact":"b"}}}}}},"facts":{{"a":{a},"b":{b}}},"conditions":{{"ready":{condition}}}}}"#
)
}
fn envelope(children: &[String], scope: &str) -> String {
format!(
r#"{{"id":"combined","revision":1,"checks":[{}],"scope":{scope}}}"#,
children.join(",")
)
}
fn check(raw: &str, want: &str) {
let out = run("compose", raw);
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stdout)
);
let compiled = String::from_utf8(out.stdout).unwrap();
let validation = run("validate", &compiled);
assert!(validation.status.success());
let evaluated = run("run", &compiled);
assert!(evaluated.status.success());
assert!(
String::from_utf8_lossy(&evaluated.stdout).contains(&format!("\"verdict\":\"{want}\"")),
"{}",
String::from_utf8_lossy(&evaluated.stdout)
);
assert_eq!(run("compose", raw).stdout, compiled.as_bytes());
}
#[test]
fn independent_names_and_types_compile_to_existing_kernel() {
check(
&envelope(
&[
child("bool", "true", "true", "{}", true),
child(
"int",
"\"9007199254740993\"",
"\"9007199254740993\"",
"{}",
true,
),
],
"{}",
),
"pass",
);
}
#[test]
fn failure_and_required_condition_remain_distinct() {
for (condition, want) in [(true, "fail"), (false, "abstain")] {
check(
&envelope(
&[
child("bool", "true", "false", "{}", true),
child("bool", "true", "true", "{}", condition),
],
"{}",
),
want,
);
}
}
#[test]
fn scope_intersection_never_broadens_children() {
for key in ["project", "persona", "harness", "version", "machine"] {
let a = format!("{{\"{key}\":\"one\"}}");
let b = format!("{{\"{key}\":\"two\"}}");
let children = [
child("bool", "true", "true", &a, true),
child("bool", "true", "true", "{}", true),
];
check(&envelope(&children, &a), "pass");
check(&envelope(&children, "{}"), "abstain");
check(&envelope(&children, &b), "abstain");
let conflict = envelope(
&[children[0].clone(), child("bool", "true", "true", &b, true)],
&a,
);
assert!(!run("compose", &conflict).status.success());
}
}
#[test]
fn malformed_and_child_current_scope_are_rejected() {
let c = child("bool", "true", "true", "{}", true);
let scoped = format!("{},\"scope\":{{}}}}", &c[..c.len() - 1]);
for raw in [
envelope(&[scoped], "{}"),
envelope(&[c.clone()], "{}").replacen("\"revision\":1", "\"revision\":1,\"revision\":2", 1),
envelope(&[], "{}"),
envelope(&vec![c.clone(); 17], "{}"),
envelope(&[c.replace("\"op\":\"eq\"", "\"op\":\"unknown\"")], "{}"),
] {
let out = run("compose", &raw);
assert!(!out.status.success());
assert!(out.stdout.len() < 65536);
}
}
#[test]
fn missing_fact_is_not_filled_by_another_child() {
let c = child("bool", "true", "true", "{}", true);
let missing = c.replace(
"\"facts\":{\"a\":true,\"b\":true}",
"\"facts\":{\"a\":true}",
);
check(&envelope(&[missing, c], "{}"), "abstain");
}
#[test]
fn aggregate_node_limit_is_checked_not_just_each_child() {
let leaf = r#"{"op":"eq","left":{"fact":"a"},"right":{"fact":"b"}}"#;
let claim = format!(
"{{\"op\":\"all\",\"args\":[{}]}}",
vec![leaf; 800].join(",")
);
let request = child("bool", "true", "true", "{}", true).replace(leaf, &claim);
assert!(run("validate", &request).status.success());
let out = run("compose", &envelope(&[request.clone(), request], "{}"));
assert!(!out.status.success());
assert!(out.stdout.len() < 65536);
assert!(String::from_utf8_lossy(&out.stdout).contains("4096 nodes"));
}
#[test]
fn output_limit_refuses_large_compiled_facts() {
let value = format!("\"{}\"", "x".repeat(40000));
let request = child("string", &value, &value, "{}", true);
assert!(run("validate", &request).status.success());
let out = run("compose", &envelope(&[request], "{}"));
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stdout).contains("64 KiB"));
}
#[test]
fn maximum_length_names_are_namespaced_without_rejecting_valid_children() {
let name = "a".repeat(128);
let request =
child("bool", "true", "true", "{}", true).replace("\"a\"", &format!("\"{name}\""));
assert!(run("validate", &request).status.success());
check(&envelope(&[request], "{}"), "pass");
}
#[test]
fn escaped_duplicate_and_invalid_composition_identity_are_errors() {
let c = child("bool", "true", "true", "{}", true);
let raw = envelope(&[c], "{}");
for bad in [
raw.replacen("\"revision\":1", "\"revision\":1,\"revis\\u0069on\":2", 1),
raw.replacen("\"id\":\"combined\"", "\"id\":\"*\"", 1),
raw.replacen("\"revision\":1", "\"revision\":1e0", 1),
] {
assert!(!run("compose", &bad).status.success());
}
}
#[test]
fn composed_output_supports_existing_receipts() {
let raw = envelope(&[child("bool", "true", "true", "{}", true)], "{}");
let out = run("compose", &raw);
assert!(out.status.success());
let compiled = String::from_utf8(out.stdout).unwrap();
let receipt = run("receipt", &compiled);
assert!(receipt.status.success());
let replay = format!(
"{{\"request\":{},\"receipt\":{}}}",
compiled.trim(),
String::from_utf8(receipt.stdout).unwrap().trim()
);
let out = run("replay", &replay);
assert!(out.status.success());
assert!(String::from_utf8_lossy(&out.stdout).contains("consistent"));
}