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: do not upsert tasks that are not valid (#1808)
Will Hutchinsondb7fb251a85b
diff --git a/rust/cloud-storage/github/src/domain/service/sync/handle_comment.rs b/rust/cloud-storage/github/src/domain/service/sync/handle_comment.rsindex 4b1e6575f4..6af84802de 100644--- a/rust/cloud-storage/github/src/domain/service/sync/handle_comment.rs+++ b/rust/cloud-storage/github/src/domain/service/sync/handle_comment.rs@@ -37,12 +37,22 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServilet github_key = Self::github_key(event);+ // Validate comment task IDs against the document service+ let resolved = self.resolve_tasks(&comment_task_ids).await;++ if resolved.validated_task_ids.is_empty() {+ tracing::debug!("no valid task documents found for extracted IDs");+ return Ok(());+ }+// Ensure PR context task IDs are tracked in the repo (handles PRs// that existed before the tracking table was introduced).if let Some(ref key) = github_key {let pr_context_tasks = {let pr_context = event.extract_pr_context_text().join(" ");- MacroTaskId::extract_from_text(&pr_context)+ let extracted = MacroTaskId::extract_from_text(&pr_context);+ // Validate PR context task IDs too+ self.resolve_tasks(&extracted).await.validated_task_ids};if !pr_context_tasks.is_empty() {tracing::trace!(@@ -59,15 +69,15 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServi}}- // Filter to only truly new task IDs using the repo+ // Filter to only truly new validated task IDs using the repolet truly_new = if let Some(ref key) = github_key {self.repo- .filter_duplicate_tasks(key.clone(), &comment_task_ids)+ .filter_duplicate_tasks(key.clone(), &resolved.validated_task_ids).await.inspect_err(|e| tracing::error!(error=?e, "failed to filter duplicate tasks"))- .unwrap_or_else(|_| comment_task_ids.clone())+ .unwrap_or_else(|_| resolved.validated_task_ids.clone())} else {- comment_task_ids.clone()+ resolved.validated_task_ids.clone()};tracing::trace!(@@ -76,22 +86,27 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServi"filtered to truly new task IDs");- if truly_new.is_empty() {- tracing::debug!("all task IDs already tracked for this PR, skipping");- return Ok(());- }+ if !truly_new.is_empty() {+ let new_task_id_set: std::collections::HashSet<&MacroTaskId> =+ truly_new.iter().collect();+ let new_task_links: Vec<_> = resolved+ .validated_task_ids+ .iter()+ .zip(resolved.task_links.iter())+ .filter(|(id, _)| new_task_id_set.contains(id))+ .map(|(_, link)| link.clone())+ .collect();- let resolved = self.resolve_tasks(&truly_new).await;-- let pr_meta = self.acquire_pr_meta(event).await;- if let Some(ref meta) = pr_meta {- self.post_task_comment(meta, &resolved.task_links).await;+ let pr_meta = self.acquire_pr_meta(event).await;+ if let Some(ref meta) = pr_meta {+ self.post_task_comment(meta, &new_task_links).await;+ }}- // Track the new task IDs in the repo+ // Track only validated task IDs in the repoif let Some(ref key) = github_key {self.repo- .upsert_task_ids(key.clone(), &comment_task_ids)+ .upsert_task_ids(key.clone(), &resolved.validated_task_ids).await.inspect_err(|e| tracing::error!(error=?e, "failed to upsert comment task IDs")).ok();diff --git a/rust/cloud-storage/github/src/domain/service/sync/handle_pr.rs b/rust/cloud-storage/github/src/domain/service/sync/handle_pr.rsindex 53d66500e6..da66e9edb3 100644--- a/rust/cloud-storage/github/src/domain/service/sync/handle_pr.rs+++ b/rust/cloud-storage/github/src/domain/service/sync/handle_pr.rs@@ -31,38 +31,55 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServireturn Ok(());}+ // Resolve all extracted IDs against the document service to validate+ // they are actual task documents (filters out false positives like "macro-inc")+ let resolved_all = self.resolve_tasks(&task_ids).await;++ if resolved_all.validated_task_ids.is_empty() {+ tracing::debug!("no valid task documents found for extracted IDs");+ return Ok(());+ }+let github_key = Self::github_key(event);- // Determine which tasks are new for this PR+ // Determine which validated tasks are new for this PRlet new_task_ids = if let Some(ref key) = github_key {self.repo- .filter_duplicate_tasks(key.clone(), &task_ids)+ .filter_duplicate_tasks(key.clone(), &resolved_all.validated_task_ids).await.inspect_err(|e| tracing::error!(error=?e, "failed to filter duplicate tasks"))- .unwrap_or_else(|_| task_ids.clone())+ .unwrap_or_else(|_| resolved_all.validated_task_ids.clone())} else {- task_ids.clone()+ resolved_all.validated_task_ids.clone()};- // Resolve new tasks and post comment- let new_resolved = self.resolve_tasks(&new_task_ids).await;-- let pr_meta = self.acquire_pr_meta(event).await;- if let Some(ref meta) = pr_meta {- self.post_task_comment(meta, &new_resolved.task_links).await;+ // Post comment for newly discovered tasks+ if !new_task_ids.is_empty() {+ let new_task_id_set: HashSet<&MacroTaskId> = new_task_ids.iter().collect();+ let new_task_links: Vec<_> = resolved_all+ .validated_task_ids+ .iter()+ .zip(resolved_all.task_links.iter())+ .filter(|(id, _)| new_task_id_set.contains(id))+ .map(|(_, link)| link.clone())+ .collect();++ let pr_meta = self.acquire_pr_meta(event).await;+ if let Some(ref meta) = pr_meta {+ self.post_task_comment(meta, &new_task_links).await;+ }}- // Track all task IDs in the repo+ // Track only validated task IDs in the repoif let Some(ref key) = github_key {self.repo- .upsert_task_ids(key.clone(), &task_ids)+ .upsert_task_ids(key.clone(), &resolved_all.validated_task_ids).await.inspect_err(|e| tracing::error!(error=?e, "failed to upsert task IDs")).ok();}- // Update status for all tasks from PR text- let resolved_all = self.resolve_tasks(&task_ids).await;+ // Update status for all validated tasksself.update_task_statuses(&resolved_all.doc_ids, "In Review").await;@@ -95,38 +112,55 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServireturn Ok(());}+ // Resolve all extracted IDs against the document service to validate+ // they are actual task documents (filters out false positives like "macro-inc")+ let resolved_all = self.resolve_tasks(&task_ids).await;++ if resolved_all.validated_task_ids.is_empty() {+ tracing::debug!("no valid task documents found for extracted IDs");+ return Ok(());+ }+let github_key = Self::github_key(event);- // Determine which tasks are new for this PR+ // Determine which validated tasks are new for this PRlet new_task_ids = if let Some(ref key) = github_key {self.repo- .filter_duplicate_tasks(key.clone(), &task_ids)+ .filter_duplicate_tasks(key.clone(), &resolved_all.validated_task_ids).await.inspect_err(|e| tracing::error!(error=?e, "failed to filter duplicate tasks"))- .unwrap_or_else(|_| task_ids.clone())+ .unwrap_or_else(|_| resolved_all.validated_task_ids.clone())} else {- task_ids.clone()+ resolved_all.validated_task_ids.clone()};- // Resolve new tasks and post comment- let new_resolved = self.resolve_tasks(&new_task_ids).await;-- let pr_meta = self.acquire_pr_meta(event).await;- if let Some(ref meta) = pr_meta {- self.post_task_comment(meta, &new_resolved.task_links).await;+ // Post comment for newly discovered tasks+ if !new_task_ids.is_empty() {+ let new_task_id_set: HashSet<&MacroTaskId> = new_task_ids.iter().collect();+ let new_task_links: Vec<_> = resolved_all+ .validated_task_ids+ .iter()+ .zip(resolved_all.task_links.iter())+ .filter(|(id, _)| new_task_id_set.contains(id))+ .map(|(_, link)| link.clone())+ .collect();++ let pr_meta = self.acquire_pr_meta(event).await;+ if let Some(ref meta) = pr_meta {+ self.post_task_comment(meta, &new_task_links).await;+ }}- // Track all task IDs in the repo+ // Track only validated task IDs in the repoif let Some(ref key) = github_key {self.repo- .upsert_task_ids(key.clone(), &task_ids)+ .upsert_task_ids(key.clone(), &resolved_all.validated_task_ids).await.inspect_err(|e| tracing::error!(error=?e, "failed to upsert task IDs")).ok();}- // Update status for all tasks from PR text- let resolved_all = self.resolve_tasks(&task_ids).await;+ // Update status for all validated tasksself.update_task_statuses(&resolved_all.doc_ids, "In Review").await;@@ -161,6 +195,7 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServi);// Also include all previously tracked tasks from the repo+ // (these were already validated when they were stored)if let Some(key) = Self::github_key(event) {match self.repo.get_task_ids(key).await {Ok(repo_tasks) => {@@ -188,6 +223,7 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServi"total task IDs for close event");+ // resolve_tasks validates each ID is an actual task documentlet resolved = self.resolve_tasks(&all_task_ids).await;// No bot comment on closediff --git a/rust/cloud-storage/github/src/domain/service/sync/mod.rs b/rust/cloud-storage/github/src/domain/service/sync/mod.rsindex 90e4f7b4e7..36975cd76a 100644--- a/rust/cloud-storage/github/src/domain/service/sync/mod.rs+++ b/rust/cloud-storage/github/src/domain/service/sync/mod.rs@@ -69,6 +69,8 @@ struct ResolvedTasks {doc_ids: Vec<String>,/// Markdown links for resolved tasks (used for PR comments).task_links: Vec<String>,+ /// Task IDs that were validated as actual task documents.+ validated_task_ids: Vec<MacroTaskId>,}impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServiceImpl<D, R, C> {@@ -140,6 +142,7 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServilet mut doc_ids = Vec::new();let mut task_links = Vec::new();+ let mut validated_task_ids = Vec::new();for task_id in task_ids {let uuid = match task_id.to_uuid() {@@ -176,6 +179,7 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServidoc_ids.push(doc_id.clone());task_links.push(create_macro_task_comment_link(doc_name, doc_id));+ validated_task_ids.push(task_id.clone());} else {tracing::trace!(task_id=%uuid, "document found but is not a task, skipping");}@@ -198,6 +202,7 @@ impl<D: DocumentService, R: GithubSyncRepo, C: GithubSyncClient> GithubSyncServiResolvedTasks {doc_ids,task_links,+ validated_task_ids,}}diff --git a/rust/cloud-storage/github/src/domain/service/sync/test.rs b/rust/cloud-storage/github/src/domain/service/sync/test.rsindex c01fb651f2..c938d437f3 100644--- a/rust/cloud-storage/github/src/domain/service/sync/test.rs+++ b/rust/cloud-storage/github/src/domain/service/sync/test.rs@@ -1078,3 +1078,37 @@ async fn comment_deduplicates_against_repo() {"comment should not re-trigger for task already tracked in repo");}++#[tokio::test]+async fn false_positive_macro_prefix_ignored() {+ // "macro-inc" matches the regex but does not correspond to a real task document.+ let (service, doc_service) = make_sync_service_with_doc_service();+ let event = ValidatedGithubWebhookEvent::new(+ "pull_request".to_string(),+ serde_json::json!({+ "action": "opened",+ "pull_request": {+ "number": 42,+ "title": "update macro-inc dependency",+ "body": null,+ "head": { "ref": "feature/update-deps" }+ },+ "repository": {+ "name": "my-repo",+ "owner": { "login": "my-org" }+ },+ "installation": { "id": 12345 }+ }),+ );++ service.process_webhook_event(&event).await.unwrap();++ assert!(+ service.client.pr_comments().is_empty(),+ "false positive macro- prefix should not trigger a comment"+ );+ assert!(+ doc_service.task_status_calls().is_empty(),+ "false positive macro- prefix should not trigger a status update"+ );+}