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
package/src/cli/init.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Scaffold the documents and configuration a new repository needs before any gate
|
|
2
|
+
// can pass. The citations gate reads docs/decisions.md; without it, gate one fails
|
|
3
|
+
// on every file. So `init` is not a convenience — it is the gate's own precondition.
|
|
4
|
+
|
|
5
|
+
import { copyFile, mkdir, readdir, readFile, writeFile } from "node:fs/promises";
|
|
6
|
+
import { withEnforcementMap } from "../enforcement-map.mjs";
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
|
|
11
|
+
const templates = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../templates");
|
|
12
|
+
|
|
13
|
+
// Copied into the repository root. `../decisions.examples.md` sits outside this list on
|
|
14
|
+
// purpose: it cites ids the shipped register does not define, so a repository that took it
|
|
15
|
+
// would fail the citations gate. A new
|
|
16
|
+
// repository should not carry somebody else's domain decisions.
|
|
17
|
+
const FILES = [
|
|
18
|
+
["qc.config.json", "qc.config.json"],
|
|
19
|
+
["quality-thresholds.json", "quality-thresholds.json"],
|
|
20
|
+
[".jscpd.json", ".jscpd.json"],
|
|
21
|
+
["docs/decisions.md", "docs/decisions.md"],
|
|
22
|
+
["docs/architecture.md", "docs/architecture.md"],
|
|
23
|
+
["docs/enforcement.md", "docs/enforcement.md"],
|
|
24
|
+
["docs/guards.md", "docs/guards.md"],
|
|
25
|
+
["docs/performance.md", "docs/performance.md"],
|
|
26
|
+
["docs/pagination.md", "docs/pagination.md"],
|
|
27
|
+
["docs/glossary.md", "docs/glossary.md"],
|
|
28
|
+
["docs/ui.md", "docs/ui.md"],
|
|
29
|
+
["githooks/pre-commit", ".githooks/pre-commit"],
|
|
30
|
+
["githooks/pre-push", ".githooks/pre-push"],
|
|
31
|
+
["github/workflows/ci.yml", ".github/workflows/ci.yml"],
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
// The enforcement map is filled in on the way out, so a rule added to the kit cannot ship a
|
|
35
|
+
// template that fails the kit's own enforcement-map gate.
|
|
36
|
+
async function copyTemplate(from, to, force, config) {
|
|
37
|
+
if (existsSync(to) && !force) return { to, status: "kept" };
|
|
38
|
+
await mkdir(path.dirname(to), { recursive: true });
|
|
39
|
+
const overwriting = existsSync(to);
|
|
40
|
+
if (to.endsWith(config.docs.enforcement.split("/").pop())) {
|
|
41
|
+
await writeFile(to, withEnforcementMap(await readFile(from, "utf8"), config));
|
|
42
|
+
} else {
|
|
43
|
+
await copyFile(from, to);
|
|
44
|
+
}
|
|
45
|
+
return { to, status: overwriting && force ? "overwritten" : "written" };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const SCRIPTS = {
|
|
49
|
+
"spec:check": "qc check",
|
|
50
|
+
"gen:feature": "qc feature",
|
|
51
|
+
prepare: "qc install-hooks",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Add the scripts the docs reference, without touching one the repository already set. */
|
|
55
|
+
async function addScripts(config) {
|
|
56
|
+
const file = path.join(config.root, "package.json");
|
|
57
|
+
if (!existsSync(file)) return [];
|
|
58
|
+
const manifest = JSON.parse(await readFile(file, "utf8"));
|
|
59
|
+
const scripts = manifest.scripts ?? {};
|
|
60
|
+
const added = Object.entries(SCRIPTS).filter(([name]) => scripts[name] === undefined);
|
|
61
|
+
if (added.length === 0) return [{ to: file, status: "kept" }];
|
|
62
|
+
manifest.scripts = { ...scripts, ...Object.fromEntries(added) };
|
|
63
|
+
await writeFile(file, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
64
|
+
return [{ to: file, status: "written" }];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function runInit(config, args = []) {
|
|
68
|
+
const force = args.includes("--force");
|
|
69
|
+
const results = [];
|
|
70
|
+
for (const [source, target] of FILES) {
|
|
71
|
+
results.push(await copyTemplate(path.join(templates, source), path.join(config.root, target), force, config));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const name of ["pre-commit", "pre-push"]) {
|
|
75
|
+
const hook = path.join(config.root, ".githooks", name);
|
|
76
|
+
if (existsSync(hook)) await writeFile(hook, await readFile(hook, "utf8"), { mode: 0o755 });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// The docs and the CI workflow both tell a reader to run these, so `init` puts them
|
|
80
|
+
// where a reader will look rather than leaving the instruction dangling.
|
|
81
|
+
results.push(...(await addScripts(config)));
|
|
82
|
+
|
|
83
|
+
for (const { to, status } of results) {
|
|
84
|
+
console.log(`${status.padEnd(11)} ${path.relative(config.root, to)}`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const kept = results.filter((result) => result.status === "kept").length;
|
|
88
|
+
if (kept > 0) console.log(`\n${kept} file(s) already existed and were left alone. --force overwrites.`);
|
|
89
|
+
console.log(`
|
|
90
|
+
Next:
|
|
91
|
+
1. Edit docs/decisions.md — delete what does not apply, add your own. Ids are stable;
|
|
92
|
+
never renumber, because every citation points at a number.
|
|
93
|
+
2. Edit docs/glossary.md and the per-surface table in docs/performance.md.
|
|
94
|
+
3. Point qc.config.json at your layout if it differs from the reference. \`qc config\`
|
|
95
|
+
prints what is in force — featureRoots matching nothing is a failed check, not a pass.
|
|
96
|
+
4. \`qc install-hooks\` to bind the pre-commit and pre-push hooks.
|
|
97
|
+
5. \`qc feature <domain-name>\` to scaffold the first slice, then \`qc check\`.`);
|
|
98
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// A human is bound by exactly what binds an agent, so the hook is installed rather
|
|
2
|
+
// than documented. core.hooksPath is repository-local and survives a clone only if
|
|
3
|
+
// somebody runs this, which is why it also belongs in a `prepare` script.
|
|
4
|
+
|
|
5
|
+
import { execFile } from "node:child_process";
|
|
6
|
+
import { promisify } from "node:util";
|
|
7
|
+
import { chmod } from "node:fs/promises";
|
|
8
|
+
import { existsSync } from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
|
|
11
|
+
const run = promisify(execFile);
|
|
12
|
+
|
|
13
|
+
export async function installHooks(config, dir = ".githooks") {
|
|
14
|
+
const hooks = path.join(config.root, dir);
|
|
15
|
+
if (!existsSync(hooks)) {
|
|
16
|
+
console.error(`No ${dir}/ here. Run \`qc init\` first.`);
|
|
17
|
+
process.exitCode = 1;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
for (const name of ["pre-commit", "commit-msg", "pre-push"]) {
|
|
21
|
+
const hook = path.join(hooks, name);
|
|
22
|
+
if (existsSync(hook)) await chmod(hook, 0o755);
|
|
23
|
+
}
|
|
24
|
+
await run("git", ["config", "core.hooksPath", dir], { cwd: config.root });
|
|
25
|
+
console.log(`git core.hooksPath -> ${dir}`);
|
|
26
|
+
}
|
package/src/cli/qc.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The one entrypoint. A hook, a git hook and CI all call this, so they cannot drift.
|
|
3
|
+
|
|
4
|
+
import process from "node:process";
|
|
5
|
+
import { load } from "../config.mjs";
|
|
6
|
+
import { runCheck } from "./check.mjs";
|
|
7
|
+
|
|
8
|
+
const USAGE = `qc — architecture gates
|
|
9
|
+
|
|
10
|
+
qc check [file] every structural gate; one file is the fast path a hook takes
|
|
11
|
+
qc init scaffold the docs, config, hooks and workflow into this repository
|
|
12
|
+
qc feature <name> scaffold a feature in the configured anatomy
|
|
13
|
+
qc install-hooks point git at the kit's pre-commit hook
|
|
14
|
+
qc work-order-check refuse to commit on the wrong branch, per .claude/work-order.local.json
|
|
15
|
+
qc commit-msg <file> refuse a commit message that credits a tool as an author
|
|
16
|
+
qc decisions [id] where decisions are cited; --squash closes the gaps
|
|
17
|
+
qc enforcement-map refresh the generated rule/gate table in docs/enforcement.md
|
|
18
|
+
qc doctor the hooks and the lockfile must run the same harness
|
|
19
|
+
qc config print the resolved configuration
|
|
20
|
+
|
|
21
|
+
Every gate reads qc.config.json. A key it does not set keeps the reference default.`;
|
|
22
|
+
|
|
23
|
+
async function main() {
|
|
24
|
+
const [command, ...rest] = process.argv.slice(2);
|
|
25
|
+
const config = load(process.cwd());
|
|
26
|
+
|
|
27
|
+
switch (command) {
|
|
28
|
+
case "check": {
|
|
29
|
+
const { problems, lines } = await runCheck(config, rest[0]);
|
|
30
|
+
if (problems.length > 0) {
|
|
31
|
+
for (const problem of problems) {
|
|
32
|
+
console.error(`FAIL ${problem.feature ?? problem.path} ${problem.rule}: ${problem.detail}`);
|
|
33
|
+
}
|
|
34
|
+
console.error(`\n${problems.length} problem(s). ${config.docs.enforcement}`);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
for (const line of lines) console.log(line);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
case "init": {
|
|
41
|
+
const { runInit } = await import("./init.mjs");
|
|
42
|
+
await runInit(config, rest);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
case "feature": {
|
|
46
|
+
const { runScaffold } = await import("./scaffold.mjs");
|
|
47
|
+
await runScaffold(config, rest);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
case "install-hooks": {
|
|
51
|
+
const { installHooks } = await import("./install-hooks.mjs");
|
|
52
|
+
await installHooks(config);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
case "work-order-check": {
|
|
56
|
+
const { runWorkOrderCheck } = await import("./work-order-guard.mjs");
|
|
57
|
+
process.exit(await runWorkOrderCheck(config.root));
|
|
58
|
+
}
|
|
59
|
+
case "commit-msg": {
|
|
60
|
+
const { readFileSync } = await import("node:fs");
|
|
61
|
+
const { checkCommitAttribution } = await import("./commit-attribution.mjs");
|
|
62
|
+
const problems = checkCommitAttribution(readFileSync(rest[0], "utf8"), config.commitMessage);
|
|
63
|
+
for (const problem of problems) console.error(`FAIL commit-msg ${problem.rule}: ${problem.detail}`);
|
|
64
|
+
process.exit(problems.length > 0 ? 1 : 0);
|
|
65
|
+
}
|
|
66
|
+
case "decisions": {
|
|
67
|
+
const { runDecisions } = await import("./decisions.mjs");
|
|
68
|
+
process.exit(await runDecisions(config, rest));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
case "enforcement-map": {
|
|
72
|
+
const { runSyncEnforcementMap } = await import("./sync-enforcement-map.mjs");
|
|
73
|
+
await runSyncEnforcementMap(config);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
case "doctor": {
|
|
77
|
+
const { runDoctor } = await import("./doctor.mjs");
|
|
78
|
+
process.exit(await runDoctor(config));
|
|
79
|
+
}
|
|
80
|
+
case "config":
|
|
81
|
+
console.log(JSON.stringify(config, null, 2));
|
|
82
|
+
return;
|
|
83
|
+
default:
|
|
84
|
+
console.log(USAGE);
|
|
85
|
+
process.exit(command === undefined || command === "help" ? 0 : 1);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
await main();
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// The right thing is the cheapest thing. docs/enforcement.md.
|
|
2
|
+
// An agent that runs the generator cannot produce a wrong anatomy.
|
|
3
|
+
//
|
|
4
|
+
// The emitted files import their runtime from `kernelModule` in qc.config.json, never
|
|
5
|
+
// from this package: the kit prescribes the shape and ships no runtime to depend on.
|
|
6
|
+
|
|
7
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
8
|
+
import { existsSync } from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
|
|
11
|
+
const NAME = /^[a-z][a-z0-9-]*$/;
|
|
12
|
+
|
|
13
|
+
function names(name) {
|
|
14
|
+
return {
|
|
15
|
+
name,
|
|
16
|
+
camel: name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()),
|
|
17
|
+
pascal: name.replace(/(^|-)([a-z])/g, (_, __, letter) => letter.toUpperCase()),
|
|
18
|
+
snake: name.replace(/-/g, "_"),
|
|
19
|
+
upper: name.replace(/-/g, "_").toUpperCase(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function sliceFiles(id, config) {
|
|
24
|
+
const kernel = config.kernelModule;
|
|
25
|
+
const { name, camel, pascal, snake, upper } = id;
|
|
26
|
+
const tenant = config.tenant;
|
|
27
|
+
return {
|
|
28
|
+
"index.ts": `import { defineIndex } from "${kernel}";
|
|
29
|
+
import { trigger } from "./trigger.js";
|
|
30
|
+
|
|
31
|
+
export const ${camel} = defineIndex({
|
|
32
|
+
name: "${name}",
|
|
33
|
+
surface: {},
|
|
34
|
+
resourceTypes: ["${name}"],
|
|
35
|
+
invalidation: [],
|
|
36
|
+
trigger,
|
|
37
|
+
});
|
|
38
|
+
`,
|
|
39
|
+
"schema.ts": `// Storage schema for ${name}. ADR-0054, docs/architecture.md.
|
|
40
|
+
|
|
41
|
+
import { ${tenant.tableFactory}, text, timestamp } from "${config.storage.modules[0]}/pg-core";
|
|
42
|
+
|
|
43
|
+
export const ${camel}Table = ${tenant.tableFactory}("${snake}", {
|
|
44
|
+
id: text("id").primaryKey(),
|
|
45
|
+
${tenant.column}: text("${tenant.sqlColumn}").notNull(),
|
|
46
|
+
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
47
|
+
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
|
48
|
+
});
|
|
49
|
+
`,
|
|
50
|
+
"trigger.ts": `// Thin composition point: a route names a slice handler and nothing else.
|
|
51
|
+
|
|
52
|
+
import { defineTrigger } from "${kernel}";
|
|
53
|
+
import { create${pascal} } from "./slices/create.js";
|
|
54
|
+
|
|
55
|
+
export const trigger = defineTrigger({
|
|
56
|
+
routes: [
|
|
57
|
+
{ method: "POST", path: "/v1/${name}", auth: "tenant", handler: create${pascal} },
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
`,
|
|
61
|
+
"shared/types.ts": `// Row types and the column projection every slice reads through.
|
|
62
|
+
|
|
63
|
+
export type ${pascal}Row = {
|
|
64
|
+
id: string;
|
|
65
|
+
${tenant.column}: string;
|
|
66
|
+
createdAt: string;
|
|
67
|
+
updatedAt: string;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const ${upper}_COLUMNS = \`id, ${tenant.sqlColumn} as "${tenant.column}", created_at as "createdAt", updated_at as "updatedAt"\`;
|
|
71
|
+
`,
|
|
72
|
+
"slices/create.ts": `// One operation: validate, run the pipeline, write. docs/architecture.md.
|
|
73
|
+
|
|
74
|
+
import { definePipeline } from "${kernel}";
|
|
75
|
+
import { ${upper}_COLUMNS, type ${pascal}Row } from "../shared/types.js";
|
|
76
|
+
|
|
77
|
+
type Create${pascal}State = {
|
|
78
|
+
${tenant.column}: string;
|
|
79
|
+
row?: ${pascal}Row;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export const create${pascal}Pipeline = definePipeline<Create${pascal}State>({
|
|
83
|
+
name: "create-${name}",
|
|
84
|
+
steps: [],
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export async function create${pascal}() {
|
|
88
|
+
throw new Error("not implemented");
|
|
89
|
+
}
|
|
90
|
+
`,
|
|
91
|
+
// The gate wants a workflow proven to run without a view, and the generator is the
|
|
92
|
+
// cheapest place to make that true. Replace the shape assertion with real cases.
|
|
93
|
+
"slices/create.test.ts": `import test from "node:test";
|
|
94
|
+
import assert from "node:assert/strict";
|
|
95
|
+
import { create${pascal}Pipeline } from "./create.js";
|
|
96
|
+
|
|
97
|
+
test("the create pipeline is declared headlessly", () => {
|
|
98
|
+
assert.equal(create${pascal}Pipeline.name, "create-${name}");
|
|
99
|
+
});
|
|
100
|
+
`,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function flatFiles(id, config) {
|
|
105
|
+
const kernel = config.kernelModule;
|
|
106
|
+
const { name, camel, snake, upper } = id;
|
|
107
|
+
const tenant = config.tenant;
|
|
108
|
+
return {
|
|
109
|
+
"index.ts": `import { defineIndex } from "${kernel}";
|
|
110
|
+
import { trigger } from "./trigger.js";
|
|
111
|
+
|
|
112
|
+
export const ${camel} = defineIndex({
|
|
113
|
+
name: "${name}",
|
|
114
|
+
surface: {},
|
|
115
|
+
resourceTypes: ["${name}"],
|
|
116
|
+
invalidation: [],
|
|
117
|
+
trigger,
|
|
118
|
+
});
|
|
119
|
+
`,
|
|
120
|
+
"trigger.ts": `import { defineTrigger } from "${kernel}";
|
|
121
|
+
|
|
122
|
+
export const trigger = defineTrigger({ routes: [] });
|
|
123
|
+
`,
|
|
124
|
+
"pipeline.ts": `// Ordered named steps. Headless: no view library reaches in here. QC-010.
|
|
125
|
+
|
|
126
|
+
import { definePipeline } from "${kernel}";
|
|
127
|
+
|
|
128
|
+
export const ${camel}Pipeline = definePipeline({ name: "${name}", steps: [] });
|
|
129
|
+
`,
|
|
130
|
+
"resource.ts": `// The only block that touches storage. ADR-0047.
|
|
131
|
+
|
|
132
|
+
import { ${tenant.tableFactory}, text, timestamp } from "${config.storage.modules[0]}/pg-core";
|
|
133
|
+
|
|
134
|
+
export const ${camel}Table = ${tenant.tableFactory}("${snake}", {
|
|
135
|
+
id: text("id").primaryKey(),
|
|
136
|
+
${tenant.column}: text("${tenant.sqlColumn}").notNull(),
|
|
137
|
+
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
export const ${upper}_COLUMNS = \`id, ${tenant.sqlColumn} as "${tenant.column}", created_at as "createdAt"\`;
|
|
141
|
+
`,
|
|
142
|
+
"pipeline.test.ts": `import test from "node:test";
|
|
143
|
+
import assert from "node:assert/strict";
|
|
144
|
+
import { ${camel}Pipeline } from "./pipeline.js";
|
|
145
|
+
|
|
146
|
+
test("the pipeline is declared headlessly", () => {
|
|
147
|
+
assert.equal(${camel}Pipeline.name, "${name}");
|
|
148
|
+
});
|
|
149
|
+
`,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function runScaffold(config, args = []) {
|
|
154
|
+
const name = args.find((arg) => !arg.startsWith("-"));
|
|
155
|
+
if (!name || !NAME.test(name)) {
|
|
156
|
+
console.error("usage: qc feature <domain-name> [--flat] [--root <path>]");
|
|
157
|
+
console.error("The name is the full domain word, never a code.");
|
|
158
|
+
process.exitCode = 1;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const flat = args.includes("--flat");
|
|
163
|
+
const rootArg = args[args.indexOf("--root") + 1];
|
|
164
|
+
const root = args.includes("--root") && rootArg ? rootArg : config.paths.serverFeatures;
|
|
165
|
+
const dir = path.join(config.root, root, name);
|
|
166
|
+
|
|
167
|
+
if (existsSync(dir)) {
|
|
168
|
+
console.error(`${path.relative(config.root, dir)} already exists.`);
|
|
169
|
+
process.exitCode = 1;
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// The generator cannot emit what the anatomy gate will reject: the wrong thing has to
|
|
174
|
+
// stay the expensive one. docs/enforcement.md.
|
|
175
|
+
const sliceOnly = config.anatomy?.sliceOnlyRoots ?? ["backend/src/features"];
|
|
176
|
+
if (flat && sliceOnly.some((only) => root.includes(only))) {
|
|
177
|
+
console.error(`${root} is bounded-vertical-slice only, so --flat would fail the anatomy gate.`);
|
|
178
|
+
console.error("Drop --flat, scaffold elsewhere with --root, or take this root out of anatomy.sliceOnlyRoots.");
|
|
179
|
+
process.exitCode = 1;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const id = names(name);
|
|
184
|
+
const files = flat ? flatFiles(id, config) : sliceFiles(id, config);
|
|
185
|
+
for (const [file, contents] of Object.entries(files)) {
|
|
186
|
+
const target = path.join(dir, file);
|
|
187
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
188
|
+
await writeFile(target, contents);
|
|
189
|
+
console.log(`written ${path.relative(config.root, target)}`);
|
|
190
|
+
}
|
|
191
|
+
console.log(`
|
|
192
|
+
${flat ? "Flat pipeline" : "Bounded vertical slice"} anatomy. An empty block is a review finding:
|
|
193
|
+
fill each file or delete the ones this feature does not need, then \`qc check\`.`);
|
|
194
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Refreshes the generated block in an existing docs/enforcement.md, without touching anything
|
|
2
|
+
// else `qc init` would — for the day-to-day case of the kit shipping a new rule or gate.
|
|
3
|
+
|
|
4
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { withEnforcementMap } from "../enforcement-map.mjs";
|
|
7
|
+
|
|
8
|
+
export async function runSyncEnforcementMap(config) {
|
|
9
|
+
const file = path.join(config.root, config.docs.enforcement);
|
|
10
|
+
const before = await readFile(file, "utf8").catch(() => null);
|
|
11
|
+
if (before === null) {
|
|
12
|
+
console.error(`No ${config.docs.enforcement} here. Run \`qc init\` first.`);
|
|
13
|
+
process.exitCode = 1;
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const after = withEnforcementMap(before, config);
|
|
17
|
+
if (after === null) {
|
|
18
|
+
console.error(`No generated marker found in ${config.docs.enforcement} — nothing to refresh.`);
|
|
19
|
+
process.exitCode = 1;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (after === before) {
|
|
23
|
+
console.log(`OK enforcement-map ${config.docs.enforcement} already current`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
await writeFile(file, after);
|
|
27
|
+
console.log(`written ${config.docs.enforcement}`);
|
|
28
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { test } from "node:test";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { branchMismatch, denyOutput, globToRegExp, inSpecialGitOperation, isAllowed } from "./work-order-guard.mjs";
|
|
7
|
+
|
|
8
|
+
test("a path under a declared glob is allowed", () => {
|
|
9
|
+
assert.equal(isAllowed("frontend/src/platform/ui/button.tsx", ["frontend/src/platform/**"]), true);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("a path outside every declared glob is refused", () => {
|
|
13
|
+
assert.equal(isAllowed("backend/src/main.ts", ["frontend/src/platform/**"]), false);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("no declared paths means no restriction", () => {
|
|
17
|
+
assert.equal(isAllowed("anything/at/all.ts", []), true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("a single star does not cross a directory boundary", () => {
|
|
21
|
+
assert.equal(isAllowed(".github/workflows/nested/ci.yml", [".github/workflows/*.yml"]), false);
|
|
22
|
+
assert.equal(isAllowed(".github/workflows/ci.yml", [".github/workflows/*.yml"]), true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("the deny reason names the file and the declared paths", () => {
|
|
26
|
+
const output = denyOutput("backend/src/main.ts", ["frontend/src/platform/**"]);
|
|
27
|
+
assert.equal(output.hookSpecificOutput.permissionDecision, "deny");
|
|
28
|
+
assert.match(output.hookSpecificOutput.permissionDecisionReason, /backend\/src\/main\.ts/);
|
|
29
|
+
assert.match(output.hookSpecificOutput.permissionDecisionReason, /frontend\/src\/platform/);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("globToRegExp escapes a regex metacharacter in a literal path segment", () => {
|
|
33
|
+
assert.equal(globToRegExp("docs/a.b/**").test("docs/aXb/index.md"), false);
|
|
34
|
+
assert.equal(globToRegExp("docs/a.b/**").test("docs/a.b/index.md"), true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("no declared branch means no restriction", () => {
|
|
38
|
+
assert.equal(branchMismatch(undefined, "main"), null);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("the current branch matching the declared one is not a mismatch", () => {
|
|
42
|
+
assert.equal(branchMismatch("infra/5-reconcile", "infra/5-reconcile"), null);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("a mismatch names both the declared branch and the current one", () => {
|
|
46
|
+
const reason = branchMismatch("infra/5-reconcile", "main");
|
|
47
|
+
assert.match(reason, /infra\/5-reconcile/);
|
|
48
|
+
assert.match(reason, /main/);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("detached HEAD is named explicitly, not left blank", () => {
|
|
52
|
+
assert.match(branchMismatch("infra/5-reconcile", ""), /detached HEAD/);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("a rebase or cherry-pick in progress is a special git operation", () => {
|
|
56
|
+
const markers = new Set(["rebase-merge"]);
|
|
57
|
+
assert.equal(inSpecialGitOperation("/repo/.git", (p) => markers.has(path.basename(p))), true);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("an ordinary checkout is not a special git operation", () => {
|
|
61
|
+
assert.equal(inSpecialGitOperation("/repo/.git", () => false), false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("the installed pre-commit hook checks the work order before its staged-file filter", () => {
|
|
65
|
+
const templatePath = path.join(
|
|
66
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
67
|
+
"..",
|
|
68
|
+
"..",
|
|
69
|
+
"templates",
|
|
70
|
+
"githooks",
|
|
71
|
+
"pre-commit",
|
|
72
|
+
);
|
|
73
|
+
const lines = readFileSync(templatePath, "utf8").split("\n");
|
|
74
|
+
const checkLine = lines.findIndex((line) => line.includes("qc work-order-check"));
|
|
75
|
+
const filterLine = lines.findIndex((line) => line.includes("STAGED="));
|
|
76
|
+
assert.notEqual(checkLine, -1, "pre-commit should call qc work-order-check");
|
|
77
|
+
assert.notEqual(filterLine, -1, "pre-commit should still filter staged files by extension");
|
|
78
|
+
assert.ok(checkLine < filterLine, "the branch check must run before the extension filter, or a .tf/.yml/.md commit skips it");
|
|
79
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Enforces swarm.md's isolation rule: an agent that generates code works in its own worktree, on
|
|
2
|
+
// its own branch. Two agents editing one branch is the failure this exists to prevent, and it has
|
|
3
|
+
// two shapes -- editing straight onto the trunk, and two sessions sharing one working directory.
|
|
4
|
+
// Git already refuses to check one branch out into two worktrees, so it covers neither shape.
|
|
5
|
+
//
|
|
6
|
+
// Off by default, like every other opt-in in this plugin: a solo agent declares nothing and is
|
|
7
|
+
// never restricted. A repository turns it on in `qc.config.json` under `swarm.isolation`.
|
|
8
|
+
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
|
|
11
|
+
export const OFF = "off";
|
|
12
|
+
export const BRANCH = "branch";
|
|
13
|
+
export const WORKTREE = "worktree";
|
|
14
|
+
const LEVELS = [OFF, BRANCH, WORKTREE];
|
|
15
|
+
const DEFAULT_PROTECTED = ["main", "master"];
|
|
16
|
+
const DEFAULT_LEASE_HOURS = 8;
|
|
17
|
+
|
|
18
|
+
/** @returns the normalised isolation settings, or throws naming the key a repository got wrong. */
|
|
19
|
+
export function isolationSettings(swarm = {}) {
|
|
20
|
+
const raw = swarm.isolation ?? {};
|
|
21
|
+
const require = raw.require ?? OFF;
|
|
22
|
+
if (!LEVELS.includes(require)) {
|
|
23
|
+
throw new Error(`swarm.isolation.require must be one of ${LEVELS.join(", ")}, got ${JSON.stringify(require)}`);
|
|
24
|
+
}
|
|
25
|
+
const leaseHours = raw.leaseHours ?? DEFAULT_LEASE_HOURS;
|
|
26
|
+
if (typeof leaseHours !== "number" || !(leaseHours > 0)) {
|
|
27
|
+
throw new Error(`swarm.isolation.leaseHours must be a positive number, got ${JSON.stringify(raw.leaseHours)}`);
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
require,
|
|
31
|
+
protectedBranches: raw.protectedBranches ?? DEFAULT_PROTECTED,
|
|
32
|
+
leaseHours,
|
|
33
|
+
allow: raw.allow ?? [],
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A linked worktree has its own git dir; the primary's git dir is the common one. */
|
|
38
|
+
export function worktreeKind(gitDir, gitCommonDir) {
|
|
39
|
+
return path.resolve(gitDir) === path.resolve(gitCommonDir) ? "primary" : "linked";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A lease is another agent's only while it is fresh -- an abandoned session must not wedge a worktree. */
|
|
43
|
+
export function leaseHeldByAnother(lease, sessionId, now, leaseHours) {
|
|
44
|
+
if (!lease?.sessionId || !sessionId) return null;
|
|
45
|
+
if (lease.sessionId === sessionId) return null;
|
|
46
|
+
const age = now - Date.parse(lease.updatedAt ?? 0);
|
|
47
|
+
if (!(age >= 0) || age > leaseHours * 3_600_000) return null;
|
|
48
|
+
return lease;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The whole decision, as data, so every branch of it is testable without a git checkout.
|
|
53
|
+
* @returns a reason string to refuse the edit, or null to allow it.
|
|
54
|
+
*/
|
|
55
|
+
export function isolationRefusal(state) {
|
|
56
|
+
const { settings, branch, kind, inSpecialOperation, lease, sessionId, now } = state;
|
|
57
|
+
if (settings.require === OFF) return null;
|
|
58
|
+
if (inSpecialOperation) return null;
|
|
59
|
+
|
|
60
|
+
if (!branch) {
|
|
61
|
+
return "HEAD is detached, so this edit belongs to no branch. Check out a work-order branch before generating code.";
|
|
62
|
+
}
|
|
63
|
+
if (settings.protectedBranches.includes(branch)) {
|
|
64
|
+
return (
|
|
65
|
+
`"${branch}" is a protected branch and an agent does not generate code on it. ` +
|
|
66
|
+
`Create a work order's own branch first: \`git worktree add ../<slug> -b <branch>\`.`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
if (settings.require === WORKTREE && kind === "primary") {
|
|
70
|
+
return (
|
|
71
|
+
`This is the primary worktree, shared by every session that opens the repository. ` +
|
|
72
|
+
`Give this work order its own: \`git worktree add ../<slug> -b ${branch}\` from a clean checkout.`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
const held = leaseHeldByAnother(lease, sessionId, now, settings.leaseHours);
|
|
76
|
+
if (held) {
|
|
77
|
+
return (
|
|
78
|
+
`Another agent (session ${held.sessionId.slice(0, 8)}) is already working in this worktree on ` +
|
|
79
|
+
`"${held.branch ?? branch}". Two agents on one branch lose each other's edits. ` +
|
|
80
|
+
`Take your own: \`git worktree add ../<slug> -b <branch>\`.`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function denyOutput(reason) {
|
|
87
|
+
return {
|
|
88
|
+
hookSpecificOutput: {
|
|
89
|
+
hookEventName: "PreToolUse",
|
|
90
|
+
permissionDecision: "deny",
|
|
91
|
+
permissionDecisionReason: `swarm isolation: ${reason}`,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|