From fea0c90558b8e17ffe02ac168dd25eafce6007e3 Mon Sep 17 00:00:00 2001 From: jedarden Date: Sun, 24 May 2026 17:31:25 -0400 Subject: [PATCH] feat(reshard): tag shadow writes with _miroir_origin for CDC suppression (P5.1.b, miroir-uhj.1.2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 2 dual-hash dual-write now tags shadow documents with _miroir_origin: reshard_backfill so CDC suppresses them by default (plan §13.13). Live writes (old hash) remain untagged and are emitted normally to CDC. Changes: - prepare_dual_write_documents() now sets _miroir_origin on shadow docs - Added test verifying shadow docs have origin tag, live docs do not This prevents CDC double-publishing during reshard dual-write phase. Co-Authored-By: Claude Opus 4.7 --- crates/miroir-core/src/reshard.rs | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/miroir-core/src/reshard.rs b/crates/miroir-core/src/reshard.rs index 602a1cf..aabcb90 100644 --- a/crates/miroir-core/src/reshard.rs +++ b/crates/miroir-core/src/reshard.rs @@ -1775,6 +1775,8 @@ pub fn prepare_dual_write_documents( // Clone document for shadow index with new shard tag let mut shadow_doc = doc.clone(); shadow_doc["_miroir_shard"] = serde_json::json!(new_shard_id); + // Tag for CDC suppression (plan §13.13) + shadow_doc["_miroir_origin"] = serde_json::json!("reshard_backfill"); shadow_documents.push(shadow_doc); } @@ -1908,6 +1910,36 @@ mod tests_dual_write { assert_eq!(prep.live_documents.len(), 0); assert_eq!(prep.shadow_documents.len(), 0); } + + #[test] + fn prepare_dual_write_tags_shadow_with_reshard_backfill_origin() { + let documents = vec![json!({"id": "product:123", "name": "Widget"})]; + + let reshard_state = ReshardOperationState { + shadow_index: "products__reshard_128".to_string(), + old_shards: 64, + target_shards: 128, + phase: ReshardPhase::DualWriteActive, + started_at: 1000, + }; + + let prep = prepare_dual_write_documents(&documents, "id", &reshard_state); + + // Shadow documents should be tagged with _miroir_origin for CDC suppression + let shadow_doc = &prep.shadow_documents[0]; + assert_eq!( + shadow_doc.get("_miroir_origin"), + Some(&json!("reshard_backfill")), + "shadow documents should have _miroir_origin: reshard_backfill" + ); + + // Live documents should NOT have _miroir_origin (client writes are emitted) + let live_doc = &prep.live_documents[0]; + assert!( + live_doc.get("_miroir_origin").is_none(), + "live documents should not have _miroir_origin" + ); + } } #[cfg(test)]