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
|
@@ -133,3 +133,31 @@ export function agentTypeOf(transcript) {
|
|
|
133
133
|
return null;
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
|
+
|
|
137
|
+
/** The number of distinct tool calls in a transcript: each `tool_use` block counts once, whatever lines repeat its message. */
|
|
138
|
+
export function toolCallsIn(file) {
|
|
139
|
+
let text;
|
|
140
|
+
try {
|
|
141
|
+
text = readFileSync(file, "utf8");
|
|
142
|
+
} catch {
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
const ids = new Set();
|
|
146
|
+
let anonymous = 0;
|
|
147
|
+
for (const line of text.split(/\r?\n/)) {
|
|
148
|
+
if (!line.includes('"tool_use"')) continue;
|
|
149
|
+
let content;
|
|
150
|
+
try {
|
|
151
|
+
content = JSON.parse(line)?.message?.content;
|
|
152
|
+
} catch {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (!Array.isArray(content)) continue;
|
|
156
|
+
for (const block of content) {
|
|
157
|
+
if (block?.type !== "tool_use") continue;
|
|
158
|
+
if (typeof block.id === "string") ids.add(block.id);
|
|
159
|
+
else anonymous += 1;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return ids.size + anonymous;
|
|
163
|
+
}
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
readRequests,
|
|
14
14
|
requestOf,
|
|
15
15
|
subagentTranscriptOf,
|
|
16
|
+
toolCallsIn,
|
|
16
17
|
} from "./transcript-usage.mjs";
|
|
17
18
|
|
|
18
19
|
function folder(t) {
|
|
@@ -136,3 +137,11 @@ test("agentTypeOf reads the type from the meta file beside a transcript", (t) =>
|
|
|
136
137
|
writeFileSync(path.join(base, "agent-a1.meta.json"), JSON.stringify({ agentType: "architecture:sdd-reviewer" }));
|
|
137
138
|
assert.equal(agentTypeOf(file), "architecture:sdd-reviewer");
|
|
138
139
|
});
|
|
140
|
+
|
|
141
|
+
test("toolCallsIn counts each tool_use id once, and a file that does not read has none", (t) => {
|
|
142
|
+
const file = path.join(folder(t), "t.jsonl");
|
|
143
|
+
const line = (ids) => JSON.stringify({ type: "assistant", message: { id: "m", content: ids.map((id) => ({ type: "tool_use", id })) } });
|
|
144
|
+
writeFileSync(file, [line(["a"]), line(["a", "b"]), line([]), "not json with tool_use", JSON.stringify({ type: "user", message: { content: "tool_use" } })].join("\n"));
|
|
145
|
+
assert.equal(toolCallsIn(file), 2);
|
|
146
|
+
assert.equal(toolCallsIn(path.join(folder(t), "missing.jsonl")), 0);
|
|
147
|
+
});
|
|
@@ -71,3 +71,41 @@ test("each workflow script is silent in a checkout without swarm.dispatch", (t)
|
|
|
71
71
|
assert.equal(result.stdout, "", script);
|
|
72
72
|
}
|
|
73
73
|
});
|
|
74
|
+
|
|
75
|
+
test("hooks.json runs the skill guard on the Skill tool, and gives the dispatch guard time for its git and gh reads", () => {
|
|
76
|
+
assert.deepEqual(matchersOf("PreToolUse", "skill-guard.mjs"), ["Skill"]);
|
|
77
|
+
const entry = hooks.PreToolUse.find((candidate) => candidate.hooks.some((hook) => hook.command.includes("/dispatch-guard.mjs\"")));
|
|
78
|
+
assert.equal(entry.matcher, "Agent");
|
|
79
|
+
assert.equal(entry.hooks.find((hook) => hook.command.includes("/dispatch-guard.mjs\"")).timeout, 30);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("the skill guard is silent without swarm.dispatch, and denies a code-review skill with it", (t) => {
|
|
83
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-skill-hooks-")));
|
|
84
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
85
|
+
execFileSync("git", ["init", "-q"], { cwd: base, stdio: "pipe" });
|
|
86
|
+
const run = (config) => {
|
|
87
|
+
writeFileSync(path.join(base, "qc.config.json"), JSON.stringify(config));
|
|
88
|
+
const event = { session_id: "s", cwd: base, hook_event_name: "PreToolUse", tool_name: "Skill", tool_input: { skill: "code-review" } };
|
|
89
|
+
return spawnSync(process.execPath, [path.join(CLI, "skill-guard.mjs")], { input: JSON.stringify(event), encoding: "utf8" });
|
|
90
|
+
};
|
|
91
|
+
assert.equal(run({ swarm: {} }).stdout, "");
|
|
92
|
+
assert.equal(JSON.parse(run({ swarm: { dispatch: {} } }).stdout).hookSpecificOutput.permissionDecision, "deny");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("cost-stop.mjs runs on every SubagentStop, and is silent in a checkout without swarm.dispatch", (t) => {
|
|
96
|
+
assert.deepEqual(matchersOf("SubagentStop", "cost-stop.mjs"), [undefined]);
|
|
97
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-cost-hooks-")));
|
|
98
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
99
|
+
execFileSync("git", ["init", "-q"], { cwd: base, stdio: "pipe" });
|
|
100
|
+
writeFileSync(path.join(base, "qc.config.json"), JSON.stringify({ swarm: {} }));
|
|
101
|
+
const transcript = path.join(base, "agent.jsonl");
|
|
102
|
+
writeFileSync(path.join(base, "agent.jsonl"), `${JSON.stringify({ type: "assistant", message: { id: "m", model: "m", usage: { input_tokens: 1, output_tokens: 1 } } })}\n`);
|
|
103
|
+
const result = spawnSync(process.execPath, [path.join(CLI, "cost-stop.mjs")], {
|
|
104
|
+
input: JSON.stringify({ session_id: "s", cwd: base, hook_event_name: "SubagentStop", agent_id: "a", agent_type: "sdd-implementer", agent_transcript_path: transcript }),
|
|
105
|
+
env: { ...process.env, TEMP: base, TMP: base, TMPDIR: base },
|
|
106
|
+
encoding: "utf8",
|
|
107
|
+
});
|
|
108
|
+
assert.equal(result.status, 0, result.stderr);
|
|
109
|
+
assert.equal(result.stdout, "");
|
|
110
|
+
assert.equal(existsSync(path.join(base, ".git", "qc", "ledger.jsonl")), false);
|
|
111
|
+
});
|
|
@@ -12,7 +12,9 @@ import { ledgerFileOf } from "./ledger.mjs";
|
|
|
12
12
|
|
|
13
13
|
const KEY = "swarm.review";
|
|
14
14
|
const MERGE_KINDS = ["branch", "task"];
|
|
15
|
-
const
|
|
15
|
+
const SWITCH_KEYS = ["oneBranchReview", "requireGreen", "requireBase", "productDecisions"];
|
|
16
|
+
const LIMIT_KEYS = ["roundFindings", "roundFiles"];
|
|
17
|
+
const REVIEW_KEYS = ["merge", "maxTaskCalls", "controllerPaths", "reviewerTypes", "plannerTypes", "codeReviewSkills", ...SWITCH_KEYS, ...LIMIT_KEYS];
|
|
16
18
|
const STATE_FILE = "state.json";
|
|
17
19
|
|
|
18
20
|
const isNameList = (value) => Array.isArray(value) && value.every((name) => typeof name === "string" && name !== "");
|
|
@@ -28,7 +30,8 @@ export function reviewSettings(swarm = {}) {
|
|
|
28
30
|
}
|
|
29
31
|
const unknown = Object.keys(raw).find((key) => !REVIEW_KEYS.includes(key));
|
|
30
32
|
if (unknown !== undefined) throw new Error(`${KEY}.${unknown} in qc.config.json is not a review setting; the keys are ${REVIEW_KEYS.join(", ")}`);
|
|
31
|
-
const
|
|
33
|
+
const merged = merge(reviewDefaults, raw);
|
|
34
|
+
const { merge: mergeKind, maxTaskCalls, controllerPaths, reviewerTypes, plannerTypes, codeReviewSkills } = merged;
|
|
32
35
|
if (!MERGE_KINDS.includes(mergeKind)) throw wrong("merge", mergeKind, `one of ${MERGE_KINDS.join(", ")}`);
|
|
33
36
|
if (!Number.isInteger(maxTaskCalls) || maxTaskCalls < 1) throw wrong("maxTaskCalls", maxTaskCalls, "a positive whole number");
|
|
34
37
|
if (!isNameList(controllerPaths)) throw wrong("controllerPaths", controllerPaths, "a list of globs");
|
|
@@ -36,7 +39,15 @@ export function reviewSettings(swarm = {}) {
|
|
|
36
39
|
if (!isNameList(reviewerTypes?.[kind])) throw wrong(`reviewerTypes.${kind}`, reviewerTypes?.[kind], "a list of agent types");
|
|
37
40
|
}
|
|
38
41
|
if (!isNameList(plannerTypes)) throw wrong("plannerTypes", plannerTypes, "a list of agent types");
|
|
39
|
-
|
|
42
|
+
if (!isNameList(codeReviewSkills)) throw wrong("codeReviewSkills", codeReviewSkills, "a list of skill names");
|
|
43
|
+
for (const key of SWITCH_KEYS) {
|
|
44
|
+
if (typeof merged[key] !== "boolean") throw wrong(key, merged[key], "true or false");
|
|
45
|
+
}
|
|
46
|
+
for (const key of LIMIT_KEYS) {
|
|
47
|
+
if (!Number.isInteger(merged[key]) || merged[key] < 0) throw wrong(key, merged[key], "a whole number, 0 or more");
|
|
48
|
+
}
|
|
49
|
+
const enforcement = Object.fromEntries([...SWITCH_KEYS, ...LIMIT_KEYS].map((key) => [key, merged[key]]));
|
|
50
|
+
return { merge: mergeKind, maxTaskCalls, controllerPaths, reviewerTypes, plannerTypes, codeReviewSkills, ...enforcement, implementerTypes: dispatch.implementerTypes };
|
|
40
51
|
}
|
|
41
52
|
|
|
42
53
|
/** @returns the workflow of the checkout that holds `cwd`, or null when it has no config or the checks are off. */
|
|
@@ -53,6 +53,14 @@ test("a bad key throws and names it", () => {
|
|
|
53
53
|
["on", /swarm\.review in qc\.config\.json must be an object/],
|
|
54
54
|
[{ mergeKind: "task" }, /swarm\.review\.mergeKind/],
|
|
55
55
|
[{ merge: "task", maxTaskCall: 5 }, /swarm\.review\.maxTaskCall/],
|
|
56
|
+
[{ oneBranchReview: "yes" }, /swarm\.review\.oneBranchReview/],
|
|
57
|
+
[{ requireGreen: 1 }, /swarm\.review\.requireGreen/],
|
|
58
|
+
[{ requireBase: null }, /swarm\.review\.requireBase/],
|
|
59
|
+
[{ productDecisions: "on" }, /swarm\.review\.productDecisions/],
|
|
60
|
+
[{ codeReviewSkills: "code-review" }, /swarm\.review\.codeReviewSkills/],
|
|
61
|
+
[{ codeReviewSkills: [""] }, /swarm\.review\.codeReviewSkills/],
|
|
62
|
+
[{ roundFindings: -1 }, /swarm\.review\.roundFindings/],
|
|
63
|
+
[{ roundFiles: 1.5 }, /swarm\.review\.roundFiles/],
|
|
56
64
|
]) {
|
|
57
65
|
assert.throws(() => reviewSettings({ dispatch: {}, review }), key);
|
|
58
66
|
}
|
|
@@ -77,3 +85,19 @@ test("a hook whose cwd holds no config reads the checkout its session remembered
|
|
|
77
85
|
assert.equal(workflowOf(call, tmp).root, dirs[0]);
|
|
78
86
|
assert.equal(workflowOf({ session_id: "s-2", cwd: outside }, tmp), null);
|
|
79
87
|
});
|
|
88
|
+
|
|
89
|
+
test("the enforcement keys default on, and a repository switches each one off", () => {
|
|
90
|
+
const settings = reviewSettings({ dispatch: {} });
|
|
91
|
+
assert.deepEqual(
|
|
92
|
+
[settings.oneBranchReview, settings.codeReviewSkills, settings.requireGreen, settings.requireBase, settings.productDecisions, settings.roundFindings, settings.roundFiles],
|
|
93
|
+
[true, ["code-review"], true, true, true, 8, 10],
|
|
94
|
+
);
|
|
95
|
+
const off = reviewSettings({
|
|
96
|
+
dispatch: {},
|
|
97
|
+
review: { oneBranchReview: false, codeReviewSkills: [], requireGreen: false, requireBase: false, productDecisions: false, roundFindings: 0, roundFiles: 0 },
|
|
98
|
+
});
|
|
99
|
+
assert.deepEqual(
|
|
100
|
+
[off.oneBranchReview, off.codeReviewSkills, off.requireGreen, off.requireBase, off.productDecisions, off.roundFindings, off.roundFiles],
|
|
101
|
+
[false, [], false, false, false, 0, 0],
|
|
102
|
+
);
|
|
103
|
+
});
|
package/src/config.mjs
CHANGED
|
@@ -559,6 +559,19 @@ export const reviewDefaults = {
|
|
|
559
559
|
},
|
|
560
560
|
// The agent types whose stop runs the plan check.
|
|
561
561
|
plannerTypes: ["sdd-planner", "architecture:sdd-planner"],
|
|
562
|
+
// Refuse a branch-reviewer dispatch with no `Reviewed: <sha>` line once the ledger holds a branch verdict on the branch.
|
|
563
|
+
oneBranchReview: true,
|
|
564
|
+
// Skill names, bare or after a `plugin:` prefix, that the model may not call. `[]` turns the check off.
|
|
565
|
+
codeReviewSkills: ["code-review"],
|
|
566
|
+
// Refuse a reviewer dispatch while CI on the head is red or running.
|
|
567
|
+
requireGreen: true,
|
|
568
|
+
// Refuse a reviewer dispatch when the head lacks the tip of `worktree.base`.
|
|
569
|
+
requireBase: true,
|
|
570
|
+
// Refuse an implementer dispatch whose brief has no `## Product decisions` heading.
|
|
571
|
+
productDecisions: true,
|
|
572
|
+
// Warn on an implementer brief with more numbered findings, or more named files. `0` turns a warning off.
|
|
573
|
+
roundFindings: 8,
|
|
574
|
+
roundFiles: 10,
|
|
562
575
|
};
|
|
563
576
|
|
|
564
577
|
// What each key of `swarm.direct` means when a repository omits it. A fix inside these limits skips the
|
|
@@ -65,12 +65,20 @@ recorded.
|
|
|
65
65
|
|---|---|
|
|
66
66
|
| A review states its verdict and the sha it reviewed on its first line | `report-stop.mjs`, on `SubagentHandback` and `SubagentStop` |
|
|
67
67
|
| An implementer reports the failing test before the passing one | `report-stop.mjs` |
|
|
68
|
-
| A task is reviewed before the next implementer starts on its branch | `dispatch-guard.mjs`, through `task-gate.mjs` |
|
|
68
|
+
| A task is reviewed before the next implementer starts on its branch, unless CI on the stop head is not green: the review runs once CI is green | `dispatch-guard.mjs`, through `task-gate.mjs` |
|
|
69
69
|
| A fix round goes back to the implementer that wrote the task | `dispatch-guard.mjs`, through `task-gate.mjs` |
|
|
70
70
|
| A merge pins the head that an APPROVED review named, or a head inside the direct lane, whose limits come from the commit the PR merges into | `merge-guard.mjs`, through `direct-lane.mjs` |
|
|
71
71
|
| An issue that a merge names is closed, or has a comment after the merge | `issue-gate.mjs`, on `Stop` |
|
|
72
72
|
| The controller edits only its own paths, or a small fix inside the direct lane, whose limits come from the base | `controller-guard.mjs`, through `direct-lane.mjs` |
|
|
73
73
|
| A plan task names its files, a test step, and a commit step, within the budget | `qc plan-check`, and `plan-stop.mjs` |
|
|
74
|
+
| A branch is reviewed whole once; a later review names `Reviewed: <sha>`, the last head a branch review named, and reads the increment (`swarm.review.oneBranchReview`) | `dispatch-guard.mjs`, on `Agent` |
|
|
75
|
+
| A model never calls a code-review skill: dispatch `sdd-branch-reviewer`, or ask the user to type `/code-review` (`codeReviewSkills`) | `skill-guard.mjs`, on `Skill` |
|
|
76
|
+
| A review runs on a green head: CI on the head is not red or running (`requireGreen`) | `dispatch-guard.mjs`, on `Agent` |
|
|
77
|
+
| A review runs on a head that holds the tip of `worktree.base`: merge the base first (`requireBase`) | `dispatch-guard.mjs`, on `Agent` |
|
|
78
|
+
| An implementer prompt names its brief on a `Brief: <path>` line, and the brief holds a `## Product decisions` section with the user's rulings, or `none` (`productDecisions`). A reviewer prompt names a `Worktree: <path>` whose branch and head the gate can read | `dispatch-guard.mjs`, on `Agent` |
|
|
79
|
+
| An implementer brief stays within `roundFindings` findings and `roundFiles` files; a larger round splits in parallel on disjoint paths (a warning) | `dispatch-guard.mjs`, on `Agent` |
|
|
80
|
+
| Each subagent stop records its tokens and tool calls by role; `qc ledger --cost [branch]` totals them | `cost-stop.mjs`, on `SubagentStop` |
|
|
81
|
+
| A typecheck that fails only with `TS2307` runs once more before the stop reports red | `hooks/stop-gate.sh`, on `Stop` |
|
|
74
82
|
|
|
75
83
|
## Worktrees — QC-015
|
|
76
84
|
|
|
@@ -79,6 +87,9 @@ These hooks are on wherever `qc.config.json` is. `"worktree": { "enforce": false
|
|
|
79
87
|
| Rule | Check |
|
|
80
88
|
|---|---|
|
|
81
89
|
| A worktree is one folder directly under `worktree.dir` of the main checkout | `bash-command-guard.mjs`, on `PreToolUse` of `Bash` and `PowerShell` |
|
|
90
|
+
| `git worktree add` runs where the guard can name the repository: no `GIT_DIR`, `GIT_COMMON_DIR`, `GIT_WORK_TREE`, `core.worktree`, or `core.bare` anywhere in the command; use `-C`, `cd`, or `Set-Location` | `bash-command-guard.mjs`, on `PreToolUse` of `Bash` and `PowerShell` |
|
|
91
|
+
| A `-C`, `cd`, or `Set-Location` path before `git worktree add` is a literal path, with no variable, command substitution, or subexpression: write the path literally | `bash-command-guard.mjs`, on `PreToolUse` of `Bash` and `PowerShell` |
|
|
92
|
+
| A shell write names its checkout: a `--work-tree` directory counts, and a `--git-dir` with no work tree is reported as unresolved (a warning) | `bash-edit-guard.mjs`, on `PostToolUse` of `Bash` and `PowerShell` |
|
|
82
93
|
| A pull request this session merged leaves no worktree, local branch, or remote branch on its head | `worktree-gate.mjs`, on `Stop`, from the merge record of `merge-guard.mjs` |
|
|
83
94
|
| A squash-merged branch is deleted | `qc worktree remove`, on a merged pull request whose head is the branch at its tip |
|
|
84
95
|
|