ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! **THE INDEX GRAIN'S DEPTH SPEND**, read off the tokens: one resolution per fetch, all of them
//! through the framework's own law, and BOTH set doors' parameter beside their `limit`.
//!
//! Beside [`k_seat`](super::k_seat) on this directory's own precedent — one file per thing being
//! proved. The BEHAVIOURAL half is `ingraph`'s `tests/route_depth.rs`, where three engines record
//! the `k` each leg was handed; what only a token reading can prove is that no fetch site does its
//! OWN arithmetic, which is a claim about every site at once rather than about the ones a fixture
//! happens to walk.

use super::{
  ElectionLabel, IndexModel,
  tests::{document, embedding_index, two_elections},
};

/// The framework law every fetch site resolves through.
const LAW: &str = "::ingraph::__private::retrieve::route_depth(";

/// The edge fact, read off a route's own seat.
const EDGE: &str = "::ingraph::__private::retrieve::Route::depth";

/// The emission with its spaces taken out — what a claim about a call or a signature is read off.
fn tight(model: &IndexModel) -> String {
  model.emit().to_string().split_whitespace().collect()
}

/// ONE door's own tokens, cut at the next item.
///
/// The cut is what makes a COUNT mean anything here: the emission holds every door TWICE — once
/// inside the observation gate and once outside it — so an unbounded tail doubles every number and
/// an assertion over one would be an assertion about the gate rather than about the door.
fn door<'a>(emission: &'a str, name: &str) -> &'a str {
  let at = emission
    .split(&format!("asyncfn{name}<"))
    .nth(1)
    .unwrap_or_else(|| panic!("`{name}` is emitted: {emission}"));
  let end = [at.find("#[doc"), at.find("asyncfn")]
    .into_iter()
    .flatten()
    .min()
    .unwrap_or(at.len());
  &at[..end]
}

/// The same model with its election relabelled a SET, which is what the two `_with` doors hang off.
fn as_set(mut model: IndexModel) -> IndexModel {
  for elected in &mut model.embeddings {
    elected.label = ElectionLabel::List;
    elected.ty = syn::parse_quote!(::std::vec::Vec<::ingraph::Embedding<f32, 4, Clip>>);
  }
  model
}

/// **NO FETCH SITE DOES ITS OWN ARITHMETIC** — the one-authority claim, over the whole emission.
///
/// Before this round each site spelled its own `ceiling`-or-`limit` chain and widened a
/// `NonZeroU32` inline. Six of them, in three emitters, each free to drift from the others. The
/// negative assertion is what says the chain moved rather than being copied a seventh time: a
/// widening spelled inside a door is a door that decided a precedence.
#[test]
fn no_door_spells_a_chain_of_its_own() {
  for model in [document(), embedding_index(), two_elections()] {
    let emitted = tight(&model);
    assert!(
      !emitted.contains("NonZeroUsize::try_from"),
      "a door widened a ceiling by hand: {emitted}",
    );
  }
}

/// **A TIER'S ROUTES EACH RESOLVE THEIR OWN DEPTH, OFF THEIR OWN EDGE.**
///
/// Two routes, and both fused doors — the plain one and its by-example twin — resolve once per
/// route off that route's own `Route`. The COUNT is what makes it a claim rather than a spot check:
/// a door resolving once and reusing the number across its routes is the plausible wrong shape, and
/// it lands at one rather than two.
#[test]
fn each_tier_route_resolves_its_own_edge_in_both_fused_doors() {
  let emitted = tight(&two_elections());

  for name in ["vector_fuse", "vector_fuse_similar"] {
    let body = door(&emitted, name);
    assert_eq!(
      body.matches(LAW).count(),
      2,
      "`{name}` resolves once per route: {body}",
    );
    assert_eq!(
      body.matches(EDGE).count(),
      2,
      "`{name}` reads each route's own edge: {body}",
    );
  }
}

