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,234 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
+
import { chmodSync, 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 { decide, issueStatus } from "./issue-gate.mjs";
|
|
9
|
+
import { appendRecord, readLedger } from "./ledger.mjs";
|
|
10
|
+
import { rememberSession } from "./workflow-settings.mjs";
|
|
11
|
+
|
|
12
|
+
const HOOK = fileURLToPath(new URL("./issue-gate.mjs", import.meta.url));
|
|
13
|
+
const MERGED_AT = "2026-09-29T04:35:17Z";
|
|
14
|
+
|
|
15
|
+
function workspace(t) {
|
|
16
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-issue-gate-")));
|
|
17
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
18
|
+
const on = path.join(base, "on");
|
|
19
|
+
const off = path.join(base, "off");
|
|
20
|
+
execFileSync("git", ["init", "-q", on], { stdio: "pipe" });
|
|
21
|
+
execFileSync("git", ["init", "-q", off], { stdio: "pipe" });
|
|
22
|
+
writeFileSync(path.join(on, "qc.config.json"), JSON.stringify({ swarm: { dispatch: {} } }));
|
|
23
|
+
writeFileSync(path.join(off, "qc.config.json"), JSON.stringify({ swarm: {} }));
|
|
24
|
+
return { on, off, ledger: path.join(on, ".git", "qc", "ledger.jsonl") };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const merge = (session, issues, extra = {}) => ({
|
|
28
|
+
type: "merge",
|
|
29
|
+
session,
|
|
30
|
+
pr: 60,
|
|
31
|
+
repo: "o/r",
|
|
32
|
+
sha: "abc1234",
|
|
33
|
+
branch: "feat/62-x",
|
|
34
|
+
base: "main",
|
|
35
|
+
mergedAt: MERGED_AT,
|
|
36
|
+
issues,
|
|
37
|
+
ghEnv: {},
|
|
38
|
+
...extra,
|
|
39
|
+
});
|
|
40
|
+
const stopCall = (cwd, session = "s") => ({ session_id: session, cwd, hook_event_name: "Stop", stop_hook_active: false });
|
|
41
|
+
|
|
42
|
+
/** A gh stand-in answering `gh issue view <n>` from a map of issue number to view. */
|
|
43
|
+
function fakeGh(views) {
|
|
44
|
+
const calls = [];
|
|
45
|
+
return {
|
|
46
|
+
calls,
|
|
47
|
+
gh: (args, options) => {
|
|
48
|
+
calls.push({ args, options });
|
|
49
|
+
const view = views[args[2]];
|
|
50
|
+
return view === undefined ? null : JSON.stringify(view);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
test("issueStatus reads a close, a comment after the merge, and an open issue", () => {
|
|
56
|
+
assert.equal(issueStatus({ state: "CLOSED", comments: [] }, MERGED_AT), "closed");
|
|
57
|
+
assert.equal(issueStatus({ state: "OPEN", comments: [{ createdAt: "2026-09-29T05:00:00Z" }] }, MERGED_AT), "commented");
|
|
58
|
+
assert.equal(issueStatus({ state: "OPEN", comments: [{ createdAt: "2026-09-28T00:00:00Z" }] }, MERGED_AT), "open");
|
|
59
|
+
assert.equal(issueStatus({}, MERGED_AT), null);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("with swarm.dispatch off, or no merge in this session, the stop passes with no gh call", (t) => {
|
|
63
|
+
const ws = workspace(t);
|
|
64
|
+
appendRecord(ws.ledger, merge("other-session", [{ repo: "o/r", number: 12 }]));
|
|
65
|
+
const { gh, calls } = fakeGh({});
|
|
66
|
+
assert.equal(decide(stopCall(ws.off), { gh }), null);
|
|
67
|
+
assert.equal(decide(stopCall(ws.on), { gh }), null);
|
|
68
|
+
assert.equal(calls.length, 0);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("a closed issue and a commented one are settled once, and never asked about again", (t) => {
|
|
72
|
+
const ws = workspace(t);
|
|
73
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 59 }, { repo: "o/r", number: 12 }]));
|
|
74
|
+
const first = fakeGh({ 59: { state: "CLOSED", comments: [] }, 12: { state: "OPEN", comments: [{ createdAt: "2026-09-29T06:00:00Z" }] } });
|
|
75
|
+
assert.equal(decide(stopCall(ws.on), { gh: first.gh }), null);
|
|
76
|
+
assert.deepEqual(first.calls.map((call) => call.args), [
|
|
77
|
+
["issue", "view", "59", "-R", "o/r", "--json", "state,comments"],
|
|
78
|
+
["issue", "view", "12", "-R", "o/r", "--json", "state,comments"],
|
|
79
|
+
]);
|
|
80
|
+
assert.deepEqual(
|
|
81
|
+
readLedger(ws.ledger).filter((record) => record.type === "issue-update").map((record) => [record.number, record.how, record.prRepo, record.pr]),
|
|
82
|
+
[[59, "closed", "o/r", 60], [12, "commented", "o/r", 60]],
|
|
83
|
+
);
|
|
84
|
+
const second = fakeGh({});
|
|
85
|
+
assert.equal(decide(stopCall(ws.on), { gh: second.gh }), null);
|
|
86
|
+
assert.equal(second.calls.length, 0);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("an open issue with no comment since the merge blocks the stop and names both ways out", (t) => {
|
|
90
|
+
const ws = workspace(t);
|
|
91
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 12 }], { base: "develop", ghEnv: { GH_CONFIG_DIR: "~/.config/gh-personal" } }));
|
|
92
|
+
const { gh, calls } = fakeGh({ 12: { state: "OPEN", comments: [{ createdAt: "2026-09-28T00:00:00Z" }] } });
|
|
93
|
+
const output = decide(stopCall(ws.on), { gh });
|
|
94
|
+
assert.equal(output.decision, "block");
|
|
95
|
+
assert.match(output.reason, /o\/r#12, named by o\/r#60 \(merged into develop\)/);
|
|
96
|
+
assert.match(output.reason, /`gh issue close 12 -R o\/r --comment/);
|
|
97
|
+
assert.match(output.reason, /`gh issue comment 12 -R o\/r --body/);
|
|
98
|
+
// The settings are named in words, so the command runs in Bash and in PowerShell alike.
|
|
99
|
+
assert.doesNotMatch(output.reason, /GH_CONFIG_DIR=|\$env:/);
|
|
100
|
+
assert.match(output.reason, /GH_CONFIG_DIR to `~\/\.config\/gh-personal`/);
|
|
101
|
+
assert.deepEqual(calls[0].options.env, { GH_CONFIG_DIR: "~/.config/gh-personal" });
|
|
102
|
+
assert.equal(readLedger(ws.ledger).some((record) => record.type === "issue-update"), false);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("an issue gh cannot read is named to the user, blocks nothing, and is asked about again", (t) => {
|
|
106
|
+
const ws = workspace(t);
|
|
107
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 77 }]));
|
|
108
|
+
const output = decide(stopCall(ws.on), { gh: fakeGh({}).gh });
|
|
109
|
+
assert.equal(output.decision, undefined);
|
|
110
|
+
assert.match(output.systemMessage, /o\/r#77/);
|
|
111
|
+
const again = fakeGh({});
|
|
112
|
+
decide(stopCall(ws.on), { gh: again.gh });
|
|
113
|
+
assert.equal(again.calls.length, 1);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("a stop already blocked once by this hook is never blocked again, and the open issues are named", (t) => {
|
|
117
|
+
const ws = workspace(t);
|
|
118
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 12 }]));
|
|
119
|
+
const { gh } = fakeGh({ 12: { state: "OPEN", comments: [] } });
|
|
120
|
+
const output = decide({ ...stopCall(ws.on), stop_hook_active: true }, { gh });
|
|
121
|
+
assert.equal(output.decision, undefined);
|
|
122
|
+
assert.match(output.systemMessage, /o\/r#12/);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("a pending merge is resolved from the PR's state, and an unmerged one neither blocks nor is dropped", (t) => {
|
|
126
|
+
const ws = workspace(t);
|
|
127
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 12 }], { pending: true, mergedAt: null }));
|
|
128
|
+
const open = fakeGh({ 60: { state: "OPEN", mergedAt: null } });
|
|
129
|
+
assert.equal(decide(stopCall(ws.on), { gh: open.gh }), null);
|
|
130
|
+
assert.deepEqual(open.calls.map((call) => call.args), [["pr", "view", "60", "-R", "o/r", "--json", "state,mergedAt"]]);
|
|
131
|
+
assert.equal(readLedger(ws.ledger).length, 1, "the pending record stays");
|
|
132
|
+
assert.equal(decide(stopCall(ws.on), { gh: fakeGh({}).gh }), null, "an unreadable PR blocks nothing");
|
|
133
|
+
const merged = fakeGh({ 60: { state: "MERGED", mergedAt: MERGED_AT }, 12: { state: "OPEN", comments: [] } });
|
|
134
|
+
const output = decide(stopCall(ws.on), { gh: merged.gh });
|
|
135
|
+
assert.equal(output.decision, "block");
|
|
136
|
+
assert.match(output.reason, /o\/r#12, named by o\/r#60/);
|
|
137
|
+
const resolved = readLedger(ws.ledger).filter((record) => record.type === "merge" && !record.pending);
|
|
138
|
+
assert.equal(resolved.length, 1);
|
|
139
|
+
assert.equal(resolved[0].mergedAt, MERGED_AT);
|
|
140
|
+
const again = fakeGh({ 12: { state: "CLOSED", comments: [] } });
|
|
141
|
+
assert.equal(decide(stopCall(ws.on), { gh: again.gh }), null);
|
|
142
|
+
assert.deepEqual(again.calls.map((call) => call.args[0]), ["issue"], "a resolved PR is not read again");
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("a stop whose cwd holds no config finds the checkout its session remembered", (t) => {
|
|
146
|
+
const ws = workspace(t);
|
|
147
|
+
const tmp = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-issue-gate-tmp-")));
|
|
148
|
+
const outside = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-issue-gate-out-")));
|
|
149
|
+
t.after(() => rmSync(tmp, { recursive: true, force: true }));
|
|
150
|
+
t.after(() => rmSync(outside, { recursive: true, force: true }));
|
|
151
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 12 }]));
|
|
152
|
+
const { gh, calls } = fakeGh({ 12: { state: "OPEN", comments: [] } });
|
|
153
|
+
assert.equal(decide(stopCall(outside), { gh, tmp }), null);
|
|
154
|
+
assert.equal(calls.length, 0);
|
|
155
|
+
rememberSession("s", ws.on, tmp);
|
|
156
|
+
assert.equal(decide(stopCall(outside), { gh, tmp }).decision, "block");
|
|
157
|
+
assert.equal(calls.length, 1);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("the hook process is silent in a checkout without the checks", (t) => {
|
|
161
|
+
const ws = workspace(t);
|
|
162
|
+
const result = spawnSync(process.execPath, [HOOK], { input: JSON.stringify(stopCall(ws.off)), encoding: "utf8" });
|
|
163
|
+
assert.equal(result.status, 0, result.stderr);
|
|
164
|
+
assert.equal(result.stdout, "");
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("a merge with no gh settings adds no settings sentence", (t) => {
|
|
168
|
+
const ws = workspace(t);
|
|
169
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 12 }]));
|
|
170
|
+
const output = decide(stopCall(ws.on), { gh: fakeGh({ 12: { state: "OPEN", comments: [] } }).gh });
|
|
171
|
+
assert.doesNotMatch(output.reason, /Set GH_|for these gh calls/);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("a ledger write that fails keeps the block for the issues still open", (t) => {
|
|
175
|
+
const ws = workspace(t);
|
|
176
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 59 }, { repo: "o/r", number: 12 }]));
|
|
177
|
+
chmodSync(ws.ledger, 0o444); // reads work, and every append throws
|
|
178
|
+
t.after(() => {
|
|
179
|
+
try {
|
|
180
|
+
chmodSync(ws.ledger, 0o666);
|
|
181
|
+
} catch {
|
|
182
|
+
// The workspace cleanup already removed the file.
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
const { gh } = fakeGh({ 59: { state: "CLOSED", comments: [] }, 12: { state: "OPEN", comments: [] } });
|
|
186
|
+
const output = decide(stopCall(ws.on), { gh });
|
|
187
|
+
assert.equal(output.decision, "block");
|
|
188
|
+
assert.match(output.reason, /o\/r#12/);
|
|
189
|
+
assert.doesNotMatch(output.reason, /o\/r#59/);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test("a pending merge whose PR closed unmerged is recorded resolved once and is never read again", (t) => {
|
|
193
|
+
const ws = workspace(t);
|
|
194
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 12 }], { pending: true, mergedAt: null }));
|
|
195
|
+
const closed = fakeGh({ 60: { state: "CLOSED", mergedAt: null } });
|
|
196
|
+
assert.equal(decide(stopCall(ws.on), { gh: closed.gh }), null);
|
|
197
|
+
assert.deepEqual(closed.calls.map((call) => call.args[0]), ["pr"]);
|
|
198
|
+
const records = readLedger(ws.ledger);
|
|
199
|
+
assert.equal(records.length, 2);
|
|
200
|
+
assert.equal(records[1].closed, true);
|
|
201
|
+
assert.equal(records[1].pending, undefined);
|
|
202
|
+
const again = fakeGh({ 60: { state: "CLOSED", mergedAt: null } });
|
|
203
|
+
assert.equal(decide(stopCall(ws.on), { gh: again.gh }), null);
|
|
204
|
+
assert.equal(again.calls.length, 0, "the closed PR is not read again");
|
|
205
|
+
assert.equal(readLedger(ws.ledger).length, 2, "and no second record is written");
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("a PR closed unmerged names no issue, and one merged after a later pending record is read again", (t) => {
|
|
209
|
+
const ws = workspace(t);
|
|
210
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 12 }], { pending: true, mergedAt: null }));
|
|
211
|
+
decide(stopCall(ws.on), { gh: fakeGh({ 60: { state: "CLOSED", mergedAt: null } }).gh });
|
|
212
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 12 }], { pending: true, mergedAt: null }));
|
|
213
|
+
const merged = fakeGh({ 60: { state: "MERGED", mergedAt: MERGED_AT }, 12: { state: "OPEN", comments: [] } });
|
|
214
|
+
assert.equal(decide(stopCall(ws.on), { gh: merged.gh }).decision, "block");
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("a stop the hook already blocked writes nothing to the ledger", (t) => {
|
|
218
|
+
const ws = workspace(t);
|
|
219
|
+
appendRecord(ws.ledger, merge("s", [{ repo: "o/r", number: 59 }, { repo: "o/r", number: 12 }]));
|
|
220
|
+
appendRecord(ws.ledger, { ...merge("s", [{ repo: "o/r", number: 70 }], { pending: true, mergedAt: null }), pr: 61 });
|
|
221
|
+
appendRecord(ws.ledger, { ...merge("s", [], { pending: true, mergedAt: null }), pr: 62 });
|
|
222
|
+
const before = readLedger(ws.ledger).length;
|
|
223
|
+
const { gh } = fakeGh({
|
|
224
|
+
59: { state: "CLOSED", comments: [] },
|
|
225
|
+
12: { state: "OPEN", comments: [] },
|
|
226
|
+
70: { state: "OPEN", comments: [] },
|
|
227
|
+
61: { state: "MERGED", mergedAt: MERGED_AT },
|
|
228
|
+
62: { state: "CLOSED", mergedAt: null },
|
|
229
|
+
});
|
|
230
|
+
const output = decide({ ...stopCall(ws.on), stop_hook_active: true }, { gh });
|
|
231
|
+
assert.equal(output.decision, undefined);
|
|
232
|
+
assert.match(output.systemMessage, /o\/r#12/);
|
|
233
|
+
assert.equal(readLedger(ws.ledger).length, before, "a stop with the flag set records nothing new");
|
|
234
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// The resource for the workflow ledger: JSON lines in `<git common dir>/qc/ledger.jsonl`, shared by every
|
|
2
|
+
// worktree. This module is its one writer, and every workflow check reads it through `readLedger`.
|
|
3
|
+
|
|
4
|
+
import { appendFileSync, closeSync, mkdirSync, openSync, readFileSync, readSync, statSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
7
|
+
|
|
8
|
+
export const RECORD_TYPES = ["dispatch", "stop", "verdict", "merge", "issue-update"];
|
|
9
|
+
const MIN_SHA = 7;
|
|
10
|
+
const HEX = /^[0-9a-f]+$/;
|
|
11
|
+
|
|
12
|
+
/** @returns the git common dir of the checkout at `root`, read with no git process, or null. */
|
|
13
|
+
export function commonDirOf(root) {
|
|
14
|
+
const dotGit = path.join(root, ".git");
|
|
15
|
+
let stat;
|
|
16
|
+
try {
|
|
17
|
+
stat = statSync(dotGit);
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
if (stat.isDirectory()) return dotGit;
|
|
22
|
+
// A linked worktree's `.git` file names its own git dir, whose `commondir` names the shared one.
|
|
23
|
+
const match = /^gitdir:[ \t]*(.+?)[ \t]*$/m.exec(readFileSync(dotGit, "utf8"));
|
|
24
|
+
if (!match) return null;
|
|
25
|
+
const gitDir = path.resolve(root, match[1]);
|
|
26
|
+
try {
|
|
27
|
+
return path.resolve(gitDir, readFileSync(path.join(gitDir, "commondir"), "utf8").trim());
|
|
28
|
+
} catch {
|
|
29
|
+
return gitDir;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @returns the ledger file of the checkout at `root`, or null when it has no git dir. */
|
|
34
|
+
export function ledgerFileOf(root) {
|
|
35
|
+
const common = commonDirOf(root);
|
|
36
|
+
return common ? path.join(common, "qc", "ledger.jsonl") : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** True when the file holds bytes and its last one is not a newline, which is a line torn by a crash. */
|
|
40
|
+
function endsTorn(file) {
|
|
41
|
+
let fd;
|
|
42
|
+
try {
|
|
43
|
+
fd = openSync(file, "r");
|
|
44
|
+
const { size } = statSync(file);
|
|
45
|
+
if (size === 0) return false;
|
|
46
|
+
const last = Buffer.alloc(1);
|
|
47
|
+
readSync(fd, last, 0, 1, size - 1);
|
|
48
|
+
return last[0] !== 0x0a;
|
|
49
|
+
} catch {
|
|
50
|
+
return false;
|
|
51
|
+
} finally {
|
|
52
|
+
if (fd !== undefined) closeSync(fd);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Appends one record, stamped with its time, on a line of its own. An unknown type throws, so no reader meets a shape it does not know. */
|
|
57
|
+
export function appendRecord(file, record, now = new Date()) {
|
|
58
|
+
if (!RECORD_TYPES.includes(record?.type)) throw new Error(`ledger: unknown record type ${JSON.stringify(record?.type)}`);
|
|
59
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
60
|
+
const line = `${JSON.stringify({ ...record, at: now.toISOString() })}\n`;
|
|
61
|
+
// A torn last line ends at this newline, so it never swallows the new record.
|
|
62
|
+
appendFileSync(file, endsTorn(file) ? `\n${line}` : line);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Every record in the file, oldest first. A missing file is an empty ledger, and a torn line is skipped. */
|
|
66
|
+
export function readLedger(file) {
|
|
67
|
+
let text;
|
|
68
|
+
try {
|
|
69
|
+
text = readFileSync(file, "utf8");
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (error.code === "ENOENT") return [];
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
const records = [];
|
|
75
|
+
for (const line of text.split(/\r?\n/)) {
|
|
76
|
+
if (line.trim() === "") continue;
|
|
77
|
+
try {
|
|
78
|
+
const record = JSON.parse(line);
|
|
79
|
+
if (RECORD_TYPES.includes(record?.type)) records.push(record);
|
|
80
|
+
} catch {
|
|
81
|
+
// A line torn by a crash mid-write carries no record.
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return records;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** True when two shas name one commit: the shorter, of at least seven hex digits, starts the longer. */
|
|
88
|
+
export function shaMatches(a, b) {
|
|
89
|
+
if (typeof a !== "string" || typeof b !== "string") return false;
|
|
90
|
+
const [short, long] = a.length <= b.length ? [a.toLowerCase(), b.toLowerCase()] : [b.toLowerCase(), a.toLowerCase()];
|
|
91
|
+
return short.length >= MIN_SHA && HEX.test(short) && long.startsWith(short);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The newest verdict of one of `kinds` that names `sha`, or null. */
|
|
95
|
+
export const latestVerdictFor = (records, sha, kinds) =>
|
|
96
|
+
records.findLast((record) => record.type === "verdict" && kinds.includes(record.kind) && shaMatches(record.sha, sha)) ?? null;
|
|
97
|
+
|
|
98
|
+
/** The newest verdict of any kind on `branch`, or null. */
|
|
99
|
+
export const latestVerdictOn = (records, branch) =>
|
|
100
|
+
records.findLast((record) => record.type === "verdict" && record.branch === branch) ?? null;
|
|
101
|
+
|
|
102
|
+
/** The newest implementer dispatch that named a worktree on `branch`, or null. */
|
|
103
|
+
export const lastTaskDispatchOn = (records, branch) =>
|
|
104
|
+
records.findLast((record) => record.type === "dispatch" && record.task === true && record.branch === branch) ?? null;
|
|
105
|
+
|
|
106
|
+
const short = (sha) => (typeof sha === "string" ? sha.slice(0, MIN_SHA) : "");
|
|
107
|
+
|
|
108
|
+
const SUMMARIES = {
|
|
109
|
+
dispatch: (r) =>
|
|
110
|
+
`${r.agentType} ${r.task ? `${r.branch} ${short(r.head)}` : "(no task)"}${r.resumeReason ? ` NO-RESUME: ${r.resumeReason}` : ""}${
|
|
111
|
+
r.handoff ? ` HANDOFF: ${r.handoff}` : ""
|
|
112
|
+
}`,
|
|
113
|
+
stop: (r) => `${r.role} ${r.agentType} ${r.agentId}${r.plan ? ` ${r.plan}` : ""}${r.handoff ? ` HANDOFF: ${r.handoff}` : ""}`,
|
|
114
|
+
verdict: (r) => `${r.kind} ${r.verdict} ${short(r.sha)} ${r.branch ?? "(no branch)"}`,
|
|
115
|
+
merge: (r) =>
|
|
116
|
+
`${r.repo}#${r.pr} ${short(r.sha)} ${r.branch ?? ""} into ${r.base ?? "?"} names ${
|
|
117
|
+
(r.issues ?? []).map((issue) => `${issue.repo}#${issue.number}`).join(" ") || "no issue"
|
|
118
|
+
}`,
|
|
119
|
+
"issue-update": (r) => `${r.repo}#${r.number} ${r.how} after ${r.prRepo}#${r.pr}`,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/** One line of `qc ledger`: the time, the type, and the fields that say what happened. */
|
|
123
|
+
export const formatRecord = (record) =>
|
|
124
|
+
`${record.at} ${record.type.padEnd(12)} ${SUMMARIES[record.type](record).replace(/\s+/g, " ").trim()}`;
|
|
125
|
+
|
|
126
|
+
/** `qc ledger [branch]`: every record, or those that name the branch. @returns the exit code */
|
|
127
|
+
export function runLedger(config, [branch] = []) {
|
|
128
|
+
const root = checkoutRootOf(config.root ?? process.cwd());
|
|
129
|
+
const file = root ? ledgerFileOf(root) : null;
|
|
130
|
+
if (!file) {
|
|
131
|
+
console.error("qc ledger: this folder is in no git checkout.");
|
|
132
|
+
return 1;
|
|
133
|
+
}
|
|
134
|
+
const records = readLedger(file).filter((record) => branch === undefined || record.branch === branch);
|
|
135
|
+
for (const record of records) console.log(formatRecord(record));
|
|
136
|
+
if (records.length === 0) console.log(branch ? `No record in ${file} names ${branch}.` : `The ledger at ${file} is empty.`);
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
+
import { appendFileSync, 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 {
|
|
9
|
+
appendRecord,
|
|
10
|
+
commonDirOf,
|
|
11
|
+
lastTaskDispatchOn,
|
|
12
|
+
latestVerdictFor,
|
|
13
|
+
latestVerdictOn,
|
|
14
|
+
ledgerFileOf,
|
|
15
|
+
readLedger,
|
|
16
|
+
shaMatches,
|
|
17
|
+
} from "./ledger.mjs";
|
|
18
|
+
|
|
19
|
+
const QC = fileURLToPath(new URL("./qc.mjs", import.meta.url));
|
|
20
|
+
const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe", encoding: "utf8" }).trim();
|
|
21
|
+
|
|
22
|
+
/** A primary checkout with one commit, and a linked worktree of it on feat/1-x. */
|
|
23
|
+
function repo(t) {
|
|
24
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-ledger-")));
|
|
25
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
26
|
+
const main = path.join(base, "main");
|
|
27
|
+
git(base, "init", "-q", "-b", "main", main);
|
|
28
|
+
for (const [key, value] of [["user.name", "qc"], ["user.email", "qc@example.com"], ["commit.gpgsign", "false"]]) {
|
|
29
|
+
git(main, "config", key, value);
|
|
30
|
+
}
|
|
31
|
+
writeFileSync(path.join(main, "a.txt"), "a\n");
|
|
32
|
+
git(main, "add", ".");
|
|
33
|
+
git(main, "commit", "-q", "-m", "init");
|
|
34
|
+
const linked = path.join(base, "linked");
|
|
35
|
+
git(main, "worktree", "add", "-q", "-b", "feat/1-x", linked);
|
|
36
|
+
return { base, main, linked };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test("a linked worktree and its primary checkout share one ledger", (t) => {
|
|
40
|
+
const { main, linked } = repo(t);
|
|
41
|
+
assert.equal(realpathSync.native(commonDirOf(linked)), realpathSync.native(path.join(main, ".git")));
|
|
42
|
+
assert.equal(ledgerFileOf(main), path.join(main, ".git", "qc", "ledger.jsonl"));
|
|
43
|
+
const commonOf = (root) => realpathSync.native(path.dirname(path.dirname(ledgerFileOf(root))));
|
|
44
|
+
assert.equal(commonOf(linked), commonOf(main));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("a folder with no .git has no ledger", (t) => {
|
|
48
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-ledger-none-")));
|
|
49
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
50
|
+
assert.equal(commonDirOf(base), null);
|
|
51
|
+
assert.equal(ledgerFileOf(base), null);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("records append as JSON lines with their time, and a torn line is skipped", (t) => {
|
|
55
|
+
const { main } = repo(t);
|
|
56
|
+
const file = ledgerFileOf(main);
|
|
57
|
+
assert.deepEqual(readLedger(file), []);
|
|
58
|
+
appendRecord(file, { type: "dispatch", agentType: "sdd-implementer" }, new Date("2026-01-01T00:00:00Z"));
|
|
59
|
+
appendFileSync(file, '{"type":"verdict","kind":\n');
|
|
60
|
+
appendRecord(file, { type: "stop", agentType: "sdd-implementer", agentId: "a1" }, new Date("2026-01-01T00:01:00Z"));
|
|
61
|
+
assert.deepEqual(
|
|
62
|
+
readLedger(file).map((record) => [record.type, record.at]),
|
|
63
|
+
[
|
|
64
|
+
["dispatch", "2026-01-01T00:00:00.000Z"],
|
|
65
|
+
["stop", "2026-01-01T00:01:00.000Z"],
|
|
66
|
+
],
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("an unknown record type is refused, so no reader meets a shape it does not know", (t) => {
|
|
71
|
+
const { main } = repo(t);
|
|
72
|
+
assert.throws(() => appendRecord(ledgerFileOf(main), { type: "note" }), /unknown record type "note"/);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("two shas match when the shorter, of seven hex digits or more, starts the longer", () => {
|
|
76
|
+
const full = "0123456789abcdef0123456789abcdef01234567";
|
|
77
|
+
assert.ok(shaMatches("0123456", full));
|
|
78
|
+
assert.ok(shaMatches(full.toUpperCase(), full));
|
|
79
|
+
assert.ok(!shaMatches("012345", full));
|
|
80
|
+
assert.ok(!shaMatches("0123457", full));
|
|
81
|
+
assert.ok(!shaMatches("zzzzzzz", "zzzzzzzz"));
|
|
82
|
+
assert.ok(!shaMatches(undefined, full));
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("the queries find the newest record of each kind", () => {
|
|
86
|
+
const records = [
|
|
87
|
+
{ type: "verdict", kind: "task", verdict: "CHANGES_REQUIRED", sha: "aaaaaaa1", branch: "feat/1-x" },
|
|
88
|
+
{ type: "dispatch", task: true, branch: "feat/1-x", head: "h0" },
|
|
89
|
+
{ type: "verdict", kind: "task", verdict: "APPROVED", sha: "aaaaaaa1", branch: "feat/1-x" },
|
|
90
|
+
{ type: "verdict", kind: "branch", verdict: "APPROVED", sha: "bbbbbbb2", branch: "feat/2-y" },
|
|
91
|
+
{ type: "dispatch", task: false, branch: null, head: null },
|
|
92
|
+
];
|
|
93
|
+
assert.equal(latestVerdictFor(records, "aaaaaaa", ["task"]).verdict, "APPROVED");
|
|
94
|
+
assert.equal(latestVerdictFor(records, "aaaaaaa", ["branch"]), null);
|
|
95
|
+
assert.equal(latestVerdictOn(records, "feat/2-y").kind, "branch");
|
|
96
|
+
assert.equal(latestVerdictOn(records, "feat/3-z"), null);
|
|
97
|
+
assert.equal(lastTaskDispatchOn(records, "feat/1-x").head, "h0");
|
|
98
|
+
assert.equal(lastTaskDispatchOn(records, "feat/2-y"), null);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("qc ledger prints one line per record, and a branch keeps only its own", (t) => {
|
|
102
|
+
const { main } = repo(t);
|
|
103
|
+
const empty = spawnSync(process.execPath, [QC, "ledger"], { cwd: main, encoding: "utf8" });
|
|
104
|
+
assert.equal(empty.status, 0, empty.stderr);
|
|
105
|
+
assert.match(empty.stdout, /is empty/);
|
|
106
|
+
const file = ledgerFileOf(main);
|
|
107
|
+
appendRecord(file, { type: "verdict", kind: "task", verdict: "APPROVED", sha: "0123456789abcdef", branch: "feat/1-x", agentId: "r1" });
|
|
108
|
+
appendRecord(file, { type: "verdict", kind: "branch", verdict: "CHANGES_REQUIRED", sha: "fedcba9876543210", branch: "feat/2-y", agentId: "r2" });
|
|
109
|
+
const all = spawnSync(process.execPath, [QC, "ledger"], { cwd: main, encoding: "utf8" });
|
|
110
|
+
assert.equal(all.status, 0, all.stderr);
|
|
111
|
+
assert.equal(all.stdout.trim().split(/\r?\n/).length, 2);
|
|
112
|
+
const one = spawnSync(process.execPath, [QC, "ledger", "feat/2-y"], { cwd: main, encoding: "utf8" });
|
|
113
|
+
assert.match(one.stdout, /verdict\s+branch CHANGES_REQUIRED fedcba9 feat\/2-y/);
|
|
114
|
+
assert.doesNotMatch(one.stdout, /feat\/1-x/);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("an append after a torn last line keeps the new record whole", (t) => {
|
|
118
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-ledger-torn-")));
|
|
119
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
120
|
+
const file = path.join(base, "ledger.jsonl");
|
|
121
|
+
appendRecord(file, { type: "stop", role: "implementer" });
|
|
122
|
+
appendFileSync(file, '{"type":"stop","role":"tor');
|
|
123
|
+
appendRecord(file, { type: "verdict", verdict: "APPROVED" });
|
|
124
|
+
assert.deepEqual(readLedger(file).map((record) => record.type), ["stop", "verdict"]);
|
|
125
|
+
appendRecord(file, { type: "stop", role: "task" });
|
|
126
|
+
assert.equal(readLedger(file).length, 3, "a file that ends in a newline gets no blank line");
|
|
127
|
+
});
|