architecture-harness 0.16.0 → 0.17.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/check-registries.test.mjs +181 -0
- package/src/cli/check.mjs +130 -9
- package/src/cli/cli.test.mjs +215 -2
- 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 +9 -3
- package/src/cli/dispatch-reminder.mjs +19 -11
- package/src/cli/dispatch-reminder.test.mjs +32 -0
- package/src/cli/dispatch.mjs +3 -0
- package/src/cli/dispatch.test.mjs +15 -1
- package/src/cli/doctor.mjs +2 -1
- package/src/cli/explore-guard.mjs +2 -2
- 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/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 +136 -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 +96 -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 +16 -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 +256 -0
- package/src/cli/report-stop.test.mjs +432 -0
- package/src/cli/shell-command.mjs +81 -6
- package/src/cli/shell-command.test.mjs +101 -1
- package/src/cli/task-gate.mjs +157 -0
- package/src/cli/task-gate.test.mjs +421 -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 +84 -3
- 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,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
|
+
}
|
|
@@ -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,136 @@
|
|
|
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
|
+
stop: (r) => `${r.role} ${r.agentType} ${r.agentId}${r.plan ? ` ${r.plan}` : ""}`,
|
|
112
|
+
verdict: (r) => `${r.kind} ${r.verdict} ${short(r.sha)} ${r.branch ?? "(no branch)"}`,
|
|
113
|
+
merge: (r) =>
|
|
114
|
+
`${r.repo}#${r.pr} ${short(r.sha)} ${r.branch ?? ""} into ${r.base ?? "?"} names ${
|
|
115
|
+
(r.issues ?? []).map((issue) => `${issue.repo}#${issue.number}`).join(" ") || "no issue"
|
|
116
|
+
}`,
|
|
117
|
+
"issue-update": (r) => `${r.repo}#${r.number} ${r.how} after ${r.prRepo}#${r.pr}`,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/** One line of `qc ledger`: the time, the type, and the fields that say what happened. */
|
|
121
|
+
export const formatRecord = (record) =>
|
|
122
|
+
`${record.at} ${record.type.padEnd(12)} ${SUMMARIES[record.type](record).replace(/\s+/g, " ").trim()}`;
|
|
123
|
+
|
|
124
|
+
/** `qc ledger [branch]`: every record, or those that name the branch. @returns the exit code */
|
|
125
|
+
export function runLedger(config, [branch] = []) {
|
|
126
|
+
const root = checkoutRootOf(config.root ?? process.cwd());
|
|
127
|
+
const file = root ? ledgerFileOf(root) : null;
|
|
128
|
+
if (!file) {
|
|
129
|
+
console.error("qc ledger: this folder is in no git checkout.");
|
|
130
|
+
return 1;
|
|
131
|
+
}
|
|
132
|
+
const records = readLedger(file).filter((record) => branch === undefined || record.branch === branch);
|
|
133
|
+
for (const record of records) console.log(formatRecord(record));
|
|
134
|
+
if (records.length === 0) console.log(branch ? `No record in ${file} names ${branch}.` : `The ledger at ${file} is empty.`);
|
|
135
|
+
return 0;
|
|
136
|
+
}
|
|
@@ -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
|
+
});
|