architecture-harness 0.12.0 → 0.14.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 +14 -4
- package/src/cli/bash-command-guard.mjs +25 -8
- package/src/cli/bash-command-guard.test.mjs +44 -0
- package/src/cli/branch-name.test.mjs +31 -0
- package/src/cli/check.mjs +5 -0
- package/src/cli/commit-msg.mjs +94 -0
- package/src/cli/commit-msg.test.mjs +60 -0
- package/src/cli/dispatch-guard.mjs +78 -0
- package/src/cli/dispatch-guard.test.mjs +160 -0
- package/src/cli/dispatch-reminder.mjs +43 -0
- package/src/cli/dispatch-reminder.test.mjs +78 -0
- package/src/cli/dispatch-slot.mjs +70 -0
- package/src/cli/dispatch-slot.test.mjs +80 -0
- package/src/cli/dispatch.mjs +84 -0
- package/src/cli/dispatch.test.mjs +90 -0
- package/src/cli/doctor.mjs +39 -5
- package/src/cli/doctor.test.mjs +18 -1
- package/src/cli/edit-batch-state.mjs +5 -2
- package/src/cli/git-hooks.mjs +67 -0
- package/src/cli/git-hooks.test.mjs +85 -0
- package/src/cli/plugin-agents.test.mjs +54 -0
- package/src/cli/post-edit-gate.test.mjs +10 -0
- package/src/cli/powershell-command.mjs +3 -0
- package/src/cli/pr-check.mjs +74 -0
- package/src/cli/pr-check.test.mjs +59 -0
- package/src/cli/qc.mjs +8 -6
- package/src/cli/stop-gate.test.mjs +113 -0
- package/src/cli/templates.test.mjs +9 -0
- package/src/cli/work-order-guard.mjs +25 -1
- package/src/config.mjs +31 -7
- package/src/gates/config-floor.mjs +23 -8
- package/src/gates/config-floor.test.mjs +10 -0
- package/src/gates/english-source.mjs +1 -1
- package/templates/githooks/commit-msg +2 -1
- package/templates/github/workflows/ci.yml +7 -0
- package/src/cli/commit-attribution.mjs +0 -68
- package/src/cli/commit-attribution.test.mjs +0 -47
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The SessionStart trigger for `swarm.dispatch`: one short note at each start, resume, clear, compaction
|
|
3
|
+
// and fork, because a rule held only in the conversation is lost to a compaction.
|
|
4
|
+
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { dispatchSettingsAt } from "./dispatch.mjs";
|
|
7
|
+
|
|
8
|
+
export const REMINDER =
|
|
9
|
+
"Subagent workflow (swarm.dispatch in qc.config.json): run a plan with superpowers:subagent-driven-development. " +
|
|
10
|
+
"Dispatch architecture:sdd-planner to write a plan, architecture:sdd-implementer for one task, and " +
|
|
11
|
+
"architecture:sdd-reviewer to review it. Run one implementer at a time: a hook refuses a second while one runs. " +
|
|
12
|
+
"Name a model on any other dispatch, and pass each brief as a file path.";
|
|
13
|
+
|
|
14
|
+
const context = (text) => ({ hookSpecificOutput: { hookEventName: "SessionStart", additionalContext: text } });
|
|
15
|
+
|
|
16
|
+
/** @returns the hook output, or null when the checkout does not turn the section on. */
|
|
17
|
+
export function decide(call) {
|
|
18
|
+
let settings;
|
|
19
|
+
try {
|
|
20
|
+
settings = dispatchSettingsAt(call.cwd ?? process.cwd());
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return context(`Dispatch guard is off: ${error.message}`);
|
|
23
|
+
}
|
|
24
|
+
return settings ? context(REMINDER) : null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function readStdin() {
|
|
28
|
+
let data = "";
|
|
29
|
+
for await (const chunk of process.stdin) data += chunk;
|
|
30
|
+
return data;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const isEntryPoint = process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1];
|
|
34
|
+
if (isEntryPoint) {
|
|
35
|
+
let call = null;
|
|
36
|
+
try {
|
|
37
|
+
call = JSON.parse(await readStdin());
|
|
38
|
+
} catch {
|
|
39
|
+
// Not hook input, so there is no checkout to read.
|
|
40
|
+
}
|
|
41
|
+
const result = call ? decide(call) : null;
|
|
42
|
+
if (result) process.stdout.write(JSON.stringify(result));
|
|
43
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
3
|
+
import { mkdirSync, mkdtempSync, readFileSync, 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 { REMINDER, decide } from "./dispatch-reminder.mjs";
|
|
9
|
+
|
|
10
|
+
const NOTE = fileURLToPath(new URL("./dispatch-reminder.mjs", import.meta.url));
|
|
11
|
+
const HOOKS = fileURLToPath(new URL("../../../../hooks/hooks.json", import.meta.url));
|
|
12
|
+
|
|
13
|
+
/** Three checkouts: one per config, and `null` writes no config at all. */
|
|
14
|
+
function checkouts(t, configs) {
|
|
15
|
+
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-dispatch-note-")));
|
|
16
|
+
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
17
|
+
return configs.map((config, index) => {
|
|
18
|
+
const dir = path.join(base, String(index));
|
|
19
|
+
mkdirSync(dir);
|
|
20
|
+
execFileSync("git", ["init", "-q"], { cwd: dir, stdio: "pipe" });
|
|
21
|
+
if (config !== null) writeFileSync(path.join(dir, "qc.config.json"), JSON.stringify(config));
|
|
22
|
+
return dir;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const start = (cwd, source) => ({ session_id: "s", cwd, hook_event_name: "SessionStart", source });
|
|
27
|
+
|
|
28
|
+
test("a checkout with no config, or no dispatch section, gets no note", (t) => {
|
|
29
|
+
const [plain, off] = checkouts(t, [null, { swarm: { toolCallBudget: 50 } }]);
|
|
30
|
+
assert.equal(decide(start(plain, "startup")), null);
|
|
31
|
+
assert.equal(decide(start(off, "startup")), null);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("with the section on, every start adds the note, a compaction included", (t) => {
|
|
35
|
+
const [on] = checkouts(t, [{ swarm: { dispatch: {} } }]);
|
|
36
|
+
for (const source of ["startup", "resume", "clear", "compact", "fork"]) {
|
|
37
|
+
const output = decide(start(on, source));
|
|
38
|
+
assert.equal(output.hookSpecificOutput.hookEventName, "SessionStart", source);
|
|
39
|
+
assert.equal(output.hookSpecificOutput.additionalContext, REMINDER, source);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("the note names the workflow, the three types, and the one-implementer rule, in a short text", () => {
|
|
44
|
+
for (const word of [
|
|
45
|
+
"superpowers:subagent-driven-development",
|
|
46
|
+
"architecture:sdd-planner",
|
|
47
|
+
"architecture:sdd-implementer",
|
|
48
|
+
"architecture:sdd-reviewer",
|
|
49
|
+
"one implementer at a time",
|
|
50
|
+
]) {
|
|
51
|
+
assert.ok(REMINDER.includes(word), word);
|
|
52
|
+
}
|
|
53
|
+
assert.ok(REMINDER.length < 600, `${REMINDER.length} characters`);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("a config error reaches the session as context", (t) => {
|
|
57
|
+
const [bad] = checkouts(t, [{ swarm: { dispatch: { maxPromptChars: "long" } } }]);
|
|
58
|
+
assert.match(decide(start(bad, "startup")).hookSpecificOutput.additionalContext, /Dispatch guard is off/);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("the hook runs as its own process and prints the note as JSON, or nothing with no config", (t) => {
|
|
62
|
+
const [on, plain] = checkouts(t, [{ swarm: { dispatch: {} } }, null]);
|
|
63
|
+
const run = (cwd) => spawnSync(process.execPath, [NOTE], { input: JSON.stringify(start(cwd, "startup")), encoding: "utf8" });
|
|
64
|
+
const noted = run(on);
|
|
65
|
+
assert.equal(noted.status, 0, noted.stderr);
|
|
66
|
+
assert.equal(JSON.parse(noted.stdout).hookSpecificOutput.additionalContext, REMINDER);
|
|
67
|
+
const silent = run(plain);
|
|
68
|
+
assert.equal(silent.status, 0, silent.stderr);
|
|
69
|
+
assert.equal(silent.stdout, "");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("hooks.json runs the note on every SessionStart source", () => {
|
|
73
|
+
const { hooks } = JSON.parse(readFileSync(HOOKS, "utf8"));
|
|
74
|
+
const entries = (hooks.SessionStart ?? []).filter((entry) =>
|
|
75
|
+
entry.hooks.some((hook) => hook.command.includes("dispatch-reminder.mjs")),
|
|
76
|
+
);
|
|
77
|
+
assert.deepEqual(entries.map((entry) => entry.matcher), [undefined]);
|
|
78
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// The resource for the implementer slot: one folder per session under the OS temp folder, with one
|
|
2
|
+
// claim file and the settings of the claim. A stop reads those settings, since its cwd can hold no config.
|
|
3
|
+
|
|
4
|
+
import { readFileSync, statSync, writeFileSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { claimFileOf, claimPath, fileSafe, releasePath } from "./edit-batch-state.mjs";
|
|
7
|
+
|
|
8
|
+
const KEY = "implementer";
|
|
9
|
+
const SETTINGS_FILE = "settings.json";
|
|
10
|
+
const MINUTE_MS = 60_000;
|
|
11
|
+
|
|
12
|
+
/** The slot folder of one session. */
|
|
13
|
+
export const slotDirOf = (sessionId, tmp) => path.join(tmp, "architecture-kit", "dispatch", fileSafe(sessionId));
|
|
14
|
+
|
|
15
|
+
/** The claim file of the slot in one session folder. */
|
|
16
|
+
export const slotFileOf = (dir) => claimFileOf(dir, KEY);
|
|
17
|
+
|
|
18
|
+
function recordedSettings(dir) {
|
|
19
|
+
try {
|
|
20
|
+
const { implementerTypes, slotMinutes } = JSON.parse(readFileSync(path.join(dir, SETTINGS_FILE), "utf8"));
|
|
21
|
+
return Array.isArray(implementerTypes) && slotMinutes > 0 ? { implementerTypes, slotMinutes } : null;
|
|
22
|
+
} catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Claims the slot through an exclusive create, so two dispatches in one block cannot both win.
|
|
29
|
+
* @returns null when this call holds the slot, or the holder's times and file; throws on a file system error
|
|
30
|
+
*/
|
|
31
|
+
export function claimSlot(dir, settings, now) {
|
|
32
|
+
const ttlMs = settings.slotMinutes * MINUTE_MS;
|
|
33
|
+
const slotFile = slotFileOf(dir);
|
|
34
|
+
for (;;) {
|
|
35
|
+
if (claimPath(dir, KEY, now, ttlMs)) {
|
|
36
|
+
const { implementerTypes, slotMinutes } = settings;
|
|
37
|
+
try {
|
|
38
|
+
writeFileSync(path.join(dir, SETTINGS_FILE), JSON.stringify({ implementerTypes, slotMinutes }));
|
|
39
|
+
} catch (error) {
|
|
40
|
+
// A claim with no settings is freed by no stop, so it would hold the slot until it expires.
|
|
41
|
+
releasePath(dir, KEY);
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
let claimedMs;
|
|
47
|
+
try {
|
|
48
|
+
claimedMs = statSync(slotFile).mtimeMs;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
// The holder stopped between the claim and this read, so the slot is free again.
|
|
51
|
+
if (error.code === "ENOENT") continue;
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
return { claimedAt: new Date(claimedMs), expiresAt: new Date(claimedMs + ttlMs), slotFile };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Claims a free slot for an implementer that starts, as a resumed one does. A start cannot refuse, so it never waits. */
|
|
59
|
+
export function reclaimSlot(dir, agentType, now) {
|
|
60
|
+
const settings = recordedSettings(dir);
|
|
61
|
+
if (settings?.implementerTypes.includes(agentType)) claimPath(dir, KEY, now, settings.slotMinutes * MINUTE_MS);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Frees the slot when an implementer stops. @returns true when the stopped agent is an implementer */
|
|
65
|
+
export function releaseSlot(dir, agentType) {
|
|
66
|
+
const settings = recordedSettings(dir);
|
|
67
|
+
if (!settings?.implementerTypes.includes(agentType)) return false;
|
|
68
|
+
releasePath(dir, KEY);
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { dispatchDefaults } from "../config.mjs";
|
|
7
|
+
import { claimSlot, reclaimSlot, releaseSlot, slotDirOf, slotFileOf } from "./dispatch-slot.mjs";
|
|
8
|
+
|
|
9
|
+
function sessionDir(t) {
|
|
10
|
+
const tmp = mkdtempSync(path.join(os.tmpdir(), "qc-dispatch-slot-"));
|
|
11
|
+
t.after(() => rmSync(tmp, { recursive: true, force: true }));
|
|
12
|
+
return slotDirOf("s", tmp);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const settings = { ...dispatchDefaults, slotMinutes: 1 };
|
|
16
|
+
|
|
17
|
+
test("the slot folder is keyed on the session, with a file-safe name", () => {
|
|
18
|
+
assert.equal(slotDirOf("sess/1:a", "/tmp"), path.join("/tmp", "architecture-kit", "dispatch", "sess_1_a"));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("the first claim holds the slot, and a second claim with no stop between gets the holder", (t) => {
|
|
22
|
+
const dir = sessionDir(t);
|
|
23
|
+
assert.equal(claimSlot(dir, settings, Date.now()), null);
|
|
24
|
+
const holder = claimSlot(dir, settings, Date.now());
|
|
25
|
+
assert.equal(holder.slotFile, slotFileOf(dir));
|
|
26
|
+
assert.equal(holder.expiresAt.getTime() - holder.claimedAt.getTime(), 60_000);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("a claim older than slotMinutes is taken over", (t) => {
|
|
30
|
+
const dir = sessionDir(t);
|
|
31
|
+
claimSlot(dir, settings, Date.now());
|
|
32
|
+
const old = new Date(Date.now() - 120_000);
|
|
33
|
+
utimesSync(slotFileOf(dir), old, old);
|
|
34
|
+
assert.equal(claimSlot(dir, settings, Date.now()), null);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("only an implementer's stop frees the slot, and an empty agent type frees nothing", (t) => {
|
|
38
|
+
const dir = sessionDir(t);
|
|
39
|
+
claimSlot(dir, settings, Date.now());
|
|
40
|
+
assert.equal(releaseSlot(dir, ""), false);
|
|
41
|
+
assert.equal(releaseSlot(dir, "architecture:sdd-reviewer"), false);
|
|
42
|
+
assert.equal(existsSync(slotFileOf(dir)), true);
|
|
43
|
+
assert.equal(releaseSlot(dir, "architecture:sdd-implementer"), true);
|
|
44
|
+
assert.equal(existsSync(slotFileOf(dir)), false);
|
|
45
|
+
assert.equal(claimSlot(dir, settings, Date.now()), null);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("the stop reads the list recorded at the claim", (t) => {
|
|
49
|
+
const dir = sessionDir(t);
|
|
50
|
+
claimSlot(dir, { ...settings, implementerTypes: ["builder"] }, Date.now());
|
|
51
|
+
assert.equal(releaseSlot(dir, "sdd-implementer"), false);
|
|
52
|
+
assert.equal(releaseSlot(dir, "builder"), true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("a resumed implementer claims a free slot again, and a reviewer does not", (t) => {
|
|
56
|
+
const dir = sessionDir(t);
|
|
57
|
+
claimSlot(dir, settings, Date.now());
|
|
58
|
+
releaseSlot(dir, "sdd-implementer");
|
|
59
|
+
reclaimSlot(dir, "architecture:sdd-reviewer", Date.now());
|
|
60
|
+
assert.equal(existsSync(slotFileOf(dir)), false);
|
|
61
|
+
reclaimSlot(dir, "sdd-implementer", Date.now());
|
|
62
|
+
assert.notEqual(claimSlot(dir, settings, Date.now()), null);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("a session folder with no settings, or settings that do not parse, changes nothing", (t) => {
|
|
66
|
+
const dir = sessionDir(t);
|
|
67
|
+
mkdirSync(dir, { recursive: true });
|
|
68
|
+
assert.equal(releaseSlot(dir, "sdd-implementer"), false);
|
|
69
|
+
writeFileSync(path.join(dir, "settings.json"), "not json");
|
|
70
|
+
assert.equal(releaseSlot(dir, "sdd-implementer"), false);
|
|
71
|
+
reclaimSlot(dir, "sdd-implementer", Date.now());
|
|
72
|
+
assert.equal(existsSync(slotFileOf(dir)), false);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("a claim whose settings cannot be written is released, and the error reaches the caller", (t) => {
|
|
76
|
+
const dir = sessionDir(t);
|
|
77
|
+
mkdirSync(path.join(dir, "settings.json"), { recursive: true });
|
|
78
|
+
assert.throws(() => claimSlot(dir, settings, Date.now()));
|
|
79
|
+
assert.equal(existsSync(slotFileOf(dir)), false);
|
|
80
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// The branch that judges one Agent dispatch against `swarm.dispatch`: a named model or an allowed
|
|
2
|
+
// type, and a prompt short enough to point at a brief file. The implementer slot is dispatch-slot's.
|
|
3
|
+
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { CONFIG_FILE, dispatchDefaults, load, merge } from "../config.mjs";
|
|
7
|
+
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
8
|
+
|
|
9
|
+
const KEY = "swarm.dispatch";
|
|
10
|
+
const DEFAULT_TYPE = "general-purpose";
|
|
11
|
+
const FORK = "fork";
|
|
12
|
+
|
|
13
|
+
const isNameList = (value) => Array.isArray(value) && value.every((name) => typeof name === "string" && name !== "");
|
|
14
|
+
const wrong = (key, value, want) =>
|
|
15
|
+
new Error(`${KEY}.${key} in qc.config.json must be ${want}, got ${JSON.stringify(value)}`);
|
|
16
|
+
|
|
17
|
+
/** @returns the dispatch settings, or null when the section is absent; throws naming the key a repository got wrong. */
|
|
18
|
+
export function dispatchSettings(swarm = {}) {
|
|
19
|
+
const raw = swarm.dispatch;
|
|
20
|
+
if (raw === undefined || raw === null || raw === false) return null;
|
|
21
|
+
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
22
|
+
throw new Error(`${KEY} in qc.config.json must be an object, got ${JSON.stringify(raw)}`);
|
|
23
|
+
}
|
|
24
|
+
const { allowedTypes, implementerTypes, maxPromptChars, slotMinutes } = merge(dispatchDefaults, raw);
|
|
25
|
+
if (!isNameList(allowedTypes)) throw wrong("allowedTypes", allowedTypes, "a list of agent types");
|
|
26
|
+
if (!isNameList(implementerTypes)) throw wrong("implementerTypes", implementerTypes, "a list of agent types");
|
|
27
|
+
if (!Number.isInteger(maxPromptChars) || maxPromptChars < 1) {
|
|
28
|
+
throw wrong("maxPromptChars", maxPromptChars, "a positive whole number");
|
|
29
|
+
}
|
|
30
|
+
if (typeof slotMinutes !== "number" || !(slotMinutes > 0)) throw wrong("slotMinutes", slotMinutes, "a positive number");
|
|
31
|
+
return { allowedTypes, implementerTypes, maxPromptChars, slotMinutes };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @returns the settings of the checkout that holds `cwd`, or null when it has no config or no section. */
|
|
35
|
+
export function dispatchSettingsAt(cwd) {
|
|
36
|
+
const root = checkoutRootOf(cwd);
|
|
37
|
+
if (!root || !existsSync(path.join(root, CONFIG_FILE))) return null;
|
|
38
|
+
return dispatchSettings(load(root).swarm);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The type a dispatch names, trimmed. The Agent tool runs general-purpose when it names none. */
|
|
42
|
+
export const typeOf = (input) =>
|
|
43
|
+
typeof input.subagent_type === "string" && input.subagent_type.trim() !== "" ? input.subagent_type.trim() : DEFAULT_TYPE;
|
|
44
|
+
|
|
45
|
+
export const isImplementer = (type, settings) => settings.implementerTypes.includes(type);
|
|
46
|
+
|
|
47
|
+
function modelRefusal(type, settings) {
|
|
48
|
+
const allowed = settings.allowedTypes.join(", ");
|
|
49
|
+
if (type === FORK) {
|
|
50
|
+
return (
|
|
51
|
+
"Dispatch guard: a fork runs on the session's model whatever `model` says, so it passes only when " +
|
|
52
|
+
`\`fork\` is in ${KEY}.allowedTypes in qc.config.json. Dispatch one of: ${allowed}, or another type with a model.`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
return (
|
|
56
|
+
`Dispatch guard: the dispatch names no model, so the ${type} agent runs on the session's model. ` +
|
|
57
|
+
`Pass \`model\` with the cheapest tier that fits the task, or dispatch one of: ${allowed}. ` +
|
|
58
|
+
`To allow another type with no model, add it to ${KEY}.allowedTypes in qc.config.json.`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const promptRefusal = (length, max) =>
|
|
63
|
+
`Dispatch guard: the prompt has ${length} characters, over ${KEY}.maxPromptChars (${max}) in qc.config.json. ` +
|
|
64
|
+
"Write the brief to a file, and send a short prompt that names the brief and the report file.";
|
|
65
|
+
|
|
66
|
+
/** @returns the reason the dispatch breaks a rule that needs no state, or null to let it through. */
|
|
67
|
+
export function dispatchRefusal(input, settings) {
|
|
68
|
+
const type = typeOf(input);
|
|
69
|
+
const model = typeof input.model === "string" ? input.model.trim() : "";
|
|
70
|
+
const allowed = settings.allowedTypes.includes(type);
|
|
71
|
+
if (!allowed && (type === FORK || model === "")) return modelRefusal(type, settings);
|
|
72
|
+
const length = typeof input.prompt === "string" ? input.prompt.length : 0;
|
|
73
|
+
if (length > settings.maxPromptChars) return promptRefusal(length, settings.maxPromptChars);
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The reason a second implementer waits, with the two ways the slot frees. */
|
|
78
|
+
export function slotRefusal({ type, claimedAt, expiresAt, slotFile }) {
|
|
79
|
+
return (
|
|
80
|
+
`Dispatch guard: an implementer holds the one implementer slot of this session since ${claimedAt.toISOString()}. ` +
|
|
81
|
+
`Wait for it to stop, then dispatch ${type}. The slot frees when it stops, or at ${expiresAt.toISOString()}. ` +
|
|
82
|
+
`If no implementer runs, because its dispatch was denied after this hook, delete ${slotFile}.`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { defaults, dispatchDefaults, merge } from "../config.mjs";
|
|
4
|
+
import { dispatchRefusal, dispatchSettings, isImplementer, slotRefusal, typeOf } from "./dispatch.mjs";
|
|
5
|
+
|
|
6
|
+
test("a swarm section with no dispatch key keeps the guard off", () => {
|
|
7
|
+
assert.equal(dispatchSettings({}), null);
|
|
8
|
+
assert.equal(dispatchSettings(merge(defaults, { swarm: { toolCallBudget: 5 } }).swarm), null);
|
|
9
|
+
assert.equal(dispatchSettings({ dispatch: false }), null);
|
|
10
|
+
assert.equal(dispatchSettings({ dispatch: null }), null);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("an empty dispatch section turns the guard on with every default", () => {
|
|
14
|
+
assert.deepEqual(dispatchSettings(merge(defaults, { swarm: { dispatch: {} } }).swarm), dispatchDefaults);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("a list in the section replaces its default rather than extending it", () => {
|
|
18
|
+
const settings = dispatchSettings({ dispatch: { allowedTypes: ["Explore"] } });
|
|
19
|
+
assert.deepEqual(settings.allowedTypes, ["Explore"]);
|
|
20
|
+
assert.deepEqual(settings.implementerTypes, dispatchDefaults.implementerTypes);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("a wrong value names its key", () => {
|
|
24
|
+
for (const [dispatch, key] of [
|
|
25
|
+
["on", /swarm\.dispatch in qc\.config\.json must be an object/],
|
|
26
|
+
[["Explore"], /swarm\.dispatch in qc\.config\.json must be an object/],
|
|
27
|
+
[{ allowedTypes: "Explore" }, /swarm\.dispatch\.allowedTypes/],
|
|
28
|
+
[{ implementerTypes: [""] }, /swarm\.dispatch\.implementerTypes/],
|
|
29
|
+
[{ maxPromptChars: 0 }, /swarm\.dispatch\.maxPromptChars/],
|
|
30
|
+
[{ slotMinutes: -1 }, /swarm\.dispatch\.slotMinutes/],
|
|
31
|
+
]) {
|
|
32
|
+
assert.throws(() => dispatchSettings({ dispatch }), key, JSON.stringify(dispatch));
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const on = dispatchSettings({ dispatch: { maxPromptChars: 20 } });
|
|
37
|
+
|
|
38
|
+
test("a dispatch with neither a model nor an allowed type is refused, and the reason names both fixes", () => {
|
|
39
|
+
for (const input of [{ prompt: "p" }, { subagent_type: "general-purpose", prompt: "p" }, { subagent_type: "Explore", model: " " }]) {
|
|
40
|
+
const reason = dispatchRefusal(input, on) ?? "";
|
|
41
|
+
assert.match(reason, /names no model/, JSON.stringify(input));
|
|
42
|
+
assert.match(reason, /swarm\.dispatch\.allowedTypes/);
|
|
43
|
+
assert.match(reason, /architecture:sdd-implementer/);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("a named model or an allowed type passes", () => {
|
|
48
|
+
assert.equal(dispatchRefusal({ subagent_type: "general-purpose", model: "sonnet", prompt: "p" }, on), null);
|
|
49
|
+
assert.equal(dispatchRefusal({ model: "haiku" }, on), null);
|
|
50
|
+
assert.equal(dispatchRefusal({ subagent_type: "architecture:sdd-reviewer" }, on), null);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("a fork passes only as an allowed type, since it ignores the model", () => {
|
|
54
|
+
assert.match(dispatchRefusal({ subagent_type: "fork", model: "haiku" }, on) ?? "", /`fork` is in swarm\.dispatch\.allowedTypes/);
|
|
55
|
+
const withFork = dispatchSettings({ dispatch: { allowedTypes: ["fork"] } });
|
|
56
|
+
assert.equal(dispatchRefusal({ subagent_type: "fork" }, withFork), null);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("a prompt over the limit is refused, and a prompt at the limit passes", () => {
|
|
60
|
+
const reason = dispatchRefusal({ model: "haiku", prompt: "x".repeat(21) }, on) ?? "";
|
|
61
|
+
assert.match(reason, /21 characters/);
|
|
62
|
+
assert.match(reason, /swarm\.dispatch\.maxPromptChars \(20\)/);
|
|
63
|
+
assert.match(reason, /Write the brief to a file/);
|
|
64
|
+
assert.equal(dispatchRefusal({ model: "haiku", prompt: "x".repeat(20) }, on), null);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("the model rule comes before the length rule", () => {
|
|
68
|
+
assert.match(dispatchRefusal({ prompt: "x".repeat(21) }, on) ?? "", /names no model/);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("an omitted type is general-purpose, and only a listed type is an implementer", () => {
|
|
72
|
+
assert.equal(typeOf({}), "general-purpose");
|
|
73
|
+
assert.equal(typeOf({ subagent_type: "" }), "general-purpose");
|
|
74
|
+
assert.equal(typeOf({ subagent_type: " \t" }), "general-purpose");
|
|
75
|
+
assert.equal(isImplementer("architecture:sdd-implementer", on), true);
|
|
76
|
+
assert.equal(isImplementer("sdd-implementer", on), true);
|
|
77
|
+
assert.equal(isImplementer("architecture:sdd-reviewer", on), false);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("the slot refusal names the expiry and the file that frees the slot", () => {
|
|
81
|
+
const reason = slotRefusal({
|
|
82
|
+
type: "sdd-implementer",
|
|
83
|
+
claimedAt: new Date(0),
|
|
84
|
+
expiresAt: new Date(3_600_000),
|
|
85
|
+
slotFile: "/tmp/slot.claim",
|
|
86
|
+
});
|
|
87
|
+
assert.match(reason, /holds the one implementer slot/);
|
|
88
|
+
assert.match(reason, /1970-01-01T01:00:00\.000Z/);
|
|
89
|
+
assert.match(reason, /delete \/tmp\/slot\.claim/);
|
|
90
|
+
});
|
package/src/cli/doctor.mjs
CHANGED
|
@@ -9,6 +9,7 @@ import { execFile } from "node:child_process";
|
|
|
9
9
|
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
10
10
|
import path from "node:path";
|
|
11
11
|
import { promisify } from "node:util";
|
|
12
|
+
import { hookDrift } from "./git-hooks.mjs";
|
|
12
13
|
|
|
13
14
|
const execFileAsync = promisify(execFile);
|
|
14
15
|
|
|
@@ -38,9 +39,11 @@ export function versionOf(harnessRoot) {
|
|
|
38
39
|
* own files -- that verdict is worthless and has to stop the turn. A copy *ahead* is what every
|
|
39
40
|
* day of developing this kit looks like; it is merely stricter, and anything it reports is a real
|
|
40
41
|
* finding to act on. Blocking on that direction would make the kit unable to grow.
|
|
42
|
+
* A plugin whose hooks run the installed copy never judges the repository with its own gates, so
|
|
43
|
+
* a copy behind is then a note.
|
|
41
44
|
* @returns null when the two agree, else a report carrying `blocking`.
|
|
42
45
|
*/
|
|
43
|
-
export function drift(installed, plugin) {
|
|
46
|
+
export function drift(installed, plugin, { hooksRunInstalled = false } = {}) {
|
|
44
47
|
if (!plugin || !installed) return null;
|
|
45
48
|
const missing = installed.gates.filter((name) => !plugin.gates.includes(name));
|
|
46
49
|
const extra = plugin.gates.filter((name) => !installed.gates.includes(name));
|
|
@@ -48,20 +51,25 @@ export function drift(installed, plugin) {
|
|
|
48
51
|
return {
|
|
49
52
|
missing,
|
|
50
53
|
extra,
|
|
51
|
-
blocking: missing.length > 0,
|
|
54
|
+
blocking: missing.length > 0 && !hooksRunInstalled,
|
|
52
55
|
installedVersion: installed.version,
|
|
53
56
|
pluginVersion: plugin.version,
|
|
54
57
|
};
|
|
55
58
|
}
|
|
56
59
|
|
|
57
60
|
export function driftReport(found, pluginRoot, checkoutRoot = pluginRoot) {
|
|
58
|
-
const
|
|
61
|
+
const behindOnly = found.missing.length > 0 && !found.blocking;
|
|
62
|
+
const label = found.blocking ? "FAIL harness " : behindOnly ? "NOTE harness " : "WARN harness ";
|
|
59
63
|
const lines = [
|
|
60
64
|
`${label} the hooks and the lockfile run different harnesses`,
|
|
61
65
|
` installed ${found.installedVersion ?? "?"} — what \`pnpm check\` and CI enforce`,
|
|
62
66
|
` plugin ${found.pluginVersion ?? "?"} — what the agent hooks enforce, from ${pluginRoot}`,
|
|
63
67
|
];
|
|
64
|
-
if (
|
|
68
|
+
if (behindOnly) {
|
|
69
|
+
lines.push(` the plugin copy is missing: ${found.missing.join(", ")}`);
|
|
70
|
+
lines.push(` the hooks run the installed copy, so the structure check is unaffected`);
|
|
71
|
+
lines.push(` update the plugin and restart the session to get its newer guards`);
|
|
72
|
+
} else if (found.missing.length > 0) {
|
|
65
73
|
lines.push(` the plugin copy is missing: ${found.missing.join(", ")}`);
|
|
66
74
|
lines.push(` so its \`qc check\` reports unknown-check for rules your docs correctly list`);
|
|
67
75
|
lines.push(` fix the checkout, never the repository: git -C ${checkoutRoot} log --oneline -1`);
|
|
@@ -86,6 +94,17 @@ async function behindMain(root) {
|
|
|
86
94
|
}
|
|
87
95
|
}
|
|
88
96
|
|
|
97
|
+
/** True when the plugin's stop gate prefers the repository's installed harness over its own copy. */
|
|
98
|
+
export function hooksRunInstalled(pluginCheckout, read = readFileSync) {
|
|
99
|
+
try {
|
|
100
|
+
return read(path.join(pluginCheckout, "hooks", "stop-gate.sh"), "utf8").includes(
|
|
101
|
+
"node_modules/architecture-harness/src/cli/qc.mjs",
|
|
102
|
+
);
|
|
103
|
+
} catch {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
89
108
|
function pluginHarnessRoot() {
|
|
90
109
|
const root = process.env.CLAUDE_PLUGIN_ROOT;
|
|
91
110
|
if (!root) return null;
|
|
@@ -99,6 +118,21 @@ function installedHarnessRoot(configRoot) {
|
|
|
99
118
|
}
|
|
100
119
|
|
|
101
120
|
export async function runDoctor(config) {
|
|
121
|
+
const hooksFailed = reportHooks(config);
|
|
122
|
+
const harnessCode = await compareHarnesses(config);
|
|
123
|
+
return hooksFailed ? 1 : harnessCode;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** @returns true when an installed git hook lacks a call `hooks.required` names. */
|
|
127
|
+
function reportHooks(config) {
|
|
128
|
+
const { problems, notes } = hookDrift(config.root, config.hooks?.required ?? {});
|
|
129
|
+
for (const problem of problems) console.error(`FAIL hooks ${problem.path}: ${problem.detail}`);
|
|
130
|
+
for (const note of notes) console.log(`NOTE hooks ${note}`);
|
|
131
|
+
if (problems.length === 0) console.log("OK hooks each git hook makes the calls hooks.required names");
|
|
132
|
+
return problems.length > 0;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function compareHarnesses(config) {
|
|
102
136
|
const installedRoot = installedHarnessRoot(config.root);
|
|
103
137
|
const pluginRoot = pluginHarnessRoot();
|
|
104
138
|
|
|
@@ -116,7 +150,7 @@ export async function runDoctor(config) {
|
|
|
116
150
|
const plugin = { gates: gateNames(pluginRoot), version: versionOf(pluginRoot) };
|
|
117
151
|
const checkoutRoot = path.resolve(pluginRoot, "..", "..");
|
|
118
152
|
const position = await behindMain(checkoutRoot);
|
|
119
|
-
const found = drift(installed, plugin);
|
|
153
|
+
const found = drift(installed, plugin, { hooksRunInstalled: hooksRunInstalled(checkoutRoot) });
|
|
120
154
|
if (!found) {
|
|
121
155
|
console.log(`OK plugin the same ${plugin.gates.length} gate(s) as the lockfile pins`);
|
|
122
156
|
return 0;
|
package/src/cli/doctor.test.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
2
|
import { test } from "node:test";
|
|
3
|
-
import { drift, driftReport, gateNames } from "./doctor.mjs";
|
|
3
|
+
import { drift, driftReport, gateNames, hooksRunInstalled } from "./doctor.mjs";
|
|
4
4
|
|
|
5
5
|
const installed = { gates: ["citations", "comment-style", "headless-sagas", "test-mirror"], version: "0.3.0" };
|
|
6
6
|
|
|
@@ -55,3 +55,20 @@ test("gate names come from the directory, with the test files excluded", () => {
|
|
|
55
55
|
const read = () => ["citations.mjs", "citations.test.mjs", "headless-sagas.mjs", "README.md"];
|
|
56
56
|
assert.deepEqual(gateNames(process.cwd(), read), ["citations", "headless-sagas"]);
|
|
57
57
|
});
|
|
58
|
+
|
|
59
|
+
test("a checkout behind the lockfile is a note, never a fail, when the plugin's hooks run the installed copy", () => {
|
|
60
|
+
const found = drift(installed, { gates: ["citations"], version: "0.2.0" }, { hooksRunInstalled: true });
|
|
61
|
+
assert.equal(found.blocking, false);
|
|
62
|
+
const report = driftReport(found, "/k");
|
|
63
|
+
assert.match(report, /^NOTE/);
|
|
64
|
+
assert.match(report, /the hooks run the installed copy/);
|
|
65
|
+
assert.doesNotMatch(report, /unknown-check/);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("the plugin's hooks run the installed copy only when its stop gate prefers it", () => {
|
|
69
|
+
const gate = (text) => () => text;
|
|
70
|
+
const newer = 'if [ -f "$ROOT/node_modules/architecture-harness/src/cli/qc.mjs" ]; then';
|
|
71
|
+
assert.equal(hooksRunInstalled("/k", gate(newer)), true);
|
|
72
|
+
assert.equal(hooksRunInstalled("/k", gate('QC="node ${CLAUDE_PLUGIN_ROOT}/packages/qc-harness/src/cli/qc.mjs"')), false);
|
|
73
|
+
assert.equal(hooksRunInstalled("/k", () => { throw new Error("ENOENT"); }), false);
|
|
74
|
+
});
|
|
@@ -5,14 +5,17 @@ import { createHash } from "node:crypto";
|
|
|
5
5
|
import { existsSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
|
|
8
|
-
const fileSafe = (id) => String(id).replace(/[^A-Za-z0-9_.-]/g, "_");
|
|
9
8
|
const BATCH_AWARE = "batch-aware";
|
|
10
9
|
|
|
10
|
+
/** An id as a file name: each character outside letters, digits, `_`, `.` and `-` becomes `_`. */
|
|
11
|
+
export const fileSafe = (id) => String(id).replace(/[^A-Za-z0-9_.-]/g, "_");
|
|
12
|
+
|
|
11
13
|
/** The batch folder of one agent in one session. */
|
|
12
14
|
export const batchDirOf = (sessionId, agentKey, tmp) =>
|
|
13
15
|
path.join(tmp, "architecture-kit", "edit-batch", `${fileSafe(sessionId)}-${fileSafe(agentKey)}`);
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
/** The claim file of one key in one folder. A hash names it, so a key may hold any character. */
|
|
18
|
+
export const claimFileOf = (dir, key) => path.join(dir, `${createHash("sha256").update(key).digest("hex")}.claim`);
|
|
16
19
|
|
|
17
20
|
/**
|
|
18
21
|
* Claims one path through an exclusive create, so two hooks that run at once cannot both win.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// The git hooks are the gates a human commit passes. A hook that drifts from the kit's template
|
|
2
|
+
// skips a step and nothing says so, so this reads the hooks and names each required call they lack.
|
|
3
|
+
|
|
4
|
+
import { execFileSync } from "node:child_process";
|
|
5
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
|
|
8
|
+
const TRACKED = ".githooks";
|
|
9
|
+
|
|
10
|
+
function gitOutput(root, ...args) {
|
|
11
|
+
try {
|
|
12
|
+
return execFileSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
13
|
+
} catch {
|
|
14
|
+
return "";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @returns the folder git runs hooks from, and whether a missing hook there is drift. A folder
|
|
20
|
+
* named by core.hooksPath, or the tracked `.githooks` every clone installs, must hold each hook.
|
|
21
|
+
* Git's own hooks folder is local to one clone, so a hook missing there is only not installed.
|
|
22
|
+
*/
|
|
23
|
+
export function hookFolder(root) {
|
|
24
|
+
const configured = gitOutput(root, "config", "--get", "core.hooksPath");
|
|
25
|
+
if (configured) return { dir: path.resolve(root, configured), strict: true };
|
|
26
|
+
if (existsSync(path.join(root, TRACKED))) return { dir: path.join(root, TRACKED), strict: true };
|
|
27
|
+
const local = gitOutput(root, "rev-parse", "--git-path", "hooks");
|
|
28
|
+
return local ? { dir: path.resolve(root, local), strict: false } : null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The hook's text without its comment lines, so a commented-out call is no call. */
|
|
32
|
+
const liveText = (text) =>
|
|
33
|
+
text
|
|
34
|
+
.split(/\r?\n/)
|
|
35
|
+
.filter((line) => !line.trimStart().startsWith("#"))
|
|
36
|
+
.join("\n");
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {string} root the repository root
|
|
40
|
+
* @param {Record<string, string[]>} required each hook name, and the calls it must make
|
|
41
|
+
* @returns {{problems: {path: string, rule: string, detail: string}[], notes: string[]}}
|
|
42
|
+
*/
|
|
43
|
+
export function hookDrift(root, required) {
|
|
44
|
+
const problems = [];
|
|
45
|
+
const notes = [];
|
|
46
|
+
const folder = hookFolder(root);
|
|
47
|
+
if (!folder) return { problems, notes: ["no git checkout, so no hooks to read"] };
|
|
48
|
+
|
|
49
|
+
for (const [name, calls] of Object.entries(required)) {
|
|
50
|
+
if (calls.length === 0) continue;
|
|
51
|
+
const file = path.join(folder.dir, name);
|
|
52
|
+
const shown = path.relative(root, file).split(path.sep).join("/");
|
|
53
|
+
if (!existsSync(file)) {
|
|
54
|
+
if (folder.strict) {
|
|
55
|
+
problems.push({ path: shown, rule: "hook-drift", detail: `${name} is missing, so its steps never run. Copy it from the kit's templates/githooks.` });
|
|
56
|
+
} else {
|
|
57
|
+
notes.push(`${name} is not installed at ${shown}. Run \`qc install-hooks\`.`);
|
|
58
|
+
}
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const text = liveText(readFileSync(file, "utf8"));
|
|
62
|
+
for (const call of calls.filter((expected) => !text.includes(expected))) {
|
|
63
|
+
problems.push({ path: shown, rule: "hook-drift", detail: `${name} never calls \`${call}\`, so that step is off. Add it, as the kit's templates/githooks/${name} does.` });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return { problems, notes };
|
|
67
|
+
}
|