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(search): a zeroed message date no longer drops its whole email thread (#5437)

gbirman17cae6d818d6
diff --git a/crates/opensearch_client/src/date_format.rs b/crates/opensearch_client/src/date_format.rs
index d996c8a959..14309cc901 100644
--- a/crates/opensearch_client/src/date_format.rs
+++ b/crates/opensearch_client/src/date_format.rs
@@ -1,13 +1,12 @@
use crate::{Result, error::OpensearchClientError};
const MAX_REASONABLE_SECONDS: i64 = 32503680000; // Year ~3000
-const MIN_REASONABLE_SECONDS: i64 = 946684800; // Year 2000
-
const MAX_REASONABLE_MILLIS: i64 = MAX_REASONABLE_SECONDS * 1000;
-const MIN_REASONABLE_MILLIS: i64 = MIN_REASONABLE_SECONDS * 1000;
-/// A validated epoch-milliseconds timestamp, serialized as a raw `i64` for
-/// OpenSearch date fields mapped with `format: epoch_millis`.
+/// An epoch-milliseconds timestamp, serialized as a raw `i64` for OpenSearch
+/// date fields mapped with `format: epoch_millis`.
+///
+/// Zero and negative values are valid. Values past year 3000 are rejected.
#[derive(Debug, Clone, Copy, serde::Serialize)]
pub struct EpochMillis(i64);
@@ -21,17 +20,14 @@ impl EpochMillis {
),
});
}
- if millis < MIN_REASONABLE_MILLIS {
- return Err(OpensearchClientError::ValidationFailed {
- details: format!(
- "timestamp {} appears to be in seconds (before year 2000). Expected milliseconds since Unix epoch.",
- millis
- ),
- });
- }
Ok(Self(millis))
}
+ /// The timestamp, or `None` if it is rejected.
+ pub fn plausible(millis: i64) -> Option<Self> {
+ Self::new(millis).ok()
+ }
+
pub fn get(&self) -> i64 {
self.0
}
diff --git a/crates/opensearch_client/src/date_format/test.rs b/crates/opensearch_client/src/date_format/test.rs
index 11fc8d9cd6..4d341c1592 100644
--- a/crates/opensearch_client/src/date_format/test.rs
+++ b/crates/opensearch_client/src/date_format/test.rs
@@ -2,25 +2,28 @@ use super::*;
#[test]
fn test_epoch_millis_valid() {
- let now_millis = 1704067200123; // 2024-01-01 with sub-second precision
+ let now_millis = 1704067200123;
let epoch = EpochMillis::new(now_millis);
assert!(epoch.is_ok());
assert_eq!(epoch.unwrap().get(), now_millis);
}
#[test]
-fn test_epoch_millis_seconds() {
- let now_seconds = 1704067200; // 2024-01-01 in seconds
- let result = EpochMillis::new(now_seconds);
- assert!(result.is_err());
- if let Err(OpensearchClientError::ValidationFailed { details }) = result {
- assert!(details.contains("appears to be in seconds"));
- }
+fn test_epoch_millis_zero_and_negative_are_accepted() {
+ assert_eq!(EpochMillis::new(0).unwrap().get(), 0);
+ assert_eq!(EpochMillis::new(-1000).unwrap().get(), -1000);
+ assert_eq!(EpochMillis::new(-86_400_000).unwrap().get(), -86_400_000);
+}
+
+#[test]
+fn test_epoch_millis_seconds_are_accepted() {
+ let now_seconds = 1704067200;
+ assert_eq!(EpochMillis::new(now_seconds).unwrap().get(), now_seconds);
}
#[test]
fn test_epoch_millis_too_far_future() {
- let too_far = 32503680000001; // Just after year 3000 in millis
+ let too_far = 32503680000001;
let result = EpochMillis::new(too_far);
assert!(result.is_err());
if let Err(OpensearchClientError::ValidationFailed { details }) = result {
@@ -28,6 +31,18 @@ fn test_epoch_millis_too_far_future() {
}
}
+#[test]
+fn test_plausible_filters_only_future_garbage() {
+ assert!(EpochMillis::plausible(32503680000001).is_none());
+ assert_eq!(EpochMillis::plausible(0).map(|e| e.get()), Some(0));
+ assert_eq!(EpochMillis::plausible(-1000).map(|e| e.get()), Some(-1000));
+ let millis = 1704067200123;
+ assert_eq!(
+ EpochMillis::plausible(millis).map(|e| e.get()),
+ Some(millis)
+ );
+}
+
#[test]
fn test_epoch_millis_serialize() {
let epoch = EpochMillis::new(1704067200123).unwrap();
diff --git a/services/search_processing_service/src/process/email/upsert.rs b/services/search_processing_service/src/process/email/upsert.rs
index 9d50f17339..19cb1c1eb5 100644
--- a/services/search_processing_service/src/process/email/upsert.rs
+++ b/services/search_processing_service/src/process/email/upsert.rs
@@ -75,8 +75,7 @@ pub async fn process_upsert_message(
let updated_at_millis = message_info
.internal_date_ts
- .map(|date| EpochMillis::new(date.timestamp_millis()))
- .transpose()?
+ .and_then(|date| EpochMillis::plausible(date.timestamp_millis()))
.unwrap_or(now_millis);
// A full index overwrites the doc, so thread properties must ride along
@@ -145,8 +144,7 @@ pub async fn process_upsert_message(
updated_at_millis,
sent_at_millis: message_info
.internal_date_ts
- .map(|date| EpochMillis::new(date.timestamp_millis()))
- .transpose()?,
+ .and_then(|date| EpochMillis::plausible(date.timestamp_millis())),
properties,
};
@@ -215,12 +213,10 @@ pub async fn process_upsert_thread_message(
.context("expected content for upsertable email message")?;
let sent_at_millis = message
.internal_date_ts
- .map(|date| EpochMillis::new(date.timestamp_millis()))
- .transpose()?;
+ .and_then(|date| EpochMillis::plausible(date.timestamp_millis()));
let updated_at_millis = message
.internal_date_ts
- .map(|date| EpochMillis::new(date.timestamp_millis()))
- .transpose()?
+ .and_then(|date| EpochMillis::plausible(date.timestamp_millis()))
.unwrap_or(now_millis);
upsert_email_message_args.push(UpsertEmailArgs {
@@ -345,12 +341,10 @@ pub async fn process_upsert_thread_batch_message(
.context("expected content for upsertable email message")?;
let sent_at_millis = message
.internal_date_ts
- .map(|date| EpochMillis::new(date.timestamp_millis()))
- .transpose()?;
+ .and_then(|date| EpochMillis::plausible(date.timestamp_millis()));
let updated_at_millis = message
.internal_date_ts
- .map(|date| EpochMillis::new(date.timestamp_millis()))
- .transpose()?
+ .and_then(|date| EpochMillis::plausible(date.timestamp_millis()))
.unwrap_or(now_millis);
upsert_email_message_args.push(UpsertEmailArgs {