architecture-harness 0.20.2 → 0.20.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli/bash-command-guard.mjs +70 -11
- package/src/cli/bash-command-guard.test.mjs +219 -0
- package/src/cli/bash-edit-guard.mjs +19 -16
- package/src/cli/bash-edit-guard.test.mjs +19 -0
- package/src/cli/ci-state.mjs +54 -0
- package/src/cli/ci-state.test.mjs +76 -0
- package/src/cli/cost-stop.mjs +98 -0
- package/src/cli/cost-stop.test.mjs +149 -0
- package/src/cli/dispatch-guard.mjs +3 -3
- package/src/cli/dispatch-guard.test.mjs +14 -6
- package/src/cli/dispatch-reminder.mjs +7 -3
- package/src/cli/dispatch-reminder.test.mjs +16 -1
- package/src/cli/git-read.mjs +41 -3
- package/src/cli/git-read.test.mjs +31 -1
- package/src/cli/ledger.mjs +52 -3
- package/src/cli/ledger.test.mjs +60 -0
- package/src/cli/merge-guard.mjs +1 -14
- package/src/cli/plugin-agents.test.mjs +11 -0
- package/src/cli/powershell-command.mjs +8 -13
- package/src/cli/powershell-command.test.mjs +12 -1
- package/src/cli/qc.mjs +1 -1
- package/src/cli/report-stop.test.mjs +2 -1
- package/src/cli/review-gate.mjs +224 -0
- package/src/cli/review-gate.test.mjs +334 -0
- package/src/cli/shell-command.mjs +77 -13
- package/src/cli/shell-command.test.mjs +44 -3
- package/src/cli/skill-guard.mjs +56 -0
- package/src/cli/skill-guard.test.mjs +87 -0
- package/src/cli/stop-gate.test.mjs +80 -0
- package/src/cli/task-gate-review.test.mjs +225 -0
- package/src/cli/task-gate.mjs +54 -16
- package/src/cli/task-gate.test.mjs +65 -6
- package/src/cli/test-brief.mjs +12 -0
- package/src/cli/transcript-usage.mjs +28 -0
- package/src/cli/transcript-usage.test.mjs +9 -0
- package/src/cli/workflow-hooks.test.mjs +38 -0
- package/src/cli/workflow-settings.mjs +14 -3
- package/src/cli/workflow-settings.test.mjs +24 -0
- package/src/config.mjs +13 -0
- package/templates/docs/enforcement.md +12 -1
package/src/cli/merge-guard.mjs
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
// A merge that names another repository than the checkout's `origin` is that repository's own, so it passes.
|
|
6
6
|
// With `swarm.dispatch` off, the record is still written for the worktree check of QC-015, and nothing is judged.
|
|
7
7
|
|
|
8
|
-
import { spawnSync } from "node:child_process";
|
|
9
8
|
import { fileURLToPath } from "node:url";
|
|
10
9
|
import { PARSERS } from "./bash-command-guard.mjs";
|
|
11
10
|
import { LANE_BUDGET_MS, mergeLane, sizeLine } from "./direct-lane.mjs";
|
|
12
11
|
import { ghApiMerges, ghMerges } from "./gh-merge-command.mjs";
|
|
13
12
|
import { runGh } from "./gh-run.mjs";
|
|
13
|
+
import { originOf, repoOfSpelling } from "./git-read.mjs";
|
|
14
14
|
import { appendRecord, latestVerdictFor, readLedger } from "./ledger.mjs";
|
|
15
15
|
import { workflowAt } from "./workflow-settings.mjs";
|
|
16
16
|
import { worktreeRuleAt } from "./worktree-settings.mjs";
|
|
@@ -19,7 +19,6 @@ const VIEW_FIELDS = "number,url,state,mergedAt,baseRefName,headRefName,body,clos
|
|
|
19
19
|
const MAYBE_GH = /\bgh(?:\.exe)?\b/i;
|
|
20
20
|
const BODY_REF = /(?<![\w/#])#(\d+)\b/g;
|
|
21
21
|
const REF_LINE = /^\s*(?:[-*]\s+)?(?:refs|closes|fixes|resolves)\b/i;
|
|
22
|
-
const ORIGIN_TIMEOUT_MS = 5_000;
|
|
23
22
|
const PR_URL = /^https?:\/\/[^/]+\/([^/]+\/[^/]+)\/pull\/\d+/;
|
|
24
23
|
const POST_EVENTS = new Set(["PostToolUse", "PostToolUseFailure"]);
|
|
25
24
|
const REVIEWER = { branch: "the branch reviewer (sdd-branch-reviewer)", task: "the task reviewer (sdd-reviewer)" };
|
|
@@ -112,18 +111,6 @@ export function namedIssues(pr, repo) {
|
|
|
112
111
|
return [...found.values()];
|
|
113
112
|
}
|
|
114
113
|
|
|
115
|
-
/** `owner/repo` from a git remote URL in the https, ssh, or scp spelling, or from gh's `[HOST/]OWNER/REPO`; lower case. */
|
|
116
|
-
function repoOfSpelling(spelled) {
|
|
117
|
-
const parts = String(spelled).trim().replace(/\/+$/, "").replace(/\.git$/i, "").split(/[/:]/).filter(Boolean);
|
|
118
|
-
return parts.length >= 2 ? parts.slice(-2).join("/").toLowerCase() : null;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** The repository of the checkout's `origin` remote, or null when git cannot say. */
|
|
122
|
-
function originOf(root) {
|
|
123
|
-
const result = spawnSync("git", ["remote", "get-url", "origin"], { cwd: root, encoding: "utf8", windowsHide: true, timeout: ORIGIN_TIMEOUT_MS });
|
|
124
|
-
return result.status === 0 ? repoOfSpelling(result.stdout) : null;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
114
|
/** Splits the merges into those of this checkout's repository and those that name another repository. */
|
|
128
115
|
function partition(merges, root) {
|
|
129
116
|
const origin = merges.some((merge) => merge.repo) ? originOf(root) : null;
|
|
@@ -155,3 +155,14 @@ test("no type pins a prompt cache lifetime, so a user's own setting decides it",
|
|
|
155
155
|
assert.equal(front.cacheTtl, undefined, `${name} sets cacheTtl`);
|
|
156
156
|
}
|
|
157
157
|
});
|
|
158
|
+
|
|
159
|
+
test("both reviewers review only the increment when the dispatch carries a Reviewed line, and still put the verdict first", () => {
|
|
160
|
+
for (const name of ["sdd-reviewer", "sdd-branch-reviewer"]) {
|
|
161
|
+
const text = readFileSync(path.join(AGENTS, `${name}.md`), "utf8").replace(/\r/g, "");
|
|
162
|
+
assert.match(text, /`Reviewed: <sha>`/, name);
|
|
163
|
+
assert.match(text, /<sha>\.\.<head>/, name);
|
|
164
|
+
assert.match(text, /only that increment|only the increment/i, name);
|
|
165
|
+
assert.match(text, /Without the line, review as before/, name);
|
|
166
|
+
assert.ok(text.indexOf("`Reviewed: <sha>`") < text.indexOf("`VERDICT: APPROVED <sha>`"), `${name} keeps the verdict line rule last`);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// A fragment that reads a PowerShell command the way shell-command.mjs reads a POSIX one, for the
|
|
2
2
|
// Bash edit guard. It shares that parser and its git vocabulary, and an unknown name counts as a write.
|
|
3
3
|
|
|
4
|
-
import { GIT_READS, commandWords, gitCall, segmentsOf } from "./shell-command.mjs";
|
|
4
|
+
import { GIT_READS, checkoutDirsOf, commandWords, gitCall, homeOrGroup, segmentsOf, unresolvedGitDirsOf } from "./shell-command.mjs";
|
|
5
5
|
|
|
6
6
|
const DIALECT = { escape: "`" };
|
|
7
7
|
|
|
@@ -27,10 +27,10 @@ export function locationTarget(segment) {
|
|
|
27
27
|
const [program, ...args] = commandWords(segment);
|
|
28
28
|
if (!LOCATION_NAMES.has(program?.toLowerCase())) return null;
|
|
29
29
|
for (let i = 0; i < args.length; i++) {
|
|
30
|
-
if (PATH_PARAMETERS.has(args[i].toLowerCase())) return args[i + 1] ??
|
|
30
|
+
if (PATH_PARAMETERS.has(args[i].toLowerCase())) return args[i + 1] ?? homeOrGroup(segment);
|
|
31
31
|
if (!args[i].startsWith("-")) return args[i];
|
|
32
32
|
}
|
|
33
|
-
return
|
|
33
|
+
return homeOrGroup(segment);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
function isReadOnly(segment) {
|
|
@@ -48,13 +48,8 @@ function isReadOnly(segment) {
|
|
|
48
48
|
/** False only when every segment is known to read, so an unknown command counts as a write. */
|
|
49
49
|
export const mayWrite = (command) => !segmentsOf(command, DIALECT).every(isReadOnly);
|
|
50
50
|
|
|
51
|
-
/** Every directory the command names through a location change
|
|
52
|
-
export
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (target !== null) dirs.push(target);
|
|
57
|
-
dirs.push(...(gitCall(segment)?.dirs ?? []).filter(Boolean));
|
|
58
|
-
}
|
|
59
|
-
return dirs;
|
|
60
|
-
}
|
|
51
|
+
/** Every directory the command names through a location change, `git -C` or a work tree, in order, as written. */
|
|
52
|
+
export const checkoutDirs = (command) => checkoutDirsOf(powershellSegments(command), locationTarget);
|
|
53
|
+
|
|
54
|
+
/** The git directories a command sets with no work tree. */
|
|
55
|
+
export const unresolvedGitDirs = (command) => unresolvedGitDirsOf(powershellSegments(command));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
2
|
import { test } from "node:test";
|
|
3
|
-
import { checkoutDirs, mayWrite } from "./powershell-command.mjs";
|
|
3
|
+
import { checkoutDirs, mayWrite, unresolvedGitDirs } from "./powershell-command.mjs";
|
|
4
4
|
|
|
5
5
|
test("a PowerShell command that only reads skips the check, in any letter case", () => {
|
|
6
6
|
for (const command of [
|
|
@@ -49,4 +49,15 @@ test("the checkout directories are each location change and each git -C director
|
|
|
49
49
|
["C:\\repo\\a", "C:\\b c", "..\\d", "C:\\e"],
|
|
50
50
|
);
|
|
51
51
|
assert.deepEqual(checkoutDirs("Get-ChildItem"), []);
|
|
52
|
+
assert.deepEqual(checkoutDirs("Set-Content f x; git -C"), []);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("a work tree counts as a checkout directory in PowerShell, and a bare git dir is unresolved", () => {
|
|
56
|
+
const W = String.raw`C:\w`;
|
|
57
|
+
const R = String.raw`C:\r\.git`;
|
|
58
|
+
assert.deepEqual(checkoutDirs(`git --work-tree=${W} checkout main`), [W]);
|
|
59
|
+
assert.deepEqual(checkoutDirs(`$env:GIT_WORK_TREE = "${W}"; git checkout main`), [W]);
|
|
60
|
+
assert.deepEqual(unresolvedGitDirs(`$env:GIT_DIR = "${R}"; git checkout main`), [R]);
|
|
61
|
+
assert.deepEqual(unresolvedGitDirs(`git --git-dir=${R} --work-tree=${W} checkout main`), []);
|
|
62
|
+
assert.deepEqual(checkoutDirs("git -C"), []);
|
|
52
63
|
});
|
package/src/cli/qc.mjs
CHANGED
|
@@ -22,7 +22,7 @@ const USAGE = `qc — architecture gates
|
|
|
22
22
|
Vale on the lines a diff adds to the docs, and on a PR body; needs the vale binary
|
|
23
23
|
qc commit-msg <file> conventional (commitlint), English, and the trailer when attribution is on
|
|
24
24
|
qc decisions [id] where decisions are cited; --squash closes the gaps
|
|
25
|
-
qc ledger [branch]
|
|
25
|
+
qc ledger [--cost] [branch] the workflow ledger: dispatches, stops, verdicts, merges, issue updates; --cost totals tokens and tool calls
|
|
26
26
|
qc plan-check <plan.md> each task names files, a test step and a commit step, within the tool-call budget
|
|
27
27
|
qc tokens [--session <id>] [--project <dir>] [--claude-dir <dir>] [--json]
|
|
28
28
|
one session's tokens and cache hit rate per agent type, from its transcripts
|
|
@@ -8,6 +8,7 @@ import { fileURLToPath } from "node:url";
|
|
|
8
8
|
import { appendRecord, readLedger } from "./ledger.mjs";
|
|
9
9
|
import { branchOf, decide, verdictOf } from "./report-stop.mjs";
|
|
10
10
|
import { judgeTask } from "./task-gate.mjs";
|
|
11
|
+
import { BRIEF } from "./test-brief.mjs";
|
|
11
12
|
import { rememberSession } from "./workflow-settings.mjs";
|
|
12
13
|
|
|
13
14
|
const HOOK = fileURLToPath(new URL("./report-stop.mjs", import.meta.url));
|
|
@@ -336,7 +337,7 @@ test("a stop with no new commit writes a worktree and a branch but no head, so i
|
|
|
336
337
|
cwd: ws.on,
|
|
337
338
|
hook_event_name: "PreToolUse",
|
|
338
339
|
tool_name: "Agent",
|
|
339
|
-
tool_input: { subagent_type: IMPLEMENTER, description: "t", prompt: `
|
|
340
|
+
tool_input: { subagent_type: IMPLEMENTER, description: "t", prompt: `Brief: ${BRIEF}\nWorktree: ${a.dir}` },
|
|
340
341
|
},
|
|
341
342
|
ws.tmp,
|
|
342
343
|
);
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// The checks of `swarm.review` that read a dispatch prompt and its branch: one branch review, a head that
|
|
2
|
+
// is green and holds the base tip, and a brief with product decisions. The task gate calls them.
|
|
3
|
+
|
|
4
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { gitOutWithin, isAncestor, remoteTip } from "./git-read.mjs";
|
|
7
|
+
import { shaMatches } from "./ledger.mjs";
|
|
8
|
+
import { commitLookup, nativePath } from "./workflow-place.mjs";
|
|
9
|
+
|
|
10
|
+
// Every read of a reviewer dispatch is capped, so the whole guard ends inside the Agent hook's 30 second limit.
|
|
11
|
+
// Worst case: head 2 + branch 2 + origin tip 8 + lookup 2 + ancestor 2 + CI (origin 1, gh 3) = 20 s.
|
|
12
|
+
export const READ_MS = 2_000;
|
|
13
|
+
const REMOTE_MS = 8_000;
|
|
14
|
+
|
|
15
|
+
const REVIEWED_LINE = /^[ \t]*Reviewed:[ \t]*([0-9a-f]{7,40})[ \t]*$/im;
|
|
16
|
+
const BRIEF_LINE = /^[ \t]*Brief:[ \t]*(\S.*?)[ \t]*$/im;
|
|
17
|
+
const QUOTES = /^["'`]|["'`]$/g;
|
|
18
|
+
const PRODUCT_HEADING = /^##[ \t]+Product decisions[ \t]*$/i;
|
|
19
|
+
const SECTION_END = /^#{1,2}[ \t]/;
|
|
20
|
+
const FINDING_LINE = /^[ \t]*\d+\.[ \t]/gm;
|
|
21
|
+
const URL_TEXT = /https?:\/\/\S+/gi;
|
|
22
|
+
const TOKEN_CHARS = /[^\w@~./\\-]+/;
|
|
23
|
+
const EXTENSION = /\.([A-Za-z][A-Za-z0-9]+)$/;
|
|
24
|
+
const KNOWN_EXTENSIONS = new Set(
|
|
25
|
+
"mjs cjs js jsx ts tsx json md yml yaml sh sql css html cs py go rs toml txt xml csv".split(" "),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const short = (sha) => (typeof sha === "string" ? sha.slice(0, 7) : "(none)");
|
|
29
|
+
const pass = (note = null) => ({ refusal: null, note });
|
|
30
|
+
const refuse = (refusal) => ({ refusal, note: null });
|
|
31
|
+
|
|
32
|
+
/** The sha on a prompt's `Reviewed: <sha>` line, or null. */
|
|
33
|
+
export const reviewedShaOf = (prompt) => REVIEWED_LINE.exec(String(prompt ?? ""))?.[1] ?? null;
|
|
34
|
+
|
|
35
|
+
/** The real reads of the base check, each with its own time limit. */
|
|
36
|
+
export const defaultReads = {
|
|
37
|
+
remoteTip: (cwd, remote, branch) => remoteTip(cwd, remote, branch, REMOTE_MS),
|
|
38
|
+
lookup: (cwd, ref) => commitLookup(cwd, ref, { timeoutMs: READ_MS }),
|
|
39
|
+
isAncestor: (cwd, ancestor, descendant) => isAncestor(cwd, ancestor, descendant, READ_MS),
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** The head of a checkout, or null when git cannot say within the time limit. */
|
|
43
|
+
export const headOf = (root) => gitOutWithin(READ_MS, root, "rev-parse", "HEAD");
|
|
44
|
+
|
|
45
|
+
/** The branch checked out at a checkout, or null. */
|
|
46
|
+
export const branchOfCheckout = (root) => gitOutWithin(READ_MS, root, "branch", "--show-current") || null;
|
|
47
|
+
|
|
48
|
+
/** The refusal of a reviewer whose worktree, branch, or head cannot be read: no check that needs them can run. */
|
|
49
|
+
export const unreadableRefusal = (type, why) =>
|
|
50
|
+
`Task gate: the ${type} prompt does not name a checkout the gate can read (${why}). ` +
|
|
51
|
+
"Add a line `Worktree: <path>` that names a worktree whose checked-out branch is the branch under review, then dispatch.";
|
|
52
|
+
|
|
53
|
+
function branchReviewRefusal({ records, branch, prompt }) {
|
|
54
|
+
const last = records.findLast((record) => record.type === "verdict" && record.kind === "branch" && record.branch === branch);
|
|
55
|
+
if (!last) return null;
|
|
56
|
+
const named = reviewedShaOf(prompt);
|
|
57
|
+
if (named === null) {
|
|
58
|
+
return (
|
|
59
|
+
`Task gate: ${branch} already has a branch review in the ledger (${short(last.sha)}), and a PR gets one. ` +
|
|
60
|
+
`Send the incremental review: add a line \`Reviewed: ${short(last.sha)}\`, the last head a branch review named, and pass the delta diff ${short(last.sha)}..<head> in place of the whole branch.`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
const known = records.some((record) => record.type === "verdict" && record.kind === "branch" && record.branch === branch && shaMatches(record.sha, named));
|
|
64
|
+
if (known) return null;
|
|
65
|
+
return (
|
|
66
|
+
`Task gate: \`Reviewed: ${short(named)}\` is no head a branch review of ${branch} named. ` +
|
|
67
|
+
`Write the last head a branch review named: \`Reviewed: ${short(last.sha)}\`.`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The remote and branch to ask for the tip of `base`: `origin/<branch>`, or a bare name on origin. Null for any other spelling. */
|
|
72
|
+
function remoteBranchOf(base) {
|
|
73
|
+
const named = /^origin\/(.+)$/.exec(base);
|
|
74
|
+
if (named) return named[1];
|
|
75
|
+
return base.includes("/") ? null : base;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Judges that `head` holds the tip of `base`. The tip is read from origin, or from the local ref when origin does not answer.
|
|
80
|
+
* Only a tip that is known and missing refuses; a read that fails passes with a note.
|
|
81
|
+
* @returns `{ refusal, note }`
|
|
82
|
+
*/
|
|
83
|
+
export function baseCheck({ root, base, head, reads = defaultReads }) {
|
|
84
|
+
const remoteBranch = remoteBranchOf(base);
|
|
85
|
+
let tip = remoteBranch === null ? null : reads.remoteTip(root, "origin", remoteBranch);
|
|
86
|
+
let note = null;
|
|
87
|
+
if (tip === null) {
|
|
88
|
+
const local = reads.lookup(root, base);
|
|
89
|
+
if (!local.sha) {
|
|
90
|
+
const why = local.error ?? "the ref is unknown";
|
|
91
|
+
return pass(`Task gate could not read the tip of ${base} (${why}), so the base check did not run.`);
|
|
92
|
+
}
|
|
93
|
+
tip = local.sha;
|
|
94
|
+
note = `Task gate read the tip of ${base} from local ${base}, because origin did not answer; run \`git fetch\` to make the check current.`;
|
|
95
|
+
} else {
|
|
96
|
+
const found = reads.lookup(root, tip);
|
|
97
|
+
if (found.error) return pass(`Task gate could not look up the tip of ${base} (${found.error}), so the base check did not run.`);
|
|
98
|
+
if (!found.sha) {
|
|
99
|
+
return refuse(
|
|
100
|
+
`Task gate: the tip of ${base} (${short(tip)}) is not in this checkout. Run \`git fetch\`, then merge ${base} into the branch, then dispatch the review.`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const contained = reads.isAncestor(root, tip, head);
|
|
105
|
+
if (contained === false) {
|
|
106
|
+
return refuse(
|
|
107
|
+
`Task gate: the head ${short(head)} lacks the tip of ${base} (${short(tip)}): merge ${base} into the branch first. CI tests the merge ref, so a review of a head that lacks the tip judges a different commit.`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (contained === null) return pass(`Task gate could not tell whether ${short(head)} holds the tip of ${base}, so the base check did not run.`);
|
|
111
|
+
return pass(note);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The reviewer checks, in cheap-first order: one branch review, the base tip, green CI.
|
|
116
|
+
* `ciState(sha)` is `{ reason, unknown }`; `reads` answers the base check.
|
|
117
|
+
* @returns `{ refusal, note }`
|
|
118
|
+
*/
|
|
119
|
+
export function reviewerCheck({ type, prompt, review, base, records, root, branch, head, ciState, reads = defaultReads }) {
|
|
120
|
+
if (!branch) return refuse(unreadableRefusal(type, "git read no branch there: a detached head, or git did not answer"));
|
|
121
|
+
if (!head) return refuse(unreadableRefusal(type, "git read no head there"));
|
|
122
|
+
if (review.oneBranchReview && review.reviewerTypes.branch.includes(type)) {
|
|
123
|
+
const refusal = branchReviewRefusal({ records, branch, prompt });
|
|
124
|
+
if (refusal) return refuse(refusal);
|
|
125
|
+
}
|
|
126
|
+
const notes = [];
|
|
127
|
+
if (review.requireBase) {
|
|
128
|
+
const result = baseCheck({ root, base, head, reads });
|
|
129
|
+
if (result.refusal) return result;
|
|
130
|
+
if (result.note) notes.push(result.note);
|
|
131
|
+
}
|
|
132
|
+
if (review.requireGreen) {
|
|
133
|
+
const state = ciState(head);
|
|
134
|
+
if (state.reason) {
|
|
135
|
+
return refuse(
|
|
136
|
+
`Task gate: ${state.reason}. A review waits for green CI on the head, since a red head changes before it is reviewed. Wait for the run, or fix the failure, then dispatch the review.`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
if (state.unknown) notes.push(`Task gate: CI on ${short(head)} is unknown: ${state.unknown}. The review proceeds.`);
|
|
140
|
+
}
|
|
141
|
+
return pass(notes.length > 0 ? notes.join(" ") : null);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The numbered findings of a brief: the lines that start with a number and a dot. */
|
|
145
|
+
export const countFindings = (text) => (String(text ?? "").match(FINDING_LINE) ?? []).length;
|
|
146
|
+
|
|
147
|
+
/** The distinct files a brief names: a path with a separator, or a name with a known extension. */
|
|
148
|
+
export function countFiles(text) {
|
|
149
|
+
const files = new Set();
|
|
150
|
+
for (const token of String(text ?? "").replace(URL_TEXT, " ").split(TOKEN_CHARS)) {
|
|
151
|
+
const name = token.replace(/\.+$/, "");
|
|
152
|
+
const extension = EXTENSION.exec(name)?.[1];
|
|
153
|
+
if (extension === undefined) continue;
|
|
154
|
+
if (/[\\/]/.test(name) || KNOWN_EXTENSIONS.has(extension.toLowerCase())) files.add(name.replaceAll("\\", "/"));
|
|
155
|
+
}
|
|
156
|
+
return files.size;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The path on a prompt's `Brief: <path>` line, without quotes, or null. */
|
|
160
|
+
export const briefPathOf = (prompt) => BRIEF_LINE.exec(String(prompt ?? ""))?.[1].replace(QUOTES, "") ?? null;
|
|
161
|
+
|
|
162
|
+
/** The text under a brief's `## Product decisions` heading, up to the next heading of level 1 or 2, trimmed. Null when the heading is absent. */
|
|
163
|
+
function productDecisionsOf(text) {
|
|
164
|
+
const lines = text.split(/\r?\n/);
|
|
165
|
+
const at = lines.findIndex((line) => PRODUCT_HEADING.test(line));
|
|
166
|
+
if (at < 0) return null;
|
|
167
|
+
const end = lines.findIndex((line, index) => index > at && SECTION_END.test(line));
|
|
168
|
+
return lines.slice(at + 1, end < 0 ? lines.length : end).join("\n").trim();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const BRIEF_LINE_HINT = "Write the brief to a file and name its path on a prompt line `Brief: <path>`, then dispatch.";
|
|
172
|
+
|
|
173
|
+
/** Reads the file the prompt's `Brief:` line names. @returns `{ text, source }` or `{ refusal }`. */
|
|
174
|
+
function briefOf(prompt, dirs, exists, read) {
|
|
175
|
+
const spelled = briefPathOf(prompt);
|
|
176
|
+
if (spelled === null) return { refusal: `Task gate: the implementer prompt has no \`Brief: <path>\` line. ${BRIEF_LINE_HINT}` };
|
|
177
|
+
const file = dirs.map((dir) => path.resolve(dir, nativePath(spelled))).find((candidate) => exists(candidate));
|
|
178
|
+
if (file === undefined) {
|
|
179
|
+
return { refusal: `Task gate: the \`Brief: <path>\` line names ${spelled}, and no such file exists. ${BRIEF_LINE_HINT}` };
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
return { text: read(file), source: spelled };
|
|
183
|
+
} catch (error) {
|
|
184
|
+
return { refusal: `Task gate: the \`Brief: <path>\` line names ${spelled}, and the file cannot be read (${error.message}). ${BRIEF_LINE_HINT}` };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The implementer checks on a dispatch: its `Brief:` line names a file, the file holds a `## Product decisions` section
|
|
190
|
+
* with text, and its round stays a size one implementer finishes. A missing brief or section refuses; a long brief only
|
|
191
|
+
* warns. `dirs` resolve a relative brief path. With every brief check off, the brief is not read.
|
|
192
|
+
* @returns `{ refusal, note }`
|
|
193
|
+
*/
|
|
194
|
+
export function briefCheck({ prompt, dirs, review, exists = existsSync, read = (file) => readFileSync(file, "utf8") }) {
|
|
195
|
+
if (!review.productDecisions && review.roundFindings <= 0 && review.roundFiles <= 0) return pass();
|
|
196
|
+
const brief = briefOf(prompt, dirs, exists, read);
|
|
197
|
+
if (brief.refusal) return refuse(brief.refusal);
|
|
198
|
+
const { text, source } = brief;
|
|
199
|
+
const decisions = review.productDecisions ? productDecisionsOf(text) : undefined;
|
|
200
|
+
if (decisions === null) {
|
|
201
|
+
return refuse(
|
|
202
|
+
`Task gate: the brief (${source}) has no \`## Product decisions\` heading. Ask the user the product questions first, in one question, ` +
|
|
203
|
+
"and record the answers under `## Product decisions` in the brief; write the rulings that bind the task, or `none` with the reason. Then dispatch.",
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
if (decisions === "") {
|
|
207
|
+
return refuse(
|
|
208
|
+
`Task gate: the \`## Product decisions\` section of the brief (${source}) is empty. ` +
|
|
209
|
+
"Write the rulings that bind the task under it, or `none` with the reason. Then dispatch.",
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
const findings = review.roundFindings > 0 ? countFindings(text) : 0;
|
|
213
|
+
const files = review.roundFiles > 0 ? countFiles(text) : 0;
|
|
214
|
+
const parts = [];
|
|
215
|
+
if (review.roundFindings > 0 && findings > review.roundFindings) {
|
|
216
|
+
parts.push(`${findings} numbered findings, over swarm.review.roundFindings (${review.roundFindings})`);
|
|
217
|
+
}
|
|
218
|
+
if (review.roundFiles > 0 && files > review.roundFiles) parts.push(`${files} files, over swarm.review.roundFiles (${review.roundFiles})`);
|
|
219
|
+
if (parts.length === 0) return pass();
|
|
220
|
+
return pass(
|
|
221
|
+
`Task gate: the brief (${source}) names ${parts.join(", and ")}. One implementer finishes a smaller round. ` +
|
|
222
|
+
"Split it: parallel implementers on disjoint paths, sub-branches in separate worktrees, merged by the controller.",
|
|
223
|
+
);
|
|
224
|
+
}
|