Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(soup): Soup ast does not support getting non task documents (#2564)

dev-rb0716fddf78ba
diff --git a/rust/cloud-storage/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs b/rust/cloud-storage/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
index bb9ca21e07..d55c21c756 100644
--- a/rust/cloud-storage/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
+++ b/rust/cloud-storage/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
@@ -362,7 +362,7 @@ fn build_document_filter(ast: Option<&Expr<DocumentLiteral>>) -> String {
// false is equivalent to disabled/no-op.
filter_ast::ExprFrame::Literal(DocumentLiteral::IncludeCbmAtmNc(false)) => String::new(),
filter_ast::ExprFrame::Literal(DocumentLiteral::SubType(st)) => {
- format!("dt.sub_type = '{st}'")
+ format!("(dt.sub_type IS NOT NULL AND dt.sub_type = '{st}')")
}
filter_ast::ExprFrame::Literal(DocumentLiteral::IsEmailAttachment(true)) => {
r#"EXISTS(SELECT 1 FROM document_email WHERE document_id = d.id)"#
diff --git a/rust/cloud-storage/soup/src/outbound/pg_soup_repo/expanded/tests.rs b/rust/cloud-storage/soup/src/outbound/pg_soup_repo/expanded/tests.rs
index 67904eac53..5e9e4b5762 100644
--- a/rust/cloud-storage/soup/src/outbound/pg_soup_repo/expanded/tests.rs
+++ b/rust/cloud-storage/soup/src/outbound/pg_soup_repo/expanded/tests.rs
@@ -5310,3 +5310,63 @@ async fn test_dyn_filter_is_email_attachment_false(db: PgPool) -> anyhow::Result
Ok(())
}
+
+// Test filtering documents by NOT sub_type = task (excludes tasks, includes non-tasks with NULL sub_type)
+#[sqlx::test(
+ fixtures(
+ path = "../../../../../macro_db_client/fixtures",
+ scripts("entity_filter_tests")
+ ),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn test_dyn_filter_by_not_document_sub_type_task(db: PgPool) -> anyhow::Result<()> {
+ let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
+
+ // Construct an AST with NOT(sub_type = task) via JSON deserialization
+ // This tests the fix for NULL handling: NOT (dt.sub_type IS NOT NULL AND dt.sub_type = 'task')
+ // Which correctly expands to: dt.sub_type IS NULL OR dt.sub_type != 'task'
+ let filters: EntityFilterAst = serde_json::from_value(serde_json::json!({
+ "df": {
+ "!": {
+ "l": { "dst": "task" }
+ }
+ }
+ }))?;
+
+ let items = expanded_dynamic_cursor_soup(
+ &db,
+ ExpandedDynamicCursorArgs {
+ user_id: user_id.copied(),
+ limit: 50,
+ cursor: Query::Sort(SimpleSortMethod::CreatedAt, filters),
+ exclude_frecency: false,
+ },
+ )
+ .await?;
+
+ let mut task_doc_count = 0;
+ let mut non_task_doc_count = 0;
+
+ for item in &items {
+ if let SoupItem::Document(doc) = item {
+ if doc.sub_type.is_some() {
+ task_doc_count += 1;
+ } else {
+ non_task_doc_count += 1;
+ }
+ }
+ }
+
+ // The NOT filter should exclude all task documents
+ assert_eq!(
+ task_doc_count, 0,
+ "Should get 0 task documents with NOT task filter"
+ );
+ // Should return non-task documents (those with NULL sub_type)
+ assert!(
+ non_task_doc_count > 0,
+ "Should get non-task documents (NULL sub_type) with NOT task filter"
+ );
+
+ Ok(())
+}