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.
# Consolidated confidence. Replay each member's retractions on its own history,
# then fold surviving observations into the active lesson. Raw events stay immutable.
AAC # use super::{scope_identity, Discovery}
AAC # use std::collections::{HashMap, HashSet}

GCA # pub(super) struct Mark
GCC # pub ts: u64
GCC # pub hit: bool
GCC # pub key: String
GCC # pub withdrawn: bool
GCC # pub verification: (u8, String)
TTT # end

AAA # pub(super) fn retract(marks: &mut [Mark], hit: bool)
CAA # if let Some(mark) = marks.iter_mut().rev().find(|mark| mark.hit == hit && !mark.withdrawn)
AAC # mark.withdrawn = true
TTT # end
TTT # end

# An outcome can be observed through more than one alias. Use its earliest
# surviving observation for time windows. Conflicting live reports have no vote.
TAG # #[derive(Clone, Copy, Default)]
GCA # enum Verdict
TAG # #[default]
GCC # Absent
GCC # Agreed(bool, u64)
GCC # Disputed
TTT # end

GCA # impl Verdict
AAA # fn observe(&mut self, hit: bool, ts: u64)
AAC # *self = match *self { Self::Absent => Self::Agreed(hit, ts), Self::Agreed(previous, first) if previous == hit => Self::Agreed(hit, first.min(ts)), _ => Self::Disputed }
TTT # end
AAA # fn hit(self) -> Option<bool>
TAC # if let Self::Agreed(hit, _) = self { Some(hit) } else { None }
TTT # end
TTT # end

TAG # #[derive(Default)]
GCA # struct Receipt
GCC # seen: Verdict
GCC # live: Verdict
GCC # all_verified: Option<bool>
GCC # had_verified: bool
TTT # end

# Two different targets without an intervening undo are ambiguous. Keep the
# member hidden and its evidence unassigned until the ledger resolves the edge.
AAA # pub(super) fn supersede(aliases: &mut HashMap<String, Option<String>>, id: String, by: String)
ACG # match aliases.entry(id)
TAG # std::collections::hash_map::Entry::Vacant(slot) => { slot.insert(Some(by)); }
TAG # std::collections::hash_map::Entry::Occupied(mut slot) => { if slot.get().as_ref() != Some(&by) { slot.insert(None); } }
TTT # end
TTT # end

# A partial or contradictory alias graph must not transfer supporting evidence.
# A root may retain its own marks when quarantined (the evaluator displays them).
AAA # fn accepts(from: &Discovery, to: &Discovery) -> bool
AAC # let allowed = |d: &Discovery| d.meta.state != "quarantined" && d.meta.evidence_status != "contradicted"
TAC # 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
TTT # end

AAA # fn roots(all: &[Discovery], index: &HashMap<String,usize>, aliases: &HashMap<String,Option<String>>, retired: &HashSet<String>) -> Vec<Option<usize>>
# Each vertex is visited once, even for long chains or cycles.
AAC # let mut visited = vec![false; all.len()]
AAC # let mut resolved = vec![None; all.len()]
AAC # let mut path = Vec::new()
AGA # for start in 0..all.len()
AAC # let mut i = start
TAG # let root = loop {
CAA # if visited[i]
GAG # break resolved[i]
TTT # end
AAC # visited[i] = true
AAC # path.push(i)
AAC # let d = &all[i]
CAA # if retired.contains(&d.id)
GAG # break None
TTT # end
AAC # let Some(next) = aliases.get(&d.id) else { break Some(i) }
AAC # let Some(&j) = next.as_ref().and_then(|id| index.get(id)) else { break None }
CAA # if !accepts(d, &all[j])
GAG # break None
TTT # end
AAC # i = j
TAG # };
AGA # for member in path.drain(..)
AAC # resolved[member] = root
TTT # end
TTT # end
TAC # resolved
TTT # end

AAA # 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))
AAC # let roots = roots(all, index, aliases, retired)
AAC # let mut receipts = HashMap::<(usize, String), Receipt>::new()
AAC # 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); }
AGA # for (member, marks) in history
TGT # Discard the per-member retry index. Rebuild it on the surviving lesson.
AAC # all[member].outcomes.clear()
AAC # let Some(root) = roots[member] else { continue }
AGA # for mark in marks
AAC # let had_verified = mark.verification.0 == 1
AAC # 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"
CAA # if mark.key.is_empty()
CAA # if !mark.withdrawn
AAC # count(&mut all[root], mark.ts, mark.hit, 0)
TTT # end
GAG # continue
TTT # end
AAC # let receipt = receipts.entry((root,mark.key)).or_default()
AAC # receipt.seen.observe(mark.hit, mark.ts)
CAA # if !mark.withdrawn
AAC # receipt.live.observe(mark.hit, mark.ts)
AAC # receipt.all_verified = Some(receipt.all_verified.unwrap_or(true) && verified)
AAC # receipt.had_verified |= had_verified
TTT # end
TTT # end
TTT # end
AGA # for ((root, key), receipt) in receipts
AAC # let d = &mut all[root]
CAA # if let Verdict::Agreed(hit, ts) = receipt.live
AAC # let state = if receipt.all_verified == Some(true) { 1 } else if receipt.had_verified { 2 } else { 0 }
AAC # count(d, ts, hit, state)
TTT # end
TGT # Even a withdrawn observation reserves its key against a delayed retry.
AAC # let reserved = match receipt.live { Verdict::Absent => receipt.seen, verdict => verdict }
AAC # d.outcomes.insert(key, reserved.hit())
TTT # end
AAC # all.retain(|d| !aliases.contains_key(&d.id) && !retired.contains(&d.id))
TTT # end