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,82 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import {
|
|
4
|
+
BRANCH,
|
|
5
|
+
OFF,
|
|
6
|
+
WORKTREE,
|
|
7
|
+
isolationRefusal,
|
|
8
|
+
isolationSettings,
|
|
9
|
+
leaseHeldByAnother,
|
|
10
|
+
worktreeKind,
|
|
11
|
+
} from "./worktree-isolation.mjs";
|
|
12
|
+
|
|
13
|
+
const NOW = Date.parse("2026-09-20T12:00:00Z");
|
|
14
|
+
const base = (overrides = {}) => ({
|
|
15
|
+
settings: isolationSettings({ isolation: { require: WORKTREE } }),
|
|
16
|
+
branch: "feat/tasks",
|
|
17
|
+
kind: "linked",
|
|
18
|
+
inSpecialOperation: false,
|
|
19
|
+
lease: null,
|
|
20
|
+
sessionId: "session-aaaa",
|
|
21
|
+
now: NOW,
|
|
22
|
+
...overrides,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("off is the default, and an unconfigured repository is never restricted", () => {
|
|
26
|
+
assert.equal(isolationSettings().require, OFF);
|
|
27
|
+
assert.equal(isolationRefusal(base({ settings: isolationSettings(), branch: "main", kind: "primary" })), null);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("an agent on its own branch in its own worktree is allowed", () => {
|
|
31
|
+
assert.equal(isolationRefusal(base()), null);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("generating code on a protected branch is refused and names the branch", () => {
|
|
35
|
+
const reason = isolationRefusal(base({ branch: "main" }));
|
|
36
|
+
assert.match(reason, /"main" is a protected branch/);
|
|
37
|
+
assert.match(reason, /git worktree add/);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("the protected branch list is configurable", () => {
|
|
41
|
+
const settings = isolationSettings({ isolation: { require: BRANCH, protectedBranches: ["trunk"] } });
|
|
42
|
+
assert.match(isolationRefusal(base({ settings, branch: "trunk" })), /"trunk" is a protected branch/);
|
|
43
|
+
assert.equal(isolationRefusal(base({ settings, branch: "main" })), null);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("require:branch permits the primary worktree, require:worktree does not", () => {
|
|
47
|
+
const onBranch = isolationSettings({ isolation: { require: BRANCH } });
|
|
48
|
+
assert.equal(isolationRefusal(base({ settings: onBranch, kind: "primary" })), null);
|
|
49
|
+
assert.match(isolationRefusal(base({ kind: "primary" })), /primary worktree/);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("a second session in one worktree is refused, because that is two agents on one branch", () => {
|
|
53
|
+
const lease = { sessionId: "session-bbbb", branch: "feat/tasks", updatedAt: "2026-09-20T11:30:00Z" };
|
|
54
|
+
assert.match(isolationRefusal(base({ lease })), /Another agent \(session session-/);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("an agent's own lease never refuses its own edit", () => {
|
|
58
|
+
const lease = { sessionId: "session-aaaa", branch: "feat/tasks", updatedAt: "2026-09-20T11:30:00Z" };
|
|
59
|
+
assert.equal(isolationRefusal(base({ lease })), null);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("an abandoned lease expires, so a crashed session cannot wedge a worktree", () => {
|
|
63
|
+
const stale = { sessionId: "session-bbbb", updatedAt: "2026-09-19T12:00:00Z" };
|
|
64
|
+
assert.equal(leaseHeldByAnother(stale, "session-aaaa", NOW, 8), null);
|
|
65
|
+
assert.equal(isolationRefusal(base({ lease: stale })), null);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("a detached HEAD is refused, but not mid-rebase when detaching is the point", () => {
|
|
69
|
+
assert.match(isolationRefusal(base({ branch: "" })), /HEAD is detached/);
|
|
70
|
+
assert.equal(isolationRefusal(base({ branch: "", inSpecialOperation: true })), null);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("a linked worktree is told apart from the primary by its git dir", () => {
|
|
74
|
+
assert.equal(worktreeKind("/repo/.git", "/repo/.git"), "primary");
|
|
75
|
+
assert.equal(worktreeKind("/repo/.git/worktrees/tasks", "/repo/.git"), "linked");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("a misconfigured isolation level is a named config error, not a silent pass", () => {
|
|
79
|
+
assert.throws(() => isolationSettings({ isolation: { require: "yes" } }), /swarm\.isolation\.require/);
|
|
80
|
+
assert.throws(() => isolationSettings({ isolation: { leaseHours: 0 } }), /swarm\.isolation\.leaseHours/);
|
|
81
|
+
assert.throws(() => isolationSettings({ isolation: { leaseHours: "8" } }), /swarm\.isolation\.leaseHours/);
|
|
82
|
+
});
|
package/src/config.mjs
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// One config, read by every gate and by the lint preset. A constant that named this
|
|
2
|
+
// repository's shape is a config key here. Defaults reproduce the reference layout, so a
|
|
3
|
+
// repo that adopts the reference layout writes no config at all.
|
|
4
|
+
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
|
|
9
|
+
export const CONFIG_FILE = "qc.config.json";
|
|
10
|
+
|
|
11
|
+
export const defaults = {
|
|
12
|
+
// Where features live. Every structural gate walks these.
|
|
13
|
+
featureRoots: ["backend/src/features", "frontend/src/features"],
|
|
14
|
+
// The path segment that marks a feature, and the one file that crosses its boundary.
|
|
15
|
+
featureDir: "features",
|
|
16
|
+
publicFile: "index",
|
|
17
|
+
|
|
18
|
+
docs: {
|
|
19
|
+
root: "docs",
|
|
20
|
+
decisions: "docs/decisions.md",
|
|
21
|
+
enforcement: "docs/enforcement.md",
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
// Citation ids the repository defines and the gates resolve.
|
|
25
|
+
citations: {
|
|
26
|
+
prefixes: ["ADR", "QC"],
|
|
27
|
+
// Ids recognised inside comments but owned elsewhere: standards, requirement ids.
|
|
28
|
+
external: ["REQ-[A-Z]{3}-\\d+", "T\\d+-\\d+", "D\\d+"],
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
decisions: {
|
|
32
|
+
// Content-addressed files: a ledger hashes them, so renumbering a citation inside one reads as tampering.
|
|
33
|
+
immutable: ["(^|/)(drizzle|migrations?)/[^/]*\\.sql$"],
|
|
34
|
+
// Vendored tooling: its citations resolve against its own upstream register, not this repository's.
|
|
35
|
+
exclude: [],
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
thresholds: "quality-thresholds.json",
|
|
39
|
+
|
|
40
|
+
// A key that makes a retry a resume rather than a second write. docs/guards.md.
|
|
41
|
+
idempotency: {
|
|
42
|
+
keys: ["mutationId", "clientMutationId", "idempotencyKey"],
|
|
43
|
+
volatile: ["Date.now", "Math.random", "crypto.randomUUID", "uuid", "uuidv4", "nanoid", "randomUUID"],
|
|
44
|
+
// A store that outlives nothing has nothing to dedupe, so a disposable key is right.
|
|
45
|
+
ledgerKey: "ledger",
|
|
46
|
+
throwawayLedgers: ["InMemoryPipelineLedger"],
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
// The log that is evidence only while nothing can edit it.
|
|
50
|
+
audit: { table: "audit_log" },
|
|
51
|
+
|
|
52
|
+
// Everything the I/O layer reads. A gate is pure; these say where its input lives.
|
|
53
|
+
// A path that does not exist disables the check that reads it, rather than failing:
|
|
54
|
+
// a repository without workers has no worker routes to disagree about.
|
|
55
|
+
paths: {
|
|
56
|
+
// The feature root the server-side gates read: schema, routes, SQL.
|
|
57
|
+
serverFeatures: "backend/src/features",
|
|
58
|
+
migrations: "backend/src/drizzle",
|
|
59
|
+
// Requirement id to the tests that prove it.
|
|
60
|
+
traceability: "traceability/requirements.json",
|
|
61
|
+
// The edge that forwards to the api, holding the set it forwards without a session.
|
|
62
|
+
publicRoutes: "bff/src/public-routes.ts",
|
|
63
|
+
// Each worker's entrypoint, relative to its own folder.
|
|
64
|
+
workers: "workers",
|
|
65
|
+
workerEntry: "src/entry.ts",
|
|
66
|
+
// Query shapes shared across features: statements that own no table and answer
|
|
67
|
+
// to no migration, so they join the tenant scan only.
|
|
68
|
+
sharedSql: "backend/src/application/sql/crud.ts",
|
|
69
|
+
// Roots walked for citations, doc paths and foreign references.
|
|
70
|
+
citable: ["backend/src", "frontend/src", "bff/src", "packages", "workers", "docs", "migration"],
|
|
71
|
+
// Files inside a feature that carry SQL.
|
|
72
|
+
resourceFiles: ["resource.ts", "schema.ts"],
|
|
73
|
+
resourceDirs: ["shared", "slices"],
|
|
74
|
+
triggerFile: "trigger.ts",
|
|
75
|
+
// Platform and frontend features paths for boundary checks.
|
|
76
|
+
platform: "frontend/src/platform",
|
|
77
|
+
frontendFeatures: "frontend/src/features",
|
|
78
|
+
// A repository's own gates. The kit's are proven by its suite; these are not.
|
|
79
|
+
checks: "scripts/gate",
|
|
80
|
+
// Terraform/shell roots the comment-style gate scans. Absence is ordinary — not every
|
|
81
|
+
// repository provisions its own infrastructure.
|
|
82
|
+
infra: ["infra"],
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
// Where a tool demands a literal the registry already owns, assert agreement rather
|
|
86
|
+
// than forbid the number. QC-007. Each entry names both sides.
|
|
87
|
+
agreements: [],
|
|
88
|
+
|
|
89
|
+
// Names that may appear in a comment but are owned elsewhere.
|
|
90
|
+
foreign: [],
|
|
91
|
+
|
|
92
|
+
// What a scaffolded feature imports its runtime from. A new repository points this
|
|
93
|
+
// at its own kernel; the kit emits the shape, never a dependency on the kit.
|
|
94
|
+
kernelModule: "@qc/kernel",
|
|
95
|
+
|
|
96
|
+
tenant: {
|
|
97
|
+
column: "tenantId",
|
|
98
|
+
sqlColumn: "tenant_id",
|
|
99
|
+
tableFactory: "pgTable",
|
|
100
|
+
indexFactory: "index",
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
storage: {
|
|
104
|
+
modules: ["drizzle-orm", "pg", "postgres"],
|
|
105
|
+
// The blocks permitted to import a storage module.
|
|
106
|
+
resourceFiles: ["resource.ts", "schema.ts"],
|
|
107
|
+
// Something must bind the driver for a resource to have a pool. That path is exempt.
|
|
108
|
+
driverBinding: "backend/src/infrastructure/db/",
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
// Any third-party service's SDK, same reasoning as storage: one seam, so a second
|
|
112
|
+
// provider or a test double only ever needs one place to differ. Cloud, payment,
|
|
113
|
+
// messaging, search — a repository overriding scopes or modules replaces this list
|
|
114
|
+
// wholesale (merge() does not union an array), so it repeats what it still wants.
|
|
115
|
+
externalServices: {
|
|
116
|
+
scopes: ["@aws-sdk", "@azure", "@google-cloud", "@sendgrid", "@elastic", "@algolia", "@stripe", "@twilio"],
|
|
117
|
+
modules: ["aws-sdk", "stripe", "braintree", "twilio", "nodemailer", "algoliasearch", "amqplib", "kafkajs"],
|
|
118
|
+
// A composition root wires the concrete client; nothing past it may know the provider.
|
|
119
|
+
allowedFiles: ["main.ts", "entry.ts"],
|
|
120
|
+
// A category subfolder, not a bare "adapters/": a feature-local folder named adapters/
|
|
121
|
+
// must not become exempt by accident.
|
|
122
|
+
allowedPaths: ["adapters/cloud/", "adapters/payments/", "adapters/messaging/", "adapters/search/"],
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
// The one module permitted to call fetch or build a URL.
|
|
126
|
+
apiClient: "platform/api-client.ts",
|
|
127
|
+
|
|
128
|
+
// Files that may only present, never orchestrate.
|
|
129
|
+
presenters: "frontend/src/features/*/trigger",
|
|
130
|
+
|
|
131
|
+
// A workflow is headless-first: declared by one of these, and proven by a test that
|
|
132
|
+
// imports none of the view modules. docs/architecture.md.
|
|
133
|
+
saga: {
|
|
134
|
+
factories: ["definePipeline", "defineSaga"],
|
|
135
|
+
viewModules: ["react", "react-dom"],
|
|
136
|
+
// The block that must stay callable without a view layer. One name, or a list when a
|
|
137
|
+
// repository carries more than one anatomy.
|
|
138
|
+
headlessBlock: "pipeline",
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
// English is the one language in every set. Another language is data, not prose: it lives in a
|
|
142
|
+
// declared path beside its English variant. `scripts` names the ranges a gate looks for.
|
|
143
|
+
language: {
|
|
144
|
+
scripts: ["cjk"],
|
|
145
|
+
// Language is not a property of TypeScript. A migration's column default and a contract's
|
|
146
|
+
// description reach a reader exactly like a comment does.
|
|
147
|
+
extensions: ["tsx?", "[cm]?js", "mjs", "md", "sql", "ya?ml", "json"],
|
|
148
|
+
// The whole repository. A language rule is not scoped to where decisions are cited.
|
|
149
|
+
roots: ["."],
|
|
150
|
+
exclude: ["(^|/)node_modules(/|$)", "(^|/)dist(/|$)", "(^|/)\\.git(/|$)", "(^|/)coverage(/|$)", "pnpm-lock\\.yaml$"],
|
|
151
|
+
allowNonEnglish: [],
|
|
152
|
+
translationPairs: [],
|
|
153
|
+
// What the ledger may still carry. It may be met or lowered, never raised without saying so.
|
|
154
|
+
legacyNonEnglish: {},
|
|
155
|
+
legacyBudget: undefined,
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
// A decision record carries its cost and its rejected alternatives, or it is advocacy.
|
|
159
|
+
adr: {
|
|
160
|
+
root: "docs/decisions",
|
|
161
|
+
sections: [
|
|
162
|
+
"Prerequisite",
|
|
163
|
+
"Context",
|
|
164
|
+
"Decision",
|
|
165
|
+
"Why this, specifically",
|
|
166
|
+
"What this buys",
|
|
167
|
+
"What this costs",
|
|
168
|
+
"Alternatives considered and not chosen",
|
|
169
|
+
"Related decisions",
|
|
170
|
+
],
|
|
171
|
+
minWords: 12,
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
// One agent per branch per worktree. Off until a repository opts in: a solo agent is never
|
|
175
|
+
// restricted. `require`: off | branch (refuse the trunk) | worktree (also refuse the primary).
|
|
176
|
+
swarm: {
|
|
177
|
+
isolation: { require: "off", protectedBranches: ["main", "master"], leaseHours: 8, allow: [] },
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
// Names that must be registered scoped, never singleton.
|
|
181
|
+
scopedSuffixes: ["Repository", "Saga", "UnitOfWork", "UnitOfWorkFactory"],
|
|
182
|
+
|
|
183
|
+
// A comment is one line of why; reasoning that wants a paragraph is a decision with an id.
|
|
184
|
+
comments: { maxLines: 2 },
|
|
185
|
+
|
|
186
|
+
// Switching a shipped check off names the decision that says why, or "off-by-design".
|
|
187
|
+
floor: { exemptions: {} },
|
|
188
|
+
|
|
189
|
+
// A tool is not an author. These may never appear in a commit's attribution trailer.
|
|
190
|
+
commitMessage: {
|
|
191
|
+
tools: [
|
|
192
|
+
"claude", "anthropic", "copilot", "chatgpt", "openai", "gpt-4", "gpt-5", "gemini",
|
|
193
|
+
"cursor", "codeium", "devin", "aider", "windsurf", "bot@", "noreply@anthropic.com",
|
|
194
|
+
],
|
|
195
|
+
},
|
|
196
|
+
|
|
197
|
+
// A named pattern is a pointer. `patterns` adds a repository's own vocabulary to the defaults.
|
|
198
|
+
patterns: { maxWords: 20, extra: [] },
|
|
199
|
+
|
|
200
|
+
// A promise is awaited. `allow` names the modules holding the two shapes await cannot express:
|
|
201
|
+
// racing against a cancellation, and observing a rejection nobody is waiting for any more.
|
|
202
|
+
promise: { allow: [] },
|
|
203
|
+
|
|
204
|
+
anatomy: {
|
|
205
|
+
// Bounded vertical slices.
|
|
206
|
+
slice: {
|
|
207
|
+
required: ["index.ts", "schema.ts", "trigger.ts"],
|
|
208
|
+
sliceDir: "slices",
|
|
209
|
+
sharedDir: "shared",
|
|
210
|
+
sharedFiles: ["types.ts", "queries.ts", "guards.ts", "runner.ts", "components.ts"],
|
|
211
|
+
},
|
|
212
|
+
// The flat pipeline anatomy.
|
|
213
|
+
block: {
|
|
214
|
+
required: ["index.ts", "trigger.ts", "pipeline.ts", "resource.ts"],
|
|
215
|
+
optional: ["branch.ts", "fragment.ts", "ledger.ts", "record.ts"],
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
// Every gate and lint rule is switchable. Ship all; a repo turns off what it lacks.
|
|
220
|
+
// These are the gates `qc check` runs, and a switch here really does turn one off.
|
|
221
|
+
gates: {
|
|
222
|
+
"eight-blocks": true,
|
|
223
|
+
citations: true,
|
|
224
|
+
"registry-agreement": true,
|
|
225
|
+
"sql-identifiers": true,
|
|
226
|
+
"tenant-predicate": true,
|
|
227
|
+
"claimed-requirements": true,
|
|
228
|
+
"public-routes": true,
|
|
229
|
+
"internal-routes": true,
|
|
230
|
+
"headless-sagas": true,
|
|
231
|
+
"saga-tests": true,
|
|
232
|
+
"gate-tests": true,
|
|
233
|
+
"audit-append-only": true,
|
|
234
|
+
"frontend-boundaries": true,
|
|
235
|
+
"test-mirror": true,
|
|
236
|
+
"enforcement-map": true,
|
|
237
|
+
"comment-style": true,
|
|
238
|
+
// Policy gates: each needs a repository to say what its policy *is* -- which language, which
|
|
239
|
+
// paths, which ADR sections, which opt-outs are legitimate. Shipping them on would red every
|
|
240
|
+
// repository the moment it upgrades the harness, which is how a gate bends the work it judges.
|
|
241
|
+
"config-floor": false,
|
|
242
|
+
"english-source": false,
|
|
243
|
+
"adr-format": false,
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
// Gates that produce rather than inspect: a repository's codegen imports these and
|
|
247
|
+
// writes their output. `qc check` does not run them, so they carry no switch — a
|
|
248
|
+
// switch that changed nothing would be a lie about what the config controls.
|
|
249
|
+
generators: ["tenant-tables", "contract-compose"],
|
|
250
|
+
|
|
251
|
+
rules: {
|
|
252
|
+
"no-comment-paragraph": true,
|
|
253
|
+
"name-the-pattern": false,
|
|
254
|
+
"no-promise-then": true,
|
|
255
|
+
"no-number-in-comment": true,
|
|
256
|
+
"no-cross-feature-internals": true,
|
|
257
|
+
"no-offset-pagination": true,
|
|
258
|
+
"no-orchestration-in-trigger": true,
|
|
259
|
+
"no-status-literal": true,
|
|
260
|
+
"scoped-repository": true,
|
|
261
|
+
"storage-only-in-resource": true,
|
|
262
|
+
"external-service-only-in-adapter": true,
|
|
263
|
+
"tenant-scoped-table": true,
|
|
264
|
+
"signal-last-param": true,
|
|
265
|
+
"no-raw-fetch": true,
|
|
266
|
+
"durable-idempotency-key": true,
|
|
267
|
+
"no-supersession-trail": true,
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
// Layer names and their legal import direction. The kit ships the reference set;
|
|
271
|
+
// a repo with different layers replaces the whole block.
|
|
272
|
+
layers: {
|
|
273
|
+
include: ["backend/src/**/*.ts", "packages/domain/src/**/*.ts"],
|
|
274
|
+
elements: [
|
|
275
|
+
{ type: "domain", pattern: "packages/domain/src/**" },
|
|
276
|
+
{ type: "application", pattern: "backend/src/application/**" },
|
|
277
|
+
{ type: "adapters", pattern: "backend/src/adapters/**" },
|
|
278
|
+
{ type: "infrastructure", pattern: "backend/src/infrastructure/**" },
|
|
279
|
+
{ type: "feature", pattern: "backend/src/features/*/**" },
|
|
280
|
+
],
|
|
281
|
+
allow: {
|
|
282
|
+
domain: ["domain"],
|
|
283
|
+
application: ["domain", "application"],
|
|
284
|
+
adapters: ["domain", "application", "adapters"],
|
|
285
|
+
infrastructure: ["domain", "application", "adapters", "infrastructure"],
|
|
286
|
+
feature: ["domain", "application", "adapters", "feature"],
|
|
287
|
+
},
|
|
288
|
+
external: [
|
|
289
|
+
{ from: ["domain"], disallow: ["*"] },
|
|
290
|
+
{ from: ["application"], disallow: ["*"], allow: ["zod"] },
|
|
291
|
+
],
|
|
292
|
+
},
|
|
293
|
+
|
|
294
|
+
// Extra pipeline steps a repo composes in. The CLI never hardcodes a project's chain.
|
|
295
|
+
pipeline: [],
|
|
296
|
+
|
|
297
|
+
// Files the browser rules apply to.
|
|
298
|
+
clientFiles: ["frontend/src/**/*.{ts,tsx}"],
|
|
299
|
+
|
|
300
|
+
// Tooling names the vocabulary it bans, so it trips its own checks.
|
|
301
|
+
toolingFiles: ["scripts/**/*.mjs", "**/*.test.{ts,tsx,mjs}"],
|
|
302
|
+
|
|
303
|
+
ignores: ["**/dist/**", "**/node_modules/**", "**/*.tsbuildinfo"],
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
function isPlainObject(value) {
|
|
307
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Later wins. An array replaces; an object merges key by key. */
|
|
311
|
+
export function merge(base, override) {
|
|
312
|
+
if (!isPlainObject(base) || !isPlainObject(override)) return override ?? base;
|
|
313
|
+
const out = { ...base };
|
|
314
|
+
for (const [key, value] of Object.entries(override)) {
|
|
315
|
+
out[key] = isPlainObject(value) && isPlainObject(base[key]) ? merge(base[key], value) : value;
|
|
316
|
+
}
|
|
317
|
+
return out;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Read `qc.config.json` from `root`, merged over the defaults. Absent is legal. */
|
|
321
|
+
export function load(root = process.cwd()) {
|
|
322
|
+
const file = path.join(root, CONFIG_FILE);
|
|
323
|
+
const user = existsSync(file) ? JSON.parse(readFileSync(file, "utf8")) : {};
|
|
324
|
+
const config = merge(defaults, user);
|
|
325
|
+
config.root = root;
|
|
326
|
+
return config;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Thresholds live in their own registry so lint and docs cite one number. */
|
|
330
|
+
export function thresholds(config) {
|
|
331
|
+
const file = path.join(config.root ?? process.cwd(), config.thresholds);
|
|
332
|
+
if (!existsSync(file)) return {};
|
|
333
|
+
return createRequire(import.meta.url)(file).gates ?? {};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function enabled(map, id) {
|
|
337
|
+
return map[id] !== false;
|
|
338
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { readdirSync } from "node:fs";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { defaults, load, merge } from "./config.mjs";
|
|
8
|
+
import { rules } from "./eslint/index.mjs";
|
|
9
|
+
|
|
10
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
|
|
12
|
+
// A switch naming a rule that no longer exists silently stops switching anything.
|
|
13
|
+
// This is the test that catches the next rename, not the last one.
|
|
14
|
+
test("every lint rule has a switch, and every switch names a rule", () => {
|
|
15
|
+
assert.deepEqual(Object.keys(defaults.rules).sort(), Object.keys(rules).sort());
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("every gate is either switchable or declared a generator", () => {
|
|
19
|
+
const files = readdirSync(path.join(here, "gates"))
|
|
20
|
+
.filter((file) => file.endsWith(".mjs") && !file.endsWith(".test.mjs"))
|
|
21
|
+
.map((file) => file.replace(/\.mjs$/, ""))
|
|
22
|
+
.sort();
|
|
23
|
+
const accounted = [...Object.keys(defaults.gates), ...defaults.generators].sort();
|
|
24
|
+
assert.deepEqual(accounted, files);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// A switch that turns nothing off is worse than no switch: it says the config
|
|
28
|
+
// controls something it does not. Every name under `gates` must reach the runner.
|
|
29
|
+
test("every switch under gates is one the check runner reads", async () => {
|
|
30
|
+
const source = await readFile(path.join(here, "cli/check.mjs"), "utf8");
|
|
31
|
+
for (const gate of Object.keys(defaults.gates)) {
|
|
32
|
+
assert.ok(
|
|
33
|
+
source.includes(`"${gate}"`),
|
|
34
|
+
`gates.${gate} is never consulted by the check runner`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("a generator is not offered as a switch", () => {
|
|
40
|
+
for (const generator of defaults.generators) {
|
|
41
|
+
assert.equal(defaults.gates[generator], undefined);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("a user config overrides a key without dropping its siblings", () => {
|
|
46
|
+
const merged = merge(defaults, { tenant: { column: "orgId" } });
|
|
47
|
+
assert.equal(merged.tenant.column, "orgId");
|
|
48
|
+
assert.equal(merged.tenant.tableFactory, defaults.tenant.tableFactory);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("an array replaces rather than merges", () => {
|
|
52
|
+
const merged = merge(defaults, { featureRoots: ["src/features"] });
|
|
53
|
+
assert.deepEqual(merged.featureRoots, ["src/features"]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("a repository with no config file loads the reference layout", () => {
|
|
57
|
+
const config = load(here);
|
|
58
|
+
assert.deepEqual(config.featureRoots, defaults.featureRoots);
|
|
59
|
+
});
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// Renumbering a decision means rewriting every citation of it, or leaving a dangling pointer.
|
|
2
|
+
|
|
3
|
+
import { citationPattern } from "./gates/citations.mjs";
|
|
4
|
+
|
|
5
|
+
const NOISE = new Set(
|
|
6
|
+
("a an and are as at be by for from has have in is it its never no not of on one only or so than that the their then there these they this to what when which who with without every each any all".split(" ")),
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
/** The register as rows, so an audit can read what a decision says and not merely that it exists. */
|
|
10
|
+
export function parseRegister(markdown, prefixes) {
|
|
11
|
+
const id = citationPattern(prefixes);
|
|
12
|
+
const rows = [];
|
|
13
|
+
for (const line of markdown.split("\n")) {
|
|
14
|
+
if (!line.startsWith("|")) continue;
|
|
15
|
+
const cells = line.split("|").slice(1, -1).map((cell) => cell.trim());
|
|
16
|
+
if (cells.length < 3) continue;
|
|
17
|
+
const found = cells[0].match(id);
|
|
18
|
+
if (found?.length === 1 && found[0] === cells[0]) {
|
|
19
|
+
rows.push({ id: cells[0], decision: cells[1], rule: cells[2] });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return rows;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Distinctive words only. A backticked identifier is kept: in a rule it is usually the subject. */
|
|
26
|
+
export function terms(text) {
|
|
27
|
+
const words = text.toLowerCase().replaceAll("`", " ").match(/[a-z][a-z-]{2,}/g);
|
|
28
|
+
return new Set((words ?? []).filter((word) => !NOISE.has(word)));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function similarity(left, right) {
|
|
32
|
+
if (left.size === 0 || right.size === 0) return 0;
|
|
33
|
+
let shared = 0;
|
|
34
|
+
for (const term of left) if (right.has(term)) shared += 1;
|
|
35
|
+
return shared / Math.min(left.size, right.size);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function sequences(ids) {
|
|
39
|
+
const bySeries = new Map();
|
|
40
|
+
for (const id of ids) {
|
|
41
|
+
const at = id.lastIndexOf("-");
|
|
42
|
+
const number = id.slice(at + 1);
|
|
43
|
+
const series = bySeries.get(id.slice(0, at)) ?? [];
|
|
44
|
+
series.push({ id, number: Number(number), width: number.length });
|
|
45
|
+
bySeries.set(id.slice(0, at), series);
|
|
46
|
+
}
|
|
47
|
+
for (const series of bySeries.values()) series.sort((a, b) => a.number - b.number);
|
|
48
|
+
return bySeries;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Renumbered from one in the order the rows stand, so which decision came first survives. */
|
|
52
|
+
export function squashPlan(defined) {
|
|
53
|
+
const plan = new Map();
|
|
54
|
+
for (const [prefix, series] of sequences(defined)) {
|
|
55
|
+
// One width for the series: a squash that left 001 beside 0002 would close gaps and open a seam.
|
|
56
|
+
const width = Math.max(...series.map((entry) => entry.width));
|
|
57
|
+
series.forEach((entry, index) => {
|
|
58
|
+
const next = prefix + "-" + String(index + 1).padStart(width, "0");
|
|
59
|
+
if (next !== entry.id) plan.set(entry.id, next);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return plan;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function renamePlan(spec) {
|
|
66
|
+
const plan = new Map();
|
|
67
|
+
for (const pair of spec.split(",")) {
|
|
68
|
+
const [from, to] = pair.split("=").map((half) => half.trim());
|
|
69
|
+
if (!from || !to) throw new Error(`rename wants OLD=NEW, got "${pair}"`);
|
|
70
|
+
plan.set(from, to);
|
|
71
|
+
}
|
|
72
|
+
return plan;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A plan is refused whole rather than applied in part. */
|
|
76
|
+
export function planProblems(plan, defined) {
|
|
77
|
+
const problems = [];
|
|
78
|
+
const landing = new Map();
|
|
79
|
+
for (const [from, to] of plan) {
|
|
80
|
+
if (!defined.has(from)) problems.push(`${from} is not a decision this repository defines`);
|
|
81
|
+
if (defined.has(to) && !plan.has(to)) {
|
|
82
|
+
problems.push(`${to} is already taken by a decision this plan does not move`);
|
|
83
|
+
}
|
|
84
|
+
const other = landing.get(to);
|
|
85
|
+
if (other !== undefined) problems.push(`${other} and ${from} would both become ${to}`);
|
|
86
|
+
landing.set(to, from);
|
|
87
|
+
}
|
|
88
|
+
return problems;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** One pass over one alternation: every position is visited once, so a swap cannot collapse. */
|
|
92
|
+
export function rewrite(contents, plan) {
|
|
93
|
+
if (plan.size === 0) return contents;
|
|
94
|
+
const alternation = [...plan.keys()].map((id) => id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|");
|
|
95
|
+
return contents.replaceAll(new RegExp(`\\b(?:${alternation})\\b`, "g"), (id) => plan.get(id) ?? id);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A file whose bytes are its identity: rewriting a citation inside one invalidates the hash that names it. */
|
|
99
|
+
export function isImmutable(file, patterns) {
|
|
100
|
+
return patterns.some((source) => new RegExp(source).test(file));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** `ADR-0001` is a lookup; `ADR-0001=ADR-0002` merely contains one and is not. */
|
|
104
|
+
export function isBareId(text, prefixes) {
|
|
105
|
+
const found = text.match(citationPattern(prefixes));
|
|
106
|
+
return found?.length === 1 && found[0] === text;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function citationsIn(contents, prefixes) {
|
|
110
|
+
const pattern = citationPattern(prefixes);
|
|
111
|
+
const found = [];
|
|
112
|
+
for (const [index, text] of contents.split("\n").entries()) {
|
|
113
|
+
for (const id of text.match(pattern) ?? []) found.push({ id, line: index + 1, text: text.trim() });
|
|
114
|
+
}
|
|
115
|
+
return found;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A gap and a dangling citation are defects; a decision nobody cites may be enforced by absence.
|
|
120
|
+
* @returns {{text: string, fatal: boolean}[]}
|
|
121
|
+
*/
|
|
122
|
+
export function registerProblems(defined, sites, decisionsPath) {
|
|
123
|
+
const problems = [];
|
|
124
|
+
for (const [prefix, series] of sequences(defined)) {
|
|
125
|
+
const highest = series[series.length - 1];
|
|
126
|
+
const missing = [];
|
|
127
|
+
for (let number = 1; number <= highest.number; number += 1) {
|
|
128
|
+
if (!series.some((entry) => entry.number === number)) missing.push(number);
|
|
129
|
+
}
|
|
130
|
+
if (missing.length > 0) {
|
|
131
|
+
problems.push({ text: `${prefix}: ${missing.length} gap(s) below ${highest.id}`, fatal: true });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
for (const id of [...defined].sort()) {
|
|
135
|
+
const uses = (sites.get(id) ?? []).filter((site) => site.file !== decisionsPath);
|
|
136
|
+
if (uses.length === 0) {
|
|
137
|
+
problems.push({ text: `${id}: defined and never cited — delete it, or cite it`, fatal: false });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
for (const id of [...sites.keys()].sort()) {
|
|
141
|
+
if (!defined.has(id)) {
|
|
142
|
+
problems.push({ text: `${id}: cited and not defined in ${decisionsPath}`, fatal: true });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return problems;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Pairs close enough to be one decision, strongest first. */
|
|
149
|
+
export function mergeCandidates(rows, threshold = 0.25) {
|
|
150
|
+
const scored = rows.map((row) => ({ ...row, terms: terms(`${row.decision} ${row.rule}`) }));
|
|
151
|
+
const pairs = [];
|
|
152
|
+
for (let i = 0; i < scored.length; i += 1) {
|
|
153
|
+
for (let j = i + 1; j < scored.length; j += 1) {
|
|
154
|
+
const score = similarity(scored[i].terms, scored[j].terms);
|
|
155
|
+
if (score < threshold) continue;
|
|
156
|
+
const shared = [...scored[i].terms].filter((term) => scored[j].terms.has(term)).sort();
|
|
157
|
+
pairs.push({ left: scored[i].id, right: scored[j].id, score, shared });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return pairs.sort((a, b) => b.score - a.score);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** A decision whose rule a check already states is said twice: once here, once in the generated map. */
|
|
164
|
+
export function alreadyChecked(rows, checks, threshold = 0.7) {
|
|
165
|
+
const named = checks.map((check) => ({ ...check, terms: terms(check.description) }));
|
|
166
|
+
const found = [];
|
|
167
|
+
for (const row of rows) {
|
|
168
|
+
const ruleTerms = terms(row.rule);
|
|
169
|
+
for (const check of named) {
|
|
170
|
+
const score = similarity(ruleTerms, check.terms);
|
|
171
|
+
if (score >= threshold) found.push({ id: row.id, check: check.name, score });
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return found.sort((a, b) => b.score - a.score);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** How far a decision reaches: one area means it is that area's rule, not the repository's. */
|
|
178
|
+
export function reach(sites, id, decisionsPath) {
|
|
179
|
+
const areas = new Set();
|
|
180
|
+
let files = 0;
|
|
181
|
+
for (const site of sites.get(id) ?? []) {
|
|
182
|
+
if (site.file === decisionsPath) continue;
|
|
183
|
+
files += 1;
|
|
184
|
+
const parts = site.file.split("/");
|
|
185
|
+
areas.add(parts.length > 2 ? parts.slice(0, 2).join("/") : parts[0]);
|
|
186
|
+
}
|
|
187
|
+
return { files, areas: [...areas].sort() };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Every row asked to earn its id. Each verdict is a prompt for a person, never a finding. */
|
|
191
|
+
export function audit(rows, sites, decisionsPath, checks = []) {
|
|
192
|
+
const verdicts = rows.map((row) => {
|
|
193
|
+
const { files, areas } = reach(sites, row.id, decisionsPath);
|
|
194
|
+
const verdict = files === 0 ? "abort?" : areas.length === 1 && files === 1 ? "inline?" : areas.length === 1 ? "local?" : "keep";
|
|
195
|
+
return { id: row.id, decision: row.decision, files, areas, verdict };
|
|
196
|
+
});
|
|
197
|
+
return { verdicts, merges: mergeCandidates(rows), checked: alreadyChecked(rows, checks) };
|
|
198
|
+
}
|