@feigi/fleet-ctl 3.72.4 → 3.72.5

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.72.4",
3
+ "version": "3.72.5",
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": {
@@ -30,8 +30,15 @@
30
30
  // follow the --pr rule below.
31
31
  // impl-<N>/
32
32
  // removed when issue #N is CLOSED, or OPEN with no live
33
- // impl-<N>[-x] token and idle past the retention, so a bailed or
34
- // released ticket's partition is not inherited by its next claim.
33
+ // impl-<N>[-x] token, no claim worktree standing, and idle past
34
+ // the retention, so a bailed or released ticket's partition is not
35
+ // inherited by its next claim. A claim worktree stands when a
36
+ // worktree directly under this checkout's .worktrees/ names N as a
37
+ // whole segment of its directory name and that directory is there:
38
+ // an OPEN ticket's partition is kept while one stands, however idle.
39
+ // A claim worktree that cannot be checked — `git worktree list`
40
+ // failing, a .worktrees/ that cannot be read, or a directory there
41
+ // named for N that git does not list — keeps it, with that reason.
35
42
  // reports/<member>.json
36
43
  // a member's report file. `reports/` itself is never removed, and
37
44
  // is not aged out whole: each file is judged on its own, by the
@@ -93,7 +100,7 @@
93
100
  // read for any reason other than ENOENT. Nothing is removed before a refusal.
94
101
 
95
102
  import { spawnSync } from "node:child_process";
96
- import { lstatSync, readdirSync, statSync } from "node:fs";
103
+ import { lstatSync, readdirSync, realpathSync, statSync } from "node:fs";
97
104
  import { basename, dirname, isAbsolute, join, resolve } from "node:path";
98
105
  import { fileURLToPath } from "node:url";
99
106
  import { defineFlags, isDigits, makeDie, writeAll } from "./arg.mjs";
@@ -205,26 +212,90 @@ function liveNames(file) {
205
212
  return liveOnLedger(d.rows, d.dispatched).tokens.map((t) => t.name);
206
213
  }
207
214
 
208
- // The ledger file, or null when it is absent. Only ENOENT is absence; a
209
- // workspace that cannot be resolved, or a path that cannot be looked up,
210
- // refuses. `lstat`, so a dangling symlink is a ledger that cannot be read.
215
+ // The checkout the fleet dir sits in, and its ledger file — `file` null when it
216
+ // is absent. Only ENOENT is absence; a workspace that cannot be resolved, or a
217
+ // path that cannot be looked up, refuses. `lstat`, so a dangling symlink is a
218
+ // ledger that cannot be read.
211
219
  function ledgerFile() {
212
- let file;
220
+ let dir;
213
221
  try {
214
- file = fleetFile("ledger.md");
222
+ dir = fleetFile(null);
215
223
  } catch (e) {
216
224
  if (!(e instanceof FleetDirUnresolvable)) throw e;
217
225
  die(`${e.message} — the ledger cannot be found, so which members are live is unknown`);
218
226
  }
227
+ const file = join(dir, "ledger.md");
219
228
  try {
220
229
  lstatSync(file);
221
- return file;
230
+ return { checkout: dirname(dir), file };
222
231
  } catch (e) {
223
- if (e.code === "ENOENT") return null;
232
+ if (e.code === "ENOENT") return { checkout: dirname(dir), file: null };
224
233
  die(`could not look for the ledger ${file}: ${e.message}`);
225
234
  }
226
235
  }
227
236
 
