@feigi/fleet-ctl 3.63.0 → 3.64.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feigi/fleet-ctl",
3
- "version": "3.63.0",
3
+ "version": "3.64.0",
4
4
  "description": "Agent fleet: run-team controller, merge bot, PR reviewer, and the ticket pipeline they share",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
package/scripts/pull.mjs CHANGED
@@ -53,9 +53,19 @@
53
53
  // the brief.
54
54
  // 4 Q2 decided `decided`, or a cause whose comment is posted and whose
55
55
  // label replaces `ready-for-agent`.
56
- // 5 Q3 collision `clear`, `behind-pr:#M`, `behind-issue:#M`. The script
57
- // prints the brief's paths and the open PR numbers and runs no
58
- // scan of its own. An exclusion is a ledger row.
56
+ // 5 Q3 collision the scan first: the brief's paths, and every path a
57
+ // `paths <p>…` answer added, against each open PR's `gh pr diff
58
+ // <M> --name-only` and each live worktree branch's `git diff
59
+ // --name-only -z origin/main...<branch>` — `main` excepted, and
60
+ // a branch whose upstream is gone, its PR already merged or
61
+ // closed. A path matches a changed file of that path, one
62
+ // under it as a directory, or, with no `/`, one of that name.
63
+ // One line per PR or branch sharing a path, `no overlap` when
64
+ // none, and `could not look: <which>` for one that could not be
65
+ // read; the `#N` refs of the brief's Out of scope section are
66
+ // printed as the behind-issue candidates. Then `clear`,
67
+ // `behind-pr:#M`, `behind-issue:#M`, or `paths <p>…`, which
68
+ // scans again and asks again. An exclusion is a ledger row.
59
69
  // 6 claim `claim-ticket.sh <N> impl-<N> implementer --apply`.
60
70
  // 7 route `ticket-router.mjs route …`; a router that refuses releases
61
71
  // the claim, and a release that fails is exit 2 with the claim
@@ -96,7 +106,7 @@ const SCRIPTS = dirname(fileURLToPath(import.meta.url));
96
106
  const die = makeDie(NAME);
97
107
  const MAX_BUFFER = 64 * 1024 * 1024;
98
108
  const NETWORK_TIMEOUT_MS = 120_000;
99
- const PR_LIMIT = 1000;
109
+ const PR_LIMIT = 200;
100
110
  // A command's stdout and each of its arguments are kept to this many characters
101
111
  // in the record, the tail kept: the end of a child's output says why it stopped.
102
112
  const RECORD_MAX = 4000;
@@ -193,6 +203,17 @@ export function outOfScope(text) {
193
203
  return all.slice(at, end).join("\n").trim();
194
204
  }
195
205
 
