diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 9b9eb0a8..8d422789 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -128,9 +128,13 @@ int DraftPolicy::chain(int t_mtp, double p_mtp, int k_avail, int match) const { const double r = (e0 + p_mtp * gain) / cost_ms(t_mtp + k); if (r > best) { best = r; best_k = k; } } - if (best_k > 0 && best > base * (1.0 + margin_)) return best_k; - // as choose(): a size whose cost is only guessed is tried a few times when the continuation looks likely const int t_full = t_mtp + kmax; + if (best_k > 0 && best > base * (1.0 + margin_)) { + // As choose(): a profitable shorter chain must not keep a stale full size from being measured again. + if (best_k < kmax && p_mtp * c >= 0.6 && stale(t_full)) return kmax; + return best_k; + } + // as choose(): a size whose cost is only guessed is tried a few times when the continuation looks likely if ((cost_n_[t_full] < kProbes || stale(t_full)) && p_mtp * c >= 0.6) return kmax; return 0; } diff --git a/src/spec/draft_policy_test.cpp b/src/spec/draft_policy_test.cpp index 923fc79d..48a4f795 100644 --- a/src/spec/draft_policy_test.cpp +++ b/src/spec/draft_policy_test.cpp @@ -90,6 +90,30 @@ int main() { check(q.chain(4, 0.9, 3, 4) == 0, "chain: dear rows, never accepted -> none"); check(q.chain(4, 0.9, 0, 4) == 0 && q.chain(8, 1.0, 3, 40) == 0, "chain: nothing proposed / no room -> none"); } + { + // The stale-size check must also run when a shorter chain already pays. Synthetic costs isolate the + // selection policy: even a size that stays expensive is retried, but only once per stale interval. + DraftPolicy p(6); + for (int i = 0; i < 3; ++i) p.observe(false, 4, 3, 0, 40.0); + for (int i = 0; i < 3; ++i) p.observe_chain(4, 1, 4, 40, 42.0); + for (int i = 0; i < 3; ++i) p.observe_chain(4, 2, 5, 40, 1000.0); + check(p.chain(4, 1.0, 2, 40) == 1, "chain: the profitable shorter chain initially wins"); + int probes = 0, last_probe = -1; + bool spaced = true, confident = true; + for (int i = 0; i < 1000; ++i) { + const int k = p.chain(4, 1.0, 2, 40); + if (k == 2) { + ++probes; + spaced &= last_probe < 0 ? i >= 300 : i - last_probe > 300; + last_probe = i; + confident &= p.chain(4, 0.5, 2, 40) < 2; + } + p.observe_chain(4, k, 3 + k, 40, k == 2 ? 1000.0 : 42.0); + } + check(probes > 0 && probes <= 3 && spaced, + "chain: stale full size is re-probed despite a profitable shorter chain"); + check(probes > 0 && confident, "chain: stale re-probes retain the MTP confidence gate"); + } std::printf(g_fail ? "FAIL\n" : "PASS\n"); return g_fail ? 1 : 0; }