architecture-harness 0.23.1 → 0.25.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 +3 -1
- package/src/cli/budget-guard.mjs +1 -0
- package/src/cli/budget-guard.test.mjs +7 -1
- package/src/cli/budget.mjs +3 -4
- package/src/cli/budget.test.mjs +5 -5
- package/src/cli/ci-template.test.mjs +17 -27
- package/src/cli/cli.test.mjs +22 -2
- package/src/cli/commit-msg.mjs +7 -10
- package/src/cli/commit-msg.test.mjs +10 -7
- package/src/cli/generated-file-guard.mjs +1 -1
- package/src/cli/generated-file-guard.test.mjs +4 -2
- package/src/cli/init.mjs +70 -25
- package/src/cli/lint-changed.mjs +157 -0
- package/src/cli/lint-changed.test.mjs +102 -0
- package/src/cli/plugin-agents.test.mjs +1 -1
- package/src/cli/post-edit-gate.test.mjs +3 -1
- package/src/cli/qc.mjs +23 -3
- package/src/cli/scaffold.mjs +23 -2
- package/src/cli/scaffold.test.mjs +51 -0
- package/src/cli/stop-gate.test.mjs +32 -128
- package/src/cli/typecheck.mjs +46 -0
- package/src/cli/typecheck.test.mjs +43 -0
- package/src/cli/user-config-package.mjs +16 -0
- package/src/cli/user-config-toml.mjs +144 -0
- package/src/cli/user-config.mjs +155 -0
- package/src/cli/user-config.test.mjs +274 -0
- package/src/cli/worktree-gate.mjs +11 -12
- package/src/cli/worktree-gate.test.mjs +28 -29
- package/src/cli/worktree-isolation.mjs +3 -3
- package/src/cli/worktree-isolation.test.mjs +1 -1
- package/src/cli/worktree.mjs +9 -1
- package/src/cli/worktree.test.mjs +16 -1
- package/src/config.mjs +13 -11
- package/src/eslint/preset.mjs +8 -3
- package/src/eslint/preset.test.mjs +15 -8
- package/src/vale-style.test.mjs +5 -5
- package/templates/CLAUDE.md +2 -2
- package/templates/githooks/pre-commit +7 -10
- package/templates/github/workflows/ci.yml +22 -89
- package/templates/github/workflows/pr-policy.yml +36 -0
- package/user-config/claude/settings.shared.json +74 -0
- package/user-config/claude/statusline.js +323 -0
- package/user-config/codex/config.shared.toml +19 -0
- package/src/cli/edit-batch-guard.mjs +0 -77
- package/src/cli/edit-batch-guard.test.mjs +0 -131
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { test } from "node:test";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { onChangedLines, parseDiff } from "./lint-changed.mjs";
|
|
10
|
+
|
|
11
|
+
const QC = path.join(path.dirname(fileURLToPath(import.meta.url)), "qc.mjs");
|
|
12
|
+
|
|
13
|
+
test("a diff gives the new-side lines of each hunk, and a deleted file is absent", () => {
|
|
14
|
+
const diff = [
|
|
15
|
+
"diff --git a/src/a.ts b/src/a.ts",
|
|
16
|
+
"--- a/src/a.ts",
|
|
17
|
+
"+++ b/src/a.ts",
|
|
18
|
+
"@@ -3 +3 @@ x",
|
|
19
|
+
"@@ -10,0 +11,2 @@ y",
|
|
20
|
+
"@@ -20,2 +22,0 @@ z",
|
|
21
|
+
"diff --git a/src/gone.ts b/src/gone.ts",
|
|
22
|
+
"--- a/src/gone.ts",
|
|
23
|
+
"+++ /dev/null",
|
|
24
|
+
"@@ -1,4 +0,0 @@",
|
|
25
|
+
].join("\n");
|
|
26
|
+
const changed = parseDiff(diff);
|
|
27
|
+
assert.deepEqual([...changed.keys()], ["src/a.ts"]);
|
|
28
|
+
assert.deepEqual([...changed.get("src/a.ts")], [3, 11, 12]);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("only a problem on a changed line counts; a fatal one and a new file count whole", () => {
|
|
32
|
+
const root = path.resolve("/repo");
|
|
33
|
+
const results = [
|
|
34
|
+
{ filePath: path.join(root, "src", "a.ts"), messages: [{ line: 3, severity: 2 }, { line: 4, severity: 2 }, { fatal: true, severity: 2, line: 9 }] },
|
|
35
|
+
{ filePath: path.join(root, "src", "new.ts"), messages: [{ line: 40, severity: 1 }] },
|
|
36
|
+
];
|
|
37
|
+
const changed = new Map([["src/a.ts", new Set([3])], ["src/new.ts", "all"]]);
|
|
38
|
+
const { kept, skipped } = onChangedLines(results, changed, root);
|
|
39
|
+
assert.deepEqual(kept.map(({ file, message }) => `${file}:${message.line}`), ["src/a.ts:3", "src/a.ts:9", "src/new.ts:40"]);
|
|
40
|
+
assert.equal(skipped, 1);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** A git repository with a real ESLint, linked from the kit's own install, and one rule: no-var. */
|
|
44
|
+
function repo(t) {
|
|
45
|
+
const root = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-lint-")));
|
|
46
|
+
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
47
|
+
const eslintDir = path.dirname(createRequire(import.meta.url).resolve("eslint/package.json"));
|
|
48
|
+
mkdirSync(path.join(root, "node_modules"));
|
|
49
|
+
symlinkSync(eslintDir, path.join(root, "node_modules", "eslint"), "junction");
|
|
50
|
+
writeFileSync(path.join(root, "package.json"), '{ "type": "module" }\n');
|
|
51
|
+
writeFileSync(path.join(root, ".gitignore"), "node_modules/\n");
|
|
52
|
+
writeFileSync(path.join(root, "qc.config.json"), '{ "featureRoots": [] }\n');
|
|
53
|
+
writeFileSync(path.join(root, "eslint.config.mjs"), 'export default [{ files: ["**/*.mjs"], rules: { "no-var": "error", "no-console": "warn" } }];\n');
|
|
54
|
+
mkdirSync(path.join(root, "src"));
|
|
55
|
+
writeFileSync(path.join(root, "src", "old.mjs"), "var legacy = 1;\nexport const kept = legacy;\n");
|
|
56
|
+
const git = (...args) => spawnSync("git", args, { cwd: root, encoding: "utf8" });
|
|
57
|
+
git("init", "-q", "-b", "main");
|
|
58
|
+
git("-c", "user.email=t@example.com", "-c", "user.name=t", "add", "-A");
|
|
59
|
+
git("-c", "user.email=t@example.com", "-c", "user.name=t", "commit", "-q", "--no-verify", "-m", "init");
|
|
60
|
+
const qc = (...args) => spawnSync(process.execPath, [QC, "lint", ...args], { cwd: root, encoding: "utf8" });
|
|
61
|
+
return { root, git, qc };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
test("an older problem in a file the change edits is left alone, and the change passes", (t) => {
|
|
65
|
+
const { root, qc } = repo(t);
|
|
66
|
+
writeFileSync(path.join(root, "src", "old.mjs"), "var legacy = 1;\nexport const kept = legacy;\nexport const added = 2;\n");
|
|
67
|
+
const result = qc();
|
|
68
|
+
assert.equal(result.status, 0, result.stderr);
|
|
69
|
+
assert.match(result.stderr, /1 older problem\(s\) on lines this change leaves alone/);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("a problem on a changed line fails, and a warning there prints but passes", (t) => {
|
|
73
|
+
const { root, qc } = repo(t);
|
|
74
|
+
writeFileSync(path.join(root, "src", "old.mjs"), "var legacy = 1;\nexport const kept = legacy;\nvar added = 2;\nexport { added };\n");
|
|
75
|
+
const failed = qc();
|
|
76
|
+
assert.equal(failed.status, 1);
|
|
77
|
+
assert.match(failed.stderr, /src\/old\.mjs:3:1 {2}error .*no-var/);
|
|
78
|
+
writeFileSync(path.join(root, "src", "old.mjs"), "var legacy = 1;\nexport const kept = legacy;\nconsole.log(kept);\n");
|
|
79
|
+
const warned = qc();
|
|
80
|
+
assert.equal(warned.status, 0, warned.stderr);
|
|
81
|
+
assert.match(warned.stderr, /0 error\(s\) and 1 warning\(s\)/);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("an untracked file counts whole, and --staged judges what the index holds", (t) => {
|
|
85
|
+
const { root, git, qc } = repo(t);
|
|
86
|
+
writeFileSync(path.join(root, "src", "new.mjs"), "var fresh = 1;\nexport { fresh };\n");
|
|
87
|
+
assert.equal(qc().status, 1);
|
|
88
|
+
assert.equal(qc("--staged").status, 0, "nothing is staged yet");
|
|
89
|
+
git("add", "src/new.mjs");
|
|
90
|
+
writeFileSync(path.join(root, "src", "new.mjs"), "const fresh = 1;\nexport { fresh };\n");
|
|
91
|
+
const staged = qc("--staged");
|
|
92
|
+
assert.equal(staged.status, 1, "the index still holds the var");
|
|
93
|
+
assert.match(staged.stderr, /src\/new\.mjs:1:1 {2}error/);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("a repository without ESLint skips the lint", (t) => {
|
|
97
|
+
const { root, qc } = repo(t);
|
|
98
|
+
rmSync(path.join(root, "node_modules"), { recursive: true, force: true });
|
|
99
|
+
const result = qc();
|
|
100
|
+
assert.equal(result.status, 0);
|
|
101
|
+
assert.match(result.stdout, /lint skipped/);
|
|
102
|
+
});
|
|
@@ -8,7 +8,7 @@ import { REMINDER } from "./dispatch-reminder.mjs";
|
|
|
8
8
|
import { dispatchRefusal, dispatchSettings } from "./dispatch.mjs";
|
|
9
9
|
|
|
10
10
|
const AGENTS = fileURLToPath(new URL("../../../../agents/", import.meta.url));
|
|
11
|
-
const README = fileURLToPath(new URL("../../../../
|
|
11
|
+
const README = fileURLToPath(new URL("../../../../docs/reference.md", import.meta.url));
|
|
12
12
|
const PLUGIN = "architecture";
|
|
13
13
|
// The built-in tools a background subagent keeps, as the Claude Code sub-agents page lists them.
|
|
14
14
|
// Claude Code drops any other listed name without an error, so such a name is dead weight.
|
|
@@ -31,7 +31,8 @@ function workspace() {
|
|
|
31
31
|
path.join(cli, "qc.mjs"),
|
|
32
32
|
`import { appendFileSync } from "node:fs";\nappendFileSync(${JSON.stringify(log)}, process.cwd() + String.fromCharCode(10));\nprocess.exit(1);\n`,
|
|
33
33
|
);
|
|
34
|
-
|
|
34
|
+
// The hook calls the CLI twice, for lint and for check; each folder it ran in counts once.
|
|
35
|
+
const ranIn = () => (existsSync(log) ? [...new Set(readFileSync(log, "utf8").trim().split("\n").map((dir) => realpathSync.native(dir)))] : []);
|
|
35
36
|
return { project, linked, plain, outside, pluginRoot: path.join(base, "plugin"), ranIn, cleanup: () => rmSync(base, { recursive: true, force: true }) };
|
|
36
37
|
}
|
|
37
38
|
|
|
@@ -87,6 +88,7 @@ test("a checkout with its own installed harness is checked by that copy, not the
|
|
|
87
88
|
const cli = path.join(ws.linked, "node_modules", "architecture-harness", "src", "cli");
|
|
88
89
|
mkdirSync(cli, { recursive: true });
|
|
89
90
|
writeFileSync(path.join(cli, "qc.mjs"), "process.exit(0);\n");
|
|
91
|
+
writeFileSync(path.join(cli, "lint-changed.mjs"), "");
|
|
90
92
|
assert.equal(runHook(ws, path.join(ws.linked, "backend", "src")), 0);
|
|
91
93
|
assert.deepEqual(ws.ranIn(), []);
|
|
92
94
|
});
|
package/src/cli/qc.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// The one entrypoint. A hook, a git hook and CI all call this, so they cannot drift.
|
|
3
3
|
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
4
6
|
import process from "node:process";
|
|
5
7
|
import { load } from "../config.mjs";
|
|
6
8
|
import { runCheck } from "./check.mjs";
|
|
@@ -10,8 +12,13 @@ const USAGE = `qc — architecture gates
|
|
|
10
12
|
|
|
11
13
|
qc check [file...] [--files-from <path|->]
|
|
12
14
|
every structural gate; files given, or listed NUL-separated in a file or on stdin, take the fast path
|
|
13
|
-
qc
|
|
14
|
-
|
|
15
|
+
qc lint [--staged] [--base <ref>] [file...]
|
|
16
|
+
ESLint on the changed lines only: a problem on an untouched line never fails
|
|
17
|
+
qc typecheck the typecheck script, else tsc; writes no build output
|
|
18
|
+
qc init [--full] scaffold the config, hooks and workflow; --full adds the reference architecture
|
|
19
|
+
qc user-config apply|check|capture [--dry-run]
|
|
20
|
+
synchronize the shared Claude and Codex user settings
|
|
21
|
+
qc feature <name> scaffold a feature of the reference architecture; needs qc init --full
|
|
15
22
|
qc run [name] drive one feature command headless; no name lists them
|
|
16
23
|
qc install-hooks point git at the kit's pre-commit hook
|
|
17
24
|
qc work-order-check refuse to commit on the wrong branch, per .claude/work-order.local.json
|
|
@@ -59,6 +66,10 @@ Every gate reads qc.config.json. A key it does not set keeps the reference defau
|
|
|
59
66
|
|
|
60
67
|
async function main() {
|
|
61
68
|
const [command, ...rest] = process.argv.slice(2);
|
|
69
|
+
if (command === "user-config") {
|
|
70
|
+
const { runUserConfig } = await import("./user-config.mjs");
|
|
71
|
+
process.exit(await runUserConfig(rest));
|
|
72
|
+
}
|
|
62
73
|
if (command === "stack") {
|
|
63
74
|
const { runStack } = await import("./stack.mjs");
|
|
64
75
|
process.exit(await runStack(rest));
|
|
@@ -108,7 +119,8 @@ async function main() {
|
|
|
108
119
|
for (const problem of problems) {
|
|
109
120
|
console.error(`FAIL ${problem.feature ?? problem.path} ${problem.rule}: ${problem.detail}`);
|
|
110
121
|
}
|
|
111
|
-
|
|
122
|
+
const guide = existsSync(path.join(config.root, config.docs.enforcement)) ? ` ${config.docs.enforcement}` : "";
|
|
123
|
+
console.error(`\n${problems.length} problem(s).${guide}`);
|
|
112
124
|
process.exit(1);
|
|
113
125
|
}
|
|
114
126
|
for (const line of lines) console.log(line);
|
|
@@ -116,6 +128,14 @@ async function main() {
|
|
|
116
128
|
process.exitCode = await runCheckCommands(config);
|
|
117
129
|
return;
|
|
118
130
|
}
|
|
131
|
+
case "lint": {
|
|
132
|
+
const { runLintChanged } = await import("./lint-changed.mjs");
|
|
133
|
+
process.exit(await runLintChanged(config, rest));
|
|
134
|
+
}
|
|
135
|
+
case "typecheck": {
|
|
136
|
+
const { runTypecheck } = await import("./typecheck.mjs");
|
|
137
|
+
process.exit(runTypecheck(config));
|
|
138
|
+
}
|
|
119
139
|
case "init": {
|
|
120
140
|
const { runInit } = await import("./init.mjs");
|
|
121
141
|
await runInit(config, rest);
|
package/src/cli/scaffold.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// The emitted files import their runtime from `kernelModule` in qc.config.json, never
|
|
5
5
|
// from this package: the kit prescribes the shape and ships no runtime to depend on.
|
|
6
6
|
|
|
7
|
-
import { mkdir, writeFile } from "node:fs/promises";
|
|
7
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
8
8
|
import { existsSync } from "node:fs";
|
|
9
9
|
import path from "node:path";
|
|
10
10
|
|
|
@@ -150,6 +150,16 @@ test("the pipeline is declared headlessly", () => {
|
|
|
150
150
|
};
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
/** Adds `root` to featureRoots in qc.config.json, so the anatomy gate sweeps the new feature. */
|
|
154
|
+
async function addFeatureRoot(config, root) {
|
|
155
|
+
const file = path.join(config.root, "qc.config.json");
|
|
156
|
+
if (config.featureRoots.includes(root) || !existsSync(file)) return;
|
|
157
|
+
const qcConfig = JSON.parse(await readFile(file, "utf8"));
|
|
158
|
+
qcConfig.featureRoots = [...(qcConfig.featureRoots ?? config.featureRoots), root];
|
|
159
|
+
await writeFile(file, `${JSON.stringify(qcConfig, null, 2)}\n`);
|
|
160
|
+
console.log(`added ${root} to featureRoots in qc.config.json`);
|
|
161
|
+
}
|
|
162
|
+
|
|
153
163
|
export async function runScaffold(config, args = []) {
|
|
154
164
|
const name = args.find((arg) => !arg.startsWith("-"));
|
|
155
165
|
if (!name || !NAME.test(name)) {
|
|
@@ -159,9 +169,19 @@ export async function runScaffold(config, args = []) {
|
|
|
159
169
|
return;
|
|
160
170
|
}
|
|
161
171
|
|
|
172
|
+
// The emitted files cite the reference architecture, so a repository without it fails its citations gate.
|
|
173
|
+
const architecture = path.posix.join(config.docs.root, "architecture.md");
|
|
174
|
+
if (!existsSync(path.join(config.root, architecture))) {
|
|
175
|
+
console.error(`qc feature writes the reference architecture, and its files cite ${architecture}.`);
|
|
176
|
+
console.error("Run `npx qc init --full` first.");
|
|
177
|
+
process.exitCode = 1;
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
162
181
|
const flat = args.includes("--flat");
|
|
163
182
|
const rootArg = args[args.indexOf("--root") + 1];
|
|
164
|
-
const
|
|
183
|
+
const given = args.includes("--root") && rootArg ? rootArg : config.paths.serverFeatures;
|
|
184
|
+
const root = path.relative(config.root, path.resolve(config.root, given)).split(path.sep).join("/");
|
|
165
185
|
const dir = path.join(config.root, root, name);
|
|
166
186
|
|
|
167
187
|
if (existsSync(dir)) {
|
|
@@ -188,6 +208,7 @@ export async function runScaffold(config, args = []) {
|
|
|
188
208
|
await writeFile(target, contents);
|
|
189
209
|
console.log(`written ${path.relative(config.root, target)}`);
|
|
190
210
|
}
|
|
211
|
+
await addFeatureRoot(config, root);
|
|
191
212
|
console.log(`
|
|
192
213
|
${flat ? "Flat pipeline" : "Bounded vertical slice"} anatomy. An empty block is a review finding:
|
|
193
214
|
fill each file or delete the ones this feature does not need, then \`qc check\`.`);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { load } from "../config.mjs";
|
|
7
|
+
import { runScaffold } from "./scaffold.mjs";
|
|
8
|
+
|
|
9
|
+
function repo(withArchitecture) {
|
|
10
|
+
const dir = realpathSync.native(mkdtempSync(path.join(tmpdir(), "qc-scaffold-")));
|
|
11
|
+
writeFileSync(path.join(dir, "qc.config.json"), `${JSON.stringify({ featureRoots: [] })}\n`);
|
|
12
|
+
if (withArchitecture) {
|
|
13
|
+
mkdirSync(path.join(dir, "docs"));
|
|
14
|
+
writeFileSync(path.join(dir, "docs", "architecture.md"), "# Architecture\n");
|
|
15
|
+
}
|
|
16
|
+
return dir;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function scaffold(dir, args) {
|
|
20
|
+
const { log, error } = console;
|
|
21
|
+
console.log = console.error = () => {};
|
|
22
|
+
try {
|
|
23
|
+
await runScaffold(load(dir), args);
|
|
24
|
+
return process.exitCode;
|
|
25
|
+
} finally {
|
|
26
|
+
Object.assign(console, { log, error });
|
|
27
|
+
process.exitCode = undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test("without the reference architecture doc, qc feature refuses and writes nothing", async () => {
|
|
32
|
+
const dir = repo(false);
|
|
33
|
+
try {
|
|
34
|
+
assert.equal(await scaffold(dir, ["orders"]), 1);
|
|
35
|
+
assert.equal(existsSync(path.join(dir, "backend")), false);
|
|
36
|
+
} finally {
|
|
37
|
+
rmSync(dir, { recursive: true, force: true });
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("qc feature adds its root to featureRoots once, so the anatomy gate sweeps the new feature", async () => {
|
|
42
|
+
const dir = repo(true);
|
|
43
|
+
try {
|
|
44
|
+
assert.equal(await scaffold(dir, ["orders"]), undefined);
|
|
45
|
+
assert.equal(await scaffold(dir, ["invoices", "--root", "./backend/src/features/"]), undefined);
|
|
46
|
+
assert.ok(existsSync(path.join(dir, "backend", "src", "features", "orders", "schema.ts")));
|
|
47
|
+
assert.deepEqual(JSON.parse(readFileSync(path.join(dir, "qc.config.json"), "utf8")).featureRoots, ["backend/src/features"]);
|
|
48
|
+
} finally {
|
|
49
|
+
rmSync(dir, { recursive: true, force: true });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
@@ -43,7 +43,10 @@ function workspace(t, installed) {
|
|
|
43
43
|
const cli = path.join(repo, "node_modules", "architecture-harness", "src", "cli");
|
|
44
44
|
stubHarness(cli, "installed", log, installed.codes, installed.withDoctor, installed.outputs);
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
// The typecheck and the lint run on every stop; `ran` lists the gates that judge the structure, `tools` the other two.
|
|
47
|
+
const lines = () => (existsSync(log) ? readFileSync(log, "utf8").trim().split("\n") : []);
|
|
48
|
+
const ran = () => lines().filter((line) => !/ (?:typecheck|lint)$/.test(line));
|
|
49
|
+
const tools = () => lines().filter((line) => / (?:typecheck|lint)$/.test(line));
|
|
47
50
|
// Each workspace owns its temp folder, so the gate's memory of a block is shared with no other test.
|
|
48
51
|
const tmp = path.join(base, "tmp");
|
|
49
52
|
mkdirSync(tmp, { recursive: true });
|
|
@@ -62,7 +65,7 @@ function workspace(t, installed) {
|
|
|
62
65
|
},
|
|
63
66
|
encoding: "utf8",
|
|
64
67
|
});
|
|
65
|
-
return { ran, run, tmpDir: tmp, installedCli: installed ? path.join(repo, "node_modules", "architecture-harness", "src", "cli") : null };
|
|
68
|
+
return { ran, tools, run, tmpDir: tmp, installedCli: installed ? path.join(repo, "node_modules", "architecture-harness", "src", "cli") : null };
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
test("an installed harness newer than the bundled one judges the repository, and the stop passes", (t) => {
|
|
@@ -80,43 +83,35 @@ test("with no installed harness, the bundled copy judges the repository", (t) =>
|
|
|
80
83
|
assert.match(result.stderr, /FAIL {2}check from plugin/);
|
|
81
84
|
});
|
|
82
85
|
|
|
83
|
-
test("drift alone
|
|
86
|
+
test("drift alone never blocks: the stop passes with a note that names the setup", (t) => {
|
|
84
87
|
const ws = workspace(t, { codes: { doctor: 1 }, withDoctor: true });
|
|
85
88
|
const result = ws.run();
|
|
86
|
-
assert.equal(result.status, 2);
|
|
87
|
-
assert.deepEqual(ws.ran(), ["installed doctor", "installed check"]);
|
|
88
|
-
assert.match(result.stderr, /FAIL {2}doctor from installed/);
|
|
89
|
-
assert.match(result.stderr, /kit setup, not in your code/);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test("drift alone after a refused stop lets the session end with a note, so a stale plugin never loops it", (t) => {
|
|
93
|
-
const ws = workspace(t, { codes: { doctor: 1 }, withDoctor: true });
|
|
94
|
-
const result = ws.run({ stop_hook_active: true });
|
|
95
89
|
assert.equal(result.status, 0, result.stderr);
|
|
96
|
-
assert.
|
|
90
|
+
assert.deepEqual(ws.ran(), ["installed doctor", "installed check"]);
|
|
91
|
+
const message = JSON.parse(result.stdout).systemMessage;
|
|
92
|
+
assert.match(message, /kit setup drifts/);
|
|
93
|
+
assert.match(message, /FAIL {2}doctor from installed/);
|
|
97
94
|
});
|
|
98
95
|
|
|
99
|
-
test("a red check
|
|
96
|
+
test("a red check beside drift blocks, and the block text carries the code failure, not the drift", (t) => {
|
|
100
97
|
const ws = workspace(t, { codes: { doctor: 1, check: 1 }, withDoctor: true });
|
|
101
|
-
const result = ws.run(
|
|
98
|
+
const result = ws.run();
|
|
102
99
|
assert.equal(result.status, 2);
|
|
103
100
|
assert.match(result.stderr, /FAIL {2}check from installed/);
|
|
101
|
+
assert.doesNotMatch(result.stderr, /doctor from installed/);
|
|
104
102
|
});
|
|
105
103
|
|
|
106
104
|
const HOOK_DRIFT = "FAIL .githooks/pre-commit hook-drift: pre-commit never calls `qc work-order-check`, so that step is off.";
|
|
107
105
|
|
|
108
|
-
test("hook drift reported only through qc check is drift, so
|
|
106
|
+
test("hook drift reported only through qc check is drift, so the stop passes with a note", (t) => {
|
|
109
107
|
const output = [HOOK_DRIFT, "", "1 problem(s). docs/enforcement.md"].join("\n");
|
|
110
108
|
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: output } });
|
|
111
|
-
const
|
|
112
|
-
assert.equal(refused.status, 2);
|
|
113
|
-
assert.match(refused.stderr, /kit setup, not in your code/);
|
|
114
|
-
const result = ws.run({ stop_hook_active: true });
|
|
109
|
+
const result = ws.run();
|
|
115
110
|
assert.equal(result.status, 0, result.stderr);
|
|
116
|
-
assert.match(JSON.parse(result.stdout).systemMessage, /
|
|
111
|
+
assert.match(JSON.parse(result.stdout).systemMessage, /kit setup drifts/);
|
|
117
112
|
});
|
|
118
113
|
|
|
119
|
-
test("hook drift beside a real gate failure
|
|
114
|
+
test("hook drift beside a real gate failure blocks", (t) => {
|
|
120
115
|
const output = [HOOK_DRIFT, "FAIL orders citations: ADR-0099 is cited but not defined", "", "2 problem(s)."].join("\n");
|
|
121
116
|
const ws = workspace(t, { codes: { check: 1 }, withDoctor: false, outputs: { check: output } });
|
|
122
117
|
const result = ws.run({ stop_hook_active: true });
|
|
@@ -276,118 +271,27 @@ test("the memory file holds a hash of the failure text, not the text", (t) => {
|
|
|
276
271
|
assert.ok(!stored.includes("ADR-0099"));
|
|
277
272
|
});
|
|
278
273
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
mkdirSync(path.join(repo, "node_modules", ".bin"), { recursive: true });
|
|
288
|
-
writeFileSync(path.join(repo, "node_modules", ".bin", "tsc"), "");
|
|
289
|
-
const stubDir = path.join(base, "stub");
|
|
290
|
-
mkdirSync(stubDir, { recursive: true });
|
|
291
|
-
runs.forEach((run, i) => {
|
|
292
|
-
writeFileSync(path.join(stubDir, `run-${i + 1}.code`), String(run.code));
|
|
293
|
-
writeFileSync(path.join(stubDir, `run-${i + 1}.out`), run.out);
|
|
294
|
-
});
|
|
295
|
-
const counter = path.join(stubDir, "count");
|
|
296
|
-
writeFileSync(
|
|
297
|
-
path.join(stubDir, "npx"),
|
|
298
|
-
[
|
|
299
|
-
"#!/usr/bin/env bash",
|
|
300
|
-
`[ "$1" = tsc ] || exit 0`,
|
|
301
|
-
`n=$(cat "${counter}" 2>/dev/null || echo 0); n=$((n + 1)); echo $n > "${counter}"`,
|
|
302
|
-
`while [ ! -f "${stubDir}/run-$n.code" ]; do n=$((n - 1)); done`,
|
|
303
|
-
`cat "${stubDir}/run-$n.out"`,
|
|
304
|
-
`exit $(cat "${stubDir}/run-$n.code")`,
|
|
305
|
-
].join("\n") + "\n",
|
|
306
|
-
{ mode: 0o755 },
|
|
307
|
-
);
|
|
308
|
-
const run = (input = {}) => ws.run(input, { PATH: `${stubDir}${path.delimiter}${process.env.PATH}` });
|
|
309
|
-
const count = () => (existsSync(counter) ? Number(readFileSync(counter, "utf8")) : 0);
|
|
310
|
-
return { run, count };
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
const TS2307 = "src/a.ts(1,22): error TS2307: Cannot find module 'zod' or its corresponding type declarations.\n";
|
|
314
|
-
const TS2322 = "src/b.ts(4,7): error TS2322: Type 'string' is not assignable to type 'number'.\n";
|
|
315
|
-
|
|
316
|
-
test("a typecheck that fails only with TS2307 runs once more, and a second pass ends the stop green", (t) => {
|
|
317
|
-
const gate = tscWorkspace(t, [{ code: 2, out: TS2307 }, { code: 0, out: "" }]);
|
|
318
|
-
const result = gate.run();
|
|
319
|
-
assert.equal(result.status, 0, result.stderr);
|
|
320
|
-
assert.equal(gate.count(), 2);
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
test("a typecheck that still fails with TS2307 on the second run blocks, with the second run's text", (t) => {
|
|
324
|
-
const gate = tscWorkspace(t, [{ code: 2, out: TS2307 }, { code: 2, out: TS2307.replace("zod", "hono") }]);
|
|
325
|
-
const result = gate.run();
|
|
326
|
-
assert.equal(result.status, 2);
|
|
327
|
-
assert.equal(gate.count(), 2);
|
|
328
|
-
assert.match(result.stderr, /typecheck failed/);
|
|
329
|
-
assert.match(result.stderr, /hono/);
|
|
274
|
+
test("an installed harness with `qc lint` runs the typecheck and the lint, and an older one leaves both to the plugin", (t) => {
|
|
275
|
+
const current = workspace(t, { codes: {}, withDoctor: false });
|
|
276
|
+
writeFileSync(path.join(current.installedCli, "lint-changed.mjs"), "");
|
|
277
|
+
assert.equal(current.run().status, 0);
|
|
278
|
+
assert.deepEqual(current.tools(), ["installed typecheck", "installed lint"]);
|
|
279
|
+
const older = workspace(t, { codes: {}, withDoctor: false });
|
|
280
|
+
assert.equal(older.run().status, 0);
|
|
281
|
+
assert.deepEqual(older.tools(), ["plugin typecheck", "plugin lint"]);
|
|
330
282
|
});
|
|
331
283
|
|
|
332
|
-
test("a
|
|
333
|
-
for (const
|
|
334
|
-
const
|
|
335
|
-
|
|
284
|
+
test("a red typecheck or a red lint blocks, each under its own heading", (t) => {
|
|
285
|
+
for (const command of ["typecheck", "lint"]) {
|
|
286
|
+
const ws = workspace(t, { codes: { [command]: 1 }, withDoctor: false });
|
|
287
|
+
writeFileSync(path.join(ws.installedCli, "lint-changed.mjs"), "");
|
|
288
|
+
const result = ws.run();
|
|
336
289
|
assert.equal(result.status, 2);
|
|
337
|
-
assert.
|
|
338
|
-
assert.match(result.stderr, /
|
|
290
|
+
assert.match(result.stderr, new RegExp(`${command} failed:\nFAIL {2}${command} from installed`));
|
|
291
|
+
assert.match(result.stderr, /The change is red/);
|
|
339
292
|
}
|
|
340
293
|
});
|
|
341
294
|
|
|
342
|
-
test("a large TS2322 log does not retry, since the early exit of the filter must not read as a pipe failure", (t) => {
|
|
343
|
-
const out = TS2322.repeat(20_000);
|
|
344
|
-
const gate = tscWorkspace(t, [{ code: 2, out }, { code: 0, out: "" }]);
|
|
345
|
-
const result = gate.run();
|
|
346
|
-
assert.equal(result.status, 2);
|
|
347
|
-
assert.equal(gate.count(), 1);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
test("a failing typecheck with no error line does not retry, and a passing one runs once", (t) => {
|
|
351
|
-
const broken = tscWorkspace(t, [{ code: 1, out: "tsc: command crashed\n" }, { code: 0, out: "" }]);
|
|
352
|
-
assert.equal(broken.run().status, 2);
|
|
353
|
-
assert.equal(broken.count(), 1);
|
|
354
|
-
const green = tscWorkspace(t, [{ code: 0, out: "" }]);
|
|
355
|
-
assert.equal(green.run().status, 0);
|
|
356
|
-
assert.equal(green.count(), 1);
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
test("a root typecheck script replaces tsc -b, keeps the TS2307 retry, and the lint call carries a cache", (t) => {
|
|
360
|
-
const ws = workspace(t, { codes: {}, withDoctor: false });
|
|
361
|
-
const base = path.dirname(ws.tmpDir);
|
|
362
|
-
const repo = path.join(base, "repo");
|
|
363
|
-
mkdirSync(path.join(repo, "node_modules", ".bin"), { recursive: true });
|
|
364
|
-
writeFileSync(path.join(repo, "node_modules", ".bin", "eslint"), "");
|
|
365
|
-
writeFileSync(path.join(repo, "package.json"), JSON.stringify({ scripts: { typecheck: "tsgo" } }));
|
|
366
|
-
writeFileSync(path.join(repo, "pnpm-lock.yaml"), "");
|
|
367
|
-
const stubDir = path.join(base, "stub");
|
|
368
|
-
mkdirSync(stubDir, { recursive: true });
|
|
369
|
-
const calls = path.join(stubDir, "calls");
|
|
370
|
-
const counter = path.join(stubDir, "count");
|
|
371
|
-
writeFileSync(
|
|
372
|
-
path.join(stubDir, "pnpm"),
|
|
373
|
-
[
|
|
374
|
-
"#!/usr/bin/env bash",
|
|
375
|
-
`echo "pnpm $*" >> "${calls}"`,
|
|
376
|
-
`n=$(cat "${counter}" 2>/dev/null || echo 0); n=$((n + 1)); echo $n > "${counter}"`,
|
|
377
|
-
`if [ $n -eq 1 ]; then printf '%s' "${TS2307.trim()}"; exit 2; fi`,
|
|
378
|
-
"exit 0",
|
|
379
|
-
].join("\n") + "\n",
|
|
380
|
-
{ mode: 0o755 },
|
|
381
|
-
);
|
|
382
|
-
writeFileSync(path.join(stubDir, "npx"), ["#!/usr/bin/env bash", `echo "npx $*" >> "${calls}"`, "exit 0"].join("\n") + "\n", { mode: 0o755 });
|
|
383
|
-
const result = ws.run({}, { PATH: `${stubDir}${path.delimiter}${process.env.PATH}` });
|
|
384
|
-
assert.equal(result.status, 0, result.stderr);
|
|
385
|
-
const lines = readFileSync(calls, "utf8").trim().split("\n");
|
|
386
|
-
assert.deepEqual(lines.slice(0, 2), ["pnpm run -s typecheck", "pnpm run -s typecheck"]);
|
|
387
|
-
assert.ok(!lines.some((l) => l.startsWith("npx tsc")));
|
|
388
|
-
assert.ok(lines.includes("npx eslint . --max-warnings 0 --cache --cache-location node_modules/.cache/eslint-stop-gate"));
|
|
389
|
-
});
|
|
390
|
-
|
|
391
295
|
/** A workspace whose repository is a git repository with one commit, so the gate can fingerprint it. */
|
|
392
296
|
function gitWorkspace(t, installed = { codes: {}, withDoctor: false }) {
|
|
393
297
|
const ws = workspace(t, installed);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// `qc typecheck`: the repository's own `typecheck` script when it has one, else the installed tsc.
|
|
2
|
+
// tsc builds only a tsconfig with project references, because a build writes a .tsbuildinfo file
|
|
3
|
+
// and leaves the tree dirty. The hooks and CI call this one command, so they run the same check.
|
|
4
|
+
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
|
|
9
|
+
/** The command that typechecks `root`, or null when it has no typecheck script and no tsc. */
|
|
10
|
+
export function typecheckCommand(root) {
|
|
11
|
+
const manifest = path.join(root, "package.json");
|
|
12
|
+
let scripts = {};
|
|
13
|
+
try {
|
|
14
|
+
scripts = JSON.parse(readFileSync(manifest, "utf8")).scripts ?? {};
|
|
15
|
+
} catch {}
|
|
16
|
+
if (typeof scripts.typecheck === "string") {
|
|
17
|
+
const manager = existsSync(path.join(root, "pnpm-lock.yaml")) ? "pnpm" : existsSync(path.join(root, "yarn.lock")) ? "yarn" : "npm";
|
|
18
|
+
return { cmd: manager, args: ["run", "-s", "typecheck"], shell: true };
|
|
19
|
+
}
|
|
20
|
+
const tsc = path.join(root, "node_modules", "typescript", "bin", "tsc");
|
|
21
|
+
const tsconfig = path.join(root, "tsconfig.json");
|
|
22
|
+
if (!existsSync(tsc) || !existsSync(tsconfig)) return null;
|
|
23
|
+
const references = /"references"\s*:/.test(readFileSync(tsconfig, "utf8"));
|
|
24
|
+
return { cmd: process.execPath, args: [tsc, ...(references ? ["-b"] : ["--noEmit", "-p", "."]), "--pretty", "false"], shell: false };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** True when every error is TS2307, which a concurrent install produces while a module is missing for a moment. */
|
|
28
|
+
export function onlyMissingModules(output) {
|
|
29
|
+
const errors = output.split(/\r?\n/).filter((line) => /error TS\d+/.test(line));
|
|
30
|
+
return errors.length > 0 && errors.every((line) => line.includes("error TS2307"));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @returns the exit code of the typecheck, or 0 when the repository has nothing to typecheck. */
|
|
34
|
+
export function runTypecheck(config) {
|
|
35
|
+
const command = typecheckCommand(config.root);
|
|
36
|
+
if (command === null) {
|
|
37
|
+
console.log("typecheck skipped: no typecheck script and no tsc with a tsconfig.json");
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
const once = () => spawnSync(command.cmd, command.args, { cwd: config.root, encoding: "utf8", shell: command.shell, windowsHide: true, maxBuffer: 1 << 26 });
|
|
41
|
+
let result = once();
|
|
42
|
+
if (result.status !== 0 && onlyMissingModules(`${result.stdout}${result.stderr}`)) result = once();
|
|
43
|
+
process.stdout.write(result.stdout ?? "");
|
|
44
|
+
process.stderr.write(result.stderr ?? "");
|
|
45
|
+
return result.status ?? 1;
|
|
46
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { mkdirSync, mkdtempSync, 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 { onlyMissingModules, typecheckCommand } from "./typecheck.mjs";
|
|
7
|
+
|
|
8
|
+
function root(t, files) {
|
|
9
|
+
const dir = mkdtempSync(path.join(os.tmpdir(), "qc-typecheck-"));
|
|
10
|
+
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
11
|
+
for (const [name, text] of Object.entries(files)) {
|
|
12
|
+
mkdirSync(path.dirname(path.join(dir, name)), { recursive: true });
|
|
13
|
+
writeFileSync(path.join(dir, name), text);
|
|
14
|
+
}
|
|
15
|
+
return dir;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const TSC = { "node_modules/typescript/bin/tsc": "" };
|
|
19
|
+
|
|
20
|
+
test("a typecheck script wins, run by the package manager of the lockfile", (t) => {
|
|
21
|
+
const dir = root(t, { "package.json": '{ "scripts": { "typecheck": "tsc" } }', "pnpm-lock.yaml": "", "tsconfig.json": "{}", ...TSC });
|
|
22
|
+
assert.deepEqual(typecheckCommand(dir), { cmd: "pnpm", args: ["run", "-s", "typecheck"], shell: true });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("without a script, tsc checks with --noEmit, so no .tsbuildinfo is written", (t) => {
|
|
26
|
+
const dir = root(t, { "package.json": "{}", "tsconfig.json": '{ "compilerOptions": {} }', ...TSC });
|
|
27
|
+
assert.deepEqual(typecheckCommand(dir).args.slice(1), ["--noEmit", "-p", ".", "--pretty", "false"]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("a tsconfig with project references builds, because --noEmit would check none of them", (t) => {
|
|
31
|
+
const dir = root(t, { "tsconfig.json": '{ "files": [], "references": [{ "path": "a" }] }', ...TSC });
|
|
32
|
+
assert.deepEqual(typecheckCommand(dir).args.slice(1), ["-b", "--pretty", "false"]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("no script and no tsc means nothing to typecheck", (t) => {
|
|
36
|
+
assert.equal(typecheckCommand(root(t, { "package.json": "{}", "tsconfig.json": "{}" })), null);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("a run whose only errors are TS2307 is retried; any other error is not", () => {
|
|
40
|
+
assert.equal(onlyMissingModules("a.ts(1,1): error TS2307: Cannot find module 'x'."), true);
|
|
41
|
+
assert.equal(onlyMissingModules("a.ts(1,1): error TS2307: x\nb.ts(2,2): error TS2322: y"), false);
|
|
42
|
+
assert.equal(onlyMissingModules("npm ERR! missing script"), false);
|
|
43
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// A pipeline that stages the root user configuration for the npm package.
|
|
2
|
+
|
|
3
|
+
import { copyFileSync, mkdirSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
export function packageUserConfig(source, target) {
|
|
8
|
+
for (const file of ["claude/statusline.js", "claude/settings.shared.json", "codex/config.shared.toml"]) {
|
|
9
|
+
mkdirSync(path.dirname(path.join(target, file)), { recursive: true });
|
|
10
|
+
copyFileSync(path.join(source, file), path.join(target, file));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
15
|
+
packageUserConfig(fileURLToPath(new URL("../../../../user-config/", import.meta.url)), fileURLToPath(new URL("../../user-config/", import.meta.url)));
|
|
16
|
+
}
|