237
+ // The linked worktrees under `checkout`'s `.worktrees/`, read the way
238
+ // stranded.mjs reads them. `paths` is every path git lists; `home` is that
239
+ // directory's realpath, or null when it does not exist, so no worktree sits
240
+ // inside it. `{unknown}` when git's registry cannot be read or the directory
241
+ // cannot be resolved; what stands inside it is read in claimWorktree(). An
242
+ // unknown is never read as no worktree.
243
+ function worktreeListing(checkout) {
244
+ const r = spawnSync("git", ["worktree", "list", "--porcelain", "-z"], {
245
+ cwd: checkout, encoding: "utf8", maxBuffer: MAX_BUFFER, timeout: GH_TIMEOUT_MS, env: gitEnv(),
246
+ });
247
+ if (r.error || r.status !== 0) return { unknown: `git worktree list failed (${how(r)})` };
248
+ const paths = r.stdout.split("\0").filter((f) => f.startsWith("worktree ")).map((f) => f.slice("worktree ".length));
249
+ const dir = join(checkout, ".worktrees");
250
+ try {
251
+ return { paths, home: realpathSync(dir) };
252
+ } catch (e) {
253
+ if (e.code === "ENOENT") return { paths, home: null };
254
+ return { unknown: `cannot resolve ${dir} (${e.code ?? e.message})` };
255
+ }
256
+ }
257
+
258
+ // Whether a claim worktree stands for issue `n`: a listed worktree directly under
259
+ // `.worktrees/` whose directory name names `n` as a whole segment, and whose
260
+ // directory is still there. `{stands}` is true or false; `{unknown}`
261
+ // when a candidate's presence cannot be told. A registration whose directory is
262
+ // gone stands for nothing. A directory under `.worktrees/` named for `n` that git
263
+ // does not list is an unknown, never none: git drops a worktree whose
264
+ // registration it cannot read — an unreadable `.git/worktrees/`, an empty
265
+ // `gitdir` file — from the list and still exits 0. The segment rule is
266
+ // stranded.mjs's, for a claim's own worktree.
267
+ function claimWorktree(listing, n) {
268
+ if (listing.unknown !== undefined) return { unknown: listing.unknown };
269
+ if (listing.home === null) return { stands: false };
270
+ const named = new RegExp(`(^|[/-])${n}([-/]|$)`);
271
+ for (const p of listing.paths) {
272
+ if (!named.test(basename(p))) continue;
273
+ try {
274
+ if (realpathSync(dirname(p)) !== listing.home) continue;
275
+ } catch (e) {
276
+ if (e.code === "ENOENT") continue;
277
+ return { unknown: `cannot resolve ${dirname(p)} (${e.code ?? e.message})` };
278
+ }
279
+ try {
280
+ statSync(p);
281
+ } catch (e) {
282
+ if (e.code === "ENOENT") continue;
283
+ return { unknown: `cannot tell whether ${p} exists (${e.code ?? e.message})` };
284
+ }
285
+ return { stands: true };
286
+ }
287
+ let names;
288
+ try {
289
+ names = readdirSync(listing.home);
290
+ } catch (e) {
291
+ if (e.code === "ENOENT") return { stands: false };
292
+ return { unknown: `cannot read ${listing.home} (${e.code ?? e.message})` };
293
+ }
294
+ const unlisted = names.find((name) => named.test(name));
295
+ if (unlisted !== undefined) return { unknown: `${join(listing.home, unlisted)} is there but git does not list it as a worktree` };
296
+ return { stands: false };
297
+ }
298
+
228
299
  // `{type, state}` per number GitHub answered (type "Issue" or "PullRequest",
229
300
  // or null for a number that names nothing), or `{unreachable}` for a number
230
301
  // that has no usable answer. GitHub resolves every alias it can and nulls the
@@ -285,7 +356,7 @@ function githubStates(numbers) {
285
356
  }
286
357
 
287
358
  // The root sweep's verdicts: `{remove: [{path, reason}], kept: [{path, reason}]}`.
