Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/nornir

src/mimir_lens/room_feed/vector_tests.rs

//! One vector per function node, end to end over [`super::fixture::vectors`]:
//! the feed's function half (what is embedded, and when), the link log, the
//! ring job, and the two lenses asked the way a front door asks them.

use std::collections::BTreeMap;

use serde_json::{Value, json};

use super::fixture::{self, vectors as vx};
use crate::vector::fnvec::{self, links};

fn state(v: &Value) -> &str {
    v["lens_meta"]["state"].as_str().unwrap_or_else(|| panic!("no lens_meta.state: {v}"))
}

fn note(v: &Value) -> &str {
    v["evidence_note"].as_str().unwrap_or("")
}

fn member_ids(cluster: &Value) -> Vec<String> {
    let mut ids: Vec<String> = cluster["members"].as_array().unwrap().iter().map(|m| m["id"].as_str().unwrap().to_string()).collect();
    ids.sort();
    ids
}

fn both_embedded(f: &vx::Vx, bag: &vx::Bag) -> (fnvec::Run, fnvec::Run) {
    (f.embed(vx::MONO, bag), f.embed(vx::OTHER, bag))
}

// ── item 1: one vector per function node ────────────────────────────────────

/// Every function node with text of its own gets a link; a body that occurs
/// several times — in two crates, in two repos, one level deeper — is embedded
/// ONCE; a function a macro wrote gets no chunk.
#[test]
fn every_function_node_is_linked_and_an_identical_body_is_embedded_once() {
    let _g = fixture::serial();
    let f = vx::build();
    let bag = vx::Bag::default();
    let (mono, other) = both_embedded(&f, &bag);
    // mono: 11 fn + method nodes, `width` is the macro's → 10 chunks, 7 distinct texts
    assert_eq!((mono.functions, mono.distinct_bodies, mono.without_chunk), (10, 7, 1), "{mono:?}");
    assert_eq!((mono.new_vectors, mono.plus, mono.minus), (7, 10, 0), "{mono:?}");
    // other: `parse_header` is mono's text — its vector is already there
    // … and its `proto::decode` is build-script output outside the checkout: no chunk
    assert_eq!((other.functions, other.new_vectors, other.plus, other.without_chunk), (2, 1, 2, 1), "{other:?}");
    assert_eq!(bag.count(), 8, "8 distinct bodies in the workspace, 8 texts embedded");
    assert!(!bag.asked.lock().unwrap().iter().any(|t| t.contains("make_getter!")), "a macro invocation is not a function body");

    let repos: BTreeMap<String, i64> = [(vx::MONO.to_string(), 1), (vx::OTHER.to_string(), 1)].into();
    let read = fnvec::read(&f.ws.warehouse, &repos).unwrap();
    assert_eq!((read.links[vx::MONO].len(), read.links[vx::OTHER].len(), read.by_hash.len()), (10, 2, 8));
    // the three copies of one function share one content hash, hence one vector
    let hash = |repo: &str, id: String| read.of(repo, &id).unwrap_or_else(|| panic!("no vector for {id}")).0.to_string();
    let copies = [hash(vx::MONO, vx::alpha("parse_header().")), hash(vx::MONO, vx::beta("http/parse_header().")), hash(vx::OTHER, vx::gamma("parse_header()."))];
    assert!(copies[0] == copies[1] && copies[1] == copies[2], "{copies:?}");
    assert_eq!(copies[0], crate::vector::chunk::hash_text(vx::PARSE_HEADER), "the key is the hash of the function's own text");
    assert!(read.of(vx::MONO, &vx::alpha("width().")).is_none());
    assert_eq!((read.model_id.as_str(), read.last_run[vx::MONO].backend.as_str()), ("test-bag", "test bag (cpu)"));
}

// ── item 2: the feed embeds deltas ──────────────────────────────────────────

