]> git.hungrycats.org Git - bees/commitdiff
plan: report the dst sub-span of each selected edge
authorZygo Blaxell <bees@furryterror.org>
Wed, 2 Sep 2026 16:54:36 +0000 (12:54 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:06 +0000 (15:00 -0400)
The covering search's result reported only match indices, discarding
the dst sub-span each edge actually used.  When selected matches
overlap in dst space that information is not recoverable downstream:
scan_next_compose_plan_tree re-derives slices with a sorted-match
sweep and can emit from a different match, or at different boundaries,
than the search validated — the source of the residual once-only
compose-trap throws (259 in a full-corpus run).

Replace PlanSearchResult's index vector with PlanSearchEdge records
carrying the match index plus the used dst span.  The greedy fallback
records its [pos, best_end) step directly; the exact DP recovers the
span from the src slice already stored in the back-trace edge (the
slice is src_begin + (a - dst_begin), so the inverse is exact).

Behavior-neutral: the one consumer keeps reading only the match
index.  Follow-up commits make composition consume the spans.

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

index 8859882c07accffa2d3d9c4b138baa30bc52690c..539bc73e22678e53fe7a4fd6acc8061b050be3c2 100644 (file)
@@ -336,7 +336,8 @@ scan_next_plan_greedy(const PlanSearchInputs &in,
                                const uint64_t sa = m.m_src_begin + (pos - m.m_dst_begin);
                                used_src[m.m_src_key].emplace_back(
                                        sa, sa + (best_end - pos));
-                               result.m_selected_matches.push_back(best_idx);
+                               result.m_selected_edges.push_back(PlanSearchEdge{
+                                       best_idx, pos, best_end });
                                matched_blocks += block_count(pos, best_end);
                                ++dedupe_ops;
                                pos = best_end;
@@ -895,7 +896,15 @@ scan_next_plan_shortest_path(const PlanSearchInputs &in,
        for (auto it = edges.rbegin(); it != edges.rend(); ++it) {
                const auto &e = **it;
                if (e.m_dedupe) {
-                       result.m_selected_matches.push_back(e.m_match_idx);
+                       // Recover the edge's dst span from its recorded src slice:
+                       // the slice was computed as src_begin + (a - dst_begin), so
+                       // the inverse gives back [a, b) exactly.
+                       const auto &m = in.m_matches[e.m_match_idx];
+                       const uint64_t db = m.m_dst_begin
+                               + (e.m_src_begin - m.m_src_begin);
+                       result.m_selected_edges.push_back(PlanSearchEdge{
+                               e.m_match_idx, db,
+                               db + (e.m_src_end - e.m_src_begin) });
                } else {
                        result.m_copy_slices.push_back(
                                PlanSearchRegion{ e.m_copy_begin, e.m_copy_end });
index 634b5135554f1629830b86e8bdc6fad160c3dc74..8413954f101a444cd0ae1ac0ffbf8592b466dc3b 100644 (file)
@@ -221,13 +221,25 @@ struct PlanSearchInputs {
        uint64_t m_clone_alignment  = 4096;  ///< s_clone_alignment (physical credit)
 };
 
-/// One covering returned by the search.  m_selected_matches indexes into
-/// PlanSearchInputs::m_matches (the search's chosen dedupe edges, each a
-/// full match span); m_copy_slices are the leftover data gaps copied via
-/// the tempfile.  m_debt is the covering's signed space-debt, equal by
-/// construction to scan_next_dst_debt() of the ChosenPlan built from it.
+/// One selected dedupe edge of a covering: which candidate match, and the
+/// dst sub-span of it the covering actually uses.  The search may use
+/// disjoint sub-spans of dst-overlapping matches, so the edge's span — not
+/// the match's full span — is the authoritative statement of which dst
+/// bytes this edge serves.  The src slice follows from the span:
+/// src_begin + (m_dst_begin - match.m_dst_begin), same length.
+struct PlanSearchEdge {
+       size_t   m_match_idx = 0;   ///< index into PlanSearchInputs::m_matches
+       uint64_t m_dst_begin = 0;
+       uint64_t m_dst_end   = 0;
+};
+
+/// One covering returned by the search.  m_selected_edges are the chosen
+/// dedupe edges (match index + used dst sub-span); m_copy_slices are the
+/// leftover data gaps copied via the tempfile.  m_debt is the covering's
+/// signed space-debt, equal by construction to scan_next_dst_debt() of
+/// the ChosenPlan built from it.
 struct PlanSearchResult {
-       std::vector<size_t>           m_selected_matches;  ///< indices into m_matches
+       std::vector<PlanSearchEdge>   m_selected_edges;    ///< chosen dedupe edges
        std::vector<PlanSearchRegion> m_copy_slices;       ///< unclaimed copy gaps
        size_t m_matched_blocks = 0;
        double m_debt = 0.0;
index cef07868ca5a512ff6c8baad0a614df701fbf87e..10e673f17bafdf36778c6d7c1a76c5d342b04ca6 100644 (file)
@@ -1191,11 +1191,11 @@ scan_next_choose_match_plan(const BeesExtent &dst,
                return {};
        }
 
-       // Materialize the winning covering into the ChosenPlan.  Selected
-       // match indices index into in.m_matches, whose m_group_id is the flat
+       // Materialize the winning covering into the ChosenPlan.  Each edge's
+       // match index indexes into in.m_matches, whose m_group_id is the flat
        // index into `flat`.
-       for (const auto match_idx : found->m_selected_matches) {
-               const auto group_id = in.m_matches[match_idx].m_group_id;
+       for (const auto &edge : found->m_selected_edges) {
+               const auto group_id = in.m_matches[edge.m_match_idx].m_group_id;
                const auto &fm = flat[group_id];
                chosen.m_selected_groups.push_back(fm.m_group);
                chosen.m_selected_matches.push_back(*fm.m_match);
index bacbd98064c76a0d43071afb4048e15dcfece956..12f9b504f48d1fc0e5807cda175ec2d620470678 100644 (file)
@@ -518,7 +518,7 @@ result_recomputed_debt(const PlanSearchInputs &in,
        const BeesRewritePolicy &policy, const PlanSearchResult &r)
 {
        return scan_next_covering_debt(in, policy,
-               r.m_selected_matches.size(), r.m_copy_slices.size(),
+               r.m_selected_edges.size(), r.m_copy_slices.size(),
                r.m_matched_blocks);
 }
 
@@ -577,7 +577,7 @@ test_search_overlap_high_ref_count()
 
        const auto res = scan_next_plan_shortest_path(in, policy);
        assert(res.has_value());
-       assert(res->m_selected_matches.empty());  // no dedupe pays at 50 refs
+       assert(res->m_selected_edges.empty());  // no dedupe pays at 50 refs
        assert(res->m_matched_blocks == 0);
        assert(res->m_debt == result_recomputed_debt(in, policy, *res));
        const auto omin = oracle_min_debt(in, policy);
@@ -601,7 +601,7 @@ test_search_interior_split_left_as_copy()
        // Committing the interior match makes 3 ops (copy [0,5), dedupe [5,6),
        // copy [6,10)); leaving it is 1 copy op.  refs_added jumps by 50*2,
        // dwarfing the 4096-byte credit, so the search leaves it as copy.
-       assert(res->m_selected_matches.empty());
+       assert(res->m_selected_edges.empty());
        assert(res->m_copy_slices.size() == 1);
        assert(res->m_debt == result_recomputed_debt(in, policy, *res));
        assert(res->m_debt == *oracle_min_debt(in, policy));
@@ -611,7 +611,7 @@ test_search_interior_split_left_as_copy()
        const auto in1 = make_inputs(10, { {5, 6} }, /*ref=*/1);
        const auto res1 = scan_next_plan_shortest_path(in1, policy);
        assert(res1.has_value());
-       assert(res1->m_selected_matches.size() == 1);
+       assert(res1->m_selected_edges.size() == 1);
        assert(res1->m_debt == *oracle_min_debt(in1, policy));
 }
 
@@ -629,7 +629,7 @@ test_search_group_win()
 
        const auto res = scan_next_plan_shortest_path(in, policy);
        assert(res.has_value());
-       assert(res->m_selected_matches.size() == 2);  // both selected
+       assert(res->m_selected_edges.size() == 2);  // both selected
        assert(res->m_copy_slices.empty());           // nothing left to copy
        assert(res->m_matched_blocks == 8);
        assert(res->m_debt == result_recomputed_debt(in, policy, *res));
@@ -658,7 +658,7 @@ test_search_limit_pruning()
        // dedupe[0,4) is not a single match... so dedupe one 2-block match +
        // one copy = 2 ops), so the search returns something.
        assert(res.has_value());
-       const size_t total_ops = res->m_selected_matches.size()
+       const size_t total_ops = res->m_selected_edges.size()
                + res->m_copy_slices.size() + in.m_hole_ops;
        assert(total_ops <= policy.m_total_max);
        // And it matches the oracle minimum among limit-satisfying coverings.
@@ -710,7 +710,7 @@ test_search_brute_force_oracle()
                        // collapses the split); they are the acceptance gate's authority,
                        // so the search may return a covering that exceeds a per-type cap
                        // when total-max is set above it.
-                       const size_t total_ops = res->m_selected_matches.size()
+                       const size_t total_ops = res->m_selected_edges.size()
                                + res->m_copy_slices.size() + in.m_hole_ops;
                        assert(total_ops <= policy.m_total_max);
                }
@@ -737,7 +737,7 @@ test_search_compressed_all_or_nothing()
        const auto res = scan_next_plan_shortest_path(in_full, policy);
        assert(res.has_value());
        assert(res->m_copy_slices.empty());
-       assert(res->m_selected_matches.size() == 1);
+       assert(res->m_selected_edges.size() == 1);
        assert(res->m_debt == result_recomputed_debt(in_full, policy, *res));
 
        // force-rewrite lifts the compressed copy prohibition: a compressed dst
@@ -748,7 +748,7 @@ test_search_compressed_all_or_nothing()
        in_force.m_force_rewrite  = true;
        const auto rf = scan_next_plan_shortest_path(in_force, policy);
        assert(rf.has_value());
-       assert(rf->m_selected_matches.empty());
+       assert(rf->m_selected_edges.empty());
        assert(rf->m_copy_slices.size() == 1);          // one copy op, no fragment
        assert(rf->m_debt == result_recomputed_debt(in_force, policy, *rf));
 
@@ -842,8 +842,8 @@ test_search_v_budget_greedy_fallback()
        // cover is within the op limits.
        assert(greedy->m_matched_blocks == 20);
        assert(greedy->m_copy_slices.empty());
-       assert(greedy->m_selected_matches.size() == 3);
-       assert(greedy->m_selected_matches.size() <= policy.m_dedupe_max);
+       assert(greedy->m_selected_edges.size() == 3);
+       assert(greedy->m_selected_edges.size() <= policy.m_dedupe_max);
 
        // Debt reconciles by construction, and equals both the exact DP and the
        // oracle minimum — greedy is optimal on a coverable extent.
@@ -948,8 +948,8 @@ test_search_prefers_high_ref_src()
                policy.m_plan_max_vertices = 16384;
                const auto dp = scan_next_plan_shortest_path(in, policy);
                assert(dp.has_value());
-               assert(dp->m_selected_matches.size() == 1);
-               assert(dp->m_selected_matches[0] == hi);   // canonical src chosen
+               assert(dp->m_selected_edges.size() == 1);
+               assert(dp->m_selected_edges[0].m_match_idx == hi);   // canonical src chosen
                assert(dp->m_debt == result_recomputed_debt(in, policy, *dp));
 
                // Greedy fallback (tiny V budget forces it): same decision, and the
@@ -957,8 +957,8 @@ test_search_prefers_high_ref_src()
                policy.m_plan_max_vertices = 1;
                const auto gr = scan_next_plan_shortest_path(in, policy);
                assert(gr.has_value());
-               assert(gr->m_selected_matches.size() == 1);
-               assert(gr->m_selected_matches[0] == hi);
+               assert(gr->m_selected_edges.size() == 1);
+               assert(gr->m_selected_edges[0].m_match_idx == hi);
                assert(gr->m_debt == result_recomputed_debt(in, policy, *gr));
        }
 
@@ -973,8 +973,8 @@ test_search_prefers_high_ref_src()
                policy.m_plan_max_vertices = 16384;
                const auto dp = scan_next_plan_shortest_path(in, policy);
                assert(dp.has_value());
-               assert(dp->m_selected_matches.size() == 1);
-               assert(dp->m_selected_matches[0] == 0);   // full-span src wins on reach
+               assert(dp->m_selected_edges.size() == 1);
+               assert(dp->m_selected_edges[0].m_match_idx == 0);   // full-span src wins on reach
        }
 }