]> git.hungrycats.org Git - bees/commitdiff
scan_next: gate each Plan B dst on its own debt, not the aggregate
authorZygo Blaxell <bees@furryterror.org>
Thu, 9 Jul 2026 04:53:10 +0000 (00:53 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:03 +0000 (15:00 -0400)
Plan B executes each destination independently — each is a reversed Plan A
against the single start src — but its cost was evaluated only in aggregate: the
per-candidate debts were summed and the plan admitted if the sum beat
do-nothing.  That let a batch of profitable singleton dedupes subsidize an
individually-catastrophic drain.  The instrumentation showed it directly: one
Plan B emitted 130 dst lines sharing one aggregate debt, 129 of them 1-ref
extents (each a real space win) and one a 9912-ref extent whose own debt is
~+521k (redirecting 9912 references onto a fresh src, priced by ref-op-cost).
The group summed to -7197, so the fat drain rode along — re-canonicalizing a
near-canonical extent instead of leaving it to accumulate and retire.

Gate each candidate on its own signed space-debt at accept time: drop it unless
its per-entry debt beats the do-nothing floor (< 0).  The profitable singletons
are unaffected; the high-ref drain is declined, so the fat extent stays the
canonical src, keeps absorbing, and retires at refs-max as intended.  A
force-marked candidate bypasses the gate, mirroring the FullRewrite floor's
benefit-gate bypass, since force means "rewrite regardless of benefit".

This is the last of the reference-cluster cost-model defects: retirement
(refs_fetch) settles clusters, refs_created makes ref-op-cost price ref moves,
and this gate stops Plan B from subsidizing a drain past that price.

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

index 6b5dfd6c5e367cb6b01c18cb233d2fdd545b89d4..df78f15baf4556493af601cfbe4c25c6c2f69ba5 100644 (file)
@@ -2458,6 +2458,23 @@ BeesStartAsSrcPlan::process_candidate(
                entry.m_entry_cost.m_extents_delta  = debt.m_extents_delta;
                entry.m_entry_cost.m_physical_freed = debt.m_physical_freed;
        }
+
+       // Per-dst benefit gate.  Plan B executes each dst independently (each is a
+       // reversed Plan A against the single start src), so each must beat the
+       // do-nothing floor on its own.  The aggregate cost below sums the entries,
+       // which without this gate lets a batch of profitable singleton dedupes
+       // subsidize an individually-catastrophic drain — redirecting a high-ref
+       // extent's references onto a fresh src costs refs_created * ref-op-cost,
+       // which do-nothing avoids entirely, yet the group's summed debt can still
+       // be negative and carry the drain along.  Drop a candidate whose own signed
+       // debt does not beat do-nothing (>= 0); the profitable candidates are
+       // unaffected.  A force-marked candidate must be rewritten regardless, so it
+       // bypasses the gate (mirroring the FullRewrite floor's benefit-gate bypass).
+       if (!m_filter_cache->force_rewrite(candidate.bytenr())
+           && !(entry.m_entry_cost.space_debt(m_policy) < 0.0)) {
+               return;
+       }
+
        m_impl->m_entries.push_back(std::move(entry));
 
        // Aggregate from scratch — O(entries) per accept is fine because