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: suppress bom part failures for text extraction (#2259)

gbirman2aa6bb5ffada
diff --git a/rust/cloud-storage/document_text_extractor/src/handler/extract_text_citations.rs b/rust/cloud-storage/document_text_extractor/src/handler/extract_text_citations.rs
index e421da7d0d..b2fd23f70b 100644
--- a/rust/cloud-storage/document_text_extractor/src/handler/extract_text_citations.rs
+++ b/rust/cloud-storage/document_text_extractor/src/handler/extract_text_citations.rs
@@ -151,11 +151,16 @@ pub async fn extract_text_from_document(
db: Arc<service::db::DB>,
) -> Result<Option<DocumentKey>, Error> {
let document_key = DocumentKey::from_s3_key(key).map_err(|e| {
- tracing::error!(error=?e, "invalid key format");
+ tracing::error!(error=?e, key=%key, "invalid key format");
Error::from("invalid key format")
})?;
- let document_id = document_key.document_id();
+ if document_key.is_bom_part() {
+ tracing::trace!("skipping bom part key");
+ return Ok(None);
+ }
+
+ let document_id = document_key.document_id().expect("document key has id");
let file_type = if document_key.is_converted_pdf() {
Some(model::document::FileType::Pdf)
diff --git a/rust/cloud-storage/s3_key/src/document_key.rs b/rust/cloud-storage/s3_key/src/document_key.rs
index 69ee84aab4..f2e1259ed5 100644
--- a/rust/cloud-storage/s3_key/src/document_key.rs
+++ b/rust/cloud-storage/s3_key/src/document_key.rs
@@ -18,6 +18,7 @@ pub const DOCX_EXTENSION: &str = "docx";
/// - `Versioned`: `{user_id}/{document_id}/{version_id}` — a specific document version
/// - `ConvertedPdf`: `{user_id}/{document_id}/converted.pdf` — a DOCX converted to PDF
/// - `TempDocx`: `temp_files/{document_id}.docx` — a temporary DOCX export
+/// - `BomPart`: `{sha}` — a content-addressable BOM part from DOCX uploads
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum DocumentKey {
/// A versioned document: `{user_id}/{document_id}/{version_id}`
@@ -41,6 +42,17 @@ pub enum DocumentKey {
/// The document ID.
document_id: String,
},
+ /// A content-addressable BOM part from DOCX uploads: `{sha}`
+ BomPart {
+ /// The SHA hash of the BOM part.
+ sha: String,
+ },
+}
+
+const SHA256_HEX_LEN: usize = 64;
+
+fn is_sha256_hex(s: &str) -> bool {
+ s.len() == SHA256_HEX_LEN && s.bytes().all(|b| b.is_ascii_hexdigit())
}
impl DocumentKey {
@@ -82,16 +94,22 @@ impl DocumentKey {
})
}
}
- n => anyhow::bail!("invalid key format: expected 2 or 3 segments, got {n}"),
+ 1 if is_sha256_hex(split[0]) => Ok(Self::BomPart {
+ sha: split[0].to_string(),
+ }),
+ n => anyhow::bail!(
+ "invalid key format: expected 2 or 3 segments, got {n} for key '{key}'"
+ ),
}
}
- /// Returns the document ID for all key variants.
- pub fn document_id(&self) -> &str {
+ /// Returns the document ID for document key variants. Returns `None` for `BomPart`.
+ pub fn document_id(&self) -> Option<&str> {
match self {
Self::Versioned { document_id, .. }
| Self::ConvertedPdf { document_id, .. }
- | Self::TempDocx { document_id } => document_id,
+ | Self::TempDocx { document_id } => Some(document_id),
+ Self::BomPart { .. } => None,
}
}
@@ -105,6 +123,11 @@ impl DocumentKey {
matches!(self, Self::TempDocx { .. })
}
+ /// Returns `true` if this is a BOM part key.
+ pub fn is_bom_part(&self) -> bool {
+ matches!(self, Self::BomPart { .. })
+ }
+
/// Returns `true` if this is a converted DOCX-to-PDF key.
pub fn is_converted_pdf(&self) -> bool {
matches!(self, Self::ConvertedPdf { .. })
@@ -119,7 +142,7 @@ impl DocumentKey {
match self {
Self::Versioned { version_id, .. } => Some(version_id.to_string()),
Self::ConvertedPdf { .. } => Some(CONVERTED_DOCUMENT_FILE_NAME.to_string()),
- Self::TempDocx { .. } => None,
+ Self::TempDocx { .. } | Self::BomPart { .. } => None,
}
}
@@ -136,6 +159,7 @@ impl DocumentKey {
document_id,
} => build_docx_to_pdf_converted_document_key(user_id, document_id),
Self::TempDocx { document_id } => build_temp_docx_key(document_id),
+ Self::BomPart { sha } => sha.clone(),
}
}
}
diff --git a/rust/cloud-storage/s3_key/src/document_key/test.rs b/rust/cloud-storage/s3_key/src/document_key/test.rs
index 9c8b7849fe..bba10cf733 100644
--- a/rust/cloud-storage/s3_key/src/document_key/test.rs
+++ b/rust/cloud-storage/s3_key/src/document_key/test.rs
@@ -62,6 +62,27 @@ fn test_invalid_key_format() {
assert!(DocumentKey::from_s3_key("too/many/segments/here").is_err());
}
+#[test]
+fn test_bom_part_key() {
+ let key = DocumentKey::from_s3_key(
+ "7b5ce90c96ec3c24d8764ba75076bc0c2c5256b2d44e71cf9a8f001ea21ed678",
+ )
+ .unwrap();
+ assert_eq!(
+ key,
+ DocumentKey::BomPart {
+ sha: "7b5ce90c96ec3c24d8764ba75076bc0c2c5256b2d44e71cf9a8f001ea21ed678".to_string(),
+ }
+ );
+ assert!(key.is_bom_part());
+ assert_eq!(key.document_id(), None);
+ assert_eq!(key.version_id_string(), None);
+ assert_eq!(
+ key.to_key(),
+ "7b5ce90c96ec3c24d8764ba75076bc0c2c5256b2d44e71cf9a8f001ea21ed678"
+ );
+}
+
#[test]
fn test_invalid_version_id() {
assert!(DocumentKey::from_s3_key("user123/doc456/not_a_number").is_err());
@@ -76,13 +97,13 @@ fn test_invalid_temp_file_extension() {
#[test]
fn test_document_id_accessor() {
let versioned = DocumentKey::from_s3_key("user/doc1/1").unwrap();
- assert_eq!(versioned.document_id(), "doc1");
+ assert_eq!(versioned.document_id(), Some("doc1"));
let converted = DocumentKey::from_s3_key("user/doc2/converted.pdf").unwrap();
- assert_eq!(converted.document_id(), "doc2");
+ assert_eq!(converted.document_id(), Some("doc2"));
let temp = DocumentKey::from_s3_key("temp_files/doc3.docx").unwrap();
- assert_eq!(temp.document_id(), "doc3");
+ assert_eq!(temp.document_id(), Some("doc3"));
}
#[test]
diff --git a/rust/cloud-storage/search_upload_handler/src/handler.rs b/rust/cloud-storage/search_upload_handler/src/handler.rs
index 49b6d8bbd9..dea36f20f5 100644
--- a/rust/cloud-storage/search_upload_handler/src/handler.rs
+++ b/rust/cloud-storage/search_upload_handler/src/handler.rs
@@ -29,17 +29,17 @@ pub async fn handler(
let document_key = match DocumentKey::from_s3_key(&key) {
Ok(key) => key,
Err(e) => {
- tracing::warn!(error=?e, "unable to parse key");
+ tracing::warn!(error=?e, key=%key, "unable to parse key");
return Ok(());
}
};
- if document_key.is_temp() {
- tracing::trace!("skipping temp file");
+ if document_key.is_temp() || document_key.is_bom_part() {
+ tracing::trace!("skipping non-document key");
return Ok(());
}
- let document_id = document_key.document_id();
+ let document_id = document_key.document_id().expect("document key has id");
tracing::trace!(?document_key, "processing document key");
diff --git a/rust/cloud-storage/target/lambda/document_text_extractor/bootstrap.zip b/rust/cloud-storage/target/lambda/document_text_extractor/bootstrap.zip
index 5a9048d280..fe1c29c962 100644
Binary files a/rust/cloud-storage/target/lambda/document_text_extractor/bootstrap.zip and b/rust/cloud-storage/target/lambda/document_text_extractor/bootstrap.zip differ
diff --git a/rust/cloud-storage/target/lambda/search_upload_handler/bootstrap.zip b/rust/cloud-storage/target/lambda/search_upload_handler/bootstrap.zip
index a97e2e6388..bd9370f0e3 100644
Binary files a/rust/cloud-storage/target/lambda/search_upload_handler/bootstrap.zip and b/rust/cloud-storage/target/lambda/search_upload_handler/bootstrap.zip differ