Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(tasks): add rerank-score floor to similarity search and exclude closed tasks from dedup (#4470)
Eric Hayes0d556409fe5e
diff --git a/rust/cloud-storage/.sqlx/query-ae1b88cef7fd6cc6ef1c302a0f3a04d156d39f39abea24050e63caf56267898a.json b/rust/cloud-storage/.sqlx/query-22e9d18b3a97f5cacbb79562837bb6e5793ea6e38a02428571e2add8296e6a9c.jsonsimilarity index 63%rename from rust/cloud-storage/.sqlx/query-ae1b88cef7fd6cc6ef1c302a0f3a04d156d39f39abea24050e63caf56267898a.jsonrename to rust/cloud-storage/.sqlx/query-22e9d18b3a97f5cacbb79562837bb6e5793ea6e38a02428571e2add8296e6a9c.jsonindex e4b3c2a5e5..2a092522c9 100644--- a/rust/cloud-storage/.sqlx/query-ae1b88cef7fd6cc6ef1c302a0f3a04d156d39f39abea24050e63caf56267898a.json+++ b/rust/cloud-storage/.sqlx/query-22e9d18b3a97f5cacbb79562837bb6e5793ea6e38a02428571e2add8296e6a9c.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n WITH query AS (\n SELECT key, vec::vector AS vec\n FROM unnest($1::text[], $2::text[]) AS t(key, vec)\n ),\n scored AS (\n SELECT\n e.document_id,\n e.search_key,\n e.content,\n e.embedding::text AS embedding_text,\n MAX(1 - (e.embedding <=> q.vec))::real AS score\n FROM task_duplicate_embedding e\n JOIN \"Document\" d ON d.id = e.document_id\n JOIN document_sub_type dst ON dst.document_id = d.id AND dst.sub_type = 'task'\n LEFT JOIN team_task tt ON tt.document_id = d.id\n CROSS JOIN query q\n WHERE d.\"deletedAt\" IS NULL\n AND (\n d.owner = $3\n OR ($4::uuid IS NOT NULL AND tt.team_id = $4)\n )\n AND ($5::text IS NULL OR e.document_id <> $5)\n AND (\n NOT $6\n OR NOT EXISTS (\n SELECT 1\n FROM task_duplicate_match m\n WHERE m.task_id = LEAST($5, e.document_id)\n AND m.duplicate_task_id = GREATEST($5, e.document_id)\n AND m.status = 'dismissed'\n )\n )\n GROUP BY e.document_id, e.search_key, e.content, e.embedding\n ),\n ranked AS (\n SELECT document_id, MAX(score) AS best\n FROM scored\n GROUP BY document_id\n ORDER BY best DESC\n LIMIT $7\n )\n SELECT\n s.document_id AS \"document_id!\",\n s.search_key AS \"search_key!\",\n s.content AS \"content!\",\n s.embedding_text AS \"embedding_text!\",\n s.score AS \"score!\"\n FROM scored s\n JOIN ranked r ON r.document_id = s.document_id\n ORDER BY r.best DESC, s.document_id, s.score DESC\n ",+ "query": "\n WITH query AS (\n SELECT key, vec::vector AS vec\n FROM unnest($1::text[], $2::text[]) AS t(key, vec)\n ),\n scored AS (\n SELECT\n e.document_id,\n e.search_key,\n e.content,\n e.embedding::text AS embedding_text,\n MAX(1 - (e.embedding <=> q.vec))::real AS score\n FROM task_duplicate_embedding e\n JOIN \"Document\" d ON d.id = e.document_id\n JOIN document_sub_type dst ON dst.document_id = d.id AND dst.sub_type = 'task'\n LEFT JOIN team_task tt ON tt.document_id = d.id\n CROSS JOIN query q\n WHERE d.\"deletedAt\" IS NULL\n AND (\n d.owner = $3\n OR ($4::uuid IS NOT NULL AND tt.team_id = $4)\n )\n AND ($5::text IS NULL OR e.document_id <> $5)\n AND (\n NOT $6\n OR NOT EXISTS (\n SELECT 1\n FROM task_duplicate_match m\n WHERE m.task_id = LEAST($5, e.document_id)\n AND m.duplicate_task_id = GREATEST($5, e.document_id)\n AND m.status = 'dismissed'\n )\n )\n AND NOT EXISTS (\n SELECT 1\n FROM entity_properties ep\n WHERE ep.entity_id = e.document_id\n AND ep.entity_type = 'TASK'\n AND ep.property_definition_id = $8\n AND ep.values->'value' ?| $9::text[]\n )\n GROUP BY e.document_id, e.search_key, e.content, e.embedding\n ),\n ranked AS (\n SELECT document_id, MAX(score) AS best\n FROM scored\n GROUP BY document_id\n ORDER BY best DESC\n LIMIT $7\n )\n SELECT\n s.document_id AS \"document_id!\",\n s.search_key AS \"search_key!\",\n s.content AS \"content!\",\n s.embedding_text AS \"embedding_text!\",\n s.score AS \"score!\"\n FROM scored s\n JOIN ranked r ON r.document_id = s.document_id\n ORDER BY r.best DESC, s.document_id, s.score DESC\n ","describe": {"columns": [{@@ -37,7 +37,9 @@"Uuid","Text","Bool",- "Int8"+ "Int8",+ "Uuid",+ "TextArray"]},"nullable": [@@ -48,5 +50,5 @@null]},- "hash": "ae1b88cef7fd6cc6ef1c302a0f3a04d156d39f39abea24050e63caf56267898a"+ "hash": "22e9d18b3a97f5cacbb79562837bb6e5793ea6e38a02428571e2add8296e6a9c"}diff --git a/rust/cloud-storage/.sqlx/query-da11b9c3b9850e3a763035f7873921f7887f3bf828b5be3c7a599fade54a0dc1.json b/rust/cloud-storage/.sqlx/query-e02aed5de18227559d2e0281cd08eca41eb05aa92a250e63823e135bee988bd5.jsonsimilarity index 68%rename from rust/cloud-storage/.sqlx/query-da11b9c3b9850e3a763035f7873921f7887f3bf828b5be3c7a599fade54a0dc1.jsonrename to rust/cloud-storage/.sqlx/query-e02aed5de18227559d2e0281cd08eca41eb05aa92a250e63823e135bee988bd5.jsonindex 45d7afe7de..9744bec453 100644--- a/rust/cloud-storage/.sqlx/query-da11b9c3b9850e3a763035f7873921f7887f3bf828b5be3c7a599fade54a0dc1.json+++ b/rust/cloud-storage/.sqlx/query-e02aed5de18227559d2e0281cd08eca41eb05aa92a250e63823e135bee988bd5.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT\n m.id,\n CASE WHEN m.task_id = $1 THEN m.duplicate_task_id ELSE m.task_id END AS \"other_task_id!\",\n d.name AS \"other_task_name!\",\n m.vector_score AS \"vector_score!\",\n m.judge_reason\n FROM task_duplicate_match m\n JOIN \"Document\" d\n ON d.id = CASE WHEN m.task_id = $1 THEN m.duplicate_task_id ELSE m.task_id END\n WHERE m.status = 'active'\n AND (m.task_id = $1 OR m.duplicate_task_id = $1)\n AND d.\"deletedAt\" IS NULL\n ORDER BY m.vector_score DESC, m.created_at DESC\n LIMIT 10\n ",+ "query": "\n SELECT\n m.id,\n CASE WHEN m.task_id = $1 THEN m.duplicate_task_id ELSE m.task_id END AS \"other_task_id!\",\n d.name AS \"other_task_name!\",\n m.vector_score AS \"vector_score!\",\n m.judge_reason\n FROM task_duplicate_match m\n JOIN \"Document\" d\n ON d.id = CASE WHEN m.task_id = $1 THEN m.duplicate_task_id ELSE m.task_id END\n WHERE m.status = 'active'\n AND (m.task_id = $1 OR m.duplicate_task_id = $1)\n AND d.\"deletedAt\" IS NULL\n AND NOT EXISTS (\n SELECT 1\n FROM entity_properties ep\n WHERE ep.entity_id = d.id\n AND ep.entity_type = 'TASK'\n AND ep.property_definition_id = $2\n AND ep.values->'value' ?| $3::text[]\n )\n ORDER BY m.vector_score DESC, m.created_at DESC\n LIMIT 10\n ","describe": {"columns": [{@@ -31,7 +31,9 @@],"parameters": {"Left": [- "Text"+ "Text",+ "Uuid",+ "TextArray"]},"nullable": [@@ -42,5 +44,5 @@true]},- "hash": "da11b9c3b9850e3a763035f7873921f7887f3bf828b5be3c7a599fade54a0dc1"+ "hash": "e02aed5de18227559d2e0281cd08eca41eb05aa92a250e63823e135bee988bd5"}diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lockindex a5e38b60ea..5155095f17 100644--- a/rust/cloud-storage/Cargo.lock+++ b/rust/cloud-storage/Cargo.lock@@ -12954,6 +12954,7 @@ dependencies = ["serde","serde_json","sqlx",+ "system_properties","thiserror 2.0.18","tokio","tracing",diff --git a/rust/cloud-storage/task_dedup/Cargo.toml b/rust/cloud-storage/task_dedup/Cargo.tomlindex 69ccee71b7..82fac6f8b3 100644--- a/rust/cloud-storage/task_dedup/Cargo.toml+++ b/rust/cloud-storage/task_dedup/Cargo.toml@@ -50,6 +50,7 @@ reqwest = { workspace = true }serde = { workspace = true }serde_json = { workspace = true }sqlx = { workspace = true }+system_properties = { path = "../system_properties" }thiserror = { workspace = true }tracing = { workspace = true }utoipa = { workspace = true }diff --git a/rust/cloud-storage/task_dedup/src/domain/service.rs b/rust/cloud-storage/task_dedup/src/domain/service.rsindex eee02bcfa3..16377dda3f 100644--- a/rust/cloud-storage/task_dedup/src/domain/service.rs+++ b/rust/cloud-storage/task_dedup/src/domain/service.rs@@ -29,6 +29,14 @@ pub struct TaskDedupConfig {pub duplicate_limit: i64,/// Minimum vector similarity for a candidate to be considered.pub min_vector_similarity: f64,+ /// Minimum rerank score (a Cohere relevance score in production) for a+ /// result to be returned from the draft similarity search. That path has no+ /// LLM judge to weed out weak candidates, so the reranker's score is the+ /// only relevance gate; it is tuned low (from the+ /// `eval_similarity_rerank_floor` sweep, which scores with the production+ /// Cohere reranker) so recall on the labeled corpus stays at 100%.+ /// Detection is unaffected — its candidates are gated by the judge instead.+ pub min_rerank_score: f64,/// Maximum candidates sent to the LLM judge per new task, in vector-score/// order. Bounds judge calls now that the cheap lexical pre-filter is gone.pub max_judge_candidates: usize,@@ -45,6 +53,7 @@ impl Default for TaskDedupConfig {vector_candidate_limit: 24,duplicate_limit: 10,min_vector_similarity: 0.35,+ min_rerank_score: 0.05,max_judge_candidates: 10,judge_concurrency: 4,}@@ -244,11 +253,16 @@ where.await.map_err(|error| TaskDedupError::Dependency(error.into()))?;+ // No judge runs on this path, so the rerank score is the only relevance+ // gate; results below the floor are noise the composer shouldn't show.+ // Reranked results are ordered by score, so the floor trims a suffix.let query_content = full_text(title, markdown);let ranked = self.rerank(&query_content, results).await?.into_iter()+ .filter(|(_, rerank_score)| f64::from(*rerank_score) >= self.config.min_rerank_score)+ .map(|(candidate, _)| candidate).take(self.config.duplicate_limit.max(0) as usize).collect::<Vec<_>>();@@ -316,6 +330,7 @@ where.rerank(&query_content, results).await?.into_iter()+ .map(|(candidate, _)| candidate).take(self.config.max_judge_candidates).collect();@@ -412,15 +427,16 @@ where}/// Drops results below the similarity floor, reranks the survivors against- /// `query`, and returns them as [`Candidate`]s ordered by descending- /// relevance. The reranker only carries each result's `document_id` through,- /// so the collapsed content and vector score are looked back up afterwards.- /// An empty survivor set skips the reranker entirely.+ /// `query`, and returns them as [`Candidate`]s (paired with their rerank+ /// score) ordered by descending relevance. The reranker only carries each+ /// result's `document_id` through, so the collapsed content and vector score+ /// are looked back up afterwards. An empty survivor set skips the reranker+ /// entirely.async fn rerank(&self,query: &str,results: Vec<SearchResults<String, DIMS>>,- ) -> Result<Vec<Candidate>, TaskDedupError> {+ ) -> Result<Vec<(Candidate, f32)>, TaskDedupError> {let mut lookup: HashMap<String, Candidate> = HashMap::new();let mut survivors: Vec<SearchResults<String, DIMS>> = Vec::new();for result in results {@@ -443,7 +459,11 @@ whereOk(reranked.into_iter()- .filter_map(|scored| lookup.remove(&scored.item))+ .filter_map(|scored| {+ lookup+ .remove(&scored.item)+ .map(|candidate| (candidate, scored.score))+ }).collect())}diff --git a/rust/cloud-storage/task_dedup/src/domain/service/test.rs b/rust/cloud-storage/task_dedup/src/domain/service/test.rsindex 31a1a01997..8c58c28c8a 100644--- a/rust/cloud-storage/task_dedup/src/domain/service/test.rs+++ b/rust/cloud-storage/task_dedup/src/domain/service/test.rs@@ -469,6 +469,17 @@ fn below_floor() -> f32 {TaskDedupConfig::default().min_vector_similarity as f32 - 0.05}+/// A rerank score just above the production rerank floor, so floor tests keep+/// passing when the floor is retuned.+fn above_rerank_floor() -> f64 {+ TaskDedupConfig::default().min_rerank_score + 0.05+}++/// A rerank score just below the production rerank floor.+fn below_rerank_floor() -> f64 {+ TaskDedupConfig::default().min_rerank_score - 0.05+}+fn judge(duplicates: &[&str]) -> Arc<MockJudge> {Arc::new(MockJudge {duplicates: duplicates.iter().map(|s| s.to_string()).collect(),@@ -759,7 +770,10 @@ async fn similarity_search_filters_ranks_and_persists_nothing() {("Y", "cy", below_floor()),("Z", "cz", above_floor()),]),- MockReranker::new(&[]),+ MockReranker::new(&[+ ("cx", above_rerank_floor() + 0.01),+ ("cz", above_rerank_floor()),+ ]),judge(&[]),Arc::new(MockNotifier::default()),Arc::new(MockMatchRepo::default()),@@ -810,7 +824,7 @@ async fn similarity_search_orders_by_rerank_score() {let h = build(TaskDedupConfig::default(),MockVectorDb::with_results(vec![("X", "cx", 0.9), ("Z", "cz", 0.8)]),- MockReranker::new(&[("cx", 0.1), ("cz", 0.9)]),+ MockReranker::new(&[("cx", above_rerank_floor()), ("cz", 0.9)]),judge(&[]),Arc::new(MockNotifier::default()),Arc::new(MockMatchRepo::default()),@@ -831,6 +845,52 @@ async fn similarity_search_orders_by_rerank_score() {assert_eq!(ids, vec!["Z", "X"]);}+#[tokio::test]+async fn similarity_search_drops_results_below_rerank_floor() {+ let h = build(+ TaskDedupConfig::default(),+ // Both clear the vector floor; only X clears the rerank floor.+ MockVectorDb::with_results(vec![("X", "cx", 0.9), ("Y", "cy", 0.9)]),+ MockReranker::new(&[("cx", above_rerank_floor()), ("cy", below_rerank_floor())]),+ judge(&[]),+ Arc::new(MockNotifier::default()),+ Arc::new(MockMatchRepo::default()),+ );++ let results = h+ .service+ .similarity_search(+ "owner",+ None,+ "title",+ &EmbeddingMarkdown::from_client_trusted("body".to_string()),+ )+ .await+ .unwrap();++ let ids: Vec<&str> = results.iter().map(|r| r.task_id.as_str()).collect();+ assert_eq!(ids, vec!["X"]);+}++#[tokio::test]+async fn detect_judges_candidates_below_rerank_floor() {+ // The rerank floor gates only the draft similarity search; detection keeps+ // sending below-floor candidates to the judge, which is its relevance gate.+ let h = build(+ TaskDedupConfig::default(),+ MockVectorDb::with_results(vec![("A", "cand-a", 0.9)]),+ MockReranker::new(&[("cand-a", below_rerank_floor())]),+ judge(&["cand-a"]),+ Arc::new(MockNotifier::default()),+ Arc::new(MockMatchRepo::default()),+ );++ h.service.detect_new_task(new_task("NEW")).await.unwrap();++ assert_eq!(judged_right_sides(&h.judge), vec!["cand-a".to_string()]);+ assert_eq!(h.matches.upserted_matches.lock().unwrap().len(), 1);+}+#[tokio::test]async fn similarity_search_truncates_to_duplicate_limit() {let config = TaskDedupConfig {@@ -844,7 +904,11 @@ async fn similarity_search_truncates_to_duplicate_limit() {("B", "cb", 0.90),("C", "cc", 0.85),]),- MockReranker::new(&[]),+ MockReranker::new(&[+ ("ca", above_rerank_floor() + 0.02),+ ("cb", above_rerank_floor() + 0.01),+ ("cc", above_rerank_floor()),+ ]),judge(&[]),Arc::new(MockNotifier::default()),Arc::new(MockMatchRepo::default()),diff --git a/rust/cloud-storage/task_dedup/src/outbound/postgres.rs b/rust/cloud-storage/task_dedup/src/outbound/postgres.rsindex 6429d18445..f9579e6c6c 100644--- a/rust/cloud-storage/task_dedup/src/outbound/postgres.rs+++ b/rust/cloud-storage/task_dedup/src/outbound/postgres.rs@@ -17,10 +17,22 @@ use embedding::{Content, KeyedEmbedding, LabeledEmbedding, Match, SearchResults,use sqlx::PgPool;use uuid::Uuid;+use system_properties::{StatusOption, SystemPropertyKey};+use crate::domain::models::{TaskDedupError, TaskDuplicate, TaskSearchParameters};use crate::domain::ports::TaskMatchRepo;use crate::domain::service::ordered_pair;+/// Status option ids for tasks excluded from every dedup surface: a finished or+/// cancelled task is not an actionable duplicate, so it is neither retrieved as+/// a similarity candidate nor shown in the duplicates list.+fn closed_status_option_ids() -> Vec<String> {+ vec![+ StatusOption::COMPLETED_UUID.to_string(),+ StatusOption::CANCELED_UUID.to_string(),+ ]+}+/// Postgres/pgvector implementation of [`VectorDb`] for task embeddings.////// Each task contributes one row per embeddable field (`title`, `body`); the@@ -108,8 +120,9 @@ impl VectorStore<DIMS> for PgTaskVectorDb {// field score and capped at `limit`; all of a kept entity's field rows// are returned so the service can reconstruct its text.//- // Iterative index scan keeps recall high despite the owner/team,- // self-, and dismissed-pair filters dropping rows after the HNSW scan.+ // Iterative index scan keeps recall high despite the owner/team, self-,+ // dismissed-pair, and closed-status filters dropping rows after the+ // HNSW scan.// SET LOCAL binds to the transaction's connection, so the search must run// in the same transaction to see it.let mut tx = self.pool.begin().await?;@@ -150,6 +163,14 @@ impl VectorStore<DIMS> for PgTaskVectorDb {AND m.status = 'dismissed'))+ AND NOT EXISTS (+ SELECT 1+ FROM entity_properties ep+ WHERE ep.entity_id = e.document_id+ AND ep.entity_type = 'TASK'+ AND ep.property_definition_id = $8+ AND ep.values->'value' ?| $9::text[]+ )GROUP BY e.document_id, e.search_key, e.content, e.embedding),ranked AS (@@ -176,6 +197,8 @@ impl VectorStore<DIMS> for PgTaskVectorDb {params.exclude_document_id,params.exclude_dismissed,params.limit,+ SystemPropertyKey::STATUS_UUID,+ &closed_status_option_ids(),).fetch_all(&mut *tx).await?;@@ -354,10 +377,20 @@ impl TaskMatchRepo for PgTaskMatchRepo {WHERE m.status = 'active'AND (m.task_id = $1 OR m.duplicate_task_id = $1)AND d."deletedAt" IS NULL+ AND NOT EXISTS (+ SELECT 1+ FROM entity_properties ep+ WHERE ep.entity_id = d.id+ AND ep.entity_type = 'TASK'+ AND ep.property_definition_id = $2+ AND ep.values->'value' ?| $3::text[]+ )ORDER BY m.vector_score DESC, m.created_at DESCLIMIT 10"#,document_id,+ SystemPropertyKey::STATUS_UUID,+ &closed_status_option_ids(),).fetch_all(&self.pool).await?;diff --git a/rust/cloud-storage/task_dedup/src/outbound/postgres/test.rs b/rust/cloud-storage/task_dedup/src/outbound/postgres/test.rsindex 113b815637..92bb7a0ff8 100644--- a/rust/cloud-storage/task_dedup/src/outbound/postgres/test.rs+++ b/rust/cloud-storage/task_dedup/src/outbound/postgres/test.rs@@ -187,6 +187,30 @@ async fn insert_task_embedding(pool: &PgPool, document_id: &str, title: &str, bo}}+/// Sets a task's Status system property the way the properties service stores+/// it: a single-element SelectOption array holding the option id.+async fn set_task_status(pool: &PgPool, document_id: &str, status: StatusOption) {+ sqlx::query!(+ r#"+ INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)+ VALUES (+ $1,+ $2,+ 'TASK',+ $3,+ jsonb_build_object('type', 'SelectOption', 'value', jsonb_build_array($4::text))+ )+ "#,+ Uuid::new_v4(),+ document_id,+ SystemPropertyKey::STATUS_UUID,+ status.uuid().to_string(),+ )+ .execute(pool)+ .await+ .unwrap();+}+async fn insert_match(pool: &PgPool, task_id: &str, duplicate_task_id: &str) -> Uuid {let id = Uuid::new_v4();let (task_id, duplicate_task_id) =@@ -428,6 +452,107 @@ async fn deleted_duplicate_tasks_are_hidden(pool: PgPool) {assert!(duplicates.is_empty());}+#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../documents/fixtures",+ scripts("documents_test_data")+ )+)]+async fn closed_duplicate_tasks_are_hidden(pool: PgPool) {+ setup_tasks(&pool).await;+ insert_match(&pool, TASK_ONE, TASK_TWO).await;+ insert_match(&pool, TASK_ONE, TASK_THREE).await;+ set_task_status(&pool, TASK_TWO, StatusOption::Completed).await;+ set_task_status(&pool, TASK_THREE, StatusOption::Canceled).await;++ let service = service(pool.clone());+ let duplicates = service.active_duplicates(TASK_ONE).await.unwrap();+ assert!(+ duplicates.is_empty(),+ "completed/canceled tasks should not be listed as duplicates"+ );+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../documents/fixtures",+ scripts("documents_test_data")+ )+)]+async fn open_duplicate_tasks_stay_visible(pool: PgPool) {+ setup_tasks(&pool).await;+ insert_match(&pool, TASK_ONE, TASK_TWO).await;+ set_task_status(&pool, TASK_TWO, StatusOption::InProgress).await;++ let service = service(pool.clone());+ let duplicates = service.active_duplicates(TASK_ONE).await.unwrap();+ assert_eq!(duplicates.len(), 1);+ assert_eq!(duplicates[0].task_id, TASK_TWO);+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../documents/fixtures",+ scripts("documents_test_data")+ )+)]+async fn similarity_search_excludes_closed_tasks(pool: PgPool) {+ setup_tasks(&pool).await;+ // Both tasks carry the identical searched-for text; only status differs.+ insert_task_embedding(&pool, TASK_ONE, DETECTION_TITLE, DETECTION_BODY).await;+ insert_task_embedding(&pool, TASK_TWO, DETECTION_TITLE, DETECTION_BODY).await;+ set_task_status(&pool, TASK_ONE, StatusOption::Completed).await;+ set_task_status(&pool, TASK_TWO, StatusOption::InProgress).await;++ let service = service(pool.clone());+ let results = service+ .similarity_search(+ OWNER,+ Some(TEAM_ID),+ DETECTION_TITLE,+ &EmbeddingMarkdown::from_client_trusted(DETECTION_BODY.to_string()),+ )+ .await+ .unwrap();++ assert_eq!(+ results+ .iter()+ .map(|r| r.task_id.as_str())+ .collect::<Vec<_>>(),+ vec![TASK_TWO],+ "the completed task should be excluded from similarity candidates"+ );+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../documents/fixtures",+ scripts("documents_test_data")+ )+)]+async fn detection_skips_closed_tasks(pool: PgPool) {+ setup_tasks(&pool).await;+ insert_task_embedding(&pool, TASK_TWO, DETECTION_TITLE, DETECTION_BODY).await;+ set_task_status(&pool, TASK_TWO, StatusOption::Canceled).await;++ let service = service(pool.clone());+ service+ .detect_new_task(detection_task(TASK_ONE))+ .await+ .unwrap();++ let duplicates = service.active_duplicates(TASK_ONE).await.unwrap();+ assert!(+ duplicates.is_empty(),+ "a canceled task should not be matched as a duplicate of a new task"+ );+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS",fixtures(diff --git a/rust/cloud-storage/task_dedup/tests/eval/cases/eval_recall_at_k.rs b/rust/cloud-storage/task_dedup/tests/eval/cases/eval_recall_at_k.rsindex 92bc5344f9..47de0e712d 100644--- a/rust/cloud-storage/task_dedup/tests/eval/cases/eval_recall_at_k.rs+++ b/rust/cloud-storage/task_dedup/tests/eval/cases/eval_recall_at_k.rs@@ -65,6 +65,7 @@ async fn recall_at_k_baseline(pool: PgPool) {vector_candidate_limit: 500,duplicate_limit: 500,min_vector_similarity: f64::NEG_INFINITY,+ min_rerank_score: f64::NEG_INFINITY,..TaskDedupConfig::default()},);diff --git a/rust/cloud-storage/task_dedup/tests/eval/cases/eval_retrieval_recall.rs b/rust/cloud-storage/task_dedup/tests/eval/cases/eval_retrieval_recall.rsindex e4607542dd..399f623842 100644--- a/rust/cloud-storage/task_dedup/tests/eval/cases/eval_retrieval_recall.rs+++ b/rust/cloud-storage/task_dedup/tests/eval/cases/eval_retrieval_recall.rs@@ -53,12 +53,16 @@ async fn retrieval_recall_baseline(pool: PgPool) {}}- // Large limits: measure retrievability above the floor, not the UI top-5.+ // Large limits: measure retrievability above the vector floor, not the UI+ // top-5. The rerank floor is disabled so this keeps measuring what reaches+ // the detect path's judge (which has no rerank floor); the similarity+ // endpoint's floor has its own eval (`eval_similarity_rerank_floor`).let service = build_service(&pool,TaskDedupConfig {vector_candidate_limit: 300,duplicate_limit: 300,+ min_rerank_score: f64::NEG_INFINITY,..TaskDedupConfig::default()},);diff --git a/rust/cloud-storage/task_dedup/tests/eval/cases/eval_similarity_rerank_floor.rs b/rust/cloud-storage/task_dedup/tests/eval/cases/eval_similarity_rerank_floor.rsnew file mode 100644index 0000000000..fbaa5d4e3a--- /dev/null+++ b/rust/cloud-storage/task_dedup/tests/eval/cases/eval_similarity_rerank_floor.rs@@ -0,0 +1,225 @@+//! Rerank-score floor for the draft similarity search. Seeds and embeds the+//! whole corpus (all real tasks act as distractors), then for every labeled+//! pair retrieves candidates exactly as the endpoint does and scores them with+//! the **production Cohere reranker** — the floor gates Cohere relevance+//! scores, so unlike the other measurements this one cannot substitute the+//! no-op reranker. Sweeping a cutoff over the true counterpart's score shows+//! how much below-floor noise each candidate floor trims and where recall+//! starts to drop, so `min_rerank_score` is chosen from data: the highest floor+//! that still returns every expected duplicate (100% recall), taken low+//! deliberately since the composer would rather show a weak match than hide a+//! real duplicate.+//!+//! The production floor is then gated: the test fails if any expected-duplicate+//! pair scores below `TaskDedupConfig::default().min_rerank_score`, so a floor+//! bump that would hide a known duplicate can't land silently.+//!+//! `#[ignore]` — hits OpenAI and Cohere. Run with `just eval`.++use std::collections::HashMap;++use embedding::embedding_provider::openai::{DIMS, TextEmbedding3Small};+use embedding::{Content, EmbeddingModel, KeyedEmbedding, RerankModel, VectorStore};+use futures::StreamExt;+use macro_db_migrator::MACRO_DB_MIGRATIONS;+use sqlx::PgPool;+use task_dedup::outbound::cohere::CohereReranker;+use task_dedup::outbound::postgres::PgTaskVectorDb;+use task_dedup::{TaskDedupConfig, TaskSearchParameters};++use crate::util::corpus::{embeddable, full_text, load_corpus, seed_ids};+use crate::util::harness::{EVAL_CONCURRENCY, cohere_key, openai_key};+use crate::util::metrics::{fmt_ratio, sweep_report};+use crate::util::seed::{EVAL_OWNER, seed_documents};++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../../documents/fixtures", scripts("documents_test_data"))+)]+#[ignore = "hits OpenAI and Cohere; run locally with API keys"]+async fn similarity_rerank_floor_keeps_full_recall(pool: PgPool) {+ let corpus = load_corpus();+ let ids = seed_ids(&corpus);+ seed_documents(&pool, &corpus, &ids).await;++ let embedder = TextEmbedding3Small::new(openai_key());+ let vector_db = PgTaskVectorDb::new(pool.clone());+ let reranker = CohereReranker::new(cohere_key());++ // Embed the whole corpus concurrently, keeping each task's query vectors so+ // the pair measurements below reuse them instead of re-embedding.+ let embedded: Vec<(String, _)> = futures::stream::iter(&corpus.tasks)+ .map(|task| {+ let embedder = &embedder;+ async move {+ let embedding = embedder.embed(&embeddable(task)).await.expect("embed task");+ (task.id.clone(), embedding)+ }+ })+ .buffer_unordered(EVAL_CONCURRENCY)+ .collect()+ .await;+ let mut queries: HashMap<String, Vec<KeyedEmbedding<DIMS>>> = HashMap::new();+ for (corpus_id, embedding) in embedded {+ queries.insert(+ corpus_id.clone(),+ embedding+ .iter()+ .map(|field| KeyedEmbedding {+ search_key: field.search_key,+ embedding: field.embedding,+ })+ .collect(),+ );+ if !embedding.is_empty() {+ vector_db+ .upsert_embeddings(ids[&corpus_id].clone(), embedding)+ .await+ .expect("persist embedding");+ }+ }++ // For each labeled pair: retrieve B's candidates with no floor and a large+ // limit (excluding B itself, as production excludes the query draft by not+ // having stored it), rerank them with Cohere, and record the relevance+ // score the true counterpart A comes back with (0.0 when A is not retrieved+ // at all), plus each query's other scores so the sweep can also report how+ // much noise a floor trims.+ let outcomes: Vec<(f64, bool, Vec<f64>, &task_dedup::eval::LabeledPair)> =+ futures::stream::iter(&corpus.pairs)+ .map(|pair| {+ let vector_db = &vector_db;+ let reranker = &reranker;+ let corpus = &corpus;+ let ids = &ids;+ let queries = &queries;+ async move {+ let task_b = corpus.task(&pair.b).expect("task b");+ let a_id = &ids[&pair.a];+ let b_id = &ids[&pair.b];+ // Rebuild the query per pair: `KeyedEmbedding` is not `Clone`,+ // but its fields are `Copy`.+ let query: Vec<KeyedEmbedding<DIMS>> = queries[&pair.b]+ .iter()+ .map(|field| KeyedEmbedding {+ search_key: field.search_key,+ embedding: field.embedding,+ })+ .collect();+ if query.is_empty() {+ return (0.0, pair.expected_duplicate, Vec::new(), pair);+ }+ let results = vector_db+ .cosine_search(+ query,+ TaskSearchParameters {+ owner: EVAL_OWNER.to_string(),+ team_id: None,+ limit: 500,+ exclude_document_id: Some(b_id.clone()),+ exclude_dismissed: false,+ },+ )+ .await+ .expect("cosine_search");+ if results.is_empty() {+ return (0.0, pair.expected_duplicate, Vec::new(), pair);+ }+ let reranked = reranker+ .rerank(+ Content::Owned(full_text(&task_b.title, &task_b.body)),+ results,+ )+ .await+ .expect("cohere rerank");+ let score = reranked+ .iter()+ .find(|scored| &scored.item == a_id)+ .map_or(0.0, |scored| f64::from(scored.score));+ let others: Vec<f64> = reranked+ .iter()+ .filter(|scored| &scored.item != a_id)+ .map(|scored| f64::from(scored.score))+ .collect();+ (score, pair.expected_duplicate, others, pair)+ }+ })+ .buffered(EVAL_CONCURRENCY)+ .collect()+ .await;++ let points: Vec<(f64, bool)> = outcomes+ .iter()+ .map(|(score, expected, _, _)| (*score, *expected))+ .collect();+ let thresholds: Vec<f64> = (5..=95).step_by(5).map(|t| f64::from(t) / 100.0).collect();+ println!(+ "{}",+ sweep_report(+ "similarity-search rerank-score floor sweep (Cohere relevance scores)",+ &points,+ &thresholds,+ )+ );++ // How much each floor trims: mean non-counterpart results per query that+ // would still be returned. The floor's payoff is shrinking this without+ // touching recall.+ let queries_measured = outcomes.len().max(1);+ println!(" floor mean other results returned per query");+ for &threshold in &thresholds {+ let kept: usize = outcomes+ .iter()+ .map(|(_, _, others, _)| others.iter().filter(|score| **score >= threshold).count())+ .sum();+ println!(+ " {threshold:>4.2} {:.1}",+ kept as f64 / queries_measured as f64+ );+ }++ // The floor is set by the weakest-scoring true duplicates, so list them:+ // these are the pairs a higher floor would hide from the composer.+ let mut weakest: Vec<(&f64, &task_dedup::eval::LabeledPair)> = outcomes+ .iter()+ .filter(|(_, expected, _, _)| *expected)+ .map(|(score, _, _, pair)| (score, *pair))+ .collect();+ weakest.sort_by(|left, right| left.0.total_cmp(right.0));+ println!("\nlowest-scoring expected duplicates (score, case, B -> A):");+ for (score, pair) in weakest.iter().take(8) {+ let title = |id: &str| corpus.task(id).map_or("?", |task| task.title.as_str());+ println!(+ " {score:.3} [{}] {} ({:?}) -> {} ({:?})",+ pair.case.label(),+ pair.b,+ title(&pair.b),+ pair.a,+ title(&pair.a),+ );+ }++ let floor = TaskDedupConfig::default().min_rerank_score;+ let positives: Vec<&(f64, bool)> = points.iter().filter(|(_, expected)| *expected).collect();+ let recalled = positives+ .iter()+ .filter(|(score, _)| *score >= floor)+ .count();+ let min_positive = positives+ .iter()+ .map(|(score, _)| *score)+ .fold(f64::INFINITY, f64::min);+ println!(+ "\nproduction min_rerank_score={floor:.2}: recall {recalled}/{} ({}), lowest positive score {min_positive:.3}",+ positives.len(),+ fmt_ratio((!positives.is_empty()).then(|| recalled as f64 / positives.len() as f64)),+ );++ assert!(!positives.is_empty(), "corpus has no positive pairs");+ assert_eq!(+ recalled,+ positives.len(),+ "every expected duplicate must survive the production rerank floor \+ ({floor:.2}); lowest positive score was {min_positive:.3}"+ );+}diff --git a/rust/cloud-storage/task_dedup/tests/eval/util/harness.rs b/rust/cloud-storage/task_dedup/tests/eval/util/harness.rsindex bd4dc66df6..2a669723bf 100644--- a/rust/cloud-storage/task_dedup/tests/eval/util/harness.rs+++ b/rust/cloud-storage/task_dedup/tests/eval/util/harness.rs@@ -44,6 +44,27 @@ pub fn openai_key() -> String {.to_string()}+macro_env_var::env_var! {+ /// Env read only by the rerank-floor eval, which runs the production Cohere+ /// reranker instead of [`NoOpReranker`](super::rerank::NoOpReranker).+ struct CohereVars {+ CohereApiKey,+ }+}++/// The Cohere key for the production reranker, with a clear message when it is+/// missing.+pub fn cohere_key() -> String {+ CohereVars::new()+ .expect(+ "COHERE_API_KEY must be set to run the rerank-floor eval \+ (it scores with the production Cohere reranker); export it first",+ )+ .cohere_api_key+ .as_ref()+ .to_string()+}+/// Notifier that drops live-update notifications (irrelevant to the eval).struct NoopNotifier;