architecture-harness 0.20.3 → 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 +28 -5
- package/src/cli/bash-command-guard.test.mjs +100 -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 +17 -9
- package/src/cli/ci-state.test.mjs +15 -1
- 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 +5 -3
- package/src/cli/dispatch-reminder.test.mjs +5 -1
- package/src/cli/git-read.mjs +28 -3
- package/src/cli/git-read.test.mjs +31 -1
- package/src/cli/ledger.mjs +50 -3
- package/src/cli/ledger.test.mjs +46 -0
- package/src/cli/powershell-command.mjs +8 -13
- package/src/cli/powershell-command.test.mjs +11 -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 +65 -9
- package/src/cli/shell-command.test.mjs +21 -1
- 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 +35 -11
- package/src/cli/task-gate.test.mjs +6 -5
- 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 +11 -0
|
@@ -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();
|
|
@@ -258,10 +259,16 @@ export function gitCall(segment) {
|
|
|
258
259
|
return { sub: null, dirs, explicitDir, configs, args: [] };
|
|
259
260
|
}
|
|
260
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
|
+
|
|
261
268
|
/** @returns the directory a `cd` or `pushd` segment moves to, or null. */
|
|
262
269
|
export function cdTarget(segment) {
|
|
263
270
|
const [program, target] = commandWords(segment);
|
|
264
|
-
return program === "cd" || program === "pushd" ? (target ??
|
|
271
|
+
return program === "cd" || program === "pushd" ? (target ?? homeOrGroup(segment)) : null;
|
|
265
272
|
}
|
|
266
273
|
|
|
267
274
|
const READ_PROGRAMS = new Set([
|
|
@@ -292,13 +299,62 @@ export function isReadOnly(segment) {
|
|
|
292
299
|
/** False only when every segment is known to read, so an unknown program counts as a write. */
|
|
293
300
|
export const mayWrite = (command) => !segmentsOf(command).every(isReadOnly);
|
|
294
301
|
|
|
295
|
-
|
|
296
|
-
|
|
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) {
|
|
297
337
|
const dirs = [];
|
|
298
|
-
for (const segment of
|
|
299
|
-
const
|
|
300
|
-
if (
|
|
338
|
+
for (const segment of segments) {
|
|
339
|
+
const target = locate(segment);
|
|
340
|
+
if (target !== null) dirs.push(target);
|
|
301
341
|
dirs.push(...(gitCall(segment)?.dirs ?? []).filter(Boolean));
|
|
342
|
+
dirs.push(...gitTrees(segment).workTrees);
|
|
302
343
|
}
|
|
303
344
|
return dirs;
|
|
304
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
|
|
|
@@ -237,3 +237,23 @@ test("a leading ! is dropped, so the negated command is read", () => {
|
|
|
237
237
|
assert.deepEqual(segmentsOf("if ! git diff --quiet; then x; fi").map((segment) => segment.words)[0], ["git", "diff", "--quiet"]);
|
|
238
238
|
assert.deepEqual(segmentsOf("echo !").map((segment) => segment.words), [["echo", "!"]]);
|
|
239
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
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
+
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { decide, isListedSkill } from "./skill-guard.mjs";
|
|
9
|
+
|
|
10
|
+
function checkout(t, config) {
|
|
11
|
+
const dir = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-skill-guard-")));
|
|
12
|
+
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
13
|
+
execFileSync("git", ["init", "-q"], { cwd: dir, stdio: "pipe" });
|
|
14
|
+
if (config !== null) writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify(config));
|
|
15
|
+
return dir;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const call = (cwd, skill, extra = {}) => ({
|
|
19
|
+
session_id: "s",
|
|
20
|
+
cwd,
|
|
21
|
+
hook_event_name: "PreToolUse",
|
|
22
|
+
tool_name: "Skill",
|
|
23
|
+
tool_input: { skill, args: "max" },
|
|
24
|
+
...extra,
|
|
25
|
+
});
|
|
26
|
+
const reasonOf = (result) => result?.hookSpecificOutput?.permissionDecisionReason ?? "";
|
|
27
|
+
|
|
28
|
+
test("a skill is listed by its name or by the part after the colon", () => {
|
|
29
|
+
const list = ["code-review"];
|
|
30
|
+
assert.equal(isListedSkill("code-review", list), true);
|
|
31
|
+
assert.equal(isListedSkill("Code-Review", list), true);
|
|
32
|
+
assert.equal(isListedSkill(" /code-review ", list), true);
|
|
33
|
+
assert.equal(isListedSkill("code-review:code-review", list), true);
|
|
34
|
+
assert.equal(isListedSkill("any-plugin:code-review", list), true);
|
|
35
|
+
assert.equal(isListedSkill("code-reviewer", list), false);
|
|
36
|
+
assert.equal(isListedSkill("superpowers:requesting-code-review", list), false);
|
|
37
|
+
assert.equal(isListedSkill("a:b", ["a:b"]), true);
|
|
38
|
+
assert.equal(isListedSkill("code-review", []), false);
|
|
39
|
+
assert.equal(isListedSkill(undefined, list), false);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("with swarm.dispatch on, the guard refuses a code-review skill and names the way out", (t) => {
|
|
43
|
+
const dir = checkout(t, { swarm: { dispatch: {} } });
|
|
44
|
+
for (const skill of ["code-review", "code-review:code-review", "x:code-review"]) {
|
|
45
|
+
const result = decide(call(dir, skill));
|
|
46
|
+
assert.equal(result.hookSpecificOutput.permissionDecision, "deny", skill);
|
|
47
|
+
assert.match(reasonOf(result), /sdd-branch-reviewer/);
|
|
48
|
+
assert.match(reasonOf(result), /\/code-review/);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("a skill outside the list passes, and so does every other tool", (t) => {
|
|
53
|
+
const dir = checkout(t, { swarm: { dispatch: {} } });
|
|
54
|
+
assert.equal(decide(call(dir, "superpowers:brainstorming")), null);
|
|
55
|
+
assert.equal(decide(call(dir, "code-reviewer")), null);
|
|
56
|
+
assert.equal(decide({ ...call(dir, "code-review"), tool_name: "Read" }), null);
|
|
57
|
+
assert.equal(decide({ ...call(dir, "code-review"), tool_input: {} }), null);
|
|
58
|
+
assert.equal(decide({ ...call(dir, "code-review"), hook_event_name: "PostToolUse" }), null);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("swarm.review.codeReviewSkills replaces the list, and [] turns the check off", (t) => {
|
|
62
|
+
const custom = checkout(t, { swarm: { dispatch: {}, review: { codeReviewSkills: ["my-review"] } } });
|
|
63
|
+
assert.equal(decide(call(custom, "code-review")), null);
|
|
64
|
+
assert.equal(decide(call(custom, "p:my-review")).hookSpecificOutput.permissionDecision, "deny");
|
|
65
|
+
const off = checkout(t, { swarm: { dispatch: {}, review: { codeReviewSkills: [] } } });
|
|
66
|
+
assert.equal(decide(call(off, "code-review")), null);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("the guard is silent where swarm.dispatch is off, or where there is no config", (t) => {
|
|
70
|
+
assert.equal(decide(call(checkout(t, null), "code-review")), null);
|
|
71
|
+
assert.equal(decide(call(checkout(t, { swarm: {} }), "code-review")), null);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("a wrong swarm.review value lets the call through with a note", (t) => {
|
|
75
|
+
const dir = checkout(t, { swarm: { dispatch: {}, review: { codeReviewSkills: "code-review" } } });
|
|
76
|
+
const result = decide(call(dir, "code-review"));
|
|
77
|
+
assert.equal(result.hookSpecificOutput.permissionDecision, undefined);
|
|
78
|
+
assert.match(result.hookSpecificOutput.additionalContext, /Skill guard is off: .*codeReviewSkills/);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("the script reads the hook payload from stdin and writes the refusal", (t) => {
|
|
82
|
+
const dir = checkout(t, { swarm: { dispatch: {} } });
|
|
83
|
+
const script = fileURLToPath(new URL("./skill-guard.mjs", import.meta.url));
|
|
84
|
+
const run = (skill) => spawnSync(process.execPath, [script], { input: JSON.stringify(call(dir, skill)), encoding: "utf8" });
|
|
85
|
+
assert.equal(JSON.parse(run("code-review").stdout).hookSpecificOutput.permissionDecision, "deny");
|
|
86
|
+
assert.equal(run("brainstorming").stdout, "");
|
|
87
|
+
});
|