@feigi/fleet-ctl 3.147.4 → 3.147.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/evict.mjs +72 -37
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feigi/fleet-ctl",
3
- "version": "3.147.4",
3
+ "version": "3.147.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": {
package/scripts/evict.mjs CHANGED
@@ -21,7 +21,8 @@
21
21
  // `wt_op_state` reads, which reap.sh reads too;
22
22
  // - this script's own cwd is inside it;
23
23
  // - a live member token holds it (below);
24
- // - it carries the mark already;
24
+ // - it carries the mark and ignores nothing but the runner, or its ignored
25
+ // paths cannot be listed;
25
26
  // - `git status --porcelain -uall` in it is non-empty;
26
27
  // - HEAD moved or a restore ran in it within IDLE_MS: the newer of the
27
28
  // newest entry of the tree's own HEAD reflog (`logs/HEAD` in its admin
@@ -31,7 +32,10 @@
31
32
  // - anything about it cannot be read — its `.git`, its admin dir, a status,
32
33
  // a reflog. A worktree listing that cannot be read keeps the whole
33
34
  // `.worktrees/` directory, with that reason.
34
- // Every other candidate is evicted.
35
+ // Every other candidate is evicted. A tree that carries the mark and still
36
+ // holds ignored paths — a removal that partly failed, a restore's partial
37
+ // install, a restore still installing, an install by hand — meets the rest of
38
+ // those rules like an unmarked one, and when none holds it is evicted again.
35
39
  //
36
40
  // LIVE TOKENS. The ledger's live tokens (ledger-grammar.mjs's liveOnLedger())
37
41
  // of the members that work a claim tree's ticket or PR — `impl-<N>`,
@@ -49,11 +53,13 @@
49
53
  // an absent ledger is no live token. Only ENOENT is absence.
50
54
  //
51
55
  // `--apply` judges each tree to evict again, writes the mark (EVICTED, a file
52
- // in the tree's git admin dir), then removes every ignored path but the
53
- // runner through reclaim-scratch.mjs's one removal sequence. Tracked files,
54
- // HEAD, the branch and the registration are not touched. Best-effort per
55
- // tree: a tree whose mark cannot be written, or a path in it that cannot be
56
- // removed, lands in `failed` and keeps its mark; the sweep goes on.
56
+ // in the tree's git admin dir) unless the tree carries it already, then removes
57
+ // every ignored path but the runner through reclaim-scratch.mjs's one removal
58
+ // sequence. Tracked files, HEAD, the branch and the registration are not
59
+ // touched. Best-effort per tree: a tree whose mark cannot be written, or a path
60
+ // in it that cannot be removed, lands in `failed`; the sweep goes on. A mark
61
+ // written stays as written, and a path that cannot be removed is tried again,
62
+ // and named in `failed` again, on every sweep until it is gone.
57
63
  //
58
64
  // Prints ONE JSON line:
59
65
  // {"applied","ledger":<abs path|null>,"evicted":[{"path","reason","ignored"}],
@@ -73,11 +79,14 @@
73
79
  // inside the repository only against origin/main, so the tree's own change, a
74
80
  // tree behind origin/main or a later word of the command can each produce it.
75
81
  // A status that is not empty BEFORE it refuses in words of its own, before
76
- // anything runs: that change is no Install step's. Only once the status after
77
- // it is empty does it write the restore time (RESTORED) and then remove the
78
- // mark; every refusal leaves the mark. A tree with no mark refuses: there is
79
- // nothing to restore. Without `--apply` it runs nothing and prints the command
80
- // it would run. Prints
82
+ // anything runs: that change is no Install step's. Once the checks ahead of
83
+ // the Install step pass, and before it starts, it writes the restore time
84
+ // (RESTORED), so the tree is held for IDLE_MS from the restore's start; a time
85
+ // that cannot be written refuses before the Install step runs. Only once the
86
+ // status after the Install step is empty does it remove the mark; every
87
+ // refusal leaves the mark. A tree with no mark refuses: there is nothing to
88
+ // restore. Without `--apply` it runs nothing and prints the command it would
89
+ // run. Prints
81
90
  // `{"applied","worktree","install"}`; exit 0 restored (or would), 2 refused.
82
91
  //
83
92
  // That install sequence is `installIn()`, exported: finisher-tree.mjs runs the
@@ -314,7 +323,17 @@ function judge(w, ctx) {
314
323
  if (held !== null) return { kept: held };
315
324
  const mark = evictionMark(w.path);
316
325
  if (mark.unknown !== undefined) return { kept: mark.unknown };
317
- if (mark.marked) return { kept: `already evicted: ${join(mark.dir, EVICTED)} is there` };
326
+ // A marked tree is judged like any other once it is known to hold ignored
327
+ // paths; one that holds none is the eviction done.
328
+ let ignored;
329
+ if (mark.marked) {
330
+ try {
331
+ ignored = ignoredPaths(w.path);
332
+ } catch (e) {
333
+ return { kept: e.message };
334
+ }
335
+ if (ignored.length === 0) return { kept: `already evicted: ${join(mark.dir, EVICTED)} is there` };
336
+ }
318
337
  const status = git(w.path, ["status", "--porcelain", "-uall"]);
319
338
  if (status.error || status.status !== 0) return { kept: `cannot read its status (${how(status)})` };
320
339
  if (status.stdout !== "") return { kept: `git status is not clean (first: ${status.stdout.split("\n")[0]})` };
@@ -329,22 +348,27 @@ function judge(w, ctx) {
329
348
  const last = Math.max(moved, restored ?? 0);
330
349
  const what = restored !== null && restored >= moved ? "restored" : "HEAD last moved";
331
350
  if (!isIdle(ctx.now, last)) return { kept: `${what} ${new Date(last).toISOString()}, within ${IDLE_MS / DAY_MS} days` };
332
- let ignored;
333
- try {
334
- ignored = ignoredPaths(w.path);
335
- } catch (e) {
336
- return { kept: e.message };
351
+ if (!mark.marked) {
352
+ try {
353
+ ignored = ignoredPaths(w.path);
354
+ } catch (e) {
355
+ return { kept: e.message };
356
+ }
357
+ if (ignored.length === 0) return { kept: `it ignores nothing but ${RUNNER}` };
337
358
  }
338
- if (ignored.length === 0) return { kept: `it ignores nothing but ${RUNNER}` };
339
- return { evict: { reason: `${what} ${new Date(last).toISOString()}, over ${IDLE_MS / DAY_MS} days ago`, ignored, dir: admin.dir } };
359
+ const idle = `${what} ${new Date(last).toISOString()}, over ${IDLE_MS / DAY_MS} days ago`;
360
+ return { evict: { reason: mark.marked ? `already evicted, ignored paths left — ${idle}` : idle, ignored, dir: admin.dir, marked: mark.marked } };
340
361
  }
341
362
 
342
- // Mark the tree, then remove each ignored path in it; null, or the error.
363
+ // Mark the tree, unless it carries the mark already, then remove each ignored
364
+ // path in it; null, or the error.
343
365
  function evictTree(path, verdict, now) {
344
- try {
345
- writeFileSync(join(verdict.dir, EVICTED), `${new Date(now).toISOString()}\n`);
346
- } catch (e) {
347
- return `could not write ${join(verdict.dir, EVICTED)} (${e.code ?? e.message}), so nothing was removed`;
366
+ if (!verdict.marked) {
367
+ try {
368
+ writeFileSync(join(verdict.dir, EVICTED), `${new Date(now).toISOString()}\n`);
369
+ } catch (e) {
370
+ return `could not write ${join(verdict.dir, EVICTED)} (${e.code ?? e.message}), so nothing was removed`;
371
+ }
348
372
  }
349
373
  const errors = [];
350
374
  for (const p of verdict.ignored) {
@@ -403,17 +427,21 @@ function sweep({ afterRotate, apply }) {
403
427
  * derive derive-testcmd.sh would not name the Install step
404
428
  * status-before the tree state could not be read before the Install step
405
429
  * dirty-before the tree was not clean before it: nothing ran
430
+ * before-run `beforeRun` refused: nothing ran
406
431
  * not-run the Install step could not be started, or exited 126 or 127
407
432
  * failed it was killed by a signal, or exited non-zero
408
433
  * status-after the tree state could not be read after it
409
434
  * dirtied it left the tree changed
410
435
  * `verb` names the operation in the dirty-before refusal, and `retry` whether
411
436
  * that refusal tells its reader to clear the change and run the operation again:
412
- * a restore's caller may, the finisher may not. `install` is null in a refusal
413
- * that came before the command was read. Nothing here writes a mark or a
414
- * restore time, and it never exits the process.
437
+ * a restore's caller may, the finisher may not. `beforeRun`, when given, is
438
+ * called with `apply` set, after every check ahead of the Install step has
439
+ * passed and just before it starts; it returns null, or the message of a
440
+ * refusal, which then keeps the Install step from running. `install` is null in
441
+ * a refusal that came before the command was read. Nothing here writes a mark
442
+ * or a restore time, and it never exits the process.
415
443
  */
416
- export function installIn(tree, apply, { verb = "restore", retry = true } = {}) {
444
+ export function installIn(tree, apply, { verb = "restore", retry = true, beforeRun = () => null } = {}) {
417
445
  const refuse = (kind, message, install = null, exit = null) => ({ refusal: { kind, message, install, exit } });
418
446
  const derived = run("sh", [join(SCRIPT_DIR, "derive-testcmd.sh"), tree, "install", "--at", "origin/main"]);
419
447
  const install = String(derived.stdout ?? "").replace(/\n$/, "");
@@ -429,6 +457,8 @@ export function installIn(tree, apply, { verb = "restore", retry = true } = {})
429
457
  return refuse("dirty-before", `${tree} is not clean before the ${verb} (first: ${before.stdout.split("\n")[0]}) — the Install step did not run${then}`, install);
430
458
  }
431
459
  if (!apply) return { install };
460
+ const stopped = beforeRun();
461
+ if (stopped !== null) return refuse("before-run", stopped, install);
432
462
  // Through `sh -c` with `CI` unset and the output dropped, as claim-ticket.sh
433
463
  // runs it; no timeout, as there.
434
464
  const r = run("sh", ["-c", install], { cwd: tree, drop: ["CI"], stdio: "ignore", timeout: 0 });
@@ -453,18 +483,23 @@ function restore(tree, apply) {
453
483
  const mark = evictionMark(tree);
454
484
  if (mark.unknown !== undefined) die(`cannot tell whether ${tree} is evicted: ${mark.unknown}`);
455
485
  if (!mark.marked) die(`${tree} is not evicted (no ${join(mark.dir, EVICTED)}) — nothing to restore`);
456
- const done = installIn(tree, apply);
457
- if (done.refusal) die(done.refusal.message);
458
- const { install } = done;
459
- if (apply) {
460
- // The restore time first: a mark removed with no time beside it would read
461
- // the tree idle by its HEAD alone and evict it again at once.
462
- const restored = join(mark.dir, RESTORED);
486
+ const restored = join(mark.dir, RESTORED);
487
+ // The restore time first, once the checks ahead of the Install step pass: the
488
+ // idle gate then holds the tree while it installs and, when the Install step
489
+ // is refused, for IDLE_MS after, and a mark removed with no time beside it
490
+ // would read the tree idle by its HEAD alone and evict it again at once.
491
+ const record = () => {
463
492
  try {
464
493
  writeFileSync(restored, `${new Date().toISOString()}\n`);
465
494
  } catch (e) {
466
- die(`installed in ${tree}, but could not record the restore in ${restored} (${e.code ?? e.message}) — the mark stays`);
495
+ return `could not record the restore in ${restored} (${e.code ?? e.message}), so the Install step did not run — the mark stays`;
467
496
  }
497
+ return null;
498
+ };
499
+ const done = installIn(tree, apply, { beforeRun: record });
500
+ if (done.refusal) die(done.refusal.message);
501
+ const { install } = done;
502
+ if (apply) {
468
503
  try {
469
504
  unlinkSync(join(mark.dir, EVICTED));
470
505
  } catch (e) {