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.

Seanaye/fix/preserve v3 projections (#6136)

Sean Aye9ebdfdedf0a1
diff --git a/Cargo.lock b/Cargo.lock
index 094e492596..ab48be1426 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4138,9 +4138,7 @@ checksum = "7704b5fdd17b18ae31c4c1da5a2e0305a2bf17b5249300a9ee9ed7b72114c636"
name = "cowlike"
version = "0.1.0"
dependencies = [
- "bot_id",
"either",
- "macro_user_id",
]
[[package]]
@@ -11036,7 +11034,6 @@ dependencies = [
"macro_uuid",
"model-entity",
"model-error-response",
- "model_notifications",
"model_user",
"models_pagination",
"rate_limit",
diff --git a/crates/channel_sender/src/test.rs b/crates/channel_sender/src/test.rs
index cfd35e816d..ddf81c77a9 100644
--- a/crates/channel_sender/src/test.rs
+++ b/crates/channel_sender/src/test.rs
@@ -32,3 +32,29 @@ fn fallback_user_name_uses_email_local_part() {
assert_eq!(user_id.email_part().local_part(), "shepherd.hatton");
}
+
+#[test]
+fn bot_sender_into_owned_and_copied_preserve_contents() {
+ let storage = "bot|00000000-0000-0000-0000-00000000a1a1";
+
+ let copied_value = ChannelSender::parse_from_str(storage).unwrap();
+ let copied = copied_value.copied();
+ assert!(matches!(copied.0, InnerVal::Left(bot_id) if bot_id.as_ref() == storage));
+
+ let owned_value = ChannelSender::parse_from_str(storage).unwrap();
+ let owned = owned_value.into_owned();
+ assert!(matches!(owned.0, InnerVal::Left(bot_id) if bot_id.as_ref() == storage));
+}
+
+#[test]
+fn user_sender_into_owned_and_copied_preserve_contents() {
+ let storage = "macro|[email protected]";
+
+ let copied_value = ChannelSender::parse_from_str(storage).unwrap();
+ let copied = copied_value.copied();
+ assert!(matches!(copied.0, InnerVal::Right(user_id) if user_id.as_ref() == storage));
+
+ let owned_value = ChannelSender::parse_from_str(storage).unwrap();
+ let owned = owned_value.into_owned();
+ assert!(matches!(owned.0, InnerVal::Right(user_id) if user_id.as_ref() == storage));
+}
diff --git a/crates/cowlike/Cargo.toml b/crates/cowlike/Cargo.toml
index a503a1631f..a41dde0755 100644
--- a/crates/cowlike/Cargo.toml
+++ b/crates/cowlike/Cargo.toml
@@ -6,7 +6,3 @@ version = "0.1.0"
[dependencies]
either = { workspace = true }
-
-[dev-dependencies]
-bot_id = { path = "../bot_id" }
-macro_user_id = { path = "../macro_user_id" }
diff --git a/crates/cowlike/src/lib.rs b/crates/cowlike/src/lib.rs
index add1510c39..fd472370c4 100644
--- a/crates/cowlike/src/lib.rs
+++ b/crates/cowlike/src/lib.rs
@@ -2,9 +2,6 @@
//! This crate exposes the [CowLike] trait
//! And the ArcCowStr
-#[cfg(test)]
-mod test;
-
use std::sync::Arc;
use either::Either;
diff --git a/crates/cowlike/src/test.rs b/crates/cowlike/src/test.rs
deleted file mode 100644
index bf71c963b3..0000000000
--- a/crates/cowlike/src/test.rs
+++ /dev/null
@@ -1,33 +0,0 @@
-use bot_id::{BotIdStr, cowlike::CowLike};
-use either::Either;
-use macro_user_id::user_id::MacroUserIdStr;
-
-#[test]
-fn either_left_bot_id_str_into_owned_and_copied_preserve_contents() {
- let storage = "bot|00000000-0000-0000-0000-00000000a1a1";
-
- let copied_value: Either<BotIdStr<'_>, MacroUserIdStr<'_>> =
- Either::Left(BotIdStr::parse_from_str(storage).unwrap());
- let copied = copied_value.copied();
- assert!(matches!(copied, Either::Left(bot_id) if bot_id.as_ref() == storage));
-
- let owned_value: Either<BotIdStr<'_>, MacroUserIdStr<'_>> =
- Either::Left(BotIdStr::parse_from_str(storage).unwrap());
- let owned = owned_value.into_owned();
- assert!(matches!(owned, Either::Left(bot_id) if bot_id.as_ref() == storage));
-}
-
-#[test]
-fn either_right_macro_user_id_str_into_owned_and_copied_preserve_contents() {
- let storage = "macro|[email protected]";
-
- let copied_value: Either<BotIdStr<'_>, MacroUserIdStr<'_>> =
- Either::Right(MacroUserIdStr::parse_from_str(storage).unwrap());
- let copied = copied_value.copied();
- assert!(matches!(copied, Either::Right(user_id) if user_id.as_ref() == storage));
-
- let owned_value: Either<BotIdStr<'_>, MacroUserIdStr<'_>> =
- Either::Right(MacroUserIdStr::parse_from_str(storage).unwrap());
- let owned = owned_value.into_owned();
- assert!(matches!(owned, Either::Right(user_id) if user_id.as_ref() == storage));
-}
diff --git a/crates/model_notifications/src/metadata/test.rs b/crates/model_notifications/src/metadata/test.rs
index b7beea915f..7afdbeb058 100644
--- a/crates/model_notifications/src/metadata/test.rs
+++ b/crates/model_notifications/src/metadata/test.rs
@@ -919,3 +919,62 @@ fn reminder_metadata_reads_back_without_a_firing() {
assert!(metadata.scheduled_for.is_none());
}
+
+#[test]
+fn notification_status_patch_decodes_notif_event_metadata() {
+ use std::borrow::Cow;
+
+ use notification::domain::models::{
+ PatchDelete, UserNotificationRow, websocket_notification_event::NotificationTopicEvent,
+ };
+
+ let user = uid("macro|[email protected]");
+ let assigned_by = uid("macro|[email protected]");
+ let row = UserNotificationRow {
+ owner_id: user.clone(),
+ notification_id: Uuid::nil(),
+ notification_event_type: "task_assigned".to_string(),
+ entity: EntityType::Document.with_entity_string("document-id".to_string()),
+ sent: true,
+ done: false,
+ created_at: Utc::now(),
+ viewed_at: None,
+ updated_at: Utc::now(),
+ deleted_at: None,
+ notification_metadata: serde_json::json!({
+ "taskId": "task-1",
+ "taskName": "Test task",
+ "assignedBy": assigned_by.as_ref(),
+ }),
+ sender_id: None,
+ };
+ let event = NotificationTopicEvent::NotificationStatusesUpdatedForUser {
+ user,
+ updates: vec![PatchDelete::Patch {
+ diff: Cow::Owned(row),
+ }],
+ };
+
+ let NotificationTopicEvent::NotificationStatusesUpdatedForUser { updates, .. } = event
+ .deserialize_metadata::<crate::NotifEvent>()
+ .expect("tagged status patch metadata decodes")
+ else {
+ panic!("expected user notification statuses event");
+ };
+ let PatchDelete::Patch { diff } = &updates[0] else {
+ panic!("expected status patch");
+ };
+ let crate::NotifEvent::TaskAssigned(TaskAssignedMetadata {
+ task_id,
+ task_name,
+ assigned_by: decoded_assigned_by,
+ ..
+ }) = &diff.notification_metadata
+ else {
+ panic!("expected task assigned metadata");
+ };
+
+ assert_eq!(task_id, "task-1");
+ assert_eq!(task_name.as_deref(), Some("Test task"));
+ assert_eq!(decoded_assigned_by, &assigned_by);
+}
diff --git a/crates/notification/Cargo.toml b/crates/notification/Cargo.toml
index ef25384d83..23ec7549b7 100644
--- a/crates/notification/Cargo.toml
+++ b/crates/notification/Cargo.toml
@@ -78,7 +78,6 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }
[dev-dependencies]
http-body-util = { workspace = true }
macro_db_migrator = { path = "../macro_db_migrator" }
-model_notifications = { path = "../model_notifications" }
rate_limit = { path = "../rate_limit", features = ["testing"] }
tokio = { workspace = true, features = ["macros", "rt", "test-util"] }
tower = { workspace = true }
diff --git a/crates/notification/src/domain/models/websocket_notification_event.rs b/crates/notification/src/domain/models/websocket_notification_event.rs
index 3a6e0e1f72..8fb1994055 100644
--- a/crates/notification/src/domain/models/websocket_notification_event.rs
+++ b/crates/notification/src/domain/models/websocket_notification_event.rs
@@ -5,6 +5,7 @@ use std::borrow::Cow;
use macro_event_broker::{Event, MacroEvent, TopicEvent};
use macro_event_topics::MacroNotificationsTopic;
use macro_user_id::user_id::MacroUserIdStr;
+use rootcause::prelude::{Report, ResultExt as _};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use uuid::Uuid;
@@ -45,6 +46,79 @@ pub enum NotificationTopicEvent<'a, T: Clone> {
},
}
+impl NotificationTopicEvent<'static, serde_json::Value> {
+ /// Deserializes the JSON metadata carried by every notification row into `T`.
+ ///
+ /// Status patches use the row's notification event type as the external serde tag,
+ /// while delivery requests deserialize their metadata values directly.
+ pub fn deserialize_metadata<T>(self) -> Result<NotificationTopicEvent<'static, T>, Report>
+ where
+ T: Clone + DeserializeOwned + 'static,
+ {
+ match self {
+ NotificationTopicEvent::WebSocketDeliveryRequested(WebSocketNotificationMetadata {
+ notifications,
+ }) => Ok(NotificationTopicEvent::WebSocketDeliveryRequested(
+ WebSocketNotificationMetadata {
+ notifications: notifications
+ .into_iter()
+ .map(decode_notification_row)
+ .collect::<Result<Vec<_>, _>>()?,
+ },
+ )),
+ NotificationTopicEvent::NotificationStatusUpdatedForUsers { users, update } => {
+ Ok(NotificationTopicEvent::NotificationStatusUpdatedForUsers { users, update })
+ }
+ NotificationTopicEvent::NotificationStatusesUpdatedForUser { user, updates } => {
+ Ok(NotificationTopicEvent::NotificationStatusesUpdatedForUser {
+ user,
+ updates: updates
+ .into_iter()
+ .map(decode_update)
+ .collect::<Result<Vec<_>, _>>()?,
+ })
+ }
+ }
+ }
+}
+
+fn decode_notification_row<T>(
+ row: UserNotificationRow<serde_json::Value>,
+) -> Result<UserNotificationRow<T>, Report>
+where
+ T: DeserializeOwned,
+{
+ Ok(row.try_map(|metadata| {
+ serde_json::from_value(metadata).context("failed to decode notification metadata")
+ })?)
+}
+
+fn decode_status_notification_row<T>(
+ row: UserNotificationRow<serde_json::Value>,
+) -> Result<UserNotificationRow<T>, Report>
+where
+ T: DeserializeOwned,
+{
+ Ok(row
+ .into_tagged()
+ .deserialize_metadata::<T>()
+ .context("failed to decode tagged notification status metadata")?)
+}
+
+fn decode_update<T>(
+ update: PatchDelete<Uuid, Cow<'static, UserNotificationRow<serde_json::Value>>>,
+) -> Result<PatchDelete<Uuid, Cow<'static, UserNotificationRow<T>>>, Report>
+where
+ T: Clone + DeserializeOwned + 'static,
+{
+ match update {
+ PatchDelete::Patch { diff } => Ok(PatchDelete::Patch {
+ diff: Cow::Owned(decode_status_notification_row(diff.into_owned())?),
+ }),
+ PatchDelete::Delete { id } => Ok(PatchDelete::Delete { id }),
+ }
+}
+
impl<'a, T: Clone + Serialize + DeserializeOwned + Send + Sync> TopicEvent
for NotificationTopicEvent<'a, T>
{
diff --git a/crates/notification/src/outbound/notification_consumer.rs b/crates/notification/src/outbound/notification_consumer.rs
index 01e3bba9b0..5b826d075a 100644
--- a/crates/notification/src/outbound/notification_consumer.rs
+++ b/crates/notification/src/outbound/notification_consumer.rs
@@ -7,7 +7,7 @@
#[cfg(test)]
mod test;
-use std::{borrow::Cow, marker::PhantomData, time::Duration};
+use std::{marker::PhantomData, time::Duration};
use kafka_util::{InitialOffset, KafkaEventConsumer, Ungrouped};
use macro_event_broker::{
@@ -18,12 +18,7 @@ use rootcause::prelude::{Report, ResultExt as _};
use serde::{Serialize, de::DeserializeOwned};
use crate::domain::{
- models::{
- PatchDelete, UserNotificationRow,
- websocket_notification_event::{
- JsonNotificationMacroEvent, NotificationTopicEvent, WebSocketNotificationMetadata,
- },
- },
+ models::websocket_notification_event::{JsonNotificationMacroEvent, NotificationTopicEvent},
ports::NotificationTopicEventConsumer,
};
@@ -114,82 +109,13 @@ where
return match event {
DeclaredMacroEvent::JsonNotificationMacroEvent(event) => {
- decode_typed_event(event.into_topic_event())
+ event.into_topic_event().deserialize_metadata()
}
};
}
}
}
-fn decode_typed_event<T>(
- event: NotificationTopicEvent<'static, serde_json::Value>,
-) -> Result<NotificationTopicEvent<'static, T>, Report>
-where
- T: Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
-{
- match event {
- NotificationTopicEvent::WebSocketDeliveryRequested(WebSocketNotificationMetadata {
- notifications,
- }) => Ok(NotificationTopicEvent::WebSocketDeliveryRequested(
- WebSocketNotificationMetadata {
- notifications: notifications
- .into_iter()
- .map(decode_notification_row)
- .collect::<Result<Vec<_>, _>>()?,
- },
- )),
- NotificationTopicEvent::NotificationStatusUpdatedForUsers { users, update } => {
- Ok(NotificationTopicEvent::NotificationStatusUpdatedForUsers { users, update })
- }
- NotificationTopicEvent::NotificationStatusesUpdatedForUser { user, updates } => {
- Ok(NotificationTopicEvent::NotificationStatusesUpdatedForUser {
- user,
- updates: updates
- .into_iter()
- .map(decode_update)
- .collect::<Result<Vec<_>, _>>()?,
- })
- }
- }
-}
-
-fn decode_notification_row<T>(
- row: UserNotificationRow<serde_json::Value>,
-) -> Result<UserNotificationRow<T>, Report>
-where
- T: DeserializeOwned,
-{
- Ok(row.try_map(|metadata| {
- serde_json::from_value(metadata).context("failed to decode notification metadata")
- })?)
-}
-
-fn decode_status_notification_row<T>(
- row: UserNotificationRow<serde_json::Value>,
-) -> Result<UserNotificationRow<T>, Report>
-where
- T: DeserializeOwned,
-{
- Ok(row
- .into_tagged()
- .deserialize_metadata::<T>()
- .context("failed to decode tagged notification status metadata")?)
-}
-
-fn decode_update<T>(
- update: PatchDelete<uuid::Uuid, Cow<'static, UserNotificationRow<serde_json::Value>>>,
-) -> Result<PatchDelete<uuid::Uuid, Cow<'static, UserNotificationRow<T>>>, Report>
-where
- T: Clone + DeserializeOwned + 'static,
-{
- match update {
- PatchDelete::Patch { diff } => Ok(PatchDelete::Patch {
- diff: Cow::Owned(decode_status_notification_row(diff.into_owned())?),
- }),
- PatchDelete::Delete { id } => Ok(PatchDelete::Delete { id }),
- }
-}
-
impl<T> NotificationTopicEventConsumer<T> for NotificationTopicConsumer<T>
where
T: Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
diff --git a/crates/notification/src/outbound/notification_consumer/test.rs b/crates/notification/src/outbound/notification_consumer/test.rs
index 5fff0ee2fd..a8f0c7fb3a 100644
--- a/crates/notification/src/outbound/notification_consumer/test.rs
+++ b/crates/notification/src/outbound/notification_consumer/test.rs
@@ -6,10 +6,9 @@ use macro_event_broker::{
};
use macro_user_id::user_id::MacroUserIdStr;
use model_entity::EntityType;
-use model_notifications::{NotifEvent, TaskAssignedMetadata};
use uuid::Uuid;
-use super::{DeclaredMacroEvent, decode_typed_event};
+use super::DeclaredMacroEvent;
use crate::domain::models::websocket_notification_event::{
NotificationTopicEvent, WebSocketNotificationMetadata,
};
@@ -80,47 +79,6 @@ fn row() -> UserNotificationRow<serde_json::Value> {
}
}
-#[test]
-fn status_patch_decodes_tagged_notification_metadata() {
- let mut row = row();
- let user = row.owner_id.clone();
- let assigned_by = MacroUserIdStr::try_from("macro|[email protected]".to_string())
- .expect("valid assigner ID");
- row.notification_event_type = "task_assigned".to_string();
- row.notification_metadata = serde_json::json!({
- "taskId": "task-1",
- "taskName": "Test task",
- "assignedBy": assigned_by.as_ref(),
- });
- let event = NotificationTopicEvent::NotificationStatusesUpdatedForUser {
- user,
- updates: vec![PatchDelete::Patch {
- diff: Cow::Owned(row),
- }],
- };
-
- let NotificationTopicEvent::NotificationStatusesUpdatedForUser { updates, .. } =
- decode_typed_event::<NotifEvent>(event).expect("tagged status patch metadata decodes")
- else {
- panic!("expected user notification statuses event");
- };
- let PatchDelete::Patch { diff } = &updates[0] else {
- panic!("expected status patch");
- };
- let NotifEvent::TaskAssigned(TaskAssignedMetadata {
- task_id,
- task_name,
- assigned_by: decoded_assigned_by,
- ..
- }) = &diff.notification_metadata
- else {
- panic!("expected task assigned metadata");
- };
- assert_eq!(task_id, "task-1");
- assert_eq!(task_name.as_deref(), Some("Test task"));
- assert_eq!(decoded_assigned_by, &assigned_by);
-}
-
#[test]
fn typed_decoder_returns_every_variant_with_notification_rows() {
let row = row();
@@ -145,7 +103,7 @@ fn typed_decoder_returns_every_variant_with_notification_rows() {
let decoded = events
.into_iter()
- .map(decode_typed_event::<serde_json::Value>)
+ .map(|event| event.deserialize_metadata::<serde_json::Value>())
.collect::<Result<Vec<_>, _>>()
.expect("all variants decode as notification rows");
diff --git a/crates/soup_filter_cache_adapter/src/lib.rs b/crates/soup_filter_cache_adapter/src/lib.rs
index 2858ae1c71..397a6f36bf 100644
--- a/crates/soup_filter_cache_adapter/src/lib.rs
+++ b/crates/soup_filter_cache_adapter/src/lib.rs
@@ -19,7 +19,7 @@ use predicate_index::{
use soup_filter_projection::{
DirectProjectionInput, DirectProjectionPatchInput, DocumentSubType,
SoupCacheProjectionSupplement, SoupFlatEntityKind, compose_soup_flat_v3,
- decode_cache_projection_supplement, patch_direct_fields, project_direct_fields,
+ decode_cache_projection_supplement, patch_direct_fields,
};
use std::collections::HashSet;
@@ -81,11 +81,11 @@ pub fn compile_filter_request(
/// Document supplements are decoded only where `cacheProjection` is selected
/// for the surrounding entity and are merged with direct fields from that same
/// object. A selected null or missing Document supplement marks v3 incomplete;
-/// selected null values on direct-only Projects and Chats are expected. Mutation
-/// payloads that omit the field become bounded v3 direct-field patches,
-/// preserving server-owned facts from an existing complete projection. Query
-/// and subscription documents without the field retain the legacy v1 direct
-/// projection during the staged rollout.
+/// selected null values on direct-only Projects and Chats are expected. Payloads
+/// that omit the field become bounded v3 direct-field patches, preserving
+/// server-owned facts from an existing complete projection. A payload without
+/// direct projection fields emits an empty patch so an existing v3 projection is
+/// retained while a missing base becomes explicitly incomplete.
pub fn authoritative_projection_mutations(
query: &str,
operation_name: Option<&str>,
@@ -117,7 +117,6 @@ pub fn authoritative_projection_mutations(
&operation.selection_set,
root_type,
root,
- operation.kind,
&mut mutations,
&mut has_unbound_incomplete_entity,
);
@@ -139,7 +138,6 @@ fn walk_authoritative_object(
selections: &[Selection],
declared_type: &str,
object: &serde_json::Map<String, serde_json::Value>,
- operation_kind: OperationKind,
mutations: &mut IndexMap<String, ProjectionMutation>,
has_unbound_incomplete_entity: &mut bool,
) {
@@ -171,36 +169,18 @@ fn walk_authoritative_object(
if let Some((key_text, record_key)) = normalized_key {
let kind = projection_kind(&partition).expect("supported partition has a kind");
let mutation = if projection_fields.is_empty() {
- if operation_kind == OperationKind::Mutation {
- match authoritative_v3_patch_for_object(
- record_key.clone(),
- partition.clone(),
- object,
- ) {
- Ok(mutation) => mutation,
- Err(()) => Some(ProjectionMutation::MarkIncomplete {
- record_key,
- profile: vocabulary::profile_v3(),
- partition,
- kind: ProjectionIncompleteKind::Dirty,
- }),
- }
- } else {
- Some(
- direct_projection_for_object(
- record_key.clone(),
- partition.clone(),
- object,
- None,
- )
- .map(ProjectionMutation::Replace)
- .unwrap_or(ProjectionMutation::MarkIncomplete {
- record_key,
- profile: vocabulary::profile(),
- partition,
- kind: ProjectionIncompleteKind::Dirty,
- }),
- )
+ match authoritative_v3_patch_for_object(
+ record_key.clone(),
+ partition.clone(),
+ object,
+ ) {
+ Ok(mutation) => Some(mutation),
+ Err(()) => Some(ProjectionMutation::MarkIncomplete {
+ record_key,
+ profile: vocabulary::profile_v3(),
+ partition,
+ kind: ProjectionIncompleteKind::Dirty,
+ }),
}
} else if kind == SoupFlatEntityKind::Document {
Some(selected_document_projection_for_object(
@@ -254,7 +234,6 @@ fn walk_authoritative_object(
&field.selection_set,
field_meta.ty.name,
child,
- operation_kind,
mutations,
has_unbound_incomplete_entity,
),
@@ -265,7 +244,6 @@ fn walk_authoritative_object(
&field.selection_set,
field_meta.ty.name,
child,
- operation_kind,
mutations,
has_unbound_incomplete_entity,
);
@@ -355,6 +333,19 @@ fn insert_authoritative_mutation(
key: String,
mutation: ProjectionMutation,
) {
+ let mutation_is_empty_patch = matches!(
+ &mutation,
+ ProjectionMutation::Patch {
+ exact,
+ integers,
+ sorts,
+ ..
+ } if exact.is_empty() && integers.is_empty() && sorts.is_empty()
+ );
+ if mutation_is_empty_patch && mutations.contains_key(&key) {
+ return;
+ }
+
let existing_is_replace = matches!(mutations.get(&key), Some(ProjectionMutation::Replace(_)));
if matches!(mutation, ProjectionMutation::Replace(_)) || !existing_is_replace {
mutations.insert(key, mutation);
@@ -489,21 +480,6 @@ fn direct_projection_input_for_object(
})
}
-fn direct_projection_for_object(
- record_key: RecordKey,
- partition: Token,
- object: &serde_json::Map<String, serde_json::Value>,
- updated_at_fallback_ms: Option<i64>,
-) -> Option<IndexDocument> {
- project_direct_fields(direct_projection_input_for_object(
- record_key,
- &partition,
- object,
- updated_at_fallback_ms,
- )?)
- .ok()
-}
-
fn complete_v3_projection_for_object(
record_key: RecordKey,
partition: Token,
@@ -524,7 +500,7 @@ fn authoritative_v3_patch_for_object(
record_key: RecordKey,
partition: Token,
object: &serde_json::Map<String, serde_json::Value>,
-) -> Result<Option<ProjectionMutation>, ()> {
+) -> Result<ProjectionMutation, ()> {
let kind = projection_kind(&partition).ok_or(())?;
let project_field = if kind == SoupFlatEntityKind::Project {
"parentId"
@@ -537,7 +513,14 @@ fn authoritative_v3_patch_for_object(
|| (kind == SoupFlatEntityKind::Document
&& (object.contains_key("fileType") || object.contains_key("subType")));
if !has_direct_patch {
- return Ok(None);
+ return Ok(ProjectionMutation::Patch {
+ record_key,
+ profile: vocabulary::profile_v3(),
+ partition,
+ exact: Vec::new(),
+ integers: Vec::new(),
+ sorts: Vec::new(),
+ });
}
let patch = patch_direct_fields(DirectProjectionPatchInput {
@@ -587,14 +570,14 @@ fn authoritative_v3_patch_for_object(
values: document_sub_type_values(value)?,
});
}
- Ok(Some(ProjectionMutation::Patch {
+ Ok(ProjectionMutation::Patch {
record_key,
profile: vocabulary::profile_v3(),
partition,
exact,
integers,
sorts,
- }))
+ })
}
fn document_sub_type(value: &serde_json::Value) -> Result<Option<DocumentSubType>, ()> {
diff --git a/crates/soup_filter_cache_adapter/src/test.rs b/crates/soup_filter_cache_adapter/src/test.rs
index d2587e335a..8e1190a1b3 100644
--- a/crates/soup_filter_cache_adapter/src/test.rs
+++ b/crates/soup_filter_cache_adapter/src/test.rs
@@ -161,7 +161,7 @@ fn optimistic_mutations_keep_first_seen_order_and_deletion_precedence() {
}
#[test]
-fn authoritative_direct_fields_do_not_require_a_projection_schema_field() {
+fn authoritative_direct_fields_without_projection_schema_field_become_v3_patch() {
let query = r#"query LegacySoup {
user {
soup(input: { limit: 1 }) {
@@ -201,10 +201,179 @@ fn authoritative_direct_fields_do_not_require_a_projection_schema_field() {
.expect("legacy query parses");
assert!(matches!(
mutations.as_slice(),
- [ProjectionMutation::Replace(_)]
+ [ProjectionMutation::Patch { profile, .. }]
+ if profile == &vocabulary::profile_v3()
+ ));
+}
+
+#[test]
+fn partial_queries_preserve_v3_authority_and_mark_missing_v3() {
+ let id = "00000000-0000-0000-0000-000000000001";
+ let base_mutations = authoritative_projection_mutations(
+ SUPPLEMENT_SUBSCRIPTION,
+ Some("Supplement"),
+ &supplement_subscription_data(selected_document(
+ id,
+ serde_json::Value::String(document_supplement(id, false)),
+ serde_json::Value::Null,
+ )),
+ )
+ .unwrap();
+ let [ProjectionMutation::Replace(base)] = base_mutations.as_slice() else {
+ panic!("complete Soup data must hydrate v3 authority");
+ };
+ let base = base.clone();
+ let key = base.record_key.clone();
+
+ let query = r#"query SoupNotifications {
+ user {
+ soup(input: { limit: 500 }) {
+ items {
+ __typename
+ id
+ notifications { id }
+ }
+ }
+ }
+ }"#;
+ let mutations = authoritative_projection_mutations(
+ query,
+ Some("SoupNotifications"),
+ &serde_json::json!({
+ "user": {
+ "soup": {
+ "items": [{
+ "__typename": "GraphqlSoupDocument",
+ "id": id,
+ "notifications": []
+ }]
+ }
+ }
+ }),
+ )
+ .unwrap();
+ let [
+ ProjectionMutation::Patch {
+ profile,
+ exact,
+ integers,
+ sorts,
+ ..
+ },
+ ] = mutations.as_slice()
+ else {
+ panic!("a projection-less partial query must produce a bounded v3 patch");
+ };
+ assert_eq!(profile, &vocabulary::profile_v3());
+ assert!(exact.is_empty());
+ assert!(integers.is_empty());
+ assert!(sorts.is_empty());
+
+ let mut existing = std::collections::HashMap::from([(
+ key.clone(),
+ cache_core::predicate::ProjectionState::Complete(base.clone()),
+ )]);
+ cache_core::predicate::apply_authoritative_projection_mutations(&mut existing, &mutations);
+ assert_eq!(
+ existing.get(&key),
+ Some(&cache_core::predicate::ProjectionState::Complete(base))
+ );
+
+ let mut missing = std::collections::HashMap::new();
+ cache_core::predicate::apply_authoritative_projection_mutations(&mut missing, &mutations);
+ assert!(matches!(
+ missing.get(&key),
+ Some(cache_core::predicate::ProjectionState::Incomplete {
+ profile,
+ kind: ProjectionIncompleteKind::Missing,
+ ..
+ }) if profile == &vocabulary::profile_v3()
));
}
+#[test]
+fn empty_query_patch_does_not_overwrite_an_earlier_field_patch() {
+ let id = "00000000-0000-0000-0000-000000000001";
+ let query = r#"query RepeatedSoupEntity {
+ user {
+ rich: soup(input: { limit: 1 }) {
+ items {
+ __typename
+ id
+ ... on GraphqlSoupDocument {
+ ownerId
+ projectId
+ fileType
+ createdAt
+ updatedAt
+ subType { __typename }
+ }
+ }
+ }
+ partial: soup(input: { limit: 1 }) {
+ items {
+ __typename
+ id
+ notifications { id }
+ }
+ }
+ }
+ }"#;
+ let mutations = authoritative_projection_mutations(
+ query,
+ Some("RepeatedSoupEntity"),
+ &serde_json::json!({
+ "user": {
+ "rich": {
+ "items": [{
+ "__typename": "GraphqlSoupDocument",
+ "id": id,
+ "ownerId": "macro|[email protected]",
+ "projectId": null,
+ "fileType": "md",
+ "createdAt": "2025-01-01T00:00:00.000001Z",
+ "updatedAt": "2025-01-02T00:00:00.000001Z",
+ "subType": null
+ }]
+ },
+ "partial": {
+ "items": [{
+ "__typename": "GraphqlSoupDocument",
+ "id": id,
+ "notifications": []
+ }]
+ }
+ }
+ }),
+ )
+ .unwrap();
+ let [
+ ProjectionMutation::Patch {
+ exact,
+ integers,
+ sorts,
+ ..
+ },
+ ] = mutations.as_slice()
+ else {
+ panic!("the richer appearance must retain its field patch");
+ };
+ assert!(exact.iter().any(|patch| {
+ patch.attribute == vocabulary::owner()
+ && patch.values == vec![ExactValue::utf8("macro|[email protected]").unwrap()]
+ }));
+ assert!(
+ integers
+ .iter()
+ .any(|patch| patch.attribute == vocabulary::updated_at())
+ );
+ assert!(
+ sorts
+ .iter()
+ .any(|fact| fact.attribute == vocabulary::updated_at())
+ );
+}
+
const SUPPLEMENT_SUBSCRIPTION: &str = r#"subscription Supplement {
soupUpdates {
__typename
diff --git a/crates/soup_filter_projection/src/lib.rs b/crates/soup_filter_projection/src/lib.rs
index 7a23561fcb..1e6bb78012 100644
--- a/crates/soup_filter_projection/src/lib.rs
+++ b/crates/soup_filter_projection/src/lib.rs
@@ -202,62 +202,6 @@ pub fn project_direct_fields(
)
}
-/// Compose direct GraphQL facts and an optional server-only supplement into one
-/// canonical, validated `soup-flat-v2` projection.
-///
-/// Documents require exactly one supplement and obtain only their authoritative
-/// email-attachment fact from it. Projects and Chats are complete from direct
-/// fields alone and reject supplements.
-pub fn compose_soup_flat_v2(
- input: DirectProjectionInput,
- document_sub_type: Option<DocumentSubType>,
- supplement: Option<&SoupCacheProjectionSupplement>,
-) -> Result<IndexDocument, SoupFlatV2CompositionError> {
- let kind = input.kind;
- let expected_record_key = input.record_key.clone();
- let expected_partition = kind.partition();
- let mut document = project_direct_fields(input)?;
- document.profile = vocabulary::profile_v2();
-
- match kind {
- SoupFlatEntityKind::Document => {
- let supplement =
- supplement.ok_or(SoupFlatV2CompositionError::MissingDocumentSupplement)?;
- if supplement.record_key() != &expected_record_key {
- return Err(SoupFlatV2CompositionError::SupplementRecordKeyMismatch);
- }
- if supplement.target_profile() != &vocabulary::profile_v2() {
- return Err(SoupFlatV2CompositionError::SupplementTargetProfileMismatch);
- }
- if supplement.partition() != &expected_partition {
- return Err(SoupFlatV2CompositionError::SupplementPartitionMismatch);
- }
- if let Some(sub_type) = document_sub_type {
- document.exact_facts.push(utf8_fact(
- vocabulary::document_sub_type(),
- sub_type.to_string(),
- )?);
- }
- document.exact_facts.push(ExactFact {
- attribute: vocabulary::email_attachment(),
- value: ExactValue::new([u8::from(supplement.is_email_attachment())])?,
- });
- }
- SoupFlatEntityKind::Project | SoupFlatEntityKind::Chat => {
- if supplement.is_some() {
- return Err(SoupFlatV2CompositionError::UnexpectedSupplement);
- }
- if document_sub_type.is_some() {
- return Err(SoupFlatV2CompositionError::UnexpectedDocumentSubType);
- }
- }
- }
-
- document.canonicalize();
- validate_soup_flat_v2(&document)?;
- Ok(document)
-}
-
/// Compose direct GraphQL facts and a server-only supplement into one
/// canonical, validated `soup-flat-v3` projection.
///
diff --git a/crates/soup_filter_projection/src/test.rs b/crates/soup_filter_projection/src/test.rs
index 90f9eb9ce2..5fba42a346 100644
--- a/crates/soup_filter_projection/src/test.rs
+++ b/crates/soup_filter_projection/src/test.rs
@@ -62,37 +62,6 @@ fn document_key(id: Uuid) -> RecordKey {
RecordKey::new(format!("GraphqlSoupDocument:{id}")).unwrap()
}
-fn composed_v2_document(
- id: Uuid,
- sub_type: Option<&str>,
- is_email_attachment: bool,
-) -> IndexDocument {
- let document = document(id, None);
- let sub_type = sub_type.map(|value| match value {
- "task" => DocumentSubType::Task,
- "snippet" => DocumentSubType::Snippet,
- "skill" => DocumentSubType::Skill,
- value => panic!("unsupported test subtype {value}"),
- });
- let supplement =
- SoupCacheProjectionSupplement::document_v2(document_key(id), is_email_attachment);
- compose_soup_flat_v2(
- DirectProjectionInput {
- record_key: document_key(id),
- kind: SoupFlatEntityKind::Document,
- id: document.id,
- owner: document.owner_id.to_string(),
- project_id: document.project_id,
- file_type: document.file_type,
- created_at: document.created_at,
- updated_at: document.updated_at,
- },
- sub_type,
- Some(&supplement),
- )
- .unwrap()
-}
-
#[test]
fn document_projection_contains_only_direct_profile_facts() {
let id = Uuid::from_u128(1);
@@ -317,31 +286,6 @@ fn document_server_facts_attached_to_another_entity_are_rejected() {
));
}
-#[test]
-fn complete_v2_validation_remains_separate_from_the_supplement() {
- let projection = composed_v2_document(Uuid::from_u128(1), Some("task"), false);
- validate_soup_flat_v2(&projection).unwrap();
-
- let mut missing = projection.clone();
- missing
- .exact_facts
- .retain(|fact| fact.attribute != vocabulary::email_attachment());
- assert!(matches!(
- validate_soup_flat_v2(&missing),
- Err(ProfileValidationError::MissingRequired("email-attachment"))
- ));
-
- let mut duplicate = projection;
- duplicate.exact_facts.push(ExactFact {
- attribute: vocabulary::email_attachment(),
- value: ExactValue::new(vec![1]).unwrap(),
- });
- assert!(matches!(
- validate_soup_flat_v2(&duplicate),
- Err(ProfileValidationError::Duplicate("email-attachment"))
- ));
-}
-
#[test]
fn supplement_capsule_v2_native_golden_round_trip_is_deterministic() {
let id = Uuid::from_u128(1);