architecture-harness 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +14 -4
- package/src/cli/bash-command-guard.mjs +25 -8
- package/src/cli/bash-command-guard.test.mjs +44 -0
- package/src/cli/branch-name.test.mjs +31 -0
- package/src/cli/check.mjs +5 -0
- package/src/cli/commit-msg.mjs +94 -0
- package/src/cli/commit-msg.test.mjs +60 -0
- package/src/cli/dispatch-guard.mjs +78 -0
- package/src/cli/dispatch-guard.test.mjs +160 -0
- package/src/cli/dispatch-reminder.mjs +43 -0
- package/src/cli/dispatch-reminder.test.mjs +78 -0
- package/src/cli/dispatch-slot.mjs +70 -0
- package/src/cli/dispatch-slot.test.mjs +80 -0
- package/src/cli/dispatch.mjs +84 -0
- package/src/cli/dispatch.test.mjs +90 -0
- package/src/cli/doctor.mjs +39 -5
- package/src/cli/doctor.test.mjs +18 -1
- package/src/cli/edit-batch-state.mjs +5 -2
- package/src/cli/git-hooks.mjs +67 -0
- package/src/cli/git-hooks.test.mjs +85 -0
- package/src/cli/plugin-agents.test.mjs +54 -0
- package/src/cli/post-edit-gate.test.mjs +10 -0
- package/src/cli/powershell-command.mjs +3 -0
- package/src/cli/pr-check.mjs +74 -0
- package/src/cli/pr-check.test.mjs +59 -0
- package/src/cli/qc.mjs +8 -6
- package/src/cli/stop-gate.test.mjs +113 -0
- package/src/cli/templates.test.mjs +9 -0
- package/src/cli/work-order-guard.mjs +25 -1
- package/src/config.mjs +31 -7
- package/src/gates/config-floor.mjs +23 -8
- package/src/gates/config-floor.test.mjs +10 -0
- package/src/gates/english-source.mjs +1 -1
- package/templates/githooks/commit-msg +2 -1
- package/templates/github/workflows/ci.yml +7 -0
- package/src/cli/commit-attribution.mjs +0 -68
- package/src/cli/commit-attribution.test.mjs +0 -47
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "architecture-harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,11 +51,13 @@
|
|
|
51
51
|
"test": "node src/eslint/rules.test.mjs && node --test \"src/**/*.test.mjs\""
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"eslint": ">=9",
|
|
55
54
|
"@typescript-eslint/eslint-plugin": ">=8",
|
|
56
55
|
"@typescript-eslint/parser": ">=8",
|
|
56
|
+
"eslint": ">=9",
|
|
57
57
|
"eslint-plugin-boundaries": ">=5",
|
|
58
|
-
"eslint-plugin-sonarjs": ">=3"
|
|
58
|
+
"eslint-plugin-sonarjs": ">=3",
|
|
59
|
+
"@commitlint/lint": ">=19",
|
|
60
|
+
"@commitlint/config-conventional": ">=19"
|
|
59
61
|
},
|
|
60
62
|
"peerDependenciesMeta": {
|
|
61
63
|
"@typescript-eslint/eslint-plugin": {
|
|
@@ -69,12 +71,20 @@
|
|
|
69
71
|
},
|
|
70
72
|
"eslint-plugin-sonarjs": {
|
|
71
73
|
"optional": true
|
|
74
|
+
},
|
|
75
|
+
"@commitlint/lint": {
|
|
76
|
+
"optional": true
|
|
77
|
+
},
|
|
78
|
+
"@commitlint/config-conventional": {
|
|
79
|
+
"optional": true
|
|
72
80
|
}
|
|
73
81
|
},
|
|
74
82
|
"devDependencies": {
|
|
75
|
-
"
|
|
83
|
+
"@commitlint/config-conventional": "^21.2.3",
|
|
84
|
+
"@commitlint/lint": "^21.2.3",
|
|
76
85
|
"@typescript-eslint/eslint-plugin": "^8",
|
|
77
86
|
"@typescript-eslint/parser": "^8",
|
|
87
|
+
"eslint": "^9",
|
|
78
88
|
"eslint-plugin-boundaries": "^5",
|
|
79
89
|
"eslint-plugin-sonarjs": "^3",
|
|
80
90
|
"typescript": "^5"
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The PreToolUse trigger that refuses a Bash command which skips the git hooks, or
|
|
3
|
-
// script by hand. It reads the command through shell
|
|
2
|
+
// The PreToolUse trigger that refuses a Bash or PowerShell command which skips the git hooks, or
|
|
3
|
+
// which runs a hook script by hand. It reads the command through the shell parsers and never runs it.
|
|
4
4
|
|
|
5
5
|
import { existsSync } from "node:fs";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import { fileURLToPath } from "node:url";
|
|
8
8
|
import { CONFIG_FILE } from "../config.mjs";
|
|
9
9
|
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
10
|
+
import { powershellSegments } from "./powershell-command.mjs";
|
|
10
11
|
import { commandWords, gitCall, segmentsOf } from "./shell-command.mjs";
|
|
11
12
|
|
|
12
13
|
const HOOK_SCRIPTS = [
|
|
@@ -24,6 +25,11 @@ const VALUE_OPTIONS = new Set([
|
|
|
24
25
|
/** The short options of commit whose value is the rest of the cluster, or the next word. */
|
|
25
26
|
const COMMIT_VALUE_FLAGS = new Set(["m", "F", "C", "c", "t"]);
|
|
26
27
|
|
|
28
|
+
/** The parser for each guarded tool. */
|
|
29
|
+
const PARSERS = { Bash: segmentsOf, PowerShell: powershellSegments };
|
|
30
|
+
/** The shells whose `-c` or `-Command` argument is a command line of its own, and the parser that reads it. */
|
|
31
|
+
const NESTED = { bash: segmentsOf, sh: segmentsOf, pwsh: powershellSegments, powershell: powershellSegments };
|
|
32
|
+
|
|
27
33
|
const programName = (word) => path.basename(word ?? "").toLowerCase().replace(/\.exe$/, "");
|
|
28
34
|
const isNoVerify = (arg) => arg.length >= NO_VERIFY_MIN && NO_VERIFY.startsWith(arg);
|
|
29
35
|
|
|
@@ -69,6 +75,14 @@ function runsTests(segment) {
|
|
|
69
75
|
return [program, ...args].some((word) => programName(word) === "vitest");
|
|
70
76
|
}
|
|
71
77
|
|
|
78
|
+
/** @returns the command line a shell runs from `-c` or `-Command`, with its parser, or null. */
|
|
79
|
+
function nestedCommand(segment) {
|
|
80
|
+
const [program, ...args] = commandWords(segment);
|
|
81
|
+
const parse = NESTED[programName(program)];
|
|
82
|
+
const at = args.findIndex((arg) => /^-(c|command)$/i.test(arg));
|
|
83
|
+
return parse && at !== -1 && args[at + 1] !== undefined ? { command: args[at + 1], parse } : null;
|
|
84
|
+
}
|
|
85
|
+
|
|
72
86
|
const REASONS = {
|
|
73
87
|
hooks:
|
|
74
88
|
"the command skips the git hooks. The pre-commit and pre-push hooks are the gates: run the command " +
|
|
@@ -81,31 +95,34 @@ const REASONS = {
|
|
|
81
95
|
};
|
|
82
96
|
|
|
83
97
|
/** @returns the reasons the command breaks a rule, in a stable order. */
|
|
84
|
-
export function violations(command) {
|
|
85
|
-
const segments =
|
|
98
|
+
export function violations(command, parse = segmentsOf) {
|
|
99
|
+
const segments = parse(command);
|
|
86
100
|
const found = new Set();
|
|
87
101
|
for (const segment of segments) {
|
|
88
102
|
const git = gitCall(segment);
|
|
89
103
|
if (git && skipsHooks(git)) found.add("hooks");
|
|
90
104
|
if (git && setsHooksPath(git)) found.add("hooksPath");
|
|
105
|
+
const nested = nestedCommand(segment);
|
|
106
|
+
if (nested) for (const key of violations(nested.command, nested.parse)) found.add(key);
|
|
91
107
|
}
|
|
92
108
|
if (!segments.some(runsTests) && segments.some(runsHookScript)) found.add("script");
|
|
93
109
|
return Object.keys(REASONS).filter((key) => found.has(key));
|
|
94
110
|
}
|
|
95
111
|
|
|
96
|
-
/** The verdict on one Bash call. @returns the hook output, or null to let the call run with no message. */
|
|
112
|
+
/** The verdict on one Bash or PowerShell call. @returns the hook output, or null to let the call run with no message. */
|
|
97
113
|
export function decide(call) {
|
|
98
114
|
const command = call.tool_input?.command;
|
|
99
|
-
|
|
115
|
+
const parse = PARSERS[call.tool_name];
|
|
116
|
+
if (!parse || typeof command !== "string") return null;
|
|
100
117
|
const root = checkoutRootOf(call.cwd ?? process.cwd());
|
|
101
118
|
if (!root || !existsSync(path.join(root, CONFIG_FILE))) return null;
|
|
102
|
-
const found = violations(command);
|
|
119
|
+
const found = violations(command, parse);
|
|
103
120
|
if (found.length === 0) return null;
|
|
104
121
|
return {
|
|
105
122
|
hookSpecificOutput: {
|
|
106
123
|
hookEventName: "PreToolUse",
|
|
107
124
|
permissionDecision: "deny",
|
|
108
|
-
permissionDecisionReason:
|
|
125
|
+
permissionDecisionReason: `${call.tool_name} command guard: ${found.map((key) => REASONS[key]).join(" Also, ")}`,
|
|
109
126
|
},
|
|
110
127
|
};
|
|
111
128
|
}
|
|
@@ -94,3 +94,47 @@ test("the hook process prints the deny", (t) => {
|
|
|
94
94
|
assert.equal(result.status, 0, result.stderr);
|
|
95
95
|
assert.equal(JSON.parse(result.stdout).hookSpecificOutput.permissionDecision, "deny");
|
|
96
96
|
});
|
|
97
|
+
|
|
98
|
+
const powershell = (cwd, command) => ({ ...bash(cwd, command), tool_name: "PowerShell" });
|
|
99
|
+
|
|
100
|
+
test("a PowerShell command is held to the same rules as a Bash one", (t) => {
|
|
101
|
+
const ws = workspace(t);
|
|
102
|
+
const denied = {
|
|
103
|
+
"skips the git hooks": [
|
|
104
|
+
"git commit --no-verify -m x",
|
|
105
|
+
"git commit -nm x",
|
|
106
|
+
"git push --no-verify",
|
|
107
|
+
String.raw`Set-Location C:\repo; git commit -n -m x`,
|
|
108
|
+
"git --% commit --no-verify",
|
|
109
|
+
"& git.exe commit --no-verify -m x",
|
|
110
|
+
'pwsh -NoProfile -Command "git commit --no-verify -m x"',
|
|
111
|
+
"bash -c 'git push --no-verify'",
|
|
112
|
+
],
|
|
113
|
+
"core\.hooksPath": ["git -c core.hooksPath= commit -m x", "git -c core.hooksPath=NUL push"],
|
|
114
|
+
"writes a lease": [
|
|
115
|
+
String.raw`& node C:\kit\packages\qc-harness\src\cli\work-order-guard.mjs`,
|
|
116
|
+
String.raw`Get-Content input.json | node .\budget-guard.mjs`,
|
|
117
|
+
"bash hooks/pre-edit-guard.sh",
|
|
118
|
+
],
|
|
119
|
+
};
|
|
120
|
+
for (const [reason, commands] of Object.entries(denied)) {
|
|
121
|
+
for (const command of commands) {
|
|
122
|
+
assert.match(reasonOf(decide(powershell(ws.adopted, command))) ?? "", new RegExp(reason), command);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
for (const command of [
|
|
126
|
+
'git commit -m "-n is fine"',
|
|
127
|
+
"git log -n 5",
|
|
128
|
+
"git push -n",
|
|
129
|
+
String.raw`node --test packages\qc-harness\src\cli\work-order-guard.test.mjs`,
|
|
130
|
+
"Get-Content packages/qc-harness/src/cli/work-order-guard.mjs",
|
|
131
|
+
'pwsh -Command "git status"',
|
|
132
|
+
]) {
|
|
133
|
+
assert.equal(decide(powershell(ws.adopted, command)), null, command);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("the deny names the tool it guarded", (t) => {
|
|
138
|
+
const ws = workspace(t);
|
|
139
|
+
assert.match(reasonOf(decide(powershell(ws.adopted, "git push --no-verify"))), /^PowerShell command guard:/);
|
|
140
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync } 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 { defaults } from "../config.mjs";
|
|
8
|
+
import { branchNameRefusal, runWorkOrderCheck } from "./work-order-guard.mjs";
|
|
9
|
+
|
|
10
|
+
test("a branch names its issue: feat/340-roles passes and feat/back-action fails", () => {
|
|
11
|
+
assert.equal(branchNameRefusal("feat/340-roles", defaults.branches), null);
|
|
12
|
+
assert.match(branchNameRefusal("feat/back-action", defaults.branches), /names no issue/);
|
|
13
|
+
assert.match(branchNameRefusal("Feat/340-roles", defaults.branches), /names no issue/);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("main, a release branch and a detached HEAD pass, and a null pattern switches the rule off", () => {
|
|
17
|
+
for (const branch of ["main", "master", "release/1.2", ""]) assert.equal(branchNameRefusal(branch, defaults.branches), null, branch);
|
|
18
|
+
assert.equal(branchNameRefusal("feat/back-action", { ...defaults.branches, pattern: null }), null);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("qc work-order-check refuses a commit on a branch that names no issue", async (t) => {
|
|
22
|
+
const root = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-branch-name-")));
|
|
23
|
+
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
24
|
+
const git = (...args) => execFileSync("git", args, { cwd: root, stdio: "pipe" });
|
|
25
|
+
git("init", "-q", "-b", "feat/back-action");
|
|
26
|
+
writeFileSync(path.join(root, "qc.config.json"), "{}");
|
|
27
|
+
t.mock.method(console, "error", () => {});
|
|
28
|
+
assert.equal(await runWorkOrderCheck(root), 1);
|
|
29
|
+
git("checkout", "-q", "-b", "feat/340-roles");
|
|
30
|
+
assert.equal(await runWorkOrderCheck(root), 0);
|
|
31
|
+
});
|
package/src/cli/check.mjs
CHANGED
|
@@ -43,6 +43,7 @@ import { checkIntegrationImports } from "../gates/integration-imports.mjs";
|
|
|
43
43
|
import { checkClosedSetWriters, matchedKeys } from "../gates/closed-set-writers.mjs";
|
|
44
44
|
import { checkDocClaims, parseDocClaims } from "../gates/doc-claims.mjs";
|
|
45
45
|
import { ratchetInputs } from "./registry-history.mjs";
|
|
46
|
+
import { hookDrift } from "./git-hooks.mjs";
|
|
46
47
|
import { repoFiles } from "./repo-files.mjs";
|
|
47
48
|
|
|
48
49
|
// Each skip is tested on "/" + the relative path, so it reads a path the way it read a full one.
|
|
@@ -703,6 +704,10 @@ export async function runCheck(config, only = [], { lister = repoFiles } = {}) {
|
|
|
703
704
|
if (enabled(config.gates, "config-floor")) {
|
|
704
705
|
problems.push(...checkConfigFloor(defaults, config, { exemptions: config.floor.exemptions }));
|
|
705
706
|
lines.push("OK floor every check the kit ships on is in force, or names the decision that switched it off");
|
|
707
|
+
const hooks = hookDrift(config.root, config.hooks.required);
|
|
708
|
+
problems.push(...hooks.problems);
|
|
709
|
+
lines.push("OK hooks each git hook makes the calls hooks.required names");
|
|
710
|
+
for (const note of hooks.notes) lines.push(`NOTE hooks ${note}`);
|
|
706
711
|
}
|
|
707
712
|
|
|
708
713
|
if (enabled(config.gates, "english-source")) {
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// A commit message is conventional, English, and carries the attribution trailer when a team asks
|
|
2
|
+
// for one. The conventional rules are commitlint's, loaded from the repository's own node_modules,
|
|
3
|
+
// so the harness gains no runtime dependency and no rule set of its own.
|
|
4
|
+
|
|
5
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { pathToFileURL } from "node:url";
|
|
9
|
+
import { scriptPattern } from "../gates/english-source.mjs";
|
|
10
|
+
|
|
11
|
+
export const INSTALL_HINT = "pnpm add -D @commitlint/lint @commitlint/config-conventional";
|
|
12
|
+
const TRAILER = /^co-authored-by:\s*\S/im;
|
|
13
|
+
|
|
14
|
+
const importPath = async (file) => (await import(pathToFileURL(file).href)).default;
|
|
15
|
+
|
|
16
|
+
/** @returns the ESM entry of a package installed beside a file, which require.resolve cannot read from an import-only exports map. */
|
|
17
|
+
function importEntry(fromFile, name) {
|
|
18
|
+
for (let dir = path.dirname(fromFile); path.dirname(dir) !== dir; dir = path.dirname(dir)) {
|
|
19
|
+
const manifest = path.join(dir, "node_modules", name, "package.json");
|
|
20
|
+
if (!existsSync(manifest)) continue;
|
|
21
|
+
const pkg = JSON.parse(readFileSync(manifest, "utf8"));
|
|
22
|
+
let entry = pkg.exports?.["."] ?? pkg.exports ?? pkg.main ?? "index.js";
|
|
23
|
+
while (typeof entry === "object" && entry !== null) entry = entry.import ?? entry.default;
|
|
24
|
+
return path.join(path.dirname(manifest), entry);
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The parser options of the config's preset, so `feat!:` parses. Undefined keeps commitlint's default parser. */
|
|
30
|
+
async function parserOptions(configPath, preset) {
|
|
31
|
+
if (typeof preset !== "string") return preset?.parserOpts;
|
|
32
|
+
const entry = importEntry(configPath, preset);
|
|
33
|
+
if (!entry) return undefined;
|
|
34
|
+
const created = await (await importPath(entry))();
|
|
35
|
+
return created.parser ?? created.parserOpts;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @returns commitlint's lint function and the conventional config from the repository, or null when either is missing. */
|
|
39
|
+
export async function loadCommitlint(root) {
|
|
40
|
+
const require = createRequire(path.join(root, "package.json"));
|
|
41
|
+
let lintPath;
|
|
42
|
+
let configPath;
|
|
43
|
+
try {
|
|
44
|
+
lintPath = require.resolve("@commitlint/lint");
|
|
45
|
+
configPath = require.resolve("@commitlint/config-conventional");
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error.code === "MODULE_NOT_FOUND") return null;
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
const config = await importPath(configPath);
|
|
51
|
+
return { lint: await importPath(lintPath), rules: config.rules, parserOpts: await parserOptions(configPath, config.parserPreset) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {string} raw the message file as git wrote it
|
|
56
|
+
* @param {{conventional?: boolean, attribution?: boolean, scripts?: string[]}} options
|
|
57
|
+
* @param {{lint: Function, rules: object, parserOpts?: object} | null} commitlint
|
|
58
|
+
* @returns {Promise<{rule: string, detail: string}[]>}
|
|
59
|
+
*/
|
|
60
|
+
export async function checkCommitMessage(raw, options, commitlint) {
|
|
61
|
+
const message = raw
|
|
62
|
+
.split(/\r?\n/)
|
|
63
|
+
.filter((line) => !line.startsWith("#"))
|
|
64
|
+
.join("\n")
|
|
65
|
+
.trim();
|
|
66
|
+
const problems = [];
|
|
67
|
+
if (options.conventional !== false) {
|
|
68
|
+
if (commitlint) {
|
|
69
|
+
const report = await commitlint.lint(message, commitlint.rules, commitlint.parserOpts ? { parserOpts: commitlint.parserOpts } : {});
|
|
70
|
+
for (const error of report.errors) problems.push({ rule: `conventional/${error.name}`, detail: error.message });
|
|
71
|
+
} else {
|
|
72
|
+
problems.push({
|
|
73
|
+
rule: "commitlint-missing",
|
|
74
|
+
detail: `commitlint is not installed here. Run \`${INSTALL_HINT}\`, or set commitMessage.conventional to false.`,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const script = scriptPattern(options.scripts ?? ["cjk"]);
|
|
79
|
+
message.split("\n").forEach((line, index) => {
|
|
80
|
+
if (script.test(line)) problems.push({ rule: "english", detail: `line ${index + 1} is not English. A commit message is English.` });
|
|
81
|
+
});
|
|
82
|
+
if (options.attribution === true && !TRAILER.test(message)) {
|
|
83
|
+
problems.push({ rule: "attribution", detail: "the message has no Co-Authored-By trailer, and commitMessage.attribution requires one." });
|
|
84
|
+
}
|
|
85
|
+
return problems;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function runCommitMsg(config, file) {
|
|
89
|
+
const options = { ...config.commitMessage, scripts: config.language?.scripts };
|
|
90
|
+
const commitlint = options.conventional === false ? null : await loadCommitlint(config.root);
|
|
91
|
+
const problems = await checkCommitMessage(readFileSync(file, "utf8"), options, commitlint);
|
|
92
|
+
for (const problem of problems) console.error(`FAIL commit-msg ${problem.rule}: ${problem.detail}`);
|
|
93
|
+
return problems.length > 0 ? 1 : 0;
|
|
94
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { defaults } from "../config.mjs";
|
|
8
|
+
import { INSTALL_HINT, checkCommitMessage, loadCommitlint, runCommitMsg } from "./commit-msg.mjs";
|
|
9
|
+
|
|
10
|
+
// The harness package installs commitlint as a dev dependency, as an adopting repository would.
|
|
11
|
+
const HARNESS = fileURLToPath(new URL("../..", import.meta.url));
|
|
12
|
+
const TRAILER = "\n\nCo-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>\n";
|
|
13
|
+
|
|
14
|
+
function emptyDir(t) {
|
|
15
|
+
const dir = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-commit-msg-")));
|
|
16
|
+
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
17
|
+
return dir;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
test("a conventional subject passes, and a subject that is not one fails with commitlint's rule", async () => {
|
|
21
|
+
const commitlint = await loadCommitlint(HARNESS);
|
|
22
|
+
assert.deepEqual(await checkCommitMessage("feat(cli): add qc pr-check\n", {}, commitlint), []);
|
|
23
|
+
assert.deepEqual(await checkCommitMessage("feat(cli)!: drop the tools list\n", {}, commitlint), []);
|
|
24
|
+
const rules = (await checkCommitMessage("Added some stuff\n", {}, commitlint)).map((problem) => problem.rule);
|
|
25
|
+
assert.ok(rules.includes("conventional/type-empty"), rules.join());
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("a message that is not English fails, whatever commitlint says", async () => {
|
|
29
|
+
const commitlint = await loadCommitlint(HARNESS);
|
|
30
|
+
const problems = await checkCommitMessage("fix(ui): 写真を保存する\n", {}, commitlint);
|
|
31
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["english"]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("with no commitlint installed, the check fails with the install command, unless conventional is off", async (t) => {
|
|
35
|
+
const commitlint = await loadCommitlint(emptyDir(t));
|
|
36
|
+
assert.equal(commitlint, null);
|
|
37
|
+
const problems = await checkCommitMessage("feat: x\n", {}, commitlint);
|
|
38
|
+
assert.deepEqual(problems.map((problem) => problem.rule), ["commitlint-missing"]);
|
|
39
|
+
assert.ok(problems[0].detail.includes(INSTALL_HINT));
|
|
40
|
+
assert.deepEqual(await checkCommitMessage("feat: x\n", { conventional: false }, null), []);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("attribution is off by default, so a Claude trailer passes; on, a message without the trailer fails", async () => {
|
|
44
|
+
const commitlint = await loadCommitlint(HARNESS);
|
|
45
|
+
assert.equal(defaults.commitMessage.attribution, false);
|
|
46
|
+
assert.deepEqual(await checkCommitMessage(`feat: x${TRAILER}`, defaults.commitMessage, commitlint), []);
|
|
47
|
+
const on = { ...defaults.commitMessage, attribution: true };
|
|
48
|
+
assert.deepEqual((await checkCommitMessage("feat: x\n", on, commitlint)).map((problem) => problem.rule), ["attribution"]);
|
|
49
|
+
assert.deepEqual(await checkCommitMessage(`feat: x${TRAILER}`, on, commitlint), []);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("qc commit-msg reads the file git wrote, and ignores its comment lines", async (t) => {
|
|
53
|
+
const file = path.join(emptyDir(t), "COMMIT_EDITMSG");
|
|
54
|
+
writeFileSync(file, "fix(hooks): read the installed harness\n# Please enter the commit message\n");
|
|
55
|
+
const config = { root: HARNESS, commitMessage: defaults.commitMessage, language: {} };
|
|
56
|
+
assert.equal(await runCommitMsg(config, file), 0);
|
|
57
|
+
writeFileSync(file, "WIP\n");
|
|
58
|
+
t.mock.method(console, "error", () => {});
|
|
59
|
+
assert.equal(await runCommitMsg(config, file), 1);
|
|
60
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The trigger for `swarm.dispatch`. PreToolUse on Agent judges a dispatch and claims the implementer
|
|
3
|
+
// slot. SubagentStart claims it again for a resumed implementer, and SubagentStop frees it.
|
|
4
|
+
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { dispatchRefusal, dispatchSettingsAt, isImplementer, slotRefusal, typeOf } from "./dispatch.mjs";
|
|
9
|
+
import { claimSlot, reclaimSlot, releaseSlot, slotDirOf } from "./dispatch-slot.mjs";
|
|
10
|
+
|
|
11
|
+
const output = (fields) => ({ hookSpecificOutput: { hookEventName: "PreToolUse", ...fields } });
|
|
12
|
+
const deny = (reason) => output({ permissionDecision: "deny", permissionDecisionReason: reason });
|
|
13
|
+
const context = (text) => output({ additionalContext: text });
|
|
14
|
+
|
|
15
|
+
/** Claims or frees the slot as a subagent starts or stops. It reads no config, since the agent's cwd is its own. */
|
|
16
|
+
function onSubagent(call, dir, now) {
|
|
17
|
+
// Only a claim makes the folder, so a session with the guard off gets no file here.
|
|
18
|
+
if (!existsSync(dir)) return;
|
|
19
|
+
try {
|
|
20
|
+
if (call.hook_event_name === "SubagentStart") reclaimSlot(dir, call.agent_type, now);
|
|
21
|
+
else releaseSlot(dir, call.agent_type);
|
|
22
|
+
} catch {
|
|
23
|
+
// A busy or read-only temp folder is the hook's own problem, and the slot still expires.
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The verdict on one hook event. @returns the hook output, or null to let the call run with no message. */
|
|
28
|
+
export function decide(call, tmp = os.tmpdir(), now = Date.now()) {
|
|
29
|
+
const dir = slotDirOf(call.session_id ?? "session", tmp);
|
|
30
|
+
if (call.hook_event_name === "SubagentStart" || call.hook_event_name === "SubagentStop") {
|
|
31
|
+
onSubagent(call, dir, now);
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
if (call.hook_event_name !== "PreToolUse" || call.tool_name !== "Agent") return null;
|
|
35
|
+
// A subagent's dispatch follows its own definition, often another plugin's; the orchestrator is the main session.
|
|
36
|
+
if (typeof call.agent_id === "string" && call.agent_id !== "") return null;
|
|
37
|
+
|
|
38
|
+
let settings;
|
|
39
|
+
try {
|
|
40
|
+
settings = dispatchSettingsAt(call.cwd ?? process.cwd());
|
|
41
|
+
} catch (error) {
|
|
42
|
+
// A config typo must not stop every dispatch; the session sees the error and reports it.
|
|
43
|
+
return context(`Dispatch guard is off: ${error.message}`);
|
|
44
|
+
}
|
|
45
|
+
if (!settings) return null;
|
|
46
|
+
|
|
47
|
+
const input = call.tool_input ?? {};
|
|
48
|
+
const refusal = dispatchRefusal(input, settings);
|
|
49
|
+
if (refusal) return deny(refusal);
|
|
50
|
+
const type = typeOf(input);
|
|
51
|
+
if (!isImplementer(type, settings)) return null;
|
|
52
|
+
let holder;
|
|
53
|
+
try {
|
|
54
|
+
holder = claimSlot(dir, settings, now);
|
|
55
|
+
} catch {
|
|
56
|
+
// A busy or read-only temp folder is the hook's own problem, never a reason to refuse a dispatch.
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return holder ? deny(slotRefusal({ type, ...holder })) : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function readStdin() {
|
|
63
|
+
let data = "";
|
|
64
|
+
for await (const chunk of process.stdin) data += chunk;
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const isEntryPoint = process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1];
|
|
69
|
+
if (isEntryPoint) {
|
|
70
|
+
let call = null;
|
|
71
|
+
try {
|
|
72
|
+
call = JSON.parse(await readStdin());
|
|
73
|
+
} catch {
|
|
74
|
+
// Not hook input, so there is nothing to judge.
|
|
75
|
+
}
|
|
76
|
+
const result = call ? decide(call) : null;
|
|
77
|
+
if (result) process.stdout.write(JSON.stringify(result));
|
|
78
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, 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 } from "./dispatch-guard.mjs";
|
|
9
|
+
|
|
10
|
+
const GUARD = fileURLToPath(new URL("./dispatch-guard.mjs", import.meta.url));
|
|
11
|
+
const HOOKS = fileURLToPath(new URL("../../../../hooks/hooks.json", import.meta.url));
|
|
12
|
+
|
|
13
|
+
/** A checkout that turns the guard on, one whose swarm section has no dispatch key, one with no config, and a temp folder. */
|
|
14
|
+
function workspace(t, dispatch = { maxPromptChars: 80 }) {
|
|
15
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-dispatch-")));
|
|
16
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
17
|
+
const on = path.join(base, "on");
|
|
18
|
+
const off = path.join(base, "off");
|
|
19
|
+
const plain = path.join(base, "plain");
|
|
20
|
+
const tmp = path.join(base, "tmp");
|
|
21
|
+
for (const dir of [on, off, plain]) {
|
|
22
|
+
mkdirSync(dir);
|
|
23
|
+
execFileSync("git", ["init", "-q"], { cwd: dir, stdio: "pipe" });
|
|
24
|
+
}
|
|
25
|
+
mkdirSync(tmp);
|
|
26
|
+
mkdirSync(path.join(on, "src"));
|
|
27
|
+
writeFileSync(path.join(on, "qc.config.json"), JSON.stringify({ swarm: { dispatch } }));
|
|
28
|
+
writeFileSync(path.join(off, "qc.config.json"), JSON.stringify({ swarm: { toolCallBudget: 50 } }));
|
|
29
|
+
return { on, off, plain, tmp, slots: path.join(tmp, "architecture-kit", "dispatch") };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const agentCall = (cwd, tool_input, extra = {}) => ({
|
|
33
|
+
session_id: "session-aaaa",
|
|
34
|
+
cwd,
|
|
35
|
+
hook_event_name: "PreToolUse",
|
|
36
|
+
tool_name: "Agent",
|
|
37
|
+
tool_input,
|
|
38
|
+
...extra,
|
|
39
|
+
});
|
|
40
|
+
const subagentEvent = (cwd, agent_type, hook_event_name = "SubagentStop") => ({
|
|
41
|
+
session_id: "session-aaaa",
|
|
42
|
+
cwd,
|
|
43
|
+
hook_event_name,
|
|
44
|
+
agent_id: "agent-1",
|
|
45
|
+
agent_type,
|
|
46
|
+
});
|
|
47
|
+
const reasonOf = (output) =>
|
|
48
|
+
output?.hookSpecificOutput?.permissionDecision === "deny" ? output.hookSpecificOutput.permissionDecisionReason : null;
|
|
49
|
+
const implementer = {
|
|
50
|
+
subagent_type: "architecture:sdd-implementer",
|
|
51
|
+
description: "Task 1",
|
|
52
|
+
prompt: "Read the brief at plans/task-1-brief.md.",
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
test("a checkout with no config, or a swarm section with no dispatch key, sees no change", (t) => {
|
|
56
|
+
const ws = workspace(t);
|
|
57
|
+
for (const cwd of [ws.off, ws.plain]) {
|
|
58
|
+
assert.equal(decide(agentCall(cwd, { prompt: "x".repeat(500) }), ws.tmp), null);
|
|
59
|
+
assert.equal(decide(agentCall(cwd, implementer), ws.tmp), null);
|
|
60
|
+
assert.equal(decide(agentCall(cwd, implementer), ws.tmp), null);
|
|
61
|
+
assert.equal(decide(subagentEvent(cwd, "architecture:sdd-implementer"), ws.tmp), null);
|
|
62
|
+
}
|
|
63
|
+
assert.equal(existsSync(ws.slots), false);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("the main session's dispatch with no model is refused, and one with a model passes", (t) => {
|
|
67
|
+
const ws = workspace(t);
|
|
68
|
+
assert.match(reasonOf(decide(agentCall(ws.on, { prompt: "p" }), ws.tmp)) ?? "", /names no model/);
|
|
69
|
+
assert.equal(decide(agentCall(ws.on, { prompt: "p", model: "sonnet" }), ws.tmp), null);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("a subagent's dispatch is left to its own definition", (t) => {
|
|
73
|
+
const ws = workspace(t);
|
|
74
|
+
const extra = { agent_id: "agent-9", agent_type: "claude-security:patch-generator" };
|
|
75
|
+
assert.equal(decide(agentCall(ws.on, { subagent_type: "claude-security:explore", prompt: "p" }, extra), ws.tmp), null);
|
|
76
|
+
assert.equal(decide(agentCall(ws.on, implementer, extra), ws.tmp), null);
|
|
77
|
+
assert.equal(existsSync(ws.slots), false);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("the config of the checkout that holds the cwd decides", (t) => {
|
|
81
|
+
const ws = workspace(t);
|
|
82
|
+
assert.match(reasonOf(decide(agentCall(path.join(ws.on, "src"), { prompt: "p" }), ws.tmp)) ?? "", /names no model/);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("a prompt over the limit is refused, and the refusal claims no slot", (t) => {
|
|
86
|
+
const ws = workspace(t);
|
|
87
|
+
const long = { ...implementer, prompt: "x".repeat(81) };
|
|
88
|
+
assert.match(reasonOf(decide(agentCall(ws.on, long), ws.tmp)) ?? "", /81 characters/);
|
|
89
|
+
assert.equal(existsSync(ws.slots), false);
|
|
90
|
+
assert.equal(decide(agentCall(ws.on, implementer), ws.tmp), null);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("a second implementer is refused until the first one stops, and the refusal names the slot file", (t) => {
|
|
94
|
+
const ws = workspace(t);
|
|
95
|
+
assert.equal(decide(agentCall(ws.on, implementer), ws.tmp), null);
|
|
96
|
+
const second = reasonOf(decide(agentCall(ws.on, { ...implementer, subagent_type: "sdd-implementer" }), ws.tmp)) ?? "";
|
|
97
|
+
assert.match(second, /holds the one implementer slot/);
|
|
98
|
+
assert.ok(second.includes(ws.slots), second);
|
|
99
|
+
decide(subagentEvent(ws.on, ""), ws.tmp);
|
|
100
|
+
decide(subagentEvent(ws.on, "architecture:sdd-reviewer"), ws.tmp);
|
|
101
|
+
assert.notEqual(reasonOf(decide(agentCall(ws.on, implementer), ws.tmp)), null);
|
|
102
|
+
decide(subagentEvent(ws.on, "architecture:sdd-implementer"), ws.tmp);
|
|
103
|
+
assert.equal(decide(agentCall(ws.on, implementer), ws.tmp), null);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("a stop in a worktree with no config still frees the slot", (t) => {
|
|
107
|
+
const ws = workspace(t);
|
|
108
|
+
decide(agentCall(ws.on, implementer), ws.tmp);
|
|
109
|
+
decide(subagentEvent(ws.plain, "architecture:sdd-implementer"), ws.tmp);
|
|
110
|
+
assert.equal(decide(agentCall(ws.on, implementer), ws.tmp), null);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("a planner and a reviewer run beside an implementer", (t) => {
|
|
114
|
+
const ws = workspace(t);
|
|
115
|
+
decide(agentCall(ws.on, implementer), ws.tmp);
|
|
116
|
+
assert.equal(decide(agentCall(ws.on, { subagent_type: "architecture:sdd-reviewer", prompt: "p" }), ws.tmp), null);
|
|
117
|
+
assert.equal(decide(agentCall(ws.on, { subagent_type: "architecture:sdd-planner", prompt: "p" }), ws.tmp), null);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("a slot with no stop expires after slotMinutes", (t) => {
|
|
121
|
+
const ws = workspace(t);
|
|
122
|
+
decide(agentCall(ws.on, implementer), ws.tmp);
|
|
123
|
+
assert.notEqual(reasonOf(decide(agentCall(ws.on, implementer), ws.tmp)), null);
|
|
124
|
+
assert.equal(decide(agentCall(ws.on, implementer), ws.tmp, Date.now() + 61 * 60_000), null);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("a resumed implementer holds the slot again", (t) => {
|
|
128
|
+
const ws = workspace(t);
|
|
129
|
+
decide(agentCall(ws.on, implementer), ws.tmp);
|
|
130
|
+
decide(subagentEvent(ws.on, "architecture:sdd-implementer"), ws.tmp);
|
|
131
|
+
decide(subagentEvent(ws.on, "architecture:sdd-implementer", "SubagentStart"), ws.tmp);
|
|
132
|
+
assert.match(reasonOf(decide(agentCall(ws.on, implementer), ws.tmp)) ?? "", /holds the one implementer slot/);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("a config error reaches the session as context and never refuses", (t) => {
|
|
136
|
+
const ws = workspace(t, { slotMinutes: "soon" });
|
|
137
|
+
const output = decide(agentCall(ws.on, { prompt: "p" }), ws.tmp);
|
|
138
|
+
assert.equal(output.hookSpecificOutput.permissionDecision, undefined);
|
|
139
|
+
assert.match(output.hookSpecificOutput.additionalContext, /Dispatch guard is off: swarm\.dispatch\.slotMinutes/);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("the hook runs as its own process and prints its refusal as JSON", (t) => {
|
|
143
|
+
const ws = workspace(t);
|
|
144
|
+
const result = spawnSync(process.execPath, [GUARD], {
|
|
145
|
+
input: JSON.stringify(agentCall(ws.on, { prompt: "p" })),
|
|
146
|
+
env: { ...process.env, TEMP: ws.tmp, TMP: ws.tmp, TMPDIR: ws.tmp },
|
|
147
|
+
encoding: "utf8",
|
|
148
|
+
});
|
|
149
|
+
assert.equal(result.status, 0, result.stderr);
|
|
150
|
+
assert.match(reasonOf(JSON.parse(result.stdout)) ?? "", /names no model/);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("hooks.json runs the guard on Agent, SubagentStart and SubagentStop", () => {
|
|
154
|
+
const { hooks } = JSON.parse(readFileSync(HOOKS, "utf8"));
|
|
155
|
+
const matchersOf = (entries) =>
|
|
156
|
+
(entries ?? []).filter((entry) => entry.hooks.some((hook) => hook.command.includes("dispatch-guard.mjs"))).map((entry) => entry.matcher);
|
|
157
|
+
assert.deepEqual(matchersOf(hooks.PreToolUse), ["Agent"]);
|
|
158
|
+
assert.deepEqual(matchersOf(hooks.SubagentStart), [undefined]);
|
|
159
|
+
assert.deepEqual(matchersOf(hooks.SubagentStop), [undefined]);
|
|
160
|
+
});
|