architecture-harness 0.7.0 → 0.9.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.
Files changed (43) hide show
  1. package/package.json +1 -1
  2. package/src/cli/bash-command-guard.mjs +129 -0
  3. package/src/cli/bash-command-guard.test.mjs +96 -0
  4. package/src/cli/bash-edit-guard.mjs +163 -0
  5. package/src/cli/bash-edit-guard.test.mjs +114 -0
  6. package/src/cli/budget-counter.mjs +40 -0
  7. package/src/cli/budget-counter.test.mjs +35 -0
  8. package/src/cli/budget-guard.mjs +56 -0
  9. package/src/cli/budget-guard.test.mjs +90 -0
  10. package/src/cli/budget.mjs +74 -0
  11. package/src/cli/budget.test.mjs +79 -0
  12. package/src/cli/check.mjs +197 -174
  13. package/src/cli/checkout-root.mjs +16 -0
  14. package/src/cli/cli.test.mjs +79 -0
  15. package/src/cli/decisions.mjs +5 -6
  16. package/src/cli/edit-batch-guard.mjs +77 -0
  17. package/src/cli/edit-batch-guard.test.mjs +131 -0
  18. package/src/cli/edit-batch-state.mjs +57 -0
  19. package/src/cli/generated-file-guard.mjs +81 -0
  20. package/src/cli/generated-file-guard.test.mjs +104 -0
  21. package/src/cli/ignored-paths.mjs +29 -0
  22. package/src/cli/ignored-paths.test.mjs +44 -0
  23. package/src/cli/powershell-command.mjs +57 -0
  24. package/src/cli/powershell-command.test.mjs +52 -0
  25. package/src/cli/qc.mjs +10 -2
  26. package/src/cli/repo-files.mjs +65 -0
  27. package/src/cli/repo-files.test.mjs +78 -0
  28. package/src/cli/shell-command.mjs +221 -0
  29. package/src/cli/shell-command.test.mjs +118 -0
  30. package/src/cli/work-order-guard.mjs +19 -21
  31. package/src/cli/work-order-guard.test.mjs +18 -0
  32. package/src/cli/work-order-manifest.mjs +14 -0
  33. package/src/cli/worktree-isolation.mjs +3 -1
  34. package/src/cli/worktree-isolation.test.mjs +13 -0
  35. package/src/cli/worktree.mjs +145 -0
  36. package/src/cli/worktree.test.mjs +110 -0
  37. package/src/config.mjs +9 -0
  38. package/src/enforcement-map.mjs +2 -1
  39. package/src/gates/english-source.mjs +13 -9
  40. package/src/gates/english-source.test.mjs +19 -1
  41. package/src/generated-markers.mjs +16 -0
  42. package/src/glob.mjs +23 -0
  43. package/templates/CLAUDE.md +1 -1
