@feigi/fleet-ctl 3.72.0 → 3.72.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.72.0",
3
+ "version": "3.72.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": {
@@ -125,9 +125,17 @@ export function fleetFile(name, { cwd = process.cwd(), timeoutMs, canonicalise =
125
125
  export function scratchRoot({ cwd = process.cwd(), timeoutMs, home = homedir() } = {}) {
126
126
  const workspace = dirname(fleetFile(null, { cwd, timeoutMs, canonicalise: true }));
127
127
  const hex = createHash("sha256").update(workspace).digest("hex").slice(0, 8);
128
- return join(home, ".fleet", "scratch", `${basename(workspace)}-${hex}`);
128
+ return join(scratchParent(home), `${basename(workspace)}-${hex}`);
129
129
  }
130
130
 
131
+ /** The directory every run's scratch root sits directly in: `<home>/.fleet/scratch`. */
132
+ export function scratchParent(home = homedir()) {
133
+ return join(home, ".fleet", "scratch");
134
+ }
135
+
136
+ /** The shape of a scratch root's last segment: `<workspace basename>-<8 hex>`. */
137
+ export const SCRATCH_KEY_SHAPE = /-[0-9a-f]{8}$/;
138
+
131
139
  function nearestExisting(path) {
132
140
  let at = path;
133
141
  while (!existsSync(at)) {
@@ -40,8 +40,11 @@
40
40
  // cockpit `board.mjs serve --open` launched detached, `--spend-dir`
41
41
  // passed through. Recorded; never fails the run.
42
42
  // rotate `ledger.mjs rotate`. Exit 2 is a refusal and fails the step.
43
- // reclaim `reclaim-scratch.mjs --root <scratch> --after-rotate --apply`.
44
- // Recorded; never fails the run.
43
+ // reclaim `reclaim-scratch.mjs --root <scratch> --after-rotate --apply`,
44
+ // then `reclaim-scratch.mjs --siblings --root <scratch> --apply`
45
+ // for the other keys under the scratch parent, which runs
46
+ // whatever the first answered. What either receipt's failed[]
47
+ // names is listed. Recorded; never fails the run.
45
48
  // recipe `derive-testcmd.sh . install` and `. test --at origin/main`.
46
49
  // Either exiting 1 is a cache with nothing valid in it: pending,
47
50
  // `NEED derive-recipe`. Exit 3 (a tool the reader could not run)
@@ -277,8 +280,28 @@ function rotate(ctx) {
277
280
  }
278
281
 
279
282
  function reclaim({ cwd, scratch }) {
280
- const r = node("reclaim-scratch.mjs", ["--root", scratch, "--after-rotate", "--apply"], { cwd });
281
- return ok(r.status === 0 ? "scratch reclaimed" : withText(`reclaim-scratch.mjs ${r.how}, not enforced`, r));
283
+ const live = node("reclaim-scratch.mjs", ["--root", scratch, "--after-rotate", "--apply"], { cwd });
284
+ // The sibling pass runs whatever the live sweep answered: a failure there is recorded beside it, never a reason to skip it.
285
+ const siblings = node("reclaim-scratch.mjs", ["--siblings", "--root", scratch, "--apply"], { cwd });
286
+ return ok([
287
+ live.status === 0 ? "scratch reclaimed" : withText(`reclaim-scratch.mjs ${live.how}, not enforced`, live),
288
+ ...unreclaimed("scratch root", live),
289
+ ...(siblings.status === 0 ? unreclaimed("sibling keys", siblings) : [withText(`sibling keys: reclaim-scratch.mjs ${siblings.how}, not enforced`, siblings)]),
290
+ ].join("\n"));
291
+ }
292
+
293
+ // A pass that exited 0 still lists in its receipt's `failed[]` what it could
294
+ // not judge or remove: each is named here, and none fails the step.
295
+ function unreclaimed(what, r) {
296
+ if (r.status !== 0) return [];
297
+ let entries;
298
+ try {
299
+ ({ failed: entries } = JSON.parse(r.stdout));
300
+ } catch (e) {
301
+ return [`${what}: could not read reclaim-scratch.mjs's receipt: ${e.message}`];
302
+ }
303
+ if (!Array.isArray(entries)) return [`${what}: reclaim-scratch.mjs's receipt holds no failed[]`];
304
+ return entries.map((x) => `${what}: could not reclaim ${x?.path} (${x?.error})`);
282
305
  }
283
306
 
284
307
  function recipe({ cwd }) {
@@ -6,11 +6,19 @@
6
6
  //
7
7
  // reclaim-scratch.mjs --root <abs> [--after-rotate] [--apply]
8
8
  // reclaim-scratch.mjs --pr <N> --root <abs> [--apply]
9
+ // reclaim-scratch.mjs --siblings --root <abs> [--apply]
9
10
  //
10
11
  // Dry run by default; `--apply` removes. `--root` is required, an absolute
11
12
  // path to an existing directory: the controller passes the path
12
13
  // scratch-root.mjs prints.
13
14
  //
15
+ // `--siblings` is the sibling pass, its own invocation: `--root` names the live
16
+ // key, which must be this workspace's own scratch root (the path
17
+ // scratch-root.mjs prints), and the pass judges the other key-shaped
18
+ // directories beside it by age alone (judgeSiblings below), judging each again
19
+ // just before `--apply` removes it. It reads no ledger and is never combined
20
+ // with --pr or --after-rotate.
21
+ //
14
22
  // THE ROOT SWEEP judges every entry directly under `--root`:
15
23
  // pr<M>/, dispositions-fix-pr-<M>[-x].json
16
24
  // removed when PR #M is MERGED or CLOSED on GitHub and no live
@@ -86,10 +94,10 @@
86
94
 
87
95
  import { spawnSync } from "node:child_process";
88
96
  import { lstatSync, readdirSync, statSync } from "node:fs";
89
- import { dirname, isAbsolute, join, resolve } from "node:path";
97
+ import { basename, dirname, isAbsolute, join, resolve } from "node:path";
90
98
  import { fileURLToPath } from "node:url";
91
99
  import { defineFlags, isDigits, makeDie, writeAll } from "./arg.mjs";
92
- import { fleetFile, FleetDirUnresolvable } from "./fleet-dir.mjs";
100
+ import { fleetFile, FleetDirUnresolvable, SCRATCH_KEY_SHAPE, scratchRoot } from "./fleet-dir.mjs";
93
101
  import { gitEnv } from "./git-env.mjs";
94
102
  import { isCLI } from "./is-cli.mjs";
95
103
  import { liveOnLedger, parseMember, REVIEWER } from "./ledger-grammar.mjs";
@@ -143,8 +151,10 @@ function how(r) {
143
151
  * `cutoff`, or null when everything is older. Symlinks are not followed. A
144
152
  * directory that cannot be listed counts its own mtime only, and so does one
145
153
  * that can be listed but not searched: an entry inside it whose lstat is
146
- * refused is skipped, its parent's mtime already counted. */
147
- export function freshUnder(path, cutoff) {
154
+ * refused is skipped, its parent's mtime already counted. Given `unreadable`,
155
+ * an array, each path skipped for EACCES or EPERM is pushed onto it as
156
+ * `<path>: <code>`. */
157
+ export function freshUnder(path, cutoff, unreadable = null) {
148
158
  const stack = [path];
149
159
  while (stack.length) {
150
160
  const p = stack.pop();
@@ -152,6 +162,7 @@ export function freshUnder(path, cutoff) {
152
162
  try {
153
163
  st = lstatSync(p);
154
164
  } catch (e) {
165
+ if (e.code === "EACCES" || e.code === "EPERM") unreadable?.push(`${p}: ${e.code}`);
155
166
  if (e.code === "ENOENT" || e.code === "EACCES" || e.code === "EPERM") continue;
156
167
  throw e;
157
168
  }
@@ -161,6 +172,7 @@ export function freshUnder(path, cutoff) {
161
172
  try {
162
173
  names = readdirSync(p);
163
174
  } catch (e) {
175
+ if (e.code === "EACCES" || e.code === "EPERM") unreadable?.push(`${p}: ${e.code}`);
164
176
  if (e.code === "EACCES" || e.code === "EPERM" || e.code === "ENOENT") continue;
165
177
  throw e;
166
178
  }
@@ -421,9 +433,46 @@ function staleRunRoots(parent, cutoff, kept) {
421
433
  return remove;
422
434
  }
423
435
 
436
+ // THE SIBLING PASS judges every key-shaped directory directly under the scratch
437
+ // parent of `root`, other than the live key, by age alone: one with nothing
438
+ // modified within the retention goes whole; one with anything fresh stays whole,
439
+ // and so does one with any part that could not be read, since what that part
440
+ // holds is unknown. Other entries under the parent, links and the live key are
441
+ // not judged at all. The keys are judged in name order.
442
+ function judgeSiblings(root, cutoff) {
443
+ const parent = dirname(root);
444
+ const live = basename(root);
445
+ const remove = [];
446
+ const kept = [];
447
+ const failed = [];
448
+ const entries = readdirSync(parent, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
449
+ for (const entry of entries) {
450
+ if (!entry.isDirectory() || entry.name === live || !SCRATCH_KEY_SHAPE.test(entry.name)) continue;
451
+ const path = join(parent, entry.name);
452
+ let verdict;
453
+ try {
454
+ verdict = judgeSibling(path, cutoff);
455
+ } catch (e) {
456
+ failed.push({ path, error: e.message });
457
+ continue;
458
+ }
459
+ (verdict.idle ? remove : kept).push({ path, reason: verdict.reason });
460
+ }
461
+ return { remove, kept, failed };
462
+ }
463
+
464
+ // One sibling key's verdict: `idle` only when nothing under it is fresh and
465
+ // every part of it could be read.
466
+ function judgeSibling(path, cutoff) {
467
+ const unreadable = [];
468
+ if (freshUnder(path, cutoff, unreadable) !== null) return { idle: false, reason: "sibling key modified within 7 days" };
469
+ if (unreadable.length > 0) return { idle: false, reason: `sibling key could not be read whole (${unreadable[0]})` };
470
+ return { idle: true, reason: "sibling key idle past 7 days" };
471
+ }
472
+
424
473
  function main() {
425
474
  const { arg, has, sweep, stray } = defineFlags(die, {
426
- flags: { root: "value", pr: "value", "after-rotate": "bool", apply: "bool" },
475
+ flags: { root: "value", pr: "value", "after-rotate": "bool", apply: "bool", siblings: "bool" },
427
476
  });
428
477
  sweep();
429
478
  stray();
@@ -431,7 +480,8 @@ function main() {
431
480
  const pr = arg("pr");
432
481
  const afterRotate = has("after-rotate");
433
482
  const apply = has("apply");
434
- const usage = "usage: reclaim-scratch.mjs --root <abs> [--after-rotate] [--apply] | --pr <N> --root <abs> [--apply]";
483
+ const siblings = has("siblings");
484
+ const usage = "usage: reclaim-scratch.mjs --root <abs> [--after-rotate] [--apply] | --pr <N> --root <abs> [--apply] | --siblings --root <abs> [--apply]";
435
485
  if (rootArg === null) die(`--root is required — ${usage}`);
436
486
  if (!isAbsolute(rootArg)) die(`--root must be an absolute path, got ${JSON.stringify(rootArg)}`);
437
487
  if (pr !== null && !isDigits(pr)) die(`--pr must be a PR number, got ${JSON.stringify(pr)}`);
@@ -442,6 +492,19 @@ function main() {
442
492
  } catch (e) {
443
493
  die(`--root ${root} is not a directory: ${e.message}`);
444
494
  }
495
+ if (siblings) {
496
+ if (pr !== null) die("--siblings and --pr are separate passes");
497
+ if (afterRotate) die("--after-rotate is the root sweep's; --siblings reads no ledger");
498
+ // The live key is never judged, so it must be this workspace's own: any other
499
+ // key passed here would put the real live key among the judged siblings.
500
+ let own;
501
+ try {
502
+ own = scratchRoot();
503
+ } catch (e) {
504
+ die(`--siblings judges the keys beside this workspace's scratch root, which could not be resolved: ${e.message}`);
505
+ }
506
+ if (root !== own) die(`--siblings judges the keys beside the live one: --root must be this workspace's scratch root ${own}, got ${root}`);
507
+ }
445
508
 
446
509
  const now = Date.now();
447
510
  let ledger = null;
@@ -456,6 +519,16 @@ function main() {
456
519
  remove = [];
457
520
  failed.push({ path: parent, error: e.message });
458
521
  }
522
+ } else if (siblings) {
523
+ try {
524
+ const judged = judgeSiblings(root, now - RETENTION_MS);
525
+ remove = judged.remove;
526
+ kept = judged.kept;
527
+ failed.push(...judged.failed);
528
+ } catch (e) {
529
+ remove = [];
530
+ failed.push({ path: dirname(root), error: e.message });
531
+ }
459
532
  } else {
460
533
  ledger = ledgerFile();
461
534
  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`");
@@ -477,6 +550,14 @@ function main() {
477
550
  }
478
551
  let error;
479
552
  try {
553
+ // A sibling key belongs to another workspace's runs, which can write to it
554
+ // while an earlier key is still being removed: it is judged again here, so
555
+ // a key that turned fresh since the pass judged it is kept.
556
+ const again = siblings ? judgeSibling(entry.path, now - RETENTION_MS) : null;
557
+ if (again !== null && !again.idle) {
558
+ kept.push({ path: entry.path, reason: again.reason });
559
+ continue;
560
+ }
480
561
  error = removeEntry(entry.path);
481
562
  } catch (e) {
482
563
  error = e.message;
@@ -265,6 +265,13 @@ 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
+ Straight after that, the same step reclaims the other keys under the scratch parent:
269
+ `~/.fleet/bin/fleet-run reclaim-scratch.mjs --siblings --root <scratch> --apply`. A key
270
+ goes whole when nothing under it has changed in 7 days, and stays whole otherwise. The
271
+ call runs whatever the root sweep answered, and records nothing on the ledger. A non-zero
272
+ exit, and each path its receipt's `failed[]` names, is listed in the step's output; neither
273
+ stops the run.
274
+
268
275
  ## Derive the Recipe
269
276
 
270
277
  **Derive the Recipe — the Recipe derivation step — before the fold-in below