/// A commit embeds the functions whose TEXT changed and nothing else: a
/// function that only moved (another line above it) keeps its link and its
/// vector, an unchanged tree embeds nothing and writes no event.
#[test]
fn a_commit_embeds_only_the_functions_whose_text_changed() {
    let _g = fixture::serial();
    let f = vx::build();
    let bag = vx::Bag::default();
    both_embedded(&f, &bag);
    let before = bag.count();

    // the same commit again: nothing to do
    let again = f.embed(vx::MONO, &bag);
    assert_eq!((again.new_vectors, again.plus, again.minus, bag.count()), (0, 0, 0, before), "{again:?}");
    let events = |repo: &str| links::read_events_of(&f.ws.warehouse, &[repo]).unwrap();
    assert_eq!(events(vx::MONO).len(), 10);

    // seq 2: every span of alpha's file moves by one line; `total` and
    // `checksum` change, `fresh` is new
    f.second_commit();
    let run = f.embed(vx::MONO, &bag);
    assert_eq!((run.functions, run.new_vectors, run.plus, run.minus), (11, 3, 3, 0), "{run:?}");
    assert_eq!(bag.count(), before + 3);
    let written: Vec<String> = events(vx::MONO).into_iter().filter(|e| e.seq == 2).map(|e| e.moniker).collect();
    let mut expect = vec![vx::alpha("checksum()."), vx::alpha("fresh()."), vx::alpha("impl#[Reader]total().")];
    expect.sort();
    let mut got = written.clone();
    got.sort();
    assert_eq!(got, expect, "only the changed functions get an event");
    // the state at seq 1 is still readable: the log is a fold, not an overwrite
    let at1 = links::open(&events(vx::MONO), vx::MONO, 1);
    let at2 = links::open(&events(vx::MONO), vx::MONO, 2);
    assert_ne!(at1[&vx::alpha("checksum().")], at2[&vx::alpha("checksum().")]);
    assert_eq!(at1[&vx::alpha("parse_header().")], at2[&vx::alpha("parse_header().")]);
    assert!(!at1.contains_key(&vx::alpha("fresh().")) && at2.contains_key(&vx::alpha("fresh().")));
}

/// The dependency ring is embedded by its own job, once per crate@version: a
/// second run finds nothing to do, and a lockfile bump embeds only the crate
/// version that is new.
#[test]
fn the_ring_is_embedded_once_per_crate_version_and_a_bump_embeds_only_the_new_one() {
    use crate::vector::fnvec::ring;
    let _g = fixture::serial();
    let f = vx::build();
    let bag = vx::Bag::default();
    let log = f.ws.room.log().unwrap();
    let profile = crate::vector::store::Embedder::profile(&bag).id();
    let wh = &f.ws.warehouse;
    let mut embedding = fnvec::Loaded { embedder: &bag, backend: "test bag (cpu)" };

    assert!(ring::due(wh, &log, &profile).unwrap(), "a ring that was never embedded is due");
    let dirs = f.ring_dirs(&["1.0.0"]);
    let plan = ring::plan(wh, &log, &dirs, &profile, false).unwrap();
    assert_eq!(plan.todo.iter().map(|c| (c.krate.as_str(), c.functions.len(), c.dir.is_some())).collect::<Vec<_>>(), [("kit@1.0.0", 2, true)], "{}", plan.line());
    let rep = ring::run(wh, &plan, &mut embedding, &mut |_, _| {}).unwrap();
    assert_eq!((rep.crates_embedded, rep.functions, rep.new_vectors), (1, 2, 2), "{rep:?}");
    // the member feed is not what embedded them, and they are linked under the ring
    assert_eq!(links::open(&links::read_events_of(wh, &[fnvec::RING]).unwrap(), fnvec::RING, 0).len(), 2);

    // nothing moved: not due, and a plan has nothing left
    assert!(!ring::due(wh, &log, &profile).unwrap());
    let plan = ring::plan(wh, &log, &dirs, &profile, false).unwrap();
    assert!(plan.todo.is_empty() && plan.done == 1, "{}", plan.line());

    // the lockfile moves kit 1.0.0 → 2.0.0: due again, and only 2.0.0 is planned;
    // `pad` did not change between the versions, so ONE text is embedded
    f.second_commit();
    assert!(ring::due(wh, &log, &profile).unwrap());
    let dirs = f.ring_dirs(&["1.0.0", "2.0.0"]);
    let plan = ring::plan(wh, &log, &dirs, &profile, false).unwrap();
    assert_eq!(plan.todo.iter().map(|c| c.krate.as_str()).collect::<Vec<_>>(), ["kit@2.0.0"], "{}", plan.line());
    let before = bag.count();
    let rep = ring::run(wh, &plan, &mut embedding, &mut |_, _| {}).unwrap();
    assert_eq!((rep.crates_embedded, rep.functions, rep.new_vectors, bag.count() - before), (1, 2, 1, 1), "{rep:?}");

    // a crate whose source is nowhere: a `skipped` ledger row that names it, no vector
    let nowhere = ring::plan(wh, &log, &BTreeMap::new(), "another-model", false).unwrap();
    assert_eq!(nowhere.unlocated(), ["kit@2.0.0"]);
    let mut other_space = fnvec::Loaded { embedder: &bag, backend: "test" };
    let rep = ring::run(wh, &nowhere, &mut other_space, &mut |_, _| {}).unwrap();
    assert_eq!((rep.crates_embedded, rep.crates_skipped, rep.new_vectors), (0, 1, 0));
    let skipped = links::read_runs(wh).unwrap().into_iter().rfind(|r| r.unit == "kit@2.0.0" && r.status == links::STATUS_SKIPPED).expect("the skip is in the ledger");
    assert!(skipped.detail.unwrap().contains("no source directory"), "the row says why");
}

