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/fixture/trivial.rs

//! The flood fixture of `semantic_clones`' embedding index: one repo `triv`
//! with crates `tra` and `trb`, whose bodies are the SAME statements in
//! another order — the Bag embedder reads them as one (cosine 1), MinHash
//! does not (the order is another shingle set), and they touch nothing in the
//! graph (no params past one slice, no calls): cosine-only candidates.
//!
//! ```text
//! tra/src/lib.rs   fmt × 4      <T_i as Display>::fmt — a trait-impl family
//!                  new × 4      C_i::new(..) -> Self — constructors
//!                  plain0..3    plain functions, ONE crate
//!                  get0..3      one-line accessors
//!                  mirror       the plain body reversed, the SAME crate
//! tests/it.rs      check0..3    tests (a fixture file)
//! trb/src/lib.rs   other        another body over the same calls, ANOTHER crate
//! ```
//!
//! Only `other` × `plain_i` may survive (`mirror` × `plain_i` is one crate) the embedding index's gates: every
//! other cosine pair is a family, a test, a one-liner or inside one crate.

use std::collections::BTreeSet;
use std::path::PathBuf;

use mimir_ingest::CommitDump;
use mimir_schema::{EdgeKind, EdgeRow, Level, moniker};

use super::vectors::{Source, consume_next, param, stubs_into};
use super::{N, Workspace, e, n, unit};
use crate::code_room::CodeRoom;
use crate::warehouse::iceberg::IcebergWarehouse;

pub const TRIV: &str = "triv";

pub fn tra(d: &str) -> String {
    moniker::moniker("tra", "0.1.0", d)
}
pub fn trb(d: &str) -> String {
    moniker::moniker("trb", "0.1.0", d)
}

/// The six independent statements every body is made of.
const STMTS: [&str; 6] = [
    "let a = xs.len() as u32;",
    "let b = xs.first().copied().unwrap_or(0);",
    "let c = xs.last().copied().unwrap_or(0);",
    "let d = xs.iter().sum::<u32>();",
    "let e = xs.iter().max().copied().unwrap_or(0);",
    "let f = xs.iter().min().copied().unwrap_or(0);",
];

/// The statements rotated by `r` (reversed when `r` is 99), indented, and the tail.
fn body(r: usize, tail: &str) -> String {
    let mut s: Vec<&str> = STMTS.to_vec();
    if r == 99 {
        s.reverse();
    } else {
        s.rotate_left(r % STMTS.len());
    }
    let mut out: String = s.iter().map(|x| format!("    {x}\n")).collect();
    out.push_str(&format!("    {tail}\n"));
    out
}

/// The workspace with `triv` consumed (no shape, no vector yet).
pub struct Triv {
    pub ws: Workspace,
    root: PathBuf,
}

impl Triv {
    pub fn checkout(&self) -> PathBuf {
        self.root.join("src-triv")
    }

    fn at(&self) -> (i64, String) {
        let log = self.ws.room.log().unwrap();
        let o = log.block_on(log.offsets()).unwrap().remove(TRIV).expect("triv is consumed");
        (o.seq, o.sha)
    }

    /// The `clone_shapes` feed for `triv`.
    pub fn shapes(&self) -> crate::clone_signals::feed::ShapeRun {
        let (seq, sha) = self.at();
        let log = self.ws.room.log().unwrap();
        crate::clone_signals::feed::shapes_member(&self.ws.warehouse, &log, TRIV, &self.checkout(), seq, &sha).unwrap()
    }

    /// The raw and the normalised vectors, through `bag`.
    pub fn embed(&self, bag: &super::vectors::Bag) -> crate::clone_signals::feed::NormRun {
        let (seq, sha) = self.at();
        let log = self.ws.room.log().unwrap();
        let mut embedding = crate::vector::fnvec::Loaded { embedder: bag, backend: "test bag (cpu)" };
        crate::vector::fnvec::room::embed_member_with(&self.ws.warehouse, &log, TRIV, &self.checkout(), seq, &sha, &mut embedding).unwrap();
        let chunks = crate::clone_signals::feed::read_chunks(&log, TRIV, &self.checkout(), seq).unwrap();
        crate::clone_signals::feed::embed_normalised(&self.ws.warehouse, &chunks, &mut embedding).unwrap()
    }
}

/// One function node with one slice parameter (and, for a constructor, a
/// `-> Self` part), appended to a unit.
fn fun(nodes: &mut Vec<N>, edges: &mut Vec<EdgeRow>, id: &str, kind: &str, name: &str, parent: &str, ret: Option<&str>) {
    let level = if kind == "method" { Level::Part } else { Level::Item };
    nodes.push(n(id.to_string(), kind, level, name, Some(parent.to_string())));
    edges.push(e(parent, id, EdgeKind::Contains, 1));
    let (pn, pe) = param(id, "xs", "&[u32]");
    nodes.push(pn);
    edges.push(pe);
    if let Some(ty) = ret {
        let rid = format!("{id}`->`");
        nodes.push(n(rid.clone(), "ret", Level::Part, "->", Some(id.to_string())).ty(ty));
        edges.push(e(id, &rid, EdgeKind::Contains, 1));
    }
}

