instinct-cli 0.3.8

Shared local memory for coding agents. Save a fix once and find it again in a new session or another connected tool.
// generated by hxc 1.0.0-acgt-compiler from src/consolidation.hlx -- do not edit by hand

#[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
}

// hlx:3
use super::{scope_identity, Discovery};
// hlx:4
use std::collections::{HashMap, HashSet};
// hlx:6
pub(super) struct Mark {
    // hlx:7
    pub ts: u64,
    // hlx:8
    pub hit: bool,
    // hlx:9
    pub key: String,
    // hlx:10
    pub withdrawn: bool,
    // hlx:11
    pub verification: (u8, String),
    // hlx:12
}
// hlx:14
pub(super) fn retract(marks: &mut [Mark], hit: bool) {
    // hlx:15
    if let Some(mark) = marks
        .iter_mut()
        .rev()
        .find(|mark| mark.hit == hit && !mark.withdrawn)
    {
        // hlx:16
        mark.withdrawn = true;
        // hlx:17
    }
    // hlx:18
}
// hlx:22
#[derive(Clone, Copy, Default)]
// hlx:23
enum Verdict {
    // hlx:24
    #[default]
    // hlx:25
    Absent,
    // hlx:26
    Agreed(bool, u64),
    // hlx:27
    Disputed,
    // hlx:28
}
// hlx:30
impl Verdict {
    // hlx:31
    fn observe(&mut self, hit: bool, ts: u64) {
        // hlx:32
        *self = match *self {
            Self::Absent => Self::Agreed(hit, ts),
            Self::Agreed(previous, first) if previous == hit => Self::Agreed(hit, first.min(ts)),
            _ => Self::Disputed,
        };
        // hlx:33
    }
    // hlx:34
    fn hit(self) -> Option<bool> {
        // hlx:35
        if let Self::Agreed(hit, _) = self {
            Some(hit)
        } else {
            None
        }
        // hlx:36
    }
    // hlx:37
}
// hlx:39
#[derive(Default)]
// hlx:40
struct Receipt {
    // hlx:41
    seen: Verdict,
    // hlx:42
    live: Verdict,
    // hlx:43
    all_verified: Option<bool>,
    // hlx:44
    had_verified: bool,
    // hlx:45
}
// hlx:49
pub(super) fn supersede(aliases: &mut HashMap<String, Option<String>>, id: String, by: String) {
    // hlx:50
    match aliases.entry(id) {
        // hlx:51
        std::collections::hash_map::Entry::Vacant(slot) => {
            slot.insert(Some(by));
        }
        // hlx:52
        std::collections::hash_map::Entry::Occupied(mut slot) => {
            if slot.get().as_ref() != Some(&by) {
                slot.insert(None);
            }
        } // hlx:53
    }
    // hlx:54
}
// hlx:58
fn accepts(from: &Discovery, to: &Discovery) -> bool {
    // hlx:59
    let allowed =
        |d: &Discovery| d.meta.state != "quarantined" && d.meta.evidence_status != "contradicted";
    // hlx:60
    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
    // hlx:61
}
// hlx:63
fn roots(
    all: &[Discovery],
    index: &HashMap<String, usize>,
    aliases: &HashMap<String, Option<String>>,
    retired: &HashSet<String>,
) -> Vec<Option<usize>> {
    // hlx:65
    let mut visited = vec![false; all.len()];
    // hlx:66
    let mut resolved = vec![None; all.len()];
    // hlx:67
    let mut path = Vec::new();
    // hlx:68
    for start in 0..all.len() {
        // hlx:69
        let mut i = start;
        // hlx:70
        let root = loop {
            // hlx:71
            if visited[i] {
                // hlx:72
                break resolved[i];
                // hlx:73
            }
            // hlx:74
            visited[i] = true;
            // hlx:75
            path.push(i);
            // hlx:76
            let d = &all[i];
            // hlx:77
            if retired.contains(&d.id) {
                // hlx:78
                break None;
                // hlx:79
            }
            // hlx:80
            let Some(next) = aliases.get(&d.id) else {
                break Some(i);
            };
            // hlx:81
            let Some(&j) = next.as_ref().and_then(|id| index.get(id)) else {
                break None;
            };
            // hlx:82
            if !accepts(d, &all[j]) {
                // hlx:83
                break None;
                // hlx:84
            }
            // hlx:85
            i = j;
            // hlx:86
        };
        // hlx:87
        for member in path.drain(..) {
            // hlx:88
            resolved[member] = root;
            // hlx:89
        }
        // hlx:90
    }
    // hlx:91
    resolved
    // hlx:92
}
// hlx:94
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),
) {
    // hlx:95
    let roots = roots(all, index, aliases, retired);
    // hlx:96
    let mut receipts = HashMap::<(usize, String), Receipt>::new();
    // hlx:97
    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);
    };
    // hlx:98
    for (member, marks) in history {
        // hlx:99
        // Discard the per-member retry index. Rebuild it on the surviving lesson.
        // hlx:100
        all[member].outcomes.clear();
        // hlx:101
        let Some(root) = roots[member] else { continue };
        // hlx:102
        for mark in marks {
            // hlx:103
            let had_verified = mark.verification.0 == 1;
            // hlx:104
            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";
            // hlx:105
            if mark.key.is_empty() {
                // hlx:106
                if !mark.withdrawn {
                    // hlx:107
                    count(&mut all[root], mark.ts, mark.hit, 0);
                    // hlx:108
                }
                // hlx:109
                continue;
                // hlx:110
            }
            // hlx:111
            let receipt = receipts.entry((root, mark.key)).or_default();
            // hlx:112
            receipt.seen.observe(mark.hit, mark.ts);
            // hlx:113
            if !mark.withdrawn {
                // hlx:114
                receipt.live.observe(mark.hit, mark.ts);
                // hlx:115
                receipt.all_verified = Some(receipt.all_verified.unwrap_or(true) && verified);
                // hlx:116
                receipt.had_verified |= had_verified;
                // hlx:117
            }
            // hlx:118
        }
        // hlx:119
    }
    // hlx:120
    for ((root, key), receipt) in receipts {
        // hlx:121
        let d = &mut all[root];
        // hlx:122
        if let Verdict::Agreed(hit, ts) = receipt.live {
            // hlx:123
            let state = if receipt.all_verified == Some(true) {
                1
            } else if receipt.had_verified {
                2
            } else {
                0
            };
            // hlx:124
            count(d, ts, hit, state);
            // hlx:125
        }
        // hlx:126
        // Even a withdrawn observation reserves its key against a delayed retry.
        // hlx:127
        let reserved = match receipt.live {
            Verdict::Absent => receipt.seen,
            verdict => verdict,
        };
        // hlx:128
        d.outcomes.insert(key, reserved.hit());
        // hlx:129
    }
    // hlx:130
    all.retain(|d| !aliases.contains_key(&d.id) && !retired.contains(&d.id));
    // hlx:131
}