Modern Apps git

← Modern-Apps.git

musicbrainz: make the search deferral path reachable in tests

Commit
9b903cc6b9eb309aad34c3adcfe4290cf4ca2496
Author
vayun-mathur <[email protected]>
When
2026-08-21 00:23 UTC (3 months ago)
Parents
2eb804a

Diff

scripts/musicbrainz/mb_ingest/src/reader.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/reader.rs b/scripts/musicbrainz/mb_ingest/src/reader.rsindex 7f5587faaa..bd6a300b71 100644--- a/scripts/musicbrainz/mb_ingest/src/reader.rs+++ b/scripts/musicbrainz/mb_ingest/src/reader.rs@@ -276,6 +276,8 @@ pub struct MbPack<'a> {     flags: u32,     dump_date: u32,     string_blocks: u32,+    /// See [`MbPack::with_posting_intersect_max`].+    posting_intersect_max: u64, }  impl fmt::Debug for MbPack<'_> {@@ -354,7 +356,15 @@ impl<'a> MbPack<'a> {             *slot = (off, len);         } -        let me = MbPack { bytes, dir, counts, flags, dump_date, string_blocks };+        let me = MbPack {+            bytes,+            dir,+            counts,+            flags,+            dump_date,+            string_blocks,+            posting_intersect_max: Self::POSTING_INTERSECT_MAX,+        };         if me.strings_compressed() {             // The index needs one offset per block plus a trailing sentinel; without             // it, no pool offset can be resolved at all.@@ -445,6 +455,26 @@ impl<'a> MbPack<'a> {         self.flags & pack::FLAG_STRINGS_COMPRESSED != 0     } +    /// Override the posting-list size above which a query token is verified against+    /// candidate text instead of intersected by postings.+    ///+    /// This exists to make the deferral path REACHABLE IN TESTS, and that is not a+    /// convenience. Two bugs have shipped in that path — `MAX_POSTINGS_PER_TOKEN`+    /// silently truncating common-word queries, and `searchable_text` omitting+    /// artist credits so credit-only matches were dropped — and neither was+    /// catchable, because the path only activates for tokens whose posting lists+    /// exceed millions of entries and no synthetic fixture is that large. Setting+    /// this to 0 forces every non-rarest token down the deferred path, so a+    /// 12-track fixture can exercise the code that only real corpora otherwise+    /// reach.+    ///+    /// Results MUST be identical either way: the two paths are two implementations+    /// of the same predicate, and any divergence is a bug in one of them.+    pub fn with_posting_intersect_max(mut self, max: u64) -> MbPack<'a> {+        self.posting_intersect_max = max;+        self+    }+     /// Section byte lengths, for a build report or a `/status` handler.     pub fn section_sizes(&self) -> Vec<(&'static str, u64)> {         pack::SECTION_NAMES@@ -1136,7 +1166,7 @@ impl<'a> MbPack<'a> {         // set is already small by the time we get there.         let mut deferred: Vec<&str> = Vec::new();         for (token, tlo, thi, cost) in expanded.iter().skip(1) {-            if *cost > Self::POSTING_INTERSECT_MAX {+            if *cost > self.posting_intersect_max {                 deferred.push(token);                 continue;             }
scripts/musicbrainz/mb_ingest/tests/round_trip.rs
diff --git a/scripts/musicbrainz/mb_ingest/tests/round_trip.rs b/scripts/musicbrainz/mb_ingest/tests/round_trip.rsindex eddaaaaf4b..f8d45c8818 100644--- a/scripts/musicbrainz/mb_ingest/tests/round_trip.rs+++ b/scripts/musicbrainz/mb_ingest/tests/round_trip.rs@@ -626,6 +626,94 @@ fn hammer_every_query(pack: &MbPack<'_>) {     } } +#[test]+fn the_deferred_search_path_agrees_with_the_intersected_one() {+    // The deferral path only activates for tokens whose posting lists run to+    // millions, so no fixture-sized query can reach it -- which is why TWO bugs+    // shipped there (MAX_POSTINGS_PER_TOKEN truncating common-word queries, and+    // searchable_text omitting artist credits). Forcing the threshold to 0 sends+    // every non-rarest token down that path, making it reachable here.+    let built = build_fixture(BuildOptions::default());+    let normal = MbPack::open(&built.bytes).expect("open");+    let deferred = MbPack::open(&built.bytes).expect("open").with_posting_intersect_max(0);++    let queries = [+        "dark side of the moon",+        "the dark side",+        "pink floyd",+        "björk homogenic",+        "pink floyd feat björk",+        "speak to me",+        "the moon",+        "various artists",+        "untitled weird title",+        "homogenic live",+    ];+    for q in queries {+        // Both paths implement the same predicate; any divergence is a bug in one.+        assert_eq!(+            normal.search_release_groups(q, 25),+            deferred.search_release_groups(q, 25),+            "release-group search diverged between paths for {q:?}"+        );+        assert_eq!(+            normal.search_artists(q, 25),+            deferred.search_artists(q, 25),+            "artist search diverged between paths for {q:?}"+        );+        assert_eq!(+            normal.search_recordings(q, 25),+            deferred.search_recordings(q, 25),+            "recording search diverged between paths for {q:?}"+        );+    }+}++#[test]+fn credit_only_matches_survive_the_deferred_path() {+    // This is the exact bug the user found: the builder indexes artist credits, but+    // the reader verified deferred tokens against title-only text, so a credit-only+    // match was silently dropped -- only for tokens common enough to defer, which no+    // fixture reached. With the threshold at 0, the fixture reaches it.+    let built = build_fixture(BuildOptions::default());+    let deferred = MbPack::open(&built.bytes).expect("open").with_posting_intersect_max(0);++    // "björk" appears in no release-group TITLE in the fixture -- only in credits --+    // and the second token forces the first down the deferred verification path.+    let hits = deferred.search_release_groups("björk homogenic", 25);+    let titles: Vec<String> = hits+        .iter()+        .map(|h| deferred.release_group(h.idx).unwrap().title.into_owned())+        .collect();+    assert!(+        titles.iter().any(|t| t == "Homogenic"),+        "a credit-only term must survive deferred verification, got {titles:?}"+    );++    // Same for recordings, whose credits are attached per recording.+    let hits = deferred.search_recordings("björk jóga", 25);+    let found: Vec<String> = hits+        .iter()+        .map(|h| deferred.recording(h.idx).unwrap().title.into_owned())+        .collect();+    assert!(+        found.iter().any(|t| t == "Jóga"),+        "a credit-only term must survive deferred verification on recordings, got {found:?}"+    );++    // And a credit term combined with a title term from the SAME entity, where the+    // credit is the deferred one.+    let hits = deferred.search_release_groups("pink floyd echoes", 25);+    let titles: Vec<String> = hits+        .iter()+        .map(|h| deferred.release_group(h.idx).unwrap().title.into_owned())+        .collect();+    assert!(+        titles.iter().any(|t| t.starts_with("Echoes")),+        "credit + title across the two paths must intersect, got {titles:?}"+    );+}+ #[test] fn truncated_pack_is_rejected_not_read() {     let built = build_fixture(BuildOptions::default());