Skip to content
Closed
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
35 changes: 35 additions & 0 deletions pkgs/atlas-search/crates/atlas-core/src/shards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ pub struct Manifest {
pub part: Option<u32>,
pub expected_chunks: Option<u64>,
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<String>,
}

impl Manifest {
Expand All @@ -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(),
})
}

Expand Down Expand Up @@ -444,6 +451,10 @@ pub struct ShardInfo {
pub topic: String,
pub part: u32,
pub expected_chunks: Option<u64>,
/// 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<String>,
/// `count(*)` of `chunks` at discovery time.
pub chunks: u64,
pub endpoint: ShardEndpoint,
Expand Down Expand Up @@ -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()
Expand All @@ -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(),
},
Expand Down
25 changes: 25 additions & 0 deletions pkgs/atlas-search/crates/atlas-core/src/shards/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ pub fn fanout_scan(set: &ShardSet, q: &FanoutQuery<'_>) -> Result<FanoutResult>
));
}
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,
Expand Down Expand Up @@ -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(),
Expand Down
103 changes: 103 additions & 0 deletions pkgs/atlas-search/crates/atlas-core/tests/shards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
],
);
Expand Down Expand Up @@ -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<String, String> = [
("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);
Expand Down
4 changes: 4 additions & 0 deletions pkgs/atlas-search/crates/atlas-search/tests/cli_parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (?, ?)",
Expand Down
Loading