architecture-harness 0.4.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 +80 -0
- package/src/cli/check.mjs +535 -0
- package/src/cli/cli.test.mjs +120 -0
- package/src/cli/commit-attribution.mjs +68 -0
- package/src/cli/commit-attribution.test.mjs +47 -0
- package/src/cli/decisions.mjs +223 -0
- package/src/cli/doctor.mjs +135 -0
- package/src/cli/doctor.test.mjs +57 -0
- package/src/cli/init.mjs +98 -0
- package/src/cli/install-hooks.mjs +26 -0
- package/src/cli/qc.mjs +89 -0
- package/src/cli/scaffold.mjs +194 -0
- package/src/cli/sync-enforcement-map.mjs +28 -0
- package/src/cli/work-order-guard.mjs +0 -0
- package/src/cli/work-order-guard.test.mjs +79 -0
- package/src/cli/worktree-isolation.mjs +94 -0
- package/src/cli/worktree-isolation.test.mjs +82 -0
- package/src/config.mjs +338 -0
- package/src/config.test.mjs +59 -0
- package/src/decisions.mjs +198 -0
- package/src/decisions.test.mjs +183 -0
- package/src/descriptions.mjs +28 -0
- package/src/descriptions.test.mjs +27 -0
- package/src/enforcement-map.mjs +46 -0
- package/src/enforcement-map.test.mjs +60 -0
- package/src/eslint/index.mjs +37 -0
- package/src/eslint/options.mjs +53 -0
- package/src/eslint/preset.mjs +192 -0
- package/src/eslint/preset.test.mjs +96 -0
- package/src/eslint/rules/durable-idempotency-key.mjs +120 -0
- package/src/eslint/rules/external-service-only-in-adapter.mjs +52 -0
- package/src/eslint/rules/module-boundary.mjs +12 -0
- package/src/eslint/rules/name-the-pattern.mjs +111 -0
- package/src/eslint/rules/no-comment-paragraph.mjs +101 -0
- package/src/eslint/rules/no-cross-feature-internals.mjs +57 -0
- package/src/eslint/rules/no-number-in-comment.mjs +89 -0
- package/src/eslint/rules/no-offset-pagination.mjs +29 -0
- package/src/eslint/rules/no-orchestration-in-trigger.mjs +56 -0
- package/src/eslint/rules/no-promise-then.mjs +43 -0
- package/src/eslint/rules/no-promise-then.test.mjs +63 -0
- package/src/eslint/rules/no-raw-fetch.mjs +27 -0
- package/src/eslint/rules/no-status-literal.mjs +34 -0
- package/src/eslint/rules/no-supersession-trail.mjs +51 -0
- package/src/eslint/rules/scoped-repository.mjs +43 -0
- package/src/eslint/rules/signal-last-param.mjs +43 -0
- package/src/eslint/rules/storage-only-in-resource.mjs +52 -0
- package/src/eslint/rules/tenant-scoped-table.mjs +71 -0
- package/src/eslint/rules.test.mjs +627 -0
- package/src/gates/adr-format.mjs +87 -0
- package/src/gates/adr-format.test.mjs +54 -0
- package/src/gates/audit-append-only.mjs +34 -0
- package/src/gates/audit-append-only.test.mjs +36 -0
- package/src/gates/citations.mjs +93 -0
- package/src/gates/citations.test.mjs +115 -0
- package/src/gates/claimed-requirements.mjs +53 -0
- package/src/gates/claimed-requirements.test.mjs +60 -0
- package/src/gates/comment-style.mjs +76 -0
- package/src/gates/comment-style.test.mjs +46 -0
- package/src/gates/config-floor.mjs +68 -0
- package/src/gates/config-floor.test.mjs +52 -0
- package/src/gates/contract-compose.mjs +38 -0
- package/src/gates/contract-compose.test.mjs +40 -0
- package/src/gates/eight-blocks.mjs +223 -0
- package/src/gates/eight-blocks.test.mjs +359 -0
- package/src/gates/enforcement-map.mjs +62 -0
- package/src/gates/enforcement-map.test.mjs +41 -0
- package/src/gates/english-source.mjs +176 -0
- package/src/gates/english-source.test.mjs +122 -0
- package/src/gates/frontend-boundaries.mjs +154 -0
- package/src/gates/frontend-boundaries.test.mjs +72 -0
- package/src/gates/gate-tests.mjs +44 -0
- package/src/gates/gate-tests.test.mjs +41 -0
- package/src/gates/headless-sagas.mjs +54 -0
- package/src/gates/headless-sagas.test.mjs +71 -0
- package/src/gates/internal-routes.mjs +76 -0
- package/src/gates/internal-routes.test.mjs +53 -0
- package/src/gates/public-routes.mjs +67 -0
- package/src/gates/public-routes.test.mjs +38 -0
- package/src/gates/registry-agreement.mjs +37 -0
- package/src/gates/registry-agreement.test.mjs +36 -0
- package/src/gates/saga-tests.mjs +94 -0
- package/src/gates/saga-tests.test.mjs +92 -0
- package/src/gates/sql-identifiers.mjs +244 -0
- package/src/gates/sql-identifiers.test.mjs +197 -0
- package/src/gates/tenant-predicate.mjs +175 -0
- package/src/gates/tenant-predicate.test.mjs +161 -0
- package/src/gates/tenant-tables.mjs +65 -0
- package/src/gates/tenant-tables.test.mjs +56 -0
- package/src/gates/test-mirror.mjs +47 -0
- package/src/gates/test-mirror.test.mjs +31 -0
- package/src/index.mjs +9 -0
- package/templates/.jscpd.json +15 -0
- package/templates/CLAUDE.md +54 -0
- package/templates/decisions.examples.md +25 -0
- package/templates/docs/adr-template.md +58 -0
- package/templates/docs/architecture.md +179 -0
- package/templates/docs/decisions.md +72 -0
- package/templates/docs/enforcement.md +94 -0
- package/templates/docs/glossary.md +16 -0
- package/templates/docs/guards.md +23 -0
- package/templates/docs/pagination.md +113 -0
- package/templates/docs/performance.md +35 -0
- package/templates/docs/ui.md +32 -0
- package/templates/githooks/commit-msg +4 -0
- package/templates/githooks/pre-commit +18 -0
- package/templates/githooks/pre-push +6 -0
- package/templates/github/workflows/ci.yml +21 -0
- package/templates/qc.config.json +6 -0
- package/templates/quality-thresholds.json +114 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync, existsSync, mkdirSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { load } from "../config.mjs";
|
|
8
|
+
import { runInit } from "./init.mjs";
|
|
9
|
+
import { installHooks } from "./install-hooks.mjs";
|
|
10
|
+
import { runCheck } from "./check.mjs";
|
|
11
|
+
|
|
12
|
+
function repo() {
|
|
13
|
+
const dir = mkdtempSync(path.join(tmpdir(), "qc-cli-"));
|
|
14
|
+
execFileSync("git", ["init", "-q"], { cwd: dir });
|
|
15
|
+
writeFileSync(path.join(dir, "package.json"), '{"name":"t","private":true,"type":"module"}\n');
|
|
16
|
+
return dir;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const quiet = (run) => {
|
|
20
|
+
const log = console.log;
|
|
21
|
+
console.log = () => {};
|
|
22
|
+
try {
|
|
23
|
+
return run();
|
|
24
|
+
} finally {
|
|
25
|
+
console.log = log;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
test("init writes the documents the gates read", async () => {
|
|
30
|
+
const dir = repo();
|
|
31
|
+
await quiet(() => runInit(load(dir)));
|
|
32
|
+
for (const file of ["qc.config.json", "docs/decisions.md", "docs/enforcement.md", "docs/pagination.md", ".githooks/pre-commit", ".githooks/pre-push"]) {
|
|
33
|
+
assert.ok(existsSync(path.join(dir, file)), `${file} is missing`);
|
|
34
|
+
}
|
|
35
|
+
rmSync(dir, { recursive: true, force: true });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// The examples cite ids the shipped register does not define; a repository that
|
|
39
|
+
// took them would fail its own citations gate on the first run.
|
|
40
|
+
test("init does not copy the decision examples", async () => {
|
|
41
|
+
const dir = repo();
|
|
42
|
+
await quiet(() => runInit(load(dir)));
|
|
43
|
+
assert.equal(existsSync(path.join(dir, "docs/decisions.examples.md")), false);
|
|
44
|
+
rmSync(dir, { recursive: true, force: true });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("init leaves a file that already exists alone, and --force replaces it", async () => {
|
|
48
|
+
const dir = repo();
|
|
49
|
+
mkdirSync(path.join(dir, "docs"), { recursive: true });
|
|
50
|
+
writeFileSync(path.join(dir, "docs/decisions.md"), "mine\n");
|
|
51
|
+
await quiet(() => runInit(load(dir)));
|
|
52
|
+
assert.equal(readFileSync(path.join(dir, "docs/decisions.md"), "utf8"), "mine\n");
|
|
53
|
+
await quiet(() => runInit(load(dir), ["--force"]));
|
|
54
|
+
assert.notEqual(readFileSync(path.join(dir, "docs/decisions.md"), "utf8"), "mine\n");
|
|
55
|
+
rmSync(dir, { recursive: true, force: true });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("init adds the scripts its own docs reference, without touching one already set", async () => {
|
|
59
|
+
const dir = repo();
|
|
60
|
+
writeFileSync(path.join(dir, "package.json"), '{"name":"t","type":"module","scripts":{"spec:check":"mine"}}\n');
|
|
61
|
+
await quiet(() => runInit(load(dir)));
|
|
62
|
+
const manifest = JSON.parse(readFileSync(path.join(dir, "package.json"), "utf8"));
|
|
63
|
+
assert.equal(manifest.scripts["spec:check"], "mine");
|
|
64
|
+
assert.equal(manifest.scripts.prepare, "qc install-hooks");
|
|
65
|
+
rmSync(dir, { recursive: true, force: true });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("install-hooks points git at the hook directory", async () => {
|
|
69
|
+
const dir = repo();
|
|
70
|
+
await quiet(() => runInit(load(dir)));
|
|
71
|
+
await quiet(() => installHooks(load(dir)));
|
|
72
|
+
const configured = execFileSync("git", ["config", "core.hooksPath"], { cwd: dir, encoding: "utf8" }).trim();
|
|
73
|
+
assert.equal(configured, ".githooks");
|
|
74
|
+
rmSync(dir, { recursive: true, force: true });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("install-hooks refuses before init rather than configuring nothing", async () => {
|
|
78
|
+
const dir = repo();
|
|
79
|
+
const error = console.error;
|
|
80
|
+
console.error = () => {};
|
|
81
|
+
const previous = process.exitCode;
|
|
82
|
+
try {
|
|
83
|
+
await installHooks(load(dir));
|
|
84
|
+
assert.equal(process.exitCode, 1);
|
|
85
|
+
} finally {
|
|
86
|
+
console.error = error;
|
|
87
|
+
process.exitCode = previous;
|
|
88
|
+
}
|
|
89
|
+
rmSync(dir, { recursive: true, force: true });
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// A config whose roots all point at nothing sweeps nothing, and every gate downstream of the
|
|
93
|
+
// sweep then passes on an empty set. The run has to say so rather than report a green wall.
|
|
94
|
+
test("a config whose every feature root is missing is a problem", async () => {
|
|
95
|
+
const dir = repo();
|
|
96
|
+
await quiet(() => runInit(load(dir)));
|
|
97
|
+
const { problems, lines } = await runCheck(load(dir));
|
|
98
|
+
assert.ok(
|
|
99
|
+
problems.some((problem) => problem.rule === "unfound-root"),
|
|
100
|
+
"a repository with neither configured root present reported no problem",
|
|
101
|
+
);
|
|
102
|
+
assert.ok(
|
|
103
|
+
!lines.some((line) => line.includes("OK structure")),
|
|
104
|
+
"the structure line claimed a pass while the roots matched nothing",
|
|
105
|
+
);
|
|
106
|
+
rmSync(dir, { recursive: true, force: true });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// One root missing is ordinary: a repository with no frontend is not misconfigured.
|
|
110
|
+
test("one present feature root is enough", async () => {
|
|
111
|
+
const dir = repo();
|
|
112
|
+
await quiet(() => runInit(load(dir)));
|
|
113
|
+
mkdirSync(path.join(dir, "backend/src/features"), { recursive: true });
|
|
114
|
+
const { problems } = await runCheck(load(dir));
|
|
115
|
+
assert.ok(
|
|
116
|
+
!problems.some((problem) => problem.rule === "unfound-root"),
|
|
117
|
+
"a repository with one real root was reported as pointing at nothing",
|
|
118
|
+
);
|
|
119
|
+
rmSync(dir, { recursive: true, force: true });
|
|
120
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// A commit message records who is accountable for a change. A tool is not, and a trailer naming
|
|
2
|
+
// one adds a co-author who cannot answer a question about the code. The rule is widely stated and
|
|
3
|
+
// widely ignored, because nothing checks it -- so this checks it, at the one moment it is cheap.
|
|
4
|
+
|
|
5
|
+
const TRAILER = /^\s*(?:co-authored-by|signed-off-by|assisted-by|generated-by)\s*:\s*(.+)$/i;
|
|
6
|
+
|
|
7
|
+
const DEFAULT_TOOLS = [
|
|
8
|
+
"claude",
|
|
9
|
+
"anthropic",
|
|
10
|
+
"copilot",
|
|
11
|
+
"chatgpt",
|
|
12
|
+
"openai",
|
|
13
|
+
"gpt-4",
|
|
14
|
+
"gpt-5",
|
|
15
|
+
"gemini",
|
|
16
|
+
"cursor",
|
|
17
|
+
"codeium",
|
|
18
|
+
"devin",
|
|
19
|
+
"aider",
|
|
20
|
+
"windsurf",
|
|
21
|
+
"bot@",
|
|
22
|
+
"noreply@anthropic.com",
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
// A marketing footer is the other half of the same habit.
|
|
26
|
+
const DEFAULT_FOOTERS = [/generated with .*claude/i, /co-?authored by .*(claude|copilot|chatgpt)/i, /🤖/u];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} message the full commit message
|
|
30
|
+
* @param {{tools?: string[], footers?: RegExp[]}} [options]
|
|
31
|
+
* @returns {{path: string, rule: string, detail: string}[]}
|
|
32
|
+
*/
|
|
33
|
+
export function checkCommitAttribution(message, options = {}) {
|
|
34
|
+
const tools = (options.tools ?? DEFAULT_TOOLS).map((name) => name.toLowerCase());
|
|
35
|
+
const footers = options.footers ?? DEFAULT_FOOTERS;
|
|
36
|
+
const problems = [];
|
|
37
|
+
|
|
38
|
+
message.split("\n").forEach((line, index) => {
|
|
39
|
+
const trailer = TRAILER.exec(line);
|
|
40
|
+
if (trailer) {
|
|
41
|
+
const value = trailer[1].toLowerCase();
|
|
42
|
+
const tool = tools.find((name) => value.includes(name));
|
|
43
|
+
if (tool) {
|
|
44
|
+
problems.push({
|
|
45
|
+
path: "COMMIT_MSG",
|
|
46
|
+
rule: "tool-as-author",
|
|
47
|
+
detail: `line ${index + 1} credits "${trailer[1].trim()}". A tool is not an author — the committer is accountable for the change.`,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (footers.some((pattern) => pattern.test(line))) {
|
|
53
|
+
problems.push({
|
|
54
|
+
path: "COMMIT_MSG",
|
|
55
|
+
rule: "tool-footer",
|
|
56
|
+
detail: `line ${index + 1} advertises the tool that wrote the change. The message says what changed and why, nothing else.`,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return problems;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Strips every line the check would reject, for `--fix` and for rewriting an unpushed branch. */
|
|
64
|
+
export function withoutAttribution(message, options = {}) {
|
|
65
|
+
const rejected = new Set(checkCommitAttribution(message, options).map((problem) => problem.detail.match(/^line (\d+)/)[1]));
|
|
66
|
+
const kept = message.split("\n").filter((_, index) => !rejected.has(String(index + 1)));
|
|
67
|
+
return `${kept.join("\n").replace(/\n{3,}/g, "\n\n").trimEnd()}\n`;
|
|
68
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { checkCommitAttribution, withoutAttribution } from "./commit-attribution.mjs";
|
|
4
|
+
|
|
5
|
+
const plain = "fix(photos): the timeline rests groups below the sticky header\n\nThe seek landed under it.\n";
|
|
6
|
+
|
|
7
|
+
test("an ordinary message passes", () => {
|
|
8
|
+
assert.deepEqual(checkCommitAttribution(plain), []);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("a Claude co-author trailer is refused", () => {
|
|
12
|
+
const [problem] = checkCommitAttribution(`${plain}\nCo-Authored-By: Claude Opus 5 <noreply@anthropic.com>\n`);
|
|
13
|
+
assert.equal(problem.rule, "tool-as-author");
|
|
14
|
+
assert.match(problem.detail, /A tool is not an author/);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("other assistants are refused by the same rule", () => {
|
|
18
|
+
for (const tool of ["Copilot <bot@github.com>", "ChatGPT <x@openai.com>", "Cursor <a@cursor.sh>", "Devin <d@cognition.ai>"]) {
|
|
19
|
+
assert.equal(checkCommitAttribution(`${plain}\nCo-Authored-By: ${tool}\n`).length, 1, tool);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("a human co-author is never refused", () => {
|
|
24
|
+
assert.deepEqual(checkCommitAttribution(`${plain}\nCo-Authored-By: Hieu Nguyen <hieu.nguyen@arent3d.com>\n`), []);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("the marketing footer is refused too, trailer or not", () => {
|
|
28
|
+
const [problem] = checkCommitAttribution(`${plain}\n🤖 Generated with Claude Code\n`);
|
|
29
|
+
assert.equal(problem.rule, "tool-footer");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("stripping leaves the message intact and the trailers gone", () => {
|
|
33
|
+
const dirty = `${plain}\nCo-Authored-By: Claude <noreply@anthropic.com>\n🤖 Generated with Claude Code\n`;
|
|
34
|
+
const clean = withoutAttribution(dirty);
|
|
35
|
+
assert.deepEqual(checkCommitAttribution(clean), []);
|
|
36
|
+
assert.match(clean, /timeline rests groups/);
|
|
37
|
+
assert.doesNotMatch(clean, /Claude/);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("stripping a message that needs nothing changes nothing but the trailing newline", () => {
|
|
41
|
+
assert.equal(withoutAttribution(plain).trim(), plain.trim());
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("a Signed-off-by from a person survives, from a tool does not", () => {
|
|
45
|
+
assert.deepEqual(checkCommitAttribution(`${plain}\nSigned-off-by: Hieu <h@arent3d.com>\n`), []);
|
|
46
|
+
assert.equal(checkCommitAttribution(`${plain}\nSigned-off-by: Claude <noreply@anthropic.com>\n`).length, 1);
|
|
47
|
+
});
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// `qc decisions` — where every id is cited, and how to renumber one without breaking a citation.
|
|
2
|
+
|
|
3
|
+
import { execFile } from "node:child_process";
|
|
4
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { promisify } from "node:util";
|
|
7
|
+
import { definedIds } from "../gates/citations.mjs";
|
|
8
|
+
import { gateDescriptions } from "../descriptions.mjs";
|
|
9
|
+
import { rules as lintRules } from "../eslint/index.mjs";
|
|
10
|
+
import { enabled } from "../config.mjs";
|
|
11
|
+
import {
|
|
12
|
+
audit,
|
|
13
|
+
citationsIn,
|
|
14
|
+
isBareId,
|
|
15
|
+
isImmutable,
|
|
16
|
+
parseRegister,
|
|
17
|
+
planProblems,
|
|
18
|
+
registerProblems,
|
|
19
|
+
renamePlan,
|
|
20
|
+
rewrite,
|
|
21
|
+
squashPlan,
|
|
22
|
+
} from "../decisions.mjs";
|
|
23
|
+
|
|
24
|
+
const run = promisify(execFile);
|
|
25
|
+
|
|
26
|
+
// Generated output is rewritten from its own input, so a rename there is undone by the next run.
|
|
27
|
+
const GENERATED = /\.generated\.[^.]+$/;
|
|
28
|
+
const BINARY = /\.(png|jpe?g|gif|webp|ico|pdf|zip|woff2?|ttf|lock)$/i;
|
|
29
|
+
|
|
30
|
+
export const USAGE = `qc decisions — where every decision id is cited, and how to renumber one
|
|
31
|
+
|
|
32
|
+
qc decisions what is cited where, and what is wrong with the register
|
|
33
|
+
qc decisions ADR-0054 every citation of one id, with its line
|
|
34
|
+
qc decisions --check exit 1 on a gap, an uncited id, or an undefined one
|
|
35
|
+
qc decisions --audit which decisions earn their id, which merge, which are already a check
|
|
36
|
+
qc decisions --squash close every gap: rewrite the register and every citation
|
|
37
|
+
qc decisions --rename A=B,C=D
|
|
38
|
+
|
|
39
|
+
--dry-run print what would change and write nothing
|
|
40
|
+
--force run against a dirty tree`;
|
|
41
|
+
|
|
42
|
+
// Tracked and untracked-but-not-ignored, so this sees what the citations gate sees.
|
|
43
|
+
async function tracked(root) {
|
|
44
|
+
const args = ["ls-files", "-z", "--cached", "--others", "--exclude-standard"];
|
|
45
|
+
const { stdout } = await run("git", args, { cwd: root, maxBuffer: 64 * 1024 * 1024 });
|
|
46
|
+
return [...new Set(stdout.split("\0"))].filter((name) => name !== "" && !BINARY.test(name));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function scan(config) {
|
|
50
|
+
const prefixes = config.citations.prefixes;
|
|
51
|
+
const exclude = config.decisions.exclude ?? [];
|
|
52
|
+
const sites = new Map();
|
|
53
|
+
const generated = new Map();
|
|
54
|
+
for (const file of await tracked(config.root)) {
|
|
55
|
+
// A vendored tool cites its own upstream register, never this repository's.
|
|
56
|
+
if (exclude.some((source) => new RegExp(source).test(file))) continue;
|
|
57
|
+
const contents = await readFile(path.join(config.root, file), "utf8").catch(() => null);
|
|
58
|
+
if (contents === null) continue;
|
|
59
|
+
const target = GENERATED.test(file) ? generated : sites;
|
|
60
|
+
for (const found of citationsIn(contents, prefixes)) {
|
|
61
|
+
const at = target.get(found.id) ?? [];
|
|
62
|
+
at.push({ file, line: found.line, text: found.text });
|
|
63
|
+
target.set(found.id, at);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const register = await readFile(path.join(config.root, config.docs.decisions), "utf8");
|
|
67
|
+
return { sites, generated, defined: definedIds(register, { prefixes }), rows: parseRegister(register, prefixes) };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function apply(config, plan, sites) {
|
|
71
|
+
const cited = new Set();
|
|
72
|
+
for (const [from] of plan) for (const site of sites.get(from) ?? []) cited.add(site.file);
|
|
73
|
+
const immutable = config.decisions.immutable;
|
|
74
|
+
const refused = [...cited].filter((file) => isImmutable(file, immutable)).sort();
|
|
75
|
+
const touched = new Set([...cited].filter((file) => !isImmutable(file, immutable)));
|
|
76
|
+
for (const file of [...touched].sort()) {
|
|
77
|
+
const full = path.join(config.root, file);
|
|
78
|
+
await writeFile(full, rewrite(await readFile(full, "utf8"), plan));
|
|
79
|
+
}
|
|
80
|
+
return { touched, refused };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// A hashed file's bytes are its identity, so the stale id stays and a person decides what to do about it.
|
|
84
|
+
function reportRefused(refused) {
|
|
85
|
+
if (refused.length === 0) return;
|
|
86
|
+
console.log(`\n ${refused.length} content-addressed file(s) cite a renumbered id and were NOT rewritten:`);
|
|
87
|
+
for (const file of refused) console.log(` ${file}`);
|
|
88
|
+
console.log(" Editing one invalidates the checksum its ledger recorded. Leave the id, or re-ledger deliberately.");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function usesOf(sites, id, decisionsPath) {
|
|
92
|
+
return (sites.get(id) ?? []).filter((site) => site.file !== decisionsPath);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const VERDICT = {
|
|
96
|
+
keep: "cited across more than one area",
|
|
97
|
+
"local?": "one area cites it — that area's doc may be its home",
|
|
98
|
+
"inline?": "one file cites it — that file's own line may be its home",
|
|
99
|
+
"abort?": "nothing cites it",
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
function checksOf(config) {
|
|
103
|
+
return [
|
|
104
|
+
...Object.entries(lintRules)
|
|
105
|
+
.filter(([name]) => enabled(config.rules, name))
|
|
106
|
+
.map(([name, rule]) => ({ name: `qc/${name}`, description: rule.meta.docs.description })),
|
|
107
|
+
...Object.keys(config.gates)
|
|
108
|
+
.filter((name) => enabled(config.gates, name))
|
|
109
|
+
.map((name) => ({ name: `qc check (${name})`, description: gateDescriptions[name] })),
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function printAudit(rows, sites, decisionsPath, config) {
|
|
114
|
+
const { verdicts, merges, checked } = audit(rows, sites, decisionsPath, checksOf(config));
|
|
115
|
+
const order = ["abort?", "inline?", "local?", "keep"];
|
|
116
|
+
console.log(`${rows.length} decision(s)\n`);
|
|
117
|
+
for (const name of order) {
|
|
118
|
+
const group = verdicts.filter((entry) => entry.verdict === name);
|
|
119
|
+
if (group.length === 0) continue;
|
|
120
|
+
console.log(`${name} ${VERDICT[name]}`);
|
|
121
|
+
for (const entry of group) {
|
|
122
|
+
const where = entry.areas.length > 0 ? entry.areas.slice(0, 4).join(", ") : "—";
|
|
123
|
+
console.log(` ${entry.id} ${String(entry.files).padStart(4)} file(s) ${where}`);
|
|
124
|
+
console.log(` ${entry.decision.slice(0, 96)}`);
|
|
125
|
+
}
|
|
126
|
+
console.log("");
|
|
127
|
+
}
|
|
128
|
+
if (merges.length > 0) {
|
|
129
|
+
console.log("merge? two decisions saying much the same thing");
|
|
130
|
+
for (const pair of merges) {
|
|
131
|
+
console.log(` ${pair.left} + ${pair.right} ${(pair.score * 100).toFixed(0)}% ${pair.shared.slice(0, 8).join(" ")}`);
|
|
132
|
+
}
|
|
133
|
+
console.log("");
|
|
134
|
+
}
|
|
135
|
+
if (checked.length > 0) {
|
|
136
|
+
console.log("already a check the rule is stated here and in the generated enforcement map");
|
|
137
|
+
for (const hit of checked) console.log(` ${hit.id} ${hit.check} ${(hit.score * 100).toFixed(0)}%`);
|
|
138
|
+
console.log("");
|
|
139
|
+
}
|
|
140
|
+
console.log("Every verdict is a prompt, not a finding. A decision enforced by absence is cited by nothing.");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function runDecisions(config, args) {
|
|
144
|
+
const flag = (name) => args.includes(name);
|
|
145
|
+
const value = (name) => (args.indexOf(name) === -1 ? undefined : args[args.indexOf(name) + 1]);
|
|
146
|
+
if (flag("--help")) {
|
|
147
|
+
console.log(USAGE);
|
|
148
|
+
return 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const { sites, generated, defined, rows } = await scan(config);
|
|
152
|
+
const decisionsPath = config.docs.decisions;
|
|
153
|
+
|
|
154
|
+
if (flag("--audit")) {
|
|
155
|
+
printAudit(rows, sites, decisionsPath, config);
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const one = args.find((arg) => isBareId(arg, config.citations.prefixes));
|
|
160
|
+
if (one) {
|
|
161
|
+
const uses = sites.get(one) ?? [];
|
|
162
|
+
console.log(`${one} — ${uses.length} citation(s)${defined.has(one) ? "" : " [NOT DEFINED]"}`);
|
|
163
|
+
for (const site of uses) console.log(` ${site.file}:${site.line} ${site.text.slice(0, 110)}`);
|
|
164
|
+
for (const site of generated.get(one) ?? []) console.log(` (generated) ${site.file}:${site.line}`);
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const plan = flag("--squash")
|
|
169
|
+
? squashPlan(defined)
|
|
170
|
+
: value("--rename")
|
|
171
|
+
? renamePlan(value("--rename"))
|
|
172
|
+
: null;
|
|
173
|
+
|
|
174
|
+
if (plan === null) {
|
|
175
|
+
const problems = registerProblems(defined, sites, decisionsPath);
|
|
176
|
+
const rows = [...defined]
|
|
177
|
+
.map((id) => ({ id, uses: usesOf(sites, id, decisionsPath).length }))
|
|
178
|
+
.sort((a, b) => b.uses - a.uses || a.id.localeCompare(b.id));
|
|
179
|
+
console.log(`${defined.size} decision(s), ${rows.reduce((sum, row) => sum + row.uses, 0)} citation(s)\n`);
|
|
180
|
+
for (const row of rows) console.log(` ${row.id} ${String(row.uses).padStart(4)}`);
|
|
181
|
+
if (problems.length > 0) {
|
|
182
|
+
console.log("");
|
|
183
|
+
for (const problem of problems) console.log(` ${problem.fatal ? "!" : "-"} ${problem.text}`);
|
|
184
|
+
}
|
|
185
|
+
return flag("--check") && problems.some((problem) => problem.fatal) ? 1 : 0;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const refusals = planProblems(plan, defined);
|
|
189
|
+
if (refusals.length > 0) {
|
|
190
|
+
for (const refusal of refusals) console.error(`FAIL decisions ${refusal}`);
|
|
191
|
+
return 1;
|
|
192
|
+
}
|
|
193
|
+
if (plan.size === 0) {
|
|
194
|
+
console.log("OK decisions no gaps to close");
|
|
195
|
+
return 0;
|
|
196
|
+
}
|
|
197
|
+
if (!flag("--force") && !flag("--dry-run")) {
|
|
198
|
+
const { stdout } = await run("git", ["status", "--porcelain"], { cwd: config.root });
|
|
199
|
+
if (stdout.trim() !== "") {
|
|
200
|
+
console.error("FAIL decisions dirty tree. This touches many files at once; commit or stash");
|
|
201
|
+
console.error(" first so it stays one revertible change. --force overrides.");
|
|
202
|
+
return 1;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
for (const [from, to] of plan) console.log(` ${from} -> ${to} ${(sites.get(from) ?? []).length} citation(s)`);
|
|
207
|
+
const stale = [...plan.keys()].filter((from) => generated.has(from));
|
|
208
|
+
if (stale.length > 0) console.log(`\n also in generated output: ${stale.join(", ")} — regenerate after this`);
|
|
209
|
+
|
|
210
|
+
if (flag("--dry-run")) {
|
|
211
|
+
const files = new Set();
|
|
212
|
+
for (const [from] of plan) for (const site of sites.get(from) ?? []) files.add(site.file);
|
|
213
|
+
const held = [...files].filter((file) => isImmutable(file, config.decisions.immutable));
|
|
214
|
+
console.log(`\n ${files.size - held.length} file(s) would change. Nothing written.`);
|
|
215
|
+
reportRefused(held);
|
|
216
|
+
return 0;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const { touched, refused } = await apply(config, plan, sites);
|
|
220
|
+
console.log(`\nOK decisions ${plan.size} id(s) renumbered across ${touched.size} file(s)`);
|
|
221
|
+
reportRefused(refused);
|
|
222
|
+
return 0;
|
|
223
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// The harness runs from two places at once: the plugin's hooks execute a checkout of this kit,
|
|
2
|
+
// while `pnpm check` and CI execute the copy the lockfile pins. When those disagree, the hooks
|
|
3
|
+
// judge a repository by a rulebook it was never built against, and every failure names the
|
|
4
|
+
// repository's own files -- so the obvious reading is that the repository is broken. It is not.
|
|
5
|
+
//
|
|
6
|
+
// This names the disagreement instead, which is the only cheap moment to catch it.
|
|
7
|
+
|
|
8
|
+
import { execFile } from "node:child_process";
|
|
9
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { promisify } from "node:util";
|
|
12
|
+
|
|
13
|
+
const execFileAsync = promisify(execFile);
|
|
14
|
+
|
|
15
|
+
/** @returns the gate names a harness copy ships, or null when there is no such copy. */
|
|
16
|
+
export function gateNames(harnessRoot, read = readdirSync) {
|
|
17
|
+
let entries;
|
|
18
|
+
try {
|
|
19
|
+
entries = read(path.join(harnessRoot, "src", "gates"));
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return entries
|
|
24
|
+
.filter((name) => name.endsWith(".mjs") && !name.endsWith(".test.mjs"))
|
|
25
|
+
.map((name) => name.replace(/\.mjs$/, ""))
|
|
26
|
+
.sort();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function versionOf(harnessRoot) {
|
|
30
|
+
const file = path.join(harnessRoot, "package.json");
|
|
31
|
+
if (!existsSync(file)) return null;
|
|
32
|
+
return JSON.parse(readFileSync(file, "utf8")).version ?? null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The decision, as data. Direction is the whole point: a plugin copy *behind* the lockfile judges
|
|
37
|
+
* a repository by a rulebook it was never built against, and its failures name the repository's
|
|
38
|
+
* own files -- that verdict is worthless and has to stop the turn. A copy *ahead* is what every
|
|
39
|
+
* day of developing this kit looks like; it is merely stricter, and anything it reports is a real
|
|
40
|
+
* finding to act on. Blocking on that direction would make the kit unable to grow.
|
|
41
|
+
* @returns null when the two agree, else a report carrying `blocking`.
|
|
42
|
+
*/
|
|
43
|
+
export function drift(installed, plugin) {
|
|
44
|
+
if (!plugin || !installed) return null;
|
|
45
|
+
const missing = installed.gates.filter((name) => !plugin.gates.includes(name));
|
|
46
|
+
const extra = plugin.gates.filter((name) => !installed.gates.includes(name));
|
|
47
|
+
if (missing.length === 0 && extra.length === 0 && installed.version === plugin.version) return null;
|
|
48
|
+
return {
|
|
49
|
+
missing,
|
|
50
|
+
extra,
|
|
51
|
+
blocking: missing.length > 0,
|
|
52
|
+
installedVersion: installed.version,
|
|
53
|
+
pluginVersion: plugin.version,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function driftReport(found, pluginRoot, checkoutRoot = pluginRoot) {
|
|
58
|
+
const label = found.blocking ? "FAIL harness " : "WARN harness ";
|
|
59
|
+
const lines = [
|
|
60
|
+
`${label} the hooks and the lockfile run different harnesses`,
|
|
61
|
+
` installed ${found.installedVersion ?? "?"} — what \`pnpm check\` and CI enforce`,
|
|
62
|
+
` plugin ${found.pluginVersion ?? "?"} — what the agent hooks enforce, from ${pluginRoot}`,
|
|
63
|
+
];
|
|
64
|
+
if (found.missing.length > 0) {
|
|
65
|
+
lines.push(` the plugin copy is missing: ${found.missing.join(", ")}`);
|
|
66
|
+
lines.push(` so its \`qc check\` reports unknown-check for rules your docs correctly list`);
|
|
67
|
+
lines.push(` fix the checkout, never the repository: git -C ${checkoutRoot} log --oneline -1`);
|
|
68
|
+
}
|
|
69
|
+
if (found.extra.length > 0) {
|
|
70
|
+
lines.push(` the plugin copy adds: ${found.extra.join(", ")}`);
|
|
71
|
+
lines.push(` it is ahead, not stale — anything it reports is real. Bump the lockfile to ship it.`);
|
|
72
|
+
}
|
|
73
|
+
return lines.join("\n");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @returns the branch position of a checkout against its own remote, or null when unavailable. */
|
|
77
|
+
async function behindMain(root) {
|
|
78
|
+
try {
|
|
79
|
+
const { stdout } = await execFileAsync("git", ["rev-list", "--left-right", "--count", "origin/HEAD...HEAD"], {
|
|
80
|
+
cwd: root,
|
|
81
|
+
});
|
|
82
|
+
const [behind, ahead] = stdout.trim().split(/\s+/).map(Number);
|
|
83
|
+
return { behind, ahead };
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function pluginHarnessRoot() {
|
|
90
|
+
const root = process.env.CLAUDE_PLUGIN_ROOT;
|
|
91
|
+
if (!root) return null;
|
|
92
|
+
const harness = path.join(root, "packages", "qc-harness");
|
|
93
|
+
return existsSync(harness) ? harness : null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function installedHarnessRoot(configRoot) {
|
|
97
|
+
const dir = path.join(configRoot, "node_modules", "architecture-harness");
|
|
98
|
+
return existsSync(dir) ? dir : null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function runDoctor(config) {
|
|
102
|
+
const installedRoot = installedHarnessRoot(config.root);
|
|
103
|
+
const pluginRoot = pluginHarnessRoot();
|
|
104
|
+
|
|
105
|
+
if (!installedRoot) {
|
|
106
|
+
console.log("SKIP harness no architecture-harness in node_modules — nothing to compare");
|
|
107
|
+
return 0;
|
|
108
|
+
}
|
|
109
|
+
const installed = { gates: gateNames(installedRoot), version: versionOf(installedRoot) };
|
|
110
|
+
console.log(`OK installed architecture-harness ${installed.version} — ${installed.gates.length} gate(s)`);
|
|
111
|
+
|
|
112
|
+
if (!pluginRoot) {
|
|
113
|
+
console.log("SKIP plugin CLAUDE_PLUGIN_ROOT is unset — run this from an agent session to compare");
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
const plugin = { gates: gateNames(pluginRoot), version: versionOf(pluginRoot) };
|
|
117
|
+
const checkoutRoot = path.resolve(pluginRoot, "..", "..");
|
|
118
|
+
const position = await behindMain(checkoutRoot);
|
|
119
|
+
const found = drift(installed, plugin);
|
|
120
|
+
if (!found) {
|
|
121
|
+
console.log(`OK plugin the same ${plugin.gates.length} gate(s) as the lockfile pins`);
|
|
122
|
+
return 0;
|
|
123
|
+
}
|
|
124
|
+
const report = driftReport(found, pluginRoot, checkoutRoot);
|
|
125
|
+
const note =
|
|
126
|
+
position && position.behind > 0
|
|
127
|
+
? `\n that checkout is ${position.behind} commit(s) behind its origin, ${position.ahead} ahead`
|
|
128
|
+
: "";
|
|
129
|
+
if (!found.blocking) {
|
|
130
|
+
console.log(`${report}${note}`);
|
|
131
|
+
return 0;
|
|
132
|
+
}
|
|
133
|
+
console.error(`${report}${note}`);
|
|
134
|
+
return 1;
|
|
135
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { drift, driftReport, gateNames } from "./doctor.mjs";
|
|
4
|
+
|
|
5
|
+
const installed = { gates: ["citations", "comment-style", "headless-sagas", "test-mirror"], version: "0.3.0" };
|
|
6
|
+
|
|
7
|
+
test("two copies shipping the same gates at the same version are not drift", () => {
|
|
8
|
+
assert.equal(drift(installed, { ...installed }), null);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("a plugin copy missing gates the lockfile ships is drift, and names them", () => {
|
|
12
|
+
const found = drift(installed, { gates: ["citations", "headless-sagas"], version: "0.3.0" });
|
|
13
|
+
assert.deepEqual(found.missing, ["comment-style", "test-mirror"]);
|
|
14
|
+
assert.deepEqual(found.extra, []);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("the report blames the checkout, never the repository", () => {
|
|
18
|
+
const found = drift(installed, { gates: ["citations"], version: "0.2.0" });
|
|
19
|
+
const report = driftReport(found, "/home/x/kit/packages/qc-harness", "/home/x/kit");
|
|
20
|
+
assert.match(report, /unknown-check for rules your docs correctly list/);
|
|
21
|
+
assert.match(report, /fix the checkout, never the repository: git -C \/home\/x\/kit /);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("a checkout behind the lockfile blocks, because its verdict is about the wrong rulebook", () => {
|
|
25
|
+
const found = drift(installed, { gates: ["citations"], version: "0.3.0" });
|
|
26
|
+
assert.equal(found.blocking, true);
|
|
27
|
+
assert.match(driftReport(found, "/k"), /^FAIL/);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("a checkout ahead does not block: developing the kit is not a broken repository", () => {
|
|
31
|
+
const found = drift(installed, { gates: [...installed.gates, "new-gate"], version: "0.3.0" });
|
|
32
|
+
assert.equal(found.blocking, false);
|
|
33
|
+
const report = driftReport(found, "/k");
|
|
34
|
+
assert.match(report, /^WARN/);
|
|
35
|
+
assert.match(report, /ahead, not stale/);
|
|
36
|
+
assert.doesNotMatch(report, /fix the checkout/);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("a version difference alone is drift, even with identical gates", () => {
|
|
40
|
+
assert.ok(drift(installed, { ...installed, version: "0.2.0" }));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("a plugin copy carrying an unreleased gate is reported as extra, not missing", () => {
|
|
44
|
+
const found = drift(installed, { gates: [...installed.gates, "new-gate"], version: "0.3.0" });
|
|
45
|
+
assert.deepEqual(found.extra, ["new-gate"]);
|
|
46
|
+
assert.deepEqual(found.missing, []);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("nothing to compare is not a failure", () => {
|
|
50
|
+
assert.equal(drift(installed, null), null);
|
|
51
|
+
assert.equal(drift(null, installed), null);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("gate names come from the directory, with the test files excluded", () => {
|
|
55
|
+
const read = () => ["citations.mjs", "citations.test.mjs", "headless-sagas.mjs", "README.md"];
|
|
56
|
+
assert.deepEqual(gateNames(process.cwd(), read), ["citations", "headless-sagas"]);
|
|
57
|
+
});
|