// ── item 3: the two lenses ──────────────────────────────────────────────────

/// Before any vector exists the lenses still ANSWER — from the graph, graph
/// first — and the note names why the embedding index added nothing: the
/// feed, and the verb that fills it. No vector is no refusal and no gap.
#[test]
fn without_vectors_the_lenses_answer_from_the_graph_and_say_why_the_embedding_index_added_nothing() {
    let _g = fixture::serial();
    let f = vx::build();
    for lens in ["similar_code", "same_intent"] {
        let v = f.ws.ask(lens, json!({}));
        assert!(["complete", "empty_fact"].contains(&state(&v)), "{lens}: the graph alone is complete: {v}");
        assert!(note(&v).contains("the embedding index added nothing"), "{}", note(&v));
        // nothing ran: a build that can embed says the vectors are missing, a
        // build that cannot says why none can be made here
        if fnvec::no_embedder().is_some() {
            assert!(note(&v).contains("built without embedder") && note(&v).contains("12 function(s) wait"), "{}", note(&v));
        } else {
            assert!(note(&v).contains("vectors missing for 12 of 12 function(s)"), "{}", note(&v));
        }
        assert!(note(&v).contains("`embeddings` feed") && note(&v).contains("nornir workspace embed vectors"), "the note names the feed and the verb that fills it: {}", note(&v));
        assert_eq!(v["vectors"]["with_vector"], 0, "{lens}: {v}");
        assert!(v["worst"].is_array());
    }
    // the repo filter is checked by the room first, as for every lens
    assert_eq!(state(&f.ws.ask("similar_code", json!({ "repo": "nobody" }))), "not_consumed");
}

/// The feed ran and failed (the budget gate refused it): no vector — the
/// graph answers, and the note carries the run's own reason.
#[test]
fn a_feed_run_that_failed_is_said_in_the_note_and_the_graph_answers() {
    let _g = fixture::serial();
    let f = vx::build();
    let (seq, sha) = f.at(vx::MONO);
    fnvec::record_not_run(&f.ws.warehouse, &fnvec::At::member(vx::MONO, seq, &sha), links::STATUS_ERROR, "embedding 3377 chunks would take ~29.7 min, over the 10 min budget").unwrap();
    let v = f.ws.ask("same_intent", json!({}));
    assert!(["complete", "empty_fact"].contains(&state(&v)), "{v}");
    assert!(note(&v).contains("vectors missing for 12 of 12 function(s)") && note(&v).contains("over the 10 min budget"), "{}", note(&v));
    assert!(note(&v).contains("the embedding index added nothing"), "{}", note(&v));
}

/// A build without an embedder ran the feed: the ledger says `skipped`, the
/// graph answers, and the note carries the feed's own reason.
#[test]
fn a_feed_that_could_not_embed_is_said_in_the_note_and_the_graph_answers() {
    let _g = fixture::serial();
    let f = vx::build();
    let (seq, sha) = f.at(vx::MONO);
    fnvec::record_not_run(&f.ws.warehouse, &fnvec::At::member(vx::MONO, seq, &sha), links::STATUS_SKIPPED, "built without embedder (embed-tract/embed-ort)").unwrap();
    let v = f.ws.ask("similar_code", json!({}));
    assert_eq!(state(&v), "complete", "{v}");
    assert!(note(&v).contains("built without embedder") && note(&v).contains("`skipped`"), "{}", note(&v));
    assert_eq!(v["cluster_count"], 1, "the parse_header copies, from the graph: {v}");
}

