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.
Files changed (109) hide show
  1. package/package.json +80 -0
  2. package/src/cli/check.mjs +535 -0
  3. package/src/cli/cli.test.mjs +120 -0
  4. package/src/cli/commit-attribution.mjs +68 -0
  5. package/src/cli/commit-attribution.test.mjs +47 -0
  6. package/src/cli/decisions.mjs +223 -0
  7. package/src/cli/doctor.mjs +135 -0
  8. package/src/cli/doctor.test.mjs +57 -0
  9. package/src/cli/init.mjs +98 -0
  10. package/src/cli/install-hooks.mjs +26 -0
  11. package/src/cli/qc.mjs +89 -0
  12. package/src/cli/scaffold.mjs +194 -0
  13. package/src/cli/sync-enforcement-map.mjs +28 -0
  14. package/src/cli/work-order-guard.mjs +0 -0
  15. package/src/cli/work-order-guard.test.mjs +79 -0
  16. package/src/cli/worktree-isolation.mjs +94 -0
  17. package/src/cli/worktree-isolation.test.mjs +82 -0
  18. package/src/config.mjs +338 -0
  19. package/src/config.test.mjs +59 -0
  20. package/src/decisions.mjs +198 -0
  21. package/src/decisions.test.mjs +183 -0
  22. package/src/descriptions.mjs +28 -0
  23. package/src/descriptions.test.mjs +27 -0
  24. package/src/enforcement-map.mjs +46 -0
  25. package/src/enforcement-map.test.mjs +60 -0
  26. package/src/eslint/index.mjs +37 -0
  27. package/src/eslint/options.mjs +53 -0
  28. package/src/eslint/preset.mjs +192 -0
  29. package/src/eslint/preset.test.mjs +96 -0
  30. package/src/eslint/rules/durable-idempotency-key.mjs +120 -0
  31. package/src/eslint/rules/external-service-only-in-adapter.mjs +52 -0
  32. package/src/eslint/rules/module-boundary.mjs +12 -0
  33. package/src/eslint/rules/name-the-pattern.mjs +111 -0
  34. package/src/eslint/rules/no-comment-paragraph.mjs +101 -0
  35. package/src/eslint/rules/no-cross-feature-internals.mjs +57 -0
  36. package/src/eslint/rules/no-number-in-comment.mjs +89 -0
  37. package/src/eslint/rules/no-offset-pagination.mjs +29 -0
  38. package/src/eslint/rules/no-orchestration-in-trigger.mjs +56 -0
  39. package/src/eslint/rules/no-promise-then.mjs +43 -0
  40. package/src/eslint/rules/no-promise-then.test.mjs +63 -0
  41. package/src/eslint/rules/no-raw-fetch.mjs +27 -0
  42. package/src/eslint/rules/no-status-literal.mjs +34 -0
  43. package/src/eslint/rules/no-supersession-trail.mjs +51 -0
  44. package/src/eslint/rules/scoped-repository.mjs +43 -0
  45. package/src/eslint/rules/signal-last-param.mjs +43 -0
  46. package/src/eslint/rules/storage-only-in-resource.mjs +52 -0
  47. package/src/eslint/rules/tenant-scoped-table.mjs +71 -0
  48. package/src/eslint/rules.test.mjs +627 -0
  49. package/src/gates/adr-format.mjs +87 -0
  50. package/src/gates/adr-format.test.mjs +54 -0
  51. package/src/gates/audit-append-only.mjs +34 -0
  52. package/src/gates/audit-append-only.test.mjs +36 -0
  53. package/src/gates/citations.mjs +93 -0
  54. package/src/gates/citations.test.mjs +115 -0
  55. package/src/gates/claimed-requirements.mjs +53 -0
  56. package/src/gates/claimed-requirements.test.mjs +60 -0
  57. package/src/gates/comment-style.mjs +76 -0
  58. package/src/gates/comment-style.test.mjs +46 -0
  59. package/src/gates/config-floor.mjs +68 -0
  60. package/src/gates/config-floor.test.mjs +52 -0
  61. package/src/gates/contract-compose.mjs +38 -0
  62. package/src/gates/contract-compose.test.mjs +40 -0
  63. package/src/gates/eight-blocks.mjs +223 -0
  64. package/src/gates/eight-blocks.test.mjs +359 -0
  65. package/src/gates/enforcement-map.mjs +62 -0
  66. package/src/gates/enforcement-map.test.mjs +41 -0
  67. package/src/gates/english-source.mjs +176 -0
  68. package/src/gates/english-source.test.mjs +122 -0
  69. package/src/gates/frontend-boundaries.mjs +154 -0
  70. package/src/gates/frontend-boundaries.test.mjs +72 -0
  71. package/src/gates/gate-tests.mjs +44 -0
  72. package/src/gates/gate-tests.test.mjs +41 -0
  73. package/src/gates/headless-sagas.mjs +54 -0
  74. package/src/gates/headless-sagas.test.mjs +71 -0
  75. package/src/gates/internal-routes.mjs +76 -0
  76. package/src/gates/internal-routes.test.mjs +53 -0
  77. package/src/gates/public-routes.mjs +67 -0
  78. package/src/gates/public-routes.test.mjs +38 -0
  79. package/src/gates/registry-agreement.mjs +37 -0
  80. package/src/gates/registry-agreement.test.mjs +36 -0
  81. package/src/gates/saga-tests.mjs +94 -0
  82. package/src/gates/saga-tests.test.mjs +92 -0
  83. package/src/gates/sql-identifiers.mjs +244 -0
  84. package/src/gates/sql-identifiers.test.mjs +197 -0
  85. package/src/gates/tenant-predicate.mjs +175 -0
  86. package/src/gates/tenant-predicate.test.mjs +161 -0
  87. package/src/gates/tenant-tables.mjs +65 -0
  88. package/src/gates/tenant-tables.test.mjs +56 -0
  89. package/src/gates/test-mirror.mjs +47 -0
  90. package/src/gates/test-mirror.test.mjs +31 -0
  91. package/src/index.mjs +9 -0
  92. package/templates/.jscpd.json +15 -0
  93. package/templates/CLAUDE.md +54 -0
  94. package/templates/decisions.examples.md +25 -0
  95. package/templates/docs/adr-template.md +58 -0
  96. package/templates/docs/architecture.md +179 -0
  97. package/templates/docs/decisions.md +72 -0
  98. package/templates/docs/enforcement.md +94 -0
  99. package/templates/docs/glossary.md +16 -0
  100. package/templates/docs/guards.md +23 -0
  101. package/templates/docs/pagination.md +113 -0
  102. package/templates/docs/performance.md +35 -0
  103. package/templates/docs/ui.md +32 -0
  104. package/templates/githooks/commit-msg +4 -0
  105. package/templates/githooks/pre-commit +18 -0
  106. package/templates/githooks/pre-push +6 -0
  107. package/templates/github/workflows/ci.yml +21 -0
  108. package/templates/qc.config.json +6 -0
  109. package/templates/quality-thresholds.json +114 -0
