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
44 changes: 43 additions & 1 deletion pkgs/atlas-search/crates/atlas-core/src/shards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ impl ShardConnector for StdConnector {

// ----------------------------------------------------------- manifest ---

/// The embedding model + pipeline identity that produced every
/// shard's vectors (#6060 decision record, contract points M1/M2,
/// #7646): one embedding model for every Atlas tier. Recorded per
/// shard in the manifest's `embedding_model` key and checked before
/// fan-out, so vectors embedded by different models are never merged
/// as if they were comparable.
pub const EMBEDDING_MODEL_ID: &str = "snowflake-arctic-embed-m-v2.0";

/// A shard's `atlas_manifest` (key/value self-description).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Manifest {
Expand All @@ -394,6 +402,11 @@ pub struct Manifest {
pub part: Option<u32>,
pub expected_chunks: Option<u64>,
pub metric: String,
/// The `embedding_model` key: model + pipeline identity of the
/// shard's vectors. `None` when the key is absent (a shard
/// written before the key existed); discovery refuses `None`
/// in strict mode (#7646).
pub embedding_model: Option<String>,
}

impl Manifest {
Expand All @@ -414,6 +427,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 +458,8 @@ pub struct ShardInfo {
pub topic: String,
pub part: u32,
pub expected_chunks: Option<u64>,
/// The shard's validated `embedding_model` identity (#7646).
pub embedding_model: String,
/// `count(*)` of `chunks` at discovery time.
pub chunks: u64,
pub endpoint: ShardEndpoint,
Expand Down Expand Up @@ -540,6 +556,10 @@ pub struct DiscoveryOptions {
/// The service's maximum tier. Shards above it are never
/// connected.
pub ceiling: TierCeiling,
/// The embedding-model identity this consumer embeds queries
/// with; shards recording a different (or no) identity are
/// refused at probe (#7646). Defaults to the mandated model.
pub embedding_model: String,
pub vec_table: String,
/// Integrity failures (unreadable manifest, manifest disagreeing
/// with the shard name, wrong metric) normally abort discovery
Expand All @@ -552,6 +572,7 @@ impl DiscoveryOptions {
pub fn new(ceiling: TierCeiling) -> DiscoveryOptions {
DiscoveryOptions {
ceiling,
embedding_model: EMBEDDING_MODEL_ID.to_string(),
vec_table: crate::types::DEFAULT_VEC_TABLE.to_string(),
lenient: false,
}
Expand All @@ -568,6 +589,7 @@ struct Candidate {
fn probe(
connector: &dyn ShardConnector,
c: &Candidate,
expected_embedding_model: &str,
) -> Result<(ShardInfo, Arc<dyn ShardTransport>)> {
let t = connector.connect(&c.endpoint, c.tier)?;
let m = Manifest::read(t.as_ref())
Expand All @@ -593,6 +615,25 @@ fn probe(
c.endpoint.name, m.metric
)));
}
// M1/M2 (#7646): the shard's vectors must have been embedded by
// the same model + pipeline as this consumer's query vectors.
// A missing key is a refusal too — provenance unknown is not
// provenance shared. Errors name the shard and both identities.
match &m.embedding_model {
None => {
return Err(Error::fatal(format!(
"shard {}: atlas_manifest has no `embedding_model` key; this consumer embeds with `{expected_embedding_model}` and a shard of unknown embedding provenance must not be scanned",
Comment on lines +622 to +625

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Stamp the CLI shard fixtures with the required model identity

The CLI parity fixture producer, build_shard_dir in pkgs/atlas-search/crates/atlas-search/tests/cli_parity.rs:430–436, still writes manifests without embedding_model. Consequently, this new mandatory check aborts discovery in both cli_shard_fanout_matches_python_vector_results and cli_shard_fanout_default_never_touches_secret_shard, which assert successful CLI execution. These tests are included in the atlas-search test step in .github/workflows/atlas-search-pr.yml. Update that fixture producer alongside the atlas-core fixtures so the existing CLI tests remain compatible with the new manifest contract.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed before merge — thanks. Commit 81946d882 ("test(atlas-search): stamp embedding_model in cli_parity shard fixtures (#7646 backfill)") stamps ("embedding_model", "snowflake-arctic-embed-m-v2.0") in build_shard_dir, mirroring the atlas-core fixture backfill. Verified on the laptop after that commit: cargo test -p atlas-search — 31 lib + 11 cli_parity tests, 0 failures, including both cli_shard_fanout_matches_python_vector_results and cli_shard_fanout_default_never_touches_secret_shard (they failed exactly as you describe before the stamp, which also end-to-end proved the new refusal fires through the real CLI). PR merged as a9a3748eeb.

c.endpoint.name
)));
}
Some(got) if got != expected_embedding_model => {
return Err(Error::fatal(format!(
"shard {}: embedding_model `{got}` does not match this consumer's `{expected_embedding_model}`; vectors from different embedding models are not comparable",
c.endpoint.name
)));
}
_ => {}
}
let n = t
.pipeline(&[Stmt::new("SELECT COUNT(*) AS n FROM chunks", vec![])])?
.into_iter()
Expand All @@ -609,6 +650,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().unwrap_or_default(),
chunks: n.max(0) as u64,
endpoint: c.endpoint.clone(),
},
Expand Down Expand Up @@ -683,7 +725,7 @@ pub fn discover(
let probed: Vec<Result<(ShardInfo, Arc<dyn ShardTransport>)>> = std::thread::scope(|scope| {
let handles: Vec<_> = candidates
.iter()
.map(|c| scope.spawn(move || probe(connector, c)))
.map(|c| scope.spawn(move || probe(connector, c, &opts.embedding_model)))
.collect();
handles
.into_iter()
Expand Down
15 changes: 15 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,20 @@ pub fn fanout_scan(set: &ShardSet, q: &FanoutQuery<'_>) -> Result<FanoutResult>
));
}
let shards = set.select(q.ceiling, q.topics)?;
// M1 (#7646): never merge across embedding models. Discovery
// already refused shards that disagree with the consumer, but
// the scanned set itself is checked here so a set assembled
// from more than one discovery still cannot mix identities.
if let Some(first) = shards.first() {
for s in &shards[1..] {
if s.embedding_model != first.embedding_model {
return Err(Error::fatal(format!(
"shard fan-out: scanned set mixes embedding models — shard {} records `{}` but shard {} records `{}`; vectors from different embedding models are not comparable, refusing to merge",
first.name, first.embedding_model, s.name, s.embedding_model
)));
}
}
}
let stmt = turso_vector_scan_stmt(
set.vec_table(),
q.filters,
Expand Down Expand Up @@ -361,6 +375,7 @@ mod tests {
topic: "main".into(),
part: 1,
expected_chunks: None,
embedding_model: crate::shards::EMBEDDING_MODEL_ID.to_string(),
chunks: 0,
endpoint: ShardEndpoint {
name: "atlas-core-secret-main-01".into(),
Expand Down
74 changes: 73 additions & 1 deletion 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", "snowflake-arctic-embed-m-v2.0"),
("engine", "tursodb"),
],
);
Expand Down Expand Up @@ -431,6 +432,72 @@ fn integrity_failures_abort_unless_lenient() {
assert!(err.to_string().contains("not cosine"), "{err}");
}

#[test]
fn manifest_round_trip_carries_embedding_model() {
// Oracle (#7646): the manifest key round-trips — a pairs map with
// the key parses into the field, and one without it parses as
// None (refusal is discovery's job, not the parser's).
let mut pairs = std::collections::BTreeMap::new();
for (k, v) in [
("shard", "atlas-core-internal-misc-01"),
("tier", "internal"),
("topic", "misc"),
("metric", "cosine"),
("embedding_model", "snowflake-arctic-embed-m-v2.0"),
] {
pairs.insert(k.to_string(), v.to_string());
}
let m = atlas_core::shards::Manifest::from_pairs(&pairs).unwrap();
assert_eq!(
m.embedding_model.as_deref(),
Some(atlas_core::shards::EMBEDDING_MODEL_ID)
);
pairs.remove("embedding_model");
let m = atlas_core::shards::Manifest::from_pairs(&pairs).unwrap();
assert_eq!(m.embedding_model, None);
}

#[test]
fn discovery_refuses_missing_and_mismatched_embedding_model() {
// Oracle (#7646, M1/M2): a shard whose atlas_manifest lacks the
// embedding_model key, or records a different model, is refused
// at discovery with an error naming the shard and both model
// identities; the uniform corpus (fixture-stamped with the
// mandated id) discovers cleanly — see the other tests.
let c = corpus("embedmodel", 31);

// Missing key.
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();
drop(con);
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("embedding_model"), "{msg}");
assert!(msg.contains("snowflake-arctic-embed-m-v2.0"), "{msg}");

// Mismatched key.
let con = rusqlite::Connection::open(c.dir.join("atlas-core-internal-misc-01.db")).unwrap();
con.execute(
"INSERT OR REPLACE INTO atlas_manifest VALUES ('embedding_model', 'some-other-model-v9')",
[],
)
.unwrap();
drop(con);
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("some-other-model-v9"), "{msg}");
assert!(msg.contains("snowflake-arctic-embed-m-v2.0"), "{msg}");
}

#[test]
fn fanout_equals_monolith_oracle_over_random_queries() {
let c = corpus("oracle", 21);
Expand Down Expand Up @@ -892,7 +959,8 @@ fn sharded_backend_routes_filtered_queries_to_the_monolith() {
be.shard_set(),
&FanoutQuery {
qvec: &q,
k: 10,
shard_top_k: 10,
global_top_k: 10,
filters: &f,
topics: None,
ceiling: TierCeiling(Tier::Internal),
Expand All @@ -908,6 +976,8 @@ fn sharded_backend_routes_filtered_queries_to_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 @@ -941,6 +1011,8 @@ fn sharded_backend_routes_filtered_queries_to_the_monolith() {
match_expr: None,
query_vector: Some(q),
pool: 10,
shard_top_k: 10,
global_top_k: 10,
vector_backend: VectorBackend::Scan,
},
&f,
Expand Down
1 change: 1 addition & 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,7 @@ 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-arctic-embed-m-v2.0".to_string()),
] {
con.execute(
"INSERT INTO atlas_manifest VALUES (?, ?)",
Expand Down
Loading