architecture-harness 0.16.0 → 0.18.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 +8 -3
- package/src/cli/bash-command-guard.mjs +25 -10
- package/src/cli/bash-command-guard.test.mjs +30 -0
- package/src/cli/bash-edit-guard.mjs +28 -7
- package/src/cli/bash-edit-guard.test.mjs +24 -0
- package/src/cli/budget-counter.mjs +25 -4
- package/src/cli/budget-guard.mjs +73 -4
- package/src/cli/budget-guard.test.mjs +111 -0
- package/src/cli/budget.mjs +3 -2
- package/src/cli/check-registries.test.mjs +181 -0
- package/src/cli/check.mjs +130 -9
- package/src/cli/cli.test.mjs +215 -2
- package/src/cli/context-signal.mjs +92 -0
- package/src/cli/context-signal.test.mjs +99 -0
- package/src/cli/contract-history.mjs +46 -0
- package/src/cli/contract-history.test.mjs +118 -0
- package/src/cli/controller-guard.mjs +73 -0
- package/src/cli/controller-guard.test.mjs +119 -0
- package/src/cli/dispatch-guard.mjs +14 -4
- package/src/cli/dispatch-guard.test.mjs +5 -6
- package/src/cli/dispatch-reminder.mjs +19 -11
- package/src/cli/dispatch-reminder.test.mjs +32 -0
- package/src/cli/dispatch.mjs +23 -2
- package/src/cli/dispatch.test.mjs +39 -1
- package/src/cli/doctor.mjs +2 -1
- package/src/cli/explore-guard.mjs +2 -2
- package/src/cli/explore-hint.mjs +67 -7
- package/src/cli/explore-hint.test.mjs +104 -4
- package/src/cli/gh-merge-command.mjs +228 -0
- package/src/cli/gh-merge-command.test.mjs +273 -0
- package/src/cli/gh-run.mjs +30 -0
- package/src/cli/gh-run.test.mjs +22 -0
- package/src/cli/git-hooks.test.mjs +28 -0
- package/src/cli/git-read.mjs +42 -0
- package/src/cli/git-read.test.mjs +94 -0
- package/src/cli/handoff-note.mjs +69 -0
- package/src/cli/handoff-note.test.mjs +71 -0
- package/src/cli/init.mjs +7 -3
- package/src/cli/issue-gate.mjs +169 -0
- package/src/cli/issue-gate.test.mjs +234 -0
- package/src/cli/ledger.mjs +138 -0
- package/src/cli/ledger.test.mjs +127 -0
- package/src/cli/merge-guard.mjs +193 -0
- package/src/cli/merge-guard.test.mjs +328 -0
- package/src/cli/plan-check.mjs +151 -0
- package/src/cli/plan-check.test.mjs +143 -0
- package/src/cli/plan-stop.mjs +101 -0
- package/src/cli/plan-stop.test.mjs +136 -0
- package/src/cli/plugin-agents.test.mjs +105 -8
- package/src/cli/pr-check.mjs +17 -5
- package/src/cli/pr-check.test.mjs +85 -4
- package/src/cli/prose.mjs +192 -0
- package/src/cli/prose.test.mjs +248 -0
- package/src/cli/qc.mjs +22 -0
- package/src/cli/registry-history.mjs +1 -1
- package/src/cli/report-stash.mjs +69 -0
- package/src/cli/report-stash.test.mjs +71 -0
- package/src/cli/report-stop.mjs +266 -0
- package/src/cli/report-stop.test.mjs +471 -0
- package/src/cli/shell-command.mjs +81 -6
- package/src/cli/shell-command.test.mjs +101 -1
- package/src/cli/task-gate.mjs +179 -0
- package/src/cli/task-gate.test.mjs +507 -0
- package/src/cli/tokens.mjs +239 -0
- package/src/cli/tokens.test.mjs +286 -0
- package/src/cli/transcript-usage.mjs +135 -0
- package/src/cli/transcript-usage.test.mjs +129 -0
- package/src/cli/workflow-hooks.test.mjs +72 -0
- package/src/cli/workflow-place.mjs +143 -0
- package/src/cli/workflow-place.test.mjs +146 -0
- package/src/cli/workflow-settings.mjs +77 -0
- package/src/cli/workflow-settings.test.mjs +79 -0
- package/src/cli/worktree.mjs +1 -1
- package/src/cli/worktree.test.mjs +19 -2
- package/src/config.mjs +99 -4
- package/src/config.test.mjs +38 -3
- package/src/descriptions.mjs +5 -1
- package/src/eslint/index.mjs +2 -0
- package/src/eslint/jsx.mjs +57 -0
- package/src/eslint/preset.mjs +32 -6
- package/src/eslint/preset.test.mjs +60 -0
- package/src/eslint/rules/durable-idempotency-key.mjs +26 -0
- package/src/eslint/rules/ime-safe-key.mjs +1 -50
- package/src/eslint/rules/mutation-control-pending.mjs +128 -0
- package/src/eslint/rules/no-supersession-trail.mjs +8 -4
- package/src/eslint/rules/registry-literal.mjs +36 -2
- package/src/eslint/rules.test.mjs +153 -0
- package/src/gates/call-args.mjs +90 -0
- package/src/gates/call-args.test.mjs +21 -0
- package/src/gates/config-floor.mjs +41 -20
- package/src/gates/config-floor.test.mjs +71 -1
- package/src/gates/contract-idempotency.mjs +103 -0
- package/src/gates/contract-idempotency.test.mjs +276 -0
- package/src/gates/contract-operations.mjs +120 -0
- package/src/gates/contract-operations.test.mjs +275 -0
- package/src/gates/contract-routes.mjs +82 -0
- package/src/gates/contract-routes.test.mjs +210 -0
- package/src/gates/registry-literal.mjs +79 -15
- package/src/gates/registry-literal.test.mjs +89 -0
- package/src/gates/saga-key.mjs +253 -0
- package/src/gates/saga-key.test.mjs +322 -0
- package/src/gates/scrub.mjs +108 -0
- package/src/gates/scrub.test.mjs +29 -0
- package/src/gates/tenant-isolation-test.mjs +80 -0
- package/src/gates/tenant-isolation-test.test.mjs +129 -0
- package/src/gates/tenant-predicate.mjs +644 -91
- package/src/gates/tenant-predicate.test.mjs +662 -1
- package/src/prose-lines.mjs +49 -0
- package/src/prose-lines.test.mjs +63 -0
- package/src/registries.mjs +89 -0
- package/src/registries.test.mjs +44 -0
- package/src/vale-style.test.mjs +158 -0
- package/templates/docs/enforcement.md +26 -2
- package/templates/github/workflows/ci.yml +18 -1
- package/templates/quality-thresholds.json +1 -1
- package/templates/vale/.vale.ini +7 -0
- package/templates/vale/styles/Qc/AndOr.yml +7 -0
- package/templates/vale/styles/Qc/InOrderTo.yml +6 -0
- package/templates/vale/styles/Qc/Latin.yml +9 -0
- package/templates/vale/styles/Qc/PassiveVoice.yml +21 -0
- package/templates/vale/styles/Qc/Please.yml +6 -0
- package/templates/vale/styles/Qc/Progress.yml +9 -0
- package/templates/vale/styles/Qc/SentenceLength.yml +6 -0
- package/templates/vale/styles/Qc/Utilize.yml +10 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { 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 { branchAt, branchesAt, commitOf, gitOut, gitOutWithin, linkedWorktrees, pathKey } from "./git-read.mjs";
|
|
8
|
+
|
|
9
|
+
const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe", encoding: "utf8" }).trim();
|
|
10
|
+
|
|
11
|
+
// A timed-out git call can leave a child that holds the folder for a moment, so a removal retries.
|
|
12
|
+
// rmSync's own retries do not cover the EPERM Windows raises while the child still holds the folder, so a
|
|
13
|
+
// failed removal waits and tries again.
|
|
14
|
+
function removeTree(dir) {
|
|
15
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
16
|
+
try {
|
|
17
|
+
rmSync(dir, { recursive: true, force: true, maxRetries: 20, retryDelay: 250 });
|
|
18
|
+
return;
|
|
19
|
+
} catch (error) {
|
|
20
|
+
if (attempt >= 20) throw error;
|
|
21
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 250);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function repo(t) {
|
|
27
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-git-read-")));
|
|
28
|
+
t.after(() => removeTree(base));
|
|
29
|
+
const main = path.join(base, "main");
|
|
30
|
+
git(base, "init", "-q", "-b", "main", main);
|
|
31
|
+
for (const [key, value] of [["user.name", "qc"], ["user.email", "qc@example.com"], ["commit.gpgsign", "false"]]) {
|
|
32
|
+
git(main, "config", key, value);
|
|
33
|
+
}
|
|
34
|
+
writeFileSync(path.join(main, "a.txt"), "a\n");
|
|
35
|
+
git(main, "add", ".");
|
|
36
|
+
git(main, "commit", "-q", "-m", "init");
|
|
37
|
+
const linked = path.join(base, "linked");
|
|
38
|
+
git(main, "worktree", "add", "-q", "-b", "feat/1-x", linked);
|
|
39
|
+
return { main, linked, head: git(main, "rev-parse", "HEAD") };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test("commitOf resolves a short or upper-case sha, and refuses one the repository lacks", (t) => {
|
|
43
|
+
const { main, head } = repo(t);
|
|
44
|
+
assert.equal(commitOf(main, head.slice(0, 7)), head);
|
|
45
|
+
assert.equal(commitOf(main, head.slice(0, 9).toUpperCase()), head);
|
|
46
|
+
assert.equal(commitOf(main, "deadbee"), null);
|
|
47
|
+
assert.equal(gitOut(main, "no-such-subcommand"), null);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("branchAt names the branch, and a detached head has none", (t) => {
|
|
51
|
+
const { main, linked } = repo(t);
|
|
52
|
+
assert.equal(branchAt(linked), "feat/1-x");
|
|
53
|
+
assert.equal(branchAt(main), "main");
|
|
54
|
+
git(linked, "checkout", "-q", "--detach");
|
|
55
|
+
assert.equal(branchAt(linked), null);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("branchesAt lists every local branch whose head is the sha", (t) => {
|
|
59
|
+
const { main, head } = repo(t);
|
|
60
|
+
assert.deepEqual(branchesAt(main, head), ["feat/1-x", "main"]);
|
|
61
|
+
assert.deepEqual(branchesAt(main, "0".repeat(40)), []);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("linkedWorktrees leaves out the primary checkout and names each branch and head", (t) => {
|
|
65
|
+
const { main, linked, head } = repo(t);
|
|
66
|
+
const found = linkedWorktrees(main);
|
|
67
|
+
assert.equal(found.length, 1);
|
|
68
|
+
assert.equal(pathKey(found[0].path), pathKey(linked));
|
|
69
|
+
assert.equal(found[0].branch, "feat/1-x");
|
|
70
|
+
assert.equal(found[0].head, head);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("pathKey spells one folder one way", (t) => {
|
|
74
|
+
const { linked } = repo(t);
|
|
75
|
+
assert.equal(pathKey(linked.replaceAll("\\", "/")), pathKey(linked));
|
|
76
|
+
if (process.platform === "win32") assert.equal(pathKey(linked.toUpperCase()), pathKey(linked));
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("linkedWorktrees run from a linked worktree still leaves out the primary checkout", (t) => {
|
|
80
|
+
const { main, linked } = repo(t);
|
|
81
|
+
const found = linkedWorktrees(linked);
|
|
82
|
+
assert.equal(found.length, 1);
|
|
83
|
+
assert.equal(pathKey(found[0].path), pathKey(linked));
|
|
84
|
+
assert.notEqual(pathKey(found[0].path), pathKey(main));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("a git call that outlives its timeout answers null", (t) => {
|
|
88
|
+
const { main } = repo(t);
|
|
89
|
+
const slow = ["-c", 'alias.slow=!node -e "setTimeout(() => {}, 3000)"', "slow"];
|
|
90
|
+
const started = Date.now();
|
|
91
|
+
assert.equal(gitOutWithin(200, main, ...slow), null);
|
|
92
|
+
assert.ok(Date.now() - started < 2500);
|
|
93
|
+
assert.equal(gitOutWithin(10_000, main, "rev-parse", "--is-inside-work-tree"), "true");
|
|
94
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// The note an agent writes when it stops mid-task, and the check a hook runs on it. A fresh agent resumes
|
|
2
|
+
// from the note, so the note names the brief, the place, the head, what is done, and what comes next.
|
|
3
|
+
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { nativePath } from "./workflow-place.mjs";
|
|
7
|
+
|
|
8
|
+
export const HANDOFF_LINE = /^[ \t]*HANDOFF:[ \t]*(\S.*?)[ \t]*$/m;
|
|
9
|
+
const QUOTES = /^["'`]|["'`]$/g;
|
|
10
|
+
const FIELDS = ["Brief", "Worktree", "Branch", "Head"];
|
|
11
|
+
const SECTIONS = [["Done", "done"], ["Left", "left"], ["Next step", "next"]];
|
|
12
|
+
const SHA = /^[0-9a-fA-F]{7,40}$/;
|
|
13
|
+
const fieldLine = (name) => new RegExp(`^[ \\t]*${name}:[ \\t]*(\\S.*?)[ \\t]*$`, "im");
|
|
14
|
+
|
|
15
|
+
/** The path on a report's or a prompt's `HANDOFF:` line, without quotes, or null. */
|
|
16
|
+
export function handoffPathOf(text) {
|
|
17
|
+
const match = HANDOFF_LINE.exec(String(text ?? ""));
|
|
18
|
+
return match ? match[1].replace(QUOTES, "") : null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The absolute path a spelled note path names, with a Git Bash drive path in Windows spelling. */
|
|
22
|
+
export const resolveHandoff = (spelled, cwd) => path.resolve(cwd, nativePath(spelled));
|
|
23
|
+
|
|
24
|
+
/** The lines under each `## <name>` heading, blank lines dropped. */
|
|
25
|
+
function sectionsOf(markdown) {
|
|
26
|
+
const sections = {};
|
|
27
|
+
let name = null;
|
|
28
|
+
for (const line of markdown.split(/\r?\n/)) {
|
|
29
|
+
const heading = /^##[ \t]+(.+?)[ \t]*$/.exec(line);
|
|
30
|
+
if (heading) {
|
|
31
|
+
name = heading[1];
|
|
32
|
+
sections[name] = [];
|
|
33
|
+
} else if (name !== null && line.trim() !== "") {
|
|
34
|
+
sections[name].push(line);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return sections;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The note's four lines and its three sections; a missing one is null. */
|
|
41
|
+
export function parseHandoff(markdown) {
|
|
42
|
+
const note = Object.fromEntries(FIELDS.map((name) => [name.toLowerCase(), fieldLine(name).exec(markdown)?.[1] ?? null]));
|
|
43
|
+
const sections = sectionsOf(markdown);
|
|
44
|
+
for (const [heading, key] of SECTIONS) note[key] = sections[heading] ?? null;
|
|
45
|
+
return note;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** @returns each thing the note lacks, as a phrase; empty when the note is complete. */
|
|
49
|
+
export function handoffProblems(markdown) {
|
|
50
|
+
const note = parseHandoff(markdown);
|
|
51
|
+
const problems = FIELDS.filter((name) => !note[name.toLowerCase()]).map((name) => `a line \`${name}: <value>\``);
|
|
52
|
+
if (note.head && !SHA.test(note.head)) problems.push(`a \`Head:\` sha of 7 to 40 hex digits, not "${note.head}"`);
|
|
53
|
+
for (const [heading, key] of SECTIONS) {
|
|
54
|
+
if (!note[key] || note[key].length === 0) problems.push(`a \`## ${heading}\` section with at least one line`);
|
|
55
|
+
}
|
|
56
|
+
return problems;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Reads the note a `HANDOFF:` line names. `missing` is the read error's code when the file does not read. */
|
|
60
|
+
export function checkHandoffFile(spelled, cwd) {
|
|
61
|
+
const file = resolveHandoff(spelled, cwd);
|
|
62
|
+
let markdown;
|
|
63
|
+
try {
|
|
64
|
+
markdown = readFileSync(file, "utf8");
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return { file, missing: error.code ?? error.message, problems: [] };
|
|
67
|
+
}
|
|
68
|
+
return { file, missing: null, problems: handoffProblems(markdown) };
|
|
69
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { checkHandoffFile, handoffPathOf, handoffProblems, parseHandoff, resolveHandoff } from "./handoff-note.mjs";
|
|
7
|
+
|
|
8
|
+
const NOTE = [
|
|
9
|
+
"# Hand-off: Task 3",
|
|
10
|
+
"Brief: C:/scratch/briefs/task-3.md",
|
|
11
|
+
"Worktree: C:/work/kit-85",
|
|
12
|
+
"Branch: feat/85-context",
|
|
13
|
+
"Head: 1a2b3c4",
|
|
14
|
+
"",
|
|
15
|
+
"## Done",
|
|
16
|
+
"- 1a2b3c4 the reader",
|
|
17
|
+
"",
|
|
18
|
+
"## Left",
|
|
19
|
+
"1. wire the guard",
|
|
20
|
+
"",
|
|
21
|
+
"## Next step",
|
|
22
|
+
"node --test packages/qc-harness/src/cli/budget-guard.test.mjs",
|
|
23
|
+
].join("\n");
|
|
24
|
+
|
|
25
|
+
test("a complete note parses and has no problem", () => {
|
|
26
|
+
const note = parseHandoff(NOTE);
|
|
27
|
+
assert.equal(note.head, "1a2b3c4");
|
|
28
|
+
assert.equal(note.branch, "feat/85-context");
|
|
29
|
+
assert.deepEqual(note.left, ["1. wire the guard"]);
|
|
30
|
+
assert.deepEqual(handoffProblems(NOTE), []);
|
|
31
|
+
assert.deepEqual(handoffProblems(NOTE.replaceAll("\n", "\r\n")), []);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("each missing line and each empty section is named", () => {
|
|
35
|
+
const problems = handoffProblems(NOTE.replace("Head: 1a2b3c4\n", "").replace("1. wire the guard\n", ""));
|
|
36
|
+
assert.ok(problems.includes("a line `Head: <value>`"), problems.join("; "));
|
|
37
|
+
assert.ok(problems.includes("a `## Left` section with at least one line"), problems.join("; "));
|
|
38
|
+
assert.match(handoffProblems(NOTE.replace("Head: 1a2b3c4", "Head: tbd")).join("; "), /Head:` sha of 7 to 40 hex digits/);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("the HANDOFF line is read from a report or a prompt, with or without quotes", () => {
|
|
42
|
+
assert.equal(handoffPathOf("DONE_WITH_CONCERNS\nHANDOFF: C:/scratch/handoffs/t3-1.md\n"), "C:/scratch/handoffs/t3-1.md");
|
|
43
|
+
assert.equal(handoffPathOf('HANDOFF: "/tmp/handoffs/a b.md"'), "/tmp/handoffs/a b.md");
|
|
44
|
+
assert.equal(handoffPathOf("no line"), null);
|
|
45
|
+
assert.equal(resolveHandoff("notes/a.md", path.resolve("/base")), path.resolve("/base", "notes/a.md"));
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("the marker is spelled HANDOFF, in capitals", () => {
|
|
49
|
+
assert.equal(handoffPathOf("Handoff: none"), null);
|
|
50
|
+
assert.equal(handoffPathOf("handoff: C:/scratch/handoffs/t3-1.md"), null);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("a HANDOFF word inside a line is not the HANDOFF line", () => {
|
|
54
|
+
assert.equal(handoffPathOf("see the HANDOFF: C:/scratch/handoffs/t3-1.md above"), null);
|
|
55
|
+
assert.equal(handoffPathOf("see the HANDOFF: C:/scratch/handoffs/t3-1.md above\nHANDOFF: C:/scratch/handoffs/real.md"), "C:/scratch/handoffs/real.md");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("a field name inside a line is not that field, and the field line further down still counts", () => {
|
|
59
|
+
assert.equal(parseHandoff("the Head: 1a2b3c4 was tbd").head, null);
|
|
60
|
+
assert.equal(parseHandoff("the Head: deadbeef was tbd\nHead: 1a2b3c4").head, "1a2b3c4");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("checkHandoffFile reads the note, and a missing file is one problem", (t) => {
|
|
64
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-handoff-")));
|
|
65
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
66
|
+
const file = path.join(base, "note.md");
|
|
67
|
+
writeFileSync(file, NOTE);
|
|
68
|
+
assert.deepEqual(checkHandoffFile(file, base), { file, missing: null, problems: [] });
|
|
69
|
+
const gone = checkHandoffFile(path.join(base, "gone.md"), base);
|
|
70
|
+
assert.equal(gone.missing, "ENOENT");
|
|
71
|
+
});
|
package/src/cli/init.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { copyFile, mkdir, readdir, readFile, writeFile } from "node:fs/promises";
|
|
6
6
|
import { withEnforcementMap } from "../enforcement-map.mjs";
|
|
7
|
-
import { existsSync } from "node:fs";
|
|
7
|
+
import { existsSync, readdirSync } from "node:fs";
|
|
8
8
|
import path from "node:path";
|
|
9
9
|
import { fileURLToPath } from "node:url";
|
|
10
10
|
|
|
@@ -27,8 +27,12 @@ const FILES = [
|
|
|
27
27
|
["docs/glossary.md", "docs/glossary.md"],
|
|
28
28
|
["docs/ui.md", "docs/ui.md"],
|
|
29
29
|
["githooks/pre-commit", ".githooks/pre-commit"],
|
|
30
|
+
["githooks/commit-msg", ".githooks/commit-msg"],
|
|
30
31
|
["githooks/pre-push", ".githooks/pre-push"],
|
|
31
32
|
["github/workflows/ci.yml", ".github/workflows/ci.yml"],
|
|
33
|
+
// The prose style `qc prose` hands to Vale: one config and one file per rule.
|
|
34
|
+
["vale/.vale.ini", ".vale/.vale.ini"],
|
|
35
|
+
...readdirSync(path.join(templates, "vale/styles/Qc")).map((rule) => [`vale/styles/Qc/${rule}`, `.vale/styles/Qc/${rule}`]),
|
|
32
36
|
];
|
|
33
37
|
|
|
34
38
|
// The enforcement map is filled in on the way out, so a rule added to the kit cannot ship a
|
|
@@ -71,7 +75,7 @@ export async function runInit(config, args = []) {
|
|
|
71
75
|
results.push(await copyTemplate(path.join(templates, source), path.join(config.root, target), force, config));
|
|
72
76
|
}
|
|
73
77
|
|
|
74
|
-
for (const name of ["pre-commit", "pre-push"]) {
|
|
78
|
+
for (const name of ["pre-commit", "commit-msg", "pre-push"]) {
|
|
75
79
|
const hook = path.join(config.root, ".githooks", name);
|
|
76
80
|
if (existsSync(hook)) await writeFile(hook, await readFile(hook, "utf8"), { mode: 0o755 });
|
|
77
81
|
}
|
|
@@ -93,6 +97,6 @@ Next:
|
|
|
93
97
|
2. Edit docs/glossary.md and the per-surface table in docs/performance.md.
|
|
94
98
|
3. Point qc.config.json at your layout if it differs from the reference. \`qc config\`
|
|
95
99
|
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.
|
|
100
|
+
4. \`qc install-hooks\` to bind the pre-commit, commit-msg and pre-push hooks.
|
|
97
101
|
5. \`qc feature <domain-name>\` to scaffold the first slice, then \`qc check\`.`);
|
|
98
102
|
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The Stop trigger that holds a merge to its issues. Each issue that a PR merged in this session names is
|
|
3
|
+
// closed, or has a comment after the merge, before the turn ends. It makes one gh call per unsettled issue.
|
|
4
|
+
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { runGh } from "./gh-run.mjs";
|
|
8
|
+
import { appendRecord, readLedger } from "./ledger.mjs";
|
|
9
|
+
import { workflowOf } from "./workflow-settings.mjs";
|
|
10
|
+
|
|
11
|
+
const pairOf = (prRepo, pr, repo, number) => `${prRepo}#${pr}>${repo}#${number}`;
|
|
12
|
+
|
|
13
|
+
/** @returns "closed", "commented" after `mergedAt`, "open", or null when the view says neither. */
|
|
14
|
+
export function issueStatus(view, mergedAt) {
|
|
15
|
+
if (view?.state === "CLOSED") return "closed";
|
|
16
|
+
if (view?.state !== "OPEN") return null;
|
|
17
|
+
const since = Date.parse(mergedAt);
|
|
18
|
+
return (view.comments ?? []).some((comment) => Date.parse(comment.createdAt) >= since) ? "commented" : "open";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function statusOf(issue, merge, gh, cwd) {
|
|
22
|
+
const out = gh(["issue", "view", String(issue.number), "-R", issue.repo, "--json", "state,comments"], { cwd, env: merge.ghEnv ?? {} });
|
|
23
|
+
if (out === null) return null;
|
|
24
|
+
try {
|
|
25
|
+
return issueStatus(JSON.parse(out), merge.mergedAt ?? merge.at);
|
|
26
|
+
} catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The state and merge time of a pending merge's PR, or null when gh cannot read it. */
|
|
32
|
+
function prView(merge, gh, cwd) {
|
|
33
|
+
const out = gh(["pr", "view", String(merge.pr), "-R", merge.repo, "--json", "state,mergedAt"], { cwd, env: merge.ghEnv ?? {} });
|
|
34
|
+
try {
|
|
35
|
+
return out === null ? null : JSON.parse(out);
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The merges of this session that count now: each merge record, and each pending one whose PR has since merged,
|
|
43
|
+
* which gets its own merge record. A pending merge still open, or one gh cannot read, stays pending.
|
|
44
|
+
* A pending merge whose PR closed unmerged gets one `closed` record, which counts as no merge and ends the reads.
|
|
45
|
+
*/
|
|
46
|
+
function mergesOf(records, session, workflow, gh, append) {
|
|
47
|
+
const key = (record) => `${record.repo}#${record.pr}`;
|
|
48
|
+
const done = new Set(records.filter((record) => record.type === "merge" && !record.pending && !record.closed).map(key));
|
|
49
|
+
const closedAt = new Map();
|
|
50
|
+
records.forEach((record, index) => record.type === "merge" && record.closed && closedAt.set(key(record), index));
|
|
51
|
+
const write = (record) => {
|
|
52
|
+
try {
|
|
53
|
+
append(workflow.ledger, record);
|
|
54
|
+
} catch {
|
|
55
|
+
// The record only saves a later read; the next stop reads the PR again.
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const merges = [];
|
|
59
|
+
records.forEach((record, index) => {
|
|
60
|
+
if (record.type !== "merge" || record.session !== session || record.closed) return;
|
|
61
|
+
if (!record.pending) {
|
|
62
|
+
merges.push(record);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (done.has(key(record)) || closedAt.get(key(record)) > index) return;
|
|
66
|
+
const view = prView(record, gh, workflow.root);
|
|
67
|
+
if (view?.state === "CLOSED") {
|
|
68
|
+
closedAt.set(key(record), Infinity);
|
|
69
|
+
write({ ...record, pending: undefined, closed: true, mergedAt: null });
|
|
70
|
+
}
|
|
71
|
+
if (view?.state !== "MERGED") return;
|
|
72
|
+
done.add(key(record));
|
|
73
|
+
const resolved = { ...record, mergedAt: view.mergedAt ?? record.mergedAt ?? null };
|
|
74
|
+
delete resolved.pending;
|
|
75
|
+
write(resolved);
|
|
76
|
+
merges.push(resolved);
|
|
77
|
+
});
|
|
78
|
+
return merges;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const nameOf = ({ issue }) => `${issue.repo}#${issue.number}`;
|
|
82
|
+
|
|
83
|
+
/** The GH_* settings of a merge in words, so the sentence holds in Bash and in PowerShell alike. */
|
|
84
|
+
function settingsOf(env = {}) {
|
|
85
|
+
const names = Object.entries(env);
|
|
86
|
+
return names.length === 0 ? "" : ` Set ${names.map(([name, value]) => `${name} to \`${value}\``).join(" and ")} for these gh calls.`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function refusalOf(open) {
|
|
90
|
+
const lines = open.map(({ issue, merge }) => {
|
|
91
|
+
return (
|
|
92
|
+
`- ${issue.repo}#${issue.number}, named by ${merge.repo}#${merge.pr} (merged into ${merge.base ?? "its base"}). ` +
|
|
93
|
+
`Close it: \`gh issue close ${issue.number} -R ${issue.repo} --comment "<what the merge finished>"\`. ` +
|
|
94
|
+
`Or comment on what is left: \`gh issue comment ${issue.number} -R ${issue.repo} --body "<what is left>"\`.` +
|
|
95
|
+
settingsOf(merge.ghEnv)
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
return [
|
|
99
|
+
"Issue check: a PR this session merged names an open issue with no comment since the merge. " +
|
|
100
|
+
"A `Refs` issue stays open, and a merge into a branch other than the default closes none.",
|
|
101
|
+
...lines,
|
|
102
|
+
].join("\n");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The verdict on one Stop. @returns the hook output, or null to let the turn end with no message. */
|
|
106
|
+
export function decide(call, { gh = runGh, tmp = os.tmpdir(), append: appendTo = appendRecord } = {}) {
|
|
107
|
+
if (call.hook_event_name !== "Stop") return null;
|
|
108
|
+
// A stop this hook already blocked once records nothing new, so the ledger holds one state for the retry.
|
|
109
|
+
const append = call.stop_hook_active ? () => {} : appendTo;
|
|
110
|
+
let workflow;
|
|
111
|
+
try {
|
|
112
|
+
workflow = workflowOf(call, tmp);
|
|
113
|
+
} catch {
|
|
114
|
+
// The pre-call hooks report a config error; a stop that cannot read the config lets the turn end.
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
if (!workflow) return null;
|
|
118
|
+
const session = call.session_id ?? "session";
|
|
119
|
+
let records;
|
|
120
|
+
try {
|
|
121
|
+
records = readLedger(workflow.ledger);
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
const settled = new Set(records.filter((r) => r.type === "issue-update").map((r) => pairOf(r.prRepo, r.pr, r.repo, r.number)));
|
|
126
|
+
const open = [];
|
|
127
|
+
const unread = [];
|
|
128
|
+
for (const merge of mergesOf(records, session, workflow, gh, append)) {
|
|
129
|
+
for (const issue of merge.issues ?? []) {
|
|
130
|
+
const pair = pairOf(merge.repo, merge.pr, issue.repo, issue.number);
|
|
131
|
+
if (settled.has(pair)) continue;
|
|
132
|
+
settled.add(pair);
|
|
133
|
+
const status = statusOf(issue, merge, gh, workflow.root);
|
|
134
|
+
if (status === null) unread.push(`${issue.repo}#${issue.number}`);
|
|
135
|
+
else if (status === "open") open.push({ issue, merge });
|
|
136
|
+
else {
|
|
137
|
+
try {
|
|
138
|
+
append(workflow.ledger, { type: "issue-update", session, pr: merge.pr, prRepo: merge.repo, repo: issue.repo, number: issue.number, how: status });
|
|
139
|
+
} catch {
|
|
140
|
+
// The issue is settled on GitHub; without the record the next stop reads it once more. The open list stays.
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (open.length > 0) {
|
|
146
|
+
// A stop the hook already blocked once must end, or a denied close or comment loops forever.
|
|
147
|
+
if (call.stop_hook_active) return { systemMessage: `Issue check: ${open.map(nameOf).join(", ")} still open with no update since the merge. Update each by hand.` };
|
|
148
|
+
return { decision: "block", reason: refusalOf(open) };
|
|
149
|
+
}
|
|
150
|
+
return unread.length > 0 ? { systemMessage: `Issue check: gh could not read ${unread.join(", ")}. Check each by hand.` } : null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function readStdin() {
|
|
154
|
+
let data = "";
|
|
155
|
+
for await (const chunk of process.stdin) data += chunk;
|
|
156
|
+
return data;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const isEntryPoint = process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1];
|
|
160
|
+
if (isEntryPoint) {
|
|
161
|
+
let call = null;
|
|
162
|
+
try {
|
|
163
|
+
call = JSON.parse(await readStdin());
|
|
164
|
+
} catch {
|
|
165
|
+
// Not hook input, so there is no merge to follow up.
|
|
166
|
+
}
|
|
167
|
+
const output = call ? decide(call) : null;
|
|
168
|
+
if (output) process.stdout.write(JSON.stringify(output));
|
|
169
|
+
}
|