From 45a8aabf79afa8fb33255c335b794ff2adcc83e5 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 15:16:20 -0600 Subject: [PATCH] feat(atlas): record embedding_model in atlas_manifest, refuse mixed-model fan-out (#7646) --- .../crates/atlas-core/src/shards/mod.rs | 35 ++++++ .../crates/atlas-core/src/shards/scan.rs | 25 +++++ .../crates/atlas-core/tests/shards.rs | 103 ++++++++++++++++++ .../crates/atlas-search/tests/cli_parity.rs | 4 + 4 files changed, 167 insertions(+) diff --git a/pkgs/atlas-search/crates/atlas-core/src/shards/mod.rs b/pkgs/atlas-search/crates/atlas-core/src/shards/mod.rs index eff3d3ffcb..ce4ab13d1a 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/shards/mod.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/shards/mod.rs @@ -394,6 +394,12 @@ pub struct Manifest { pub part: Option, pub expected_chunks: Option, pub metric: String, + /// The embedding model that produced the shard's vectors, from + /// the `embedding_model` manifest key (#6060 M1, #7646). `None` + /// when the key is absent: pre-M1 shards carry no identity, and + /// discovery refuses them in strict mode rather than guessing + /// one (see `probe`). + pub embedding_model: Option, } impl Manifest { @@ -414,6 +420,7 @@ impl Manifest { part: pairs.get("part").and_then(|p| p.parse().ok()), expected_chunks: pairs.get("expected_chunks").and_then(|p| p.parse().ok()), metric: need("metric")?, + embedding_model: pairs.get("embedding_model").cloned(), }) } @@ -444,6 +451,10 @@ pub struct ShardInfo { pub topic: String, pub part: u32, pub expected_chunks: Option, + /// The shard's embedding model identity (manifest + /// `embedding_model` key), carried so fan-out can prove the + /// scanned set is single-model before merging (#6060 M1). + pub embedding_model: Option, /// `count(*)` of `chunks` at discovery time. pub chunks: u64, pub endpoint: ShardEndpoint, @@ -593,6 +604,29 @@ fn probe( c.endpoint.name, m.metric ))); } + // #6060 M1 (#7646): vectors from different embedding models are + // not comparable, so a shard whose model identity is missing or + // differs from the query model is refused here, with the same + // strict/lenient posture as the metric check above. The query + // model is the mandated one: queries are embedded through + // atlas-embed with `DEFAULT_MODEL`. + match &m.embedding_model { + None => { + return Err(Error::fatal(format!( + "shard {}: atlas_manifest has no `embedding_model` key; this consumer embeds queries with `{}` and cannot verify the shard's vectors were produced by it", + c.endpoint.name, + crate::types::DEFAULT_MODEL + ))); + } + Some(model) if model != crate::types::DEFAULT_MODEL => { + return Err(Error::fatal(format!( + "shard {}: embedding_model `{model}` does not match the query model `{}`; refusing to scan a mixed-model shard", + c.endpoint.name, + crate::types::DEFAULT_MODEL + ))); + } + _ => {} + } let n = t .pipeline(&[Stmt::new("SELECT COUNT(*) AS n FROM chunks", vec![])])? .into_iter() @@ -609,6 +643,7 @@ fn probe( topic: c.topic.clone(), part: m.part.unwrap_or(c.part), expected_chunks: m.expected_chunks, + embedding_model: m.embedding_model.clone(), chunks: n.max(0) as u64, endpoint: c.endpoint.clone(), }, diff --git a/pkgs/atlas-search/crates/atlas-core/src/shards/scan.rs b/pkgs/atlas-search/crates/atlas-core/src/shards/scan.rs index cb3607fa83..aeae542f93 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/shards/scan.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/shards/scan.rs @@ -120,6 +120,30 @@ pub fn fanout_scan(set: &ShardSet, q: &FanoutQuery<'_>) -> Result )); } let shards = set.select(q.ceiling, q.topics)?; + // #6060 M1 (#7646): never merge across embedding models. + // Discovery already refuses a shard whose manifest model is + // missing or differs from the query model; re-check the selected + // set here so a fan-out fails loud instead of merging partially, + // whatever produced the set. + let mut scanned_model: Option<&str> = None; + for shard in &shards { + let model = shard.embedding_model.as_deref().ok_or_else(|| { + Error::fatal(format!( + "shard fan-out: {} has no embedding_model identity; refusing to scan", + shard.name + )) + })?; + match scanned_model { + None => scanned_model = Some(model), + Some(want) if want != model => { + return Err(Error::fatal(format!( + "shard fan-out: mixed embedding models in the scanned set ({} has `{model}`, an earlier shard has `{want}`); refusing to merge across models", + shard.name + ))); + } + _ => {} + } + } let stmt = turso_vector_scan_stmt( set.vec_table(), q.filters, @@ -315,6 +339,7 @@ mod tests { topic: "main".into(), part: 1, expected_chunks: None, + embedding_model: None, chunks: 0, endpoint: ShardEndpoint { name: "atlas-core-secret-main-01".into(), diff --git a/pkgs/atlas-search/crates/atlas-core/tests/shards.rs b/pkgs/atlas-search/crates/atlas-core/tests/shards.rs index 712996010d..291b5d0d9e 100644 --- a/pkgs/atlas-search/crates/atlas-core/tests/shards.rs +++ b/pkgs/atlas-search/crates/atlas-core/tests/shards.rs @@ -130,6 +130,7 @@ fn write_shard(dir: &Path, name: &str, chunks: &[Chunk]) { ("part", &part.to_string()), ("expected_chunks", &chunks.len().to_string()), ("metric", "cosine"), + ("embedding_model", atlas_core::types::DEFAULT_MODEL), ("engine", "tursodb"), ], ); @@ -374,6 +375,108 @@ fn new_shard_joins_without_code_changes_and_moves_the_snapshot_id() { ); } +#[test] +fn manifest_round_trip_carries_embedding_model() { + use atlas_core::shards::Manifest; + use std::collections::BTreeMap; + let pairs: BTreeMap = [ + ("shard", "atlas-core-internal-misc-01"), + ("tier", "internal"), + ("topic", "misc"), + ("metric", "cosine"), + ("embedding_model", atlas_core::types::DEFAULT_MODEL), + ] + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(); + let m = Manifest::from_pairs(&pairs).unwrap(); + assert_eq!( + m.embedding_model.as_deref(), + Some(atlas_core::types::DEFAULT_MODEL) + ); + + let mut bare = pairs.clone(); + bare.remove("embedding_model"); + let m = Manifest::from_pairs(&bare).unwrap(); + assert_eq!(m.embedding_model, None); +} + +#[test] +fn divergent_embedding_model_aborts_unless_lenient() { + let c = corpus("model-divergent", 17); + let con = rusqlite::Connection::open(c.dir.join("atlas-core-internal-misc-01.db")).unwrap(); + con.execute( + "UPDATE atlas_manifest SET value = 'other/model-v9' WHERE \"key\" = 'embedding_model'", + [], + ) + .unwrap(); + let err = c + .discover(&LogConnector::new(vec![]), Tier::Internal) + .err() + .unwrap(); + let msg = err.to_string(); + assert!(msg.contains("atlas-core-internal-misc-01"), "{msg}"); + assert!(msg.contains("other/model-v9"), "{msg}"); + assert!(msg.contains(atlas_core::types::DEFAULT_MODEL), "{msg}"); + + let lenient = discover( + &DirLister { dir: c.dir.clone() }, + &LogConnector::new(vec![]), + &DiscoveryOptions { + lenient: true, + ..DiscoveryOptions::new(TierCeiling(Tier::Internal)) + }, + ) + .unwrap(); + assert!( + lenient + .shards() + .iter() + .all(|s| s.name != "atlas-core-internal-misc-01") + ); + assert!( + lenient + .rejections + .iter() + .any(|r| r.name == "atlas-core-internal-misc-01" && r.reason.contains("other/model-v9")) + ); +} + +#[test] +fn missing_embedding_model_aborts_unless_lenient() { + let c = corpus("model-missing", 19); + let con = rusqlite::Connection::open(c.dir.join("atlas-core-internal-misc-01.db")).unwrap(); + con.execute( + "DELETE FROM atlas_manifest WHERE \"key\" = 'embedding_model'", + [], + ) + .unwrap(); + let err = c + .discover(&LogConnector::new(vec![]), Tier::Internal) + .err() + .unwrap(); + assert!( + err.to_string().contains("no `embedding_model` key"), + "{err}" + ); + + let lenient = discover( + &DirLister { dir: c.dir.clone() }, + &LogConnector::new(vec![]), + &DiscoveryOptions { + lenient: true, + ..DiscoveryOptions::new(TierCeiling(Tier::Internal)) + }, + ) + .unwrap(); + assert!( + lenient + .shards() + .iter() + .all(|s| s.name != "atlas-core-internal-misc-01") + ); +} + #[test] fn integrity_failures_abort_unless_lenient() { let c = corpus("integrity", 13); diff --git a/pkgs/atlas-search/crates/atlas-search/tests/cli_parity.rs b/pkgs/atlas-search/crates/atlas-search/tests/cli_parity.rs index d3ef0936d9..1eb79c76a1 100644 --- a/pkgs/atlas-search/crates/atlas-search/tests/cli_parity.rs +++ b/pkgs/atlas-search/crates/atlas-search/tests/cli_parity.rs @@ -434,6 +434,10 @@ fn build_shard_dir(db: &Path, tag: &str) -> PathBuf { ("part", part.trim_start_matches('0').to_string()), ("expected_chunks", ids.len().to_string()), ("metric", "cosine".to_string()), + ( + "embedding_model", + "Snowflake/snowflake-arctic-embed-m-v2.0".to_string(), + ), ] { con.execute( "INSERT INTO atlas_manifest VALUES (?, ?)",