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,193 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The trigger that holds `gh pr merge` to its review. Before the call it needs `--match-head-commit` and an
|
|
3
|
+
// APPROVED review of `swarm.review.merge` for that sha. After the call it records the merge and its issues.
|
|
4
|
+
// A merge that names another repository than the checkout's `origin` is that repository's own, so it passes.
|
|
5
|
+
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { PARSERS } from "./bash-command-guard.mjs";
|
|
9
|
+
import { ghApiMerges, ghMerges } from "./gh-merge-command.mjs";
|
|
10
|
+
import { runGh } from "./gh-run.mjs";
|
|
11
|
+
import { appendRecord, latestVerdictFor, readLedger } from "./ledger.mjs";
|
|
12
|
+
import { workflowAt } from "./workflow-settings.mjs";
|
|
13
|
+
|
|
14
|
+
const VIEW_FIELDS = "number,url,state,mergedAt,baseRefName,headRefName,body,closingIssuesReferences";
|
|
15
|
+
const MAYBE_GH = /\bgh(?:\.exe)?\b/i;
|
|
16
|
+
const BODY_REF = /(?<![\w/#])#(\d+)\b/g;
|
|
17
|
+
const REF_LINE = /^\s*(?:[-*]\s+)?(?:refs|closes|fixes|resolves)\b/i;
|
|
18
|
+
const ORIGIN_TIMEOUT_MS = 5_000;
|
|
19
|
+
const PR_URL = /^https?:\/\/[^/]+\/([^/]+\/[^/]+)\/pull\/\d+/;
|
|
20
|
+
const POST_EVENTS = new Set(["PostToolUse", "PostToolUseFailure"]);
|
|
21
|
+
const REVIEWER = { branch: "the branch reviewer (sdd-branch-reviewer)", task: "the task reviewer (sdd-reviewer)" };
|
|
22
|
+
|
|
23
|
+
const deny = (reason) => ({ hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "deny", permissionDecisionReason: reason } });
|
|
24
|
+
const context = (text) => ({ hookSpecificOutput: { hookEventName: "PreToolUse", additionalContext: text } });
|
|
25
|
+
|
|
26
|
+
/** @returns the reason the first of `merges` may not run, or null when each pins an approved head. */
|
|
27
|
+
export function mergeRefusal(merges, records, kind) {
|
|
28
|
+
for (const merge of merges) {
|
|
29
|
+
if (!merge.sha) {
|
|
30
|
+
return `Merge guard: \`gh pr merge\` must pass \`--match-head-commit <sha>\` with the head the ${kind} review approved, so a push after the review cannot merge unreviewed. Add it.`;
|
|
31
|
+
}
|
|
32
|
+
const verdict = latestVerdictFor(records, merge.sha, [kind]);
|
|
33
|
+
if (verdict?.verdict !== "APPROVED") {
|
|
34
|
+
const latest = verdict ? ` (the latest is ${verdict.verdict})` : "";
|
|
35
|
+
return (
|
|
36
|
+
`Merge guard: the ledger holds no APPROVED ${kind} review of ${merge.sha}${latest}. ` +
|
|
37
|
+
`Dispatch ${REVIEWER[kind]} on that head, and merge after it approves. ` +
|
|
38
|
+
"`qc ledger` lists the verdicts, and swarm.review.merge names the kind that counts."
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The issues a PR names: its closing references, then each `#n` on a body line that starts with `Refs`, `Closes`,
|
|
47
|
+
* `Fixes`, or `Resolves`, less its own number and each repeat. A number elsewhere in the body is context.
|
|
48
|
+
*/
|
|
49
|
+
export function namedIssues(pr, repo) {
|
|
50
|
+
const found = new Map();
|
|
51
|
+
const add = (issueRepo, number) => {
|
|
52
|
+
if (Number.isInteger(number) && !(issueRepo === repo && number === pr.number)) found.set(`${issueRepo}#${number}`, { repo: issueRepo, number });
|
|
53
|
+
};
|
|
54
|
+
for (const ref of pr.closingIssuesReferences ?? []) {
|
|
55
|
+
const owner = ref.repository?.owner?.login;
|
|
56
|
+
const name = ref.repository?.name;
|
|
57
|
+
add(owner && name ? `${owner}/${name}` : repo, ref.number);
|
|
58
|
+
}
|
|
59
|
+
for (const line of String(pr.body ?? "").split(/\r?\n/)) {
|
|
60
|
+
if (REF_LINE.test(line)) for (const match of line.matchAll(BODY_REF)) add(repo, Number(match[1]));
|
|
61
|
+
}
|
|
62
|
+
return [...found.values()];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** `owner/repo` from a git remote URL in the https, ssh, or scp spelling, or from gh's `[HOST/]OWNER/REPO`; lower case. */
|
|
66
|
+
function repoOfSpelling(spelled) {
|
|
67
|
+
const parts = String(spelled).trim().replace(/\/+$/, "").replace(/\.git$/i, "").split(/[/:]/).filter(Boolean);
|
|
68
|
+
return parts.length >= 2 ? parts.slice(-2).join("/").toLowerCase() : null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The repository of the checkout's `origin` remote, or null when git cannot say. */
|
|
72
|
+
function originOf(root) {
|
|
73
|
+
const result = spawnSync("git", ["remote", "get-url", "origin"], { cwd: root, encoding: "utf8", windowsHide: true, timeout: ORIGIN_TIMEOUT_MS });
|
|
74
|
+
return result.status === 0 ? repoOfSpelling(result.stdout) : null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Splits the merges into those of this checkout's repository and those that name another repository. */
|
|
78
|
+
function partition(merges, root) {
|
|
79
|
+
const origin = merges.some((merge) => merge.repo) ? originOf(root) : null;
|
|
80
|
+
const foreign = (merge) => origin !== null && merge.repo && repoOfSpelling(merge.repo) !== origin;
|
|
81
|
+
return { origin, own: merges.filter((merge) => !foreign(merge)), other: merges.filter(foreign) };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function recordMerge(merge, call, workflow, gh) {
|
|
85
|
+
const args = ["pr", "view", ...(merge.selector ? [merge.selector] : []), ...(merge.repo ? ["-R", merge.repo] : []), "--json", VIEW_FIELDS];
|
|
86
|
+
const out = gh(args, { cwd: call.cwd ?? process.cwd(), env: merge.env });
|
|
87
|
+
let pr = null;
|
|
88
|
+
try {
|
|
89
|
+
pr = out === null ? null : JSON.parse(out);
|
|
90
|
+
} catch {
|
|
91
|
+
pr = null;
|
|
92
|
+
}
|
|
93
|
+
if (!pr) {
|
|
94
|
+
// After a failed call there may be no merge to read, so only a successful one reports this.
|
|
95
|
+
return call.hook_event_name === "PostToolUse" ? "Merge guard: gh pr view could not read the merge, so the ledger does not record it." : null;
|
|
96
|
+
}
|
|
97
|
+
const repo = PR_URL.exec(pr.url ?? "")?.[1] ?? null;
|
|
98
|
+
const merged = pr.state === "MERGED";
|
|
99
|
+
// An `--auto` merge on an open PR is pending: the issue gate reads the PR at Stop to see whether it merged.
|
|
100
|
+
// A failed call is no proof of a queued merge: `gh pr merge --auto` may have been refused on the open PR.
|
|
101
|
+
const pending = call.hook_event_name === "PostToolUse" && !merged && merge.auto && pr.state === "OPEN";
|
|
102
|
+
if ((!merged && !pending) || !repo) return null;
|
|
103
|
+
const ofPr = (record) => record.type === "merge" && record.repo === repo && record.pr === pr.number;
|
|
104
|
+
try {
|
|
105
|
+
// A closed record ends what came before it: a PR reopened after it gets its own record.
|
|
106
|
+
const records = readLedger(workflow.ledger);
|
|
107
|
+
const closedAt = records.findLastIndex((record) => ofPr(record) && record.closed);
|
|
108
|
+
if (records.slice(closedAt + 1).some((record) => ofPr(record) && (pending || !record.pending))) return null;
|
|
109
|
+
appendRecord(workflow.ledger, {
|
|
110
|
+
type: "merge",
|
|
111
|
+
...(pending ? { pending: true } : {}),
|
|
112
|
+
session: call.session_id ?? "session",
|
|
113
|
+
pr: pr.number,
|
|
114
|
+
repo,
|
|
115
|
+
sha: merge.sha,
|
|
116
|
+
branch: pr.headRefName ?? null,
|
|
117
|
+
base: pr.baseRefName ?? null,
|
|
118
|
+
mergedAt: pr.mergedAt ?? null,
|
|
119
|
+
issues: namedIssues(pr, repo),
|
|
120
|
+
ghEnv: merge.env,
|
|
121
|
+
});
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return `Merge guard: the ledger failed (${error.message}), so this merge is not recorded.`;
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The verdict on one Bash or PowerShell event. @returns the hook output, or null to let it pass with no message. */
|
|
129
|
+
export function decide(call, { gh = runGh } = {}) {
|
|
130
|
+
const event = call.hook_event_name;
|
|
131
|
+
if (event !== "PreToolUse" && !POST_EVENTS.has(event)) return null;
|
|
132
|
+
const parse = PARSERS[call.tool_name];
|
|
133
|
+
const command = call.tool_input?.command;
|
|
134
|
+
if (!parse || typeof command !== "string" || !MAYBE_GH.test(command)) return null;
|
|
135
|
+
let workflow;
|
|
136
|
+
try {
|
|
137
|
+
workflow = workflowAt(call.cwd ?? process.cwd());
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return event === "PreToolUse" ? context(`Merge guard is off: ${error.message}`) : null;
|
|
140
|
+
}
|
|
141
|
+
if (!workflow) return null;
|
|
142
|
+
if (event === "PreToolUse") {
|
|
143
|
+
const [found] = ghApiMerges(command, parse, { cwd: call.cwd ?? process.cwd() });
|
|
144
|
+
if (found) {
|
|
145
|
+
const use = "Run `gh pr merge <n> --match-head-commit <sha>` with the head an APPROVED review named.";
|
|
146
|
+
return deny(
|
|
147
|
+
found.unreadable === undefined
|
|
148
|
+
? `Merge guard: \`gh api ${found.endpoint}\` merges with no review check and no head pin. ${use}`
|
|
149
|
+
: `Merge guard: \`gh api ${found.endpoint}\` reads \`${found.unreadable}\`, which the guard cannot read, so it may merge with no review check and no head pin. ${use}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const merges = ghMerges(command, parse);
|
|
154
|
+
if (merges.length === 0) return null;
|
|
155
|
+
const { origin, own, other } = partition(merges, workflow.root);
|
|
156
|
+
if (event === "PreToolUse") {
|
|
157
|
+
let refusal = null;
|
|
158
|
+
if (own.length > 0) {
|
|
159
|
+
try {
|
|
160
|
+
refusal = mergeRefusal(own, readLedger(workflow.ledger), workflow.review.merge);
|
|
161
|
+
} catch (error) {
|
|
162
|
+
refusal = `Merge guard: the ledger cannot be read (${error.message}), so no review can be checked. Fix the ledger file, and merge again.`;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (refusal) return deny(refusal);
|
|
166
|
+
return other.length > 0
|
|
167
|
+
? context(
|
|
168
|
+
`Merge guard passes ${[...new Set(other.map((merge) => merge.repo))].join(", ")}: it is not this checkout's repository (${origin}), ` +
|
|
169
|
+
"so its reviews are not in this ledger. The guard records nothing for it.",
|
|
170
|
+
)
|
|
171
|
+
: null;
|
|
172
|
+
}
|
|
173
|
+
const notes = own.map((merge) => recordMerge(merge, call, workflow, gh)).filter(Boolean);
|
|
174
|
+
return notes.length > 0 ? { systemMessage: notes.join(" ") } : null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function readStdin() {
|
|
178
|
+
let data = "";
|
|
179
|
+
for await (const chunk of process.stdin) data += chunk;
|
|
180
|
+
return data;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const isEntryPoint = process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1];
|
|
184
|
+
if (isEntryPoint) {
|
|
185
|
+
let call = null;
|
|
186
|
+
try {
|
|
187
|
+
call = JSON.parse(await readStdin());
|
|
188
|
+
} catch {
|
|
189
|
+
// Not hook input, so there is no command to judge.
|
|
190
|
+
}
|
|
191
|
+
const output = call ? decide(call) : null;
|
|
192
|
+
if (output) process.stdout.write(JSON.stringify(output));
|
|
193
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
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
|
+
import { appendRecord, readLedger } from "./ledger.mjs";
|
|
9
|
+
import { decide, namedIssues } from "./merge-guard.mjs";
|
|
10
|
+
|
|
11
|
+
const HOOK = fileURLToPath(new URL("./merge-guard.mjs", import.meta.url));
|
|
12
|
+
const SHA = "0123456789abcdef0123456789abcdef01234567";
|
|
13
|
+
|
|
14
|
+
/** Checkouts with the checks on (merge kind branch, and task), one with them off, each with a git dir. */
|
|
15
|
+
function workspace(t) {
|
|
16
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-merge-guard-")));
|
|
17
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
18
|
+
const make = (name, config) => {
|
|
19
|
+
const dir = path.join(base, name);
|
|
20
|
+
execFileSync("git", ["init", "-q", dir], { stdio: "pipe" });
|
|
21
|
+
writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify(config));
|
|
22
|
+
return dir;
|
|
23
|
+
};
|
|
24
|
+
const on = make("on", { swarm: { dispatch: {} } });
|
|
25
|
+
execFileSync("git", ["remote", "add", "origin", "git@github.com-personal:o/r.git"], { cwd: on, stdio: "pipe" });
|
|
26
|
+
const task = make("task", { swarm: { dispatch: {}, review: { merge: "task" } } });
|
|
27
|
+
const off = make("off", { swarm: {} });
|
|
28
|
+
return { on, task, off, ledgerOf: (dir) => path.join(dir, ".git", "qc", "ledger.jsonl") };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const shell = (cwd, command, hook_event_name = "PreToolUse", tool_name = "Bash") => ({
|
|
32
|
+
session_id: "s",
|
|
33
|
+
cwd,
|
|
34
|
+
hook_event_name,
|
|
35
|
+
tool_name,
|
|
36
|
+
tool_input: { command },
|
|
37
|
+
});
|
|
38
|
+
const denied = (output) => output?.hookSpecificOutput?.permissionDecisionReason ?? null;
|
|
39
|
+
const verdict = (kind, value, sha = SHA) => ({ type: "verdict", kind, verdict: value, sha, branch: "feat/62-x" });
|
|
40
|
+
|
|
41
|
+
/** A gh stand-in that logs each call and answers with `answer`. */
|
|
42
|
+
function fakeGh(answer) {
|
|
43
|
+
const calls = [];
|
|
44
|
+
return { calls, gh: (args, options) => (calls.push({ args, options }), typeof answer === "function" ? answer(args) : answer) };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const MERGED = JSON.stringify({
|
|
48
|
+
number: 60,
|
|
49
|
+
url: "https://github.com/o/r/pull/60",
|
|
50
|
+
state: "MERGED",
|
|
51
|
+
mergedAt: "2026-09-29T04:35:17Z",
|
|
52
|
+
baseRefName: "main",
|
|
53
|
+
headRefName: "feat/62-x",
|
|
54
|
+
body: "Closes #59\nRefs #12, see #60 and o/r#13",
|
|
55
|
+
closingIssuesReferences: [
|
|
56
|
+
{ number: 59, repository: { name: "r", owner: { login: "o" } } },
|
|
57
|
+
{ number: 3, repository: { name: "other", owner: { login: "o" } } },
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("with swarm.dispatch off, a merge is neither judged nor recorded", (t) => {
|
|
62
|
+
const ws = workspace(t);
|
|
63
|
+
const { gh, calls } = fakeGh(MERGED);
|
|
64
|
+
assert.equal(decide(shell(ws.off, "gh pr merge 60"), { gh }), null);
|
|
65
|
+
assert.equal(decide(shell(ws.off, "gh pr merge 60", "PostToolUse"), { gh }), null);
|
|
66
|
+
assert.equal(decide(shell(ws.off, "gh api -X PUT repos/o/r/pulls/60/merge"), { gh }), null);
|
|
67
|
+
assert.equal(calls.length, 0);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("a merge without --match-head-commit is refused", (t) => {
|
|
71
|
+
const ws = workspace(t);
|
|
72
|
+
assert.match(denied(decide(shell(ws.on, "gh pr merge 60 --squash"))) ?? "", /--match-head-commit <sha>/);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("a merge passes only on an APPROVED review of the configured kind for its sha", (t) => {
|
|
76
|
+
const ws = workspace(t);
|
|
77
|
+
const command = `gh pr merge 60 --squash --match-head-commit ${SHA.slice(0, 7)}`;
|
|
78
|
+
assert.match(denied(decide(shell(ws.on, command))) ?? "", /no APPROVED branch review/);
|
|
79
|
+
appendRecord(ws.ledgerOf(ws.on), verdict("task", "APPROVED"));
|
|
80
|
+
assert.match(denied(decide(shell(ws.on, command))) ?? "", /no APPROVED branch review/);
|
|
81
|
+
appendRecord(ws.ledgerOf(ws.task), verdict("task", "APPROVED"));
|
|
82
|
+
assert.equal(decide(shell(ws.task, command)), null);
|
|
83
|
+
appendRecord(ws.ledgerOf(ws.on), verdict("branch", "APPROVED"));
|
|
84
|
+
assert.equal(decide(shell(ws.on, command)), null);
|
|
85
|
+
assert.equal(decide(shell(ws.on, command, "PreToolUse", "PowerShell")), null);
|
|
86
|
+
appendRecord(ws.ledgerOf(ws.on), verdict("branch", "CHANGES_REQUIRED"));
|
|
87
|
+
assert.match(denied(decide(shell(ws.on, command))) ?? "", /the latest is CHANGES_REQUIRED/);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("a short sha in upper case matches the full sha the review approved", (t) => {
|
|
91
|
+
const ws = workspace(t);
|
|
92
|
+
appendRecord(ws.ledgerOf(ws.on), verdict("branch", "APPROVED", SHA.toUpperCase()));
|
|
93
|
+
assert.equal(decide(shell(ws.on, `gh pr merge 60 --match-head-commit ${SHA.slice(0, 7).toUpperCase()}`)), null);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("--auto gets the same review check as an immediate merge", (t) => {
|
|
97
|
+
const ws = workspace(t);
|
|
98
|
+
const auto = `gh pr merge 60 --auto --squash --match-head-commit ${SHA}`;
|
|
99
|
+
assert.match(denied(decide(shell(ws.on, "gh pr merge 60 --auto --squash"))) ?? "", /--match-head-commit <sha>/);
|
|
100
|
+
assert.match(denied(decide(shell(ws.on, auto))) ?? "", /no APPROVED branch review/);
|
|
101
|
+
appendRecord(ws.ledgerOf(ws.on), verdict("branch", "APPROVED"));
|
|
102
|
+
assert.equal(decide(shell(ws.on, auto)), null);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("a gh api call to a PR's merge endpoint is refused outright, even after an approval", (t) => {
|
|
106
|
+
const ws = workspace(t);
|
|
107
|
+
appendRecord(ws.ledgerOf(ws.on), verdict("branch", "APPROVED"));
|
|
108
|
+
for (const [command, tool] of [
|
|
109
|
+
[`gh api -X PUT repos/o/r/pulls/60/merge -f sha=${SHA}`, "Bash"],
|
|
110
|
+
[`bash -c "gh api --method PUT /repos/o/r/pulls/60/merge"`, "Bash"],
|
|
111
|
+
["gh api repos/o/r/pulls/60/merge -X PUT", "PowerShell"],
|
|
112
|
+
]) {
|
|
113
|
+
assert.match(denied(decide(shell(ws.on, command, "PreToolUse", tool))) ?? "", /gh pr merge <n> --match-head-commit <sha>/, command);
|
|
114
|
+
}
|
|
115
|
+
assert.equal(decide(shell(ws.on, "gh api repos/o/r/pulls/60/comments")), null);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("a merge inside a nested shell is judged too", (t) => {
|
|
119
|
+
const ws = workspace(t);
|
|
120
|
+
assert.match(denied(decide(shell(ws.on, `bash -c "gh pr merge 60 --match-head-commit ${SHA}"`))) ?? "", /no APPROVED/);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("after the merge, the record keeps the PR, its head sha, its base, and each issue it names", (t) => {
|
|
124
|
+
const ws = workspace(t);
|
|
125
|
+
const { gh, calls } = fakeGh(MERGED);
|
|
126
|
+
const command = `GH_CONFIG_DIR=~/.config/gh-personal gh pr merge 60 -R o/r --squash --match-head-commit ${SHA}`;
|
|
127
|
+
assert.equal(decide({ ...shell(ws.on, command, "PostToolUse"), session_id: "s-merge" }, { gh }), null);
|
|
128
|
+
assert.deepEqual(calls[0].args, [
|
|
129
|
+
"pr", "view", "60", "-R", "o/r", "--json", "number,url,state,mergedAt,baseRefName,headRefName,body,closingIssuesReferences",
|
|
130
|
+
]);
|
|
131
|
+
assert.deepEqual(calls[0].options.env, { GH_CONFIG_DIR: "~/.config/gh-personal" });
|
|
132
|
+
const [record] = readLedger(ws.ledgerOf(ws.on));
|
|
133
|
+
assert.deepEqual(
|
|
134
|
+
{ type: record.type, session: record.session, pr: record.pr, repo: record.repo, sha: record.sha, branch: record.branch, base: record.base, mergedAt: record.mergedAt, ghEnv: record.ghEnv },
|
|
135
|
+
{ type: "merge", session: "s-merge", pr: 60, repo: "o/r", sha: SHA, branch: "feat/62-x", base: "main", mergedAt: "2026-09-29T04:35:17Z", ghEnv: { GH_CONFIG_DIR: "~/.config/gh-personal" } },
|
|
136
|
+
);
|
|
137
|
+
assert.deepEqual(record.issues, [
|
|
138
|
+
{ repo: "o/r", number: 59 },
|
|
139
|
+
{ repo: "o/other", number: 3 },
|
|
140
|
+
{ repo: "o/r", number: 12 },
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("a failed read, or a second merge of one PR, adds no second record", (t) => {
|
|
145
|
+
const ws = workspace(t);
|
|
146
|
+
const command = `gh pr merge 60 --match-head-commit ${SHA}`;
|
|
147
|
+
decide(shell(ws.on, command, "PostToolUse"), { gh: fakeGh(JSON.stringify({ ...JSON.parse(MERGED), state: "OPEN" })).gh });
|
|
148
|
+
assert.equal(existsSync(ws.ledgerOf(ws.on)), false, "an immediate merge that is not merged leaves no record");
|
|
149
|
+
assert.match(decide(shell(ws.on, command, "PostToolUse"), { gh: fakeGh(null).gh })?.systemMessage ?? "", /does not record it/);
|
|
150
|
+
assert.equal(decide(shell(ws.on, command, "PostToolUseFailure"), { gh: fakeGh(null).gh }), null);
|
|
151
|
+
decide(shell(ws.on, command, "PostToolUse"), { gh: fakeGh(MERGED).gh });
|
|
152
|
+
decide(shell(ws.on, command, "PostToolUseFailure"), { gh: fakeGh(MERGED).gh });
|
|
153
|
+
assert.equal(readLedger(ws.ledgerOf(ws.on)).length, 1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("an --auto merge still open writes a pending record, once, and a later merge adds the real record", (t) => {
|
|
157
|
+
const ws = workspace(t);
|
|
158
|
+
const command = `gh pr merge 60 --auto --squash --match-head-commit ${SHA}`;
|
|
159
|
+
const open = fakeGh(JSON.stringify({ ...JSON.parse(MERGED), state: "OPEN", mergedAt: null })).gh;
|
|
160
|
+
decide(shell(ws.on, command, "PostToolUse"), { gh: open });
|
|
161
|
+
decide(shell(ws.on, command, "PostToolUse"), { gh: open });
|
|
162
|
+
const [pending, ...rest] = readLedger(ws.ledgerOf(ws.on));
|
|
163
|
+
assert.equal(rest.length, 0);
|
|
164
|
+
assert.deepEqual(
|
|
165
|
+
{ type: pending.type, pending: pending.pending, pr: pending.pr, repo: pending.repo, sha: pending.sha, branch: pending.branch, mergedAt: pending.mergedAt },
|
|
166
|
+
{ type: "merge", pending: true, pr: 60, repo: "o/r", sha: SHA, branch: "feat/62-x", mergedAt: null },
|
|
167
|
+
);
|
|
168
|
+
assert.deepEqual(pending.issues, [{ repo: "o/r", number: 59 }, { repo: "o/other", number: 3 }, { repo: "o/r", number: 12 }]);
|
|
169
|
+
decide(shell(ws.on, command, "PostToolUse"), { gh: fakeGh(MERGED).gh });
|
|
170
|
+
const merged = readLedger(ws.ledgerOf(ws.on)).filter((record) => !record.pending);
|
|
171
|
+
assert.equal(merged.length, 1);
|
|
172
|
+
assert.equal(merged[0].mergedAt, "2026-09-29T04:35:17Z");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("gh api graphql with a mergePullRequest mutation is refused, and the REST endpoint spellings all match", (t) => {
|
|
176
|
+
const ws = workspace(t);
|
|
177
|
+
appendRecord(ws.ledgerOf(ws.on), verdict("branch", "APPROVED"));
|
|
178
|
+
for (const [command, tool] of [
|
|
179
|
+
["gh api graphql -f query='mutation { mergePullRequest(input: {pullRequestId: \"X\"}) { clientMutationId } }'", "Bash"],
|
|
180
|
+
['gh api graphql -F query="mutation { mergePullRequest(input: {pullRequestId: 1}) { clientMutationId } }"', "PowerShell"],
|
|
181
|
+
[`bash -c "gh api graphql -f query='mutation { mergePullRequest(input: {}) { clientMutationId } }'"`, "Bash"],
|
|
182
|
+
["gh api -X PUT repos/o/r/pulls/60/merge/", "Bash"],
|
|
183
|
+
["gh api -X PUT /repos/o/r/pulls/60/merge/?x=1", "Bash"],
|
|
184
|
+
["gh api -X PUT https://api.github.com/repos/o/r/pulls/60/merge", "Bash"],
|
|
185
|
+
["gh api -X PUT https://api.github.com/repos/o/r/pulls/60/merge/", "PowerShell"],
|
|
186
|
+
["gh api https://api.github.com/graphql -f query='mutation { mergePullRequest(input: {}) { clientMutationId } }'", "Bash"],
|
|
187
|
+
["gh api https://ghe.example.com/api/graphql -f query='mutation { mergePullRequest(input: {}) { clientMutationId } }'", "PowerShell"],
|
|
188
|
+
]) {
|
|
189
|
+
assert.match(denied(decide(shell(ws.on, command, "PreToolUse", tool))) ?? "", /gh pr merge <n> --match-head-commit <sha>/, command);
|
|
190
|
+
}
|
|
191
|
+
assert.equal(decide(shell(ws.on, "gh api graphql -f query='query { viewer { login } }'")), null);
|
|
192
|
+
assert.equal(decide(shell(ws.on, "gh api https://api.github.com/repos/o/r/pulls/60/comments")), null);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("a command with no gh call never runs gh", (t) => {
|
|
196
|
+
const ws = workspace(t);
|
|
197
|
+
const { gh, calls } = fakeGh(MERGED);
|
|
198
|
+
assert.equal(decide(shell(ws.on, "git merge feat/x", "PostToolUse"), { gh }), null);
|
|
199
|
+
assert.equal(calls.length, 0);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("namedIssues drops the PR's own number and each repeat", () => {
|
|
203
|
+
const pr = { number: 7, body: "Fixes #7, #8 and #8. See https://x/#9 and a#10", closingIssuesReferences: [{ number: 8, repository: { name: "r", owner: { login: "o" } } }] };
|
|
204
|
+
assert.deepEqual(namedIssues(pr, "o/r"), [{ repo: "o/r", number: 8 }]);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("the hook process prints the deny", (t) => {
|
|
208
|
+
const ws = workspace(t);
|
|
209
|
+
const result = spawnSync(process.execPath, [HOOK], { input: JSON.stringify(shell(ws.on, "gh pr merge 60")), encoding: "utf8" });
|
|
210
|
+
assert.equal(result.status, 0, result.stderr);
|
|
211
|
+
assert.equal(JSON.parse(result.stdout).hookSpecificOutput.permissionDecision, "deny");
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("a merge that names another repository than the checkout's origin passes with a note and records nothing", (t) => {
|
|
215
|
+
const ws = workspace(t);
|
|
216
|
+
const other = `gh pr merge 64 -R shawrylk/architecture-kit --squash --match-head-commit ${SHA}`;
|
|
217
|
+
const output = decide(shell(ws.on, other));
|
|
218
|
+
assert.equal(denied(output), null);
|
|
219
|
+
assert.match(output.hookSpecificOutput.additionalContext, /shawrylk\/architecture-kit/);
|
|
220
|
+
assert.match(output.hookSpecificOutput.additionalContext, /o\/r/);
|
|
221
|
+
const { gh, calls } = fakeGh(MERGED);
|
|
222
|
+
assert.equal(decide(shell(ws.on, other, "PostToolUse"), { gh }), null);
|
|
223
|
+
assert.equal(calls.length, 0);
|
|
224
|
+
assert.equal(existsSync(ws.ledgerOf(ws.on)), false);
|
|
225
|
+
// The same call with no sha pin still passes for another repository, --repo included.
|
|
226
|
+
assert.equal(denied(decide(shell(ws.on, "gh pr merge 64 --repo=shawrylk/architecture-kit"))), null);
|
|
227
|
+
// The checkout's own repository, in any spelling, is still judged.
|
|
228
|
+
for (const named of ["-R o/r", "--repo O/R", "-R github.com/o/r"]) {
|
|
229
|
+
assert.match(denied(decide(shell(ws.on, `gh pr merge 60 ${named} --match-head-commit ${SHA}`))) ?? "", /no APPROVED branch review/, named);
|
|
230
|
+
}
|
|
231
|
+
// A merge of the own repository next to a merge of another one is still refused.
|
|
232
|
+
assert.match(denied(decide(shell(ws.on, `${other} && gh pr merge 60 -R o/r`))) ?? "", /--match-head-commit <sha>/);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("a checkout with no readable origin judges a merge that names a repository", (t) => {
|
|
236
|
+
const ws = workspace(t);
|
|
237
|
+
assert.match(denied(decide(shell(ws.task, `gh pr merge 60 -R o/r --match-head-commit ${SHA}`))) ?? "", /no APPROVED task review/);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test("a failed --auto merge of an open PR writes no pending record", (t) => {
|
|
241
|
+
const ws = workspace(t);
|
|
242
|
+
const command = `gh pr merge 60 --auto --squash --match-head-commit ${SHA}`;
|
|
243
|
+
const open = fakeGh(JSON.stringify({ ...JSON.parse(MERGED), state: "OPEN", mergedAt: null })).gh;
|
|
244
|
+
assert.equal(decide(shell(ws.on, command, "PostToolUseFailure"), { gh: open }), null);
|
|
245
|
+
assert.equal(existsSync(ws.ledgerOf(ws.on)), false);
|
|
246
|
+
decide(shell(ws.on, command, "PostToolUse"), { gh: open });
|
|
247
|
+
assert.equal(readLedger(ws.ledgerOf(ws.on)).length, 1, "a call that succeeded still records the pending merge");
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("a ledger that cannot be read or written never throws out of the guard", (t) => {
|
|
251
|
+
const ws = workspace(t);
|
|
252
|
+
const ledger = ws.ledgerOf(ws.on);
|
|
253
|
+
mkdirSync(ledger, { recursive: true }); // a directory where the file belongs: read and write both fail
|
|
254
|
+
const command = `gh pr merge 60 --squash --match-head-commit ${SHA}`;
|
|
255
|
+
const after = decide(shell(ws.on, command, "PostToolUse"), { gh: fakeGh(MERGED).gh });
|
|
256
|
+
assert.match(after?.systemMessage ?? "", /ledger/);
|
|
257
|
+
assert.match(denied(decide(shell(ws.on, command))) ?? "", /ledger/);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test("namedIssues takes body numbers only from Refs, Closes, Fixes, and Resolves lines", () => {
|
|
261
|
+
const pr = {
|
|
262
|
+
number: 61,
|
|
263
|
+
body: ["Adds the guard, as PR #61 and #40 did.", "Related to #41 and #42.", " refs #12, #13", "Resolves: #14", "- Fixes #15", "Closes #16 and o/r#17", "Not a Fixes line #18"].join("\r\n"),
|
|
264
|
+
};
|
|
265
|
+
assert.deepEqual(
|
|
266
|
+
namedIssues(pr, "o/r").map((issue) => issue.number),
|
|
267
|
+
[12, 13, 14, 15, 16],
|
|
268
|
+
);
|
|
269
|
+
assert.deepEqual(namedIssues({ number: 1, body: "See #5 and #6" }, "o/r"), []);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test("a graphql call with a query file holding the mutation, or a file it cannot read, is refused", (t) => {
|
|
273
|
+
const ws = workspace(t);
|
|
274
|
+
writeFileSync(path.join(ws.on, "merge.graphql"), "mutation { mergePullRequest(input: {}) { clientMutationId } }");
|
|
275
|
+
writeFileSync(path.join(ws.on, "viewer.graphql"), "query { viewer { login } }");
|
|
276
|
+
const refusal = (command) => denied(decide(shell(ws.on, command)));
|
|
277
|
+
assert.match(refusal("gh api https://api.github.com/graphql -F query=@merge.graphql") ?? "", /gh pr merge <n> --match-head-commit <sha>/);
|
|
278
|
+
assert.match(refusal("gh api graphql -F query=@missing.graphql") ?? "", /missing\.graphql/);
|
|
279
|
+
assert.equal(refusal("gh api graphql -F query=@viewer.graphql"), null);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test("an --auto merge on a PR reopened after a closed pending record is tracked again", (t) => {
|
|
283
|
+
const ws = workspace(t);
|
|
284
|
+
const command = `gh pr merge 60 --auto --squash --match-head-commit ${SHA}`;
|
|
285
|
+
const open = fakeGh(JSON.stringify({ ...JSON.parse(MERGED), state: "OPEN", mergedAt: null })).gh;
|
|
286
|
+
decide(shell(ws.on, command, "PostToolUse"), { gh: open });
|
|
287
|
+
const [pending] = readLedger(ws.ledgerOf(ws.on));
|
|
288
|
+
appendRecord(ws.ledgerOf(ws.on), { ...pending, pending: undefined, closed: true, mergedAt: null });
|
|
289
|
+
decide(shell(ws.on, command, "PostToolUse"), { gh: open });
|
|
290
|
+
const records = readLedger(ws.ledgerOf(ws.on));
|
|
291
|
+
assert.equal(records.length, 3);
|
|
292
|
+
assert.equal(records[2].pending, true);
|
|
293
|
+
decide(shell(ws.on, command, "PostToolUse"), { gh: open });
|
|
294
|
+
assert.equal(readLedger(ws.ledgerOf(ws.on)).length, 3, "and a repeat adds no fourth record");
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test("a merge names another repository through a PR URL, GH_REPO, or an attached -R, and passes with a note", (t) => {
|
|
298
|
+
const ws = workspace(t);
|
|
299
|
+
for (const command of [
|
|
300
|
+
`gh pr merge https://github.com/shawrylk/architecture-kit/pull/64 --match-head-commit ${SHA}`,
|
|
301
|
+
`GH_REPO=shawrylk/architecture-kit gh pr merge 64 --match-head-commit ${SHA}`,
|
|
302
|
+
`gh pr merge 64 -Rshawrylk/architecture-kit --match-head-commit ${SHA}`,
|
|
303
|
+
`gh pr merge 64 -sRshawrylk/architecture-kit --match-head-commit ${SHA}`,
|
|
304
|
+
]) {
|
|
305
|
+
const output = decide(shell(ws.on, command));
|
|
306
|
+
assert.equal(denied(output), null, command);
|
|
307
|
+
assert.match(output.hookSpecificOutput.additionalContext, /shawrylk\/architecture-kit/, command);
|
|
308
|
+
const { gh, calls } = fakeGh(MERGED);
|
|
309
|
+
assert.equal(decide(shell(ws.on, command, "PostToolUse"), { gh }), null);
|
|
310
|
+
assert.equal(calls.length, 0, command);
|
|
311
|
+
}
|
|
312
|
+
assert.equal(existsSync(ws.ledgerOf(ws.on)), false);
|
|
313
|
+
// The checkout's own repository, named the same ways, is still judged.
|
|
314
|
+
for (const command of [
|
|
315
|
+
`gh pr merge https://github.com/o/r/pull/60 --match-head-commit ${SHA}`,
|
|
316
|
+
`GH_REPO=o/r gh pr merge 60 --match-head-commit ${SHA}`,
|
|
317
|
+
`gh pr merge 60 -Ro/r --match-head-commit ${SHA}`,
|
|
318
|
+
]) {
|
|
319
|
+
assert.match(denied(decide(shell(ws.on, command))) ?? "", /no APPROVED branch review/, command);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test("a gh api call to a repos/ path whose body file quotes the mutation is allowed", (t) => {
|
|
324
|
+
const ws = workspace(t);
|
|
325
|
+
writeFileSync(path.join(ws.on, "review.md"), "The bypass is the mergePullRequest mutation.");
|
|
326
|
+
assert.equal(decide(shell(ws.on, "gh api repos/o/r/issues/64/comments -F body=@review.md")), null);
|
|
327
|
+
assert.match(denied(decide(shell(ws.on, "gh api graphql -F query=@review.md"))) ?? "", /gh pr merge <n> --match-head-commit <sha>/);
|
|
328
|
+
});
|