Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkgs/atlas-search/crates/atlas-core/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,16 @@ fn grouped_output(
vec_dist: &HashMap<i64, Option<f64>>,
notes: &[String],
) -> Result<Json> {
let consideration = &ranked[..(opts.k * 10).max(opts.pool).min(ranked.len())];
// The consideration window is k * 10, but k is unbounded at
// every boundary that reaches this function (CLI, service), so
// the multiplication must saturate: a wrap-range k would
// otherwise wrap the window to near zero in release builds
// (silently truncating the grouped results) and panic in
// debug builds (#5423). Saturation makes a huge k behave as
// "consider everything", which the .min(ranked.len()) then
// bounds to the actual candidate list.
let window = opts.k.saturating_mul(10).max(opts.pool).min(ranked.len());
let consideration = &ranked[..window];
let ids: Vec<i64> = consideration.iter().map(|e| e.chunk_id).collect();
let details = details_map(backend, &ids)?;
let mut order: Vec<String> = Vec::new();
Expand Down
33 changes: 33 additions & 0 deletions pkgs/atlas-search/crates/atlas-core/tests/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,39 @@ fn local_search_envelopes_match_python() {
}
}

#[test]
fn grouped_wrap_range_k_considers_all_candidates() {
// #5423: k has no upper bound, and grouped_output's k * 10
// consideration window wrapped for wrap-range k (release) or
// panicked (debug). 5534023222112865485 * 10 mod 2^64 == 2, so
// the pre-fix window collapsed to max(2, pool). A wrap-range k
// must now behave exactly like a large sane k: every fused
// candidate is considered.
let db = build_fixture_db("wrapk");
let be =
LocalBackend::new(db.to_str().unwrap(), None, DEFAULT_VEC_TABLE, 0).expect("local backend");
let exp = expectations();
let qvec_raw: Vec<f32> = exp["qvec_raw"]
.as_array()
.unwrap()
.iter()
.map(|x| x.as_f64().unwrap() as f32)
.collect();
let embedder = FixedEmbedder { raw: qvec_raw };
let run = |k: usize| {
let mut o = SearchOptions::new("how do we back up the database");
o.group_by_session = true;
o.pool = 2;
o.k = k;
run_search(&be, &embedder, &default_filters(), &o).expect("grouped search")
};
let wrap = run(5_534_023_222_112_865_485);
let sane = run(1_000_000);
assert_eq!(wrap, sane, "wrap-range k must consider all candidates");
let results = wrap["results"].as_array().expect("results array");
assert!(!results.is_empty(), "grouped results must be non-empty");
}

#[test]
fn local_get_chunk_matches_python() {
let db = build_fixture_db("getchunk");
Expand Down
Loading