/// With vectors: the copy in three crates is one cluster, the two nodes cut
/// from the SAME lines are refused (their cosine is 1.0 by construction), the
/// trait-impl family is withheld and counted, and the answer says which
/// kernel compared the vectors.
#[test]
fn similar_code_finds_the_copy_refuses_the_shared_lines_and_names_the_kernel() {
    let _g = fixture::serial();
    let f = vx::build();
    both_embedded(&f, &vx::Bag::default());
    let v = f.ws.ask("similar_code", json!({}));
    assert_eq!(state(&v), "complete", "{v}");
    assert_eq!((v["cluster_count"].as_u64(), v["pair_count"].as_u64()), (Some(1), Some(4)), "{v}");
    let cluster = &v["worst"][0];
    assert_eq!(member_ids(cluster), {
        let mut ids = vec![vx::alpha("parse_header()."), vx::beta("http/parse_header()."), vx::gamma("parse_header().")];
        ids.sort();
        ids
    });
    assert!(cluster["avg_cosine"].as_f64().unwrap() > 0.999 && cluster["structural"].as_f64().unwrap() > 0.99, "{cluster}");
    // alpha's and beta's `clamp` are ONE source region compiled twice
    assert_eq!(v["same_chunk_pairs"], 1, "{v}");
    assert!(!v.to_string().contains("clamp"), "the shared lines are in no cluster: {v}");
    // `<Reader as Default>::default` / `<Writer as Default>::default`
    assert_eq!(v["suppressed_trivial_clusters"], 1, "{v}");
    assert_eq!(v["suppressed_trivial"][0]["shape"], "trait_impl");
    // what the answer rests on
    assert_eq!((v["vectors"]["functions"].as_u64(), v["vectors"]["with_vector"].as_u64(), v["vectors"]["distinct_bodies"].as_u64()), (Some(12), Some(12), Some(8)), "{}", v["vectors"]);
    assert_eq!((v["vectors"]["macro_generated"].as_u64(), v["vectors"]["generated_outside_checkout"].as_u64(), v["vectors"]["model_id"].as_str()), (Some(1), Some(1), Some("test-bag")));
    assert_eq!((v["knn"]["device"].as_str(), v["knn"]["kernel"].as_str()), (Some("cpu"), Some(crate::vector::active_simd())), "{}", v["knn"]);
    assert!(note(&v).contains(&format!("kernel {}", crate::vector::active_simd())) && note(&v).contains("no GPU adapter"), "{}", note(&v));
    assert!(note(&v).contains("written by a macro") && note(&v).contains("generated outside the checkout"), "{}", note(&v));

    // the repo filter restricts what is REPORTED; detection stays workspace-wide
    let other = f.ws.ask("similar_code", json!({ "repo": vx::OTHER }));
    assert_eq!((other["cluster_count_matching"].as_u64(), member_ids(&other["worst"][0]).len()), (Some(1), 3), "{other}");
    // inside one crate nothing is copied-not-moved
    let strict = f.ws.ask("similar_code", json!({ "cosine": 0.9999, "structural": 0.99 }));
    assert_eq!(strict["thresholds"]["cosine"].as_f64().map(|c| (c * 10_000.0).round()), Some(9999.0));
    // with the family filter off, the trait impls are listed and labelled
    let kept = f.ws.ask("similar_code", json!({ "keep_trait_impls": true }));
    assert_eq!((kept["cluster_count"].as_u64(), kept["suppressed_trivial_clusters"].as_u64()), (Some(2), Some(0)), "{kept}");
}

/// The room moved on and the feed has not run for the new commit: the graph
/// still pairs every function (complete), and the note says how many have no
/// vector and that the vectors are of the older commit. After the feed ran
/// the embedding index covers every function again.
#[test]
fn a_room_ahead_of_its_vectors_answers_complete_and_says_how_many_vectors_are_missing() {
    let _g = fixture::serial();
    let f = vx::build();
    let bag = vx::Bag::default();
    both_embedded(&f, &bag);
    f.second_commit();
    let v = f.ws.ask("similar_code", json!({}));
    assert_eq!(state(&v), "complete", "the graph index covers every function: {v}");
    assert!(note(&v).contains("vectors missing for 1 of 13 function(s)"), "{}", note(&v));
    assert!(note(&v).contains("the vectors of `mono` are of seq 1") && note(&v).contains("the room is at seq 2"), "{}", note(&v));
    assert_eq!((v["vectors"]["with_vector"].as_u64(), v["vectors"]["missing"].as_u64()), (Some(12), Some(1)));
    assert_eq!(v["cluster_count"], 1, "the copy is still found: {v}");

    f.embed(vx::MONO, &bag);
    let v = f.ws.ask("similar_code", json!({}));
    assert_eq!((state(&v), v["vectors"]["missing"].as_u64()), ("complete", Some(0)), "{v}");
}

