architecture-harness 0.20.2 → 0.20.4
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/src/cli/bash-command-guard.mjs +70 -11
- package/src/cli/bash-command-guard.test.mjs +219 -0
- package/src/cli/bash-edit-guard.mjs +19 -16
- package/src/cli/bash-edit-guard.test.mjs +19 -0
- package/src/cli/ci-state.mjs +54 -0
- package/src/cli/ci-state.test.mjs +76 -0
- package/src/cli/cost-stop.mjs +98 -0
- package/src/cli/cost-stop.test.mjs +149 -0
- package/src/cli/dispatch-guard.mjs +3 -3
- package/src/cli/dispatch-guard.test.mjs +14 -6
- package/src/cli/dispatch-reminder.mjs +7 -3
- package/src/cli/dispatch-reminder.test.mjs +16 -1
- package/src/cli/git-read.mjs +41 -3
- package/src/cli/git-read.test.mjs +31 -1
- package/src/cli/ledger.mjs +52 -3
- package/src/cli/ledger.test.mjs +60 -0
- package/src/cli/merge-guard.mjs +1 -14
- package/src/cli/plugin-agents.test.mjs +11 -0
- package/src/cli/powershell-command.mjs +8 -13
- package/src/cli/powershell-command.test.mjs +12 -1
- package/src/cli/qc.mjs +1 -1
- package/src/cli/report-stop.test.mjs +2 -1
- package/src/cli/review-gate.mjs +224 -0
- package/src/cli/review-gate.test.mjs +334 -0
- package/src/cli/shell-command.mjs +77 -13
- package/src/cli/shell-command.test.mjs +44 -3
- package/src/cli/skill-guard.mjs +56 -0
- package/src/cli/skill-guard.test.mjs +87 -0
- package/src/cli/stop-gate.test.mjs +80 -0
- package/src/cli/task-gate-review.test.mjs +225 -0
- package/src/cli/task-gate.mjs +54 -16
- package/src/cli/task-gate.test.mjs +65 -6
- package/src/cli/test-brief.mjs +12 -0
- package/src/cli/transcript-usage.mjs +28 -0
- package/src/cli/transcript-usage.test.mjs +9 -0
- package/src/cli/workflow-hooks.test.mjs +38 -0
- package/src/cli/workflow-settings.mjs +14 -3
- package/src/cli/workflow-settings.test.mjs +24 -0
- package/src/config.mjs +13 -0
- package/templates/docs/enforcement.md +12 -1
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { reviewDefaults } from "../config.mjs";
|
|
4
|
+
import {
|
|
5
|
+
baseCheck,
|
|
6
|
+
briefCheck,
|
|
7
|
+
briefPathOf,
|
|
8
|
+
countFindings,
|
|
9
|
+
countFiles,
|
|
10
|
+
reviewedShaOf,
|
|
11
|
+
reviewerCheck,
|
|
12
|
+
} from "./review-gate.mjs";
|
|
13
|
+
|
|
14
|
+
const SHA = "a".repeat(40);
|
|
15
|
+
const TIP = "b".repeat(40);
|
|
16
|
+
const review = { ...reviewDefaults, implementerTypes: ["sdd-implementer"] };
|
|
17
|
+
const branchVerdict = { type: "verdict", kind: "branch", verdict: "APPROVED", sha: "c".repeat(40), branch: "feat/1-x" };
|
|
18
|
+
|
|
19
|
+
test("the Reviewed line names the last reviewed sha, and only a hex sha counts", () => {
|
|
20
|
+
assert.equal(reviewedShaOf("Reviewed: abcdef1\nmore"), "abcdef1");
|
|
21
|
+
assert.equal(reviewedShaOf("x\n reviewed: " + SHA + " \n"), SHA);
|
|
22
|
+
assert.equal(reviewedShaOf("Reviewed: soon"), null);
|
|
23
|
+
assert.equal(reviewedShaOf("Reviewed: abc12"), null);
|
|
24
|
+
assert.equal(reviewedShaOf("the Reviewed: abcdef1 is inline"), null);
|
|
25
|
+
assert.equal(reviewedShaOf(""), null);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// One review per PR
|
|
29
|
+
|
|
30
|
+
const branchPrompt = (more = "") => `Worktree: /w\n${more}`;
|
|
31
|
+
const check = (over = {}) =>
|
|
32
|
+
reviewerCheck({
|
|
33
|
+
type: "sdd-branch-reviewer",
|
|
34
|
+
prompt: branchPrompt(),
|
|
35
|
+
review: { ...review, reviewerTypes: { task: ["sdd-reviewer"], branch: ["sdd-branch-reviewer"] } },
|
|
36
|
+
base: "origin/main",
|
|
37
|
+
records: [],
|
|
38
|
+
root: "/w",
|
|
39
|
+
branch: "feat/1-x",
|
|
40
|
+
head: SHA,
|
|
41
|
+
ciState: () => ({ reason: null, unknown: null }),
|
|
42
|
+
reads: { remoteTip: () => TIP, lookup: () => ({ sha: TIP }), isAncestor: () => true },
|
|
43
|
+
...over,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("a first branch review passes, whether or not the ledger holds other verdicts", () => {
|
|
47
|
+
assert.deepEqual(check(), { refusal: null, note: null });
|
|
48
|
+
const taskVerdict = { ...branchVerdict, kind: "task" };
|
|
49
|
+
assert.equal(check({ records: [taskVerdict] }).refusal, null);
|
|
50
|
+
assert.equal(check({ records: [{ ...branchVerdict, branch: "feat/2-y" }] }).refusal, null);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("a second branch review with no Reviewed line is refused, and the refusal names the increment", () => {
|
|
54
|
+
const { refusal } = check({ records: [branchVerdict] });
|
|
55
|
+
assert.match(refusal, /^Task gate:/);
|
|
56
|
+
assert.match(refusal, /feat\/1-x/);
|
|
57
|
+
assert.match(refusal, /Reviewed: ccccccc/);
|
|
58
|
+
assert.match(refusal, /delta diff/);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("a second branch review that carries a Reviewed line passes", () => {
|
|
62
|
+
assert.equal(check({ records: [branchVerdict], prompt: branchPrompt("Reviewed: ccccccc") }).refusal, null);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("the refusal and the Reviewed line speak of the last head a branch review named", () => {
|
|
66
|
+
assert.match(check({ records: [branchVerdict] }).refusal, /the last head a branch review named/);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("a Reviewed sha that no branch verdict on the branch names is refused, and the refusal names the right one", () => {
|
|
70
|
+
const { refusal } = check({ records: [branchVerdict], prompt: branchPrompt("Reviewed: 1234567") });
|
|
71
|
+
assert.match(refusal, /^Task gate:/);
|
|
72
|
+
assert.match(refusal, /1234567/);
|
|
73
|
+
assert.match(refusal, /Reviewed: ccccccc/);
|
|
74
|
+
assert.match(refusal, /the last head a branch review named/);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("a Reviewed sha matches a branch verdict of either verdict, and only one on this branch", () => {
|
|
78
|
+
const changes = { ...branchVerdict, verdict: "CHANGES_REQUIRED", sha: "d".repeat(40) };
|
|
79
|
+
assert.equal(check({ records: [branchVerdict, changes], prompt: branchPrompt("Reviewed: ddddddd") }).refusal, null);
|
|
80
|
+
assert.equal(check({ records: [branchVerdict, changes], prompt: branchPrompt("Reviewed: ccccccc") }).refusal, null);
|
|
81
|
+
const other = { ...branchVerdict, branch: "feat/2-y", sha: "e".repeat(40) };
|
|
82
|
+
assert.match(check({ records: [branchVerdict, other], prompt: branchPrompt("Reviewed: eeeeeee") }).refusal ?? "", /Reviewed: ccccccc/);
|
|
83
|
+
const task = { ...branchVerdict, kind: "task", sha: "f".repeat(40) };
|
|
84
|
+
assert.match(check({ records: [branchVerdict, task], prompt: branchPrompt("Reviewed: fffffff") }).refusal ?? "", /Reviewed: ccccccc/);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("a Reviewed line with no branch verdict on the branch passes, since the ledger has nothing to match", () => {
|
|
88
|
+
assert.equal(check({ prompt: branchPrompt("Reviewed: 1234567") }).refusal, null);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("the task reviewer is not held to one review", () => {
|
|
92
|
+
assert.equal(check({ type: "sdd-reviewer", records: [branchVerdict] }).refusal, null);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("oneBranchReview off lets a second branch review through", () => {
|
|
96
|
+
assert.equal(check({ records: [branchVerdict], review: { ...review, oneBranchReview: false, reviewerTypes: { task: [], branch: ["sdd-branch-reviewer"] } } }).refusal, null);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Green CI
|
|
100
|
+
|
|
101
|
+
test("a red or running head refuses a reviewer of either kind, and the refusal names the run", () => {
|
|
102
|
+
const ciState = () => ({ reason: "CI on aaaaaaa: build ended failure", unknown: null });
|
|
103
|
+
for (const type of ["sdd-reviewer", "sdd-branch-reviewer"]) {
|
|
104
|
+
const { refusal } = check({ type, ciState });
|
|
105
|
+
assert.match(refusal, /^Task gate:/);
|
|
106
|
+
assert.match(refusal, /build ended failure/);
|
|
107
|
+
assert.match(refusal, /green/);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("a green head passes with no note", () => {
|
|
112
|
+
assert.deepEqual(check({ ciState: () => ({ reason: null, unknown: null }) }), { refusal: null, note: null });
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("a head whose CI is unknown passes with a note that says why", () => {
|
|
116
|
+
const { refusal, note } = check({ ciState: () => ({ reason: null, unknown: "gh failed or timed out" }) });
|
|
117
|
+
assert.equal(refusal, null);
|
|
118
|
+
assert.match(note, /CI on aaaaaaa is unknown: gh failed or timed out/);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("requireGreen off never asks CI", () => {
|
|
122
|
+
let asked = 0;
|
|
123
|
+
const ciState = () => ((asked += 1), { reason: "red", unknown: null });
|
|
124
|
+
assert.equal(check({ ciState, review: { ...review, requireGreen: false, reviewerTypes: { task: [], branch: ["sdd-branch-reviewer"] } } }).refusal, null);
|
|
125
|
+
assert.equal(asked, 0);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Head contains the base tip
|
|
129
|
+
|
|
130
|
+
const tipReads = (over = {}) => ({ remoteTip: () => TIP, lookup: () => ({ sha: TIP }), isAncestor: () => true, ...over });
|
|
131
|
+
|
|
132
|
+
test("a head that contains the tip of the base passes", () => {
|
|
133
|
+
assert.deepEqual(baseCheck({ root: "/w", base: "origin/main", head: SHA, reads: tipReads() }), { refusal: null, note: null });
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("a tip that is missing locally refuses, and the refusal says to fetch then merge", () => {
|
|
137
|
+
const { refusal } = baseCheck({ root: "/w", base: "origin/main", head: SHA, reads: tipReads({ lookup: () => ({ unknown: true }) }) });
|
|
138
|
+
assert.match(refusal, /git fetch/);
|
|
139
|
+
assert.match(refusal, /merge origin\/main into the branch/);
|
|
140
|
+
assert.match(refusal, /bbbbbbb/);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("a head that lacks the tip refuses, and the refusal says CI tests the merge ref", () => {
|
|
144
|
+
const { refusal } = baseCheck({ root: "/w", base: "origin/main", head: SHA, reads: tipReads({ isAncestor: () => false }) });
|
|
145
|
+
assert.match(refusal, /merge origin\/main into the branch first/);
|
|
146
|
+
assert.match(refusal, /CI tests the merge ref/);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("the remote is read for the branch after origin/, and a bare name too", () => {
|
|
150
|
+
const asked = [];
|
|
151
|
+
const remoteTip = (_cwd, remote, branch) => (asked.push([remote, branch]), TIP);
|
|
152
|
+
baseCheck({ root: "/w", base: "origin/dev", head: SHA, reads: tipReads({ remoteTip }) });
|
|
153
|
+
baseCheck({ root: "/w", base: "main", head: SHA, reads: tipReads({ remoteTip }) });
|
|
154
|
+
assert.deepEqual(asked, [["origin", "dev"], ["origin", "main"]]);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("an origin that does not answer falls back to the local ref, with a note", () => {
|
|
158
|
+
const lookups = [];
|
|
159
|
+
const lookup = (_cwd, ref) => (lookups.push(ref), { sha: TIP });
|
|
160
|
+
const { refusal, note } = baseCheck({ root: "/w", base: "origin/main", head: SHA, reads: tipReads({ remoteTip: () => null, lookup }) });
|
|
161
|
+
assert.equal(refusal, null);
|
|
162
|
+
assert.deepEqual(lookups, ["origin/main"]);
|
|
163
|
+
assert.match(note, /local origin\/main/);
|
|
164
|
+
const stale = baseCheck({ root: "/w", base: "origin/main", head: SHA, reads: tipReads({ remoteTip: () => null, isAncestor: () => false }) });
|
|
165
|
+
assert.match(stale.refusal, /merge origin\/main into the branch first/);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test("a base that git cannot read passes with a note", () => {
|
|
169
|
+
const none = baseCheck({ root: "/w", base: "origin/main", head: SHA, reads: tipReads({ remoteTip: () => null, lookup: () => ({ unknown: true }) }) });
|
|
170
|
+
assert.equal(none.refusal, null);
|
|
171
|
+
assert.match(none.note, /origin\/main/);
|
|
172
|
+
const failed = baseCheck({ root: "/w", base: "origin/main", head: SHA, reads: tipReads({ lookup: () => ({ error: "git timed out" }) }) });
|
|
173
|
+
assert.equal(failed.refusal, null);
|
|
174
|
+
assert.match(failed.note, /git timed out/);
|
|
175
|
+
const unsure = baseCheck({ root: "/w", base: "origin/main", head: SHA, reads: tipReads({ isAncestor: () => null }) });
|
|
176
|
+
assert.equal(unsure.refusal, null);
|
|
177
|
+
assert.match(unsure.note, /could not tell/);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("requireBase off never reads the base", () => {
|
|
181
|
+
const reads = { remoteTip: () => assert.fail("read"), lookup: () => assert.fail("read"), isAncestor: () => assert.fail("read") };
|
|
182
|
+
assert.equal(check({ reads, review: { ...review, requireBase: false, reviewerTypes: { task: [], branch: ["sdd-branch-reviewer"] } } }).refusal, null);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("a reviewer of either kind whose head or branch cannot be read is refused, and the refusal names the Worktree line", () => {
|
|
186
|
+
for (const type of ["sdd-reviewer", "sdd-branch-reviewer"]) {
|
|
187
|
+
for (const over of [{ head: null }, { branch: null }]) {
|
|
188
|
+
const { refusal } = check({ type, ...over, ciState: () => assert.fail("CI asked"), reads: {} });
|
|
189
|
+
assert.match(refusal, /^Task gate:/);
|
|
190
|
+
assert.match(refusal, /Worktree: <path>/);
|
|
191
|
+
assert.match(refusal, /checked-out branch/);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("the base check runs before CI, so a stale head is not sent to wait on a red run", () => {
|
|
197
|
+
const result = check({ reads: tipReads({ isAncestor: () => false }), ciState: () => assert.fail("CI asked") });
|
|
198
|
+
assert.match(result.refusal, /merge origin\/main/);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// The brief
|
|
202
|
+
|
|
203
|
+
const brief = (...sections) => sections.join("\n");
|
|
204
|
+
const files = (n) => Array.from({ length: n }, (_unused, index) => `src/file${index}.ts`).join(", ");
|
|
205
|
+
const findings = (n) => Array.from({ length: n }, (_unused, index) => `${index + 1}. fix thing ${index}`).join("\n");
|
|
206
|
+
const onBrief = (file) => file.replaceAll("\\", "/").endsWith("/plans/brief.md");
|
|
207
|
+
const briefCheckOf = (text, over = {}) =>
|
|
208
|
+
briefCheck({ prompt: "Read it.\nBrief: /plans/brief.md\nReport: /plans/report.md", dirs: ["/w"], review, exists: onBrief, read: () => text, ...over });
|
|
209
|
+
const offAll = { ...review, productDecisions: false, roundFindings: 0, roundFiles: 0 };
|
|
210
|
+
|
|
211
|
+
test("a brief with a Product decisions heading passes, with no note when it is under the limits", () => {
|
|
212
|
+
assert.deepEqual(briefCheckOf(brief("# Brief", "## Product decisions", "None.", "1. one", `Files: ${files(3)}`)), { refusal: null, note: null });
|
|
213
|
+
assert.equal(briefCheckOf("## product decisions \nNone.").refusal, null);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("a brief with no Product decisions heading is refused, and the refusal names the heading and the questions", () => {
|
|
217
|
+
const { refusal } = briefCheckOf("# Brief\nDo the work.");
|
|
218
|
+
assert.match(refusal, /^Task gate:/);
|
|
219
|
+
assert.match(refusal, /plans\/brief\.md/);
|
|
220
|
+
assert.match(refusal, /## Product decisions/);
|
|
221
|
+
assert.match(refusal, /one question/);
|
|
222
|
+
assert.equal(briefCheckOf("Product decisions are in the issue.").refusal !== null, true);
|
|
223
|
+
assert.equal(briefCheckOf("### Product decisions\nx").refusal !== null, true);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("a Product decisions heading with no text before the next heading, or the end, is refused", () => {
|
|
227
|
+
for (const text of ["## Product decisions\n", "## Product decisions\n\n \n", "## Product decisions\n\n## Findings\n1. a", "# Brief\n## Product decisions\r\n\r\n# Next\r\nx"]) {
|
|
228
|
+
const { refusal } = briefCheckOf(text);
|
|
229
|
+
assert.match(refusal ?? "", /^Task gate:.*empty/, JSON.stringify(text));
|
|
230
|
+
assert.match(refusal, /## Product decisions/);
|
|
231
|
+
assert.match(refusal, /`none`/);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("the text under the heading is a ruling or the word none, and a sub-heading is text", () => {
|
|
236
|
+
assert.equal(briefCheckOf("## Product decisions\nnone\n## Findings").refusal, null);
|
|
237
|
+
assert.equal(briefCheckOf("## Product decisions\n- A reviewer with no head is refused.\n").refusal, null);
|
|
238
|
+
assert.equal(briefCheckOf("## Product decisions\n### Ruling\nx").refusal, null);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("a prompt with no Brief line is refused, and the refusal names the line, even when the prompt holds the heading", () => {
|
|
242
|
+
const prompt = "Do the work. See /plans/brief.md.\n## Product decisions\nNone.";
|
|
243
|
+
const { refusal } = briefCheck({ prompt, dirs: ["/w"], review, exists: () => assert.fail("looked"), read: () => assert.fail("read") });
|
|
244
|
+
assert.match(refusal, /^Task gate:/);
|
|
245
|
+
assert.match(refusal, /`Brief: <path>`/);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
test("a Brief line whose file is missing or unreadable is refused, and the refusal names the line and the path", () => {
|
|
249
|
+
const missing = briefCheck({ prompt: "Brief: /plans/gone.md", dirs: ["/w"], review, exists: () => false, read: () => assert.fail("read") });
|
|
250
|
+
assert.match(missing.refusal, /`Brief: <path>`/);
|
|
251
|
+
assert.match(missing.refusal, /\/plans\/gone\.md/);
|
|
252
|
+
const read = () => {
|
|
253
|
+
throw new Error("EACCES");
|
|
254
|
+
};
|
|
255
|
+
const unreadable = briefCheck({ prompt: "Brief: /plans/brief.md", dirs: ["/w"], review, exists: () => true, read });
|
|
256
|
+
assert.match(unreadable.refusal, /`Brief: <path>`/);
|
|
257
|
+
assert.match(unreadable.refusal, /EACCES/);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("the brief is the path on the Brief line, and no other .md path or the hand-off note is", () => {
|
|
261
|
+
const read = (file) => (file.endsWith("brief.md") ? "## Product decisions\nNone." : assert.fail(file));
|
|
262
|
+
const exists = (file) => file.endsWith("note.md") || file.endsWith("brief.md") || file.endsWith("report.md");
|
|
263
|
+
const prompt = "Report: /plans/report.md\nHANDOFF: /plans/note.md\nBrief: /plans/brief.md\nSee /plans/other.md";
|
|
264
|
+
assert.equal(briefCheck({ prompt, dirs: ["/w"], review, exists, read }).refusal, null);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test("a relative Brief path is found under each directory, and a Git Bash drive path in Windows spelling", () => {
|
|
268
|
+
const seen = [];
|
|
269
|
+
const exists = (file) => (seen.push(file), false);
|
|
270
|
+
briefCheck({ prompt: "Brief: plans/b.md", dirs: ["/w/a", "/w/b"], review, exists, read: () => "" });
|
|
271
|
+
assert.equal(seen.length, 2);
|
|
272
|
+
assert.ok(seen[0].replaceAll("\\", "/").endsWith("/w/a/plans/b.md"));
|
|
273
|
+
assert.ok(seen[1].replaceAll("\\", "/").endsWith("/w/b/plans/b.md"));
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("a quoted Brief path, a Windows path, and a short-name path are read as written", () => {
|
|
277
|
+
for (const spelled of ['"/plans/brief.md"', "`/plans/brief.md`", " /plans/brief.md ", "/plans/brief.md"]) {
|
|
278
|
+
assert.equal(briefCheckOf("## Product decisions\nNone.", { prompt: `Brief: ${spelled}` }).refusal, null, spelled);
|
|
279
|
+
}
|
|
280
|
+
assert.equal(briefPathOf("Brief: C:\\Users\\NGUYEN~1\\s\\brief.md\nReport: /r/report.md"), "C:\\Users\\NGUYEN~1\\s\\brief.md");
|
|
281
|
+
assert.equal(briefPathOf("a\n brief: /b/x.md"), "/b/x.md");
|
|
282
|
+
assert.equal(briefPathOf("The Brief: is inline /b/x.md"), null);
|
|
283
|
+
assert.equal(briefPathOf("Brief:"), null);
|
|
284
|
+
assert.equal(briefPathOf(""), null);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test("productDecisions off never refuses a brief for its heading", () => {
|
|
288
|
+
assert.equal(briefCheckOf("no heading", { review: { ...review, productDecisions: false } }).refusal, null);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("with every brief check off, the brief is not read and no Brief line is needed", () => {
|
|
292
|
+
const result = briefCheck({ prompt: "Do it.", dirs: ["/w"], review: offAll, exists: () => assert.fail("looked"), read: () => assert.fail("read") });
|
|
293
|
+
assert.deepEqual(result, { refusal: null, note: null });
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test("a brief over the findings limit warns and names the split", () => {
|
|
297
|
+
const { refusal, note } = briefCheckOf(brief("## Product decisions", "None.", findings(9)));
|
|
298
|
+
assert.equal(refusal, null);
|
|
299
|
+
assert.match(note, /9 numbered findings/);
|
|
300
|
+
assert.match(note, /roundFindings \(8\)/);
|
|
301
|
+
assert.match(note, /parallel implementers on disjoint paths/);
|
|
302
|
+
assert.match(note, /sub-branches in separate worktrees/);
|
|
303
|
+
assert.match(note, /merged by the controller/);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
test("a brief over the files limit warns, and one at the limit does not", () => {
|
|
307
|
+
const over = briefCheckOf(brief("## Product decisions", "None.", `Files: ${files(11)}`));
|
|
308
|
+
assert.match(over.note, /11 files/);
|
|
309
|
+
assert.match(over.note, /roundFiles \(10\)/);
|
|
310
|
+
assert.equal(briefCheckOf(brief("## Product decisions", "None.", findings(8), `Files: ${files(10)}`)).note, null);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test("a limit of 0 turns its warning off", () => {
|
|
314
|
+
const off = { ...review, roundFindings: 0, roundFiles: 0 };
|
|
315
|
+
assert.equal(briefCheckOf(brief("## Product decisions", "None.", findings(30), files(30)), { review: off }).note, null);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test("a refused brief reports no round-size note", () => {
|
|
319
|
+
const result = briefCheckOf(brief(findings(30), files(30)));
|
|
320
|
+
assert.ok(result.refusal);
|
|
321
|
+
assert.equal(result.note, null);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
test("numbered findings are the lines that start with a number and a dot", () => {
|
|
325
|
+
assert.equal(countFindings("1. a\n 2. b\n10. c\nversion 1. x\n1) no\n- 3. no"), 3);
|
|
326
|
+
assert.equal(countFindings(""), 0);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("a file is a path with a separator or a known extension; a URL, a version, and an abbreviation are not", () => {
|
|
330
|
+
assert.equal(countFiles("src/a.ts and src\\b.ts and c.mjs and src/a.ts again"), 3);
|
|
331
|
+
assert.equal(countFiles("see https://github.com/o/r/issues/1 and e.g. v0.20.4, i.e. the 1.5x speed"), 0);
|
|
332
|
+
assert.equal(countFiles("docs/a.md, `packages/x/y.json`, (hooks/hooks.json)."), 3);
|
|
333
|
+
assert.equal(countFiles("README.md and README.md"), 1);
|
|
334
|
+
});
|
|
@@ -51,10 +51,11 @@ export function segmentsOf(command, { escape = POSIX_ESCAPE } = {}) {
|
|
|
51
51
|
word = "";
|
|
52
52
|
inWord = false;
|
|
53
53
|
};
|
|
54
|
-
|
|
54
|
+
// A segment that ends at `(` holds a command whose argument is a subexpression, so `grouped` marks it.
|
|
55
|
+
const endSegment = (grouped = false) => {
|
|
55
56
|
endWord();
|
|
56
57
|
while (LINE_KEYWORDS.has(words[0])) words.shift();
|
|
57
|
-
if (words.length > 0 || redirects.length > 0) segments.push({ words, redirects });
|
|
58
|
+
if (words.length > 0 || redirects.length > 0) segments.push(grouped ? { words, redirects, grouped } : { words, redirects });
|
|
58
59
|
words = [];
|
|
59
60
|
redirects = [];
|
|
60
61
|
};
|
|
@@ -94,7 +95,7 @@ export function segmentsOf(command, { escape = POSIX_ESCAPE } = {}) {
|
|
|
94
95
|
endWord();
|
|
95
96
|
i++;
|
|
96
97
|
} else if (c === ";" || c === "(" || c === ")") {
|
|
97
|
-
endSegment();
|
|
98
|
+
endSegment(c === "(");
|
|
98
99
|
i++;
|
|
99
100
|
} else if (c === "|") {
|
|
100
101
|
endSegment();
|
|
@@ -233,27 +234,41 @@ export const commandEnv = (segment) => unwrap(segment.words).env;
|
|
|
233
234
|
const isGit = (word) => /(^|[\\/])git(\.exe)?$/i.test(word ?? "");
|
|
234
235
|
|
|
235
236
|
/**
|
|
236
|
-
* @returns the git subcommand, each `-C` directory, each `-c` setting and the words after the
|
|
237
|
-
*
|
|
237
|
+
* @returns the git subcommand, each `-C` directory, each `-c` setting and the words after the subcommand, or null
|
|
238
|
+
* when the segment is no git call. `explicitDir` is true when the call names a git directory or a work tree, through
|
|
239
|
+
* `--git-dir`, `--work-tree`, `GIT_DIR` or `GIT_WORK_TREE`. It is never resolved: git reads a relative one from the
|
|
240
|
+
* working directory after every `-C`, so no later reader can know which repository it picks.
|
|
238
241
|
*/
|
|
239
242
|
export function gitCall(segment) {
|
|
240
243
|
const [program, ...words] = commandWords(segment);
|
|
241
244
|
if (!isGit(program)) return null;
|
|
245
|
+
const { GIT_DIR, GIT_WORK_TREE } = commandEnv(segment);
|
|
246
|
+
let explicitDir = Boolean(GIT_DIR || GIT_WORK_TREE);
|
|
242
247
|
const dirs = [];
|
|
243
248
|
const configs = [];
|
|
244
249
|
for (let i = 0; i < words.length; i++) {
|
|
245
250
|
const word = words[i];
|
|
246
251
|
if (word === "-C") dirs.push(words[++i]);
|
|
252
|
+
else if (word === "--git-dir" || word === "--work-tree") {
|
|
253
|
+
explicitDir = true;
|
|
254
|
+
i++;
|
|
255
|
+
} else if (/^--(git-dir|work-tree)=/.test(word)) explicitDir = true;
|
|
247
256
|
else if (word === "-c") configs.push(words[++i]);
|
|
248
|
-
else if (!word.startsWith("-")) return { sub: word, dirs, configs, args: words.slice(i + 1) };
|
|
257
|
+
else if (!word.startsWith("-")) return { sub: word, dirs, explicitDir, configs, args: words.slice(i + 1) };
|
|
249
258
|
}
|
|
250
|
-
return { sub: null, dirs, configs, args: [] };
|
|
259
|
+
return { sub: null, dirs, explicitDir, configs, args: [] };
|
|
251
260
|
}
|
|
252
261
|
|
|
262
|
+
/** The spelling of a location target that is a subexpression: not a literal path, so a guard cannot resolve it. */
|
|
263
|
+
export const GROUPED_TARGET = "$(...)";
|
|
264
|
+
|
|
265
|
+
/** The target of a location change that names none: home, unless a subexpression follows it. */
|
|
266
|
+
export const homeOrGroup = (segment) => (segment.grouped ? GROUPED_TARGET : "~");
|
|
267
|
+
|
|
253
268
|
/** @returns the directory a `cd` or `pushd` segment moves to, or null. */
|
|
254
269
|
export function cdTarget(segment) {
|
|
255
270
|
const [program, target] = commandWords(segment);
|
|
256
|
-
return program === "cd" || program === "pushd" ? (target ??
|
|
271
|
+
return program === "cd" || program === "pushd" ? (target ?? homeOrGroup(segment)) : null;
|
|
257
272
|
}
|
|
258
273
|
|
|
259
274
|
const READ_PROGRAMS = new Set([
|
|
@@ -284,13 +299,62 @@ export function isReadOnly(segment) {
|
|
|
284
299
|
/** False only when every segment is known to read, so an unknown program counts as a write. */
|
|
285
300
|
export const mayWrite = (command) => !segmentsOf(command).every(isReadOnly);
|
|
286
301
|
|
|
287
|
-
|
|
288
|
-
|
|
302
|
+
const TREE_KEYS = { GIT_DIR: "gitDirs", GIT_WORK_TREE: "workTrees" };
|
|
303
|
+
const PS_ENV = /^\$env:([A-Za-z_]\w*)(?:=(.*))?$/is;
|
|
304
|
+
|
|
305
|
+
/** The git directories and work trees one segment sets: through `--git-dir` and `--work-tree`, its environment, `export`, `set`, and `$env:`. */
|
|
306
|
+
function gitTrees(segment) {
|
|
307
|
+
const trees = { gitDirs: [], workTrees: [] };
|
|
308
|
+
const add = (name, value) => {
|
|
309
|
+
const key = TREE_KEYS[name.toUpperCase()];
|
|
310
|
+
if (key && value) trees[key].push(value);
|
|
311
|
+
};
|
|
312
|
+
const { words, env } = unwrap(segment.words);
|
|
313
|
+
for (const [name, value] of Object.entries(env)) add(name, value);
|
|
314
|
+
const [program, ...args] = words;
|
|
315
|
+
if (["export", "set", "setx"].includes(baseName(program ?? ""))) {
|
|
316
|
+
for (const arg of args) add(...(ASSIGNMENT.exec(arg)?.slice(1) ?? ["", ""]));
|
|
317
|
+
} else if (PS_ENV.test(program ?? "")) {
|
|
318
|
+
const [, name, inline] = PS_ENV.exec(program);
|
|
319
|
+
add(name, inline ?? (args[0] === "=" ? args[1] : undefined));
|
|
320
|
+
} else if (isGit(program)) {
|
|
321
|
+
for (let i = 0; i < args.length; i++) {
|
|
322
|
+
const [, option, inline] = /^(--git-dir|--work-tree)(?:=(.*))?$/s.exec(args[i]) ?? [];
|
|
323
|
+
if (option) add(option === "--git-dir" ? "GIT_DIR" : "GIT_WORK_TREE", inline ?? args[++i]);
|
|
324
|
+
else if (args[i] === "-C" || args[i] === "-c") i++;
|
|
325
|
+
else if (!args[i].startsWith("-")) break;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return trees;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* @param locate the dialect's reader of a location change: `cdTarget` or `locationTarget`
|
|
333
|
+
* @returns every directory the segments name through a location change, `git -C` or a work tree, in order, as written.
|
|
334
|
+
* A work tree counts because `checkout` and `restore` write there.
|
|
335
|
+
*/
|
|
336
|
+
export function checkoutDirsOf(segments, locate) {
|
|
289
337
|
const dirs = [];
|
|
290
|
-
for (const segment of
|
|
291
|
-
const
|
|
292
|
-
if (
|
|
338
|
+
for (const segment of segments) {
|
|
339
|
+
const target = locate(segment);
|
|
340
|
+
if (target !== null) dirs.push(target);
|
|
293
341
|
dirs.push(...(gitCall(segment)?.dirs ?? []).filter(Boolean));
|
|
342
|
+
dirs.push(...gitTrees(segment).workTrees);
|
|
294
343
|
}
|
|
295
344
|
return dirs;
|
|
296
345
|
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* @returns the `--git-dir` and `GIT_DIR` values of a command that sets no work tree. Their checkout cannot be told from
|
|
349
|
+
* the command, so a caller reports them instead of dropping them.
|
|
350
|
+
*/
|
|
351
|
+
export function unresolvedGitDirsOf(segments) {
|
|
352
|
+
const all = segments.map(gitTrees);
|
|
353
|
+
return all.some((trees) => trees.workTrees.length > 0) ? [] : all.flatMap((trees) => trees.gitDirs);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Every directory the command names through `cd`, `git -C` or a work tree, in order, as written. */
|
|
357
|
+
export const checkoutDirs = (command) => checkoutDirsOf(segmentsOf(command), cdTarget);
|
|
358
|
+
|
|
359
|
+
/** The git directories a command sets with no work tree. */
|
|
360
|
+
export const unresolvedGitDirs = (command) => unresolvedGitDirsOf(segmentsOf(command));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
2
|
import { test } from "node:test";
|
|
3
|
-
import { checkoutDirs, commandEnv, commandWords, gitCall, isReadOnly, mayWrite, segmentsOf } from "./shell-command.mjs";
|
|
3
|
+
import { checkoutDirs, commandEnv, commandWords, gitCall, isReadOnly, mayWrite, segmentsOf, unresolvedGitDirs } from "./shell-command.mjs";
|
|
4
4
|
|
|
5
5
|
const wordsOf = (command) => segmentsOf(command).map((segment) => segment.words);
|
|
6
6
|
|
|
@@ -32,9 +32,28 @@ test("a redirect target is recorded apart from the words, and a descriptor copy
|
|
|
32
32
|
assert.deepEqual(segmentsOf("cat log >> all.log")[0].redirects, ["all.log"]);
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
+
test("a git call flags an explicit git dir or work tree, and does not resolve it", () => {
|
|
36
|
+
const explicitOf = (command) => gitCall(segmentsOf(command)[0]).explicitDir;
|
|
37
|
+
for (const command of [
|
|
38
|
+
"git --git-dir=/r/.git status",
|
|
39
|
+
"git --git-dir /r/.git status",
|
|
40
|
+
"GIT_DIR=/r/.git git status",
|
|
41
|
+
"env GIT_DIR=/r/.git git status",
|
|
42
|
+
"git -C /a --git-dir=/r/.git status",
|
|
43
|
+
"git --work-tree=/r status",
|
|
44
|
+
"git --work-tree /r status",
|
|
45
|
+
"GIT_WORK_TREE=/r git status",
|
|
46
|
+
]) {
|
|
47
|
+
assert.equal(explicitOf(command), true, command);
|
|
48
|
+
}
|
|
49
|
+
for (const command of ["git status", "git -C /a status", "git -c core.x=y status"]) assert.equal(explicitOf(command), false, command);
|
|
50
|
+
assert.deepEqual(gitCall(segmentsOf("git -C /a --git-dir /r/.git status")[0]).dirs, ["/a"], "the git dir is no checkout directory");
|
|
51
|
+
assert.equal(gitCall(segmentsOf("git --git-dir /r/.git status")[0]).sub, "status", "the value is not the subcommand");
|
|
52
|
+
});
|
|
53
|
+
|
|
35
54
|
test("a git call names its subcommand and every -C directory", () => {
|
|
36
55
|
const [segment] = segmentsOf(`GIT_PAGER=cat git -C "C:/a b" -c core.x=y --no-pager commit -m x`);
|
|
37
|
-
assert.deepEqual(gitCall(segment), { sub: "commit", dirs: ["C:/a b"], configs: ["core.x=y"], args: ["-m", "x"] });
|
|
56
|
+
assert.deepEqual(gitCall(segment), { sub: "commit", dirs: ["C:/a b"], explicitDir: false, configs: ["core.x=y"], args: ["-m", "x"] });
|
|
38
57
|
assert.equal(gitCall(segmentsOf("gitk --all")[0]), null);
|
|
39
58
|
});
|
|
40
59
|
|
|
@@ -92,6 +111,7 @@ test("the checkout directories are every cd target and every git -C directory, i
|
|
|
92
111
|
"/repo/d",
|
|
93
112
|
]);
|
|
94
113
|
assert.deepEqual(checkoutDirs("ls"), []);
|
|
114
|
+
assert.deepEqual(checkoutDirs("echo x > f; git -C"), []);
|
|
95
115
|
});
|
|
96
116
|
|
|
97
117
|
test("a git call carries each -c setting and the words after its subcommand", () => {
|
|
@@ -99,10 +119,11 @@ test("a git call carries each -c setting and the words after its subcommand", ()
|
|
|
99
119
|
assert.deepEqual(gitCall(segment), {
|
|
100
120
|
sub: "commit",
|
|
101
121
|
dirs: ["repo"],
|
|
122
|
+
explicitDir: false,
|
|
102
123
|
configs: ["core.hooksPath=/dev/null"],
|
|
103
124
|
args: ["-m", "a b", "--no-verify"],
|
|
104
125
|
});
|
|
105
|
-
assert.deepEqual(gitCall(segmentsOf("git --version")[0]), { sub: null, dirs: [], configs: [], args: [] });
|
|
126
|
+
assert.deepEqual(gitCall(segmentsOf("git --version")[0]), { sub: null, dirs: [], explicitDir: false, configs: [], args: [] });
|
|
106
127
|
});
|
|
107
128
|
|
|
108
129
|
test("the PowerShell escape keeps a backslash literal and escapes with a backtick", () => {
|
|
@@ -216,3 +237,23 @@ test("a leading ! is dropped, so the negated command is read", () => {
|
|
|
216
237
|
assert.deepEqual(segmentsOf("if ! git diff --quiet; then x; fi").map((segment) => segment.words)[0], ["git", "diff", "--quiet"]);
|
|
217
238
|
assert.deepEqual(segmentsOf("echo !").map((segment) => segment.words), [["echo", "!"]]);
|
|
218
239
|
});
|
|
240
|
+
|
|
241
|
+
test("a work tree counts as a checkout directory, through the option, the environment, or an export", () => {
|
|
242
|
+
assert.deepEqual(checkoutDirs("git --work-tree=/w checkout main"), ["/w"]);
|
|
243
|
+
assert.deepEqual(checkoutDirs("git --work-tree /w restore f"), ["/w"]);
|
|
244
|
+
assert.deepEqual(checkoutDirs("GIT_WORK_TREE=/w git checkout main"), ["/w"]);
|
|
245
|
+
assert.deepEqual(checkoutDirs("export GIT_WORK_TREE=/w; git checkout main"), ["/w"]);
|
|
246
|
+
assert.deepEqual(checkoutDirs("git -C /a --work-tree=/w checkout main"), ["/a", "/w"]);
|
|
247
|
+
assert.deepEqual(checkoutDirs("git --work-tree"), [], "a bare option yields no empty directory");
|
|
248
|
+
assert.deepEqual(checkoutDirs("git -C"), []);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test("a git dir with no work tree is reported as unresolved, and one with a work tree is not", () => {
|
|
252
|
+
assert.deepEqual(unresolvedGitDirs("git --git-dir=/r/.git checkout main"), ["/r/.git"]);
|
|
253
|
+
assert.deepEqual(unresolvedGitDirs("git --git-dir /r/.git checkout main"), ["/r/.git"]);
|
|
254
|
+
assert.deepEqual(unresolvedGitDirs("GIT_DIR=/r/.git git checkout main"), ["/r/.git"]);
|
|
255
|
+
assert.deepEqual(unresolvedGitDirs("export GIT_DIR=/r/.git; git checkout main"), ["/r/.git"]);
|
|
256
|
+
assert.deepEqual(unresolvedGitDirs("git --git-dir=/r/.git --work-tree=/w checkout main"), []);
|
|
257
|
+
assert.deepEqual(unresolvedGitDirs("git -C /a status"), []);
|
|
258
|
+
assert.deepEqual(unresolvedGitDirs("git --git-dir"), [], "a bare option has no value to report");
|
|
259
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// PreToolUse on Skill, a check of `swarm.dispatch`: the model never runs a code-review skill. The user
|
|
3
|
+
// types `/code-review`, which never reaches this tool, and a branch review is a dispatch of the reviewer.
|
|
4
|
+
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { workflowAt } from "./workflow-settings.mjs";
|
|
7
|
+
|
|
8
|
+
const output = (fields) => ({ hookSpecificOutput: { hookEventName: "PreToolUse", ...fields } });
|
|
9
|
+
|
|
10
|
+
/** True when `name`, or the part after its last `:`, is in `list`. Case and a leading slash do not count. */
|
|
11
|
+
export function isListedSkill(name, list) {
|
|
12
|
+
if (typeof name !== "string") return false;
|
|
13
|
+
const bare = name.trim().replace(/^\//, "").toLowerCase();
|
|
14
|
+
if (bare === "") return false;
|
|
15
|
+
const lower = list.map((entry) => entry.toLowerCase());
|
|
16
|
+
return lower.includes(bare) || lower.includes(bare.slice(bare.lastIndexOf(":") + 1));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const refusal = (name) =>
|
|
20
|
+
`Skill guard: the model may not run the ${name} skill while swarm.dispatch is on, because it reviews in the main session's context. ` +
|
|
21
|
+
"Dispatch architecture:sdd-branch-reviewer for a branch review, or ask the user to type `/code-review`. " +
|
|
22
|
+
"To allow a skill, remove it from swarm.review.codeReviewSkills in qc.config.json.";
|
|
23
|
+
|
|
24
|
+
/** @returns the hook output, or null to let the call run. */
|
|
25
|
+
export function decide(call) {
|
|
26
|
+
if (call.hook_event_name !== "PreToolUse" || call.tool_name !== "Skill") return null;
|
|
27
|
+
const name = call.tool_input?.skill;
|
|
28
|
+
if (typeof name !== "string") return null;
|
|
29
|
+
let workflow;
|
|
30
|
+
try {
|
|
31
|
+
workflow = workflowAt(call.cwd ?? process.cwd());
|
|
32
|
+
} catch (error) {
|
|
33
|
+
// A config typo must not stop every skill; the session sees the error and reports it.
|
|
34
|
+
return output({ additionalContext: `Skill guard is off: ${error.message}` });
|
|
35
|
+
}
|
|
36
|
+
if (!workflow || !isListedSkill(name, workflow.review.codeReviewSkills)) return null;
|
|
37
|
+
return output({ permissionDecision: "deny", permissionDecisionReason: refusal(name.trim()) });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function readStdin() {
|
|
41
|
+
let data = "";
|
|
42
|
+
for await (const chunk of process.stdin) data += chunk;
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const isEntryPoint = process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1];
|
|
47
|
+
if (isEntryPoint) {
|
|
48
|
+
let call = null;
|
|
49
|
+
try {
|
|
50
|
+
call = JSON.parse(await readStdin());
|
|
51
|
+
} catch {
|
|
52
|
+
// Not hook input, so there is nothing to judge.
|
|
53
|
+
}
|
|
54
|
+
const result = call ? decide(call) : null;
|
|
55
|
+
if (result) process.stdout.write(JSON.stringify(result));
|
|
56
|
+
}
|