@feigi/fleet-ctl 3.152.0 → 3.153.1

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.1",
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);
@@ -2108,14 +2108,26 @@ function checkReaders(where) {
2108
2108
  die(`${where}: could not look — gh pr view ${pr} --json ${fields} answered ${e.message}; nothing written`);
2109
2109
  }
2110
2110
  };
2111
- // The title of issue `n`, as `gh issue view <n> --json title` answers it.
2112
- const issueTitle = (n) => {
2113
- const what = `gh issue view ${n} --json title`;
2114
- const { r, v } = lookJson(what, ["issue", "view", String(n), "--json", "title"]);
2111
+ // `labels` as the list of the label names it holds, or null when it is
2112
+ // anything but a list of named labels.
2113
+ const namedLabels = (labels) => {
2114
+ if (!Array.isArray(labels) || !labels.every((l) => l !== null && typeof l === "object" && typeof l.name === "string")) return null;
2115
+ return labels.map((l) => l.name);
2116
+ };
2117
+ // Issue `n`'s title and label names, as `gh issue view <n> --json
2118
+ // title,labels` answers them. An answer without a string title, or without a
2119
+ // list of named labels, could not look.
2120
+ const issueRead = (n) => {
2121
+ const what = `gh issue view ${n} --json title,labels`;
2122
+ const { r, v } = lookJson(what, ["issue", "view", String(n), "--json", "title,labels"]);
2115
2123
  if (v === null || typeof v !== "object" || typeof v.title !== "string") {
2116
2124
  die(`${where}: could not look — ${what} printed no title: ${cause(r.stdout)}; nothing written`);
2117
2125
  }
2118
- return v.title;
2126
+ const labels = namedLabels(v.labels);
2127
+ if (labels === null) {
2128
+ die(`${where}: could not look — ${what} printed no list of named labels: ${cause(r.stdout)}; nothing written`);
2129
+ }
2130
+ return { title: v.title, labels };
2119
2131
  };
2120
2132
  // The label names gh answers `args` with, `list` taking the label list off
2121
2133
  // the parsed answer. A read that fails, or answers anything but a list of
@@ -2124,38 +2136,58 @@ function checkReaders(where) {
2124
2136
  const what = `gh ${args.join(" ")}`;
2125
2137
  const lead = `could not read ${whose} labels`;
2126
2138
  const { r, v } = lookJson(what, args, lead);
2127
- const labels = list(v);
2128
- if (!Array.isArray(labels) || !labels.every((l) => l !== null && typeof l === "object" && typeof l.name === "string")) {
2139
+ const labels = namedLabels(list(v));
2140
+ if (labels === null) {
2129
2141
  die(`${where}: ${lead} — ${what} printed no list of named labels: ${cause(r.stdout)}; nothing written`);
2130
2142
  }
2131
- return labels.map((l) => l.name);
2143
+ return labels;
2132
2144
  };
2133
2145
  // PR `pr`'s label names, and the names of the labels its repository defines.
2134
2146
  const prLabels = (pr) => labelNames(`PR #${pr}'s`, ["pr", "view", String(pr), "--json", "labels"], (v) => v?.labels);
2135
2147
  const repoLabels = () => labelNames("the repository's", LABEL_LIST_ARGS, (v) => v);
2136
- return { repoDir, look, verifyShaOf, prView, prViewClosing, issueTitle, prLabels, repoLabels };
2148
+ // Issue `n`'s label names, as `gh issue view <n> --json labels` answers them.
2149
+ const issueLabels = (n) => labelNames(`issue #${n}'s`, ["issue", "view", String(n), "--json", "labels"], (v) => v?.labels);
2150
+ // Of `numbers`, a PR's closing references in its own repository, those not
2151
+ // in `own` — the PR's own tickets — whose issue carries the in-progress Label
2152
+ // role's string: tickets another claim holds. `labelsOf(n)` is issue n's
2153
+ // label names. The map is read only when a reference is not the PR's own, so
2154
+ // a single-ticket PR needs none; one that cannot be read is exit 2.
2155
+ const claimedElsewhere = (numbers, own, labelsOf) => {
2156
+ const others = numbers.filter((n) => !own.includes(n));
2157
+ if (others.length === 0) return [];
2158
+ const inProgress = roleLabel(repoDir(), "in-progress", where);
2159
+ return others.filter((n) => labelsOf(n).includes(inProgress));
2160
+ };
2161
+ return { repoDir, look, verifyShaOf, prView, prViewClosing, issueRead, prLabels, repoLabels, issueLabels, claimedElsewhere };
2137
2162
  }
2138
2163
 
2139
2164
  // The finisher gate's closing-reference checks for the PR `member` finishes,
2140
2165
  // read off the PR's closingIssuesReferences in its own repository — a
2141
2166
  // reference into another repository closes no issue here, whatever its
2142
- // number — and the titles of the issues they name; its second argument is
2143
- // rulingOf()'s answer, which let the PR through. Check 1, when a settled
2144
- // implementer's token names the PR: each ticket it names is closed by the
2145
- // body when the PR's ruling is closed_own_ticket=yes, and is not when the
2146
- // ruling is no. Check 2, for every PR: no reference may be the
2147
- // suggestion-band record of this PR's review — an issue whose title, trimmed,
2148
- // is recordTitle(), as dispositions-check.mjs finds the record. Returns
2149
- // `{refs, failures}`: the issue numbers the PR closes, its own
2150
- // suggestion-band record aside — that one is a failure here, not a ticket —
2151
- // and one line per failure. A read the checks need dies, naming the read,
2152
- // and writes nothing.
2167
+ // number — its head branch, and the titles and labels of the issues they
2168
+ // name; its second argument is rulingOf()'s answer, which let the PR through.
2169
+ // Check 1, when a settled implementer's token names the PR: each ticket it
2170
+ // names is closed by the body when the PR's ruling is closed_own_ticket=yes,
2171
+ // and is not when the ruling is no. Check 2, for every PR: no reference may
2172
+ // be the suggestion-band record of this PR's review — an issue whose title,
2173
+ // trimmed, is recordTitle(), as dispositions-check.mjs finds the record.
2174
+ // Check 3, for every PR, Ticketless ones included: no reference besides the
2175
+ // PR's own tickets may be a ticket another claim holds, its issue carrying
2176
+ // the Label map's in-progress string (claimedElsewhere()). The PR's own
2177
+ // tickets are the ones its settled tokens name and, when its head is a claim
2178
+ // branch (stranded.mjs's CLAIM_BRANCH), that claim's ticket — a PR a run
2179
+ // inherited carries no token, and its own claim holds its ticket until it
2180
+ // merges. Returns `{refs, failures}`: the issue numbers the PR closes, its
2181
+ // own suggestion-band record aside — that one is a failure here, not a
2182
+ // ticket — and one line per failure. A read the checks need dies, naming the
2183
+ // read, and writes nothing.
2153
2184
  async function closingRefusals(member, { tickets, ruling, tsv }) {
2154
2185
  const pr = member.number;
2155
2186
  const where = `dispatch ${member.name}`;
2156
2187
  const { recordTitle } = await import("./dispositions-check.mjs");
2157
- const { prViewClosing, issueTitle } = checkReaders(where);
2158
- const refs = prViewClosing(pr, "closingIssuesReferences,url").numbers;
2188
+ const { CLAIM_BRANCH } = await import("./stranded.mjs");
2189
+ const { prViewClosing, issueRead, claimedElsewhere } = checkReaders(where);
2190
+ const { view, numbers: refs } = prViewClosing(pr, "closingIssuesReferences,url,headRefName");
2159
2191
  const failures = [];
2160
2192
  if (tickets.length > 0) {
2161
2193
  const verdict = ruling.closed_own_ticket;
@@ -2172,10 +2204,21 @@ async function closingRefusals(member, { tickets, ruling, tsv }) {
2172
2204
  }
2173
2205
  }
2174
2206
  const own = recordTitle(pr);
2175
- const closed = [];
2207
+ // The label names of each issue the PR closes, its own record aside.
2208
+ const labels = new Map();
2176
2209
  for (const n of refs) {
2177
- if (issueTitle(n).trim() === own) failures.push(`PR #${pr} closes #${n}, its own suggestion-band record — rephrase the mention as "Refs #${n}" or "issue #${n}"`);
2178
- else closed.push(n);
2210
+ const issue = issueRead(n);
2211
+ if (issue.title.trim() === own) {
2212
+ failures.push(`PR #${pr} closes #${n}, its own suggestion-band record — rephrase the mention as "Refs #${n}" or "issue #${n}"`);
2213
+ } else {
2214
+ labels.set(n, issue.labels);
2215
+ }
2216
+ }
2217
+ const closed = [...labels.keys()];
2218
+ const claim = CLAIM_BRANCH.exec(view.headRefName);
2219
+ const ownTickets = claim === null ? tickets : [...tickets, Number(claim[1])];
2220
+ for (const n of claimedElsewhere(closed, ownTickets, (m) => labels.get(m))) {
2221
+ failures.push(`PR #${pr} closes #${n}, which another claim holds — rephrase it as "Refs #${n}"`);
2179
2222
  }
2180
2223
  return { refs: closed, failures };
2181
2224
  }
@@ -2220,16 +2263,23 @@ function labelRefusal(member, tickets, refs) {
2220
2263
  }
2221
2264
 
2222
2265
  // The tracker's string for the `ready-to-merge` Label role, off the Label map
2223
- // of the repository `dir` belongs to (label-map.mjs). Only the paths that
2224
- // compare or list that label read it — `dispatch merge-bot`'s queueSnapshot(),
2225
- // `dispatch finisher-pr-M`'s labelRefusal() and settle --from-report's
2226
- // finisher and merge-bot checks — so every other verb runs with no map at all.
2227
- // A map that cannot be read, or no repository to find one in, is exit 2 under
2228
- // `where`, carrying the reader's message (an absent or invalid map's names
2229
- // `label-map.mjs ensure`); nothing written.
2266
+ // of the repository `dir` belongs to: roleLabel()'s answer for that role. Only
2267
+ // the paths that compare or list that label read it — `dispatch merge-bot`'s
2268
+ // queueSnapshot(), `dispatch finisher-pr-M`'s labelRefusal() and settle
2269
+ // --from-report's finisher and merge-bot checks — so every other verb runs
2270
+ // with no map at all.
2230
2271
  function readyLabel(dir, where) {
2272
+ return roleLabel(dir, "ready-to-merge", where);
2273
+ }
2274
+
2275
+ // The tracker's string for the Label role `role`, off the Label map of the
2276
+ // repository `dir` belongs to (label-map.mjs). A map that cannot be read, or no
2277
+ // repository to find one in, is exit 2 under `where`, carrying the reader's
2278
+ // message (an absent or invalid map's names `label-map.mjs ensure`); nothing
2279
+ // written.
2280
+ function roleLabel(dir, role, where) {
2231
2281
  try {
2232
- return readLabelMap({ cwd: dir })["ready-to-merge"];
2282
+ return readLabelMap({ cwd: dir })[role];
2233
2283
  } catch (e) {
2234
2284
  if (!(e instanceof LabelMapUnreadable) && !(e instanceof FleetDirUnresolvable)) throw e;
2235
2285
  die(`${where}: ${e.message}; nothing written`);
@@ -2707,8 +2757,10 @@ function recordFiled(id, subject) {
2707
2757
  //
2708
2758
  // impl-N=PR#M verify-sha.sh <branch> <head> exits 0; PR M
2709
2759
  // reads OPEN and its closingIssuesReferences in its
2710
- // own repository name N; its head branch is
2711
- // implementer/N-impl-N;
2760
+ // own repository name N; no other closing
2761
+ // reference there is a ticket another claim holds,
2762
+ // its issue carrying in-progress; its head branch
2763
+ // is implementer/N-impl-N;
2712
2764
  // the Check-step record (below) holds for <head>
2713
2765
  // impl-N=bailed bailCause is not null; the receipt names it.
2714
2766
  // report-schema.mjs already refuses a bail that
@@ -2739,7 +2791,9 @@ function recordFiled(id, subject) {
2739
2791
  // by readyLabel() only when one of the two checks runs: on a finisher's
2740
2792
  // `labelled`, and on a merge bot's report that leaves a PR queued at dispatch
2741
2793
  // without a verdict. Outside settle, `dispatch finisher-pr-M` reads it too,
2742
- // to refuse a finisher onto a labelled PR (labelRefusal()).
2794
+ // to refuse a finisher onto a labelled PR (labelRefusal()). `in-progress` in
2795
+ // the `impl-N=PR#M` row is the Label map's string for that role, read by
2796
+ // roleLabel() only when the PR closes a ticket besides N.
2743
2797
  //
2744
2798
  // THE CHECK-STEP RECORD, for `impl-N=PR#M` and `fix-pr-M=applied:<head>`:
2745
2799
  // recipe-check.mjs writes `<reports>/<member>.check.json` on the member's
@@ -2835,7 +2889,7 @@ async function runSettleFromReport() {
2835
2889
  if (report.member !== member) invalid(`$.member: ${JSON.stringify(report.member)} is not ${member}`);
2836
2890
 
2837
2891
  // The gh and git reads this check makes, shared with the finisher gate.
2838
- const { repoDir, look, verifyShaOf, prView, prViewClosing } = checkReaders(where);
2892
+ const { repoDir, look, verifyShaOf, prView, prViewClosing, issueLabels, claimedElsewhere } = checkReaders(where);
2839
2893
  // origin's head of `branch`, or null when origin has no such branch.
2840
2894
  const remoteHead = (branch) => {
2841
2895
  const ref = `refs/heads/${branch}`;
@@ -2893,6 +2947,15 @@ async function runSettleFromReport() {
2893
2947
  const { view: pr, numbers } = prViewClosing(report.pr, "state,closingIssuesReferences,url,headRefName");
2894
2948
  check(`PR #${report.pr} reads OPEN`, pr.state === "OPEN");
2895
2949
  check(`PR #${report.pr} closes #${live.number}`, numbers.includes(live.number));
2950
+ // The claim's own ticket is the token's: a replacement impl-N-x holds
2951
+ // the same claim as impl-N, so it is never refused for #N. A PR that is
2952
+ // not OPEN, or does not close #N, has failed already, so no other
2953
+ // reference's issue is read: a read that could not look would turn
2954
+ // that exit 3 into exit 2.
2955
+ if (pr.state === "OPEN" && numbers.includes(live.number)) {
2956
+ const held = claimedElsewhere(numbers, [live.number], issueLabels);
2957
+ check(`PR #${report.pr} closes no ticket another claim holds (in-progress: ${held.length === 0 ? "none" : held.map((n) => `#${n}`).join(", ")})`, held.length === 0);
2958
+ }
2896
2959
  // The claim's branch comes from the ticket: the Pull claims with member
2897
2960
  // impl-N, so its branch is implementer/N-impl-N, and close-out releases
2898
2961
  // impl-N. Never from the settling member's name or its report: a
@@ -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;
@@ -182,6 +182,8 @@ above is the correct one.
182
182
 
183
183
  `Closes #N` closes issue on merge — as does any closing keyword (`close`, `fixes`, `resolved`, …) sitting directly before an issue reference, wherever it appears in the body. Write one `Closes #M` per issue you mean to close, so `Closes #A, closes #B` closes both; give every other issue mention a word in front of it — `closed issue #M`, or the issue named without the `#`. A keyword before a list links only the first reference in that list, never the rest: `Closes #A, #B` closes only issue A.
184
184
 
185
+ Close no ticket another claim holds — its issue carries the in-progress label — even when the ticket asks for one PR with it; write `Refs #M`. A ticket closed by another claim's PR strands that claim: neither release nor reap removes it.
186
+
185
187
  Repo gating on release label → exactly one of `patch`/`minor`/`major`; `validate-release-label` fails without it.
186
188
 
187
189
  **Never fold `--label` into the create.** A `gh pr create` that outruns the caller's tool timeout is backgrounded with the PR already open and its flags unapplied, and a timeout carries no exit status for anything to react to — so the label goes missing and every later gate reads the PR as correctly opened. Written as its own command it has its own exit status and fails loudly; `gh pr edit` with no PR argument resolves the current branch's PR, so it lands even when the create's own output was lost to the timeout. Failed → run it again before reporting the PR.
@@ -561,8 +561,8 @@ Dispatch a finisher only when every test here passes at once — the review retu
561
561
  **This bullet and the one above are a pair, and on a clean PR this one carries the
562
562
  whole route to a finisher**: a `no-op` report satisfies the gate above by
563
563
  itself, so nothing further is owed and no further event is coming.
564
- - **The PR's closing references match its ruling and exclude its own suggestion-band record** → `ledger.mjs dispatch` refuses the finisher otherwise, naming the PR and the ticket: repair the body with `gh pr edit <M> --body-file <file>`, then dispatch again.
565
- - **The PR carries exactly one release label, or its repo defines none** → `ledger.mjs dispatch` refuses the finisher otherwise, except a Ticketless PR with none, which `finisher-label.mjs` labels `patch`. Fix them, then dispatch again. In full: references/finisher.md.
564
+ - **The PR's closing references match its ruling and exclude its own suggestion-band record and every ticket another claim holds** → `ledger.mjs dispatch` refuses the finisher otherwise, naming the PR and ticket: `gh pr edit <M>` fixes the body; dispatch again.
565
+ - **The PR carries exactly one release label, or its repo defines none** → `ledger.mjs dispatch` refuses the finisher otherwise, except a Ticketless PR with none, which `finisher-label.mjs` labels `patch`. Fix them; dispatch again. In full: references/finisher.md.
566
566
  - **`ci-state.mjs --pr <N>` reads `verdict: "no-ci"`** → the CI edge never
567
567
  fires: dispatch the finisher off the reviewer's final verdict the moment it
568
568
  lands, never while you still owe it a reply. Its gate is then the
@@ -14,9 +14,12 @@ literal outcome
14
14
  Implement ticket <N>. You are ALREADY in worktree `<abs-path>`, whose PR branch is
15
15
  `<branch>`: commit on it and open your PR from it, even when the ticket below names
16
16
  another branch; the settle refuses a PR from any other branch. If the ticket names
17
- one, say in the PR body that you used `<branch>`. Your scratch directory is
18
- `<scratch>/impl-<N>/`; outside the worktree, write nowhere else but your report file
19
- below.
17
+ one, say in the PR body that you used `<branch>`. Label names in this file are roles;
18
+ the Label map gives the string to type; a member never derives the map. Close no ticket
19
+ another claim holds — one whose issue carries the in-progress label — even when the
20
+ ticket below asks for one PR with it: write `Refs #M` instead; the settle refuses a PR
21
+ that closes one. Your scratch directory is `<scratch>/impl-<N>/`; outside the worktree,
22
+ write nowhere else but your report file below.
20
23
 
21
24
  The ticket, pasted verbatim from the controller's own read — its title, the
22
25
  `## Agent Brief` comment or the issue body, whichever carries the brief, and its
@@ -119,7 +119,7 @@ and will act on it. Relay everything — reversals too — then dispatch.
119
119
  append` command to run. Any row for the PR passes, so a replacement finisher
120
120
  is never held once the first passed; a PR no such token names is not gated.
121
121
  No flag skips it: a PR `append` refuses is fixed on the ledger, as at close-out.
122
- - **The PR's closing references match its ruling and exclude its own suggestion-band record** → `ledger.mjs dispatch` reads the PR's `closingIssuesReferences` from `gh pr view <M>` before it dispatches any finisher, counting only those in the PR's own repository: a reference into another repository closes none of this repository's tickets, whatever its number. When the ruled row says `closed_own_ticket=yes`, each ticket a settled `impl-<N>=PR#<M>` token names must be closed by the body (`Closes #<N>`); when it says `no`, none may be, and no reference may be the PR's own suggestion-band record. A blank verdict is refused until it is set. A refusal names the PR and the ticket. The controller repairs the body itself with `gh pr edit <M> --body-file <file>`, then dispatches the finisher again. A `gh` read that fails refuses the dispatch and writes nothing.
122
+ - **The PR's closing references match its ruling and exclude its own suggestion-band record and every ticket another claim holds** → `ledger.mjs dispatch` reads the PR's `closingIssuesReferences` from `gh pr view <M>` before it dispatches any finisher, counting only those in the PR's own repository: a reference into another repository closes none of this repository's tickets, whatever its number. When the ruled row says `closed_own_ticket=yes`, each ticket a settled `impl-<N>=PR#<M>` token names must be closed by the body (`Closes #<N>`); when it says `no`, none may be, and no reference may be the PR's own suggestion-band record. A blank verdict is refused until it is set. Whatever the ruling, and for a Ticketless PR too, no reference besides those tickets may be one another claim holds — one whose issue carries the in-progress label, as the Label map spells it; the same `gh issue view` read of each reference answers it. A refusal names the PR and the ticket, and for a ticket another claim holds says to write `Refs #<M>`. The controller repairs the body itself with `gh pr edit <M> --body-file <file>`, then dispatches the finisher again. A `gh` read that fails refuses the dispatch and writes nothing.
123
123
  - **The PR carries exactly one release label** → `ledger.mjs dispatch` reads the PR's labels from `gh pr view <M>` before it dispatches any finisher, and refuses unless exactly one of `patch`, `minor` and `major` is among them — the count `finisher-label.mjs` makes before it labels. A repository whose label list defines none of the three is not gated. A Ticketless PR with none is not refused: `finisher-label.mjs` adds `patch` to it before its count. Any other PR with none is refused naming the tickets it closes, and any PR with several naming the labels found — the bump, or which label stays, is not picked for the controller. The controller adds or removes the label itself, then dispatches the finisher again. A `gh` read that fails or answers malformed refuses the dispatch, saying the labels could not be read, and writes nothing.
124
124
  - **The PR carries no `ready-to-merge`** → the same `gh pr view <M>` read of the PR's labels answers it, and `ledger.mjs dispatch` refuses any finisher while the label is on, writing nothing and naming `fleet-event.mjs label-off <M>`. A finisher runs on an unlabelled PR: the label a merge bot's `head-moved-after-label` refusal left comes off with its report's settle, and the command takes off any other. A repair finisher's PR lacks the label by definition, so it passes.
125
125