/// `same_intent` (the raw model, name-driven — what `semantic_clones` was
/// before the clone signals) folds the same rows and answers the same envelope.
#[test]
fn same_intent_computes_over_the_same_vectors_and_says_it_is_name_driven() {
    let _g = fixture::serial();
    let f = vx::build();
    both_embedded(&f, &vx::Bag::default());
    let v = f.ws.ask("same_intent", json!({ "repo": vx::MONO }));
    assert!(["complete", "empty_fact"].contains(&state(&v)), "{v}");
    assert_eq!((v["kind"].as_str(), v["name_driven"].as_bool()), (Some("same_intent"), Some(true)));
    assert!(note(&v).starts_with("same_intent — NAME-DRIVEN"), "{}", note(&v));
    assert_eq!((v["vectors"]["with_vector"].as_u64(), v["knn"]["device"].as_str()), (Some(12), Some("cpu")), "{v}");
    assert_eq!(v["filters_skipped"]["same_lines_pairs"], 1, "the shared lines are refused here too: {v}");
    assert!(!v["filters_skipped"].to_string().contains("chunk") && !note(&v).contains("chunk"), "no v1 chunk logic in the answer: {v}");
    assert!(note(&v).contains("raw cosine ≥") && note(&v).contains("1 pair(s) of nodes cut from the same lines"), "{}", note(&v));
    assert!(!note(&v).contains("symbol_facts") && !note(&v).contains("ONE embedding chunk"), "no v1 notes: {}", note(&v));
    // the literal copy is similar_code's finding, not a Type-4 clone
    assert!(!v["worst"].to_string().contains("parse_header"), "{}", v["worst"]);
}

// ── similar_code, incremental: the kept join advanced by what changed ───────

/// The whole answer, minus what describes the WORK rather than the finding:
/// `lens_meta.ms` (the clock), the `knn` block and the note's trailing
/// "Candidate scan: …" sentence (the full path scans, the incremental path
/// joins the changed functions only — the findings must still be identical).
fn sans_work(v: &Value) -> Value {
    let mut v = v.clone();
    if let Some(meta) = v["lens_meta"].as_object_mut() {
        meta.remove("ms");
    }
    if let Some(m) = v.as_object_mut() {
        m.remove("knn");
    }
    if let Some(head) = v["evidence_note"].as_str().and_then(|n| n.split("Candidate scan:").next()).map(str::to_string) {
        v["evidence_note"] = json!(head);
    }
    v
}

