#[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
}
#[cfg(unix)]
mod binding {
use std::{
fs,
io::Write,
path::PathBuf,
process::{Command, Output, Stdio},
sync::atomic::{AtomicUsize, Ordering},
};
static NEXT: AtomicUsize = AtomicUsize::new(0);
struct Fixture {
root: PathBuf,
}
impl Fixture {
fn new() -> Self {
let root = std::env::temp_dir().join(format!(
"icbind-{}-{}",
std::process::id(),
NEXT.fetch_add(1, Ordering::Relaxed)
));
fs::create_dir(&root).unwrap();
fs::create_dir(root.join("stage")).unwrap();
fs::write(root.join("stage/release.bin"), b"fixture").unwrap();
Self { root }
}
fn run(&self, args: &[&str], input: &str) -> Output {
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(args)
.current_dir(&self.root)
.env("HOME", &self.root)
.env("INSTINCT_STARTER", "0")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
child.stdin.take().unwrap().write_all(input.as_bytes()).ok();
child.wait_with_output().unwrap()
}
fn bank(&self, name: &str) -> String {
let o = self.run(
&[
"bank",
"--store",
name,
"--title",
"Bound inventory",
"--when",
"check staged artifact membership",
"--found",
"Unexpected entries cause failure",
"--how",
"Compare approved names",
"--evidence",
"Synthetic binding control",
],
"",
);
assert!(o.status.success(), "{:?}", o);
String::from_utf8(o.stdout)
.unwrap()
.split_whitespace()
.nth(1)
.unwrap()
.to_owned()
}
fn attach(&self, name: &str, id: &str) -> Output {
self.run(
&[
"check", "attach", "--store", name, "--lesson", id, "--input", "-",
],
r#"{"schema":"instinct.artifact-policy/v1","allowed":["release.bin"]}"#,
)
}
fn hit(&self, name: &str, id: &str) -> Output {
self.run(
&[
"hit",
id,
"--store",
name,
"--check",
"--root",
self.root.join("stage").to_str().unwrap(),
"--trusted-staging",
"--outcome-id",
"binding-probe",
],
"",
)
}
}
impl Drop for Fixture {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.root);
}
}
#[test]
fn copied_attachment_requires_explicit_reattach_to_destination_bank() {
let f = Fixture::new();
let id = f.bank("a.jsonl");
assert_eq!(id, f.bank("b.jsonl"));
assert!(f.attach("a.jsonl", &id).status.success());
fs::copy(
f.root.join("a.jsonl.capabilities.hxdb"),
f.root.join("b.jsonl.capabilities.hxdb"),
)
.unwrap();
let before = fs::read(f.root.join("b.jsonl")).unwrap();
let refused = f.hit("b.jsonl", &id);
assert!(
!refused.status.success(),
"copied attachment accepted: {:?}",
refused
);
assert_eq!(fs::read(f.root.join("b.jsonl")).unwrap(), before);
assert!(String::from_utf8_lossy(&refused.stderr).contains("bank binding"));
assert!(f.attach("b.jsonl", &id).status.success());
assert!(f.hit("b.jsonl", &id).status.success());
assert!(f.hit("a.jsonl", &id).status.success());
}
#[test]
fn relative_and_absolute_spellings_share_bank_binding() {
let f = Fixture::new();
let id = f.bank("bank.jsonl");
assert!(f.attach("./bank.jsonl", &id).status.success());
let bank = f.root.join("bank.jsonl");
assert!(f.hit(bank.to_str().unwrap(), &id).status.success());
}
#[test]
fn moved_bank_keeps_plain_feedback_but_requires_reattach_for_checked_hits() {
let f = Fixture::new();
let id = f.bank("old.jsonl");
assert!(f.attach("old.jsonl", &id).status.success());
fs::rename(f.root.join("old.jsonl"), f.root.join("moved.jsonl")).unwrap();
fs::rename(
f.root.join("old.jsonl.capabilities.hxdb"),
f.root.join("moved.jsonl.capabilities.hxdb"),
)
.unwrap();
assert!(f
.run(
&[
"hit",
&id,
"--store",
"moved.jsonl",
"--outcome-id",
"ordinary"
],
""
)
.status
.success());
let refused = f.hit("moved.jsonl", &id);
assert!(!refused.status.success());
let message = String::from_utf8_lossy(&refused.stderr);
assert!(
message.contains("instinct check attach --lesson")
&& message.contains("--store 'moved.jsonl' --input -"),
"{message}"
);
assert!(f.attach("moved.jsonl", &id).status.success());
assert!(f.hit("moved.jsonl", &id).status.success());
}
#[test]
fn symlink_alias_rename_preserves_target_binding() {
let f = Fixture::new();
let id = f.bank("target.jsonl");
std::os::unix::fs::symlink("target.jsonl", f.root.join("alias.jsonl")).unwrap();
assert!(f.attach("alias.jsonl", &id).status.success());
fs::rename(f.root.join("alias.jsonl"), f.root.join("alias-new.jsonl")).unwrap();
fs::rename(
f.root.join("alias.jsonl.capabilities.hxdb"),
f.root.join("alias-new.jsonl.capabilities.hxdb"),
)
.unwrap();
assert!(f.hit("alias-new.jsonl", &id).status.success());
}
#[test]
fn detach_preserves_history_blocks_future_checks_and_reattaches() {
let f = Fixture::new();
let id = f.bank("bank.jsonl");
assert!(f.attach("bank.jsonl", &id).status.success());
assert!(f.hit("bank.jsonl", &id).status.success());
let bank = fs::read(f.root.join("bank.jsonl")).unwrap();
let side = f.root.join("bank.jsonl.capabilities.hxdb");
let before = fs::read(&side).unwrap();
let args = ["check", "detach", "--store", "bank.jsonl", "--lesson", &id];
let detached = f.run(&args, "");
assert!(detached.status.success(), "{detached:?}");
let after = fs::read(&side).unwrap();
assert!(after.starts_with(&before) && after.len() > before.len());
assert_eq!(bank, fs::read(f.root.join("bank.jsonl")).unwrap());
assert!(!f.hit("bank.jsonl", &id).status.success());
assert!(f.run(&args, "").status.success());
assert_eq!(after, fs::read(&side).unwrap());
assert!(f
.run(
&[
"hit",
&id,
"--store",
"bank.jsonl",
"--outcome-id",
"plain-after-detach"
],
""
)
.status
.success());
assert!(f.attach("bank.jsonl", &id).status.success());
let checked = f.run(
&[
"hit",
&id,
"--store",
"bank.jsonl",
"--check",
"--root",
f.root.join("stage").to_str().unwrap(),
"--trusted-staging",
"--outcome-id",
"reattached",
],
"",
);
assert!(checked.status.success(), "{checked:?}");
let export = f.run(&["export", "--store", side.to_str().unwrap()], "");
let text = String::from_utf8(export.stdout).unwrap();
assert_eq!(text.lines().count(), 3);
assert!(text.lines().last().unwrap().contains("\"revision\":3"));
}
#[test]
fn detach_requires_explicit_options_and_does_not_create_an_attachment() {
let f = Fixture::new();
let id = f.bank("bank.jsonl");
for args in [
vec!["check", "detach", "--lesson", &id],
vec!["check", "detach", "--store", "bank.jsonl"],
vec![
"check",
"detach",
"--store",
"bank.jsonl",
"--lesson",
&id,
"--root",
"/tmp",
],
] {
assert!(!f.run(&args, "").status.success());
}
assert!(f
.run(
&["check", "detach", "--store", "bank.jsonl", "--lesson", &id],
""
)
.status
.success());
assert!(!f.root.join("bank.jsonl.capabilities.hxdb").exists());
}
#[test]
fn detach_rejects_foreign_binding_without_mutation() {
let f = Fixture::new();
let id = f.bank("a.jsonl");
assert_eq!(id, f.bank("b.jsonl"));
assert!(f.attach("a.jsonl", &id).status.success());
fs::copy(
f.root.join("a.jsonl.capabilities.hxdb"),
f.root.join("b.jsonl.capabilities.hxdb"),
)
.unwrap();
let side = f.root.join("b.jsonl.capabilities.hxdb");
let before = fs::read(&side).unwrap();
let out = f.run(
&["check", "detach", "--lesson", &id, "--store", "b.jsonl"],
"",
);
assert!(!out.status.success());
assert!(
String::from_utf8_lossy(&out.stdout).contains("bank binding"),
"{out:?}"
);
assert_eq!(before, fs::read(side).unwrap());
}
#[test]
fn detach_unknown_lesson_and_duplicate_options_write_no_sidecar() {
let f = Fixture::new();
let id = f.bank("bank.jsonl");
for args in [
vec![
"check",
"detach",
"--lesson",
"in-unknown",
"--store",
"bank.jsonl",
],
vec![
"check",
"detach",
"--lesson",
&id,
"--lesson",
&id,
"--store",
"bank.jsonl",
],
vec![
"check",
"detach",
"--lesson",
&id,
"--store",
"bank.jsonl",
"--store",
"bank.jsonl",
],
] {
assert!(!f.run(&args, "").status.success());
}
assert!(!f.root.join("bank.jsonl.capabilities.hxdb").exists());
}
#[test]
fn concurrent_detach_appends_one_revocation() {
let f = Fixture::new();
let id = f.bank("bank.jsonl");
assert!(f.attach("bank.jsonl", &id).status.success());
std::thread::scope(|scope| {
let jobs: Vec<_> = (0..8)
.map(|_| {
scope.spawn(|| {
f.run(
&["check", "detach", "--lesson", &id, "--store", "bank.jsonl"],
"",
)
})
})
.collect();
for job in jobs {
let out = job.join().unwrap();
assert!(out.status.success(), "{out:?}");
}
});
let export = f.run(
&[
"export",
"--store",
f.root
.join("bank.jsonl.capabilities.hxdb")
.to_str()
.unwrap(),
],
"",
);
assert!(export.status.success());
let text = String::from_utf8(export.stdout).unwrap();
assert_eq!(text.lines().count(), 2);
assert!(!f.hit("bank.jsonl", &id).status.success());
}
#[test]
fn revoking_one_lesson_keeps_another_attachment_active() {
let f = Fixture::new();
let id = f.bank("bank.jsonl");
let other = f.run(
&[
"bank",
"--store",
"bank.jsonl",
"--title",
"Other policy",
"--when",
"check a different staging inventory",
"--found",
"Names are bounded",
"--how",
"Compare approved names",
"--evidence",
"Synthetic",
],
"",
);
assert!(other.status.success());
let other = String::from_utf8(other.stdout)
.unwrap()
.split_whitespace()
.nth(1)
.unwrap()
.to_owned();
assert_ne!(id, other);
assert!(f.attach("bank.jsonl", &id).status.success());
assert!(f.attach("bank.jsonl", &other).status.success());
assert!(f
.run(
&["check", "detach", "--store", "bank.jsonl", "--lesson", &id],
""
)
.status
.success());
assert!(!f.hit("bank.jsonl", &id).status.success());
assert!(f.hit("bank.jsonl", &other).status.success());
}
#[test]
fn missing_bank_binding_requires_reattach() {
let f = Fixture::new();
let id = f.bank("bank.jsonl");
assert!(f.attach("bank.jsonl", &id).status.success());
let side = f.root.join("bank.jsonl.capabilities.hxdb");
let exported = f.run(&["export", "--store", side.to_str().unwrap()], "");
assert!(exported.status.success());
let raw = String::from_utf8(exported.stdout).unwrap();
let mut result = String::new();
for line in raw.lines() {
let marker = "\"bank_binding\":\"";
if let Some(start) = line.find(marker) {
let end =
start + marker.len() + line[start + marker.len()..].find('"').unwrap() + 1;
let (start, end) = if line.as_bytes().get(end) == Some(&b',') {
(start, end + 1)
} else {
(start - 1, end)
};
result.push_str(&format!("{}{}\n", &line[..start], &line[end..]));
} else {
result.push_str(line);
result.push('\n');
}
}
let source = f.root.join("legacy.jsonl"); let result = result
.lines()
.map(|line| format!("{{\"kind\":\"fixture\",{}\n", &line[1..]))
.collect::<String>();
fs::write(&source, result).unwrap();
fs::remove_file(&side).unwrap();
assert!(f
.run(
&[
"migrate",
"--store",
source.to_str().unwrap(),
"--to",
side.to_str().unwrap()
],
""
)
.status
.success());
let refused = f.hit("bank.jsonl", &id);
assert!(!refused.status.success());
assert!(f.attach("bank.jsonl", &id).status.success());
assert!(f.hit("bank.jsonl", &id).status.success());
}
}