spec: a gated request's lookup policy learns from committed rounds only, DraftPolicy choose() unchanged otherwise (#3416)

Problem (KeyanHu-git): the gated proposal needs the choice between the MTP window and a
lookup window to be fixed by the history, not by the draws of the rounds it will verify.
Freezing the policy for the whole request (the first commit) does that, but also freezes
the cost table, so a repeat-heavy request kept the window sizes it inherited. In the
author's diagnostic workload the frozen policy chose mean lookup windows of 5.87 and 3.38
on two repeat requests; with this change 4.31 and 4.05 (from the PR text; not
reproduced here).

Change: the request's copy of the DraftPolicy (source_policy) gets three calls, used only
when gated_proposal is on:
- observe_committed(): the round's accept and probe counts, applied at once. A lookup
  window counts as a probe of its size (tried_), so a size whose cost is only guessed is
  tried kProbes times, not every round.
- defer_cost(end, t, ms): the round's time is held. A time covers the window and the next
  MTP draft, so it depends on every Philox counter up to
  max(p + T - 1, p + a + 1 + S_mtp).
- apply_costs(committed): applies, in order, the held times whose end is behind the
  committed position, with the same update observe() makes (including the STRATA_SPEC_COST
  clamp), in the context bucket the round ran in; delayed_n_ keeps a held time from
  counting as a second probe.
choose() counts the probes as cost_n_ - delayed_n_ + tried_.

Port note: the DraftPolicy on the current tree keeps its costs per context bucket
(STRATA_SPEC_COST), so tried_, delayed_n_ and the held times carry the bucket. A gated
request does not run with STRATA_SPEC_COST (it falls back to the older rule), so its
copy stays in bucket 0.

Default path: unchanged. choose() reads two new terms; on any path that never calls the new
functions tried_ and delayed_n_ are +0.0 and cost_n_ - 0.0 + 0.0 == cost_n_ exactly.
Checked by driving the old DraftPolicy (namespace-renamed copy of f-027-gh-eng) and the
new one with the same random call sequences (observe, observe_chain, observe_probs,
set_context, seed_costs, set_robust, choose, chain, keep_draft; 400 sequences of 3000
steps): 25,983,538 bitwise comparisons of picks, costs and rates, 0 mismatches; one
observe_committed call makes the same harness report 60,528 mismatches. generate.cpp:
nothing calls the new functions when gated_proposal is false.

Tests: draft_policy_test (the PR's added assertions, on the bucketed class) and
spec_admission_test pass on z440 and on the owner PC.
Not covered: the sampling argument for the delay boundary (that a held time cannot steer
a window through a counter that is drawn again) is the author's; a unit test cannot show
it, only the distribution run (gate script 3416-spec-prob-dist.sh, arm NEWLK) can.
This commit is contained in:
Keyan Hu authored and Niko1221 committed 2026-10-27 01:45:36 +01:00
1 parent 499e963593
commit ca17d0f5cd
4 files changed
+124 -5

No files matched your search

+21
View File
@@ -16,7 +16,9 @@
#pragma once
#include <array>
#include <cstdint>
#include <string>
#include <vector>
namespace strata::spec {
@@ -36,6 +38,20 @@ public:
Pick choose(int t_mtp, int lookup_k, int match) const;
/// After the round: the window it used, the drafts accepted, and the round's time (verify + commit + draft).
void observe(bool lookup, int t, int accepted, int match, double round_ms);
/// Gated proposals, inside one generation: a round's committed outcome only, never its time. `t` is the
/// lookup window chosen before the draws, or the configured MTP window; both are fixed by the history. A
/// lookup window counts as a probe of its size, so a size whose cost is still guessed is tried kProbes times,
/// not every round. Round times include the next window's drafts and the rows past the accepted ones, whose
/// Philox counters the next rounds draw again, so they wait (defer_cost) or go to the live policy (observe).
void observe_committed(bool lookup, int t, int accepted, int match);
/// Gated proposals, inside one generation: a round's time, held until the committed position passes `end`,
/// the last Philox counter of any draw that time depends on (the window's drafts and rows, the next window's
/// drafts when the time includes them). Until then those counters can be drawn again by the next rounds and the
/// time (rejected rows' routing, the next draft's gated length) would steer a window by its own draws.
void defer_cost(int64_t end, int t, double round_ms);
/// Apply, in order, the held times whose `end` is below `committed` (the next window's first counter), exactly
/// as observe() prices a round, in the context bucket the round ran in.
void apply_costs(int64_t committed);
/// --lookup-chain: how many of `k_avail` lookup tokens to append after an MTP window of `t_mtp` tokens whose
/// drafts all hold with probability `p_mtp` (estimated from the draft layer's own probabilities): the k with the
@@ -96,6 +112,11 @@ private:
static constexpr int kPBuckets = 8;
std::array<double, kPBuckets> pa_ok_{}, pa_n_{}; // MTP drafts reached / accepted by probability bucket (decayed)
std::array<std::array<double, kMaxT + 1>, kCtxBuckets> last_{}; // the round at which each size was last measured
// Gated proposals (observe_committed / defer_cost / apply_costs); all zero or empty on any other path.
std::array<std::array<double, kMaxT + 1>, kCtxBuckets> tried_{}; // lookup windows of that size run with no time yet
std::array<std::array<double, kMaxT + 1>, kCtxBuckets> delayed_n_{}; // of cost_n_: times applied by apply_costs (not new probes)
struct HeldCost { int64_t end; int bucket; int t; double ms; };
std::vector<HeldCost> held_; // defer_cost, oldest first
};
} // namespace strata::spec