/// ZERO-LOSS, and the work saved, end to end: the first ask is the FULL join
/// and is KEPT; an unchanged room CARRIES it; one changed function (gamma's
/// `digest` becomes a copy of `parse_header`) is embedded alone by the feed
/// and ADVANCED alone by the lens — fewer dot products than the full join —
/// and the advanced answer equals the full recompute byte for byte (minus the
/// work description), the full path staying in the code as the oracle.
#[test]
fn similar_code_advances_the_kept_join_when_one_function_changes() {
    use crate::mimir_lens::clone_inc as inc;
    let _g = fixture::serial();
    inc::forget();
    let f = vx::build();
    let bag = vx::Bag::default();
    both_embedded(&f, &bag);
    let (f0, c0, a0) = inc::stats("similar_code");
    let before = f.ws.ask("similar_code", json!({}));
    assert_eq!(state(&before), "complete", "{before}");
    let (f1, ..) = inc::stats("similar_code");
    assert_eq!(f1 - f0, 1, "no kept join for this scope: the first ask is the full one");

    // the same room again: nothing the fold reads changed — carried
    let carried = f.ws.ask("similar_code", json!({}));
    let (f2, c1, a1) = inc::stats("similar_code");
    assert_eq!((f2 - f1, c1 - c0, a1 - a0), (0, 1, 0), "nothing changed: carried, not recomputed");
    assert_eq!(sans_work(&carried), sans_work(&before), "the carried answer IS the previous answer");

    // gamma's `digest` gets parse_header's body: the feed embeds ONE function …
    f.digest_commit();
    let run = f.embed(vx::OTHER, &bag);
    assert_eq!((run.plus, run.minus), (1, 0), "only the changed function is re-linked: {run:?}");
    // … and the lens re-pairs ONE function against the kept join
    let after = f.ws.ask("similar_code", json!({}));
    let (f3, c2, a2) = inc::stats("similar_code");
    assert_eq!((f3 - f2, c2 - c1, a2 - a1), (0, 0, 1), "one changed function is ADVANCED, not a full join");
    let w = inc::last_work().expect("the advance records what it did");
    let n = after["vectors"]["with_vector"].as_u64().unwrap();
    assert_eq!((w.lens, w.changed as u64, w.functions as u64), ("similar_code", 1, n), "{w:?}");
    assert!(w.dots < n * (n - 1) / 2, "fewer dot products than the full join: {w:?}");
    // the delta moved the answer: digest joined the parse_header cluster
    assert!(after["pair_count"].as_u64() > before["pair_count"].as_u64(), "before {before} after {after}");

    // ZERO-LOSS: forget the kept join and ask again — the FULL path, the oracle
    inc::forget();
    let full = f.ws.ask("similar_code", json!({}));
    let (f4, ..) = inc::stats("similar_code");
    assert_eq!(f4 - f3, 1, "forgetting the kept join forces the full path");
    assert_eq!(sans_work(&after), sans_work(&full), "ZERO-LOSS: the incremental answer IS the full answer");
}

// ── real vectors ────────────────────────────────────────────────────────────

/// The recorded fixture: the REAL vectors (jina-v2-base-code, one per function,
/// chunker `fn-v1`) of ten functions of znippy-zoomies @ 66a2891, with the
/// structural half the room gave them. Recorded by
/// `real_vectors_threshold_instrument`; 167 KB.
fn recorded() -> Vec<crate::mimir_analyze::SymbolVec> {
    let doc: Value = serde_json::from_str(include_str!("../../../tests/fixtures/fnvec/znippy-zoomies-pairs.json")).unwrap();
    assert_eq!((doc["model_id"].as_str(), doc["chunker"].as_str()), (Some("jina-v2-base-code"), Some(fnvec::CHUNKER)), "the fixture is of another vector space / cut rule: record it again");
    doc["functions"]
        .as_array()
        .unwrap()
        .iter()
        .map(|f| {
            let text = |k: &str| f[k].as_str().unwrap_or("").to_string();
            let (line, end) = (f["line"].as_u64().unwrap() as u32, f["end_line"].as_u64().unwrap() as u32);
            crate::mimir_analyze::SymbolVec {
                id: text("id"),
                repo: text("repo"),
                crate_name: text("crate"),
                module_path: text("module"),
                item_kind: "fn".into(),
                item_name: text("name"),
                file: text("file"),
                line,
                signature: f["signature"].as_str().map(str::to_string),
                callees: f["callees"].as_array().unwrap().iter().map(|c| c.as_str().unwrap().to_string()).collect(),
                span_lines: end - line + 1,
                chunk_hash: format!("{}\u{1f}{}\u{1f}{line}-{end}", text("repo"), text("file")),
                item_end_line: end,
                impl_header: f["impl_header"].as_str().map(str::to_string),
                vector: f["vector"].as_array().unwrap().iter().map(|x| x.as_f64().unwrap() as f32).collect(),
            }
        })
        .collect()
}