206
+ /** The ticket's `Out of scope` section: the brief's, else the body's; null when neither holds one. */
207
+ function scopeOf(issue) {
208
+ return outOfScope(briefText(issue).trim()) ?? outOfScope(String(issue.body ?? ""));
209
+ }
210
+
211
+ /** The `#N` refs in `section`, each once, in the order they first appear; `owner/repo#N` is another repository's and none. */
212
+ export function issueRefs(section) {
213
+ if (section === null) return [];
214
+ return [...new Set([...section.matchAll(/(?<![\w/.-])#(\d+)\b/g)].map((m) => `#${m[1]}`))];
215
+ }
216
+
196
217
  // The placeholders the implementer's brief template declares. dispatch-prompt.mjs
197
218
  // refuses a value that spells one, and a ticket's text is a value: one that
198
219
  // names `<N>` would stop every render, with the claim standing.
@@ -237,6 +258,20 @@ function spawn(cmd, args, { cwd, env, timeoutMs } = {}) {
237
258
 
238
259
  const GH_ENV = { GH_REPO: "" };
239
260
  const withText = (head, r) => (r.text ? `${head}\n${r.text}` : head);
261
+ // How a child failed, with the first line of what it said.
262
+ const failure = (r) => `${r.how}${r.stderr === "" ? "" : `: ${r.stderr.split("\n")[0]}`}`;
263
+
264
+ /** The branches `git worktree list --porcelain -z` names; a detached worktree names none. */
265
+ function worktreeBranches(out) {
266
+ const head = "branch refs/heads/";
267
+ return out.split("\0").filter((f) => f.startsWith(head)).map((f) => f.slice(head.length));
268
+ }
269
+
270
+ /** The local branches whose upstream is gone, from `git for-each-ref --format=%(refname:short)%09%(upstream:track)`. */
271
+ function goneBranches(out) {
272
+ const mark = "\t[gone]";
273
+ return new Set(out.split("\n").filter((l) => l.endsWith(mark)).map((l) => l.slice(0, -mark.length)));
274
+ }
240
275
 
241
276
  // ── the state ────────────────────────────────────────────────────────────────
242
277
 
@@ -253,7 +288,8 @@ function fresh(ticket, session) {
253
288
  answers: [],
254
289
  staleness: [],
255
290
  close: null,
256
- openPrs: null,
291
+ addedPaths: [],
292
+ scans: [],
257
293
  session,
258
294
  commented: null,
259
295
  route: null,
@@ -281,6 +317,14 @@ function readPull(path) {
281
317
  && Number.isInteger(s.ticket) && typeof s.stage === "string" && typeof s.status === "string"
282
318
  && Array.isArray(s.commands) && Array.isArray(s.questions) && Array.isArray(s.answers);
283
319
  if (!ok) throw new Refusal(`${path} is not a Pull record — move it aside and run again`);
320
+ // A record written before the collision scan holds neither field.
321
+ s.scans ??= [];
322
+ s.addedPaths ??= [];
323
+ if (!Array.isArray(s.scans) || !Array.isArray(s.addedPaths)) throw new Refusal(`${path} is not a Pull record — move it aside and run again`);
324
+ // A collision question is printed from the latest scan, so one standing with no scan behind it cannot be.
325
+ if (s.pending?.qid === "collision" && s.scans.length === 0) {
326
+ throw new Refusal(`${path} holds a collision question that no scan of this script asked — move it aside and run again`);
327
+ }
284
328
  return s;
285
329
  }
286
330
 
@@ -312,7 +356,12 @@ const DECIDED_TOKENS = [
312
356
  "ready-for-human-fork <the options, verbatim from the brief>",
313
357
  "ready-for-human-hands <external access|manual testing|judgment> <one line>",
314
358
  ];
315
- const COLLISION_TOKENS = ["clear", "behind-pr:#M", "behind-issue:#M"];
359
+ const COLLISION_TOKENS = [
360
+ "clear",
361
+ "behind-pr:#M an open PR, or a branch with no PR yet by its name",
362
+ "behind-issue:#M an open issue the brief's Out of scope sequences this one after",
363
+ "paths <p>… adds paths to the scan, which runs again",
364
+ ];
316
365
 
317
366
  const TOKEN_LINES = { staleness: STALENESS_TOKENS, close: CLOSE_TOKENS, decided: DECIDED_TOKENS, collision: COLLISION_TOKENS };
318
367
 
@@ -346,6 +395,7 @@ export function parseAnswer(qid, token, rest) {
346
395
  if (qid === "collision") {
347
396
  const m = /^behind-(pr|issue):#?(\S+)$/.exec(token);
348
397
  if (token === "clear") spec = {};
398
+ else if (token === "paths") spec = { text: true };
349
399
  else if (m !== null && !m[2].startsWith("-") && (m[1] === "pr" || isDigits(m[2]))) {
350
400
  spec = {};
351
401
  said = `behind-${m[1]}:#${m[2]}`;
@@ -454,7 +504,7 @@ class Pull {
454
504
  brief() {
455
505
  const issue = this.issue();
456
506
  const text = briefText(issue).trim();
457
- const found = outOfScope(text) ?? outOfScope(String(issue.body ?? ""));
507
+ const found = scopeOf(issue);
458
508
  const paths = briefPaths(text);
459
509
  return [
460
510
  `TICKET #${this.st.ticket}: ${issue.title}`,
@@ -496,12 +546,17 @@ class Pull {
496
546
  ];
497
547
  }
498
548
 
549
+ // The latest scan, line by line, and the behind-issue candidates.
499
550
  collisionContext() {
500
- const paths = briefPaths(briefText(this.issue()).trim());
501
- const prs = this.st.openPrs;
551
+ const { paths, overlaps, unread } = this.st.scans.at(-1);
552
+ const refs = issueRefs(scopeOf(this.issue()));
553
+ const who = (o) => (o.branch === undefined ? `PR #${o.pr}` : `branch ${o.branch}${o.pr === null ? "" : ` (PR #${o.pr})`}`);
502
554
  return [
503
- `paths the brief names: ${paths.length === 0 ? "(none)" : paths.join(" ")}`,
504
- `open PRs: ${prs.length === 0 ? "(none)" : prs.map((n) => `#${n}`).join(" ")}`,
555
+ `paths scanned: ${paths.length === 0 ? "(none)" : paths.join(" ")}`,
556
+ ...overlaps.map((o) => `${who(o)}: ${o.paths.join(" ")}`),
557
+ ...unread.map((u) => `could not look: ${u}`),
558
+ ...(overlaps.length === 0 ? ["no overlap"] : []),
559
+ `behind-issue candidates (Out of scope): ${refs.length === 0 ? "(none)" : refs.join(" ")}`,
505
560
  ];
506
561
  }
507
562
 
@@ -568,22 +623,57 @@ class Pull {
568
623
  return this.ask("decided", []);
569
624
  }
570
625
 
626
+ // The scan runs every time this stage does: after Q2, and after each `paths` answer.
571
627
  stage_collision() {
572
628
  const { st } = this;
573
- if (st.openPrs === null) {
574
- const r = this.gh("collision", ["pr", "list", "--state", "open", "--json", "number", "--limit", String(PR_LIMIT)]);
575
- if (r.status !== 0) return this.stop(withText(`gh pr list failed (${r.how})`, r));
576
- let prs;
577
- try {
578
- prs = JSON.parse(r.stdout);
579
- } catch (e) {
580
- return this.stop(`could not parse gh pr list: ${e.message}`);
581
- }
582
- if (!Array.isArray(prs) || !prs.every((p) => Number.isInteger(p?.number))) return this.stop("gh pr list did not print a JSON array of numbered PRs");
583
- if (prs.length >= PR_LIMIT) return this.stop(`gh pr list came back at its cap of ${PR_LIMIT} — it may hold more PRs than it showed`);
584
- st.openPrs = prs.map((p) => p.number).sort((a, b) => a - b);
585
- this.save();
629
+ const r = this.gh("collision", ["pr", "list", "--state", "open", "--limit", String(PR_LIMIT), "--json", "number,headRefName"]);
630
+ if (r.status !== 0) return this.stop(withText(`gh pr list failed (${r.how})`, r));
631
+ let prs;
632
+ try {
633
+ prs = JSON.parse(r.stdout);
634
+ } catch (e) {
635
+ return this.stop(`could not parse gh pr list: ${e.message}`);
636
+ }
637
+ if (!Array.isArray(prs) || !prs.every((p) => Number.isInteger(p?.number) && typeof p.headRefName === "string")) {
638
+ return this.stop("gh pr list did not print a JSON array of PRs with their number and head branch");
639
+ }
640
+ if (prs.length >= PR_LIMIT) return this.stop(`gh pr list came back at its cap of ${PR_LIMIT} — it may hold more PRs than it showed`);
641
+ const wt = this.exec("collision", "git", "git", [], ["worktree", "list", "--porcelain", "-z"]);
642
+ if (wt.status !== 0) return this.stop(withText(`git worktree list failed (${wt.how})`, wt));
643
+ const refs = this.exec("collision", "git", "git", [], ["for-each-ref", "--format=%(refname:short)%09%(upstream:track)", "refs/heads"]);
644
+ if (refs.status !== 0) return this.stop(withText(`git for-each-ref failed (${refs.how})`, refs));
645
+ const gone = goneBranches(refs.stdout);
646
+
647
+ const paths = [...new Set([...briefPaths(briefText(this.issue()).trim()), ...st.addedPaths])];
648
+ // A path names a changed file, a directory above it (with or without its trailing `/`), or, with no `/`, a file by its name.
649
+ const hits = (p, f) => {
650
+ const q = p.replace(/\/+$/, "");
651
+ return f === q || f.startsWith(`${q}/`) || (!q.includes("/") && f.split("/").pop() === q);
652
+ };
653
+ const shared = (stdout, sep) => {
654
+ const files = stdout.split(sep).filter((f) => f !== "");
655
+ return paths.filter((p) => files.some((f) => hits(p, f)));
656
+ };
657
+ const overlaps = [];
658
+ const unread = [];
659
+ for (const pr of prs.map((p) => p.number).sort((a, b) => a - b)) {
660
+ const d = this.gh("collision", ["pr", "diff", String(pr), "--name-only"]);
661
+ const common = shared(d.stdout, "\n");
662
+ if (d.status !== 0) unread.push(`gh pr diff ${pr} (${failure(d)})`);
663
+ // Fail closed, as pr-overlap.mjs does: a PR with no changed file is a query that did not work, never "no overlap".
664
+ else if (d.stdout === "") unread.push(`gh pr diff ${pr} (printed no file)`);
665
+ else if (common.length > 0) overlaps.push({ pr, paths: common });
666
+ }
667
+ const heads = new Map(prs.map((p) => [p.headRefName, p.number]));
668
+ for (const branch of worktreeBranches(wt.stdout).filter((b) => b !== "main" && !gone.has(b)).sort()) {
669
+ const range = `origin/main...${branch}`;
670
+ // NUL-separated: git quotes a non-ASCII path in the newline form, and a quoted name equals no path the brief spells.
671
+ const d = this.exec("collision", "git", "git", [], ["diff", "--name-only", "-z", range, "--"]);
672
+ const common = shared(d.stdout, "\0");
673
+ if (d.status !== 0) unread.push(`git diff --name-only ${range} (${failure(d)})`);
674
+ else if (common.length > 0) overlaps.push({ branch, pr: heads.get(branch) ?? null, paths: common });
586
675
  }
676
+ st.scans.push({ at: now(), paths, overlaps, unread });
587
677
  return this.ask("collision", this.collisionContext());
588
678
  }
589
679
 
@@ -774,12 +864,17 @@ class Pull {
774
864
  return this.leave(`relabelled ${label}: ${token}`);
775
865
  }
776
866
 
777
- answer_collision(token) {
867
+ answer_collision(token, text) {
778
868
  const { st } = this;
779
869
  if (token === "clear") {
780
870
  st.stage = "claim";
781
871
  return null;
782
872
  }
873
+ // The stage stays at collision, so the scan runs again and the question is asked again.
874
+ if (token === "paths") {
875
+ for (const p of text.split(/\s+/)) if (!st.addedPaths.includes(p)) st.addedPaths.push(p);
876
+ return null;
877
+ }
783
878
  const r = this.script("exclude", "ledger.mjs", ["row", String(st.ticket), `excluded · ${token}`]);
784
879
  if (r.status !== 0) return this.stop(withText(`ledger.mjs row failed (${r.how})`, r));
785
880
  return this.leave(`excluded · ${token}`);
@@ -382,7 +382,8 @@ retries one that failed while applying an answer (the question stands), and
382
382
  after a router refusal `next --head <N>` starts the ticket over; exit 3 is a
383
383
  usage mistake, or an answer the question does not take, and records nothing.
384
384
  The record is `<scratch>/impl-<N>/pull.json`. The steps below are what
385
- the driver runs, and the guidance for answering `staleness` and `decided`.
385
+ the driver runs, and the guidance for answering `staleness`, `decided` and
386
+ `collision`.
386
387
 
387
388
  One free implementer slot is one Pull. The tick names the heads — `PULL #<n>
388
389
  #<m>`, one per free slot — and each Pull runs this sequence on its head,
@@ -592,22 +593,19 @@ terminating in exactly one dispatch or an empty shortlist:
592
593
 
593
594
  No sizing agent here. `sizing-a-ticket` picks the *process path*, and that is
594
595
  phase 2's call, after the ticket is claimed.
595
- 4. **Collision scan against open PRs and live branches** — the ticket is an
596
- *un-implemented issue* with no diff, so infer its target files from the brief
597
- (the paths it names) and compare them against each open PR's `gh pr diff <PR>
598
- --name-only` **and** each live worktree's `git diff --name-only
599
- origin/main...<branch>` (the branches `git worktree list` names) — a member
600
- mid-work has a branch before it has a PR. `pr-overlap.mjs` is **PR-vs-PR
601
- only** — it runs `gh pr diff` on *both* args and errors on an issue number,
602
- so use it the way the merge bot does (PR vs PR), never on a ticket here.
603
- Step 2 catches a ticket already taken, not one that *edits a file an open PR
604
- edits*. Overlap → exclude it `behind-pr:#M`, next entry. The tracker cannot
605
- express this, and it is what actually stalls a Pull's PR at merge.
606
- 5. **Sequence check** — the brief's `Out of scope` names an open ticket this one
607
- sequences after → exclude it `behind-issue:#M`, next entry. That lives in the
608
- brief, is invisible to the dependency scan, and oldest-first order puts a
609
- chain's members next to each other, where two consecutive numbers read as
610
- two independent tickets.
596
+ 4. **Collision scan against open PRs and live branches** — `pull.mjs` runs it
597
+ before Q3 and prints one line per open PR or live worktree branch that shares
598
+ a path with the brief, `no overlap` when none, and `could not look: <which>`
599
+ for one it could not read. Answer Q3 off those lines: an overlap →
600
+ `behind-pr:#M`, a branch with no PR yet by its name; a target file the brief
601
+ does not spell as a path → `paths <p>…`, which scans again and asks again. A
602
+ PR the scan could not read is still yours to judge, and `behind-pr` on it is
603
+ taken. `pr-overlap.mjs` is **PR-vs-PR only** — it runs `gh pr diff` on both
604
+ args and errors on an issue number, so never run it on a ticket here.
605
+ 5. **Sequence check** — the `#N` refs of the brief's `Out of scope` section are
606
+ printed beside Q3 as the candidates: one naming an open ticket this one
607
+ sequences after → `behind-issue:#M`. No overlap and no such ticket →
608
+ `clear`.
611
609
  6. **Claim** — `~/.fleet/bin/fleet-run claim-ticket.sh <N> impl-<N> implementer
612
610
  --apply` (below). Exit 2 → treat it as taken, next entry.
613
611
  7. **Route, row, then dispatch** — `~/.fleet/bin/fleet-run ticket-router.mjs route