#[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
}
#[derive(Clone, Default, Debug)]
pub struct Meta {
pub source: String,
pub state: String,
pub evidence_status: String,
pub project: String,
pub persona: String,
pub harness: String,
pub version: String,
pub machine: String,
pub valid_until: u64,
}
#[derive(Clone, Default, Debug)]
pub struct Scope {
pub project: String,
pub persona: String,
pub harness: String,
pub version: String,
pub machine: String,
}
pub fn env_scope() -> Scope {
let v = |k: &str| std::env::var(k).unwrap_or_default();
Scope {
project: v("INSTINCT_PROJECT"),
persona: v("INSTINCT_PERSONA"),
harness: v("INSTINCT_HARNESS"),
version: v("INSTINCT_HARNESS_VERSION"),
machine: v("INSTINCT_MACHINE"),
}
}
pub fn scope_check(m: &Meta, s: &Scope, now: u64) -> Result<(), &'static str> {
if m.state == "quarantined" || m.evidence_status == "contradicted" {
return Err("quarantined");
}
if m.valid_until != 0 && now > m.valid_until {
return Err("expired");
}
for (required, actual, reason) in [
(&m.project, &s.project, "project"),
(&m.persona, &s.persona, "persona"),
(&m.harness, &s.harness, "harness"),
(&m.version, &s.version, "version"),
(&m.machine, &s.machine, "machine"),
] {
if !required.is_empty() && required != actual {
return Err(reason);
}
}
Ok(())
}
pub fn eligible(
m: &Meta,
s: &Scope,
now: u64,
sim: Option<f32>,
hits: u32,
quiet: bool,
) -> Result<(), &'static str> {
scope_check(m, s, now)?;
if quiet {
return Err("quiet");
}
if (m.source == "mined" || m.source == "distilled" || m.state == "candidate")
&& hits == 0
&& sim.map_or(true, |x| !x.is_finite() || x < 0.74)
{
return Err("candidate_match_unproven");
}
Ok(())
}
pub fn quiet(useful: u32, irrelevant: u32) -> bool {
(irrelevant >= 2 && useful == 0)
|| (irrelevant >= 3 && (irrelevant as u64) * 4 >= (useful as u64 + irrelevant as u64) * 3)
}
pub fn unverified(m: &Meta) -> bool {
m.evidence_status != "verified"
}
pub fn preservation_check(source: &str, candidate: &str) -> Result<(), &'static str> {
let numbers = |s: &str| -> std::collections::BTreeSet<String> {
s.split(|c: char| !c.is_ascii_digit() && c != '.' && c != '%' && c != '/')
.filter(|x| x.chars().any(|c| c.is_ascii_digit()))
.map(|x| x.trim_matches('.').to_string())
.collect()
};
if !numbers(source).is_subset(&numbers(candidate)) {
return Err("numeric_evidence_changed");
}
let a = source.to_lowercase();
let b = candidate.to_lowercase();
for q in [
"not established",
"not significant",
"inconclusive",
"uncertain",
"unverified",
"not proven",
"shown subset",
"early stop",
] {
if a.contains(q) && !b.contains(q) {
return Err("qualifier_removed");
}
}
if candidate.contains("[REDACTED]") && !source.contains("[REDACTED]") {
return Err("evidence_redacted");
}
Ok(())
}
pub fn strength(hits: u32, misses: u32) -> f32 {
(hits as f32 + 1.0) / (u64::from(hits) + u64::from(misses) + 2) as f32
}
pub fn promoted(hits: u32, misses: u32, min_hits: u32) -> bool {
hits >= min_hits && strength(hits, misses) >= 0.6
}
pub fn prompt_scope(explicit_project: Option<&str>, cwd: Option<&str>) -> Scope {
let mut scope = env_scope();
if let Some(project) = explicit_project.filter(|name| !name.is_empty()).or(cwd) {
scope.project = project.to_owned();
}
scope
}