/// **The threshold, on real recorded vectors.** `ljar/src/bin/ljar.rs` and
/// `lzip-parallel/src/bin/lzip.rs` are one program copied into two crates
/// (198 lines each); a person read every pair below against the source:
///
/// | pair | judged | cosine |
/// |---|---|---|
/// | `decode_entry` × 2, `physical_cores` × 2 | the same text | 1.000 |
/// | `local_data_offset` × 2 (`JarError` ↔ `ZipError`) | a true copy | 0.928 |
/// | `main` × 2 (89 lines, names and messages renamed) | a true copy | 0.856 |
/// | `bench_vs_miniz` (ljar) ~ `bench_inflate` (lzip) | NOT a duplicate: two different benchmarks | 0.916 |
///
/// The default must keep the copy at 0.928 and must not report the pair at
/// 0.916 — the two sit 0.012 apart, which is how narrow the usable band is.
/// `main` is the copy the vector lens does NOT find (renames move an
/// embedding more than they move the text); that miss is stated here, with
/// its number, so nobody reads the threshold as catching it.
#[test]
fn the_recorded_real_pairs_are_judged_as_a_person_judged_them() {
    use crate::mimir_analyze::{DupThresholds, analyze_duplicates, cosine};
    let s = recorded();
    let of = |krate: &str, name: &str| s.iter().find(|x| x.crate_name == krate && x.item_name == name).unwrap_or_else(|| panic!("{krate}::{name} is not in the fixture"));
    let cos = |name_a: &str, name_b: &str| cosine(&of("ljar", name_a).vector, &of("lzip-parallel", name_b).vector);
    // the recorded numbers (a fixture that drifted would move these first)
    assert!((cos("local_data_offset", "local_data_offset") - 0.9276).abs() < 2e-3, "{}", cos("local_data_offset", "local_data_offset"));
    assert!((cos("main", "main") - 0.8559).abs() < 2e-3, "{}", cos("main", "main"));
    assert!((cos("bench_vs_miniz", "bench_inflate") - 0.9158).abs() < 2e-3, "{}", cos("bench_vs_miniz", "bench_inflate"));
    assert!(cos("decode_entry", "decode_entry") > 0.9999 && cos("physical_cores", "physical_cores") > 0.9999);

    let report = analyze_duplicates(&s, &DupThresholds::default(), &crate::mimir_clones::default_canonical_crates());
    let together = |a: &str, b: &str| {
        let (ia, ib) = (&of("ljar", a).id, &of("lzip-parallel", b).id);
        report.clusters.iter().any(|c| c.members.iter().any(|m| &m.id == ia) && c.members.iter().any(|m| &m.id == ib))
    };
    let t = DupThresholds::default().cosine;
    assert!(together("decode_entry", "decode_entry") && together("physical_cores", "physical_cores"), "the identical bodies are one cluster each");
    assert!(together("local_data_offset", "local_data_offset"), "a true copy at cosine 0.928 must be reported at the default {t}");
    assert!(!together("bench_vs_miniz", "bench_inflate"), "two different benchmarks at cosine 0.916 are NOT a duplicate: the default {t} must not report them");
    // the stated miss: a real copy the vectors rate below the bar
    assert!(!together("main", "main") && cos("main", "main") < t, "`main` moved above the bar — re-judge the band before believing it");
    assert_eq!(report.cluster_count, 3, "{:?}", report.clusters.iter().map(|c| &c.canonical).collect::<Vec<_>>());
}

/// The WebGPU kNN takes per-function vectors as it took line-window ones: the
/// recorded real vectors through `gpu::knn_loud` — the GPU when the box has a
/// real adapter, its bit-identical CPU int8 twin when it has none (the verdict
/// says which, and is printed). Each function's nearest OTHER function must be
/// the one the exact f32 scan names.
#[cfg(feature = "vector-gpu")]
#[test]
fn the_webgpu_knn_ranks_real_function_vectors_like_the_exact_scan() {
    use crate::vector::{VectorIndex, gpu};
    let s = recorded();
    let dim = s[0].vector.len();
    let mut index = VectorIndex::new(dim).unwrap();
    let flat: Vec<f32> = s.iter().flat_map(|x| x.vector.iter().copied()).collect();
    index.add(&flat, &(0..s.len() as u64).collect::<Vec<_>>()).unwrap();
    let (hits, verdict) = gpu::knn_loud(&index, &flat, 2);
    println!("kNN over {} real function vectors: {}", s.len(), verdict.line());
    assert_eq!(hits.len(), s.len());
    for (i, near) in hits.iter().enumerate() {
        let other = near.iter().find(|(id, _)| *id != i as u64).unwrap_or_else(|| panic!("{}: no neighbour but itself: {near:?}", s[i].item_name));
        let exact = (0..s.len()).filter(|j| *j != i).max_by(|a, b| crate::mimir_analyze::cosine(&s[i].vector, &s[*a].vector).total_cmp(&crate::mimir_analyze::cosine(&s[i].vector, &s[*b].vector))).unwrap();
        // int8 scores carry ~1e-2 of noise: equal up to that margin
        let (got, want) = (crate::mimir_analyze::cosine(&s[i].vector, &s[other.0 as usize].vector), crate::mimir_analyze::cosine(&s[i].vector, &s[exact].vector));
        assert!(want - got < 0.02, "{}: kNN says {} ({got:.4}), the exact scan {} ({want:.4})", s[i].item_name, s[other.0 as usize].item_name, s[exact].item_name);
    }
}

