@feigi/fleet-ctl 3.128.0 → 3.128.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/pr-cost.mjs +69 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feigi/fleet-ctl",
3
- "version": "3.128.0",
3
+ "version": "3.128.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": {
@@ -8,9 +8,15 @@
8
8
  // working directory; ticket-features.tsv is written by the router script at
9
9
  // dispatch and must exist, else exit 2 — pricing.json and the router-table.json
10
10
  // beside this script are optional, though a `--pricing` named but absent is
11
- // exit 2 too) and ONE `gh pr list --state all`, scoped to PRs created in the
12
- // window, for merged state, which tier-outcomes.tsv does not record. A PR that
13
- // list does not return is counted pending. Output: a TSV per cell on stdout
11
+ // exit 2 too) and `gh pr list --state all` over the PRs created in the window,
12
+ // for merged state, which tier-outcomes.tsv does not record. GitHub search
13
+ // returns at most GH_PR_LIMIT results whatever `--limit` says, so the window is
14
+ // read in `created:` date slices (`readPrs`): the whole window first, and any
15
+ // slice that comes back full is halved by day and read again, until every
16
+ // slice is short of the cap. A one-day slice that is still full cannot be
17
+ // split and is exit 2, as is any failed slice: a PR that merged but past a
18
+ // truncated read would be counted pending, silently. A PR the slices do not
19
+ // return is counted pending. Output: a TSV per cell on stdout
14
20
  // (`--json` prints the whole report instead). `--guard` also writes the
15
21
  // verdict to `.fleet/cost-guard.json` in the main workspace — where fleet-tick
16
22
  // reads it, wherever this runs from; `--out` overrides — and exits on it:
@@ -97,6 +103,44 @@ export const MIN_N = 20;
97
103
  export const EXIT = { ok: 0, tripped: 3, none: 4 };
98
104
  export const GH_PR_LIMIT = 1000;
99
105
 
106
+ // A slice of the window that cannot be proven fully read.
107
+ export class PrListIncomplete extends Error {}
108
+
109
+ const DAY_MS = 86_400_000;
110
+ const dayNumber = (d) => Math.round(Date.parse(`${d}T00:00:00Z`) / DAY_MS);
111
+ const dayOf = (n) => new Date(n * DAY_MS).toISOString().slice(0, 10);
112
+
113
+ /**
114
+ * Every PR created from `windowStart` on, as `listSlice(created)` returns them,
115
+ * proven complete. GitHub search answers at most `limit` rows however many
116
+ * match, so a slice that returns `limit` is cut off, not whole: it is halved
117
+ * by day and both halves read again. The top slice stays open-ended (`>=A`),
118
+ * so a PR created after `today` by the local clock is still inside it. A
119
+ * single day that is still full cannot be split further and throws
120
+ * `PrListIncomplete`; a `listSlice` that fails is its own to refuse, by
121
+ * throwing or exiting, and is never read as an empty slice. A short slice is
122
+ * the whole of its days. Slices are disjoint by day; a PR is kept once by number.
123
+ */
124
+ export function readPrs(listSlice, { windowStart = WINDOW_START, today = new Date().toISOString().slice(0, 10), limit = GH_PR_LIMIT } = {}) {
125
+ const byNumber = new Map();
126
+ const todo = [{ from: windowStart, to: null }];
127
+ while (todo.length) {
128
+ const { from, to } = todo.pop();
129
+ const rows = listSlice(to === null ? `>=${from}` : `${from}..${to}`);
130
+ if (rows.length < limit) {
131
+ for (const row of rows) byNumber.set(row.number, row);
132
+ continue;
133
+ }
134
+ const last = to ?? today;
135
+ if (from >= last) {
136
+ throw new PrListIncomplete(`gh pr list created:${to === null ? `>=${from}` : from} returned ${rows.length} PRs, its cap, and one day cannot be split further — merged state may be truncated`);
137
+ }
138
+ const midN = Math.floor((dayNumber(from) + dayNumber(last)) / 2);
139
+ todo.push({ from: dayOf(midN + 1), to }, { from, to: dayOf(midN) });
140
+ }
141
+ return [...byNumber.values()];
142
+ }
143
+
100
144
  const routerUsd = (pull) => (pull.router_usd === "" ? 0 : Number(pull.router_usd));
101
145
  const round = (x, d = 4) => (x === null || !Number.isFinite(x) ? null : Number(x.toFixed(d)));
102
146
 
@@ -358,22 +402,29 @@ function main() {
358
402
  const tablePath = join(SCRIPT_DIR, "router-table.json");
359
403
  const routerTable = existsSync(tablePath) ? parse(tablePath, () => JSON.parse(read(tablePath))) : null;
360
404
 
361
- let out;
362
- try {
363
- out = execFileSync("gh", ["pr", "list", "--state", "all", "--search", `created:>=${WINDOW_START}`,
364
- "--limit", String(GH_PR_LIMIT), "--json", "number,state,mergedAt"], {
365
- encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], maxBuffer: 64 * 1024 * 1024, env: gitEnv(),
366
- });
367
- } catch (e) {
368
- die(`gh pr list failed: ${e.code ?? (e.signal ? `killed by ${e.signal}` : `exit ${e.status}`)}`);
369
- }
370
- const prs = parse("gh pr list", () => JSON.parse(out));
371
- if (!Array.isArray(prs) || prs.some((p) => !p || typeof p.number !== "number" || typeof p.state !== "string")) {
372
- die("gh pr list did not return {number,state} rows");
405
+ // One slice of the window: `created:` takes `>=A` or `A..B`, whole days, both ends in.
406
+ const listSlice = (created) => {
407
+ let out;
408
+ try {
409
+ out = execFileSync("gh", ["pr", "list", "--state", "all", "--search", `created:${created}`,
410
+ "--limit", String(GH_PR_LIMIT), "--json", "number,state,mergedAt"], {
411
+ encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], maxBuffer: 64 * 1024 * 1024, env: gitEnv(),
412
+ });
413
+ } catch (e) {
414
+ die(`gh pr list failed: ${e.code ?? (e.signal ? `killed by ${e.signal}` : `exit ${e.status}`)}`);
415
+ }
416
+ const rows = parse("gh pr list", () => JSON.parse(out));
417
+ if (!Array.isArray(rows) || rows.some((p) => !p || typeof p.number !== "number" || typeof p.state !== "string")) {
418
+ die("gh pr list did not return {number,state} rows");
419
+ }
420
+ return rows;
421
+ };
422
+ let prs;
423
+ try { prs = readPrs(listSlice); }
424
+ catch (e) {
425
+ if (e instanceof PrListIncomplete) die(e.message);
426
+ throw e;
373
427
  }
374
- // Merged state for a PR past the cap would read as missing — pending —
375
- // silently, so a full page is refused rather than trusted.
376
- if (prs.length >= GH_PR_LIMIT) die(`gh pr list returned ${prs.length} PRs, its cap — merged state may be truncated`);
377
428
 
378
429
  let report;
379
430
  try { report = computeReport({ members, tiers, features, prs, routerTable, pricing }); }