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
1 change: 1 addition & 0 deletions pkgs/atlas-search/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkgs/atlas-search/crates/atlas-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ description = "Atlas search semantics as a library: filters and sensitivity ceil

[dependencies]
atlas-embed = { path = "../atlas-embed" }
atlas-proto = { path = "../atlas-proto" }
base64 = "0.22"
rusqlite = { version = "0.37", features = ["bundled", "functions"] }
serde = { version = "1", features = ["derive"] }
Expand Down
33 changes: 28 additions & 5 deletions pkgs/atlas-search/crates/atlas-core/src/fusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ use serde_json::{Map, Value as Json};

use crate::types::BranchHit;

/// Production RRF constant (decided 2026-10-31; the Python's `RRF_K`).
pub const RRF_K: usize = 10;

/// The Python's `fts_match_expr` (the eval3.py hybrid-branch
/// construction): content words OR-joined as quoted phrases. Words
/// >3 chars, plus short tokens containing a digit (r2, s3, b2) which
Expand Down Expand Up @@ -87,6 +84,24 @@ impl FusedEntry {
pub fn fuse(
fts: &[BranchHit],
vector: &[BranchHit],
) -> (Vec<FusedEntry>, HashMap<i64, Option<f64>>) {
let defaults = atlas_proto::SearchParametersV1::default();
fuse_with_parameters(
fts,
vector,
defaults.rrf_k,
defaults.fts_weight,
defaults.vector_weight,
)
}

/// Fuse using query-time RRF and per-signal multipliers.
pub fn fuse_with_parameters(
fts: &[BranchHit],
vector: &[BranchHit],
rrf_k: usize,
fts_weight: f64,
vector_weight: f64,
) -> (Vec<FusedEntry>, HashMap<i64, Option<f64>>) {
let mut entries: Vec<FusedEntry> = Vec::new();
let mut by_id: HashMap<i64, usize> = HashMap::new();
Expand All @@ -107,14 +122,14 @@ pub fn fuse(
for (i, hit) in fts.iter().enumerate() {
let rank = i + 1;
let idx = get_or_insert(&mut entries, hit.chunk_id);
entries[idx].score += 1.0 / (RRF_K + rank) as f64;
entries[idx].score += fts_weight / (rrf_k + rank) as f64;
entries[idx].fts_rank = Some(rank);
}
let mut vec_dist: HashMap<i64, Option<f64>> = HashMap::new();
for (i, hit) in vector.iter().enumerate() {
let rank = i + 1;
let idx = get_or_insert(&mut entries, hit.chunk_id);
entries[idx].score += 1.0 / (RRF_K + rank) as f64;
entries[idx].score += vector_weight / (rrf_k + rank) as f64;
entries[idx].vector_rank = Some(rank);
vec_dist.insert(hit.chunk_id, hit.dist);
}
Expand Down Expand Up @@ -188,6 +203,14 @@ mod tests {
assert_eq!(dist.get(&42), Some(&Some(0.5)));
}

#[test]
fn query_parameters_control_rrf_and_signal_weights() {
let (fused, _) = fuse_with_parameters(&[hit(1, None)], &[hit(1, Some(0.1))], 20, 2.0, 1.0);
assert_eq!(round6(fused[0].score), 0.142857);
let (fts_only, _) = fuse_with_parameters(&[hit(1, None)], &[], 20, 0.0, 1.0);
assert_eq!(round6(fts_only[0].score), 0.0);
}

#[test]
fn fuse_sort_tie_breaks() {
// equal fused scores fall to best rank, then chunk_id
Expand Down
5 changes: 3 additions & 2 deletions pkgs/atlas-search/crates/atlas-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//! a vector pool with the caller's sensitivity ceiling and
//! filters applied **inside the branch queries** — before any
//! ranking — and [`run_search`] fuses the pools with reciprocal
//! rank fusion (k = 10, pool 100/branch by default).
//! rank fusion using the versioned per-query contract (defaults in
//! atlas-proto's query-parameters-v1.json).
//!
//! Vector strategies all live behind the backend:
//! [`VectorBackend::Scan`] (exact `vector_distance_cos` over the
Expand Down Expand Up @@ -51,7 +52,7 @@ pub use eval::{
select_best_signal,
};
pub use filters::{Filters, Sensitivity};
pub use fusion::{FusedEntry, RRF_K};
pub use fusion::FusedEntry;
pub use local::LocalBackend;
pub use search::{
BackendBranchExecutor, BranchPoolExecutor, SearchMode, SearchOptions, run_get_chunk,
Expand Down
25 changes: 20 additions & 5 deletions pkgs/atlas-search/crates/atlas-core/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

use std::collections::HashMap;

use atlas_proto::SearchParametersV1;
use serde_json::{Map, Value as Json, json};

use crate::backend::SearchBackend;
use crate::embed::QueryEmbedder;
use crate::error::{Error, Result};
use crate::filters::Filters;
use crate::fusion::{FusedEntry, fts_match_expr, fuse};
use crate::fusion::{FusedEntry, fts_match_expr, fuse_with_parameters};
use crate::types::{BranchPools, BranchQuery, ChunkDetail, DEFAULT_MODEL, VectorBackend, round6};

/// The Python's `MESSAGE_TEXT_CAP`.
Expand Down Expand Up @@ -77,22 +78,25 @@ pub struct SearchOptions {
/// Query-vector truncation (0 = full width).
pub trunc: usize,
pub vector_backend: VectorBackend,
/// Versioned query-time controls shared with the wire protocol.
pub parameters: SearchParametersV1,
}

impl Default for SearchOptions {
fn default() -> Self {
SearchOptions {
query: String::new(),
mode: SearchMode::Hybrid,
k: 10,
pool: 100,
k: SearchParametersV1::default_k(),
pool: SearchParametersV1::default_pool(),
group_by_session: false,
expand: 0,
expand: SearchParametersV1::default_expand(),
with_message: false,
model: DEFAULT_MODEL.to_string(),
embedder_label: "auto".to_string(),
trunc: 0,
vector_backend: VectorBackend::Scan,
parameters: SearchParametersV1::default(),
}
}
}
Expand Down Expand Up @@ -162,6 +166,9 @@ pub fn run_search_with_executor(
opts: &SearchOptions,
executor: &dyn BranchPoolExecutor,
) -> Result<Json> {
opts.parameters
.validate(opts.expand)
.map_err(Error::fatal)?;
if opts.expand > crate::MAX_EXPAND_NEIGHBORS {
return Err(Error::fatal(format!(
"expand must be at most {}",
Expand Down Expand Up @@ -205,11 +212,19 @@ pub fn run_search_with_executor(
match_expr,
query_vector: qvec,
pool: opts.pool,
shard_top_k: opts.parameters.shard_top_k,
global_top_k: opts.parameters.global_top_k,
vector_backend: opts.vector_backend,
},
filters,
)?;
let (ranked, vec_dist) = fuse(&pools.fts, &pools.vector);
let (ranked, vec_dist) = fuse_with_parameters(
&pools.fts,
&pools.vector,
opts.parameters.rrf_k,
opts.parameters.fts_weight,
opts.parameters.vector_weight,
);

// The local backend learns things mid-search (vector
// coverage); fold any late notes in before rendering.
Expand Down
17 changes: 13 additions & 4 deletions pkgs/atlas-search/crates/atlas-core/src/shards/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub struct ShardHit {

pub struct FanoutQuery<'a> {
pub qvec: &'a [f32],
pub k: usize,
pub shard_top_k: usize,
pub global_top_k: usize,
pub filters: &'a Filters,
/// Manifest topics to scope to (`None` = every topic).
pub topics: Option<&'a [String]>,
Expand Down Expand Up @@ -119,7 +120,12 @@ pub fn fanout_scan(set: &ShardSet, q: &FanoutQuery<'_>) -> Result<FanoutResult>
));
}
let shards = set.select(q.ceiling, q.topics)?;
let stmt = turso_vector_scan_stmt(set.vec_table(), q.filters, &qvec_json(q.qvec), q.k);
let stmt = turso_vector_scan_stmt(
set.vec_table(),
q.filters,
&qvec_json(q.qvec),
q.shard_top_k,
);
let per_shard: Vec<Result<Vec<ShardHit>>> = std::thread::scope(|scope| {
let handles: Vec<_> = shards
.iter()
Expand All @@ -138,7 +144,7 @@ pub fn fanout_scan(set: &ShardSet, q: &FanoutQuery<'_>) -> Result<FanoutResult>
all.extend(r?);
}
Ok(FanoutResult {
hits: merge_top_k(all, q.k),
hits: merge_top_k(all, q.global_top_k),
scanned: shards.iter().map(|s| s.name.clone()).collect(),
})
}
Expand Down Expand Up @@ -201,7 +207,8 @@ fn vector_pool(
set,
&FanoutQuery {
qvec,
k: q.pool,
shard_top_k: q.shard_top_k,
global_top_k: q.global_top_k,
filters,
topics,
ceiling: TierCeiling::from_sensitivity(filters.max_sensitivity),
Expand Down Expand Up @@ -247,6 +254,8 @@ impl<B: SearchBackend> SearchBackend for ShardedBackend<B> {
match_expr: q.match_expr.clone(),
query_vector: None,
pool: q.pool,
shard_top_k: q.shard_top_k,
global_top_k: q.global_top_k,
vector_backend: q.vector_backend,
};
let (set, topics) = (&*self.set, self.topics.as_deref());
Expand Down
3 changes: 3 additions & 0 deletions pkgs/atlas-search/crates/atlas-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ pub struct BranchQuery {
/// Normalized (and possibly truncated) query vector.
pub query_vector: Option<Vec<f32>>,
pub pool: usize,
/// Independent query-time limits for sharded vector fan-out.
pub shard_top_k: usize,
pub global_top_k: usize,
pub vector_backend: crate::VectorBackend,
}

Expand Down
38 changes: 28 additions & 10 deletions pkgs/atlas-search/crates/atlas-core/tests/shards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ fn fanout_equals_monolith_oracle_over_random_queries() {
&set,
&FanoutQuery {
qvec: &q,
k,
shard_top_k: 40,
global_top_k: k,
filters: &f,
topics: None,
ceiling: TierCeiling::from_sensitivity(f.max_sensitivity),
Expand Down Expand Up @@ -492,7 +493,8 @@ fn physical_ceiling_never_connects_to_tiers_above() {
&set,
&FanoutQuery {
qvec: &q,
k: 100,
shard_top_k: 100,
global_top_k: 100,
filters: &f,
topics: None,
ceiling: TierCeiling::from_sensitivity(f.max_sensitivity),
Expand All @@ -512,7 +514,8 @@ fn physical_ceiling_never_connects_to_tiers_above() {
&set,
&FanoutQuery {
qvec: &q,
k: 5,
shard_top_k: 5,
global_top_k: 5,
filters: &f,
topics: None,
ceiling: TierCeiling::from_sensitivity(f.max_sensitivity),
Expand Down Expand Up @@ -541,7 +544,8 @@ fn physical_ceiling_never_connects_to_tiers_above() {
&set,
&FanoutQuery {
qvec: &q,
k: 3,
shard_top_k: 3,
global_top_k: 3,
filters: &f,
topics: None,
ceiling: TierCeiling(Tier::Secret),
Expand All @@ -558,7 +562,8 @@ fn physical_ceiling_never_connects_to_tiers_above() {
&set,
&FanoutQuery {
qvec: &q,
k: 100,
shard_top_k: 100,
global_top_k: 100,
filters: &f,
topics: None,
ceiling: TierCeiling::from_sensitivity(f.max_sensitivity),
Expand All @@ -574,7 +579,8 @@ fn physical_ceiling_never_connects_to_tiers_above() {
&set,
&FanoutQuery {
qvec: &q,
k: 100,
shard_top_k: 100,
global_top_k: 100,
filters: &f,
topics: None,
ceiling: TierCeiling::from_sensitivity(f.max_sensitivity),
Expand All @@ -597,7 +603,8 @@ fn topic_scope_touches_only_that_topics_shards() {
&set,
&FanoutQuery {
qvec: &q,
k: 10,
shard_top_k: 10,
global_top_k: 10,
filters: &f,
topics: Some(&topics),
ceiling: TierCeiling(Tier::Internal),
Expand All @@ -624,7 +631,8 @@ fn scans_run_in_parallel() {
&set,
&FanoutQuery {
qvec: &q,
k: 10,
shard_top_k: 10,
global_top_k: 10,
filters: &f,
topics: None,
ceiling: TierCeiling(Tier::Internal),
Expand Down Expand Up @@ -664,7 +672,8 @@ fn unsupported_filters_fail_loudly_instead_of_dropping_them() {
&set,
&FanoutQuery {
qvec: &Rng(1).vec(),
k: 5,
shard_top_k: 5,
global_top_k: 5,
filters: &f,
topics: None,
ceiling: TierCeiling(Tier::Internal),
Expand Down Expand Up @@ -693,7 +702,8 @@ fn failing_shard_fails_the_query() {
&set,
&FanoutQuery {
qvec: &Rng(2).vec(),
k: 5,
shard_top_k: 5,
global_top_k: 5,
filters: &f,
topics: None,
ceiling: TierCeiling(Tier::Internal),
Expand Down Expand Up @@ -775,6 +785,8 @@ fn sharded_backend_takes_vector_from_shards_and_fts_from_the_monolith() {
match_expr: Some("nix".into()),
query_vector: Some(q.clone()),
pool: 10,
shard_top_k: 10,
global_top_k: 10,
vector_backend: VectorBackend::Scan,
},
&f,
Expand Down Expand Up @@ -802,6 +814,8 @@ fn sharded_backend_takes_vector_from_shards_and_fts_from_the_monolith() {
match_expr: None,
query_vector: Some(q.clone()),
pool: 10,
shard_top_k: 10,
global_top_k: 10,
vector_backend: VectorBackend::Scan,
},
&filters("secret-bearing"),
Expand All @@ -817,6 +831,8 @@ fn sharded_backend_takes_vector_from_shards_and_fts_from_the_monolith() {
match_expr: None,
query_vector: Some(q),
pool: 10,
shard_top_k: 10,
global_top_k: 10,
vector_backend: VectorBackend::IndexFill,
},
&f,
Expand All @@ -832,6 +848,8 @@ fn sharded_backend_takes_vector_from_shards_and_fts_from_the_monolith() {
match_expr: Some("x".into()),
query_vector: None,
pool: 5,
shard_top_k: 10,
global_top_k: 10,
vector_backend: VectorBackend::Scan,
},
&f,
Expand Down
18 changes: 18 additions & 0 deletions pkgs/atlas-search/crates/atlas-proto/query-parameters-v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"k": 10,
"pool": 100,
"expand": 0,
"parameters_version": 1,
"shard_top_k": 100,
"global_top_k": 100,
"fts_weight": 1.0,
"vector_weight": 1.0,
"jaccard_depth": 0,
"jaccard_threshold": 0.0,
"rrf_k": 10,
"planner_enabled": false,
"spell_enabled": false,
"query_class_hint": null,
"variant": "raw",
"raw": false
}
Loading
Loading