package/package.json ADDED
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "architecture-harness",
3
+ "version": "0.4.0",
4
+ "type": "module",
5
+ "description": "Architecture gates, lint rules and scaffolding for the bounded-vertical-slice architecture",
6
+ "license": "MIT",
7
+ "author": "shawrylk",
8
+ "homepage": "https://github.com/shawrylk/architecture-kit#readme",
9
+ "bugs": {
10
+ "url": "https://github.com/shawrylk/architecture-kit/issues"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/shawrylk/architecture-kit.git",
15
+ "directory": "packages/qc-harness"
16
+ },
17
+ "keywords": [
18
+ "architecture",
19
+ "eslint-plugin",
20
+ "vertical-slice",
21
+ "hexagonal",
22
+ "adr",
23
+ "multi-tenant",
24
+ "code-quality",
25
+ "claude-code",
26
+ "ai-agents"
27
+ ],
28
+ "engines": {
29
+ "node": ">=22"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "bin": {
35
+ "qc": "./src/cli/qc.mjs"
36
+ },
37
+ "exports": {
38
+ ".": "./src/index.mjs",
39
+ "./eslint": "./src/eslint/index.mjs",
40
+ "./preset": "./src/eslint/preset.mjs",
41
+ "./config": "./src/config.mjs",
42
+ "./gates/*": "./src/gates/*.mjs"
43
+ },
44
+ "files": [
45
+ "src",
46
+ "templates",
47
+ "README.md"
48
+ ],
49
+ "scripts": {
50
+ "test": "node src/eslint/rules.test.mjs && node --test \"src/**/*.test.mjs\""
51
+ },
52
+ "peerDependencies": {
53
+ "eslint": ">=9",
54
+ "@typescript-eslint/eslint-plugin": ">=8",
55
+ "@typescript-eslint/parser": ">=8",
56
+ "eslint-plugin-boundaries": ">=5",
57
+ "eslint-plugin-sonarjs": ">=3"
58
+ },
59
+ "peerDependenciesMeta": {
60
+ "@typescript-eslint/eslint-plugin": {
61
+ "optional": true
62
+ },
63
+ "@typescript-eslint/parser": {
64
+ "optional": true
65
+ },
66
+ "eslint-plugin-boundaries": {
67
+ "optional": true
68
+ },
69
+ "eslint-plugin-sonarjs": {
70
+ "optional": true
71
+ }
72
+ },
73
+ "devDependencies": {
74
+ "eslint": "^9",
75
+ "@typescript-eslint/eslint-plugin": "^8",
76
+ "@typescript-eslint/parser": "^8",
77
+ "eslint-plugin-boundaries": "^5",
78
+ "eslint-plugin-sonarjs": "^3"
79
+ }
80
+ }
@@ -0,0 +1,535 @@
1
+ // I/O only. Every decision is a pure function under src/gates/, each with a test
2
+ // beside it. ADR-0032. Where this file names a path, that path came from config.
3
+
4
+ import { readFile, readdir, stat } from "node:fs/promises";
5
+ import path from "node:path";
6
+ import { checkFeatureAnatomy, checkNoEmptyBlock } from "../gates/eight-blocks.mjs";
7
+ import { checkCitations, checkDocPaths, checkSelfContained, definedIds } from "../gates/citations.mjs";
8
+ import { checkAgreement, declaredValues } from "../gates/registry-agreement.mjs";
9
+ import {
10
+ checkSqlIdentifiers,
11
+ declaredColumns,
12
+ sqlStatements,
13
+ tableOwners,
14
+ } from "../gates/sql-identifiers.mjs";
15
+ import { checkSharedInsertCallSites, checkTenantPredicate, exemptions } from "../gates/tenant-predicate.mjs";
16
+ import { checkClaimedRequirements } from "../gates/claimed-requirements.mjs";
17
+ import { allowedPublicRoutes, checkPublicRoutes, declaredPublicRoutes } from "../gates/public-routes.mjs";
18
+ import { calledInternalRoutes, checkInternalRoutes, declaredInternalRoutes } from "../gates/internal-routes.mjs";
19
+ import { checkHeadlessPipelines } from "../gates/headless-sagas.mjs";
20
+ import { checkSagaTests, declaredSagas } from "../gates/saga-tests.mjs";
21
+ import { checkGatesAreTested } from "../gates/gate-tests.mjs";
22
+ import { checkAuditAppendOnly } from "../gates/audit-append-only.mjs";
23
+ import { checkEnforcementMap } from "../gates/enforcement-map.mjs";
24
+ import { checkFrontendBoundaries } from "../gates/frontend-boundaries.mjs";
25
+ import { checkTestMirror } from "../gates/test-mirror.mjs";
26
+ import { checkCommentStyle } from "../gates/comment-style.mjs";
27
+ import { checkEnglishSource, checkTranslationPairs } from "../gates/english-source.mjs";
28
+ import { checkAdrFormat } from "../gates/adr-format.mjs";
29
+ import { checkConfigFloor } from "../gates/config-floor.mjs";
30
+ import { defaults } from "../config.mjs";
31
+ import { rules as lintRules } from "../eslint/index.mjs";
32
+ import { enabled } from "../config.mjs";
33
+
34
+ const SKIP = /node_modules|\/dist\/|\.test\.[cm]?[jt]sx?$/;
35
+ const TEST_FILE = /\.test\.[cm]?[jt]sx?$/;
36
+ const SOURCE_EXTENSION = /\.([cm]?[jt]sx?)$/;
37
+ const CITABLE_EXTENSION = /\.(tsx?|mjs|md)$/;
38
+ const INFRA_EXTENSIONS = [".tf", ".sh", ".tfvars", ".tfvars.example", ".hcl", ".hcl.example"];
39
+ const INFRA_SKIP = [".lock.hcl"];
40
+
41
+ const read = (file) => readFile(file, "utf8").catch(() => null);
42
+ const list = (dir, options) => readdir(dir, options).catch(() => []);
43
+
44
+ /** A problem names a path a reader can open — forward slashes always, since every gate splits on "/". */
45
+ const relativeTo = (root) => (file) => (path.relative(root, file) || file).split(path.sep).join("/");
46
+
47
+ async function listFeature(dir) {
48
+ const files = [];
49
+ async function walk(current, prefix) {
50
+ for (const entry of await list(current, { withFileTypes: true })) {
51
+ const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
52
+ if (entry.isDirectory()) await walk(path.join(current, entry.name), rel);
53
+ else files.push(rel);
54
+ }
55
+ }
56
+ await walk(dir, "");
57
+ return files;
58
+ }
59
+
60
+ async function readFeature(dir) {
61
+ const files = await listFeature(dir);
62
+ const contents = [];
63
+ for (const file of files) {
64
+ const full = path.join(dir, file);
65
+ const isSource = /\.tsx?$/.test(file) && (await stat(full).catch(() => null))?.isFile();
66
+ if (isSource) contents.push({ path: full, contents: await readFile(full, "utf8") });
67
+
68
+ }
69
+ return { feature: { feature: dir, files }, contents };
70
+ }
71
+
72
+ async function featureDirs(root) {
73
+ const entries = await list(root, { withFileTypes: true });
74
+ return entries.filter((entry) => entry.isDirectory()).map((entry) => path.join(root, entry.name));
75
+ }
76
+
77
+ async function collect(roots, only) {
78
+ const features = [];
79
+ const contents = [];
80
+ for (const root of roots) {
81
+ for (const dir of await featureDirs(root)) {
82
+ if (only && !only.startsWith(dir)) continue;
83
+ const found = await readFeature(dir);
84
+ features.push(found.feature);
85
+ contents.push(...found.contents);
86
+ }
87
+ }
88
+ return { features, contents };
89
+ }
90
+
91
+ async function citableFiles(roots, root) {
92
+ const rel = relativeTo(root);
93
+ const files = [];
94
+ async function walk(dir) {
95
+ for (const entry of await list(dir, { withFileTypes: true })) {
96
+ const full = path.join(dir, entry.name);
97
+ if (SKIP.test(full)) continue;
98
+ if (entry.isDirectory()) await walk(full);
99
+ else if (CITABLE_EXTENSION.test(entry.name)) {
100
+ files.push({ path: rel(full), contents: await readFile(full, "utf8") });
101
+ }
102
+ }
103
+ }
104
+ for (const root of roots) await walk(root);
105
+ return files;
106
+ }
107
+
108
+ /** Every file under the citable roots whose extension matches, nothing skipped but build output. */
109
+ async function filesMatching(config, pattern, language) {
110
+ const rel = relativeTo(config.root);
111
+ const skip = new RegExp(language.exclude.join("|"));
112
+ const files = [];
113
+ async function walk(dir) {
114
+ for (const entry of await list(dir, { withFileTypes: true }).catch(() => [])) {
115
+ const full = path.join(dir, entry.name);
116
+ if (skip.test(rel(full))) continue;
117
+ if (entry.isDirectory()) await walk(full);
118
+ else if (pattern.test(entry.name)) files.push({ path: rel(full), contents: await readFile(full, "utf8") });
119
+ }
120
+ }
121
+ // The whole repository, not the citable roots: those name where decisions are cited, which has
122
+ // nothing to do with where a language rule applies. Contracts and infrastructure are not citable
123
+ // and were invisible because of it.
124
+ for (const root of language.roots) await walk(path.join(config.root, root));
125
+ return files;
126
+ }
127
+
128
+ /** Every source file under the citable roots, tests included. */
129
+ async function sourceFiles(config, only) {
130
+ const rel = relativeTo(config.root);
131
+ const roots = only === undefined ? config.paths.citable : [only];
132
+ const files = [];
133
+ async function walk(dir) {
134
+ for (const entry of await list(dir, { withFileTypes: true })) {
135
+ const full = path.join(dir, entry.name);
136
+ if (/node_modules|\/dist\//.test(full)) continue;
137
+ if (entry.isDirectory()) await walk(full);
138
+ else if (SOURCE_EXTENSION.test(entry.name)) {
139
+ files.push({ path: rel(full), contents: await readFile(full, "utf8") });
140
+ }
141
+ }
142
+ }
143
+ for (const root of roots) await walk(path.join(config.root, root));
144
+ return files;
145
+ }
146
+
147
+ async function registered(file) {
148
+ const source = await read(file);
149
+ if (source === null) return [];
150
+ try {
151
+ return JSON.parse(source).requirements ?? [];
152
+ } catch {
153
+ return [];
154
+ }
155
+ }
156
+
157
+ /** Where a tool demands a literal the registry already owns, assert the two agree. */
158
+ async function agreements(config) {
159
+ const problems = [];
160
+ for (const entry of config.agreements) {
161
+ const source = await read(path.join(config.root, entry.source));
162
+ const registryFile = await read(path.join(config.root, entry.registry));
163
+ if (source === null || registryFile === null) continue;
164
+ const registry = JSON.parse(registryFile)[entry.registryKey];
165
+ if (!registry) {
166
+ problems.push({
167
+ path: entry.registry,
168
+ rule: "unknown-registry-key",
169
+ detail: `'${entry.registryKey}' is not a key of ${entry.registry}`,
170
+ });
171
+ continue;
172
+ }
173
+ const declared = declaredValues(source, entry.constant);
174
+ // A constant this config named but the source does not hold means the agreement is
175
+ // checking nothing. Silence there is how a gate stops meaning anything.
176
+ if (Object.keys(declared).length === 0) {
177
+ problems.push({
178
+ path: entry.source,
179
+ rule: "unfound-constant",
180
+ detail: `'${entry.constant}' declares no values here — the agreement checks nothing`,
181
+ });
182
+ continue;
183
+ }
184
+ const toKey = (key) => `${entry.prefix ?? ""}${entry.lowercase === false ? key : key.toLowerCase()}`;
185
+ problems.push(...checkAgreement(declared, registry, toKey, entry.source));
186
+ }
187
+ return problems;
188
+ }
189
+
190
+ /** Every file inside a server feature that carries SQL, with the feature that owns it. */
191
+ async function resourceFiles(config) {
192
+ const root = path.join(config.root, config.paths.serverFeatures);
193
+ const rel = relativeTo(config.root);
194
+ const resources = [];
195
+ for (const feature of await list(root)) {
196
+ const dir = path.join(root, feature);
197
+ for (const name of config.paths.resourceFiles) {
198
+ const file = path.join(dir, name);
199
+ const source = await read(file);
200
+ if (source) resources.push({ path: rel(file), source, feature });
201
+ }
202
+ for (const subdir of config.paths.resourceDirs) {
203
+ for (const name of await list(path.join(dir, subdir))) {
204
+ if (!/\.tsx?$/.test(name) || name.includes(".test.")) continue;
205
+ const file = path.join(dir, subdir, name);
206
+ const source = await read(file);
207
+ if (source) resources.push({ path: rel(file), source, feature });
208
+ }
209
+ }
210
+ }
211
+ return resources;
212
+ }
213
+
214
+ /** Nothing runs the schema and the queries together, so this asserts their names agree. */
215
+ async function sqlAgreement(config, taken) {
216
+ const migrationDir = path.join(config.root, config.paths.migrations);
217
+ const names = (await list(migrationDir)).filter((name) => name.endsWith(".sql"));
218
+ const migrations = [];
219
+ for (const file of names) {
220
+ migrations.push({ file, sql: await readFile(path.join(migrationDir, file), "utf8") });
221
+ }
222
+ const resources = await resourceFiles(config);
223
+ const features = await list(path.join(config.root, config.paths.serverFeatures));
224
+ const tables = declaredColumns(migrations.map((migration) => migration.sql));
225
+ const owned = new Set(
226
+ [...tables]
227
+ .filter(([, columns]) => columns.has(config.tenant.sqlColumn))
228
+ .map(([table]) => table),
229
+ );
230
+
231
+ const sharedSql = await read(path.join(config.root, config.paths.sharedSql));
232
+ const scanned = sharedSql
233
+ ? [...resources, { path: config.paths.sharedSql, source: sharedSql }]
234
+ : resources;
235
+ const scoped = scanned.map(({ path: file, source }) => ({ path: file, statements: sqlStatements(source) }));
236
+ taken.push(...exemptions(scoped));
237
+
238
+ const tenantOptions = { column: config.tenant.sqlColumn };
239
+ const problems = [];
240
+ if (enabled(config.gates, "audit-append-only")) {
241
+ problems.push(...checkAuditAppendOnly(scoped, { table: config.audit.table }));
242
+ }
243
+ if (enabled(config.gates, "sql-identifiers")) {
244
+ problems.push(...checkSqlIdentifiers(tables, resources, tableOwners(migrations, features)));
245
+ }
246
+ if (enabled(config.gates, "tenant-predicate")) {
247
+ problems.push(
248
+ ...checkTenantPredicate(scoped, owned, tenantOptions),
249
+ ...checkSharedInsertCallSites(resources, tenantOptions),
250
+ );
251
+ }
252
+ return problems;
253
+ }
254
+
255
+ /** Each server feature's trigger: routes, and the requirements they claim. */
256
+ async function triggers(config) {
257
+ const root = path.join(config.root, config.paths.serverFeatures);
258
+ const rel = relativeTo(config.root);
259
+ const found = [];
260
+ for (const feature of await list(root)) {
261
+ const file = path.join(root, feature, config.paths.triggerFile);
262
+ const source = await read(file);
263
+ if (source) found.push({ path: rel(file), source });
264
+ }
265
+ return found;
266
+ }
267
+
268
+ /** Every unit's definition of done ends here: a claim a route makes must be backed. */
269
+ async function claimAgreement(config, routes) {
270
+ const entries = await registered(path.join(config.root, config.paths.traceability));
271
+ if (entries.length === 0) return [];
272
+ const tests = new Map(entries.map((entry) => [entry.id, entry.tests ?? []]));
273
+ return checkClaimedRequirements(routes, tests);
274
+ }
275
+
276
+ /** The api opens a route; the edge must let exactly that route through. */
277
+ async function publicRouteAgreement(config, routes) {
278
+ const file = path.join(config.root, config.paths.publicRoutes);
279
+ const allowed = await read(file);
280
+ if (allowed === null) return [];
281
+ return checkPublicRoutes(
282
+ declaredPublicRoutes(routes.map((route) => route.source)),
283
+ allowedPublicRoutes(allowed),
284
+ config.paths.publicRoutes,
285
+ );
286
+ }
287
+
288
+ /** A worker builds its path as a string; nothing type-checks it against the route table. */
289
+ async function internalRouteAgreement(config, routes) {
290
+ const root = path.join(config.root, config.paths.workers);
291
+ const entries = [];
292
+ for (const worker of await list(root, { withFileTypes: true })) {
293
+ if (!worker.isDirectory() || worker.name === "node_modules") continue;
294
+ const file = path.join(root, worker.name, config.paths.workerEntry);
295
+ const source = await read(file);
296
+ if (source) entries.push({ path: path.relative(config.root, file), source });
297
+ }
298
+ if (entries.length === 0) return [];
299
+ return checkInternalRoutes(
300
+ declaredInternalRoutes(routes.map((route) => route.source)),
301
+ calledInternalRoutes(entries),
302
+ );
303
+ }
304
+
305
+ async function platformFiles(config) {
306
+ const platformDir = path.join(config.root, config.paths.platform ?? "frontend/src/platform");
307
+ const rel = relativeTo(config.root);
308
+ const files = [];
309
+ async function walk(dir) {
310
+ for (const entry of await list(dir, { withFileTypes: true })) {
311
+ const full = path.join(dir, entry.name);
312
+ if (/node_modules|\/dist\//.test(full)) continue;
313
+ if (entry.isDirectory()) await walk(full);
314
+ else if (SOURCE_EXTENSION.test(entry.name)) {
315
+ files.push({ path: rel(full), contents: await readFile(full, "utf8").catch(() => "") });
316
+ }
317
+ }
318
+ }
319
+ await walk(platformDir);
320
+ return files;
321
+ }
322
+
323
+ function isInfraFile(name) {
324
+ if (INFRA_SKIP.some((suffix) => name.endsWith(suffix))) return false;
325
+ return INFRA_EXTENSIONS.some((suffix) => name.endsWith(suffix));
326
+ }
327
+
328
+ /** Terraform/shell files under the configured infra roots — absent is ordinary, not an error. */
329
+ async function infraFiles(config) {
330
+ const rel = relativeTo(config.root);
331
+ const files = [];
332
+ async function walk(dir) {
333
+ for (const entry of await list(dir, { withFileTypes: true })) {
334
+ const full = path.join(dir, entry.name);
335
+ if (/node_modules|\.terraform/.test(full)) continue;
336
+ if (entry.isDirectory()) await walk(full);
337
+ else if (isInfraFile(entry.name)) {
338
+ files.push({ path: rel(full), contents: await readFile(full, "utf8").catch(() => "") });
339
+ }
340
+ }
341
+ }
342
+ for (const root of config.paths.infra ?? []) await walk(path.join(config.root, root));
343
+ return files;
344
+ }
345
+
346
+ /**
347
+ * @param {object} config
348
+ * @param {string} [only] one file — the fast path a post-edit hook takes
349
+ * @returns {Promise<{problems: object[], lines: string[]}>}
350
+ */
351
+ export async function runCheck(config, only) {
352
+ const roots = config.featureRoots.map((root) => path.join(config.root, root));
353
+ const relativeOnly = only ? path.resolve(config.root, only) : undefined;
354
+ const { features, contents } = await collect(roots, relativeOnly);
355
+
356
+ const problems = [];
357
+ const lines = [];
358
+ const taken = [];
359
+
360
+ // A config whose roots all point at nothing sweeps nothing and still ends green. This is the
361
+ // silence `unfound-constant` already refuses for an agreement, applied to the roots themselves.
362
+ // One root missing is ordinary — a repository with no frontend is not misconfigured — so only
363
+ // the case where not a single configured root exists is reported.
364
+ const rootExists = await Promise.all(roots.map((root) => stat(root).then(() => true, () => false)));
365
+ const rootless = roots.length > 0 && !rootExists.includes(true);
366
+ if (rootless) {
367
+ problems.push({
368
+ path: "qc.config.json",
369
+ rule: "unfound-root",
370
+ detail: `no configured feature root exists (${config.featureRoots.join(", ")}) — scaffold a feature, or point featureRoots at this repository's layout; \`qc config\` prints what is in force`,
371
+ });
372
+ }
373
+
374
+ if (enabled(config.gates, "eight-blocks")) {
375
+ const relative = features.map((feature) => ({
376
+ ...feature,
377
+ feature: path.relative(config.root, feature.feature),
378
+ }));
379
+ problems.push(...checkFeatureAnatomy(relative, config.anatomy), ...checkNoEmptyBlock(contents, config.anatomy));
380
+ }
381
+ if (enabled(config.gates, "headless-sagas")) {
382
+ problems.push(...checkHeadlessPipelines(contents, { block: config.saga.headlessBlock, viewModules: config.saga.viewModules }));
383
+ }
384
+ if (!rootless) lines.push(`OK structure ${features.length} feature folder(s)`);
385
+
386
+ if (relativeOnly) return { problems, lines };
387
+
388
+ if (enabled(config.gates, "citations")) {
389
+ const cited = await citableFiles(
390
+ config.paths.citable.map((root) => path.join(config.root, root)),
391
+ config.root,
392
+ );
393
+ const decisionsFile = await read(path.join(config.root, config.docs.decisions));
394
+ const decisions = definedIds(decisionsFile ?? "", config.citations);
395
+ const requirementIds = new Set(
396
+ (await registered(path.join(config.root, config.paths.traceability))).map((entry) => entry.id),
397
+ );
398
+ const docsDir = path.join(config.root, config.docs.root);
399
+ const docs = new Set(
400
+ (await list(docsDir)).filter((name) => name.endsWith(".md")).map((name) => `${config.docs.root}/${name}`),
401
+ );
402
+ problems.push(
403
+ ...checkCitations(cited, decisions, requirementIds, config.citations),
404
+ ...checkDocPaths(cited, docs, { docsRoot: config.docs.root }),
405
+ ...checkSelfContained(cited, config.foreign),
406
+ );
407
+ lines.push("OK citations every cited id and doc path resolves, no foreign reference");
408
+ }
409
+
410
+ if (enabled(config.gates, "registry-agreement")) {
411
+ problems.push(...(await agreements(config)));
412
+ }
413
+
414
+ if (enabled(config.gates, "sql-identifiers") || enabled(config.gates, "tenant-predicate")) {
415
+ problems.push(...(await sqlAgreement(config, taken)));
416
+ lines.push("OK sql every column is declared, every statement and shared insert carries the tenant");
417
+ // An exemption is counted and named, so it stays a decision rather than a habit.
418
+ for (const exemption of taken) lines.push(` exempt ${exemption.path}: ${exemption.reason}`);
419
+ }
420
+
421
+ if (enabled(config.gates, "saga-tests")) {
422
+ const sources = await sourceFiles(config);
423
+ const sagas = declaredSagas(
424
+ sources.filter((file) => !TEST_FILE.test(file.path)),
425
+ { factories: config.saga.factories },
426
+ );
427
+ problems.push(
428
+ ...checkSagaTests(sagas, sources.filter((file) => TEST_FILE.test(file.path)), {
429
+ viewModules: config.saga.viewModules,
430
+ sources,
431
+ }),
432
+ );
433
+ lines.push(`OK sagas ${sagas.length} workflow(s), each named by a test that runs without a view`);
434
+ }
435
+
436
+ if (enabled(config.gates, "gate-tests")) {
437
+ const own = await sourceFiles(config, config.paths.checks);
438
+ problems.push(
439
+ ...checkGatesAreTested(
440
+ own.filter((file) => !TEST_FILE.test(file.path)),
441
+ own.filter((file) => TEST_FILE.test(file.path)),
442
+ ),
443
+ );
444
+ if (own.length > 0) lines.push(`OK gates ${own.filter((f) => !TEST_FILE.test(f.path)).length} repository gate(s), each with a case that must fail`);
445
+ }
446
+
447
+ if (enabled(config.gates, "enforcement-map")) {
448
+ const map = await read(path.join(config.root, config.docs.enforcement));
449
+ if (map !== null) {
450
+ problems.push(
451
+ ...checkEnforcementMap(
452
+ map,
453
+ Object.keys(lintRules).filter((name) => enabled(config.rules, name)),
454
+ Object.keys(config.gates).filter((name) => enabled(config.gates, name)),
455
+ ),
456
+ );
457
+ lines.push("OK map every check the kit runs is named in the enforcement map, and every name resolves");
458
+ }
459
+ }
460
+
461
+ const routes = await triggers(config);
462
+ if (enabled(config.gates, "claimed-requirements")) {
463
+ problems.push(...(await claimAgreement(config, routes)));
464
+ lines.push("OK claims every requirement a route claims names a test that proves it");
465
+ }
466
+ if (enabled(config.gates, "public-routes")) {
467
+ problems.push(...(await publicRouteAgreement(config, routes)));
468
+ lines.push("OK public the edge lets through exactly the routes the api serves without a session");
469
+ }
470
+ if (enabled(config.gates, "internal-routes")) {
471
+ problems.push(...(await internalRouteAgreement(config, routes)));
472
+ lines.push("OK internal every path a worker posts to is a route the api serves");
473
+ }
474
+ if (enabled(config.gates, "headless-sagas")) {
475
+ lines.push("OK headless pipelines are headless and triggers remain thin presenters");
476
+ }
477
+ if (enabled(config.gates, "frontend-boundaries")) {
478
+ const platform = await platformFiles(config);
479
+ if (platform.length > 0) {
480
+ problems.push(...checkFrontendBoundaries(platform, contents));
481
+ lines.push("OK boundaries platform remains a thin substrate without domain leaks or junk drawers");
482
+ }
483
+ }
484
+ if (enabled(config.gates, "test-mirror")) {
485
+ const all = await sourceFiles(config);
486
+ const testPaths = all.filter((file) => TEST_FILE.test(file.path)).map((file) => file.path);
487
+ const srcPaths = all.filter((file) => !TEST_FILE.test(file.path)).map((file) => file.path);
488
+ const mirrorProblems = checkTestMirror(testPaths, srcPaths);
489
+ problems.push(...mirrorProblems);
490
+ if (mirrorProblems.length === 0) {
491
+ lines.push("OK mirror frontend tests mirror src paths 1:1, no orphaned test");
492
+ }
493
+ }
494
+
495
+ if (enabled(config.gates, "comment-style")) {
496
+ const infra = await infraFiles(config);
497
+ if (infra.length > 0) {
498
+ problems.push(...checkCommentStyle(infra, config.comments));
499
+ lines.push("OK comments every comment under infra/ is one line of why, never a paragraph");
500
+ }
501
+ }
502
+
503
+ if (enabled(config.gates, "config-floor")) {
504
+ problems.push(...checkConfigFloor(defaults, config, { exemptions: config.floor.exemptions }));
505
+ lines.push("OK floor every check the kit ships on is in force, or names the decision that switched it off");
506
+ }
507
+
508
+ if (enabled(config.gates, "english-source")) {
509
+ // Its own file set. The citation and source walkers were built for other questions and each
510
+ // skips something this one needs -- tests, documents, migrations, contracts. Language is not
511
+ // a property of TypeScript: a Japanese column default in .sql reaches a reader just the same.
512
+ const everything = await filesMatching(config, new RegExp(`\\.(${config.language.extensions.join("|")})$`), config.language);
513
+ problems.push(...checkEnglishSource(everything, config.language));
514
+ problems.push(...checkTranslationPairs(everything.map((file) => file.path), config.language.translationPairs));
515
+ // The ledger is printed on every run, not only when it fails: a file listed once and never
516
+ // mentioned again is how a document stays untranslated without anyone deciding that.
517
+ const ledger = config.language.legacyNonEnglish ?? {};
518
+ const owed = Object.values(ledger).reduce((sum, n) => sum + (Number(n) || 0), 0);
519
+ lines.push("OK english comments, docs and rules are English; another language is declared data");
520
+ if (owed > 0) {
521
+ lines.push(
522
+ ` owing ${Object.keys(ledger).length} file(s), ${owed} occurrence(s) still to translate — language.legacyNonEnglish`,
523
+ );
524
+ }
525
+ }
526
+
527
+ if (enabled(config.gates, "adr-format")) {
528
+ const records = await citableFiles([path.join(config.root, config.adr.root)], config.root);
529
+ const docs = records.filter((file) => file.path.endsWith(".md"));
530
+ problems.push(...checkAdrFormat(docs, config.adr));
531
+ if (docs.length > 0) lines.push(`OK decisions ${docs.length} record(s), each with its cost and its rejected alternatives`);
532
+ }
533
+
534
+ return { problems, lines };
535
+ }