@feigi/fleet-ctl 3.152.0 → 3.153.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feigi/fleet-ctl",
3
- "version": "3.152.0",
3
+ "version": "3.153.0",
4
4
  "description": "Agent fleet: run-team controller, merge bot, PR reviewer, and the ticket pipeline they share",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -190,14 +190,19 @@ const FIELD_WORDS = [
190
190
 
191
191
  // Merged PRs against `min_n`. A Cell is judged only while the baseline has
192
192
  // `min_n` merged PRs itself: below that the guard holds nothing back, whatever a
193
- // Cell's own n.
193
+ // Cell's own n. A Cell the guard lists `unmatched` has `min_n` of its own but is
194
+ // short of them, on its side or the baseline's, in the stretches both were drawn
195
+ // in, so it was not judged either.
194
196
  function mergedFigure(e, g, isBase) {
195
197
  const n = e.n;
196
198
  const min = g.min_n;
197
199
  const base = g.baseline.cell;
198
- const judged = isBase || g.verdict !== "none";
200
+ const unmatched = !isBase && Array.isArray(g.unmatched) && g.unmatched.includes(e.cell);
201
+ const judged = isBase || (g.verdict !== "none" && !unmatched);
199
202
  const text = n >= min
200
- ? judged ? `${n} ≥ ${min} ✓` : `${n} ≥ ${min}: not judged until ${base} has ${min}`
203
+ ? judged ? `${n} ≥ ${min} ✓`
204
+ : unmatched ? `${n} ≥ ${min}: not judged, under ${min} on a side in the stretches shared with ${base}`
205
+ : `${n} ≥ ${min}: not judged until ${base} has ${min}`
201
206
  : `${n} of ${min}: not judged`;
202
207
  const beside = [];
203
208
  const unchecked = [];
@@ -210,9 +215,11 @@ function mergedFigure(e, g, isBase) {
210
215
  }
211
216
 
212
217
  // The fail rate with its gap to the baseline's and to the floor, worked from the
213
- // claim and failure counts. Whole counts and the integer cross-product are what
214
- // the guard itself judges by (`underFloor`), so a gap near the floor reads the
215
- // way the guard rules; a rounded rate never enters it.
218
+ // claim and failure counts of the Cell's whole generation. Whole counts and the
219
+ // integer cross-product are what the guard itself judges by, so a rounded rate
220
+ // never enters it. The guard holds a Cell to the baseline only over the stretches
221
+ // both were drawn in, so a gap read from the whole generations can differ from
222
+ // the one the guard ruled on, and `tripped` is what it ruled.
216
223
  function failFigure(e, g, baseEntry, isBase) {
217
224
  const claims = count(e.n_claims);
218
225
  const failed = count(e.n_failed);
@@ -55,13 +55,27 @@
55
55
  // so a cell that abandons work carries that spend; `median_usd` over the
56
56
  // merged Pulls alone, descriptive; `router_usd`, the Pulls' own routing cost,
57
57
  // summed and never in their spend. `usd_diff_ci95` is a bootstrap 95% interval
58
- // on `mean_usd` minus the baseline's — printed to be read, never judged on.
59
- // Then the `left_out`, `unpriced` and `untimed` Pull counts, and the cell's
60
- // `excluded` count, which no generation owns. The baseline, the guard file's
61
- // cells and every trip are each cell's newest generation alone, so a cell
62
- // whose model changed is judged afresh once its new generation reaches MIN_N
63
- // merged PRs. Each older generation prints on its own line beside the newest,
64
- // with no verdict and no interval.
58
+ // on the $ per merged PR difference from the baseline, over the stretches in
59
+ // which both were drawn (TIME-MATCHED, below) — printed to be read, never
60
+ // judged on. Then the `left_out`, `unpriced` and `untimed` Pull counts, and the
61
+ // cell's `excluded` count, which no generation owns. The baseline, the guard
62
+ // file's cells and every trip are each cell's newest generation alone, so a
63
+ // cell whose model changed is judged afresh once its new generation reaches
64
+ // MIN_N merged PRs. Each older generation prints on its own line beside the
65
+ // newest, with no verdict and no interval.
66
+ //
67
+ // TIME-MATCHED. The figures above are each cell's own, over every Pull of its
68
+ // generation in the window. A trip is not read off them: a cell is compared
69
+ // with the baseline only over the stretches (pull-attribution.mjs's
70
+ // STRETCHES) in which both were drawn, each stretch compared on its own and
71
+ // the stretches pooled, so a shift that hits every cell at once cancels, and
72
+ // the baseline's other weeks, in which the cell never ran, move no verdict.
73
+ // The matched $ per merged PR counts a stretch's spend even where a side
74
+ // merged nothing in it, so a cell that abandons work carries that spend there
75
+ // too. MIN_N counts each side's merged PRs in those stretches. A cell with
76
+ // MIN_N merged PRs of its own that is short of them there, or shares no
77
+ // stretch with the baseline, is not judged and never trips: its line says
78
+ // `unmatched`, and the report's `unmatched` lists it.
65
79
  //
66
80
  // THE GUARD FILE keeps each cell's whole entry as the report computed it —
67
81
  // `n` (its `n_merged`, which readers key on) beside every figure above and its
@@ -74,8 +88,9 @@
74
88
  // list. `no_ruling_prs` and `pending_prs` name the PRs behind the last two,
75
89
  // ascending, as the report's top-level `pending` list does: a `no_ruling`
76
90
  // Pull contributes the implementer PRs that make it one. Beside `min_n` it
77
- // records `trip_points`, the floor's margin, and
78
- // `tsv_blobs`: the git blob id of each TSV it read, by the path it read it at.
91
+ // records `trip_points`, the floor's margin, `unmatched`, the report's list of
92
+ // the cells not judged, and `tsv_blobs`: the git blob id of each TSV it read,
93
+ // by the path it read it at.
79
94
 
80
95
  import { createHash } from "node:crypto";
81
96
  import { execFileSync } from "node:child_process";
@@ -87,7 +102,7 @@ import { gitEnv } from "./git-env.mjs";
87
102
  import { fleetFile, FleetDirUnresolvable } from "./fleet-dir.mjs";
88
103
  import { isCLI } from "./is-cli.mjs";
89
104
  import { parseTsv as parseMemberTsv } from "./member-outcomes.mjs";
90
- import { AttributionError, TRIP_POINTS, attribute, bootUnit, bootstrapDiffCI, parseFeatures, tally, underFloor } from "./pull-attribution.mjs";
105
+ import { AttributionError, TRIP_POINTS, attribute, compareCells, parseFeatures, tally } from "./pull-attribution.mjs";
91
106
  import { STARTING_CELLS } from "./ledger-grammar.mjs";
92
107
  import { RulingError, parseTierOutcomes } from "./tier-outcomes.mjs";
93
108
 
@@ -95,7 +110,9 @@ const NAME = "pr-cost";
95
110
  const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));
96
111
 
97
112
  // The guard's window opens here and never rolls: it is reset only by editing
98
- // this constant in a reviewed change.
113
+ // this constant in a reviewed change. It is the lower bound of the Pulls the
114
+ // guard reads; the baseline is not cumulative over it, for a cell is compared
115
+ // with the baseline only over the stretches in which both were drawn.
99
116
  export const WINDOW_START = "2026-10-03";
100
117
  export const BASELINE_CELL = "slow-high";
101
118
  export const MIN_N = 20;
@@ -150,17 +167,26 @@ function median(xs) {
150
167
  return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
151
168
  }
152
169
 
153
- // The trip rule: the quality floor (pull-attribution.mjs's `underFloor`, in
154
- // integer counts), or a mean $ at least the baseline's. Each side is
155
- // `{ claims, failed, mean_usd }`.
170
+ // Whether `m`, `compareCells`' comparison of a cell with the baseline, has MIN_N merged PRs on each side. They are
171
+ // counted over the stretches both were drawn in, so a cell is never judged on a handful of matched Pulls because
172
+ // it has many unmatched ones.
173
+ const judged = (m) => m.cell.merged >= MIN_N && m.base.merged >= MIN_N;
174
+
175
+ // The trip rule, over the stretches in which both cells were drawn (`cell` and `base` are the two cells' newest
176
+ // generations' counted Pulls, as `compareCells` reads them): the cell's fail rate TRIP_POINTS or more points above
177
+ // the baseline's, in integer counts, or its $ per merged PR at least the baseline's. The $ leg takes the unrounded
178
+ // figures: the cent-rounded ones are for display, and a trip is sticky, so a cell cheaper by under half a cent
179
+ // must not trip on rounding alone. A cell that shares no stretch with the baseline, or has under MIN_N merged PRs
180
+ // on either side in those it shares, is not judged and does not trip. `tripsOn` holds the rule on a comparison,
181
+ // `trips` on two cells' Pulls.
182
+ const tripsOn = (m) => judged(m) && (m.underFloor || (m.cell.usd_per_merged !== null && m.cell.usd_per_merged >= m.base.usd_per_merged));
156
183
  export function trips(cell, base) {
157
- return underFloor(cell, base) || cell.mean_usd >= base.mean_usd;
184
+ return tripsOn(compareCells(cell, base));
158
185
  }
159
186
 
160
187
  export function computeReport({ members, tiers, features, prs, routerTable = null, pricing = null, windowStart = WINDOW_START }) {
161
188
  const units = attribute({ features, members, tiers, prs, windowStart });
162
189
  const tallies = new Map(tally(units).map((t) => [t.cell, t]));
163
- const bootUnits = (g) => g.pulls.map((u) => bootUnit(u));
164
190
  const meanUsd = (t) => (t.merged ? t.spend / t.merged : null);
165
191
  const base = tallies.get(BASELINE_CELL) ?? null;
166
192
  // The PRs behind each generation's `no_ruling` and `pending` counts, as the
@@ -194,7 +220,7 @@ export function computeReport({ members, tiers, features, prs, routerTable = nul
194
220
  pending: g.pending,
195
221
  pending_prs: prList(held?.pending_prs),
196
222
  router_usd: round(g.pulls.reduce((s, u) => s + routerUsd(u.pull), 0), 4),
197
- usd_diff_ci95: !newest || cell === BASELINE_CELL || !base ? null : bootstrapDiffCI(bootUnits(g), bootUnits(base)),
223
+ usd_diff_ci95: !newest || cell === BASELINE_CELL || !base ? null : compareCells(g.pulls, base.pulls).range("usd"),
198
224
  left_out: g.left_out,
199
225
  unpriced: g.unpriced,
200
226
  untimed: g.untimed,
@@ -209,14 +235,17 @@ export function computeReport({ members, tiers, features, prs, routerTable = nul
209
235
  // The guard, over each cell's newest generation alone.
210
236
  const b = figures.find((f) => f.cell === BASELINE_CELL) ?? { generation: null, n_merged: 0, mean_usd: null, fail_rate: null };
211
237
  const baseline = { cell: BASELINE_CELL, generation: b.generation, n: b.n_merged, mean_usd: b.mean_usd, fail_rate: b.fail_rate };
212
- // The $ leg takes the unrounded means: the cent-rounded figures are for
213
- // display, and a trip is sticky, so a cell cheaper by under half a cent
214
- // must not trip on rounding alone. The fail leg reads the claims.
215
- const quality = (t) => ({ claims: t.claims, failed: t.failed, mean_usd: meanUsd(t) });
238
+ // Each cell is held to the baseline over the stretches in which both were drawn (`compareCells`): `judged` is
239
+ // whether MIN_N merged PRs a side there let the trip rule apply, and `tripsOn` the rule. A cell short of them is
240
+ // `unmatched`.
216
241
  const tripped = [];
242
+ const unmatched = [];
217
243
  if (baseline.n >= MIN_N) {
218
244
  for (const t of tallies.values()) {
219
- if (t.cell !== BASELINE_CELL && t.merged >= MIN_N && trips(quality(t), quality(base))) tripped.push(t.cell);
245
+ if (t.cell === BASELINE_CELL || t.merged < MIN_N) continue;
246
+ const m = compareCells(t.pulls, base.pulls);
247
+ if (!judged(m)) unmatched.push(t.cell);
248
+ else if (tripsOn(m)) tripped.push(t.cell);
220
249
  }
221
250
  }
222
251
  const verdict = baseline.n < MIN_N ? "none" : tripped.length ? "tripped" : "ok";
@@ -230,6 +259,7 @@ export function computeReport({ members, tiers, features, prs, routerTable = nul
230
259
  baseline,
231
260
  cells: figures,
232
261
  tripped,
262
+ unmatched,
233
263
  verdict,
234
264
  retire,
235
265
  pending: [...new Set(units.filter((u) => u.status === "pending").map((u) => u.pendingPr))].sort((x, y) => Number(x) - Number(y)),
@@ -295,12 +325,15 @@ const TSV_COLUMNS = [
295
325
  "excluded", "left_out", "unpriced", "untimed", "guard",
296
326
  ];
297
327
 
298
- // One line per cell's newest generation, carrying the guard's verdict on it,
299
- // then one per older generation of that cell, which carries none.
328
+ // One line per cell's newest generation, carrying the guard's verdict on it:
329
+ // `tripped`, `ok`, `unmatched` (merged PRs enough of its own, too few in the
330
+ // stretches it shares with the baseline to be judged) or `n<MIN_N`; then one
331
+ // per older generation of that cell, which carries none.
300
332
  export function formatReport(report) {
301
333
  const guardOf = (f) => {
302
334
  if (f.cell === BASELINE_CELL) return "baseline";
303
335
  if (report.tripped.includes(f.cell)) return "tripped";
336
+ if (report.unmatched.includes(f.cell)) return "unmatched";
304
337
  return f.n_merged >= MIN_N && report.verdict !== "none" ? "ok" : `n<${MIN_N}`;
305
338
  };
306
339
  const cell = (v) => (v === null || v === undefined ? "" : Array.isArray(v) ? `[${v.join(", ")}]` : String(v));
@@ -336,6 +369,7 @@ export function guardFile(report, computedAt, tsvBlobs = {}) {
336
369
  return { cell, generation, n: f.n_merged, ...rest };
337
370
  }),
338
371
  tripped: report.tripped,
372
+ unmatched: report.unmatched,
339
373
  verdict: report.verdict,
340
374
  retire: report.retire,
341
375
  // The n the baseline and every judged cell need, and the margin the fail
@@ -1,8 +1,9 @@
1
1
  // The one booking of member rows, merges and claims to Pulls, read by both the
2
2
  // cost guard (pr-cost.mjs) and the router's fit (ticket-router.mjs), so the two
3
3
  // print the same figures for a cell. Neither CLI imports the other; both import
4
- // this, and with it the two judgements they share: the quality floor (the
5
- // guard's fail leg) and the bootstrap range of a $ per merged PR difference.
4
+ // this, and with it the comparison of two cells they share: the quality floor
5
+ // (the guard's fail leg), the figures a row is judged by and the bootstrap range
6
+ // of a $ per merged PR difference.
6
7
  //
7
8
  // THE UNIT is the Pull, a ticket-features.tsv row, booked AS ROUTED to its
8
9
  // `chosen_cell`: its merge, its Ruling and all its spend. A ticket Pulled twice
@@ -79,9 +80,38 @@
79
80
  // earlier model rejoins that generation's Pulls. Only the newest generation's
80
81
  // figures are judged; older ones are reported beside them, never pooled.
81
82
  //
83
+ // STRETCHES. Two cells are compared only over the stretches in which both were
84
+ // drawn. A stretch is one UTC calendar week of `run_date`, Monday first, cut
85
+ // again wherever the recorded `plugin_version` changes. The cut is global: it
86
+ // is read off the sequence of every windowed Pull, by session id then file
87
+ // order, so every cell shares the same boundaries. A blank version is unknown
88
+ // and never itself a cut; only a change between two recorded, different
89
+ // versions cuts, before the Pull that records the newer one. `attribute` gives
90
+ // every unit its stretch.
91
+ //
92
+ // THE COMPARISON. `compareCells` holds two cells' newest-generation counted
93
+ // Pulls against each other over their shared stretches: each stretch on its
94
+ // own, then the stretches pooled with Mantel-Haenszel weights. A stretch
95
+ // weighs w_cell·w_base/(w_cell + w_base) over the counts w the figure is
96
+ // weighed by — Pulls for $ and minutes per merged PR, claims for the fail
97
+ // rate. A side's $ (or minutes) per merged PR is its spend per Pull over its
98
+ // merged PRs per Pull, each averaged over the stretches with those weights, so
99
+ // the spend of a stretch in which it merged nothing still counts; its fail
100
+ // rate is its stretches' fail rates averaged, a stretch in which either side
101
+ // has no claim weighing nothing. Every Pull of a shared stretch is kept, none
102
+ // filtered or decayed, and one in a stretch the other has none in takes no
103
+ // part. A shift that hits both cells in a stretch cancels, since the gap is
104
+ // read inside it, and a cell that ran in one stretch is held to the other's
105
+ // Pulls of that stretch alone. With one shared stretch the comparison is the
106
+ // cells' own figures. The bootstrap resamples within each stretch.
107
+ //
82
108
  // THE FLOOR. A cell is under the floor against a baseline when its fail rate
83
- // is TRIP_POINTS or more points above the baseline's, compared in integer
84
- // counts. The guard holds it pooled; the fit holds it per row.
109
+ // is TRIP_POINTS or more points above the baseline's over the stretches they
110
+ // share, compared in integers so a gap of exactly the margin reads as one:
111
+ // each stretch's cross-product of the counts, weighted as above, summed as
112
+ // exact fractions, which with one stretch is the cross-product of the counts.
113
+ // The guard holds it over every group's Pulls together; the fit holds it per
114
+ // row.
85
115
 
86
116
  import { CELL } from "./ledger-grammar.mjs";
87
117
  import { parseMemberName } from "./member-record.mjs";
@@ -113,12 +143,6 @@ export const OPTIONAL_COLUMNS = new Set(FEATURE_COLUMNS.slice(FEATURE_COLUMNS.in
113
143
  // The quality floor's margin, in points of fail rate.
114
144
  export const TRIP_POINTS = 15;
115
145
 
116
- // The floor, in integer counts so a 3-of-20 difference is exactly 15 points
117
- // rather than 14.999… in floating point. Each side is `{ claims, failed }`.
118
- export function underFloor(cell, base) {
119
- return (cell.failed * base.claims - base.failed * cell.claims) * 100 >= TRIP_POINTS * cell.claims * base.claims;
120
- }
121
-
122
146
  const round2 = (x) => (Number.isFinite(x) ? Number(x.toFixed(2)) : null);
123
147
 
124
148
  // Deterministic, so a re-run reads the same range.
@@ -133,38 +157,148 @@ function mulberry32(seed) {
133
157
  };
134
158
  }
135
159
 
136
- // The bootstrap 95% range of (cell − base) per merged PR, its ends rounded to
137
- // two places. Each unit is one counted Pull, `bootUnit`'s shape:
138
- // `{ value, merged }`, `value` the measure `bootUnit` was asked for and
139
- // merged 0 or 1. The figure per merged PR is Σvalue / Σmerged. null when
140
- // either side has no unit, or when under half the resamples have a merged PR
141
- // on both sides.
160
+ // What a unit measures: its `value` over its `per` is the figure, $ or minutes of work time per merged PR, or the
161
+ // fail rate in points per claim; `weigh` is what it adds to the count its stretch is weighed by, one Pull or its
162
+ // claims.
163
+ const MEASURES = Object.freeze({
164
+ usd: { value: (u) => u.spend, per: (u) => (u.merged ? 1 : 0), weigh: () => 1 },
165
+ minutes: { value: (u) => u.workS / 60, per: (u) => (u.merged ? 1 : 0), weigh: () => 1 },
166
+ fail: { value: (u) => 100 * u.falseClaims, per: (u) => u.claims, weigh: (u) => u.claims },
167
+ });
168
+
169
+ // A counted Pull as a unit of `measure`, one of MEASURES: `{ value, per, weigh, stretch }`.
170
+ export const bootUnit = (u, measure = "usd") => {
171
+ const m = MEASURES[measure];
172
+ return { value: m.value(u), per: m.per(u), weigh: m.weigh(u), stretch: u.stretch };
173
+ };
174
+
175
+ // The stretches in which both sides have a unit, as `{ cell, base }` lists of those units, in the order the cell first meets them.
176
+ function sharedStretches(cellUnits, baseUnits) {
177
+ const byStretch = new Map();
178
+ for (const [side, units] of [["cell", cellUnits], ["base", baseUnits]]) {
179
+ for (const u of units) {
180
+ const held = byStretch.get(u.stretch) ?? byStretch.set(u.stretch, { cell: [], base: [] }).get(u.stretch);
181
+ held[side].push(u);
182
+ }
183
+ }
184
+ return [...byStretch.values()].filter((s) => s.cell.length > 0 && s.base.length > 0);
185
+ }
186
+
187
+ // One side's units in a stretch as `{ v, d, w }`: their values, their `per`s and their `weigh`s summed.
188
+ const sumOf = (units) => units.reduce((s, u) => ({ v: s.v + u.value, d: s.d + u.per, w: s.w + u.weigh }), { v: 0, d: 0, w: 0 });
189
+
190
+ // The two sides' figures pooled over the stretches: `sums` holds one `{ cell, base }` of `{ v, d, w }` per stretch.
191
+ // A stretch is weighted by w_cell·w_base/(w_cell + w_base), what the thinner side lets it weigh, one in which
192
+ // either side has no w taking no part, and a side's figure is its v per w over its d per w, each averaged with
193
+ // those weights: with w the d, the average of the stretches' v/d. A side's v and d of a stretch are scaled by
194
+ // its weight over its w, taken relative to the first stretch's, so that with one stretch the figure is v/d
195
+ // exactly. null when no stretch takes part, or when a side has no d in those that do.
196
+ function pool(sums) {
197
+ const weights = sums.map(({ cell, base }) => (cell.w > 0 && base.w > 0 ? (cell.w * base.w) / (cell.w + base.w) : 0));
198
+ const figure = (side) => {
199
+ let v = 0, d = 0, first = null;
200
+ sums.forEach((s, k) => {
201
+ if (weights[k] === 0) return;
202
+ const scale = weights[k] / s[side].w;
203
+ first ??= scale;
204
+ v += (scale / first) * s[side].v;
205
+ d += (scale / first) * s[side].d;
206
+ });
207
+ return d > 0 ? v / d : null;
208
+ };
209
+ const [cell, base] = [figure("cell"), figure("base")];
210
+ return cell === null || base === null ? null : { cell, base };
211
+ }
212
+
213
+ // The bootstrap 95% range of (cell − base), its ends rounded to two places, over the stretches in which both
214
+ // sides have units. Each stretch's units are resampled among themselves and the stretches pooled as `pool` pools
215
+ // them, so a shift in a stretch that hits both sides cancels in every resample. A unit is one counted Pull,
216
+ // `bootUnit`'s shape `{ value, per, weigh, stretch }`; units with no stretch are in one together. null when no
217
+ // stretch is shared, or when under half the resamples have a figure on both sides.
142
218
  export function bootstrapDiffCI(cellUnits, baseUnits, { iterations = 2000, seed = 1 } = {}) {
143
- if (!cellUnits.length || !baseUnits.length) return null;
219
+ const shared = sharedStretches(cellUnits, baseUnits);
220
+ if (shared.length === 0) return null;
144
221
  const rand = mulberry32(seed);
145
222
  const resample = (units) => {
146
- let u = 0, m = 0;
223
+ let v = 0, d = 0, w = 0;
147
224
  for (let i = 0; i < units.length; i++) {
148
225
  const x = units[Math.floor(rand() * units.length)];
149
- u += x.value; m += x.merged;
226
+ v += x.value; d += x.per; w += x.weigh;
150
227
  }
151
- return m ? u / m : null;
228
+ return { v, d, w };
152
229
  };
153
230
  const diffs = [];
154
231
  for (let i = 0; i < iterations; i++) {
155
- const a = resample(cellUnits), b = resample(baseUnits);
156
- if (a !== null && b !== null) diffs.push(a - b);
232
+ const cell = shared.map((s) => resample(s.cell));
233
+ const base = shared.map((s) => resample(s.base));
234
+ const pooled = pool(shared.map((_, k) => ({ cell: cell[k], base: base[k] })));
235
+ if (pooled !== null) diffs.push(pooled.cell - pooled.base);
157
236
  }
158
237
  if (diffs.length < iterations / 2) return null;
159
238
  diffs.sort((x, y) => x - y);
160
239
  return [round2(diffs[Math.floor(0.025 * (diffs.length - 1))]), round2(diffs[Math.ceil(0.975 * (diffs.length - 1))])];
161
240
  }
162
241
 
163
- // What a bootstrap unit measures: `usd`, its spend, or `minutes`, its work time.
164
- const METRIC = Object.freeze({ usd: (u) => u.spend, minutes: (u) => u.workS / 60 });
242
+ const countsOf = (units) => ({
243
+ n: units.length,
244
+ merged: units.filter((u) => u.merged).length,
245
+ claims: units.reduce((n, u) => n + u.claims, 0),
246
+ failed: units.reduce((n, u) => n + u.falseClaims, 0),
247
+ });
165
248
 
166
- // A counted Pull as a bootstrap unit, its `value` the Pull's `metric`.
167
- export const bootUnit = (u, metric = "usd") => ({ value: METRIC[metric](u), merged: u.merged ? 1 : 0 });
249
+ // Whether the pooled fail-rate gap reaches TRIP_POINTS, in integers so a gap of exactly the margin reads as one. A
250
+ // stretch adds (failed_cell·claims_base − failed_base·claims_cell)/(claims_cell + claims_base) to the gap and
251
+ // claims_cell·claims_base/(claims_cell + claims_base) to the weight `pool` gives it, a stretch in which either side
252
+ // has no claim adding nothing: with one stretch, the cross-product test of the counts. false when no stretch has a
253
+ // claim on both sides.
254
+ function reachesMargin(shared) {
255
+ let gap = [0n, 1n];
256
+ let weight = [0n, 1n];
257
+ for (const s of shared) {
258
+ const [c, b] = [countsOf(s.cell), countsOf(s.base)];
259
+ if (c.claims === 0 || b.claims === 0) continue;
260
+ const n = BigInt(c.claims + b.claims);
261
+ gap = [gap[0] * n + BigInt(c.failed * b.claims - b.failed * c.claims) * gap[1], gap[1] * n];
262
+ weight = [weight[0] * n + BigInt(c.claims * b.claims) * weight[1], weight[1] * n];
263
+ }
264
+ return weight[0] > 0n && 100n * gap[0] * weight[1] >= BigInt(TRIP_POINTS) * weight[0] * gap[1];
265
+ }
266
+
267
+ /**
268
+ * Two cells' newest-generation counted Pulls compared over the stretches in which both were drawn, the one
269
+ * comparison the fit's rows, its floor and the cost guard make. Within each shared stretch the two cells are
270
+ * compared, and the stretches are pooled by `pool`, so a shift that hits both cells in a stretch cancels and
271
+ * every Pull of a shared stretch is kept, its spend and work time in the $ and minutes per merged PR even where
272
+ * its side merged nothing in that stretch. A cell's Pulls in a stretch the other has none in take no part.
273
+ * Returns `{ stretches, cell, base, underFloor, range }`: `stretches`, how many were shared; `cell` and `base`,
274
+ * each side's counts over the shared stretches (`n`, `merged`, `claims`, `failed`) beside its figures pooled
275
+ * over them (`usd_per_merged`, `minutes_per_merged`, `fail_rate`, null where a side has no merged PR or no
276
+ * claim to carry one);
277
+ * `underFloor`, whether the cell's fail rate is TRIP_POINTS or more points above the base's, pooled; and
278
+ * `range(measure)`, the bootstrap range of (cell − base) for `measure`, one of `usd`, `minutes` (per merged
279
+ * PR) and `fail` (points), null where there is none. With one shared stretch these are the Pulls' own figures,
280
+ * the integer cross-product of their counts and the bootstrap of their units.
281
+ */
282
+ export function compareCells(cellPulls, basePulls) {
283
+ const shared = sharedStretches(cellPulls, basePulls);
284
+ const pooled = Object.fromEntries(Object.keys(MEASURES).map((measure) => [measure, pool(shared.map((s) => ({
285
+ cell: sumOf(s.cell.map((u) => bootUnit(u, measure))),
286
+ base: sumOf(s.base.map((u) => bootUnit(u, measure))),
287
+ })))]));
288
+ const side = (which) => ({
289
+ ...countsOf(shared.flatMap((s) => s[which])),
290
+ usd_per_merged: pooled.usd?.[which] ?? null,
291
+ minutes_per_merged: pooled.minutes?.[which] ?? null,
292
+ fail_rate: pooled.fail === null ? null : pooled.fail[which] / 100,
293
+ });
294
+ return {
295
+ stretches: shared.length,
296
+ cell: side("cell"),
297
+ base: side("base"),
298
+ underFloor: reachesMargin(shared),
299
+ range: (measure) => bootstrapDiffCI(cellPulls.map((u) => bootUnit(u, measure)), basePulls.map((u) => bootUnit(u, measure))),
300
+ };
301
+ }
168
302
 
169
303
  const ISSUE_NUMBER = /^[1-9][0-9]*$/;
170
304
 
@@ -283,9 +417,41 @@ function numberOf(row, field, column = field) {
283
417
  return n;
284
418
  }
285
419
 
420
+ const DAY_MS = 86_400_000;
421
+
422
+ // The Monday opening the UTC calendar week that `date` (YYYY-MM-DD) falls in, in the same form.
423
+ function weekOf(date) {
424
+ const [y, m, d] = date.split("-").map(Number);
425
+ const t = Date.UTC(y, m - 1, d);
426
+ return new Date(t - ((new Date(t).getUTCDay() + 6) % 7) * DAY_MS).toISOString().slice(0, 10);
427
+ }
428
+
429
+ /**
430
+ * Each of `pulls` to its stretch, a name: `<the Monday opening its run_date's UTC week>/<k>`, `k` the number of
431
+ * version changes before it. The changes are read off the sequence of every one of `pulls` — by session id, then
432
+ * in the order given, as `generations` orders them — so every cell is cut at the same places. Only a change
433
+ * between two recorded, different `plugin_version`s cuts, before the Pull that records the newer one: a blank
434
+ * version is unknown, leaves the stretch in force, and the first version recorded cuts nothing.
435
+ */
436
+ function stretchesOf(pulls) {
437
+ const sequence = pulls.map((pull, at) => ({ pull, at })).sort((a, b) => cmp(a.pull.session, b.pull.session) || a.at - b.at);
438
+ const stretch = new Map();
439
+ let changes = 0;
440
+ let version = "";
441
+ for (const { pull } of sequence) {
442
+ const recorded = pull.plugin_version ?? "";
443
+ if (recorded !== "") {
444
+ if (version !== "" && recorded !== version) changes++;
445
+ version = recorded;
446
+ }
447
+ stretch.set(pull, `${weekOf(pull.run_date)}/${changes}`);
448
+ }
449
+ return stretch;
450
+ }
451
+
286
452
  /**
287
453
  * One unit per ticket-features row dated on or after `windowStart` (every dated
288
- * row when null), in file order: `{ pull, cell, stratum, generation, status,
454
+ * row when null), in file order: `{ pull, cell, stratum, stretch, generation, status,
289
455
  * merged, pr, ruling, pendingPr, noRuling, noRulingPrs, claims, falseClaims, rows, spend, workS }`,
290
456
  * `generation` null exactly when the Pull is excluded. `noRulingPrs` are the
291
457
  * Pull's own implementer PRs that are merged and that no Pull holds a Ruling
@@ -294,7 +460,8 @@ function numberOf(row, field, column = field) {
294
460
  * booked `cost` is blank, and `workS`, its work time in seconds, when a booked
295
461
  * `work_s` of a row naming no parent is. `prs` is `gh pr list`'s
296
462
  * `{ number, state }` rows, or null when PR state is unknown (the fit). `stratum`
297
- * maps a features row to its stratum. Throws `AttributionError` on a features
463
+ * maps a features row to its stratum. `stretch` names the Pull's stretch among
464
+ * the windowed Pulls (`stretchesOf`), excluded Pulls included. Throws `AttributionError` on a features
298
465
  * row it cannot date or a booked `cost` or `work_s` it cannot read, and
299
466
  * `RulingError` as `rulingFor` does.
300
467
  */
@@ -327,6 +494,7 @@ export function attribute({ features, members, tiers, prs = null, windowStart =
327
494
 
328
495
  // Rulings: each PR's merge to one Pull, carriers first, then the date floor.
329
496
  const inWindow = pulls.filter((p) => windowStart === null || p.run_date >= windowStart);
497
+ const stretch = stretchesOf(inWindow);
330
498
  const rulings = rulingsByTicket(tiers);
331
499
  const fileOrder = new Map(tiers.map((t, i) => [t, i]));
332
500
  // Each PR to its latest ruling row in file order, whatever its ticket or date.
@@ -402,7 +570,7 @@ export function attribute({ features, members, tiers, prs = null, windowStart =
402
570
  else if (blank || (r && !r.carried)) status = "unpriced";
403
571
  else if (untimed) status = "untimed";
404
572
  return {
405
- pull, cell: pull.chosen_cell, stratum: stratum(pull), generation, status, merged, pr: r?.pr ?? null, ruling: r?.ruling ?? null,
573
+ pull, cell: pull.chosen_cell, stratum: stratum(pull), stretch: stretch.get(pull), generation, status, merged, pr: r?.pr ?? null, ruling: r?.ruling ?? null,
406
574
  noRuling: noRulingPrs.length > 0, noRulingPrs, pendingPr, claims, falseClaims, rows: booked, spend: blank ? null : spend, workS: untimed ? null : workS,
407
575
  };
408
576
  });
@@ -63,7 +63,7 @@ import { CELL, OBJECTIVES, POLICY_CELL, STARTING_CELLS, drawCell, formatRouting,
63
63
  import { objectiveFromReceipt, sessionFromReceipt } from "./phase0-receipt.mjs";
64
64
  import { parseTsv as parseMemberTsv } from "./member-outcomes.mjs";
65
65
  import {
66
- AttributionError, FEATURE_COLUMNS, OPTIONAL_COLUMNS, TRIP_POINTS, attribute, bootUnit, bootstrapDiffCI, generations, parseFeatures, tally, underFloor,
66
+ AttributionError, FEATURE_COLUMNS, OPTIONAL_COLUMNS, TRIP_POINTS, attribute, compareCells, generations, parseFeatures, tally,
67
67
  } from "./pull-attribution.mjs";
68
68
  import { RulingError, parseTierOutcomes } from "./tier-outcomes.mjs";
69
69
  import { LINE_REF, briefPaths, briefText } from "./ticket-text.mjs";
@@ -734,10 +734,11 @@ function estimate(units, order, nodes) {
734
734
  }
735
735
 
736
736
  // What each objective's judge reads and says: the figure per merged PR it
737
- // minimises (`estimate()`'s name and its bootstrap metric), its verdict for a
738
- // candidate past the margin and for one with no reliable difference, the key
739
- // a pair names its better side by, and its reasons for a winner every other
740
- // qualifier was reliably worse than and for one ahead on the figure.
737
+ // minimises (a `compareCells` side's name for it) and the measure its range is
738
+ // read in (`range()`'s), its verdict for a candidate past the margin and for
739
+ // one with no reliable difference, the key a pair names its better side by,
740
+ // and its reasons for a winner every other qualifier was reliably worse than
741
+ // and for one ahead on the figure.
741
742
  const JUDGE = Object.freeze({
742
743
  cost: {
743
744
  figure: "usd_per_merged", metric: "usd", over: "too-dear", unreliable: "not-reliably-cheaper",
@@ -754,35 +755,45 @@ const JUDGE = Object.freeze({
754
755
  const unjudged = () => ({ cell: null, reason: "unjudged", cells: {}, pairs: [] });
755
756
 
756
757
  /**
757
- * One row's cell under one objective — a node's of the tree — and
758
- * why. The quality floor is held twice: pooled by the guard, whose trip takes
759
- * a cell out of every row and out of the exploration draw, and per row by the
760
- * fit, which refuses a cell under the floor that row alone. The floor is
761
- * measured against the default cell under both objectives. Every figure read
762
- * is the cell's newest generation's, the default cell's included. `est` and
763
- * `pulls` are the row's `estimate()` figures and counted Pulls; `baseCell` is
764
- * the cell the row holds unless a candidate qualifies: the default cell under
765
- * `cost`, the `cost` row's cell under `speed`. The figure is $ per merged PR
766
- * under `cost` and minutes of work time per merged PR under `speed`.
758
+ * One row's cell under one objective — a node's of the tree — and why. The
759
+ * quality floor is held twice: by the guard over every group's Pulls together,
760
+ * whose trip takes a cell out of every row and out of the exploration draw,
761
+ * and per row by the fit, which refuses a cell under the floor that row alone.
762
+ * The floor is measured against the default cell under both objectives. Every
763
+ * figure read is the cell's newest generation's, the default cell's included,
764
+ * and every comparison is `compareCells`': two cells over the stretches in
765
+ * which both were drawn, the stretches pooled, so a base's other weeks move
766
+ * no verdict and a shift that hits every cell at once cancels in every gap.
767
+ * The one test it does not cancel in is ADOPT_RATIO's, a ratio of the two
768
+ * figures: a shift that raises both by the same amount brings the ratio nearer
769
+ * 1, so it can move a candidate from qualifying to `too-dear` or `too-slow`.
770
+ * `pulls` holds the row's counted Pulls per cell (`estimate()`'s); `baseCell`
771
+ * is the cell the row holds unless a candidate qualifies: the default cell
772
+ * under `cost`, the `cost` row's cell under `speed`. The figure is $ per
773
+ * merged PR under `cost` and minutes of work time per merged PR under `speed`.
767
774
  *
768
775
  * A candidate is a cell of `cells` other than the base. Its `verdict`, the
769
776
  * first that applies: `tripped` by the guard; `too-few`, under MIN_N merged
770
- * PRs in the row; `under-floor` against the default cell there; `too-dear`
771
- * (`cost`) or `too-slow` (`speed`), above ADOPT_RATIO of the base's figure;
772
- * `not-reliably-cheaper` or `not-reliably-faster`, its `range` does not lie
773
- * wholly below zero (its upper end, as `bootstrapDiffCI` returns it, is not
774
- * below zero) or there is none; else it `qualifies`. A candidate past
775
- * `too-few` records whether it is `under_floor`, the floor's `bar` (the
776
- * default cell's fail rate plus TRIP_POINTS, rounded as a fail rate is, for
777
- * reading only: the floor compares counts) and its `range`, the bootstrap 95%
778
- * range of (candidate − base) figure over those Pulls; the others record null
779
- * for each.
777
+ * PRs in the row, on either side, in the stretches it shares with the base or
778
+ * with the default cell (none shared, none counted); `under-floor` against the
779
+ * default cell there; `too-dear` (`cost`) or `too-slow` (`speed`), above
780
+ * ADOPT_RATIO of the base's figure, both figures over the stretches it shares
781
+ * with the base; `not-reliably-cheaper` or `not-reliably-faster`, its `range`
782
+ * does not lie wholly below zero (its upper end, as `bootstrapDiffCI` returns
783
+ * it, is not below zero) or there is none; else it `qualifies`. A candidate
784
+ * past `too-few` records whether it is `under_floor`, the floor's `bar` (the
785
+ * default cell's fail rate over the stretches it shares with the candidate,
786
+ * plus TRIP_POINTS, rounded as a fail rate is, for reading only: the floor
787
+ * compares counts) and its `range`, the bootstrap 95% range of (candidate −
788
+ * base) figure over the Pulls of those stretches; the others record null for
789
+ * each.
780
790
  *
781
791
  * Several qualifiers: each pair records the range of (the better by the
782
- * figure, else by name − the other) in `pairs`, naming the better side
783
- * `cheaper` or `faster`, and a qualifier another is reliably better than is
784
- * dropped. Among the rest the lower fail rate, compared in counts, wins, then
785
- * the lower figure, then the name.
792
+ * figure, else by name − the other) in `pairs`, the pair compared over the
793
+ * stretches it shares, naming the better side `cheaper` or `faster`, and a
794
+ * qualifier another is reliably better than is dropped. Among the rest the
795
+ * lower fail rate wins, then the lower figure, then the name; each is the
796
+ * qualifier's gap to the base over the stretches it shares with it.
786
797
  *
787
798
  * `reason`, why the row holds `cell`: `unjudged` (the default cell or the
788
799
  * base, or every untripped candidate, is under MIN_N merged PRs; `cell` is
@@ -791,31 +802,41 @@ const unjudged = () => ({ cell: null, reason: "unjudged", cells: {}, pairs: [] }
791
802
  * dropped), or what put it ahead of the next of the rest: `lower-fail-rate`,
792
803
  * `lower-usd` or `fewer-minutes`, or `name`.
793
804
  */
794
- function judge(objective, est = {}, pulls, cells, tripped, baseCell) {
805
+ function judge(objective, pulls = {}, cells, tripped, baseCell) {
795
806
  const { figure, metric, over, unreliable, better, reliably, ahead } = JUDGE[objective];
796
807
  const record = unjudged();
797
- const floor = est[POLICY_CELL];
798
- const base = est[baseCell];
799
- if (!floor || floor.merged < MIN_N || !base || base.merged < MIN_N) return record;
808
+ const pullsOf = (c) => pulls[c] ?? [];
809
+ const mergedOf = (c) => pullsOf(c).filter((u) => u.merged).length;
810
+ // A default cell or base short of MIN_N merged PRs has none to give any candidate, whichever stretches they share.
811
+ if (mergedOf(POLICY_CELL) < MIN_N || mergedOf(baseCell) < MIN_N) return record;
800
812
  // JSON writes −0 as 0, so a recorded end is 0 either way; `--check` compares the two.
801
- const units = (c) => pulls[c].map((u) => bootUnit(u, metric));
802
- const range = (a, b) => bootstrapDiffCI(units(a), units(b))?.map((x) => x + 0) ?? null;
813
+ const range = (m) => m.range(metric)?.map((x) => x + 0) ?? null;
803
814
  const reliablyBelow = (r) => r !== null && r[1] < 0;
804
- const bar = round4(floor.failed / floor.claims + TRIP_POINTS / 100);
815
+ // A matched figure as the table prints one, four places; none is unknown, past every figure that is known.
816
+ const printed = (side) => (side[figure] === null ? Infinity : round4(side[figure]));
817
+ const enough = (m) => m.cell.merged >= MIN_N && m.base.merged >= MIN_N;
818
+ const against = new Map();
805
819
  const qualifiers = [];
806
820
  let judged = false;
807
821
  for (const c of cells) {
808
822
  if (c === baseCell) continue;
809
- const e = est[c];
810
- if (tripped.has(c) || !e || e.merged < MIN_N) {
811
- record.cells[c] = { verdict: tripped.has(c) ? "tripped" : "too-few", under_floor: null, bar: null, range: null };
823
+ if (tripped.has(c)) {
824
+ record.cells[c] = { verdict: "tripped", under_floor: null, bar: null, range: null };
825
+ continue;
826
+ }
827
+ const vsBase = compareCells(pullsOf(c), pullsOf(baseCell));
828
+ const vsFloor = baseCell === POLICY_CELL ? vsBase : compareCells(pullsOf(c), pullsOf(POLICY_CELL));
829
+ if (!enough(vsBase) || !enough(vsFloor)) {
830
+ record.cells[c] = { verdict: "too-few", under_floor: null, bar: null, range: null };
812
831
  continue;
813
832
  }
814
833
  judged = true;
815
- const under = underFloor(e, floor);
816
- const r = range(c, baseCell);
834
+ against.set(c, vsBase);
835
+ const under = vsFloor.underFloor;
836
+ const bar = vsFloor.base.fail_rate === null ? null : round4(vsFloor.base.fail_rate + TRIP_POINTS / 100);
837
+ const r = range(vsBase);
817
838
  const verdict = under ? "under-floor"
818
- : e[figure] > ADOPT_RATIO * base[figure] ? over
839
+ : printed(vsBase.cell) > ADOPT_RATIO * printed(vsBase.base) ? over
819
840
  : reliablyBelow(r) ? "qualifies" : unreliable;
820
841
  record.cells[c] = { verdict, under_floor: under, bar, range: r };
821
842
  if (verdict === "qualifies") qualifiers.push(c);
@@ -823,36 +844,40 @@ function judge(objective, est = {}, pulls, cells, tripped, baseCell) {
823
844
  if (!judged) return record;
824
845
  if (qualifiers.length === 0) return { ...record, cell: baseCell, reason: "no-qualifier" };
825
846
 
826
- const of = (c) => est[c][figure];
827
847
  const byName = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
828
- const failCmp = (a, b) => est[a].failed * est[b].claims - est[b].failed * est[a].claims;
848
+ // A qualifier's gap to the base in fail rate, and in the figure, over the stretches they share.
849
+ const failGap = (c) => against.get(c).cell.fail_rate - against.get(c).base.fail_rate;
850
+ const figureGap = (c) => printed(against.get(c).cell) - printed(against.get(c).base);
829
851
  const named = [...qualifiers].sort(byName);
830
852
  const dropped = new Set();
831
853
  for (let i = 0; i < named.length; i++) {
832
854
  for (let j = i + 1; j < named.length; j++) {
833
- const [win, other] = of(named[j]) < of(named[i]) ? [named[j], named[i]] : [named[i], named[j]];
834
- const r = range(win, other);
855
+ const pair = compareCells(pullsOf(named[i]), pullsOf(named[j]));
856
+ const flip = printed(pair.base) < printed(pair.cell);
857
+ const [win, other] = flip ? [named[j], named[i]] : [named[i], named[j]];
858
+ const r = range(flip ? compareCells(pullsOf(win), pullsOf(other)) : pair);
835
859
  record.pairs.push({ [better]: win, other, range: r });
836
860
  if (reliablyBelow(r)) dropped.add(other);
837
861
  }
838
862
  }
839
863
  // The best qualifier, by the figure then name, is never dropped, so the rest is never empty.
840
- const [win, next] = named.filter((c) => !dropped.has(c)).sort((a, b) => failCmp(a, b) || of(a) - of(b) || byName(a, b));
864
+ const [win, next] = named.filter((c) => !dropped.has(c))
865
+ .sort((a, b) => byName(failGap(a), failGap(b)) || byName(figureGap(a), figureGap(b)) || byName(a, b));
841
866
  const reason = qualifiers.length === 1 ? "sole-qualifier"
842
867
  : next === undefined ? reliably
843
- : failCmp(win, next) !== 0 ? "lower-fail-rate"
844
- : of(win) !== of(next) ? ahead : "name";
868
+ : failGap(win) !== failGap(next) ? "lower-fail-rate"
869
+ : figureGap(win) !== figureGap(next) ? ahead : "name";
845
870
  return { ...record, cell: win, reason };
846
871
  }
847
872
 
848
873
  /** A `cost` row: `judge()` from the default cell, by $ per merged PR. */
849
- function judgeRow(est, pulls, cells, tripped) {
850
- return judge("cost", est, pulls, cells, tripped, POLICY_CELL);
874
+ function judgeRow(pulls, cells, tripped) {
875
+ return judge("cost", pulls, cells, tripped, POLICY_CELL);
851
876
  }
852
877
 
853
878
  /** A `speed` row: `judge()` from the `cost` row's cell, by minutes of work time per merged PR. */
854
- function judgeSpeedRow(est, pulls, cells, tripped, costCell) {
855
- return judge("speed", est, pulls, cells, tripped, costCell);
879
+ function judgeSpeedRow(pulls, cells, tripped, costCell) {
880
+ return judge("speed", pulls, cells, tripped, costCell);
856
881
  }
857
882
 
858
883
  /**
@@ -884,10 +909,10 @@ export function fitTable({ prior, features, members, verdicts, guard, commit = n
884
909
  const reasons = { cost: {}, speed: {} };
885
910
  for (const s of nodes) {
886
911
  const up = parentOf(s);
887
- const cost = judgeRow(estimates[s], pulls[s], prior.cells, tripped);
912
+ const cost = judgeRow(pulls[s], prior.cells, tripped);
888
913
  const own = cost.cell !== null || up === null;
889
914
  const costCell = cost.cell ?? (up === null ? POLICY_CELL : rows.cost[up]);
890
- const speed = own ? judgeSpeedRow(estimates[s], pulls[s], prior.cells, tripped, costCell) : unjudged();
915
+ const speed = own ? judgeSpeedRow(pulls[s], prior.cells, tripped, costCell) : unjudged();
891
916
  const speedCell = speed.cell ?? (own ? costCell : rows.speed[up]);
892
917
  rows.cost[s] = costCell;
893
918
  rows.speed[s] = speedCell;