@@ -0,0 +1,90 @@
1
+ import { strict as assert } from "node:assert";
2
+ import { execFileSync, spawnSync } from "node:child_process";
3
+ import { existsSync, 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
+
9
+ const GUARD = fileURLToPath(new URL("./budget-guard.mjs", import.meta.url));
10
+
11
+ /** An adopted checkout with a budget, a linked worktree of it, a checkout with no config, and a temp folder for the counters. */
12
+ function workspace(t, swarm = { toolCallBudget: 3 }) {
13
+ const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-budget-")));
14
+ t.after(() => rmSync(base, { recursive: true, force: true }));
15
+ const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe" });
16
+ const primary = path.join(base, "primary");
17
+ const linked = path.join(base, "linked");
18
+ const plain = path.join(base, "plain");
19
+ const tmp = path.join(base, "tmp");
20
+ for (const dir of [primary, plain, tmp]) mkdirSync(dir);
21
+ git(primary, "init", "-q", "-b", "main");
22
+ writeFileSync(path.join(primary, "qc.config.json"), JSON.stringify({ swarm }));
23
+ git(primary, "add", ".");
24
+ git(primary, "-c", "user.name=qc", "-c", "user.email=qc@example.com", "commit", "-q", "-m", "init");
25
+ git(primary, "worktree", "add", "-q", linked, "-b", "feat/work-order");
26
+ git(plain, "init", "-q", "-b", "main");
27
+ mkdirSync(path.join(linked, "src"));
28
+ return { primary, linked, plain, tmp, counters: path.join(tmp, "architecture-kit", "budget") };
29
+ }
30
+
31
+ const callOf = (cwd, agentId, tool_name = "Bash", tool_input = { command: "npm test" }) => ({
32
+ session_id: "session-aaaa",
33
+ ...(agentId ? { agent_id: agentId, agent_type: "general-purpose" } : {}),
34
+ cwd,
35
+ hook_event_name: "PreToolUse",
36
+ tool_name,
37
+ tool_input,
38
+ });
39
+
40
+ /** Runs the hook as its own process, with every temp-folder variable pinned to the test's folder. */
41
+ function runGuard(ws, call) {
42
+ const result = spawnSync(process.execPath, [GUARD], {
43
+ input: JSON.stringify(call),
44
+ env: { ...process.env, TEMP: ws.tmp, TMP: ws.tmp, TMPDIR: ws.tmp },
45
+ encoding: "utf8",
46
+ });
47
+ assert.equal(result.status, 0, result.stderr);
48
+ return result.stdout.trim() === "" ? null : JSON.parse(result.stdout);
49
+ }
50
+
51
+ const countOf = (ws, agentId) => JSON.parse(readFileSync(path.join(ws.counters, `session-aaaa-${agentId}.json`), "utf8")).count;
52
+
53
+ test("the main session has no agent id, so it is never counted", (t) => {
54
+ const ws = workspace(t);
55
+ for (let i = 0; i < 4; i++) assert.equal(runGuard(ws, callOf(ws.linked, null)), null);
56
+ assert.equal(existsSync(ws.counters), false);
57
+ });
58
+
59
+ test("a repository with no qc.config.json is never counted", (t) => {
60
+ const ws = workspace(t);
61
+ for (let i = 0; i < 4; i++) assert.equal(runGuard(ws, callOf(ws.plain, "agent-1")), null);
62
+ assert.equal(existsSync(ws.counters), false);
63
+ });
64
+
65
+ test("each agent of one session keeps its own count", (t) => {
66
+ const ws = workspace(t, { toolCallBudget: 50 });
67
+ runGuard(ws, callOf(ws.linked, "agent-1"));
68
+ runGuard(ws, callOf(ws.linked, "agent-1"));
69
+ runGuard(ws, callOf(ws.linked, "agent-2"));
70
+ assert.equal(countOf(ws, "agent-1"), 2);
71
+ assert.equal(countOf(ws, "agent-2"), 1);
72
+ });
73
+
74
+ test("in a linked worktree, the budget from qc.config.json denies the call that reaches it, and the hand-off still runs", (t) => {
75
+ const ws = workspace(t);
76
+ assert.equal(runGuard(ws, callOf(path.join(ws.linked, "src"), "agent-1")), null);
77
+ assert.equal(runGuard(ws, callOf(ws.linked, "agent-1")), null);
78
+ const denied = runGuard(ws, callOf(ws.linked, "agent-1"));
79
+ assert.equal(denied.hookSpecificOutput.permissionDecision, "deny");
80
+ assert.match(denied.hookSpecificOutput.permissionDecisionReason, /call 3 of 3/);
81
+ assert.equal(runGuard(ws, callOf(ws.linked, "agent-1", "Bash", { command: "git push" })), null);
82
+ });
83
+
84
+ test("the default budget reminds at its seventieth call", (t) => {
85
+ const ws = workspace(t, {});
86
+ mkdirSync(ws.counters, { recursive: true });
87
+ writeFileSync(path.join(ws.counters, "session-aaaa-agent-1.json"), JSON.stringify({ count: 69 }));
88
+ const output = runGuard(ws, callOf(ws.linked, "agent-1"));
89
+ assert.match(output.hookSpecificOutput.additionalContext, /call 70 of 100/);
90
+ });
@@ -0,0 +1,74 @@
1
+ // The branch that turns an agent's tool-call count into a verdict: nothing, a reminder, or a deny.
2
+ // An agent does not count its own calls, and each call re-sends its whole transcript, so the hook
3
+ // counts for it and closes every tool except the hand-off once the budget is spent.
4
+
5
+ import { defaults } from "../config.mjs";
6
+ import { gitCall, isReadOnly, segmentsOf, writesRedirect } from "./shell-command.mjs";
7
+
8
+ export const DEFAULT_BUDGET = defaults.swarm.toolCallBudget;
9
+ const REMIND_PERCENT = 70;
10
+ const REMIND_EVERY = 10;
11
+ const KEY = "swarm.toolCallBudget";
12
+ const HANDOFF_GIT = new Set(["status", "diff", "log", "add", "commit", "push"]);
13
+
14
+ /** @returns the budget from the `swarm` config section, or throws naming the key a repository got wrong. */
15
+ export function budgetOf(swarm = {}) {
16
+ const budget = swarm.toolCallBudget ?? DEFAULT_BUDGET;
17
+ if (!Number.isInteger(budget) || budget < 1) {
18
+ throw new Error(`${KEY} in qc.config.json must be a positive whole number, got ${JSON.stringify(swarm.toolCallBudget)}`);
19
+ }
20
+ return budget;
21
+ }
22
+
23
+ const isHandoffSegment = (segment) =>
24
+ isReadOnly(segment) || (!writesRedirect(segment) && HANDOFF_GIT.has(gitCall(segment)?.sub));
25
+
26
+ /** True for a call the hand-off needs: read, commit and push, write the note, send the report. */
27
+ export function isHandoffCall(call) {
28
+ const input = call.tool_input ?? {};
29
+ switch (call.tool_name) {
30
+ case "Read":
31
+ case "SubagentHandback":
32
+ return true;
33
+ case "Write":
34
+ case "Edit":
35
+ return String(input.file_path ?? "").replaceAll("\\", "/").includes("/handoffs/");
36
+ case "Bash": {
37
+ const segments = segmentsOf(String(input.command ?? ""));
38
+ return segments.length > 0 && segments.every(isHandoffSegment);
39
+ }
40
+ default:
41
+ return false;
42
+ }
43
+ }
44
+
45
+ const remind = (count, budget) => ({
46
+ hookSpecificOutput: {
47
+ hookEventName: "PreToolUse",
48
+ additionalContext:
49
+ `Tool-call budget: this is call ${count} of ${budget}. Finish the current step, commit and push green work, ` +
50
+ `and plan the hand-off. At call ${budget}, only the hand-off can run.`,
51
+ },
52
+ });
53
+
54
+ const deny = (count, budget) => ({
55
+ hookSpecificOutput: {
56
+ hookEventName: "PreToolUse",
57
+ permissionDecision: "deny",
58
+ permissionDecisionReason:
59
+ `Tool-call budget spent: this is call ${count} of ${budget} (${KEY} in qc.config.json). ` +
60
+ "Only the hand-off can run now. " +
61
+ "1. Commit and push green work with git add, git commit and git push. " +
62
+ "2. Write the hand-off note to a path under /handoffs/: what is done, what is left in order, and the exact next step. " +
63
+ "3. Send the final report, and name the note. " +
64
+ "Read and read-only commands still run.",
65
+ },
66
+ });
67
+
68
+ /** @returns the hook output for the call numbered `count`, or null to let it run with no message. */
69
+ export function budgetVerdict({ count, budget, call }) {
70
+ if (count >= budget) return isHandoffCall(call) ? null : deny(count, budget);
71
+ const remindAt = Math.ceil((budget * REMIND_PERCENT) / 100);
72
+ if (count >= remindAt && (count - remindAt) % REMIND_EVERY === 0) return remind(count, budget);
73
+ return null;
74
+ }
@@ -0,0 +1,79 @@
1
+ import { strict as assert } from "node:assert";
2
+ import { test } from "node:test";
3
+ import { DEFAULT_BUDGET, budgetOf, budgetVerdict, isHandoffCall } from "./budget.mjs";
4
+
5
+ const bash = (command) => ({ tool_name: "Bash", tool_input: { command } });
6
+ const write = (file_path) => ({ tool_name: "Write", tool_input: { file_path, content: "x" } });
7
+ const edit = (file_path) => ({ tool_name: "Edit", tool_input: { file_path, old_string: "a", new_string: "b" } });
8
+ const verdict = (count, call = bash("npm test"), budget = 100) => budgetVerdict({ count, budget, call });
9
+
10
+ test("the budget is swarm.toolCallBudget, and one hundred calls without it", () => {
11
+ assert.equal(DEFAULT_BUDGET, 100);
12
+ assert.equal(budgetOf(), 100);
13
+ assert.equal(budgetOf({}), 100);
14
+ assert.equal(budgetOf({ toolCallBudget: 40 }), 40);
15
+ });
16
+
17
+ test("a budget that is not a positive whole number is a named config error", () => {
18
+ for (const toolCallBudget of [0, -1, 2.5, "100"]) {
19
+ assert.throws(() => budgetOf({ toolCallBudget }), /swarm\.toolCallBudget/);
20
+ }
21
+ });
22
+
23
+ test("below seventy percent of the budget a call runs with no message", () => {
24
+ assert.equal(verdict(1), null);
25
+ assert.equal(verdict(69), null);
26
+ });
27
+
28
+ test("the reminder fires at seventy percent and every ten calls after, as model-visible context", () => {
29
+ for (const count of [70, 80, 90]) {
30
+ const output = verdict(count);
31
+ assert.equal(output.hookSpecificOutput.hookEventName, "PreToolUse");
32
+ assert.equal(output.hookSpecificOutput.permissionDecision, undefined);
33
+ const reminder = output.hookSpecificOutput.additionalContext;
34
+ assert.match(reminder, new RegExp(`call ${count} of 100`));
35
+ assert.match(reminder, /commit and push green work/);
36
+ assert.match(reminder, /hand-off/);
37
+ }
38
+ assert.equal(verdict(71), null);
39
+ assert.equal(verdict(89), null);
40
+ });
41
+
42
+ test("the reminder threshold rounds up for a small budget", () => {
43
+ assert.equal(verdict(3, bash("npm test"), 5), null);
44
+ assert.ok(verdict(4, bash("npm test"), 5).hookSpecificOutput.additionalContext);
45
+ });
46
+
47
+ test("at the budget and past it, a call outside the hand-off is denied with the hand-off steps", () => {
48
+ for (const call of [bash("npm test"), bash("git status && pnpm test"), bash("git checkout main"), bash("git status > s.txt"), { tool_name: "Grep", tool_input: {} }, write("src/a.ts")]) {
49
+ for (const count of [100, 101, 150]) {
50
+ const output = verdict(count, call);
51
+ assert.equal(output.hookSpecificOutput.permissionDecision, "deny", JSON.stringify(call));
52
+ const reason = output.hookSpecificOutput.permissionDecisionReason;
53
+ assert.match(reason, /commit and push green work/i);
54
+ assert.match(reason, /hand-off note/);
55
+ assert.match(reason, /final report/);
56
+ assert.match(reason, /swarm\.toolCallBudget/);
57
+ }
58
+ }
59
+ });
60
+
61
+ test("at the budget, every call a hand-off needs still runs", () => {
62
+ const handoff = [
63
+ bash("git status"),
64
+ bash("git diff --stat"),
65
+ bash("git log --oneline -3 | head -3"),
66
+ bash('git add -A && git commit -m "wip: stop here" && git push -u origin HEAD'),
67
+ bash('git -C "C:/work/kit wt" commit -m "wip: a; b && c"'),
68
+ bash("cd /work/wt && git push"),
69
+ bash("git commit -F - <<'EOF'\nwip: stop; here && now\nEOF"),
70
+ write("C:\\tmp\\scratch\\handoffs\\k1.md"),
71
+ edit("/tmp/scratch/handoffs/k1.md"),
72
+ { tool_name: "Read", tool_input: { file_path: "/work/wt/src/a.ts" } },
73
+ { tool_name: "SubagentHandback", tool_input: { message: "done" } },
74
+ ];
75
+ for (const call of handoff) {
76
+ assert.equal(isHandoffCall(call), true, JSON.stringify(call));
77
+ assert.equal(verdict(100, call), null, JSON.stringify(call));
78
+ }
79
+ });