architecture-harness 0.23.0 → 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/LICENSE +21 -0
- package/package.json +20 -5
- package/src/cli/bash-command-guard.mjs +44 -5
- package/src/cli/bash-command-guard.test.mjs +35 -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/check-commands.mjs +82 -0
- package/src/cli/check-commands.test.mjs +190 -0
- package/src/cli/check.mjs +34 -13
- package/src/cli/ci-template.test.mjs +17 -15
- package/src/cli/cli.test.mjs +33 -2
- package/src/cli/commit-msg.mjs +12 -12
- package/src/cli/commit-msg.test.mjs +22 -7
- package/src/cli/generated-file-guard.mjs +11 -2
- package/src/cli/generated-file-guard.test.mjs +14 -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 +12 -3
- package/src/cli/pr-check.mjs +15 -2
- package/src/cli/pr-check.test.mjs +19 -9
- package/src/cli/qc.mjs +38 -7
- package/src/cli/scaffold.mjs +23 -2
- package/src/cli/scaffold.test.mjs +51 -0
- package/src/cli/shell-command.mjs +3 -1
- package/src/cli/stop-gate.test.mjs +49 -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/work-order-guard.mjs +2 -2
- 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 +28 -11
- package/src/config.test.mjs +33 -1
- package/src/eslint/preset.mjs +8 -3
- package/src/eslint/preset.test.mjs +15 -8
- package/src/eslint/rules/no-raw-fetch.mjs +42 -2
- package/src/eslint/rules.test.mjs +15 -0
- package/src/gates/audit-append-only.mjs +18 -4
- package/src/gates/audit-append-only.test.mjs +19 -0
- package/src/gates/tenant-predicate.mjs +147 -7
- package/src/gates/tenant-predicate.test.mjs +55 -0
- package/src/gates/tenant-tables.mjs +5 -2
- package/src/gates/tenant-tables.test.mjs +8 -0
- package/src/gates/threshold-ratchet.mjs +12 -9
- package/src/gates/threshold-ratchet.test.mjs +16 -0
- package/src/plugin.test.mjs +28 -0
- package/src/screens/auth-run.test.mjs +20 -17
- package/src/screens/capture-idle.test.mjs +2 -1
- package/src/screens/catalog.test.mjs +3 -3
- package/src/screens/chrome.mjs +21 -0
- 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 +25 -84
- 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,157 @@
|
|
|
1
|
+
// `qc lint`: ESLint on the lines a change adds. A problem on a line the change leaves alone belongs
|
|
2
|
+
// to the code before it, so it never blocks: an agent that is refused for it edits code its task
|
|
3
|
+
// never named. The hooks, the pre-commit hook and CI all call this one command.
|
|
4
|
+
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { pathToFileURL } from "node:url";
|
|
10
|
+
import { gitOut, gitStatusWithin } from "./git-read.mjs";
|
|
11
|
+
|
|
12
|
+
const LINTABLE = /\.(?:[cm]?[jt]sx?)$/;
|
|
13
|
+
const DIFF_TIMEOUT_MS = 30_000;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The new-side line numbers of each file in a `git diff -U0` text. A deleted file is absent.
|
|
17
|
+
* @returns {Map<string, Set<number>>}
|
|
18
|
+
*/
|
|
19
|
+
export function parseDiff(text) {
|
|
20
|
+
const changed = new Map();
|
|
21
|
+
let current = null;
|
|
22
|
+
for (const line of text.split(/\r?\n/)) {
|
|
23
|
+
if (line.startsWith("+++ ")) {
|
|
24
|
+
const target = line.slice(4);
|
|
25
|
+
current = target === "/dev/null" ? null : target.replace(/^b\//, "");
|
|
26
|
+
if (current !== null && !changed.has(current)) changed.set(current, new Set());
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const hunk = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
30
|
+
if (hunk && current !== null) {
|
|
31
|
+
const start = Number(hunk[1]);
|
|
32
|
+
const count = hunk[2] === undefined ? 1 : Number(hunk[2]);
|
|
33
|
+
for (let n = start; n < start + count; n++) changed.get(current).add(n);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return changed;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The ref a change is measured from: the merge base with `ref` when it resolves, else HEAD, else null for a repository with no commit. */
|
|
40
|
+
export function baseOf(root, ref) {
|
|
41
|
+
if (ref) {
|
|
42
|
+
const mergeBase = gitOut(root, "merge-base", "HEAD", ref);
|
|
43
|
+
if (mergeBase) return mergeBase;
|
|
44
|
+
}
|
|
45
|
+
return gitOut(root, "rev-parse", "--verify", "--quiet", "HEAD");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Each changed file and its changed lines. `all` marks a file that is new as a whole.
|
|
50
|
+
* Staged reads the index against HEAD; otherwise the working tree against `base`, with untracked files.
|
|
51
|
+
* @returns {Map<string, Set<number> | "all">}
|
|
52
|
+
*/
|
|
53
|
+
export function changedLines(root, { staged = false, base = null } = {}) {
|
|
54
|
+
const args = ["diff", "-U0", "--no-color", "--no-ext-diff", "--diff-filter=ACMR"];
|
|
55
|
+
if (staged) args.push("--cached");
|
|
56
|
+
// With no commit yet every file is new, so there is nothing to diff against.
|
|
57
|
+
const head = gitOut(root, "rev-parse", "--verify", "--quiet", "HEAD");
|
|
58
|
+
if (head === null) {
|
|
59
|
+
const listed = gitOut(root, "ls-files", ...(staged ? [] : ["--others", "--cached", "--exclude-standard"])) ?? "";
|
|
60
|
+
return new Map(listed.split(/\r?\n/).filter(Boolean).map((file) => [file, "all"]));
|
|
61
|
+
}
|
|
62
|
+
if (!staged) args.push(base ?? head);
|
|
63
|
+
const diff = gitStatusWithin(DIFF_TIMEOUT_MS, root, ...args);
|
|
64
|
+
if (diff.error || diff.status !== 0) throw new Error(`git ${args.join(" ")} failed: ${diff.error ?? diff.stdout}`);
|
|
65
|
+
const changed = parseDiff(diff.stdout);
|
|
66
|
+
if (!staged) {
|
|
67
|
+
for (const file of (gitOut(root, "ls-files", "--others", "--exclude-standard") ?? "").split(/\r?\n/).filter(Boolean)) changed.set(file, "all");
|
|
68
|
+
}
|
|
69
|
+
return changed;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The problems of each ESLint result that sit on a changed line. A fatal message, such as a parse
|
|
74
|
+
* error, has no line of its own and always counts.
|
|
75
|
+
* @returns {{ kept: Array<{ file: string, message: object }>, skipped: number }}
|
|
76
|
+
*/
|
|
77
|
+
export function onChangedLines(results, changed, root) {
|
|
78
|
+
const kept = [];
|
|
79
|
+
let skipped = 0;
|
|
80
|
+
for (const result of results) {
|
|
81
|
+
const file = path.relative(root, result.filePath).split(path.sep).join("/");
|
|
82
|
+
const lines = changed.get(file);
|
|
83
|
+
for (const message of result.messages) {
|
|
84
|
+
if (message.fatal || lines === "all" || lines?.has(message.line)) kept.push({ file, message });
|
|
85
|
+
else skipped++;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return { kept, skipped };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The file as the index holds it, untrimmed so its line numbers stay, or null when git cannot read it. */
|
|
92
|
+
function stagedText(root, file) {
|
|
93
|
+
const result = spawnSync("git", ["show", `:${file}`], { cwd: root, encoding: "utf8", windowsHide: true, maxBuffer: 1 << 26 });
|
|
94
|
+
return result.status === 0 ? result.stdout : null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function loadEslint(root) {
|
|
98
|
+
let resolved;
|
|
99
|
+
try {
|
|
100
|
+
resolved = createRequire(path.join(root, "package.json")).resolve("eslint");
|
|
101
|
+
} catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return (await import(pathToFileURL(resolved).href)).ESLint;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function parseArgs(args) {
|
|
108
|
+
const options = { staged: false, base: undefined, files: [] };
|
|
109
|
+
for (let i = 0; i < args.length; i++) {
|
|
110
|
+
if (args[i] === "--staged") options.staged = true;
|
|
111
|
+
else if (args[i] === "--base") options.base = args[++i];
|
|
112
|
+
else options.files.push(args[i]);
|
|
113
|
+
}
|
|
114
|
+
return options;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** @returns the exit code: 1 when an error sits on a changed line, else 0. Warnings print and pass. */
|
|
118
|
+
export async function runLintChanged(config, args) {
|
|
119
|
+
const root = config.root;
|
|
120
|
+
const options = parseArgs(args);
|
|
121
|
+
const ESLint = await loadEslint(root);
|
|
122
|
+
if (ESLint === null) {
|
|
123
|
+
console.log("lint skipped: eslint is not installed in this repository");
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
const base = options.staged ? null : baseOf(root, options.base ?? config.check.base);
|
|
127
|
+
const changed = changedLines(root, { staged: options.staged, base });
|
|
128
|
+
const wanted = options.files.map((file) => path.relative(root, path.resolve(file)).split(path.sep).join("/"));
|
|
129
|
+
const eslint = new ESLint({ cwd: root });
|
|
130
|
+
const files = [];
|
|
131
|
+
for (const file of changed.keys()) {
|
|
132
|
+
if (!LINTABLE.test(file) || (wanted.length > 0 && !wanted.includes(file))) continue;
|
|
133
|
+
if (!existsSync(path.join(root, file)) || (await eslint.isPathIgnored(path.join(root, file)))) continue;
|
|
134
|
+
files.push(file);
|
|
135
|
+
}
|
|
136
|
+
if (files.length === 0) return 0;
|
|
137
|
+
|
|
138
|
+
const results = [];
|
|
139
|
+
for (const file of files) {
|
|
140
|
+
const filePath = path.join(root, file);
|
|
141
|
+
// A staged commit is judged as it will be stored, not as the working tree holds it now.
|
|
142
|
+
const staged = options.staged ? stagedText(root, file) : null;
|
|
143
|
+
results.push(...(staged === null ? await eslint.lintFiles([filePath]) : await eslint.lintText(staged, { filePath })));
|
|
144
|
+
}
|
|
145
|
+
const { kept, skipped } = onChangedLines(results, changed, root);
|
|
146
|
+
const errors = kept.filter(({ message }) => message.fatal || message.severity === 2);
|
|
147
|
+
for (const { file, message } of kept) {
|
|
148
|
+
const kind = message.fatal || message.severity === 2 ? "error" : "warning";
|
|
149
|
+
console.error(`${file}:${message.line ?? 0}:${message.column ?? 0} ${kind} ${message.message} ${message.ruleId ?? ""}`.trimEnd());
|
|
150
|
+
}
|
|
151
|
+
if (kept.length > 0) {
|
|
152
|
+
console.error(`\n${errors.length} error(s) and ${kept.length - errors.length} warning(s) on the changed lines.`);
|
|
153
|
+
}
|
|
154
|
+
// Said once, so a reader knows the gate saw them and chose not to count them.
|
|
155
|
+
if (skipped > 0) console.error(`${skipped} older problem(s) on lines this change leaves alone are not counted. Leave them alone.`);
|
|
156
|
+
return errors.length > 0 ? 1 : 0;
|
|
157
|
+
}
|
|
@@ -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,12 +31,13 @@ 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
|
|
|
38
|
-
function runHook(ws, file) {
|
|
39
|
-
const featureFile = path.join(file,
|
|
39
|
+
function runHook(ws, file, featureDir = "features") {
|
|
40
|
+
const featureFile = path.join(file, featureDir, "tasks", "index.ts");
|
|
40
41
|
mkdirSync(path.dirname(featureFile), { recursive: true });
|
|
41
42
|
writeFileSync(featureFile, "export {};\n");
|
|
42
43
|
return spawnSync("bash", [HOOK], {
|
|
@@ -74,12 +75,20 @@ test("a feature file in the session's own project is checked from the project",
|
|
|
74
75
|
assert.deepEqual(ws.ranIn(), [ws.project]);
|
|
75
76
|
});
|
|
76
77
|
|
|
78
|
+
test("a file under a layout with no features folder is checked too, since featureDir is the repository's own", (t) => {
|
|
79
|
+
const ws = workspace();
|
|
80
|
+
t.after(ws.cleanup);
|
|
81
|
+
assert.equal(runHook(ws, path.join(ws.project, "src"), "modules"), 2);
|
|
82
|
+
assert.deepEqual(ws.ranIn(), [ws.project]);
|
|
83
|
+
});
|
|
84
|
+
|
|
77
85
|
test("a checkout with its own installed harness is checked by that copy, not the plugin's", (t) => {
|
|
78
86
|
const ws = workspace();
|
|
79
87
|
t.after(ws.cleanup);
|
|
80
88
|
const cli = path.join(ws.linked, "node_modules", "architecture-harness", "src", "cli");
|
|
81
89
|
mkdirSync(cli, { recursive: true });
|
|
82
90
|
writeFileSync(path.join(cli, "qc.mjs"), "process.exit(0);\n");
|
|
91
|
+
writeFileSync(path.join(cli, "lint-changed.mjs"), "");
|
|
83
92
|
assert.equal(runHook(ws, path.join(ws.linked, "backend", "src")), 0);
|
|
84
93
|
assert.deepEqual(ws.ranIn(), []);
|
|
85
94
|
});
|
package/src/cli/pr-check.mjs
CHANGED
|
@@ -46,17 +46,26 @@ export async function checkPullRequest(event, { gh = runGh, hasToken, ci = false
|
|
|
46
46
|
notes: [],
|
|
47
47
|
};
|
|
48
48
|
}
|
|
49
|
-
|
|
49
|
+
// The CI template sets GH_TOKEN, so a CI run without one had it removed, and would pass unread.
|
|
50
|
+
if (!hasToken) {
|
|
51
|
+
return ci
|
|
52
|
+
? { problems: ["no GH_TOKEN, so the issues cannot be looked up. Set GH_TOKEN for this step."], notes: [] }
|
|
53
|
+
: { problems: [], notes: ["no GH_TOKEN, so the issues were not looked up"] };
|
|
54
|
+
}
|
|
50
55
|
|
|
51
56
|
const problems = [];
|
|
52
57
|
const notes = [];
|
|
58
|
+
let unseen = 0;
|
|
53
59
|
for (const { repo, number } of references) {
|
|
54
60
|
const name = `${repo}#${number}`;
|
|
55
61
|
const result = await gh(["issue", "view", String(number), "-R", repo, "--json", "createdAt"]);
|
|
56
62
|
if (!result.ok) {
|
|
57
63
|
const stderr = result.stderr ?? "";
|
|
58
64
|
if (NO_ISSUE.test(stderr)) problems.push(`${name} does not exist. Open the issue, then reference it.`);
|
|
59
|
-
else if (NO_REPOSITORY.test(stderr))
|
|
65
|
+
else if (NO_REPOSITORY.test(stderr)) {
|
|
66
|
+
unseen++;
|
|
67
|
+
notes.push(`the token cannot see ${repo}, so ${name} was not looked up`);
|
|
68
|
+
}
|
|
60
69
|
else if (ci && result.missing) problems.push(`gh is not installed, so ${name} cannot be checked`);
|
|
61
70
|
else {
|
|
62
71
|
const message = `could not reach GitHub for ${name}: ${stderr.trim().split("\n")[0]}`;
|
|
@@ -69,6 +78,10 @@ export async function checkPullRequest(event, { gh = runGh, hasToken, ci = false
|
|
|
69
78
|
problems.push(`${name} was opened after the pull request. Register the work before it starts.`);
|
|
70
79
|
}
|
|
71
80
|
}
|
|
81
|
+
// A reference the token cannot read proves nothing, so a body of only those names no issue.
|
|
82
|
+
if (unseen === references.length) {
|
|
83
|
+
problems.push(`pull request #${pr.number} names only issues the token cannot see. Also name an issue in ${event.repository?.full_name}.`);
|
|
84
|
+
}
|
|
72
85
|
return { problems, notes };
|
|
73
86
|
}
|
|
74
87
|
|
|
@@ -74,19 +74,30 @@ test("in CI a failed call to GitHub fails and quotes the first stderr line", asy
|
|
|
74
74
|
assert.deepEqual(result.notes, []);
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
-
test("in CI with no token the check
|
|
77
|
+
test("in CI with no token the check fails and names GH_TOKEN", async () => {
|
|
78
78
|
const result = await checkPullRequest(event("Fixes #12"), { gh: missingGh, hasToken: false, ci: true });
|
|
79
|
-
assert.
|
|
80
|
-
assert.match(result.notes.join(), /no GH_TOKEN/);
|
|
79
|
+
assert.match(result.problems.join(), /no GH_TOKEN/);
|
|
81
80
|
});
|
|
82
81
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
/** Knows acme/app#12, and cannot see acme/private at all. */
|
|
83
|
+
const ghWithPrivate = async (args) =>
|
|
84
|
+
args.includes("acme/private")
|
|
85
|
+
? { ok: false, stderr: "GraphQL: Could not resolve to a Repository with the name 'acme/private'. (repository)" }
|
|
86
|
+
: ghKnowing({ "acme/app#12": "2026-05-01T00:00:00Z" })(args);
|
|
87
|
+
|
|
88
|
+
test("in CI a repository the token cannot see is a note beside an issue it can see", async () => {
|
|
89
|
+
const result = await checkPullRequest(event("Fixes #12. Refs acme/private#5"), { gh: ghWithPrivate, hasToken: true, ci: true });
|
|
86
90
|
assert.deepEqual(result.problems, []);
|
|
87
91
|
assert.match(result.notes.join(), /the token cannot see acme.private/);
|
|
88
92
|
});
|
|
89
93
|
|
|
94
|
+
test("a body that names only issues the token cannot see fails, in CI and outside it", async () => {
|
|
95
|
+
for (const ci of [true, false]) {
|
|
96
|
+
const result = await checkPullRequest(event("Refs acme/private#5"), { gh: ghWithPrivate, hasToken: true, ci });
|
|
97
|
+
assert.match(result.problems.join(), /names only issues the token cannot see\. Also name an issue in acme\/app/);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
90
101
|
test("CI=false is not CI", () => {
|
|
91
102
|
assert.equal(isCi({ CI: "true" }), true);
|
|
92
103
|
assert.equal(isCi({ CI: "1" }), true);
|
|
@@ -97,9 +108,8 @@ test("CI=false is not CI", () => {
|
|
|
97
108
|
});
|
|
98
109
|
|
|
99
110
|
test("a repository the token cannot see is a note, never a missing issue", async () => {
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
assert.deepEqual(result.problems, []);
|
|
111
|
+
const result = await checkPullRequest(event("Fixes #12. Refs acme/private#5"), { gh: ghWithPrivate, hasToken: true });
|
|
112
|
+
assert.doesNotMatch(result.problems.join(), /does not exist/);
|
|
103
113
|
assert.match(result.notes.join(), /the token cannot see acme.private/);
|
|
104
114
|
});
|
|
105
115
|
|
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,13 +66,25 @@ 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));
|
|
65
76
|
}
|
|
66
77
|
if (command === "screens") {
|
|
67
|
-
|
|
68
|
-
|
|
78
|
+
// The screen packages are optional peers: only this command needs them, so a repository installs them for it.
|
|
79
|
+
try {
|
|
80
|
+
const { runScreens } = await import("./screens.mjs");
|
|
81
|
+
process.exit(await runScreens(rest));
|
|
82
|
+
} catch (error) {
|
|
83
|
+
const missing = /Cannot find package '(playwright|pixelmatch|pngjs)'/.exec(error?.message ?? "");
|
|
84
|
+
if (error?.code !== "ERR_MODULE_NOT_FOUND" || !missing) throw error;
|
|
85
|
+
console.error(`qc screens needs the ${missing[1]} package. Install the screen packages: pnpm add -D playwright pixelmatch pngjs`);
|
|
86
|
+
process.exit(2);
|
|
87
|
+
}
|
|
69
88
|
}
|
|
70
89
|
// A broken qc.config.json must not keep a lease in place, so the lease runs before the load.
|
|
71
90
|
if (command === "lease") {
|
|
@@ -93,18 +112,30 @@ async function main() {
|
|
|
93
112
|
case "check": {
|
|
94
113
|
const { files, listed } = await checkFiles(rest);
|
|
95
114
|
// An empty list means nothing staged to check, not the full check.
|
|
96
|
-
|
|
97
|
-
|
|
115
|
+
const { problems, lines } = listed && files.length === 0
|
|
116
|
+
? { problems: [], lines: [] }
|
|
117
|
+
: await runCheck(config, files);
|
|
98
118
|
if (problems.length > 0) {
|
|
99
119
|
for (const problem of problems) {
|
|
100
120
|
console.error(`FAIL ${problem.feature ?? problem.path} ${problem.rule}: ${problem.detail}`);
|
|
101
121
|
}
|
|
102
|
-
|
|
122
|
+
const guide = existsSync(path.join(config.root, config.docs.enforcement)) ? ` ${config.docs.enforcement}` : "";
|
|
123
|
+
console.error(`\n${problems.length} problem(s).${guide}`);
|
|
103
124
|
process.exit(1);
|
|
104
125
|
}
|
|
105
126
|
for (const line of lines) console.log(line);
|
|
127
|
+
const { runCheckCommands } = await import("./check-commands.mjs");
|
|
128
|
+
process.exitCode = await runCheckCommands(config);
|
|
106
129
|
return;
|
|
107
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
|
+
}
|
|
108
139
|
case "init": {
|
|
109
140
|
const { runInit } = await import("./init.mjs");
|
|
110
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
|
+
});
|
|
@@ -253,7 +253,9 @@ export function gitCall(segment) {
|
|
|
253
253
|
explicitDir = true;
|
|
254
254
|
i++;
|
|
255
255
|
} else if (/^--(git-dir|work-tree)=/.test(word)) explicitDir = true;
|
|
256
|
-
else if (word === "-c") configs.push(words[++i]);
|
|
256
|
+
else if (word === "-c" || word === "--config-env") configs.push(words[++i]);
|
|
257
|
+
// `--config-env=name=VAR` sets `name` from a variable, so it is a `-c` whose value hides in the environment.
|
|
258
|
+
else if (word.startsWith("--config-env=")) configs.push(word.slice("--config-env=".length));
|
|
257
259
|
else if (!word.startsWith("-")) return { sub: word, dirs, explicitDir, configs, args: words.slice(i + 1) };
|
|
258
260
|
}
|
|
259
261
|
return { sub: null, dirs, explicitDir, configs, args: [] };
|