#[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 super::{scope_identity, Discovery};
use std::collections::{HashMap, HashSet};
pub(super) struct Mark {
pub ts: u64,
pub hit: bool,
pub key: String,
pub withdrawn: bool,
pub verification: (u8, String),
}
pub(super) fn retract(marks: &mut [Mark], hit: bool) {
if let Some(mark) = marks
.iter_mut()
.rev()
.find(|mark| mark.hit == hit && !mark.withdrawn)
{
mark.withdrawn = true;
}
}
#[derive(Clone, Copy, Default)]
enum Verdict {
#[default]
Absent,
Agreed(bool, u64),
Disputed,
}
impl Verdict {
fn observe(&mut self, hit: bool, ts: u64) {
*self = match *self {
Self::Absent => Self::Agreed(hit, ts),
Self::Agreed(previous, first) if previous == hit => Self::Agreed(hit, first.min(ts)),
_ => Self::Disputed,
};
}
fn hit(self) -> Option<bool> {
if let Self::Agreed(hit, _) = self {
Some(hit)
} else {
None
}
}
}
#[derive(Default)]
struct Receipt {
seen: Verdict,
live: Verdict,
all_verified: Option<bool>,
had_verified: bool,
}
pub(super) fn supersede(aliases: &mut HashMap<String, Option<String>>, id: String, by: String) {
match aliases.entry(id) {
std::collections::hash_map::Entry::Vacant(slot) => {
slot.insert(Some(by));
}
std::collections::hash_map::Entry::Occupied(mut slot) => {
if slot.get().as_ref() != Some(&by) {
slot.insert(None);
}
} }
}
fn accepts(from: &Discovery, to: &Discovery) -> bool {
let allowed =
|d: &Discovery| d.meta.state != "quarantined" && d.meta.evidence_status != "contradicted";
allowed(from)
&& allowed(to)
&& scope_identity(&from.meta) == scope_identity(&to.meta)
&& from.meta.source == to.meta.source
&& from.meta.valid_until == to.meta.valid_until
}
fn roots(
all: &[Discovery],
index: &HashMap<String, usize>,
aliases: &HashMap<String, Option<String>>,
retired: &HashSet<String>,
) -> Vec<Option<usize>> {
let mut visited = vec![false; all.len()];
let mut resolved = vec![None; all.len()];
let mut path = Vec::new();
for start in 0..all.len() {
let mut i = start;
let root = loop {
if visited[i] {
break resolved[i];
}
visited[i] = true;
path.push(i);
let d = &all[i];
if retired.contains(&d.id) {
break None;
}
let Some(next) = aliases.get(&d.id) else {
break Some(i);
};
let Some(&j) = next.as_ref().and_then(|id| index.get(id)) else {
break None;
};
if !accepts(d, &all[j]) {
break None;
}
i = j;
};
for member in path.drain(..) {
resolved[member] = root;
}
}
resolved
}
pub(super) fn finish(
all: &mut Vec<Discovery>,
index: &HashMap<String, usize>,
aliases: &HashMap<String, Option<String>>,
retired: &HashSet<String>,
history: HashMap<usize, Vec<Mark>>,
mut observe: impl FnMut(&str, u64, bool, u8),
) {
let roots = roots(all, index, aliases, retired);
let mut receipts = HashMap::<(usize, String), Receipt>::new();
let mut count = |d: &mut Discovery, ts: u64, hit: bool, verification: u8| {
if hit {
d.hits += 1;
} else {
d.misses += 1;
}
observe(&d.id, ts, hit, verification);
};
for (member, marks) in history {
all[member].outcomes.clear();
let Some(root) = roots[member] else { continue };
for mark in marks {
let had_verified = mark.verification.0 == 1;
let verified = had_verified
&& member == root
&& mark.verification.1 == super::check::feedback::fingerprint(&all[root])
&& all[root].meta.state != "quarantined"
&& all[root].meta.evidence_status != "contradicted";
if mark.key.is_empty() {
if !mark.withdrawn {
count(&mut all[root], mark.ts, mark.hit, 0);
}
continue;
}
let receipt = receipts.entry((root, mark.key)).or_default();
receipt.seen.observe(mark.hit, mark.ts);
if !mark.withdrawn {
receipt.live.observe(mark.hit, mark.ts);
receipt.all_verified = Some(receipt.all_verified.unwrap_or(true) && verified);
receipt.had_verified |= had_verified;
}
}
}
for ((root, key), receipt) in receipts {
let d = &mut all[root];
if let Verdict::Agreed(hit, ts) = receipt.live {
let state = if receipt.all_verified == Some(true) {
1
} else if receipt.had_verified {
2
} else {
0
};
count(d, ts, hit, state);
}
let reserved = match receipt.live {
Verdict::Absent => receipt.seen,
verdict => verdict,
};
d.outcomes.insert(key, reserved.hit());
}
all.retain(|d| !aliases.contains_key(&d.id) && !retired.contains(&d.id));
}