diff --git a/pkgs/atlas-search/crates/atlas-core/src/search.rs b/pkgs/atlas-search/crates/atlas-core/src/search.rs index b7d2ecf5f2..7037872ea7 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/search.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/search.rs @@ -821,6 +821,7 @@ mod trace_tests { chunk_id: 2, dist: Some(0.25), }], + candidate_models: Default::default(), }) } fn chunk_details(&self, ids: &[i64]) -> Result> { @@ -1010,6 +1011,7 @@ mod jaccard_tests { }, ], vector: vec![], + candidate_models: Default::default(), }) } fn chunk_details(&self, ids: &[i64]) -> Result> { 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 612069386e..c67d91051c 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/shards/scan.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/shards/scan.rs @@ -260,9 +260,9 @@ fn vector_pool( topics: Option<&[String]>, q: &BranchQuery, filters: &Filters, -) -> Result> { +) -> Result<(Vec, std::collections::HashMap)> { let Some(qvec) = q.query_vector.as_deref() else { - return Ok(Vec::new()); + return Ok((Vec::new(), std::collections::HashMap::new())); }; if q.vector_backend != VectorBackend::Scan { return Err(Error::fatal( @@ -281,14 +281,28 @@ fn vector_pool( ceiling: TierCeiling::from_sensitivity(filters.max_sensitivity), }, )?; - Ok(res + // M5 provenance (#7647): each hit's shard manifest + // `embedding_model`, keyed by chunk id for the query trace. + let model_by_shard: std::collections::HashMap<&str, &str> = set + .shards() + .iter() + .map(|s| (s.name.as_str(), s.embedding_model.as_str())) + .collect(); + let mut candidate_models = std::collections::HashMap::new(); + let hits = res .hits .into_iter() - .map(|h| BranchHit { - chunk_id: h.chunk_id, - dist: Some(h.dist), + .map(|h| { + if let Some(m) = model_by_shard.get(h.shard.as_str()) { + candidate_models.insert(h.chunk_id, m.to_string()); + } + BranchHit { + chunk_id: h.chunk_id, + dist: Some(h.dist), + } }) - .collect()) + .collect(); + Ok((hits, candidate_models)) } impl SearchBackend for ShardedBackend { @@ -384,9 +398,11 @@ impl SearchBackend for ShardedBackend { pools.fts = self.inner.branch_pools(&fts_query, filters)?.fts; } if let Some(h) = vec_handle { - pools.vector = h + let (hits, models) = h .join() .map_err(|_| Error::backend("shard vector branch panicked"))??; + pools.vector = hits; + pools.candidate_models = models; } Ok(pools) }) diff --git a/pkgs/atlas-search/crates/atlas-core/src/trace.rs b/pkgs/atlas-search/crates/atlas-core/src/trace.rs index 0818e8a407..1c4fb455c9 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/trace.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/trace.rs @@ -10,6 +10,19 @@ //! fail-closed (design invariant 2): a sink failure is a search //! failure, never a silently untraced success. //! +//! Embedding-model provenance (#7647, M5 of the #6060 one-model +//! decision): the record head carries `query_embedding_model` +//! (the model the query was embedded with) once, and every result +//! carries `embedding_model` copied from the manifest of the +//! shard that produced that candidate (`BranchPools:: +//! candidate_models`; null when the candidate's provenance is +//! unknown, per the identity note below). The distinct candidate +//! models are listed at the head as `embedding_models`; when two +//! or more distinct values appear, `embedding_model_breach` is +//! true — positive evidence of an M1 breach, surfaced as-is, +//! never silently normalised or split. Statistics stay computed +//! over the whole result set without per-model conditioning. +//! //! Identity note: the design resolves each chunk to //! `(source_file_id, embed_id, embed_content_hash)` via the chunk //! detail row and the versioning design's content version. That @@ -265,6 +278,7 @@ pub fn build_trace_record( .map(|(i, id)| (*id, i + 1)) .collect(); + let mut distinct_models: Vec = Vec::new(); let mut results = Vec::new(); for (i, e) in ranked.iter().enumerate() { let d = details.get(&e.chunk_id); @@ -322,6 +336,19 @@ pub fn build_trace_record( ); let mut r = Map::new(); r.insert("chunk_id".into(), json!(e.chunk_id)); + // Embedding-model provenance (#7647): the model recorded + // in the producing shard's atlas_manifest, or null when + // the candidate carries no manifest provenance. + let candidate_model = pools.candidate_models.get(&e.chunk_id); + if let Some(m) = candidate_model { + if !distinct_models.contains(m) { + distinct_models.push(m.clone()); + } + } + r.insert( + "embedding_model".into(), + candidate_model.map_or(Json::Null, |m| Json::String(m.clone())), + ); // Resolved embed identity: null until the versioning // design's detail columns land (see module note). r.insert("source_file_id".into(), Json::Null); @@ -393,6 +420,19 @@ pub fn build_trace_record( }, ); rec.insert("parameters".into(), Json::Object(parameters)); + // M5 head provenance (#7647): the query's embedding model + // once, the distinct candidate models, and the M1-breach flag + // (two or more distinct candidate models in one trace). + rec.insert( + "query_embedding_model".into(), + Json::String(opts.model.clone()), + ); + distinct_models.sort(); + rec.insert( + "embedding_model_breach".into(), + json!(distinct_models.len() > 1), + ); + rec.insert("embedding_models".into(), json!(distinct_models)); rec.insert("results".into(), Json::Array(results)); rec.insert( "pool_counts".into(), @@ -426,6 +466,103 @@ mod tests { assert_eq!(rfc3339_utc(1_763_000_000_000), "2025-11-13T02:13:20Z"); } + fn trace_fixture(candidate_models: std::collections::HashMap) -> Json { + let mut opts = SearchOptions::new("provenance query"); + opts.model = "model-query-v1".to_string(); + let pools = BranchPools { + fts: Vec::new(), + vector: vec![ + crate::types::BranchHit { + chunk_id: 1, + dist: Some(0.1), + }, + crate::types::BranchHit { + chunk_id: 2, + dist: Some(0.2), + }, + ], + candidate_models, + }; + let ranked = vec![ + crate::fusion::FusedEntry { + chunk_id: 1, + score: 0.5, + fts_rank: None, + vector_rank: Some(1), + }, + crate::fusion::FusedEntry { + chunk_id: 2, + score: 0.4, + fts_rank: None, + vector_rank: Some(2), + }, + ]; + let ctx = TraceContext::new("cli", "agent", "test"); + build_trace_record( + "qid-test", + &ctx, + 0, + 1, + &opts, + &Filters::default(), + "turso", + &pools, + &ranked, + &HashMap::new(), + &HashMap::new(), + &[1, 2], + ) + } + + #[test] + fn trace_records_uniform_embedding_model_provenance() { + // Oracle (#7647 acceptance 1): a uniform-model trace shows + // query_embedding_model at the head and the matching + // embedding_model on every candidate, with no breach. + let mut models = std::collections::HashMap::new(); + models.insert(1, "model-a".to_string()); + models.insert(2, "model-a".to_string()); + let rec = trace_fixture(models); + assert_eq!(rec["query_embedding_model"], json!("model-query-v1")); + assert_eq!(rec["embedding_model_breach"], json!(false)); + assert_eq!(rec["embedding_models"], json!(["model-a"])); + let results = rec["results"].as_array().expect("results array"); + assert_eq!(results.len(), 2); + for r in results { + assert_eq!(r["embedding_model"], json!("model-a")); + } + } + + #[test] + fn trace_flags_mixed_embedding_models_as_m1_breach() { + // Oracle (#7647 acceptance 2): a mixed-model fixture + // exposes both model ids and flags the breach; the values + // are never normalised or split. + let mut models = std::collections::HashMap::new(); + models.insert(1, "model-a".to_string()); + models.insert(2, "model-b".to_string()); + let rec = trace_fixture(models); + assert_eq!(rec["embedding_model_breach"], json!(true)); + assert_eq!(rec["embedding_models"], json!(["model-a", "model-b"])); + let results = rec["results"].as_array().expect("results array"); + assert_eq!(results[0]["embedding_model"], json!("model-a")); + assert_eq!(results[1]["embedding_model"], json!("model-b")); + } + + #[test] + fn trace_without_manifest_provenance_records_null_models() { + // Oracle (#7647): candidates with no shard-manifest + // provenance record null, and a single distinct (absent) + // model set is not a breach. + let rec = trace_fixture(std::collections::HashMap::new()); + assert_eq!(rec["embedding_model_breach"], json!(false)); + assert_eq!(rec["embedding_models"], json!([])); + let results = rec["results"].as_array().expect("results array"); + for r in results { + assert_eq!(r["embedding_model"], Json::Null); + } + } + #[test] fn hash_normalises_whitespace() { assert_eq!( diff --git a/pkgs/atlas-search/crates/atlas-core/src/types.rs b/pkgs/atlas-search/crates/atlas-core/src/types.rs index 0936270064..bde6e748d7 100644 --- a/pkgs/atlas-search/crates/atlas-core/src/types.rs +++ b/pkgs/atlas-search/crates/atlas-core/src/types.rs @@ -109,6 +109,14 @@ pub struct BranchHit { pub struct BranchPools { pub fts: Vec, pub vector: Vec, + /// Per-candidate embedding-model provenance (#7647, M5 of the + /// #6060 decision): chunk id -> the `embedding_model` recorded + /// in the `atlas_manifest` of the shard that produced the + /// candidate, copied at fan-out time. Empty for monolith-served + /// pools: those candidates carry no shard-manifest provenance + /// and trace as null (the trace module's null-until-known + /// convention), never an invented value. + pub candidate_models: std::collections::HashMap, } /// The branch inputs, mirroring `run_search`'s per-mode state. diff --git a/pkgs/atlas-search/crates/atlas-core/tests/flags.rs b/pkgs/atlas-search/crates/atlas-core/tests/flags.rs index d30fb88cc1..2dd17cac92 100644 --- a/pkgs/atlas-search/crates/atlas-core/tests/flags.rs +++ b/pkgs/atlas-search/crates/atlas-core/tests/flags.rs @@ -37,6 +37,7 @@ impl SearchBackend for FakeBackend { }) .collect(), vector: Vec::new(), + candidate_models: Default::default(), }) } fn chunk_details(&self, ids: &[i64]) -> Result> {