288
- function judgeRoot(root, names, live, now) {
359
+ function judgeRoot(root, names, live, now, listing) {
289
360
  const cutoff = now - RETENTION_MS;
290
361
  const remove = [];
291
362
  const kept = [];
@@ -406,6 +477,15 @@ function judgeRoot(root, names, live, now) {
406
477
  kept.push({ path, reason: `issue #${n} is OPEN and live ${holder} names it` });
407
478
  continue;
408
479
  }
480
+ const claim = claimWorktree(listing, n);
481
+ if (claim.unknown !== undefined) {
482
+ kept.push({ path, reason: `its claim worktree cannot be checked: ${claim.unknown}` });
483
+ continue;
484
+ }
485
+ if (claim.stands) {
486
+ kept.push({ path, reason: "claim worktree stands" });
487
+ continue;
488
+ }
409
489
  if (freshUnder(path, cutoff) === null) remove.push({ path, reason: `issue #${n} is OPEN, no live impl-${n} token, idle past 7 days` });
410
490
  else kept.push({ path, reason: `issue #${n} is OPEN; modified within 7 days` });
411
491
  }
@@ -530,7 +610,8 @@ function main() {
530
610
  failed.push({ path: dirname(root), error: e.message });
531
611
  }
532
612
  } else {
533
- ledger = ledgerFile();
613
+ const found = ledgerFile();
614
+ ledger = found.file;
534
615
  if (ledger === null && !afterRotate) die("no .fleet/ledger.md — without it no live member can hold its scratch, so nothing is reclaimed; pass --after-rotate only straight after `ledger.mjs rotate`");
535
616
  const live = ledger === null ? [] : liveNames(ledger);
536
617
  let names;
@@ -539,7 +620,8 @@ function main() {
539
620
  } catch (e) {
540
621
  die(`could not list --root ${root}: ${e.message}`);
541
622
  }
542
- ({ remove, kept } = judgeRoot(root, names, live, now));
623
+ const listing = worktreeListing(found.checkout);
624
+ ({ remove, kept } = judgeRoot(root, names, live, now, listing));
543
625
  }
544
626
 
545
627
  const removed = [];
@@ -2027,18 +2027,15 @@ age, reading which members are live off `.fleet/ledger.md`:
2027
2027
  sweep — its token names no PR. An OPEN PR's `pr<M>/` stays, all but its
2028
2028
  `run-*` roots idle past 7 days.
2029
2029
  - **`impl-<N>/`** goes once issue #N is CLOSED, or when it is OPEN, no live
2030
- `impl-<N>` token names it and nothing under it has changed in 7 days.
2030
+ `impl-<N>` token names it, no claim worktree under `.worktrees/` still stands
2031
+ for it, and nothing under it has changed in 7 days.
2031
2032
  - **Anything else at the root** — your own ad-hoc files included — goes
2032
2033
  once nothing under it has changed in 7 days and no live token's name
2033
2034
  appears in its name.
2034
2035
 
2035
- No ledger refuses this call (exit 2, nothing removed): mid-run there is
2036
- always one, so its absence is a fault to look at, never permission. A `gh`
2037
- that cannot answer keeps every PR- and ticket-keyed entry. It prints one
2038
- JSON receipt — `removed`, `kept` and `failed`, each entry with its reason —
2039
- and exits 0 even when a removal failed; record nothing on the ledger. The
2040
- review snapshot runs the same script as `--pr <N>` on its own PR's stale
2041
- `run-*` roots.
2036
+ The review snapshot runs the same script as `--pr <N>` on its own PR's stale
2037
+ `run-*` roots. What this sweep does with no ledger, or with a `gh` that cannot
2038
+ answer, is in references/phase-0.md, under **Reclaim scratch**.
2042
2039
 
2043
2040
  ### Release the claims that never became PRs — at end of run, and on drain
2044
2041
 
@@ -265,6 +265,14 @@ as "no live token" — rotate's own verdict — where without it the script
265
265
  refuses. A ledger rotate did write is read as usual. Record nothing on the
266
266
  ledger and act on nothing in its receipt.
267
267
 
268
+ Without `--after-rotate` — the per-pass sweep **Reclaim scratch after each
269
+ merge pass** runs — no ledger refuses the call (exit 2, nothing removed):
270
+ mid-run there is always one, so its absence is a fault to look at, never
271
+ permission. A `gh` that cannot answer keeps every PR- and ticket-keyed entry.
272
+ It prints one JSON receipt — `removed`, `kept` and `failed`, each entry with
273
+ its reason — and exits 0 even when a removal failed; record nothing on the
274
+ ledger.
275
+
268
276
  Straight after that, the same step reclaims the other keys under the scratch parent:
269
277
  `~/.fleet/bin/fleet-run reclaim-scratch.mjs --siblings --root <scratch> --apply`. A key
270
278
  goes whole when nothing under it has changed in 7 days, and stays whole otherwise. The