]> git.hungrycats.org Git - bees/commitdiff
scan-next: do not map a dedupe pair across a gap in the src refs
authorZygo Blaxell <bees@furryterror.org>
Tue, 1 Sep 2026 09:30:58 +0000 (05:30 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:05 +0000 (15:00 -0400)
BeesExtentSlice::refs() walks forward from the slice start and is
permitted to skip regions that carry no ref, so the first leaf can begin
after the cursor.  scan_next_sweep_join_pairs assumed it never does:

const auto src_file_begin = src_ref.file_begin() + cursor - src_ref.begin();

With a gap, "cursor - src_ref.begin()" is an unsigned subtraction that
wraps, and adding it moves src_file_begin backwards by the size of the
gap.  The result is a plausible-looking file offset that points at data
the matcher never examined, so the pair is built against the wrong
bytes and FIDEDUPERANGE rejects it.  covered was also set on that path,
so the region with no src ref was reported as covered.

This was dormant: an instrumented baseline run over the reference
corpus recorded zero gaps in a million dedupe operations.  It stopped
being dormant once extent_data_fetch began leaving foreign refs
undetermined.  Callers skip refs with no geometry, so refs that used to
resolve -- to the wrong extent, silently -- now leave holes in the ref
coverage instead, and holes are exactly what produces a leaf beginning
after the cursor.  Measured on the same corpus: 40 gaps in 920k dedupes
with that change alone, and the NO Dedup rate rose from 17 to 45.

A gap means there is no src ref for [cursor, src_ref.begin()), so the
plan cannot be executed as planned.  Abandon it the same way a vanished
ref is abandoned, and count it as plan_src_gap.

Assisted-by: Claude-Code:claude-opus-5
Assisted-by: Claude-Code:claude-opus-5-5
src/bees-scan-next.cc

index 94136e1eb9b533bd86cdef8d30cd9f737bf802a5..fed770ccbb768903b7d1e077a06a5d2b127e526e 100644 (file)
@@ -874,6 +874,24 @@ scan_next_sweep_join_pairs(BeesContext &ctx,
                                        open_failed = true;
                                        return true; // Keep walking to try other overlapping fallbacks
                                }
+                               // BeesExtentSlice::refs() walks forward from the slice
+                               // start but may skip regions with no ref, so the first
+                               // leaf can begin AFTER the cursor.  The mapping below is
+                               // only valid when the leaf covers the cursor: with a gap,
+                               // "cursor - src_ref.begin()" is an unsigned subtraction
+                               // that wraps, and src_file_begin silently lands that many
+                               // bytes before the ref's real start.  The pair is then
+                               // built against data the matcher never looked at, and the
+                               // kernel rejects it as "NO Dedup!".
+                               //
+                               // There is no src ref for [cursor, src_ref.begin()), so
+                               // this plan cannot be executed as planned.  Leave covered
+                               // false and let the caller abandon it, exactly as it does
+                               // when a ref has vanished.
+                               if (src_ref.begin() > cursor) {
+                                       BEESCOUNT(plan_src_gap);
+                                       return false;
+                               }
                                const auto overlap_end = min(src_ref.end(), dst_ref.end());
                                const auto src_file_begin = src_ref.file_begin() + cursor - src_ref.begin();
                                const auto src_file_end = src_file_begin + overlap_end - cursor;