/// **The real-vectors instrument.** Runs the lens's own calibration view
/// (`similar_code` with `calibration: true`, the CLI's `mimir analyze
/// duplicates --calibration`) on a consumed, embedded workspace, prints it, and
/// can RECORD the vectors of named functions — the real fixture
/// `the_recorded_real_pairs_are_judged_as_a_person_judged_them` reads.
///
/// ```text
/// MIMIR_PROOF_BUILDS=<builds dir> [MIMIR_PROOF_FILES=a.rs,b.rs] \
///   [MIMIR_PROOF_RECORD=file#fn,file#fn MIMIR_PROOF_FIXTURE_OUT=<file>] \
///   cargo test --lib real_vectors_threshold_instrument -- --ignored --nocapture
/// ```
#[test]
#[ignore = "reads a real lake + warehouse: set MIMIR_PROOF_BUILDS"]
fn real_vectors_threshold_instrument() {
    let builds = std::path::PathBuf::from(std::env::var("MIMIR_PROOF_BUILDS").expect("MIMIR_PROOF_BUILDS"));
    let warehouse = crate::warehouse::iceberg::IcebergWarehouse::open_read_only(&builds).unwrap();
    let room = crate::code_room::CodeRoom::new("proof", &builds);
    let reach = crate::code_room::Reach::new(&room, &warehouse);
    let params = json!({});
    let q = super::Query::new("similar_code", &params, 50);
    let mut basis = super::Basis::default();
    let g = super::graph::Graph::open(&reach, &q, &mut basis).unwrap();
    let rows = super::vectors::rows(&g, &super::Warehouse::of(&reach), &q, &mut basis).unwrap_or_else(|r| panic!("[{}] {r}", r.state));
    let files = std::env::var("MIMIR_PROOF_FILES").ok();
    let pair = files.as_deref().and_then(|f| f.split_once(','));
    let top: usize = std::env::var("MIMIR_PROOF_TOP").ok().and_then(|v| v.parse().ok()).unwrap_or(20);
    let t0 = std::time::Instant::now();
    let view = super::vectors::calibration(&rows, crate::mimir_analyze::DupThresholds::default().cosine, top, pair);
    println!("{} function vector(s), {} distinct bodies, model {}; calibration view in {} ms", rows.symbols.len(), rows.distinct_bodies, rows.model_id, t0.elapsed().as_millis());
    println!("{}", serde_json::to_string_pretty(&view).unwrap());
    // RECORD: `MIMIR_PROOF_RECORD=file#name,file#name,…` + `MIMIR_PROOF_FIXTURE_OUT=<file>`
    if let (Ok(which), Ok(out)) = (std::env::var("MIMIR_PROOF_RECORD"), std::env::var("MIMIR_PROOF_FIXTURE_OUT")) {
        let wanted: Vec<(&str, &str)> = which.split(',').filter_map(|w| w.split_once('#')).collect();
        let recorded: Vec<Value> = rows
            .symbols
            .iter()
            .filter(|x| wanted.iter().any(|(f, n)| x.file == *f && x.item_name == *n))
            .map(|x| {
                json!({
                    "id": x.id, "repo": x.repo, "crate": x.crate_name, "module": x.module_path, "name": x.item_name,
                    "file": x.file, "line": x.line, "end_line": x.item_end_line, "signature": x.signature,
                    "callees": x.callees, "impl_header": x.impl_header, "vector": x.vector,
                })
            })
            .collect();
        assert_eq!(recorded.len(), wanted.len(), "every function asked for is recorded exactly once");
        std::fs::write(&out, serde_json::to_string(&json!({ "model_id": rows.model_id, "model_profile": rows.model_profile, "chunker": fnvec::CHUNKER, "functions": recorded })).unwrap()).unwrap();
        println!("recorded {} function vector(s) to {out}", wanted.len());
    }
}