@feigi/fleet-ctl 3.43.5 → 3.43.6

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.43.5",
3
+ "version": "3.43.6",
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": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "window_start": null,
3
- "fitted_through": null,
3
+ "fitted_commit": null,
4
4
  "n_rows": 0,
5
5
  "stage": 1,
6
6
  "cells": [
@@ -15,24 +15,32 @@
15
15
  // file degrades the line, never the exit. Exit 2 only on a usage error or
16
16
  // an unreadable/invalid table: the caller does not dispatch then.
17
17
  // fit --table <json> --features <tsv> --members <tsv> --verdicts <tsv>
18
- // --guard <cost-guard.json> [--due]
19
- // Re-fits the table from the three TSVs and the guard and rewrites
20
- // `--table`. With `--due`, writes nothing and prints `DUE=yes|no
21
- // MERGED=<n>/<N>`: whether N merged PRs have landed since
22
- // `fitted_through`.
18
+ // --guard <cost-guard.json> --commit <rev> [--due]
19
+ // Re-fits the table from the three TSVs as committed at `--commit` (read
20
+ // with `git show`, never from the working tree) and the guard, and
21
+ // rewrites `--table`, recording the commit's sha as `fitted_commit`. The
22
+ // TSV flags name paths inside the repository that holds them. With
23
+ // `--due`, writes nothing and prints `DUE=yes|no MERGED=<n>/<N>`: whether
24
+ // N merged PRs have landed since `fitted_commit`, counting the features
25
+ // rows at `--commit` that the fitted commit's features file does not hold.
23
26
  // --check --table <json> --features <tsv> --members <tsv> --verdicts <tsv>
24
- // Re-fits from the rows the table was fitted through, against the guard
25
- // snapshot the table itself records, and exits 1 when the result is not
26
- // the table: the table is a code carrier, never hand-edited.
27
+ // Re-fits from the commit the table records as `fitted_commit` (an empty
28
+ // corpus while it is null), against the guard snapshot the table itself
29
+ // records, and exits 1 when the result is not the table: the table is a
30
+ // code carrier, never hand-edited. A `fitted_commit` this repository does
31
+ // not hold exits 2 naming the `git fetch` that brings it.
27
32
  //
28
33
  // The script never touches the network: the issue, the sizing verdict and
29
- // the guard are files the caller wrote.
34
+ // the guard are files the caller wrote, and a fitted commit is fetched by the
35
+ // caller.
30
36
 
31
37
  import { readFileSync, writeFileSync, renameSync, existsSync, appendFileSync, mkdirSync } from "node:fs";
32
- import { basename, dirname, join } from "node:path";
38
+ import { spawnSync } from "node:child_process";
39
+ import { basename, dirname, join, resolve } from "node:path";
33
40
  import { fileURLToPath } from "node:url";
34
41
  import { isDeepStrictEqual } from "node:util";
35
42
  import { isCLI } from "./is-cli.mjs";
43
+ import { gitEnv } from "./git-env.mjs";
36
44
  import { makeDie, defineFlags } from "./arg.mjs";
37
45
  import { CELL, POLICY_CELL, drawCell, parseMember } from "./ledger-grammar.mjs";
38
46
  import { parseTsv as parseMemberTsv } from "./member-outcomes.mjs";
@@ -306,40 +314,33 @@ function datedRows(features) {
306
314
  }
307
315
 
308
316
  /**
309
- * Per-ticket input rows: the ticket's features rows inside [window, cutoff],
317
+ * Per-ticket input rows: the ticket's features rows inside the window,
310
318
  * attributed to its LAST row's (in file order) cell and stratum, restricted to rows the
311
319
  * free classifier routed plus exploration rows — a row a live B classifier
312
320
  * routed is the A/B's test set, not the fit's. Its verdict rules its last
313
321
  * input row. A row with a blank `run_date` (route writes one for a session id
314
- * with no date) is no input row: it cannot be placed against the window, the
315
- * cut or its ruling, and a window bound drops it anyway. A `run_date` that is
322
+ * with no date) is no input row: it cannot be placed against the window or
323
+ * its ruling, and a window bound drops it anyway. A `run_date` that is
316
324
  * neither blank nor YYYY-MM-DD is refused (see `datedRows`).
317
- * A fit over everything cuts at its latest features row's date;
318
- * the verdict and member rows are cut at that same date, so `--check`'s
319
- * re-fit at the recorded `fitted_through` reads the rows the fit read and a
320
- * verdict or member row that lands later waits for the next fit instead of
321
- * failing CI.
325
+ * A fit reads everything in the commit it is given: no row is cut by date,
326
+ * because `--check` re-reads that same commit rather than re-cutting the
327
+ * working tree's files.
322
328
  */
323
- function fitTickets({ features, members, verdicts, window, cutoff }) {
324
- const inRange = datedRows(features).filter((r) => (!window || r.run_date >= window)
325
- && (cutoff === undefined || (cutoff !== null && r.run_date <= cutoff)));
326
- const through = cutoff === undefined ? inRange.map((r) => r.run_date).sort().at(-1) ?? null : cutoff;
327
- const upToThrough = (r) => through === null || !r.run_date || r.run_date <= through;
328
- members = members.filter(upToThrough);
329
+ function fitTickets({ features, members, verdicts, window }) {
330
+ const inRange = datedRows(features).filter((r) => !window || r.run_date >= window);
329
331
  const byTicket = new Map();
330
332
  for (const r of inRange) {
331
333
  if (!byTicket.has(r.ticket)) byTicket.set(r.ticket, []);
332
334
  byTicket.get(r.ticket).push(r);
333
335
  }
334
- // A ruling whose date is not YYYY-MM-DD cannot be placed against the cut, and
335
- // a string `<=` would drop 'abc' or '2027' unseen: it is left in, so `rulingFor`
336
- // refuses it when its ticket is an input ticket. An input ticket is one the
337
- // fit reads — its last in-range row is an exploration row or has `sizing_src`
338
- // `rule`, which the skip below keeps — not every ticket with an in-range
339
- // features row. `fit --due` also refuses for a ticket the skip drops:
340
- // `mergedSince` calls `verdictOf` on every ticket with an in-window row dated
341
- // after `fitted_through`, with no such skip.
342
- const rulings = rulingsByTicket(verdicts.filter((r) => !DATE.test(r.run_date) || upToThrough(r)));
336
+ // `rulingFor` refuses a ruling whose date is not YYYY-MM-DD when its ticket
337
+ // is an input ticket. An input ticket is one the fit reads — its last
338
+ // in-window row is an exploration row or has `sizing_src` `rule`, which the
339
+ // skip below keeps — not every ticket with an in-window features row.
340
+ // `fit --due` also refuses for a ticket the skip drops: `mergedSince` calls
341
+ // `verdictOf` on every ticket with an in-window row the fitted commit does
342
+ // not hold, with no such skip.
343
+ const rulings = rulingsByTicket(verdicts);
343
344
  const lastPulls = lastPullByTicket(inRange);
344
345
  const out = [];
345
346
  for (const [ticket, rows] of byTicket) {
@@ -367,7 +368,7 @@ function fitTickets({ features, members, verdicts, window, cutoff }) {
367
368
  for (const r of rows) if (r.router_usd !== "") cost += Number(r.router_usd) || 0;
368
369
  out.push({ ticket, cell: last.chosen_cell, stratum: ruleStratum(last), verdict, cost, costKnown, fixRounds });
369
370
  }
370
- return { tickets: out, used: inRange };
371
+ return out;
371
372
  }
372
373
 
373
374
  function estimate(tickets) {
@@ -423,13 +424,12 @@ function adopt(est, cells, tripped) {
423
424
 
424
425
  /**
425
426
  * The re-fit table. `guard` is `{ tripped, n, window_start }` (readGuard's
426
- * shape); `cutoff` limits every input to rows with `run_date <= cutoff`
427
- * (--check), or is undefined for a fit over everything, which cuts at its
428
- * latest features row's date.
427
+ * shape); `commit` is the sha of the commit the inputs were read from, or null
428
+ * for the empty corpus before any fit.
429
429
  */
430
- export function fitTable({ prior, features, members, verdicts, guard, cutoff }) {
430
+ export function fitTable({ prior, features, members, verdicts, guard, commit = null }) {
431
431
  const window = guard.window_start ?? prior.window_start ?? null;
432
- const { tickets, used } = fitTickets({ features, members, verdicts, window, cutoff });
432
+ const tickets = fitTickets({ features, members, verdicts, window });
433
433
  const estimates = estimate(tickets);
434
434
  const tripped = new Set(guard.tripped);
435
435
 
@@ -447,10 +447,9 @@ export function fitTable({ prior, features, members, verdicts, guard, cutoff })
447
447
  if (!cells.includes(cell) && cells.includes(survives) && !tripped.has(survives)) cells.push(cell);
448
448
  }
449
449
  }
450
- const dates = used.map((r) => r.run_date).sort();
451
450
  return {
452
451
  window_start: window,
453
- fitted_through: dates.length ? dates[dates.length - 1] : prior.fitted_through ?? null,
452
+ fitted_commit: commit,
454
453
  n_rows: tickets.length,
455
454
  stage,
456
455
  cells,
@@ -462,13 +461,53 @@ export function fitTable({ prior, features, members, verdicts, guard, cutoff })
462
461
  };
463
462
  }
464
463
 
465
- /** Merged PRs (tickets with a verdict on their last such row in file order) among the YYYY-MM-DD-dated features rows dated after `fitted_through`. */
466
- export function mergedSince({ table, features, verdicts }) {
464
+ /**
465
+ * Merged PRs (tickets with a verdict on their last such row in file order)
466
+ * among the YYYY-MM-DD-dated features rows on or after `window_start` that the
467
+ * fitted commit's features file (`fitted`, parsed rows; empty before any fit)
468
+ * does not hold, matched by `session` and `agent`.
469
+ */
470
+ export function mergedSince({ table, features, verdicts, fitted }) {
467
471
  const rulings = rulingsByTicket(verdicts);
468
- const after = datedRows(features).filter((r) => (!table.window_start || r.run_date >= table.window_start) && (!table.fitted_through || r.run_date > table.fitted_through));
472
+ const read = new Set(fitted.map((r) => `${r.session}\0${r.agent}`));
473
+ const after = datedRows(features).filter((r) => (!table.window_start || r.run_date >= table.window_start) && !read.has(`${r.session}\0${r.agent}`));
469
474
  return [...lastPullByTicket(after)].filter(([ticket, p]) => verdictOf(rulings, ticket, p.run_date)).length;
470
475
  }
471
476
 
477
+ // ---------------------------------------------------------------------------
478
+ // the commit a fit reads
479
+ // ---------------------------------------------------------------------------
480
+
481
+ const SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
482
+
483
+ // `git` run in the directory holding `path`, so `path` names a file of the
484
+ // repository around it whichever directory the caller is in.
485
+ function gitBeside(path, args) {
486
+ const r = spawnSync("git", ["-C", dirname(resolve(path)), ...args], { encoding: "utf8", env: gitEnv(), maxBuffer: 1 << 30 });
487
+ if (r.error) throw r.error;
488
+ return r;
489
+ }
490
+
491
+ /**
492
+ * The full sha `rev` names as a commit in the repository holding `path`, or null when it names none there.
493
+ * Throws when git itself fails (not a repository, unreadable config, no git): that is not an answer about `rev`.
494
+ */
495
+ export function resolveCommit(path, rev) {
496
+ if (rev.startsWith("-")) return null;
497
+ const r = gitBeside(path, ["rev-parse", "--verify", "--quiet", `${rev}^{commit}`]);
498
+ if (r.status === 0) return r.stdout.trim();
499
+ // `--verify --quiet` exits 1, silently, for a rev that names no commit; any other status is git failing.
500
+ if (r.status === 1) return null;
501
+ throw new Error(r.stderr.trim() || `git rev-parse exited ${r.status}`);
502
+ }
503
+
504
+ /** `path` as committed at `sha`; throws when the commit does not hold it. */
505
+ export function showAt(sha, path) {
506
+ const r = gitBeside(path, ["show", `${sha}:./${basename(path)}`]);
507
+ if (r.status !== 0) throw new Error(r.stderr.trim() || `git show exited ${r.status}`);
508
+ return r.stdout;
509
+ }
510
+
472
511
  // ---------------------------------------------------------------------------
473
512
  // CLI
474
513
  // ---------------------------------------------------------------------------
@@ -479,7 +518,7 @@ function main() {
479
518
  flags: {
480
519
  session: "value", ticket: "value", arm: "value", "impl-row": "value", issue: "value",
481
520
  sizing: "value", guard: "value", table: "value", pending: "value", "chain-head": "bool",
482
- features: "value", members: "value", verdicts: "value", check: "bool", due: "bool",
521
+ features: "value", members: "value", verdicts: "value", commit: "value", check: "bool", due: "bool",
483
522
  },
484
523
  positionals: ["route", "fit"],
485
524
  });
@@ -492,7 +531,7 @@ function main() {
492
531
  }
493
532
  const allowed = {
494
533
  route: ["session", "ticket", "arm", "impl-row", "issue", "sizing", "guard", "table", "pending", "chain-head"],
495
- fit: ["table", "features", "members", "verdicts", "guard", "due"],
534
+ fit: ["table", "features", "members", "verdicts", "guard", "commit", "due"],
496
535
  check: ["check", "table", "features", "members", "verdicts"],
497
536
  }[mode];
498
537
  const given = argv.filter((a) => a.startsWith("--")).map((a) => a.slice(2).split("=")[0]);
@@ -533,24 +572,45 @@ function main() {
533
572
  return;
534
573
  }
535
574
 
536
- const read = (flag, parse) => {
575
+ const short = (sha) => (sha === null ? "no commit" : sha.slice(0, 8));
576
+ const fileAt = (sha, flag, parse) => {
537
577
  const p = need(flag);
538
- try { return parse(readFileSync(p, "utf8")); } catch (e) { die(`cannot read --${flag} ${p}: ${e.message}`); }
578
+ try { return parse(showAt(sha, p)); } catch (e) { die(`cannot read --${flag} ${p} at commit ${short(sha)}: ${e.message}`); }
579
+ };
580
+ // The three TSVs as committed at `sha`, never the working tree's copies.
581
+ const corpusAt = (sha) => ({
582
+ features: fileAt(sha, "features", parseFeatures),
583
+ members: fileAt(sha, "members", parseMemberTsv),
584
+ verdicts: fileAt(sha, "verdicts", parseTierOutcomes),
585
+ });
586
+ // The commit `rev` names in the repository holding --features, or null; git itself failing is a die, not "no such commit".
587
+ const commitOf = (rev) => {
588
+ try { return resolveCommit(need("features"), rev); } catch (e) { die(`cannot resolve ${rev} in the repository holding --features ${need("features")}: ${e.message}`); }
589
+ };
590
+ // The commit the table records: null before any fit, else a sha this repository holds.
591
+ const fittedCommit = () => {
592
+ const sha = table.fitted_commit;
593
+ if (sha === null) return null;
594
+ if (typeof sha !== "string" || !SHA.test(sha)) die(`${tablePath}: \`fitted_commit\` is not null or a full commit sha`);
595
+ if (commitOf(sha) === null) {
596
+ die(`${tablePath} was fitted from commit ${sha}, which this repository does not hold — run \`git fetch --depth=1 origin ${sha}\``);
597
+ }
598
+ return sha;
539
599
  };
540
- const features = read("features", parseFeatures);
541
- const members = read("members", parseMemberTsv);
542
- const verdicts = read("verdicts", parseTierOutcomes);
543
600
  // The join throws on a features row it cannot place and on a ruling of an input ticket it cannot place.
544
- // For `fit` and `--check` an input ticket is one `fitTickets` reads (its last in-range row is an
545
- // exploration row or has `sizing_src` `rule`), not every ticket with an in-range features row. `fit --due` also
601
+ // For `fit` and `--check` an input ticket is one `fitTickets` reads (its last in-window row is an
602
+ // exploration row or has `sizing_src` `rule`), not every ticket with an in-window features row. `fit --due` also
546
603
  // covers a ticket `fitTickets` skips, because `mergedSince` calls `verdictOf` on every ticket with an in-window
547
- // row dated after `fitted_through`, with no such skip.
604
+ // row the fitted commit does not hold, with no such skip.
548
605
  const joined = (fit) => {
549
606
  try { return fit(); } catch (e) { die(`cannot join --features ${need("features")} with --verdicts ${need("verdicts")}: ${e.message}`); }
550
607
  };
551
608
 
552
609
  if (mode === "check") {
553
- const refit = joined(() => fitTable({ prior: table, features, members, verdicts, guard: { ...table.guard, window_start: table.window_start }, cutoff: table.fitted_through }));
610
+ for (const flag of ["features", "members", "verdicts"]) need(flag);
611
+ const fitted = fittedCommit();
612
+ const { features, members, verdicts } = fitted === null ? { features: [], members: [], verdicts: [] } : corpusAt(fitted);
613
+ const refit = joined(() => fitTable({ prior: table, features, members, verdicts, guard: { ...table.guard, window_start: table.window_start }, commit: fitted }));
554
614
  const differ = Object.keys({ ...refit, ...table }).filter((k) => !isDeepStrictEqual(refit[k], table[k]));
555
615
  if (differ.length) {
556
616
  process.stderr.write(`${NAME}: ${tablePath} is not a re-fit of its own rows — differs in ${differ.map((k) => `\`${k}\``).join(", ")}; re-fitted:\n${JSON.stringify(refit, null, 2)}\n`);
@@ -558,22 +618,28 @@ function main() {
558
618
  process.exitCode = 1;
559
619
  return;
560
620
  }
561
- process.stdout.write(`${NAME}: ${tablePath} matches a re-fit of ${refit.n_rows} tickets through ${refit.fitted_through ?? "no rows"}\n`);
621
+ process.stdout.write(`${NAME}: ${tablePath} matches a re-fit of ${refit.n_rows} tickets at ${short(fitted)}\n`);
562
622
  return;
563
623
  }
564
624
 
625
+ const commitArg = need("commit");
626
+ const sha = commitOf(commitArg);
627
+ if (sha === null) die(`--commit ${commitArg} is not a commit of the repository holding --features ${need("features")}`);
628
+ const { features, members, verdicts } = corpusAt(sha);
565
629
  const guardPath = need("guard");
566
630
  const guard = readGuard(readJson(guardPath));
567
631
  if (guard === null) die(`cannot read --guard ${guardPath} as a cost guard — the fit needs its tripped cells and per-cell n`);
568
632
  if (F.has("due")) {
569
- const n = joined(() => mergedSince({ table, features, verdicts }));
633
+ const fitted = fittedCommit();
634
+ const read = fitted === null ? [] : fileAt(fitted, "features", parseFeatures);
635
+ const n = joined(() => mergedSince({ table, features, verdicts, fitted: read }));
570
636
  process.stdout.write(`DUE=${n >= FIT_EVERY_MERGED ? "yes" : "no"} MERGED=${n}/${FIT_EVERY_MERGED}\n`);
571
637
  return;
572
638
  }
573
- const next = joined(() => fitTable({ prior: table, features, members, verdicts, guard }));
639
+ const next = joined(() => fitTable({ prior: table, features, members, verdicts, guard, commit: sha }));
574
640
  writeFileSync(`${tablePath}.tmp`, `${JSON.stringify(next, null, 2)}\n`);
575
641
  renameSync(`${tablePath}.tmp`, tablePath);
576
- process.stdout.write(`${NAME}: fitted ${next.n_rows} tickets through ${next.fitted_through ?? "no rows"}; rows ${JSON.stringify(next.rows)}; burn_in ${next.burn_in}; stage ${next.stage}\n`);
642
+ process.stdout.write(`${NAME}: fitted ${next.n_rows} tickets at ${short(sha)}; rows ${JSON.stringify(next.rows)}; burn_in ${next.burn_in}; stage ${next.stage}\n`);
577
643
  }
578
644
 
579
645
  if (isCLI(import.meta.url)) main();