fn commit(root: &std::path::Path) -> CommitDump {
    // ── tra ──
    let krate = tra("crate/");
    let display = moniker::moniker("core", "1.0.0", "fmt/Display#");
    let mut nodes: Vec<N> = vec![n(krate.clone(), "crate", Level::Crate, "tra", None)];
    let mut edges: Vec<EdgeRow> = Vec::new();
    let mut src = Source::default();
    src.plain("//! tra\n");
    for i in 0..4 {
        // a trait-impl family: `impl Display for T_i { fn fmt }`
        let imp = tra(&format!("impl#[T{i}][Display]"));
        nodes.push(n(imp.clone(), "impl", Level::Item, &format!("T{i}"), Some(krate.clone())));
        edges.push(e(&krate, &imp, EdgeKind::Contains, 1));
        edges.push(e(&imp, &display, EdgeKind::Implements, 1));
        let id = tra(&format!("impl#[T{i}][Display]fmt()."));
        src.plain(&format!("impl Display for T{i} {{"));
        src.item(&id, 4, &format!("fn fmt(&self, xs: &[u32]) -> u32 {{\n{}}}", body(i, "a + b + c + d + e + f")));
        src.plain("}\n");
        fun(&mut nodes, &mut edges, &id, "method", "fmt", &imp, Some("u32"));
        // a constructor: `C_i::new(..) -> Self`
        let cimp = tra(&format!("impl#[C{i}]"));
        nodes.push(n(cimp.clone(), "impl", Level::Item, &format!("C{i}"), Some(krate.clone())));
        edges.push(e(&krate, &cimp, EdgeKind::Contains, 1));
        let id = tra(&format!("impl#[C{i}]new()."));
        src.plain(&format!("impl C{i} {{"));
        src.item(&id, 4, &format!("pub fn new(xs: &[u32]) -> Self {{\n{}}}", body(i, "Self { total: a + b + c + d + e + f }")));
        src.plain("}\n");
        fun(&mut nodes, &mut edges, &id, "method", "new", &cimp, Some("Self"));
        // a plain function, all four in ONE crate
        let id = tra(&format!("plain{i}()."));
        src.item(&id, 0, &format!("pub fn plain{i}(xs: &[u32]) -> u32 {{\n{}}}", body(i, "a + b + c + d + e + f")));
        fun(&mut nodes, &mut edges, &id, "fn", &format!("plain{i}"), &krate, Some("u32"));
        // a one-line accessor
        let id = tra(&format!("get{i}()."));
        src.item(&id, 0, &format!("pub fn get{i}(xs: &[u32]) -> u32 {{ xs.len() as u32 }}"));
        fun(&mut nodes, &mut edges, &id, "fn", &format!("get{i}"), &krate, Some("u32"));
    }
    // the plain body reversed, in the SAME crate (another tail: another exact body)
    let id = tra("mirror().");
    src.item(&id, 0, &format!("pub fn mirror(xs: &[u32]) -> u32 {{\n{}}}", body(99, "f + e + d + c + b + a")));
    fun(&mut nodes, &mut edges, &id, "fn", "mirror", &krate, Some("u32"));
    let mut spans = src.write(root, "tra/src/lib.rs");
    // tests: a fixture file of the same crate
    let mut tests = Source::default();
    tests.plain("//! integration tests\n");
    for i in 0..4 {
        let id = tra(&format!("check{i}()."));
        tests.item(&id, 0, &format!("fn check{i}(xs: &[u32]) -> u32 {{\n{}}}", body(i + 1, "a + b + c + d + e + f")));
        fun(&mut nodes, &mut edges, &id, "fn", &format!("check{i}"), &krate, Some("u32"));
    }
    spans.extend(tests.write(root, "tests/it.rs"));
    let stubs: BTreeSet<String> = [display.clone()].into();
    stubs_into(&mut nodes, &stubs);
    let tra_unit = unit("tra@0.1.0:lib", nodes, edges, spans);

    // ── trb ──
    let krate = trb("crate/");
    let mut nodes: Vec<N> = vec![n(krate.clone(), "crate", Level::Crate, "trb", None)];
    let mut edges: Vec<EdgeRow> = Vec::new();
    let mut src = Source::default();
    src.plain("//! trb\n");
    let id = trb("other().");
    // another body over the same calls: the model reads it alike, MinHash does not
    src.item(
        &id,
        0,
        "pub fn other(xs: &[u32]) -> u32 {\n    let mut t = 0u32;\n    for x in xs.iter() {\n        if *x > 0 {\n            t = t.wrapping_add(*x);\n        }\n    }\n    let e = xs.iter().max().copied().unwrap_or(0);\n    let f = xs.iter().min().copied().unwrap_or(0);\n    t + e + f + xs.len() as u32\n}",
    );
    fun(&mut nodes, &mut edges, &id, "fn", "other", &krate, Some("u32"));
    let spans = src.write(root, "trb/src/lib.rs");
    let trb_unit = unit("trb@0.1.0:lib", nodes, edges, spans);

    CommitDump {
        toolchain: "nightly-fixture".into(),
        workspace_crates: ["tra@0.1.0:lib".to_string(), "trb@0.1.0:lib".to_string()].into(),
        workspace_targets: ["tra:lib".to_string(), "trb:lib".to_string()].into(),
        failed_units: BTreeSet::new(),
        owner_disambiguations: 0,
        crates: vec![tra_unit, trb_unit],
        binary: false,
        binaries: vec![],
    }
}

/// `triv` consumed at its one commit.
pub fn build() -> Triv {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().to_path_buf();
    let builds = root.join("builds");
    std::fs::create_dir_all(&builds).unwrap();
    let warehouse = IcebergWarehouse::open(&builds).unwrap();
    let room = CodeRoom::new("trivial", &builds);
    let dir = root.join("src-triv");
    nornir_git::gitio::init(&dir).unwrap();
    let dump = commit(&dir);
    nornir_git::gitio::commit_all(&dir, "first").unwrap();
    consume_next(&room, &dir, TRIV, dump);
    Triv { ws: Workspace { room, warehouse, builds, _tmp: tmp }, root }
}