diff --git a/pkgs/atlas-search/crates/atlas-core/src/search.rs b/pkgs/atlas-search/crates/atlas-core/src/search.rs index 1ced9ba888..de50672cb9 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/search.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/search.rs @@ -448,7 +448,16 @@ fn grouped_output( vec_dist: &HashMap>, notes: &[String], ) -> Result { - 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 = consideration.iter().map(|e| e.chunk_id).collect(); let details = details_map(backend, &ids)?; let mut order: Vec = Vec::new(); diff --git a/pkgs/atlas-search/crates/atlas-core/tests/parity.rs b/pkgs/atlas-search/crates/atlas-core/tests/parity.rs index 17e5eb2c99..2c598155f8 100644 --- a/pkgs/atlas-search/crates/atlas-core/tests/parity.rs +++ b/pkgs/atlas-search/crates/atlas-core/tests/parity.rs @@ -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 = 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");