@feigi/fleet-ctl 3.151.2 → 3.152.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 +1 -1
- package/scripts/evict-tree.mjs +154 -0
- package/scripts/evict.mjs +4 -24
- package/scripts/reclaim-scratch.mjs +24 -33
- package/scripts/review-core.mjs +69 -3
package/package.json
CHANGED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// What an Evict removes from a git tree, and the one removal sequence every
|
|
2
|
+
// removal here and in Reclaim goes through. evict.mjs (a claim tree),
|
|
3
|
+
// reclaim-scratch.mjs (a review run's snapshot and copies, in its `--pr` pass)
|
|
4
|
+
// and review-core.mjs (the same trees, when a review ends) all import it; a
|
|
5
|
+
// module of its own so none of the three loads another's CLI, and so the
|
|
6
|
+
// enumeration of "every path git ignores" is written once.
|
|
7
|
+
//
|
|
8
|
+
// IGNORED PATHS. `ignoredPaths(tree)` is every path `git ls-files --others
|
|
9
|
+
// --ignored --exclude-standard --directory` lists in the tree, but the
|
|
10
|
+
// `agent-test` runner: a directory is listed whole, once, and nothing inside
|
|
11
|
+
// one already listed. Tracked files, the tree's .git and untracked files git
|
|
12
|
+
// does not ignore are not in it.
|
|
13
|
+
//
|
|
14
|
+
// A TREE, for `evictTrees(root)`, is a directory that holds `.git` at its own
|
|
15
|
+
// top level and whose `git rev-parse --show-toplevel` resolves, by realpath,
|
|
16
|
+
// to that same directory. A directory that resolves to an enclosing repository
|
|
17
|
+
// — one with no `.git` of its own, or with one git cannot read — is never
|
|
18
|
+
// stripped, and git is never asked about a directory with no `.git`. The walk
|
|
19
|
+
// goes down from `root` at any depth and never follows a symlink, never enters
|
|
20
|
+
// a `.git`, and never enters a path it removes (or, in a dry run, would
|
|
21
|
+
// remove) as ignored, so a repository inside `node_modules` goes with that
|
|
22
|
+
// directory and is not a tree of its own.
|
|
23
|
+
//
|
|
24
|
+
// No status check, no mark, no restore: a tree is evicted whatever `git status`
|
|
25
|
+
// says. A claim tree's status check and its mark are evict.mjs's own.
|
|
26
|
+
|
|
27
|
+
import { spawnSync } from "node:child_process";
|
|
28
|
+
import { lstatSync, readdirSync, realpathSync } from "node:fs";
|
|
29
|
+
import { join } from "node:path";
|
|
30
|
+
import { how } from "./evict-mark.mjs";
|
|
31
|
+
import { gitEnv } from "./git-env.mjs";
|
|
32
|
+
|
|
33
|
+
export const RUNNER = "agent-test";
|
|
34
|
+
const GIT_TIMEOUT_MS = 30_000;
|
|
35
|
+
const MAX_BUFFER = 64 * 1024 * 1024;
|
|
36
|
+
|
|
37
|
+
// The one removal sequence. `$1` is the entry. A symlink entry is unlinked
|
|
38
|
+
// before anything else runs, so no chmod or chflags ever sees it: whether a
|
|
39
|
+
// recursive chmod dereferences a link given as its operand differs between
|
|
40
|
+
// BSD and GNU, and either would change the mode or flags of a target outside
|
|
41
|
+
// the entry being removed. Links met inside a directory are left to `rm`,
|
|
42
|
+
// which unlinks them without following. `chmod -R` fails
|
|
43
|
+
// on a flagged directory, and `chflags -R` cannot read a mode-000 directory it
|
|
44
|
+
// has just unflagged, so when the first pass fails each directory's flag and
|
|
45
|
+
// mode come off together, top-down, before anything inside it is read.
|
|
46
|
+
const REMOVE = [
|
|
47
|
+
'd=$1',
|
|
48
|
+
'if [ -L "$d" ]; then rm -f "$d"; exit $?; fi',
|
|
49
|
+
'chmod -R u+rwx "$d" 2>/dev/null; m=$?',
|
|
50
|
+
'if command -v chflags >/dev/null 2>&1; then',
|
|
51
|
+
' [ $m -eq 0 ] || { chflags nouchg,nouappnd "$d"; chmod u+rwx "$d"; find "$d" -type d -exec chflags nouchg,nouappnd {} ";" -exec chmod u+rwx {} ";"; } 2>/dev/null',
|
|
52
|
+
' chflags -R nouchg,nouappnd "$d" 2>/dev/null',
|
|
53
|
+
'fi',
|
|
54
|
+
'rm -rf "$d"',
|
|
55
|
+
].join("\n");
|
|
56
|
+
|
|
57
|
+
/** Remove `path` through REMOVE; null on success, else the reason. */
|
|
58
|
+
export function removeEntry(path) {
|
|
59
|
+
const r = spawnSync("sh", ["-c", REMOVE, "sh", path], { encoding: "utf8" });
|
|
60
|
+
return r.error || r.status !== 0 ? how(r) : null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const git = (tree, args) =>
|
|
64
|
+
spawnSync("git", ["-C", tree, ...args], { encoding: "utf8", maxBuffer: MAX_BUFFER, env: gitEnv(), timeout: GIT_TIMEOUT_MS });
|
|
65
|
+
|
|
66
|
+
/** Every path the tree ignores, relative to it, but the runner; throws what git cannot answer. */
|
|
67
|
+
export function ignoredPaths(tree) {
|
|
68
|
+
const r = git(tree, ["ls-files", "-z", "--others", "--ignored", "--exclude-standard", "--directory"]);
|
|
69
|
+
if (r.error || r.status !== 0) throw new Error(`git ls-files --ignored failed (${how(r)})`);
|
|
70
|
+
const out = [];
|
|
71
|
+
for (const p of r.stdout.split("\0").filter(Boolean).sort()) {
|
|
72
|
+
const bare = p.replace(/\/$/, "");
|
|
73
|
+
if (bare === RUNNER) continue;
|
|
74
|
+
if (out.some((q) => q.endsWith("/") && p.startsWith(q))) continue;
|
|
75
|
+
out.push(p);
|
|
76
|
+
}
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Remove each of `ignored` (relative to `tree`); null, or every error in one string. */
|
|
81
|
+
export function removeIgnored(tree, ignored) {
|
|
82
|
+
const errors = [];
|
|
83
|
+
for (const p of ignored) {
|
|
84
|
+
const error = removeEntry(join(tree, p.replace(/\/$/, "")));
|
|
85
|
+
if (error !== null) errors.push(`${p}: ${error}`);
|
|
86
|
+
}
|
|
87
|
+
return errors.length ? `could not remove ${errors.join("; ")}` : null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Whether `dir` is a tree: a `.git` of its own, and git answering for this
|
|
91
|
+
// directory rather than for an enclosing repository.
|
|
92
|
+
function isTree(dir) {
|
|
93
|
+
try {
|
|
94
|
+
lstatSync(join(dir, ".git"));
|
|
95
|
+
} catch {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const r = git(dir, ["rev-parse", "--show-toplevel"]);
|
|
99
|
+
if (r.error || r.status !== 0) return false;
|
|
100
|
+
try {
|
|
101
|
+
return realpathSync(r.stdout.replace(/\n$/, "")) === realpathSync(dir);
|
|
102
|
+
} catch {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Evict every tree under `root`, `root` itself included: `{evicted, failed}`.
|
|
109
|
+
* `evicted` is `[{path, ignored}]` for each tree that held ignored paths — in a
|
|
110
|
+
* dry run, what `apply` would remove — and a tree holding none is in neither.
|
|
111
|
+
* `failed` is `[{path, error}]`: a tree whose ignored paths cannot be listed or
|
|
112
|
+
* all removed, or a directory whose listing fails for any reason but its being
|
|
113
|
+
* gone, not a directory, or unreadable. Best-effort: the walk goes on.
|
|
114
|
+
*/
|
|
115
|
+
export function evictTrees(root, { apply }) {
|
|
116
|
+
const evicted = [];
|
|
117
|
+
const failed = [];
|
|
118
|
+
const skip = new Set();
|
|
119
|
+
const stack = [root];
|
|
120
|
+
while (stack.length) {
|
|
121
|
+
const dir = stack.pop();
|
|
122
|
+
if (isTree(dir)) {
|
|
123
|
+
let ignored;
|
|
124
|
+
try {
|
|
125
|
+
ignored = ignoredPaths(dir);
|
|
126
|
+
} catch (e) {
|
|
127
|
+
failed.push({ path: dir, error: e.message });
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
for (const p of ignored) skip.add(join(dir, p.replace(/\/$/, "")));
|
|
131
|
+
if (ignored.length > 0) {
|
|
132
|
+
const error = apply ? removeIgnored(dir, ignored) : null;
|
|
133
|
+
if (error === null) evicted.push({ path: dir, ignored });
|
|
134
|
+
else failed.push({ path: dir, error });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
let entries;
|
|
138
|
+
try {
|
|
139
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
140
|
+
} catch (e) {
|
|
141
|
+
// A directory the walk cannot list or search holds no tree it can reach:
|
|
142
|
+
// skipped, as freshUnder() skips one it cannot read, and never a failure.
|
|
143
|
+
if (!["ENOENT", "ENOTDIR", "EACCES", "EPERM"].includes(e.code)) failed.push({ path: dir, error: `cannot list it (${e.code ?? e.message})` });
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
for (const e of entries) {
|
|
147
|
+
if (e.name === ".git" || !e.isDirectory()) continue;
|
|
148
|
+
const path = join(dir, e.name);
|
|
149
|
+
if (!skip.has(path)) stack.push(path);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const byPath = (a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
153
|
+
return { evicted: evicted.sort(byPath), failed: failed.sort(byPath) };
|
|
154
|
+
}
|
package/scripts/evict.mjs
CHANGED
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
//
|
|
55
55
|
// `--apply` judges each tree to evict again, writes the mark (EVICTED, a file
|
|
56
56
|
// in the tree's git admin dir) unless the tree carries it already, then removes
|
|
57
|
-
// every ignored path but the runner through
|
|
57
|
+
// every ignored path but the runner through evict-tree.mjs's one removal
|
|
58
58
|
// sequence. Tracked files, HEAD, the branch and the registration are not
|
|
59
59
|
// touched. Best-effort per tree: a tree whose mark cannot be written, or a path
|
|
60
60
|
// in it that cannot be removed, lands in `failed`; the sweep goes on. A mark
|
|
@@ -99,10 +99,11 @@ import { basename, dirname, isAbsolute, join, resolve, sep } from "node:path";
|
|
|
99
99
|
import { fileURLToPath } from "node:url";
|
|
100
100
|
import { defineFlags, isDigits, makeDie, writeAll } from "./arg.mjs";
|
|
101
101
|
import { adminDir, EVICTED, evictionMark, how, RESTORED } from "./evict-mark.mjs";
|
|
102
|
+
import { RUNNER, ignoredPaths, removeIgnored } from "./evict-tree.mjs";
|
|
102
103
|
import { gitEnv } from "./git-env.mjs";
|
|
103
104
|
import { isCLI } from "./is-cli.mjs";
|
|
104
105
|
import { liveOnLedger, parseMember, REVIEWER } from "./ledger-grammar.mjs";
|
|
105
|
-
import { ledgerFile, readLedger
|
|
106
|
+
import { ledgerFile, readLedger } from "./reclaim-scratch.mjs";
|
|
106
107
|
import { CLAIM_BRANCH, CLAIM_WORKTREE } from "./stranded.mjs";
|
|
107
108
|
|
|
108
109
|
const NAME = "evict";
|
|
@@ -114,7 +115,6 @@ const DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
114
115
|
export const IDLE_MS = 7 * DAY_MS;
|
|
115
116
|
/** Whether a tree whose HEAD last moved, or which was last restored, at `last` is idle at `now`. */
|
|
116
117
|
export const isIdle = (now, last) => now - last >= IDLE_MS;
|
|
117
|
-
const RUNNER = "agent-test";
|
|
118
118
|
const GIT_TIMEOUT_MS = 30_000;
|
|
119
119
|
const MAX_BUFFER = 64 * 1024 * 1024;
|
|
120
120
|
// claim-ticket.sh's `rederive`, word for word. The refusals that send their
|
|
@@ -277,21 +277,6 @@ function restoredAt(dir) {
|
|
|
277
277
|
return at;
|
|
278
278
|
}
|
|
279
279
|
|
|
280
|
-
// Every path the tree ignores, relative to it, but the runner — a directory
|
|
281
|
-
// git lists whole once, and nothing listed inside one already listed.
|
|
282
|
-
function ignoredPaths(tree) {
|
|
283
|
-
const r = git(tree, ["ls-files", "-z", "--others", "--ignored", "--exclude-standard", "--directory"]);
|
|
284
|
-
if (r.error || r.status !== 0) throw new Error(`git ls-files --ignored failed (${how(r)})`);
|
|
285
|
-
const out = [];
|
|
286
|
-
for (const p of r.stdout.split("\0").filter(Boolean).sort()) {
|
|
287
|
-
const bare = p.replace(/\/$/, "");
|
|
288
|
-
if (bare === RUNNER) continue;
|
|
289
|
-
if (out.some((q) => q.endsWith("/") && p.startsWith(q))) continue;
|
|
290
|
-
out.push(p);
|
|
291
|
-
}
|
|
292
|
-
return out;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
280
|
const within = (path, root) => path === root || path.startsWith(root.endsWith(sep) ? root : root + sep);
|
|
296
281
|
|
|
297
282
|
/**
|
|
@@ -370,12 +355,7 @@ function evictTree(path, verdict, now) {
|
|
|
370
355
|
return `could not write ${join(verdict.dir, EVICTED)} (${e.code ?? e.message}), so nothing was removed`;
|
|
371
356
|
}
|
|
372
357
|
}
|
|
373
|
-
|
|
374
|
-
for (const p of verdict.ignored) {
|
|
375
|
-
const error = removeEntry(join(path, p.replace(/\/$/, "")));
|
|
376
|
-
if (error !== null) errors.push(`${p}: ${error}`);
|
|
377
|
-
}
|
|
378
|
-
return errors.length ? `could not remove ${errors.join("; ")}` : null;
|
|
358
|
+
return removeIgnored(path, verdict.ignored);
|
|
379
359
|
}
|
|
380
360
|
|
|
381
361
|
function sweep({ afterRotate, apply }) {
|
|
@@ -101,12 +101,16 @@
|
|
|
101
101
|
// alone in GitHub's place.
|
|
102
102
|
//
|
|
103
103
|
// `--pr <N>` MODE is the review snapshot's prune: every directory
|
|
104
|
-
// `<root>/pr<N>/run-*` idle past the retention is removed, and
|
|
105
|
-
//
|
|
106
|
-
//
|
|
104
|
+
// `<root>/pr<N>/run-*` idle past the retention is removed whole, and every
|
|
105
|
+
// other one has the trees in it evicted — each directory that holds a .git of
|
|
106
|
+
// its own, at any depth, loses every path git ignores in it, which is
|
|
107
|
+
// evict-tree.mjs's rule: tracked files, each .git, untracked files git does not
|
|
108
|
+
// ignore and everything in the run root outside a tree stay. Nothing else is
|
|
109
|
+
// looked at — no ledger, no gh (a live review's own run root is seconds old
|
|
110
|
+
// and holds no tree yet), no sibling under pr<N>/.
|
|
107
111
|
//
|
|
108
|
-
// EVERY REMOVAL, in either mode, goes through REMOVE
|
|
109
|
-
// restored, darwin's user-immutable and append-only flags cleared where
|
|
112
|
+
// EVERY REMOVAL, in either mode, goes through evict-tree.mjs's REMOVE: owner
|
|
113
|
+
// rwx restored, darwin's user-immutable and append-only flags cleared where
|
|
110
114
|
// `chflags` exists, then `rm -rf` — so a fixture that dropped its own read bit
|
|
111
115
|
// or flagged a file cannot wedge it. Removal is best-effort per entry: a
|
|
112
116
|
// failure lands in `failed[]` and the sweep continues.
|
|
@@ -116,10 +120,12 @@
|
|
|
116
120
|
// "kept":[{"path","reason"}],"failed":[{"path","error"}]}
|
|
117
121
|
// The sibling pass's kept entries also carry "idle": true when nothing under
|
|
118
122
|
// the key was found modified within the retention, false otherwise.
|
|
123
|
+
// `--pr` mode adds `"evicted":[{"path","reason","ignored"}]` after `removed`:
|
|
124
|
+
// each tree that held ignored paths, `ignored` naming them.
|
|
119
125
|
// `ledger` is the file read, or null when none was (`--pr` mode, or
|
|
120
126
|
// `--after-rotate` with no file) — so "never read" stays distinct from "read
|
|
121
127
|
// and found empty". A dry run prints the same receipt with "applied":false,
|
|
122
|
-
// `removed` naming what --apply would remove.
|
|
128
|
+
// `removed` and `evicted` naming what --apply would remove.
|
|
123
129
|
// Exit 0 whenever the receipt is printed, failures included; 2 for a refused
|
|
124
130
|
// invocation — bad arguments, no --root, a root that is not a directory, a
|
|
125
131
|
// root sweep or sibling pass whose --root is not this workspace's own scratch
|
|
@@ -133,6 +139,7 @@ import { lstatSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync,
|
|
|
133
139
|
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
134
140
|
import { fileURLToPath } from "node:url";
|
|
135
141
|
import { defineFlags, isDigits, makeDie, writeAll } from "./arg.mjs";
|
|
142
|
+
import { evictTrees, removeEntry } from "./evict-tree.mjs";
|
|
136
143
|
import { fleetFile, FleetDirUnresolvable, SCRATCH_KEY_SHAPE, scratchRoot } from "./fleet-dir.mjs";
|
|
137
144
|
import { gitEnv } from "./git-env.mjs";
|
|
138
145
|
import { ghAliasedRead, how } from "./gh-aliased-read.mjs";
|
|
@@ -150,26 +157,6 @@ export const RETENTION_MS = 7 * DAY_MS;
|
|
|
150
157
|
const GH_TIMEOUT_MS = 30_000;
|
|
151
158
|
const MAX_BUFFER = 64 * 1024 * 1024;
|
|
152
159
|
|
|
153
|
-
// The one removal sequence. `$1` is the entry. A symlink entry is unlinked
|
|
154
|
-
// before anything else runs, so no chmod or chflags ever sees it: whether a
|
|
155
|
-
// recursive chmod dereferences a link given as its operand differs between
|
|
156
|
-
// BSD and GNU, and either would change the mode or flags of a target outside
|
|
157
|
-
// the entry being removed. Links met inside a directory are left to `rm`,
|
|
158
|
-
// which unlinks them without following. `chmod -R` fails
|
|
159
|
-
// on a flagged directory, and `chflags -R` cannot read a mode-000 directory it
|
|
160
|
-
// has just unflagged, so when the first pass fails each directory's flag and
|
|
161
|
-
// mode come off together, top-down, before anything inside it is read.
|
|
162
|
-
const REMOVE = [
|
|
163
|
-
'd=$1',
|
|
164
|
-
'if [ -L "$d" ]; then rm -f "$d"; exit $?; fi',
|
|
165
|
-
'chmod -R u+rwx "$d" 2>/dev/null; m=$?',
|
|
166
|
-
'if command -v chflags >/dev/null 2>&1; then',
|
|
167
|
-
' [ $m -eq 0 ] || { chflags nouchg,nouappnd "$d"; chmod u+rwx "$d"; find "$d" -type d -exec chflags nouchg,nouappnd {} ";" -exec chmod u+rwx {} ";"; } 2>/dev/null',
|
|
168
|
-
' chflags -R nouchg,nouappnd "$d" 2>/dev/null',
|
|
169
|
-
'fi',
|
|
170
|
-
'rm -rf "$d"',
|
|
171
|
-
].join("\n");
|
|
172
|
-
|
|
173
160
|
const PR_ENTRY = /^pr([1-9][0-9]*)$/;
|
|
174
161
|
const IMPL_ENTRY = /^impl-([1-9][0-9]*)$/;
|
|
175
162
|
const DISPOSITIONS_ENTRY = /^dispositions-fix-pr-([1-9][0-9]*)(?:-[a-z])?\.json$/;
|
|
@@ -215,12 +202,6 @@ export function freshUnder(path, cutoff, unreadable = null) {
|
|
|
215
202
|
return null;
|
|
216
203
|
}
|
|
217
204
|
|
|
218
|
-
/** Remove `path` through REMOVE; null on success, else the reason. */
|
|
219
|
-
export function removeEntry(path) {
|
|
220
|
-
const r = spawnSync("sh", ["-c", REMOVE, "sh", path], { encoding: "utf8" });
|
|
221
|
-
return r.error || r.status !== 0 ? how(r) : null;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
205
|
// The ledger's rows and `## Dispatched` entries, read through `ledger.mjs
|
|
225
206
|
// read` so the ledger keeps one parser. `--file` names it outright, so the read
|
|
226
207
|
// resolves no workspace and runs no git. `die` is the caller's, and `undone`
|
|
@@ -705,6 +686,8 @@ function main() {
|
|
|
705
686
|
|
|
706
687
|
const now = Date.now();
|
|
707
688
|
let ledger = null;
|
|
689
|
+
// `--pr` mode's evictions; null in every other mode.
|
|
690
|
+
let evicted = null;
|
|
708
691
|
let remove;
|
|
709
692
|
let kept = [];
|
|
710
693
|
const failed = [];
|
|
@@ -716,6 +699,13 @@ function main() {
|
|
|
716
699
|
remove = [];
|
|
717
700
|
failed.push({ path: parent, error: e.message });
|
|
718
701
|
}
|
|
702
|
+
// `--pr` mode also evicts the trees in every run root it keeps.
|
|
703
|
+
evicted = [];
|
|
704
|
+
for (const run of kept) {
|
|
705
|
+
const done = evictTrees(run.path, { apply });
|
|
706
|
+
for (const t of done.evicted) evicted.push({ path: t.path, reason: "a tree in a kept review run root", ignored: t.ignored });
|
|
707
|
+
failed.push(...done.failed);
|
|
708
|
+
}
|
|
719
709
|
} else if (siblings) {
|
|
720
710
|
if (apply) {
|
|
721
711
|
const wrote = writeCheckout(root);
|
|
@@ -769,7 +759,8 @@ function main() {
|
|
|
769
759
|
if (error === null) removed.push(entry);
|
|
770
760
|
else failed.push({ path: entry.path, error });
|
|
771
761
|
}
|
|
772
|
-
|
|
762
|
+
const receipt = { applied: apply, ledger, removed, ...(evicted === null ? {} : { evicted }), kept, failed };
|
|
763
|
+
writeAll(1, `${JSON.stringify(receipt)}\n`);
|
|
773
764
|
}
|
|
774
765
|
|
|
775
766
|
if (isCLI(import.meta.url)) main();
|
package/scripts/review-core.mjs
CHANGED
|
@@ -23,12 +23,14 @@
|
|
|
23
23
|
// `null`, which every guard here — `if (snap) {...}`, `unrunCrashed`,
|
|
24
24
|
// `verdictFor` — is written against).
|
|
25
25
|
//
|
|
26
|
-
//
|
|
26
|
+
// The imports. arg.mjs's isDigits(): this file is an ordinary
|
|
27
27
|
// module, so it consumes the repo's digits rule directly rather than
|
|
28
28
|
// declaring its own copy. node:path's `posix`, to normalise a
|
|
29
29
|
// scratch path before `runnerScratchRefusal` reads its last component, and
|
|
30
|
-
// its `isAbsolute`, to refuse a scratch that is not an absolute path
|
|
31
|
-
//
|
|
30
|
+
// its `isAbsolute`, to refuse a scratch that is not an absolute path; node:fs's
|
|
31
|
+
// `lstatSync`, to tell a real run-root directory from a symlink before the
|
|
32
|
+
// review's end evicts the trees in it. evict-tree.mjs's evictTrees(), which does
|
|
33
|
+
// that eviction. fleet-dir.mjs's scratchRoot(), scratchRootRefusal() and FleetDirUnresolvable.
|
|
32
34
|
// scratchRoot() and FleetDirUnresolvable serve the one caller that passes no
|
|
33
35
|
// `scratch` at all — a standalone review with no dispatcher — which takes the
|
|
34
36
|
// fleet's own scratch root for this repository; scratchRootRefusal() vets every
|
|
@@ -41,8 +43,10 @@
|
|
|
41
43
|
// review-eval.mjs's import of it failed. A suite sweep keeps a
|
|
42
44
|
// shipped `.mjs` from importing a relative module under any other extension.
|
|
43
45
|
|
|
46
|
+
import { lstatSync } from "node:fs";
|
|
44
47
|
import { isAbsolute, posix } from "node:path";
|
|
45
48
|
import { isDigits } from "./arg.mjs";
|
|
49
|
+
import { evictTrees } from "./evict-tree.mjs";
|
|
46
50
|
import { FleetDirUnresolvable, scratchRoot, scratchRootRefusal } from "./fleet-dir.mjs";
|
|
47
51
|
|
|
48
52
|
// --- Schemas ----------------------------------------------------------
|
|
@@ -1003,7 +1007,65 @@ function standaloneScratchRoot() {
|
|
|
1003
1007
|
return root;
|
|
1004
1008
|
}
|
|
1005
1009
|
|
|
1010
|
+
/**
|
|
1011
|
+
* The review. When it returns or throws after its snapshot step has reported a
|
|
1012
|
+
* run root of this PR, it evicts every tree in that run root: the snapshot and
|
|
1013
|
+
* each specialist's and refuter's copy lose every path git ignores in them,
|
|
1014
|
+
* and nothing restores them. The run root's other files stay, review.json
|
|
1015
|
+
* among them. A failed eviction, and a reported run root it refuses to touch,
|
|
1016
|
+
* are logged; neither, nor a log that throws, changes what the review returns
|
|
1017
|
+
* or throws. A snapshot step that reported no run root evicts nothing and
|
|
1018
|
+
* logs nothing.
|
|
1019
|
+
*/
|
|
1006
1020
|
export async function runReview(host, args) {
|
|
1021
|
+
const run = { partition: null, runRoot: null };
|
|
1022
|
+
try {
|
|
1023
|
+
return await reviewRun(host, args, run);
|
|
1024
|
+
} finally {
|
|
1025
|
+
evictRunRoot(host.log, run);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
// A reported run root eviction may touch: this PR's partition of the scratch
|
|
1030
|
+
// root — the very string the snapshot step's `mktemp` template starts with, so
|
|
1031
|
+
// a scratch spelled with a trailing slash or a `./` segment still matches — a
|
|
1032
|
+
// slash, `run-` and the eight `mktemp` characters, and nothing after — so no
|
|
1033
|
+
// `..` segment, no trailing slash, no other directory. It must also be a real
|
|
1034
|
+
// directory, not a symlink.
|
|
1035
|
+
function evictableRunRoot(runRoot, partition) {
|
|
1036
|
+
if (!runRoot.startsWith(`${partition}/`)) return false;
|
|
1037
|
+
if (!/^run-[A-Za-z0-9]{8}$/.test(runRoot.slice(partition.length + 1))) return false;
|
|
1038
|
+
try {
|
|
1039
|
+
return lstatSync(partition).isDirectory() && lstatSync(runRoot).isDirectory();
|
|
1040
|
+
} catch {
|
|
1041
|
+
return false;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
// `run.runRoot` is set only once `run.partition` is, so a run root implies a
|
|
1046
|
+
// partition.
|
|
1047
|
+
function evictRunRoot(log, { partition, runRoot }) {
|
|
1048
|
+
const say = (m) => {
|
|
1049
|
+
try {
|
|
1050
|
+
log(m);
|
|
1051
|
+
} catch {
|
|
1052
|
+
// The review's own outcome stands whatever the log sink does.
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
if (runRoot === null) return;
|
|
1056
|
+
try {
|
|
1057
|
+
if (!evictableRunRoot(runRoot, partition)) {
|
|
1058
|
+
say(`no eviction at the end of the review: the reported run root ${runRoot} is not a real directory named run- and eight characters directly in ${partition}`);
|
|
1059
|
+
return;
|
|
1060
|
+
}
|
|
1061
|
+
const done = evictTrees(runRoot, { apply: true });
|
|
1062
|
+
for (const f of done.failed) say(`eviction at the end of the review failed in ${f.path}: ${f.error}`);
|
|
1063
|
+
} catch (e) {
|
|
1064
|
+
say(`eviction at the end of the review failed in ${runRoot}: ${e?.message ?? e}`);
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
async function reviewRun(host, args, run) {
|
|
1007
1069
|
const { agent, phase, log } = host;
|
|
1008
1070
|
const pipeline = host.pipeline ?? defaultPipeline;
|
|
1009
1071
|
const parallel = host.parallel ?? defaultParallel;
|
|
@@ -1069,6 +1131,7 @@ export async function runReview(host, args) {
|
|
|
1069
1131
|
const rootRefusal = scratchRootRefusal(scratch);
|
|
1070
1132
|
if (rootRefusal !== null) throw new Error(`review-pr: args.scratch: ${rootRefusal}`);
|
|
1071
1133
|
}
|
|
1134
|
+
run.partition = runRootParent;
|
|
1072
1135
|
|
|
1073
1136
|
const explicitDimensions = resolveDimensions(A.dimensions, DEFAULT_DIMENSIONS);
|
|
1074
1137
|
|
|
@@ -1255,6 +1318,9 @@ installError only replaces installCmd and installExit, and uncoveredError only
|
|
|
1255
1318
|
replaces uncovered.`,
|
|
1256
1319
|
{ label: `snapshot${forPr}`, phase: "Snapshot", agentType: SNAPSHOT_AGENT_TYPE, schema: SNAPSHOT_SCHEMA },
|
|
1257
1320
|
);
|
|
1321
|
+
// From here the run root the snapshot step made is known, whatever the
|
|
1322
|
+
// report says next: runReview's end evicts the trees in it.
|
|
1323
|
+
if (snap && typeof snap.runRoot === "string") run.runRoot = snap.runRoot;
|
|
1258
1324
|
|
|
1259
1325
|
if (snap) {
|
|
1260
1326
|
if (typeof snap.head === "string") snap.head = snap.head.trim().toLowerCase();
|