architecture-harness 0.4.2 → 0.6.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 +1 -1
- package/src/cli/check.mjs +32 -6
- package/src/cli/cli.test.mjs +44 -0
- package/src/cli/lease-file.mjs +33 -0
- package/src/cli/lease.mjs +136 -0
- package/src/cli/lease.test.mjs +140 -0
- package/src/cli/post-edit-gate.test.mjs +75 -0
- package/src/cli/qc.mjs +22 -10
- package/src/cli/run-dispatch.mjs +52 -0
- package/src/cli/run.mjs +75 -0
- package/src/cli/run.test.mjs +71 -0
- package/src/cli/work-order-guard.mjs +0 -0
- package/src/cli/work-order-guard.test.mjs +127 -2
- package/src/cli/worktree-isolation.mjs +48 -14
- package/src/cli/worktree-isolation.test.mjs +57 -0
- package/src/config.mjs +34 -0
- package/src/descriptions.mjs +2 -1
- package/src/gates/eight-blocks.mjs +5 -1
- package/src/gates/eight-blocks.test.mjs +21 -0
- package/src/gates/feature-cli.mjs +198 -0
- package/src/gates/feature-cli.test.mjs +172 -0
- package/src/gates/test-mirror.mjs +57 -37
- package/src/gates/test-mirror.test.mjs +67 -4
- package/templates/githooks/pre-push +18 -0
package/package.json
CHANGED
package/src/cli/check.mjs
CHANGED
|
@@ -17,12 +17,13 @@ import { checkClaimedRequirements } from "../gates/claimed-requirements.mjs";
|
|
|
17
17
|
import { allowedPublicRoutes, checkPublicRoutes, declaredPublicRoutes } from "../gates/public-routes.mjs";
|
|
18
18
|
import { calledInternalRoutes, checkInternalRoutes, declaredInternalRoutes } from "../gates/internal-routes.mjs";
|
|
19
19
|
import { checkHeadlessPipelines } from "../gates/headless-sagas.mjs";
|
|
20
|
+
import { checkFeatureCommands, governedFeatures } from "../gates/feature-cli.mjs";
|
|
20
21
|
import { checkSagaTests, declaredSagas } from "../gates/saga-tests.mjs";
|
|
21
22
|
import { checkGatesAreTested } from "../gates/gate-tests.mjs";
|
|
22
23
|
import { checkAuditAppendOnly } from "../gates/audit-append-only.mjs";
|
|
23
24
|
import { checkEnforcementMap } from "../gates/enforcement-map.mjs";
|
|
24
25
|
import { checkFrontendBoundaries } from "../gates/frontend-boundaries.mjs";
|
|
25
|
-
import { checkTestMirror } from "../gates/test-mirror.mjs";
|
|
26
|
+
import { checkTestMirror, mirrorFolders } from "../gates/test-mirror.mjs";
|
|
26
27
|
import { checkCommentStyle } from "../gates/comment-style.mjs";
|
|
27
28
|
import { checkEnglishSource, checkTranslationPairs } from "../gates/english-source.mjs";
|
|
28
29
|
import { checkAdrFormat } from "../gates/adr-format.mjs";
|
|
@@ -381,6 +382,25 @@ export async function runCheck(config, only) {
|
|
|
381
382
|
if (enabled(config.gates, "headless-sagas")) {
|
|
382
383
|
problems.push(...checkHeadlessPipelines(contents, { block: config.saga.headlessBlock, viewModules: config.saga.viewModules }));
|
|
383
384
|
}
|
|
385
|
+
if (enabled(config.gates, "feature-cli") && !relativeOnly) {
|
|
386
|
+
const rel = relativeTo(config.root);
|
|
387
|
+
const relativeFeatures = features.map((feature) => ({ ...feature, feature: rel(feature.feature) }));
|
|
388
|
+
const relativeContents = contents.map((file) => ({ ...file, path: rel(file.path) }));
|
|
389
|
+
problems.push(
|
|
390
|
+
...checkFeatureCommands(relativeFeatures, relativeContents, {
|
|
391
|
+
block: config.cli.block,
|
|
392
|
+
roots: config.cli.roots,
|
|
393
|
+
viewModules: config.cli.viewModules,
|
|
394
|
+
serverModules: config.cli.serverModules,
|
|
395
|
+
factory: config.cli.factory,
|
|
396
|
+
publisher: config.cli.publisher,
|
|
397
|
+
drives: config.cli.drives,
|
|
398
|
+
sagaFactories: config.cli.sagaFactories,
|
|
399
|
+
tests: relativeContents.filter((file) => TEST_FILE.test(file.path)),
|
|
400
|
+
}),
|
|
401
|
+
);
|
|
402
|
+
lines.push(`OK commands ${governedFeatures(relativeFeatures, { roots: config.cli.roots })} feature(s) drivable headless through \`qc run\``);
|
|
403
|
+
}
|
|
384
404
|
if (!rootless) lines.push(`OK structure ${features.length} feature folder(s)`);
|
|
385
405
|
|
|
386
406
|
if (relativeOnly) return { problems, lines };
|
|
@@ -482,13 +502,19 @@ export async function runCheck(config, only) {
|
|
|
482
502
|
}
|
|
483
503
|
}
|
|
484
504
|
if (enabled(config.gates, "test-mirror")) {
|
|
485
|
-
const
|
|
486
|
-
const
|
|
487
|
-
const
|
|
488
|
-
|
|
505
|
+
const { roots } = config.testMirror;
|
|
506
|
+
const walked = new Set();
|
|
507
|
+
for (const dir of mirrorFolders(roots)) {
|
|
508
|
+
for (const file of await sourceFiles(config, dir)) walked.add(file.path);
|
|
509
|
+
}
|
|
510
|
+
const all = [...walked];
|
|
511
|
+
const testPaths = all.filter((file) => TEST_FILE.test(file));
|
|
512
|
+
const srcPaths = all.filter((file) => !TEST_FILE.test(file));
|
|
513
|
+
const mirrorProblems = checkTestMirror(testPaths, srcPaths, roots);
|
|
489
514
|
problems.push(...mirrorProblems);
|
|
490
515
|
if (mirrorProblems.length === 0) {
|
|
491
|
-
|
|
516
|
+
const judged = roots.map((root) => root.tests).join(", ") || "no configured root";
|
|
517
|
+
lines.push(`OK mirror tests mirror src paths 1:1, no orphaned test, in ${judged}`);
|
|
492
518
|
}
|
|
493
519
|
}
|
|
494
520
|
|
package/src/cli/cli.test.mjs
CHANGED
|
@@ -118,3 +118,47 @@ test("one present feature root is enough", async () => {
|
|
|
118
118
|
);
|
|
119
119
|
rmSync(dir, { recursive: true, force: true });
|
|
120
120
|
});
|
|
121
|
+
|
|
122
|
+
const MIRROR_RULES = new Set(["unmirrored-test", "orphaned-test", "invalid-mirror-root"]);
|
|
123
|
+
|
|
124
|
+
/** An initialised repository with two mirror roots, and the given files under it. */
|
|
125
|
+
async function mirrored(files) {
|
|
126
|
+
const dir = repo();
|
|
127
|
+
await quiet(() => runInit(load(dir)));
|
|
128
|
+
const roots = [{ src: "frontend/src", tests: "frontend/tests" }, { src: "backend/src", tests: "backend/tests" }];
|
|
129
|
+
writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify({ testMirror: { roots } }));
|
|
130
|
+
for (const file of files) {
|
|
131
|
+
mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
|
|
132
|
+
writeFileSync(path.join(dir, file), "export {};\n");
|
|
133
|
+
}
|
|
134
|
+
return dir;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// backend/tests is in no citable root, so only a walk of the configured roots can see its orphan.
|
|
138
|
+
test("check judges every configured mirror root, including a tests folder outside the citable roots", async () => {
|
|
139
|
+
const dir = await mirrored([
|
|
140
|
+
"backend/src/orders/place.ts",
|
|
141
|
+
"backend/src/orders/place.test.ts",
|
|
142
|
+
"backend/src/orders/ship.ts",
|
|
143
|
+
"backend/tests/orders/ship.test.ts",
|
|
144
|
+
"backend/tests/orders/cancel.test.ts",
|
|
145
|
+
]);
|
|
146
|
+
const { problems } = await runCheck(load(dir));
|
|
147
|
+
const found = problems.filter((problem) => MIRROR_RULES.has(problem.rule)).map(({ path: file, rule }) => ({ file, rule }));
|
|
148
|
+
assert.deepEqual(
|
|
149
|
+
found.sort((a, b) => a.file.localeCompare(b.file)),
|
|
150
|
+
[
|
|
151
|
+
{ file: "backend/src/orders/place.test.ts", rule: "unmirrored-test" },
|
|
152
|
+
{ file: "backend/tests/orders/cancel.test.ts", rule: "orphaned-test" },
|
|
153
|
+
],
|
|
154
|
+
);
|
|
155
|
+
rmSync(dir, { recursive: true, force: true });
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("a clean mirror run names every root it judged", async () => {
|
|
159
|
+
const dir = await mirrored(["backend/src/orders/ship.ts", "backend/tests/orders/ship.test.ts"]);
|
|
160
|
+
const { lines } = await runCheck(load(dir));
|
|
161
|
+
const line = lines.find((entry) => entry.startsWith("OK mirror"));
|
|
162
|
+
assert.ok(line?.includes("frontend/tests") && line.includes("backend/tests"), `mirror line: ${line}`);
|
|
163
|
+
rmSync(dir, { recursive: true, force: true });
|
|
164
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The resource for the worktree lease: the one file that says which agent session holds a worktree.
|
|
2
|
+
// It lives in the worktree's own git dir, so it is never committed and goes away with the worktree.
|
|
3
|
+
|
|
4
|
+
import { readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
export const LEASE_FILE = "qc-agent-lease.json";
|
|
8
|
+
|
|
9
|
+
export const leaseFileOf = (gitDir) => path.join(gitDir, LEASE_FILE);
|
|
10
|
+
|
|
11
|
+
/** @returns the parsed lease, or null when there is no file or it does not parse. */
|
|
12
|
+
export function readLease(gitDir) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(readFileSync(leaseFileOf(gitDir), "utf8"));
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** @param {string | null} sessionId already validated; null claims nothing. */
|
|
21
|
+
export function claimLease(gitDir, sessionId, branch, now = Date.now()) {
|
|
22
|
+
if (!sessionId) return;
|
|
23
|
+
try {
|
|
24
|
+
const payload = { sessionId, branch, updatedAt: new Date(now).toISOString() };
|
|
25
|
+
writeFileSync(leaseFileOf(gitDir), `${JSON.stringify(payload, null, 2)}\n`);
|
|
26
|
+
} catch {
|
|
27
|
+
// A read-only or absent git dir is not a reason to block an edit.
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function removeLease(gitDir) {
|
|
32
|
+
rmSync(leaseFileOf(gitDir), { force: true });
|
|
33
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// `qc lease`, the trigger that shows and releases the worktree lease the edit guard claims.
|
|
2
|
+
// A lease another session holds is released only on purpose: by its holder, or by a person with --force.
|
|
3
|
+
|
|
4
|
+
import { execFile } from "node:child_process";
|
|
5
|
+
import { existsSync, statSync } from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { parseArgs, promisify } from "node:util";
|
|
8
|
+
import { load } from "../config.mjs";
|
|
9
|
+
import { leaseFileOf, readLease, removeLease } from "./lease-file.mjs";
|
|
10
|
+
import { OFF, duration, isolationSettings, leaseState, sessionIdOf, timestamp } from "./worktree-isolation.mjs";
|
|
11
|
+
|
|
12
|
+
const execFileAsync = promisify(execFile);
|
|
13
|
+
|
|
14
|
+
export const USAGE = `qc lease — the lease that holds a worktree for one agent session
|
|
15
|
+
|
|
16
|
+
qc lease status [path] its holder, branch, age, and the time it lapses
|
|
17
|
+
qc lease release [path] --session <id> remove the lease that this session holds
|
|
18
|
+
qc lease release [path] --force remove any lease, and print what it removed
|
|
19
|
+
|
|
20
|
+
path is any folder inside the worktree, and defaults to the current directory.`;
|
|
21
|
+
|
|
22
|
+
/** The worktree root and git dir that hold a path, or null when no checkout does. */
|
|
23
|
+
async function worktreeAt(target) {
|
|
24
|
+
const absolute = path.resolve(target);
|
|
25
|
+
const dir = existsSync(absolute) && !statSync(absolute).isDirectory() ? path.dirname(absolute) : absolute;
|
|
26
|
+
try {
|
|
27
|
+
const { stdout } = await execFileAsync("git", ["rev-parse", "--show-toplevel", "--absolute-git-dir"], { cwd: dir });
|
|
28
|
+
const [root, gitDir] = stdout.trim().split(/\r?\n/);
|
|
29
|
+
return { root, gitDir };
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const NO_TIME = "(no readable time)";
|
|
36
|
+
|
|
37
|
+
const written = (state) =>
|
|
38
|
+
Number.isNaN(state.written) ? NO_TIME : `${timestamp(state.written)}, ${duration(Math.max(state.age, 0))} ago`;
|
|
39
|
+
|
|
40
|
+
function lapses(state, now) {
|
|
41
|
+
if (Number.isNaN(state.lapsesAt)) return NO_TIME;
|
|
42
|
+
const when = now > state.lapsesAt ? `${duration(now - state.lapsesAt)} ago` : `in ${duration(state.lapsesAt - now)}`;
|
|
43
|
+
return `${timestamp(state.lapsesAt)}, ${when}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function verdict(state, settings) {
|
|
47
|
+
if (settings.require === OFF) return "inactive: swarm.isolation.require is off, so no edit reads it";
|
|
48
|
+
if (state.holder === null || Number.isNaN(state.written)) return "unreadable: it binds no session";
|
|
49
|
+
if (state.live) return "held: an edit from any other session is refused";
|
|
50
|
+
return state.age < 0 ? "written in the future: it binds no session" : "lapsed: it binds no session";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The status report as lines. `lease` is undefined when there is no file, and null when it does not parse. */
|
|
54
|
+
export function statusLines(file, lease, settings, now) {
|
|
55
|
+
if (lease === undefined) return [`no lease: ${file} does not exist`];
|
|
56
|
+
const state = leaseState(lease, now, settings.leaseHours);
|
|
57
|
+
return [
|
|
58
|
+
`lease ${file}`,
|
|
59
|
+
`holder ${state.holder ?? "(none)"}`,
|
|
60
|
+
`branch ${state.branch ?? "(none)"}`,
|
|
61
|
+
`written ${written(state)}`,
|
|
62
|
+
`lapses ${lapses(state, now)}`,
|
|
63
|
+
`state ${verdict(state, settings)}`,
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** @returns null when the release may go ahead, or the reason it may not. */
|
|
68
|
+
export function releaseRefusal(lease, { session, force }) {
|
|
69
|
+
if (force) return null;
|
|
70
|
+
const caller = sessionIdOf(session);
|
|
71
|
+
const holder = sessionIdOf(lease?.sessionId);
|
|
72
|
+
if (!caller) {
|
|
73
|
+
return `refused: pass --session <id> to release your own lease, or --force to release any lease. Holder: ${holder ?? "(none)"}.`;
|
|
74
|
+
}
|
|
75
|
+
if (holder === null) return "refused: the lease names no readable holder, so only --force removes it.";
|
|
76
|
+
if (caller === holder) return null;
|
|
77
|
+
return `refused: session ${holder} holds this lease, not ${caller}. Only its holder releases it without --force.`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function removed(file, lease, now) {
|
|
81
|
+
// A release reads no config, so a broken one cannot block it; the lapse is not needed here.
|
|
82
|
+
const state = leaseState(lease, now, Number.POSITIVE_INFINITY);
|
|
83
|
+
return `released ${file}: session ${state.holder ?? "(none)"} on "${state.branch ?? "(none)"}", written ${written(state)}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function runLease(args, now = Date.now()) {
|
|
87
|
+
let parsed;
|
|
88
|
+
try {
|
|
89
|
+
parsed = parseArgs({
|
|
90
|
+
args,
|
|
91
|
+
allowPositionals: true,
|
|
92
|
+
options: { session: { type: "string" }, force: { type: "boolean", default: false } },
|
|
93
|
+
});
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.error(`${error.message}\n\n${USAGE}`);
|
|
96
|
+
return 1;
|
|
97
|
+
}
|
|
98
|
+
const [action, target = process.cwd(), ...extra] = parsed.positionals;
|
|
99
|
+
if (!["status", "release"].includes(action) || extra.length > 0) {
|
|
100
|
+
console.log(USAGE);
|
|
101
|
+
return action === undefined || action === "help" ? 0 : 1;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const worktree = await worktreeAt(target);
|
|
105
|
+
if (!worktree) {
|
|
106
|
+
console.error(`${target} is not inside a git checkout, so it has no lease.`);
|
|
107
|
+
return 1;
|
|
108
|
+
}
|
|
109
|
+
const file = leaseFileOf(worktree.gitDir);
|
|
110
|
+
const lease = existsSync(file) ? readLease(worktree.gitDir) : undefined;
|
|
111
|
+
|
|
112
|
+
if (action === "status") {
|
|
113
|
+
let settings;
|
|
114
|
+
try {
|
|
115
|
+
settings = isolationSettings(load(worktree.root).swarm);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error(error.message);
|
|
118
|
+
return 1;
|
|
119
|
+
}
|
|
120
|
+
for (const line of statusLines(file, lease, settings, now)) console.log(line);
|
|
121
|
+
return 0;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (lease === undefined) {
|
|
125
|
+
console.log(`no lease: ${file} does not exist`);
|
|
126
|
+
return 0;
|
|
127
|
+
}
|
|
128
|
+
const refusal = releaseRefusal(lease, parsed.values);
|
|
129
|
+
if (refusal) {
|
|
130
|
+
console.error(refusal);
|
|
131
|
+
return 1;
|
|
132
|
+
}
|
|
133
|
+
removeLease(worktree.gitDir);
|
|
134
|
+
console.log(removed(file, lease, now));
|
|
135
|
+
return 0;
|
|
136
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
const QC = fileURLToPath(new URL("./qc.mjs", import.meta.url));
|
|
10
|
+
const MINUTE = 60_000;
|
|
11
|
+
const HOUR = 60 * MINUTE;
|
|
12
|
+
const HOLDER = "session-bbbb-0000-1111";
|
|
13
|
+
|
|
14
|
+
/** A primary checkout with the given isolation config, one linked worktree, and a folder in no checkout to run from. */
|
|
15
|
+
function leasedWorktree(isolation = { require: "worktree" }) {
|
|
16
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-lease-")));
|
|
17
|
+
const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe", encoding: "utf8" });
|
|
18
|
+
const primary = path.join(base, "primary");
|
|
19
|
+
const linked = path.join(base, "linked");
|
|
20
|
+
const outside = path.join(base, "outside");
|
|
21
|
+
mkdirSync(primary);
|
|
22
|
+
mkdirSync(outside);
|
|
23
|
+
git(primary, "init", "-q", "-b", "main");
|
|
24
|
+
writeFileSync(path.join(primary, "qc.config.json"), JSON.stringify({ swarm: { isolation } }));
|
|
25
|
+
git(primary, "add", ".");
|
|
26
|
+
git(primary, "-c", "user.name=qc", "-c", "user.email=qc@example.com", "commit", "-q", "-m", "init");
|
|
27
|
+
git(primary, "worktree", "add", "-q", linked, "-b", "feat/work-order");
|
|
28
|
+
const leaseFile = path.join(realpathSync.native(git(linked, "rev-parse", "--absolute-git-dir").trim()), "qc-agent-lease.json");
|
|
29
|
+
const writeLease = (ageMs, sessionId = HOLDER) => {
|
|
30
|
+
const updatedAt = new Date(Date.now() - ageMs).toISOString();
|
|
31
|
+
writeFileSync(leaseFile, JSON.stringify({ sessionId, branch: "feat/work-order", updatedAt }));
|
|
32
|
+
return Date.parse(updatedAt);
|
|
33
|
+
};
|
|
34
|
+
// Run from a folder in no checkout, so the command can only find the worktree through its path argument.
|
|
35
|
+
const qc = (...args) =>
|
|
36
|
+
spawnSync(process.execPath, [QC, "lease", ...args], { cwd: outside, input: "", encoding: "utf8" });
|
|
37
|
+
return { linked, outside, leaseFile, writeLease, qc, cleanup: () => rmSync(base, { recursive: true, force: true }) };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const iso = (ms) => new Date(ms).toISOString();
|
|
41
|
+
|
|
42
|
+
test("status reports a fresh lease: its holder, branch, age, lapse time, and that it binds", (t) => {
|
|
43
|
+
const ws = leasedWorktree();
|
|
44
|
+
t.after(ws.cleanup);
|
|
45
|
+
const written = ws.writeLease(30 * MINUTE + 10_000);
|
|
46
|
+
const result = ws.qc("status", ws.linked);
|
|
47
|
+
assert.equal(result.status, 0, result.stderr);
|
|
48
|
+
assert.match(result.stdout, new RegExp(`holder\\s+${HOLDER}`));
|
|
49
|
+
assert.match(result.stdout, /branch\s+feat\/work-order/);
|
|
50
|
+
assert.ok(result.stdout.includes(`${iso(written)}, 30m ago`), result.stdout);
|
|
51
|
+
assert.ok(result.stdout.includes(`${iso(written + 8 * HOUR)}, in 7h 29m`), result.stdout);
|
|
52
|
+
assert.match(result.stdout, /state\s+held/);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("status reads leaseHours from the worktree's own config, and reports a lapsed lease as lapsed", (t) => {
|
|
56
|
+
const ws = leasedWorktree({ require: "worktree", leaseHours: 1 });
|
|
57
|
+
t.after(ws.cleanup);
|
|
58
|
+
const written = ws.writeLease(2 * HOUR + 10_000);
|
|
59
|
+
const result = ws.qc("status", ws.linked);
|
|
60
|
+
assert.equal(result.status, 0, result.stderr);
|
|
61
|
+
assert.ok(result.stdout.includes(`${iso(written + HOUR)}, 1h 0m ago`), result.stdout);
|
|
62
|
+
assert.match(result.stdout, /state\s+lapsed/);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("status and release on a worktree with no lease both say so", (t) => {
|
|
66
|
+
const ws = leasedWorktree();
|
|
67
|
+
t.after(ws.cleanup);
|
|
68
|
+
for (const args of [["status", ws.linked], ["release", ws.linked, "--force"]]) {
|
|
69
|
+
const result = ws.qc(...args);
|
|
70
|
+
assert.equal(result.status, 0, result.stderr);
|
|
71
|
+
assert.match(result.stdout, /no lease/);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("release without --force refuses a lease that another session holds", (t) => {
|
|
76
|
+
const ws = leasedWorktree();
|
|
77
|
+
t.after(ws.cleanup);
|
|
78
|
+
ws.writeLease(5 * MINUTE);
|
|
79
|
+
const result = ws.qc("release", ws.linked, "--session", "session-aaaa");
|
|
80
|
+
assert.equal(result.status, 1);
|
|
81
|
+
assert.ok(result.stderr.includes(HOLDER), result.stderr);
|
|
82
|
+
assert.equal(existsSync(ws.leaseFile), true, "a refused release removed the lease");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("release with neither --session nor --force refuses, and names both", (t) => {
|
|
86
|
+
const ws = leasedWorktree();
|
|
87
|
+
t.after(ws.cleanup);
|
|
88
|
+
ws.writeLease(5 * MINUTE);
|
|
89
|
+
const result = ws.qc("release", ws.linked);
|
|
90
|
+
assert.equal(result.status, 1);
|
|
91
|
+
assert.match(result.stderr, /--session/);
|
|
92
|
+
assert.match(result.stderr, /--force/);
|
|
93
|
+
assert.equal(existsSync(ws.leaseFile), true);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("release by the holder's own session removes the lease", (t) => {
|
|
97
|
+
const ws = leasedWorktree();
|
|
98
|
+
t.after(ws.cleanup);
|
|
99
|
+
ws.writeLease(5 * MINUTE);
|
|
100
|
+
const result = ws.qc("release", ws.linked, "--session", HOLDER);
|
|
101
|
+
assert.equal(result.status, 0, result.stderr);
|
|
102
|
+
assert.equal(existsSync(ws.leaseFile), false);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("release --force removes another session's lease, and prints what it removed", (t) => {
|
|
106
|
+
const ws = leasedWorktree();
|
|
107
|
+
t.after(ws.cleanup);
|
|
108
|
+
const written = ws.writeLease(5 * MINUTE);
|
|
109
|
+
const result = ws.qc("release", ws.linked, "--force");
|
|
110
|
+
assert.equal(result.status, 0, result.stderr);
|
|
111
|
+
assert.equal(existsSync(ws.leaseFile), false);
|
|
112
|
+
assert.ok(result.stdout.includes(HOLDER), result.stdout);
|
|
113
|
+
assert.ok(result.stdout.includes("feat/work-order"), result.stdout);
|
|
114
|
+
assert.ok(result.stdout.includes(iso(written)), result.stdout);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("release reads no config, so a qc.config.json that does not parse cannot block it", (t) => {
|
|
118
|
+
const ws = leasedWorktree();
|
|
119
|
+
t.after(ws.cleanup);
|
|
120
|
+
ws.writeLease(5 * MINUTE);
|
|
121
|
+
for (const folder of [ws.outside, ws.linked]) writeFileSync(path.join(folder, "qc.config.json"), "{ not json");
|
|
122
|
+
const result = ws.qc("release", ws.linked, "--force");
|
|
123
|
+
assert.equal(result.status, 0, result.stderr);
|
|
124
|
+
assert.equal(existsSync(ws.leaseFile), false);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("the qc usage lists both lease commands", () => {
|
|
128
|
+
const result = spawnSync(process.execPath, [QC, "help"], { encoding: "utf8" });
|
|
129
|
+
assert.equal(result.status, 0, result.stderr);
|
|
130
|
+
assert.match(result.stdout, /qc lease status \[path\]/);
|
|
131
|
+
assert.match(result.stdout, /qc lease release \[path\]/);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("a path in no git checkout is a named error, not a silent pass", (t) => {
|
|
135
|
+
const ws = leasedWorktree();
|
|
136
|
+
t.after(ws.cleanup);
|
|
137
|
+
const result = ws.qc("status", ws.outside);
|
|
138
|
+
assert.equal(result.status, 1);
|
|
139
|
+
assert.match(result.stderr, /not inside a git checkout/);
|
|
140
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
const HOOK = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "..", "..", "..", "hooks", "post-edit-gate.sh");
|
|
10
|
+
|
|
11
|
+
/** A session's project checkout, one linked worktree, a checkout with no config, a folder in none, and a qc CLI that records where it ran. */
|
|
12
|
+
function workspace() {
|
|
13
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-post-edit-")));
|
|
14
|
+
const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe" });
|
|
15
|
+
const project = path.join(base, "project");
|
|
16
|
+
const linked = path.join(base, "linked");
|
|
17
|
+
const plain = path.join(base, "plain");
|
|
18
|
+
const outside = path.join(base, "outside", "features", "x");
|
|
19
|
+
for (const dir of [project, plain, outside]) mkdirSync(dir, { recursive: true });
|
|
20
|
+
git(project, "init", "-q", "-b", "main");
|
|
21
|
+
writeFileSync(path.join(project, "qc.config.json"), "{}");
|
|
22
|
+
git(project, "add", ".");
|
|
23
|
+
git(project, "-c", "user.name=qc", "-c", "user.email=qc@example.com", "commit", "-q", "-m", "init");
|
|
24
|
+
git(project, "worktree", "add", "-q", linked, "-b", "feat/work-order");
|
|
25
|
+
git(plain, "init", "-q", "-b", "main");
|
|
26
|
+
|
|
27
|
+
const log = path.join(base, "qc-ran.log");
|
|
28
|
+
const cli = path.join(base, "plugin", "packages", "qc-harness", "src", "cli");
|
|
29
|
+
mkdirSync(cli, { recursive: true });
|
|
30
|
+
writeFileSync(
|
|
31
|
+
path.join(cli, "qc.mjs"),
|
|
32
|
+
`import { appendFileSync } from "node:fs";\nappendFileSync(${JSON.stringify(log)}, process.cwd() + String.fromCharCode(10));\nprocess.exit(1);\n`,
|
|
33
|
+
);
|
|
34
|
+
const ranIn = () => (existsSync(log) ? readFileSync(log, "utf8").trim().split("\n").map((dir) => realpathSync.native(dir)) : []);
|
|
35
|
+
return { project, linked, plain, outside, pluginRoot: path.join(base, "plugin"), ranIn, cleanup: () => rmSync(base, { recursive: true, force: true }) };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function runHook(ws, file) {
|
|
39
|
+
const featureFile = path.join(file, "features", "tasks", "index.ts");
|
|
40
|
+
mkdirSync(path.dirname(featureFile), { recursive: true });
|
|
41
|
+
writeFileSync(featureFile, "export {};\n");
|
|
42
|
+
return spawnSync("bash", [HOOK], {
|
|
43
|
+
input: JSON.stringify({ tool_input: { file_path: featureFile } }),
|
|
44
|
+
env: { ...process.env, CLAUDE_PROJECT_DIR: ws.project, CLAUDE_PLUGIN_ROOT: ws.pluginRoot },
|
|
45
|
+
encoding: "utf8",
|
|
46
|
+
}).status;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
test("a feature file in no git checkout runs no gate, even when the session's project has adopted the kit", (t) => {
|
|
50
|
+
const ws = workspace();
|
|
51
|
+
t.after(ws.cleanup);
|
|
52
|
+
assert.equal(runHook(ws, ws.outside), 0);
|
|
53
|
+
assert.deepEqual(ws.ranIn(), []);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("a feature file in a linked worktree is checked from that worktree, not the session's project", (t) => {
|
|
57
|
+
const ws = workspace();
|
|
58
|
+
t.after(ws.cleanup);
|
|
59
|
+
assert.equal(runHook(ws, path.join(ws.linked, "backend", "src")), 2);
|
|
60
|
+
assert.deepEqual(ws.ranIn(), [ws.linked]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("a feature file in a checkout with no qc.config.json runs no gate", (t) => {
|
|
64
|
+
const ws = workspace();
|
|
65
|
+
t.after(ws.cleanup);
|
|
66
|
+
assert.equal(runHook(ws, path.join(ws.plain, "backend", "src")), 0);
|
|
67
|
+
assert.deepEqual(ws.ranIn(), []);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("a feature file in the session's own project is checked from the project", (t) => {
|
|
71
|
+
const ws = workspace();
|
|
72
|
+
t.after(ws.cleanup);
|
|
73
|
+
assert.equal(runHook(ws, path.join(ws.project, "backend", "src")), 2);
|
|
74
|
+
assert.deepEqual(ws.ranIn(), [ws.project]);
|
|
75
|
+
});
|
package/src/cli/qc.mjs
CHANGED
|
@@ -7,21 +7,29 @@ import { runCheck } from "./check.mjs";
|
|
|
7
7
|
|
|
8
8
|
const USAGE = `qc — architecture gates
|
|
9
9
|
|
|
10
|
-
qc check [file]
|
|
11
|
-
qc init
|
|
12
|
-
qc feature <name>
|
|
13
|
-
qc
|
|
14
|
-
qc
|
|
15
|
-
qc
|
|
16
|
-
qc
|
|
17
|
-
qc
|
|
18
|
-
qc
|
|
19
|
-
qc
|
|
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 run [name] drive one feature command headless; no name lists them
|
|
14
|
+
qc install-hooks point git at the kit's pre-commit hook
|
|
15
|
+
qc work-order-check refuse to commit on the wrong branch, per .claude/work-order.local.json
|
|
16
|
+
qc lease status [path] who holds this worktree's lease, since when, and when it lapses
|
|
17
|
+
qc lease release [path] remove it: --session <id> for your own, --force for another's
|
|
18
|
+
qc commit-msg <file> refuse a commit message that credits a tool as an author
|
|
19
|
+
qc decisions [id] where decisions are cited; --squash closes the gaps
|
|
20
|
+
qc enforcement-map refresh the generated rule/gate table in docs/enforcement.md
|
|
21
|
+
qc doctor the hooks and the lockfile must run the same harness
|
|
22
|
+
qc config print the resolved configuration
|
|
20
23
|
|
|
21
24
|
Every gate reads qc.config.json. A key it does not set keeps the reference default.`;
|
|
22
25
|
|
|
23
26
|
async function main() {
|
|
24
27
|
const [command, ...rest] = process.argv.slice(2);
|
|
28
|
+
// A broken qc.config.json must not keep a lease in place, so the lease runs before the load.
|
|
29
|
+
if (command === "lease") {
|
|
30
|
+
const { runLease } = await import("./lease.mjs");
|
|
31
|
+
process.exit(await runLease(rest));
|
|
32
|
+
}
|
|
25
33
|
const config = load(process.cwd());
|
|
26
34
|
|
|
27
35
|
switch (command) {
|
|
@@ -47,6 +55,10 @@ async function main() {
|
|
|
47
55
|
await runScaffold(config, rest);
|
|
48
56
|
return;
|
|
49
57
|
}
|
|
58
|
+
case "run": {
|
|
59
|
+
const { runRun } = await import("./run.mjs");
|
|
60
|
+
process.exit(await runRun(config, rest));
|
|
61
|
+
}
|
|
50
62
|
case "install-hooks": {
|
|
51
63
|
const { installHooks } = await import("./install-hooks.mjs");
|
|
52
64
|
await installHooks(config);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Runs inside the repository's loader, so it may import a TypeScript registry. Kept separate from
|
|
2
|
+
// run.mjs for exactly that reason: the parent process must not need the loader to decide anything.
|
|
3
|
+
|
|
4
|
+
import process from "node:process";
|
|
5
|
+
import { pathToFileURL } from "node:url";
|
|
6
|
+
import { parseInput } from "./run.mjs";
|
|
7
|
+
|
|
8
|
+
/** Every command the registry publishes, sorted, one per line — what `qc run` with no name prints. */
|
|
9
|
+
export function listing(commands) {
|
|
10
|
+
return Object.keys(commands)
|
|
11
|
+
.sort()
|
|
12
|
+
.map((key) => {
|
|
13
|
+
const describe = commands[key]?.describe;
|
|
14
|
+
return describe === undefined ? ` ${key}` : ` ${key.padEnd(38)}${describe}`;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function dispatch(registry, name, argv) {
|
|
19
|
+
const module = await import(pathToFileURL(registry).href);
|
|
20
|
+
const commands = module.commands ?? module.default;
|
|
21
|
+
if (commands === undefined || typeof commands !== "object") {
|
|
22
|
+
console.error(`qc run: ${registry} exports no \`commands\` record`);
|
|
23
|
+
return 1;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (name === undefined) {
|
|
27
|
+
console.log("qc run <feature>.<command> [--json '<payload>'] [--key value]\n");
|
|
28
|
+
for (const line of listing(commands)) console.log(line);
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const command = commands[name];
|
|
33
|
+
if (command === undefined) {
|
|
34
|
+
console.error(`qc run: no command \`${name}\`. Known commands:\n${listing(commands).join("\n")}`);
|
|
35
|
+
return 1;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const result = await command.run(parseInput(argv));
|
|
39
|
+
if (result !== undefined) console.log(JSON.stringify(result, null, 2));
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Spawned by run.mjs under the repository's loader: argv is [registry, name?, ...rest].
|
|
44
|
+
if (process.argv[1] === new URL(import.meta.url).pathname) {
|
|
45
|
+
const [registry, name, ...rest] = process.argv.slice(2);
|
|
46
|
+
try {
|
|
47
|
+
process.exit(await dispatch(registry, name, rest));
|
|
48
|
+
} catch (cause) {
|
|
49
|
+
console.error(`qc run: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
}
|