/// **A MEMBER'S OWN `keys` RESOLVES TOO, AND WITH NO EDGE OF ITS OWN** — the fractal, at the
/// grain where the parent has already spent the edge.
///
/// A single-election vector declaration's capability impl reaches one door, so it resolves once; the
/// EDGE argument is `None`, because the parent resolved its `Route` before it called and what
/// arrives as `limit` IS that edge's number. Both halves are asserted, since a `None` alone would
/// also be satisfied by an emission that had simply forgotten to read a seat.
#[test]
fn a_members_own_read_resolves_against_its_declaration_and_not_an_edge() {
  let emitted = tight(&embedding_index());
  // No cut: the capability impl is emitted ONCE, outside the observation gate the doors live inside,
  // so the tail is this one impl and nothing after it takes a budget.
  let capability = emitted
    .split("fnkeys<'__ingraph_ctx>(")
    .nth(1)
    .expect("a vector declaration answers a fused read");

  assert_eq!(capability.matches(LAW).count(), 1, "{capability}");
  assert!(
    capability.contains("route_depth(::core::option::Option::None,"),
    "a member read an edge fact its parent had already spent: {capability}",
  );
  assert!(
    !capability.contains(EDGE),
    "there is no `Route` at this grain to read one off: {capability}",
  );
}

/// **BOTH `_with` DOORS TAKE `depth` BESIDE `limit`** — the ASYMMETRY pin, flipped by the letter it
/// was written as.
///
/// Its negative half used to read *and its raw twin does NOT*, and that was honest rather than a
/// gap: the ruling that opened the seat named the by-example door, the raw `FoldedQuery` twin beside it
/// was the same shape and was not named, so the asymmetry went up as a letter instead of being
/// guessed at. The letter came back 甲 — every clause of the rationale was measured to cover the
/// twin word for word — so the pin becomes what it was always waiting to be: a SYMMETRY claim, over
/// both doors, at the same position and in the same currency.
#[test]
fn both_set_doors_carry_the_streams_depth() {
  let emitted = tight(&as_set(embedding_index()));

  for name in [
    "retrieve_similar_by_image_embedding_with",
    "retrieve_keys_by_image_embedding_with",
  ] {
    let body = door(&emitted, name);
    assert!(
      body.contains(
        "limit:::core::num::NonZeroUsize,depth:::core::option::Option<::core::num::NonZeroUsize>,"
      ),
      "`{name}` does not carry the streams\' depth beside its limit: {body}",
    );
  }
}

/// **EACH SET DOOR RESOLVES ITS DEPTH ONCE AND SPENDS IT PER STREAM.**
///
/// Once, because the `m` streams share one number — resolving inside the loop would be the same
/// answer computed `m` times and would invite a per-stream variation nobody ruled. Per stream,
/// because the number reaches the `knn` inside the loop and not the fold outside it: the two bounds
/// are at different grains and the fold's own `limit` is what still counts entities.
///
/// Read at BOTH doors since the twin grew the seat, which is what says the two spend the law rather
/// than one spending it and the other spelling something that happens to agree.
#[test]
fn the_set_doors_depth_is_resolved_once_and_reaches_the_engine() {
  let emitted = tight(&as_set(embedding_index()));

  for name in [
    "retrieve_similar_by_image_embedding_with",
    "retrieve_keys_by_image_embedding_with",
  ] {
    let body = door(&emitted, name);

    assert_eq!(body.matches(LAW).count(), 1, "`{name}`: {body}");
    assert!(
      body.contains("route_depth(depth,::core::option::Option::None,limit)"),
      "`{name}` resolved something other than its own parameter: {body}",
    );
    assert!(
      body.contains("__ingraph_depth,options,).await"),
      "`{name}`: the resolved depth did not reach the engine: {body}",
    );
    assert!(
      body.contains("&fold,::core::num::NonZeroUsize::get(limit)"),
      "`{name}`: the fold stopped counting entities at `limit`: {body}",
    );
  }
}