#[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::{
collections::HashMap,
path::{Path, PathBuf},
};
use super::{eligibility::Scope, Discovery};
#[derive(Debug, PartialEq, Eq)]
struct Stamp {
length: u64,
device: u64,
inode: u64,
change: (i64, i64),
}
#[cfg(unix)]
fn stamp(path: &Path) -> Result<Option<Stamp>, String> {
use std::os::unix::fs::MetadataExt;
match std::fs::metadata(path) {
Ok(m) => Ok(Some(Stamp {
length: m.len(),
device: m.dev(),
inode: m.ino(),
change: (m.ctime(), m.ctime_nsec()),
})),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.to_string()),
}
}
#[cfg(not(unix))]
fn stamp(_: &Path) -> Result<Option<Stamp>, String> {
Ok(None)
}
pub(super) struct Index {
stamp: Stamp,
scope: [String; 5],
since: u64,
valid_until: u64,
entries: Vec<Discovery>,
lengths: Vec<usize>,
avg: f32,
postings: HashMap<String, Vec<(usize, usize)>>,
}
fn build(stamp: Stamp, scope: [String; 5], entries: Vec<Discovery>, since: u64) -> Index {
let mut lengths = Vec::with_capacity(entries.len());
let mut postings: HashMap<String, Vec<(usize, usize)>> = HashMap::new();
let mut valid_until = u64::MAX;
for (id, d) in entries.iter().enumerate() {
if d.meta.valid_until != 0 {
valid_until = valid_until.min(d.meta.valid_until);
}
let words = super::tokens(&d.lex_text());
lengths.push(words.len());
let mut counts = HashMap::new();
for word in words {
*counts.entry(word).or_insert(0usize) += 1;
}
for (word, tf) in counts {
postings.entry(word).or_default().push((id, tf));
}
}
let avg = (lengths.iter().sum::<usize>() as f32 / entries.len().max(1) as f32).max(1.0);
Index {
stamp,
scope,
since,
valid_until,
entries,
lengths,
avg,
postings,
}
}
fn scores(index: &Index, query: &str) -> Vec<f32> {
let mut result = vec![0.0; index.entries.len()];
let mut terms = super::tokens(query);
terms.sort();
terms.dedup();
for term in terms {
if let Some(list) = index.postings.get(&term) {
let n = index.entries.len() as f32;
let df = list.len() as f32;
let idf = ((n - df + 0.5) / (df + 0.5) + 1.0).ln();
for &(id, count) in list {
let tf = count as f32;
result[id] += idf * tf * 2.2
/ (tf + 1.2 * (0.25 + 0.75 * index.lengths[id] as f32 / index.avg));
}
}
}
result
}
pub(super) fn recall(
cache: &mut Option<Index>,
path: &PathBuf,
query: &str,
k: usize,
scope: &Scope,
) -> Result<Vec<Discovery>, String> {
if !cfg!(unix) || !super::helix_db::is_native(path) {
return super::recall_methods(path, query, k, scope);
}
let Some(before) = stamp(path)? else {
*cache = None;
return Ok(Vec::new());
};
let context = [
&scope.project,
&scope.persona,
&scope.harness,
&scope.version,
&scope.machine,
]
.map(|s| s.to_owned());
let now = super::now_secs();
if !cache.as_ref().is_some_and(|i| {
i.stamp == before && i.scope == context && now >= i.since && now <= i.valid_until
}) {
*cache = None;
let entries = super::load_scoped(path, scope, now)?;
if stamp(path)?.as_ref() != Some(&before) {
return super::recall_methods(path, query, k, scope);
}
*cache = Some(build(before, context, entries, now));
}
let index = cache.as_ref().unwrap();
super::rank_methods(path, query, k, scope, &index.entries, scores(index, query))
}