@feigi/fleet-ctl 0.0.0 → 3.16.3
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/agents/{fleet-implementer.agent.md → fleet-implementer-slow-high.agent.md} +23 -2
- package/agents/fleet-implementer-slow-medium.agent.md +231 -0
- package/agents/fleet-implementer-smol-high.agent.md +231 -0
- package/agents/{fleet-implementer-alt.agent.md → fleet-implementer-task-high.agent.md} +23 -2
- package/agents/fleet-implementer-task-max.agent.md +231 -0
- package/agents/fleet-review-comments.agent.md +4 -2
- package/agents/fleet-review-correctness.agent.md +2 -1
- package/agents/fleet-review-runner.agent.md +14 -10
- package/agents/fleet-review-silent-failure.agent.md +4 -3
- package/agents/fleet-review-simplify.agent.md +2 -1
- package/agents/fleet-review-snapshot.agent.md +2 -1
- package/agents/fleet-review-test-run.agent.md +29 -0
- package/agents/fleet-review-tests.agent.md +10 -5
- package/agents/fleet-review-types.agent.md +4 -2
- package/agents/fleet-review-verifier.agent.md +4 -3
- package/commands/review-and-fix.md +7 -6
- package/commands/run-merge-bot.md +14 -4
- package/package.json +4 -2
- package/scripts/agent-test.test.mjs +36 -32
- package/scripts/ambient-git-vars-mjs-prose.test.mjs +23 -13
- package/scripts/apply-ruleset.test.mjs +4 -3
- package/scripts/arg-header-probes-prose.test.mjs +1 -0
- package/scripts/arg.test.mjs +18 -18
- package/scripts/board-cli.test.mjs +18 -21
- package/scripts/board-identity.test.mjs +9 -9
- package/scripts/board-liveness-banner.test.mjs +6 -6
- package/scripts/board-prev-shape.test.mjs +12 -13
- package/scripts/board-tryparse-null.test.mjs +7 -7
- package/scripts/board.mjs +4 -7
- package/scripts/board.test.mjs +57 -61
- package/scripts/candidates.test.mjs +228 -17
- package/scripts/ci-completes-premise-prose.test.mjs +1 -1
- package/scripts/ci-state-prose.test.mjs +2 -2
- package/scripts/ci-state.mjs +389 -187
- package/scripts/ci-state.test.mjs +458 -165
- package/scripts/citation-sweep-prose.test.mjs +59 -1
- package/scripts/claim-lifecycle.test.mjs +2 -1
- package/scripts/claim-ticket.test.mjs +35 -34
- package/scripts/closing-keyword-prose.test.mjs +1 -1
- package/scripts/cockpit-tick-nonblocking.test.mjs +8 -8
- package/scripts/compute-board.mjs +13 -3
- package/scripts/compute-board.test.mjs +64 -1
- package/scripts/compute-spend.mjs +46 -8
- package/scripts/compute-spend.test.mjs +64 -10
- package/scripts/cross-repo-citation-prose.test.mjs +18 -19
- package/scripts/delete-merged-branch.sh +218 -0
- package/scripts/delete-merged-branch.test.mjs +576 -0
- package/scripts/derive-testcmd.sh +18 -7
- package/scripts/derive-testcmd.test.mjs +9 -8
- package/scripts/diff-stats.mjs +2 -2
- package/scripts/diff-stats.test.mjs +7 -11
- package/scripts/dispatch-block-golden-prose.test.mjs +111 -6
- package/scripts/dispatch-block-pins-prose.test.mjs +5 -5
- package/scripts/dispositions-check.mjs +507 -0
- package/scripts/dispositions-check.test.mjs +843 -0
- package/scripts/dispositions-gate-prose.test.mjs +114 -0
- package/scripts/drop-merged-label.test.mjs +3 -4
- package/scripts/exec-stub.mjs +93 -0
- package/scripts/exec-stub.test.mjs +69 -0
- package/scripts/filing-bar-seat-prose.test.mjs +1 -1
- package/scripts/finisher-test-verdict-prose.test.mjs +102 -0
- package/scripts/fix-applier-correction-rules-prose.test.mjs +11 -10
- package/scripts/fleet-bootstrap +5 -20
- package/scripts/fleet-bootstrap.test.mjs +75 -0
- package/scripts/fleet-heartbeat.mjs +2 -2
- package/scripts/fleet-provenance +15 -24
- package/scripts/fleet-provenance.test.mjs +86 -12
- package/scripts/fleet-run.test.mjs +5 -5
- package/scripts/fleet-state.mjs +33 -11
- package/scripts/fleet-state.test.mjs +90 -3
- package/scripts/fleet-tick.mjs +346 -70
- package/scripts/fleet-tick.test.mjs +566 -16
- package/scripts/force-with-lease-reason-prose.test.mjs +1 -1
- package/scripts/frontmatter-check.mjs +3 -2
- package/scripts/frontmatter-check.test.mjs +6 -6
- package/scripts/immutable-body-claim-prose.test.mjs +53 -262
- package/scripts/implementer-model-tier.test.mjs +98 -506
- package/scripts/inflight.test.mjs +18 -127
- package/scripts/is-cli.mjs +26 -0
- package/scripts/is-cli.test.mjs +99 -0
- package/scripts/json.test.mjs +3 -3
- package/scripts/ledger-dispatch.test.mjs +368 -7
- package/scripts/ledger-grammar.mjs +85 -1
- package/scripts/ledger-grammar.test.mjs +86 -1
- package/scripts/ledger-read-require-file.test.mjs +7 -7
- package/scripts/ledger-rotate.test.mjs +142 -0
- package/scripts/ledger.mjs +181 -17
- package/scripts/ledger.test.mjs +12 -130
- package/scripts/main-checkout.mjs +312 -0
- package/scripts/main-checkout.test.mjs +332 -0
- package/scripts/member-outcomes-header.test.mjs +3 -3
- package/scripts/member-outcomes.mjs +4 -5
- package/scripts/member-outcomes.test.mjs +92 -18
- package/scripts/member-prompt-prose.test.mjs +17 -13
- package/scripts/member-record.mjs +8 -3
- package/scripts/member-record.test.mjs +5 -5
- package/scripts/member-write-guard.mjs +5 -5
- package/scripts/member-write-guard.test.mjs +6 -3
- package/scripts/no-undo-audit.sh +24 -21
- package/scripts/no-undo-audit.test.mjs +9 -13
- package/scripts/pointer-target-prose.test.mjs +1 -1
- package/scripts/pr-overlap.test.mjs +6 -9
- package/scripts/prompt-renderer.mjs +10 -9
- package/scripts/prose-pin.mjs +10 -11
- package/scripts/prove-merge.test.mjs +3 -3
- package/scripts/quiet-payload-prose.test.mjs +1 -1
- package/scripts/reap.test.mjs +3 -4
- package/scripts/release-label-split-prose.test.mjs +17 -2
- package/scripts/release-ticket.test.mjs +35 -44
- package/scripts/review-core-cwd-isolation.test.mjs +7 -6
- package/scripts/review-core-reads.test.mjs +1 -1
- package/scripts/review-core-scratch-refusal.test.mjs +64 -0
- package/scripts/review-core-shared-test-run.test.mjs +238 -0
- package/scripts/review-core-snapshot-path.test.mjs +59 -12
- package/scripts/review-core-specialist-scratch.test.mjs +12 -10
- package/scripts/review-core-testcmd.test.mjs +76 -45
- package/scripts/review-core-unrun.test.mjs +154 -168
- package/scripts/review-core.mjs +250 -56
- package/scripts/review-eval.mjs +11 -4
- package/scripts/review-host-fixture.mjs +12 -3
- package/scripts/review-in-run-retry.test.mjs +27 -3
- package/scripts/review-runner-spawns.test.mjs +5 -3
- package/scripts/review-runner.test.mjs +14 -3
- package/scripts/review-tier-audit.test.mjs +1 -1
- package/scripts/run-merge-bot-prose.test.mjs +29 -6
- package/scripts/scratch-partition-invariant-prose.test.mjs +1 -1
- package/scripts/section-between-guard-prose.test.mjs +4 -3
- package/scripts/select-dimensions.test.mjs +1 -1
- package/scripts/shell-traps-prose.test.mjs +6 -9
- package/scripts/shortlist.mjs +4 -1
- package/scripts/shortlist.test.mjs +30 -16
- package/scripts/snapshot-repo.test.mjs +177 -4
- package/scripts/stash-prohibition-prose.test.mjs +1 -1
- package/scripts/temp-dir.mjs +47 -0
- package/scripts/tier-check.mjs +95 -28
- package/scripts/tier-check.test.mjs +131 -29
- package/scripts/tier-outcomes-header.test.mjs +11 -54
- package/scripts/tier-outcomes.mjs +34 -12
- package/scripts/tier-outcomes.test.mjs +157 -4
- package/scripts/tier-roles.mjs +118 -29
- package/scripts/tier-roles.test.mjs +226 -61
- package/scripts/tracker-block-copy-prose.test.mjs +3 -3
- package/scripts/verify-sha.test.mjs +4 -5
- package/scripts/watcher-degraded-block.test.mjs +5 -8
- package/scripts/within-run-pair-prose.test.mjs +43 -75
- package/scripts/worktree-audit.test.mjs +2 -1
- package/skills/next-ticket/SKILL.md +2 -2
- package/skills/run-team/SKILL.md +433 -303
- package/skills/run-team/references/ci-and-staleness.md +7 -6
- package/skills/run-team/references/correction-tickets.md +3 -3
- package/skills/run-team/references/isolation.md +1 -1
- package/skills/sizing-a-ticket/SKILL.md +8 -8
- package/scripts/tier-guard-gate-prose.test.mjs +0 -216
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The disposition check: a review fix-applier's rulings on a review's
|
|
3
|
+
// findings, checked as code rather than read back from its report. The
|
|
4
|
+
// fix-applier writes its record to `<scratch>/dispositions-<pr>.json` beside
|
|
5
|
+
// the review result file `<scratch>/review-<pr>.json`; this script reads the
|
|
6
|
+
// two together, judges the record, and writes its verdict onto the PR's
|
|
7
|
+
// ledger row as a token `ledger.mjs dispatch` gates the PR's finisher on:
|
|
8
|
+
//
|
|
9
|
+
// dispositions-ok=fix-pr-<M>[-x]:<head> every rule below held
|
|
10
|
+
// dispositions-mismatch=fix-pr-<M>[-x]:<head> at least one entry broke one
|
|
11
|
+
// dispositions-escalate=fix-pr-<M>[-x]:<head> every rule held, but a critical
|
|
12
|
+
// or important finding was deferred
|
|
13
|
+
// `remedy-outside-diff`: a human rules
|
|
14
|
+
//
|
|
15
|
+
// `<head>` is the review file's `head` — the review the record answers — so a
|
|
16
|
+
// verdict on one review never answers a later one.
|
|
17
|
+
//
|
|
18
|
+
// The record:
|
|
19
|
+
//
|
|
20
|
+
// { "head": "<the review file's head>",
|
|
21
|
+
// "entries": [ { "bucket": "survived" | "unverified" | "refuted",
|
|
22
|
+
// "index": <position in that bucket of the review file>,
|
|
23
|
+
// "scope": "in" | "out",
|
|
24
|
+
// "claimKind": "behavior" | "shape",
|
|
25
|
+
// "disposition": "apply" | "defer",
|
|
26
|
+
// "reason": "<string>", optional
|
|
27
|
+
// "issue": <number>, optional: filed or commented to
|
|
28
|
+
// "verdictPath": "<path>", optional: an in-scope suggestion's refuter verdict
|
|
29
|
+
// "remedyFiles": ["<path>", …] optional: files the remedy names
|
|
30
|
+
// }, … ] }
|
|
31
|
+
//
|
|
32
|
+
// One entry per `survived` finding and per `unverified` finding, plus one per
|
|
33
|
+
// `refuted` finding the fix-applier reverses, its evidence in `reason`. A
|
|
34
|
+
// position is stable because a review round's file never changes.
|
|
35
|
+
//
|
|
36
|
+
// Rules — each broken one is reported as `<bucket>[<index>]: <rule>`:
|
|
37
|
+
// - Coverage: a `survived` or `unverified` finding with no entry was
|
|
38
|
+
// dropped. An entry naming no finding, or a second entry for one finding,
|
|
39
|
+
// answers nothing.
|
|
40
|
+
// - Shape: `scope`, `claimKind` and `disposition` take the values above;
|
|
41
|
+
// the optional fields, when present, take the types above.
|
|
42
|
+
// - Scope presumption: the touched lines are the new-side lines of
|
|
43
|
+
// `git diff <merge-base> <head>`, the merge-base taken between
|
|
44
|
+
// `origin/main` and the review's `<head>` — never the worktree's later
|
|
45
|
+
// commits, so a fix-applier commit that shifts lines changes nothing
|
|
46
|
+
// here. A finding whose `file` and `line` sit on a touched line is in
|
|
47
|
+
// scope whatever the entry declares, and a finding with no `line` (or no
|
|
48
|
+
// `file`) is in scope. Otherwise the declared `scope` stands.
|
|
49
|
+
// - An in-scope `survived` finding deferred passes only with `reason` one
|
|
50
|
+
// of ALLOWED_DEFER. Any other reason, or none, is a mismatch.
|
|
51
|
+
// - `remedy-outside-diff` passes only when `remedyFiles` names at least one
|
|
52
|
+
// file absent from the file list of `git diff <merge-base>...<head>`
|
|
53
|
+
// (renames and deletions listed on both sides, so a file the PR deleted
|
|
54
|
+
// or moved away is in the diff). Empty `remedyFiles`, or every file named
|
|
55
|
+
// inside the diff, is a mismatch. The reason can be gamed by naming any
|
|
56
|
+
// untouched file, so a deferral for it of a `critical` or `important`
|
|
57
|
+
// finding — or of one whose severity is neither that nor `suggestion` —
|
|
58
|
+
// is escalated rather than passed: the verdict is `escalate`, and the
|
|
59
|
+
// output names each such finding. A `suggestion` passes as `ok`. A record
|
|
60
|
+
// that also breaks a rule is a `mismatch`, its escalations printed too.
|
|
61
|
+
//
|
|
62
|
+
// Exit status: 0 ok; 1 mismatch or escalate; 2 nothing judged and nothing
|
|
63
|
+
// written — a bad flag, an unreadable or malformed review file, a record file
|
|
64
|
+
// that exists but cannot be read, a git or ledger failure. A missing or
|
|
65
|
+
// unparseable RECORD is judged, not refused: the fix-applier wrote nothing a
|
|
66
|
+
// reader can use, so every finding it had to cover is dropped.
|
|
67
|
+
//
|
|
68
|
+
// Without a ledger the record is judged all the same and the exit status is
|
|
69
|
+
// the whole verdict: no token is written, no row is read, and `token` in the
|
|
70
|
+
// stdout payload is null. That is the shape of a standalone `/review-and-fix`
|
|
71
|
+
// with no controller, and it is chosen, never inferred — pass `--no-ledger`.
|
|
72
|
+
// A repository's `.fleet/ledger.md` outlives the fleet run that wrote it, so a
|
|
73
|
+
// standalone member in a worktree of such a repository would otherwise find
|
|
74
|
+
// the previous run's ledger, and a ledger that exists is never skipped: one
|
|
75
|
+
// holding no row for the member is a fault (exit 2), as is a ledger path that
|
|
76
|
+
// cannot be looked up (a parent that is a file, no permission, a symlink loop).
|
|
77
|
+
// Only a path that does not exist is "no ledger". `--no-ledger` names that
|
|
78
|
+
// state outright: no ledger is looked for, whatever the repository holds, and
|
|
79
|
+
// it contradicts `--ledger`.
|
|
80
|
+
|
|
81
|
+
import { readFileSync, existsSync, lstatSync } from "node:fs";
|
|
82
|
+
import { execFileSync } from "node:child_process";
|
|
83
|
+
import { join, dirname, isAbsolute, relative, posix } from "node:path";
|
|
84
|
+
import { fileURLToPath } from "node:url";
|
|
85
|
+
import { makeDie, defineFlags } from "./arg.mjs";
|
|
86
|
+
import { isCLI } from "./is-cli.mjs";
|
|
87
|
+
import { gitEnv, workspaceDirFromGitCommonDir } from "./git-env.mjs";
|
|
88
|
+
import { parseMember, memberTokens } from "./ledger-grammar.mjs";
|
|
89
|
+
import { dispositionsToken, rowNums, sameHead } from "./fleet-tick.mjs";
|
|
90
|
+
|
|
91
|
+
const NAME = "dispositions-check";
|
|
92
|
+
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url));
|
|
93
|
+
const LEDGER_SCRIPT = join(SCRIPT_DIR, "ledger.mjs");
|
|
94
|
+
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// pure core — no filesystem or process access until main()
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
const REMEDY_OUTSIDE_DIFF = "remedy-outside-diff";
|
|
100
|
+
export const ALLOWED_DEFER = Object.freeze(["false-rationale", "mutual-exclusion", "remedy-worse", REMEDY_OUTSIDE_DIFF]);
|
|
101
|
+
const COVERED = ["survived", "unverified"];
|
|
102
|
+
const BUCKETS = [...COVERED, "refuted"];
|
|
103
|
+
const ENUMS = { scope: ["in", "out"], claimKind: ["behavior", "shape"], disposition: ["apply", "defer"] };
|
|
104
|
+
|
|
105
|
+
// Why `review` is not a review file this check can judge a record against,
|
|
106
|
+
// or null when it is: a 7-40 hex `head`, every bucket a list of finding
|
|
107
|
+
// objects, and `counts` reconciling with the lists it counts. checkDispositions
|
|
108
|
+
// reads `review` on that precondition, and main() refuses a file that breaks
|
|
109
|
+
// it rather than judging a record against it.
|
|
110
|
+
export function reviewProblem(review) {
|
|
111
|
+
if (review === null || typeof review !== "object" || Array.isArray(review)) return "is not a review result object";
|
|
112
|
+
if (!/^[0-9a-f]{7,40}$/i.test(String(review.head ?? ""))) return "carries no head commit";
|
|
113
|
+
for (const bucket of BUCKETS) {
|
|
114
|
+
if (!Array.isArray(review[bucket])) return `has no ${bucket} list`;
|
|
115
|
+
const bad = review[bucket].findIndex((f) => f === null || typeof f !== "object" || Array.isArray(f));
|
|
116
|
+
if (bad !== -1) return `has a ${bucket}[${bad}] that is not a finding object`;
|
|
117
|
+
if (bucket !== "refuted" && review.counts?.[bucket] !== review[bucket].length) {
|
|
118
|
+
return `says counts.${bucket} is ${review.counts?.[bucket]}, but ${bucket} holds ${review[bucket].length} — the review file does not reconcile with itself`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The new-side line numbers each file's hunks touch, from `git diff
|
|
126
|
+
* --unified=0` output with `a/`/`b/` prefixes. A pure deletion (`+c,0`)
|
|
127
|
+
* touches no new-side line; a deleted file has no new side at all.
|
|
128
|
+
*
|
|
129
|
+
* A `+++ ` line is a file header only BETWEEN hunks: inside one, each hunk's
|
|
130
|
+
* own line counts are consumed first, so an added content line that begins
|
|
131
|
+
* `++ ` (printed `+++ …`) is content, never a header that would credit every
|
|
132
|
+
* later hunk to a path that does not exist.
|
|
133
|
+
* @returns {Map<string, Set<number>>}
|
|
134
|
+
*/
|
|
135
|
+
export function touchedLines(diffText) {
|
|
136
|
+
const touched = new Map();
|
|
137
|
+
let file = null;
|
|
138
|
+
let oldLeft = 0;
|
|
139
|
+
let newLeft = 0;
|
|
140
|
+
for (const line of diffText.split("\n")) {
|
|
141
|
+
if (oldLeft > 0 || newLeft > 0) {
|
|
142
|
+
if (line.startsWith("-")) oldLeft--;
|
|
143
|
+
else if (line.startsWith("+")) newLeft--;
|
|
144
|
+
else if (line.startsWith(" ")) { oldLeft--; newLeft--; }
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (line.startsWith("+++ ")) {
|
|
148
|
+
const path = headerPath(line.slice(4));
|
|
149
|
+
file = path === "/dev/null" ? null : path.replace(/^b\//, "");
|
|
150
|
+
if (file !== null && !touched.has(file)) touched.set(file, new Set());
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const h = /^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
154
|
+
if (h) {
|
|
155
|
+
oldLeft = h[1] === undefined ? 1 : Number(h[1]);
|
|
156
|
+
const start = Number(h[2]);
|
|
157
|
+
newLeft = h[3] === undefined ? 1 : Number(h[3]);
|
|
158
|
+
if (file !== null) for (let n = start; n < start + newLeft; n++) touched.get(file).add(n);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return touched;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const C_ESCAPES = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, '"': 34, "\\": 92 };
|
|
165
|
+
|
|
166
|
+
// A `+++ ` header's path as git wrote it: the trailing tab git appends to a
|
|
167
|
+
// name holding a space dropped, and a C-quoted name (`"b/q\"x.js"`, used for
|
|
168
|
+
// a quote, a backslash, a control character or — with core.quotePath — a
|
|
169
|
+
// non-ASCII byte) unquoted, its octal escapes read as UTF-8 bytes.
|
|
170
|
+
function headerPath(raw) {
|
|
171
|
+
const p = raw.endsWith("\t") ? raw.slice(0, -1) : raw;
|
|
172
|
+
if (!(p.length >= 2 && p.startsWith('"') && p.endsWith('"'))) return p;
|
|
173
|
+
const bytes = [];
|
|
174
|
+
const body = p.slice(1, -1);
|
|
175
|
+
for (let i = 0; i < body.length; i++) {
|
|
176
|
+
const c = body[i];
|
|
177
|
+
if (c !== "\\") { bytes.push(...Buffer.from(c, "utf8")); continue; }
|
|
178
|
+
const next = body[i + 1];
|
|
179
|
+
if (/[0-7]/.test(next ?? "")) {
|
|
180
|
+
const oct = /^[0-7]{1,3}/.exec(body.slice(i + 1))[0];
|
|
181
|
+
bytes.push(parseInt(oct, 8));
|
|
182
|
+
i += oct.length;
|
|
183
|
+
} else if (next in C_ESCAPES) {
|
|
184
|
+
bytes.push(C_ESCAPES[next]);
|
|
185
|
+
i += 1;
|
|
186
|
+
} else {
|
|
187
|
+
bytes.push(92);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return Buffer.from(bytes).toString("utf8");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// A finding's `file` as the diff spells it: relative to the repository root,
|
|
194
|
+
// no leading `./`. A specialist that reported an absolute path into the
|
|
195
|
+
// review's snapshot, or into the repository itself, is read relative to it.
|
|
196
|
+
// An absolute path under none of `roots` — a specialist's own copy of the
|
|
197
|
+
// snapshot, a `/tmp` alias of a `/private/tmp` root — is read as the longest
|
|
198
|
+
// of `known` (the diff's file names) it ends in, at a path-component
|
|
199
|
+
// boundary: no suffix of the path naming a touched file is the only case in
|
|
200
|
+
// which it cannot sit on a touched line under any root.
|
|
201
|
+
export function repoPath(file, roots = [], known = []) {
|
|
202
|
+
let p = String(file);
|
|
203
|
+
if (isAbsolute(p)) {
|
|
204
|
+
for (const root of roots) {
|
|
205
|
+
if (!root) continue;
|
|
206
|
+
const rel = relative(root, p);
|
|
207
|
+
if (rel && !rel.startsWith("..") && !isAbsolute(rel)) return rel;
|
|
208
|
+
}
|
|
209
|
+
let best = null;
|
|
210
|
+
for (const k of known) {
|
|
211
|
+
if (p.endsWith(`/${k}`) && (best === null || k.length > best.length)) best = k;
|
|
212
|
+
}
|
|
213
|
+
return best ?? p;
|
|
214
|
+
}
|
|
215
|
+
while (p.startsWith("./")) p = p.slice(2);
|
|
216
|
+
return p;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Why a finding is in scope regardless of what its entry declares, or null
|
|
220
|
+
// when the declared scope stands.
|
|
221
|
+
function presumedInScope(finding, touched, roots) {
|
|
222
|
+
if (!Number.isInteger(finding.line)) return "it has no line";
|
|
223
|
+
if (typeof finding.file !== "string" || finding.file === "") return "it has no file";
|
|
224
|
+
const file = repoPath(finding.file, roots, touched.keys());
|
|
225
|
+
if (touched.get(file)?.has(finding.line)) return `${file}:${finding.line} is a line the PR's diff touched`;
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function shapeErrors(e) {
|
|
230
|
+
const errors = [];
|
|
231
|
+
for (const [field, values] of Object.entries(ENUMS)) {
|
|
232
|
+
if (!values.includes(e[field])) errors.push(`${field} ${JSON.stringify(e[field] ?? null)} is not one of ${values.join("|")}`);
|
|
233
|
+
}
|
|
234
|
+
if (e.reason !== undefined && typeof e.reason !== "string") errors.push("reason is not a string");
|
|
235
|
+
if (e.issue !== undefined && !(Number.isInteger(e.issue) && e.issue > 0)) errors.push("issue is not an issue number");
|
|
236
|
+
if (e.verdictPath !== undefined && typeof e.verdictPath !== "string") errors.push("verdictPath is not a string");
|
|
237
|
+
if (e.remedyFiles !== undefined && !(Array.isArray(e.remedyFiles) && e.remedyFiles.every((f) => typeof f === "string"))) {
|
|
238
|
+
errors.push("remedyFiles is not an array of paths");
|
|
239
|
+
}
|
|
240
|
+
return errors;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Every rule the record breaks against the review, as `violations` of
|
|
245
|
+
* `{bucket, index, rule}` — `bucket`/`index` null for a rule about the record
|
|
246
|
+
* as a whole — and every deferral that needs a human, as `escalations` of
|
|
247
|
+
* `{bucket, index, severity, files}`. Empty `violations` means the record
|
|
248
|
+
* holds every rule. `record` is the parsed record, or null when there is none
|
|
249
|
+
* to read (`recordProblem` then says why). `touched` is touchedLines() for the
|
|
250
|
+
* review's head; `diffFiles` every file name `git diff <merge-base>...<head>`
|
|
251
|
+
* lists, required only when an entry defers `remedy-outside-diff`; `roots` the
|
|
252
|
+
* directories an absolute finding or remedy path is read relative to.
|
|
253
|
+
* `review` must pass reviewProblem() — a bucket missing or holding a
|
|
254
|
+
* non-object is a TypeError here, not a violation.
|
|
255
|
+
*/
|
|
256
|
+
export function checkDispositions({ review, record, recordProblem = null, touched, diffFiles = null, roots = [] }) {
|
|
257
|
+
const violations = [];
|
|
258
|
+
const escalations = [];
|
|
259
|
+
const at = (bucket, index, rule) => violations.push({ bucket, index, rule });
|
|
260
|
+
|
|
261
|
+
let entries = [];
|
|
262
|
+
if (record === null) {
|
|
263
|
+
at(null, null, recordProblem ?? "no disposition record");
|
|
264
|
+
} else if (typeof record !== "object" || Array.isArray(record) || !Array.isArray(record.entries)) {
|
|
265
|
+
at(null, null, "the record is not {head, entries: [...]}");
|
|
266
|
+
} else {
|
|
267
|
+
entries = record.entries;
|
|
268
|
+
const head = String(record.head ?? "").toLowerCase();
|
|
269
|
+
const reviewHead = String(review.head).toLowerCase();
|
|
270
|
+
if (!/^[0-9a-f]{7,40}$/.test(head) || !sameHead(head, reviewHead)) {
|
|
271
|
+
at(null, null, `the record answers head ${JSON.stringify(record.head ?? null)}, not the review's ${review.head}`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const seen = new Map();
|
|
276
|
+
entries.forEach((e, n) => {
|
|
277
|
+
if (e === null || typeof e !== "object" || Array.isArray(e)) {
|
|
278
|
+
at(null, null, `entries[${n}] is not an object`);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const { bucket, index } = e;
|
|
282
|
+
if (!BUCKETS.includes(bucket)) {
|
|
283
|
+
at(null, null, `entries[${n}] names bucket ${JSON.stringify(bucket ?? null)}, not one of ${BUCKETS.join("|")}`);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (!Number.isInteger(index) || index < 0 || index >= review[bucket].length) {
|
|
287
|
+
at(bucket, index ?? null, `entries[${n}] names no finding — ${bucket} holds ${review[bucket].length}`);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
const key = `${bucket}[${index}]`;
|
|
291
|
+
if (seen.has(key)) {
|
|
292
|
+
at(bucket, index, `a second entry (entries[${n}]) for a finding entries[${seen.get(key)}] already answers`);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
seen.set(key, n);
|
|
296
|
+
const errors = shapeErrors(e);
|
|
297
|
+
if (errors.length > 0) {
|
|
298
|
+
at(bucket, index, `malformed entry — ${errors.join("; ")}`);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (bucket === "refuted") {
|
|
302
|
+
if (!e.reason?.trim()) at(bucket, index, "a reversed refutation names its evidence in reason, and this one names none");
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (bucket !== "survived" || e.disposition !== "defer") return;
|
|
306
|
+
const presumed = presumedInScope(review[bucket][index], touched, roots);
|
|
307
|
+
if (presumed === null && e.scope === "out") return;
|
|
308
|
+
if (!ALLOWED_DEFER.includes(e.reason)) {
|
|
309
|
+
const why = presumed === null ? "its entry declares scope in" : `${presumed}, so it is in scope whatever its entry declares`;
|
|
310
|
+
const reason = e.reason === undefined || e.reason === "" ? "no reason" : `reason ${JSON.stringify(e.reason)}`;
|
|
311
|
+
at(bucket, index, `an in-scope survived finding deferred with ${reason} — ${why}; it defers only for ${ALLOWED_DEFER.join(", ")}`);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (e.reason !== REMEDY_OUTSIDE_DIFF) return;
|
|
315
|
+
if (diffFiles === null) throw new TypeError("checkDispositions: an entry defers remedy-outside-diff and no diffFiles was given");
|
|
316
|
+
const inDiff = new Set(diffFiles);
|
|
317
|
+
const named = (e.remedyFiles ?? []).map((f) => f.trim()).filter((f) => f !== "");
|
|
318
|
+
const outside = named.map((f) => posix.normalize(repoPath(f, roots, inDiff))).filter((f) => !inDiff.has(f));
|
|
319
|
+
if (outside.length === 0) {
|
|
320
|
+
const what = named.length === 0 ? "remedyFiles names no file" : "every file remedyFiles names is in the PR's diff";
|
|
321
|
+
at(bucket, index, `reason remedy-outside-diff needs a remedy file absent from the PR's diff — ${what}`);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
const severity = review[bucket][index].severity;
|
|
325
|
+
if (severity !== "suggestion") escalations.push({ bucket, index, severity: severity ?? null, files: outside });
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
for (const bucket of COVERED) {
|
|
329
|
+
review[bucket].forEach((_, index) => {
|
|
330
|
+
if (!seen.has(`${bucket}[${index}]`)) at(bucket, index, "no entry — a finding with no entry is a dropped finding");
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
return { violations, escalations };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function formatViolation({ bucket, index, rule }) {
|
|
337
|
+
return bucket === null ? `record: ${rule}` : `${bucket}[${index}]: ${rule}`;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function formatEscalation({ bucket, index, severity, files }) {
|
|
341
|
+
return `${bucket}[${index}]: a ${severity ?? "severity-less"} finding deferred remedy-outside-diff, its remedy in ${files.join(", ")} — a human rules`;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// The row text with `token` in place of every verdict the same member wrote
|
|
345
|
+
// for the same review head — a re-run replaces its own earlier verdict rather
|
|
346
|
+
// than leaving two to disagree. Unchanged when the token is already the only
|
|
347
|
+
// one. Separators a removal leaves doubled are folded.
|
|
348
|
+
export function withVerdict(rowText, token) {
|
|
349
|
+
const mine = dispositionsToken(token);
|
|
350
|
+
if (mine === null) throw new Error(`withVerdict: '${token}' is not a dispositions-ok=/dispositions-mismatch=/dispositions-escalate= token`);
|
|
351
|
+
const words = String(rowText).trim().split(/\s+/).filter(Boolean);
|
|
352
|
+
const kept = words.filter((w) => {
|
|
353
|
+
const d = dispositionsToken(w);
|
|
354
|
+
return d === null || d.member.name !== mine.member.name || !sameHead(d.head, mine.head);
|
|
355
|
+
});
|
|
356
|
+
if (kept.length === words.length - 1 && words.includes(token)) return words.join(" ");
|
|
357
|
+
const folded = kept.join(" ").replace(/·(?:\s+·)+/g, "·").replace(/^·\s*|\s*·$/g, "").trim();
|
|
358
|
+
return folded === "" ? token : `${folded} · ${token}`;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ---------------------------------------------------------------------------
|
|
362
|
+
// CLI
|
|
363
|
+
// ---------------------------------------------------------------------------
|
|
364
|
+
|
|
365
|
+
const die = makeDie(NAME);
|
|
366
|
+
const { arg, has, sweep, stray } = defineFlags(die, {
|
|
367
|
+
flags: { member: "value", scratch: "value", repo: "value", ledger: "value", "no-ledger": "bool" },
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
// The one git primitive. An ambient GIT_DIR or GIT_WORK_TREE — a hook, a
|
|
371
|
+
// `rebase --exec` — would answer for another repository and judge the record
|
|
372
|
+
// against that repository's diff, so both are scrubbed.
|
|
373
|
+
function git(repo, args, what) {
|
|
374
|
+
try {
|
|
375
|
+
return execFileSync("git", ["-C", repo, ...args], {
|
|
376
|
+
encoding: "utf8", env: gitEnv({ LC_ALL: "C" }), maxBuffer: 256 * 1024 * 1024, stdio: ["ignore", "pipe", "pipe"],
|
|
377
|
+
});
|
|
378
|
+
} catch (e) {
|
|
379
|
+
die(`could not ${what}: ${e.stderr?.toString().trim() || e.message}`);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function runLedger(ledgerFile, args, what) {
|
|
384
|
+
try {
|
|
385
|
+
return execFileSync(process.execPath, [LEDGER_SCRIPT, "--file", ledgerFile, ...args], { encoding: "utf8" });
|
|
386
|
+
} catch (e) {
|
|
387
|
+
die(`could not ${what}: ${e.stderr?.trim() || e.message}`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// The ledger file this run writes its verdict to, or null when there is none:
|
|
392
|
+
// `--ledger`, else the run's `.fleet/ledger.md` under the git common dir of
|
|
393
|
+
// `repo` — the workspace every worktree of one repository shares, which is
|
|
394
|
+
// where `ledger.mjs dispatch` wrote the member's row. The path is handed to
|
|
395
|
+
// `ledger.mjs` as `--file`, so the existence probe and the calls that follow
|
|
396
|
+
// answer for one file. Only ENOENT means "there is none": any other failure
|
|
397
|
+
// to look the path up is a fault, not a standalone run. `lstat`, not `stat`, so
|
|
398
|
+
// a dangling symlink is a ledger that cannot be read rather than an absent one.
|
|
399
|
+
function ledgerInUse(explicit, repo) {
|
|
400
|
+
const file = explicit ?? join(
|
|
401
|
+
workspaceDirFromGitCommonDir(git(repo, ["rev-parse", "--git-common-dir"], "find the git common dir"), repo)
|
|
402
|
+
?? die("could not find the git common dir: git printed none"),
|
|
403
|
+
".fleet", "ledger.md",
|
|
404
|
+
);
|
|
405
|
+
try {
|
|
406
|
+
lstatSync(file);
|
|
407
|
+
return file;
|
|
408
|
+
} catch (e) {
|
|
409
|
+
if (e.code === "ENOENT") return null;
|
|
410
|
+
die(`could not look for the ledger ${file}: ${e.message}`);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function readJson(path) {
|
|
415
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function main() {
|
|
419
|
+
const usage = "usage: dispositions-check.mjs --member fix-pr-<M>[-x] --scratch <dir> [--repo <path>] [--ledger <path> | --no-ledger]";
|
|
420
|
+
const name = arg("member");
|
|
421
|
+
const scratch = arg("scratch");
|
|
422
|
+
if (!name || !scratch) die(usage);
|
|
423
|
+
sweep();
|
|
424
|
+
stray();
|
|
425
|
+
const member = parseMember(name);
|
|
426
|
+
if (member === null || member.family !== "fix-pr") die(`'${name}' is not a fix-applier — expected fix-pr-<M>, a -b, -c … suffix allowed`);
|
|
427
|
+
const pr = member.number;
|
|
428
|
+
const repo = arg("repo") ?? process.cwd();
|
|
429
|
+
const standalone = has("no-ledger");
|
|
430
|
+
if (standalone && arg("ledger") !== null) die("--no-ledger and --ledger contradict: name one");
|
|
431
|
+
|
|
432
|
+
const reviewPath = join(scratch, `review-${pr}.json`);
|
|
433
|
+
let review;
|
|
434
|
+
try {
|
|
435
|
+
review = readJson(reviewPath);
|
|
436
|
+
} catch (e) {
|
|
437
|
+
die(`could not read the review file ${reviewPath}: ${e.message}`);
|
|
438
|
+
}
|
|
439
|
+
const problem = reviewProblem(review);
|
|
440
|
+
if (problem !== null) die(`${reviewPath} ${problem}`);
|
|
441
|
+
const head = String(review.head).toLowerCase();
|
|
442
|
+
|
|
443
|
+
const recordPath = join(scratch, `dispositions-${pr}.json`);
|
|
444
|
+
let record = null, recordProblem = null;
|
|
445
|
+
if (!existsSync(recordPath)) {
|
|
446
|
+
recordProblem = `no disposition record at ${recordPath}`;
|
|
447
|
+
} else {
|
|
448
|
+
// A record that cannot be READ — a directory, no permission — is a fault
|
|
449
|
+
// in the environment, not a ruling the fix-applier wrote: nothing is
|
|
450
|
+
// judged and nothing written. Only one that reads and will not parse is
|
|
451
|
+
// the fix-applier's own, and judged.
|
|
452
|
+
let text;
|
|
453
|
+
try {
|
|
454
|
+
text = readFileSync(recordPath, "utf8");
|
|
455
|
+
} catch (e) {
|
|
456
|
+
die(`could not read the disposition record ${recordPath}: ${e.message}`);
|
|
457
|
+
}
|
|
458
|
+
try {
|
|
459
|
+
record = JSON.parse(text);
|
|
460
|
+
} catch (e) {
|
|
461
|
+
recordProblem = `${recordPath} is not JSON — ${e.message}`;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const base = git(repo, ["merge-base", "origin/main", head], `find the merge-base of origin/main and ${head}`).trim();
|
|
466
|
+
const diff = git(repo, ["-c", "core.quotePath=false", "diff", "--no-color", "--no-ext-diff", "--unified=0", "-M",
|
|
467
|
+
"--src-prefix=a/", "--dst-prefix=b/", base, head], `diff ${base}..${head}`);
|
|
468
|
+
const top = git(repo, ["rev-parse", "--show-toplevel"], "find the repository root").trim();
|
|
469
|
+
// Every file the diff lists, a rename or a deletion by both its names — a
|
|
470
|
+
// file the PR moved away or removed is a file the PR's diff names.
|
|
471
|
+
const diffFiles = git(repo, ["diff", "--no-color", "--no-ext-diff", "--no-renames", "--name-only", "-z", base, head],
|
|
472
|
+
`list the files of ${base}..${head}`).split("\0").filter(Boolean);
|
|
473
|
+
const ledgerFile = standalone ? null : ledgerInUse(arg("ledger"), repo);
|
|
474
|
+
|
|
475
|
+
const { violations, escalations } = checkDispositions({
|
|
476
|
+
review, record, recordProblem, touched: touchedLines(diff), diffFiles, roots: [review.snapshot, top],
|
|
477
|
+
});
|
|
478
|
+
const verdict = violations.length > 0 ? "mismatch" : escalations.length > 0 ? "escalate" : "ok";
|
|
479
|
+
const token = `dispositions-${verdict}=${member.name}:${head}`;
|
|
480
|
+
|
|
481
|
+
// The verdict lands on the row carrying the member on its own PR's row —
|
|
482
|
+
// where `ledger.mjs dispatch` wrote it, and where the gate reads PR M's
|
|
483
|
+
// tokens from. No ledger, no row: the exit status below is the verdict.
|
|
484
|
+
if (ledgerFile !== null) {
|
|
485
|
+
const data = JSON.parse(runLedger(ledgerFile, ["read"], "read the ledger"));
|
|
486
|
+
const row = data.rows.find((r) => rowNums(r).pr === pr && memberTokens(r).some((t) => t.name === member.name));
|
|
487
|
+
if (row === undefined) die(`${member.name} is on no row of PR #${pr} — \`ledger.mjs dispatch\` records a fix-applier before its record can be checked; with no controller, pass --no-ledger`);
|
|
488
|
+
const key = row.split(/\s/)[0];
|
|
489
|
+
if (!/^#[0-9]+$/.test(key)) die(`the row carrying ${member.name} has no #<n> key for \`ledger.mjs row\` to rewrite it by: ${row}`);
|
|
490
|
+
const text = row.slice(key.length).trim();
|
|
491
|
+
const updated = withVerdict(text, token);
|
|
492
|
+
if (updated === text) console.error(` ${member.name}: row ${key} already carries ${token} — not written again`);
|
|
493
|
+
else runLedger(ledgerFile, ["row", key, updated], `write ${token} onto row ${key}`);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
for (const v of violations) console.error(`${member.name}: ${formatViolation(v)}`);
|
|
497
|
+
for (const x of escalations) console.error(`${member.name}: ${formatEscalation(x)}`);
|
|
498
|
+
console.log(JSON.stringify({ member: member.name, pr, head, verdict, token: ledgerFile === null ? null : token, violations, escalations }));
|
|
499
|
+
// exitCode, not exit(): stdout to a pipe is written asynchronously, and an
|
|
500
|
+
// exit() here could cut the payload off.
|
|
501
|
+
process.exitCode = verdict === "ok" ? 0 : 1;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// Only run main() as a CLI, never when imported (see is-cli.mjs): a string
|
|
505
|
+
// comparison skipped main() through a symlinked path and exited 0 — the code
|
|
506
|
+
// that means ok — having judged and written nothing.
|
|
507
|
+
if (isCLI(import.meta.url)) main();
|