architecture-harness 0.18.2 → 0.19.1
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 +60 -11
- package/src/cli/bash-command-guard.test.mjs +66 -0
- package/src/cli/check-input.mjs +24 -0
- package/src/cli/check.mjs +2 -2
- package/src/cli/cli.test.mjs +50 -1
- package/src/cli/dispatch-guard.test.mjs +6 -23
- package/src/cli/dispatch-reminder.mjs +3 -3
- package/src/cli/dispatch-reminder.test.mjs +15 -10
- package/src/cli/dispatch.mjs +4 -3
- package/src/cli/dispatch.test.mjs +25 -17
- package/src/cli/explore-guard.mjs +11 -87
- package/src/cli/explore-guard.test.mjs +34 -97
- package/src/cli/explore-hint.mjs +3 -3
- package/src/cli/explore-hint.test.mjs +14 -5
- package/src/cli/init.mjs +12 -0
- package/src/cli/merge-guard.mjs +4 -0
- package/src/cli/merge-guard.test.mjs +21 -2
- package/src/cli/plugin-agents.test.mjs +4 -4
- package/src/cli/powershell-command.mjs +1 -1
- package/src/cli/qc.mjs +9 -4
- package/src/cli/workflow-hooks.test.mjs +1 -0
- package/src/cli/worktree-gate.mjs +117 -0
- package/src/cli/worktree-gate.test.mjs +152 -0
- package/src/cli/worktree-settings.mjs +27 -0
- package/src/cli/worktree.mjs +60 -21
- package/src/cli/worktree.test.mjs +106 -7
- package/src/config.mjs +19 -13
- package/src/eslint/preset.mjs +2 -2
- package/src/eslint/preset.test.mjs +4 -2
- package/src/gates/sql-identifiers.mjs +11 -3
- package/src/gates/sql-identifiers.test.mjs +39 -0
- package/templates/.jscpd.json +1 -0
- package/templates/docs/decisions.md +1 -0
- package/templates/docs/enforcement.md +10 -0
- package/templates/githooks/pre-commit +8 -6
|
@@ -1,41 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { createHash } from "node:crypto";
|
|
7
|
-
import {
|
|
8
|
-
closeSync,
|
|
9
|
-
existsSync,
|
|
10
|
-
mkdirSync,
|
|
11
|
-
openSync,
|
|
12
|
-
readdirSync,
|
|
13
|
-
readSync,
|
|
14
|
-
realpathSync,
|
|
15
|
-
statSync,
|
|
16
|
-
unlinkSync,
|
|
17
|
-
writeFileSync,
|
|
18
|
-
} from "node:fs";
|
|
19
|
-
import os from "node:os";
|
|
2
|
+
// The branch for long Read calls adds advice while leaving the read available.
|
|
3
|
+
|
|
4
|
+
import { closeSync, existsSync, openSync, readSync, realpathSync } from "node:fs";
|
|
20
5
|
import path from "node:path";
|
|
21
6
|
import { fileURLToPath } from "node:url";
|
|
22
7
|
import { globMatcher } from "../glob.mjs";
|
|
23
8
|
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
24
9
|
import { exploreSettingsAt, exploreToolLines } from "./dispatch.mjs";
|
|
25
|
-
import { slotDirOf } from "./dispatch-slot.mjs";
|
|
26
10
|
|
|
27
11
|
const BINARY_EXTENSIONS = new Set(["png", "jpg", "jpeg", "gif", "webp", "svg", "pdf", "ipynb"]);
|
|
28
12
|
const SNIFF_BYTES = 8192;
|
|
29
|
-
const REFUSED_SUFFIX = ".refused";
|
|
30
|
-
const MAX_REFUSED = 200;
|
|
31
13
|
|
|
32
14
|
const output = (fields) => ({ hookSpecificOutput: { hookEventName: "PreToolUse", ...fields } });
|
|
33
|
-
const deny = (reason) => output({ permissionDecision: "deny", permissionDecisionReason: reason });
|
|
34
15
|
const context = (text) => output({ additionalContext: text });
|
|
35
16
|
|
|
36
|
-
|
|
37
|
-
* case-folded drive letter, a symlink) resolve to one, everywhere this guard compares or hashes a
|
|
38
|
-
* path; falls back to `resolved` itself when the real path cannot be read. */
|
|
17
|
+
// Canonical paths keep checkout and exemption checks consistent across symlinks and alternate spellings.
|
|
39
18
|
function realOf(resolved) {
|
|
40
19
|
try {
|
|
41
20
|
return realpathSync.native(resolved);
|
|
@@ -44,58 +23,6 @@ function realOf(resolved) {
|
|
|
44
23
|
}
|
|
45
24
|
}
|
|
46
25
|
|
|
47
|
-
// One marker file per refused path, named by its hash, never a shared list: two Read hooks of one
|
|
48
|
-
// batch each create their own marker, so neither call can lose the other's write.
|
|
49
|
-
const markerFileOf = (dir, realAbsolute) => path.join(dir, `${createHash("sha256").update(realAbsolute).digest("hex")}${REFUSED_SUFFIX}`);
|
|
50
|
-
|
|
51
|
-
/** Records one refusal. An existing marker (a concurrent refusal of the same path) is left as is. */
|
|
52
|
-
function recordRefusal(dir, realAbsolute) {
|
|
53
|
-
try {
|
|
54
|
-
mkdirSync(dir, { recursive: true });
|
|
55
|
-
writeFileSync(markerFileOf(dir, realAbsolute), "", { flag: "wx" });
|
|
56
|
-
} catch (error) {
|
|
57
|
-
if (error.code !== "EEXIST") return; // No memory of this refusal just refuses the next read again too.
|
|
58
|
-
}
|
|
59
|
-
capMarkers(dir);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** True and consumed when `realAbsolute` was refused before: a second attempt passes, a third is refused anew. */
|
|
63
|
-
function consumeRefusal(dir, realAbsolute) {
|
|
64
|
-
try {
|
|
65
|
-
unlinkSync(markerFileOf(dir, realAbsolute));
|
|
66
|
-
return true;
|
|
67
|
-
} catch {
|
|
68
|
-
return false; // ENOENT (never refused), or a race that already consumed it — either way, not refused now.
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Keeps at most `MAX_REFUSED` markers, dropping the oldest by mtime. Ignores a concurrent unlink of the same file. */
|
|
73
|
-
function capMarkers(dir) {
|
|
74
|
-
let names;
|
|
75
|
-
try {
|
|
76
|
-
names = readdirSync(dir).filter((name) => name.endsWith(REFUSED_SUFFIX));
|
|
77
|
-
} catch {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const dated = names
|
|
81
|
-
.map((name) => {
|
|
82
|
-
try {
|
|
83
|
-
return { name, mtimeMs: statSync(path.join(dir, name)).mtimeMs };
|
|
84
|
-
} catch {
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
})
|
|
88
|
-
.filter((entry) => entry !== null)
|
|
89
|
-
.sort((a, b) => a.mtimeMs - b.mtimeMs);
|
|
90
|
-
for (const { name } of dated.slice(0, dated.length - MAX_REFUSED)) {
|
|
91
|
-
try {
|
|
92
|
-
unlinkSync(path.join(dir, name));
|
|
93
|
-
} catch {
|
|
94
|
-
// Already gone — another call raced this same cleanup, and the cap still holds.
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
26
|
function isBinaryByExtension(file) {
|
|
100
27
|
const ext = path.extname(file).slice(1).toLowerCase();
|
|
101
28
|
return BINARY_EXTENSIONS.has(ext);
|
|
@@ -148,14 +75,15 @@ function exceedsLineBudget(file, maxLines) {
|
|
|
148
75
|
}
|
|
149
76
|
}
|
|
150
77
|
|
|
151
|
-
function
|
|
152
|
-
const lines =
|
|
153
|
-
lines.push("
|
|
78
|
+
function readHint(tools) {
|
|
79
|
+
const lines = ["Explore hint: this whole-file read runs long. The read can proceed."];
|
|
80
|
+
if (tools.length > 0) lines.push("Optional explore tools:", ...exploreToolLines(tools));
|
|
81
|
+
lines.push("For a shorter read, you can use offset and limit.");
|
|
154
82
|
return lines.join("\n");
|
|
155
83
|
}
|
|
156
84
|
|
|
157
85
|
/** The verdict on one Read call. @returns the hook output, or null to let the call run with no message. */
|
|
158
|
-
export function decide(call
|
|
86
|
+
export function decide(call) {
|
|
159
87
|
if (call.hook_event_name !== "PreToolUse" || call.tool_name !== "Read") return null;
|
|
160
88
|
const input = call.tool_input ?? {};
|
|
161
89
|
const filePath = input.file_path;
|
|
@@ -166,7 +94,7 @@ export function decide(call, tmp = os.tmpdir()) {
|
|
|
166
94
|
settings = exploreSettingsAt(call.cwd ?? process.cwd());
|
|
167
95
|
} catch (error) {
|
|
168
96
|
// A config typo must not stop every read; the session sees the error and reports it.
|
|
169
|
-
return context(`Explore
|
|
97
|
+
return context(`Explore hint is off: ${error.message}`);
|
|
170
98
|
}
|
|
171
99
|
if (!settings) return null;
|
|
172
100
|
|
|
@@ -185,11 +113,7 @@ export function decide(call, tmp = os.tmpdir()) {
|
|
|
185
113
|
if (isBinaryByExtension(realAbsolute) || isBinaryByContent(realAbsolute)) return null;
|
|
186
114
|
if (!exceedsLineBudget(realAbsolute, settings.maxReadLines)) return null;
|
|
187
115
|
|
|
188
|
-
|
|
189
|
-
if (consumeRefusal(dir, realAbsolute)) return null;
|
|
190
|
-
|
|
191
|
-
recordRefusal(dir, realAbsolute);
|
|
192
|
-
return deny(refusalMessage(settings.tools));
|
|
116
|
+
return context(readHint(settings.tools));
|
|
193
117
|
}
|
|
194
118
|
|
|
195
119
|
async function readStdin() {
|
|
@@ -1,29 +1,25 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
2
|
import { execFileSync } from "node:child_process";
|
|
3
|
-
import {
|
|
4
|
-
import { existsSync, mkdirSync, mkdtempSync, readdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
5
4
|
import os from "node:os";
|
|
6
5
|
import path from "node:path";
|
|
7
6
|
import { test } from "node:test";
|
|
8
7
|
import { decide } from "./explore-guard.mjs";
|
|
9
|
-
import { slotDirOf } from "./dispatch-slot.mjs";
|
|
10
8
|
|
|
11
|
-
const TOOLS = [{ name: "
|
|
12
|
-
const markerNameOf = (absolute) => `${createHash("sha256").update(absolute).digest("hex")}.refused`;
|
|
9
|
+
const TOOLS = [{ name: "CocoIndex", use: "find the files for a concept", how: 'ccc search "<question>"' }];
|
|
13
10
|
|
|
14
|
-
/**
|
|
11
|
+
/** Temporary checkouts isolate the read hint from the repository configuration. */
|
|
15
12
|
function workspace(t, explore = { tools: TOOLS, maxReadLines: 5 }) {
|
|
16
13
|
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-explore-")));
|
|
17
14
|
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
18
15
|
const on = path.join(base, "on");
|
|
19
16
|
const plain = path.join(base, "plain");
|
|
20
|
-
const tmp = path.join(base, "tmp");
|
|
21
17
|
for (const dir of [on, plain]) {
|
|
22
18
|
mkdirSync(dir);
|
|
23
19
|
execFileSync("git", ["init", "-q"], { cwd: dir, stdio: "pipe" });
|
|
24
20
|
}
|
|
25
21
|
writeFileSync(path.join(on, "qc.config.json"), JSON.stringify({ swarm: { explore } }));
|
|
26
|
-
return { on, plain
|
|
22
|
+
return { on, plain };
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
const readCall = (cwd, file_path, extra = {}) => ({
|
|
@@ -40,47 +36,56 @@ const longFile = (dir, name = "long.txt", lines = 20) => {
|
|
|
40
36
|
return file;
|
|
41
37
|
};
|
|
42
38
|
|
|
43
|
-
|
|
44
|
-
result?.hookSpecificOutput?.permissionDecision === "deny" ? result.hookSpecificOutput.permissionDecisionReason : null;
|
|
45
|
-
|
|
46
|
-
test("a whole-file read of a long text file is refused, naming the tool and the retry", (t) => {
|
|
39
|
+
test("a whole-file read receives optional advice and proceeds on the first attempt", (t) => {
|
|
47
40
|
const ws = workspace(t);
|
|
48
41
|
const file = longFile(ws.on);
|
|
49
|
-
const
|
|
50
|
-
assert.
|
|
51
|
-
assert.
|
|
42
|
+
const output = decide(readCall(ws.on, file)).hookSpecificOutput;
|
|
43
|
+
assert.equal(output.hookEventName, "PreToolUse");
|
|
44
|
+
assert.equal(output.permissionDecision, undefined);
|
|
45
|
+
assert.match(output.additionalContext, /optional/i);
|
|
46
|
+
assert.match(output.additionalContext, /CocoIndex/);
|
|
52
47
|
});
|
|
53
48
|
|
|
54
|
-
test("
|
|
49
|
+
test("repeated whole-file reads proceed without a retry", (t) => {
|
|
55
50
|
const ws = workspace(t);
|
|
56
51
|
const file = longFile(ws.on);
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
for (let i = 0; i < 3; i += 1) {
|
|
53
|
+
const output = decide(readCall(ws.on, file)).hookSpecificOutput;
|
|
54
|
+
assert.equal(output.permissionDecision, undefined);
|
|
55
|
+
assert.ok(output.additionalContext);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("a long read with no configured tools receives range advice and proceeds", (t) => {
|
|
60
|
+
const ws = workspace(t, { maxReadLines: 5 });
|
|
61
|
+
const output = decide(readCall(ws.on, longFile(ws.on))).hookSpecificOutput;
|
|
62
|
+
assert.equal(output.permissionDecision, undefined);
|
|
63
|
+
assert.match(output.additionalContext, /offset and limit/);
|
|
59
64
|
});
|
|
60
65
|
|
|
61
66
|
test("a ranged read passes even on the first attempt", (t) => {
|
|
62
67
|
const ws = workspace(t);
|
|
63
68
|
const file = longFile(ws.on);
|
|
64
|
-
assert.equal(decide(readCall(ws.on, file, { offset: 1, limit: 5 })
|
|
69
|
+
assert.equal(decide(readCall(ws.on, file, { offset: 1, limit: 5 })), null);
|
|
65
70
|
});
|
|
66
71
|
|
|
67
72
|
test("an exempt path passes", (t) => {
|
|
68
73
|
const ws = workspace(t, { tools: TOOLS, maxReadLines: 5, exempt: ["**/long.txt"] });
|
|
69
74
|
const file = longFile(ws.on);
|
|
70
|
-
assert.equal(decide(readCall(ws.on, file)
|
|
75
|
+
assert.equal(decide(readCall(ws.on, file)), null);
|
|
71
76
|
});
|
|
72
77
|
|
|
73
78
|
test("a file within the line budget passes", (t) => {
|
|
74
79
|
const ws = workspace(t);
|
|
75
80
|
const file = longFile(ws.on, "short.txt", 3);
|
|
76
|
-
assert.equal(decide(readCall(ws.on, file)
|
|
81
|
+
assert.equal(decide(readCall(ws.on, file)), null);
|
|
77
82
|
});
|
|
78
83
|
|
|
79
84
|
test("a binary file by extension passes with no line count", (t) => {
|
|
80
85
|
const ws = workspace(t);
|
|
81
86
|
const file = path.join(ws.on, "image.png");
|
|
82
87
|
writeFileSync(file, Buffer.from(Array.from({ length: 20 }, (_, i) => `line ${i}\n`).join(""), "utf8"));
|
|
83
|
-
assert.equal(decide(readCall(ws.on, file)
|
|
88
|
+
assert.equal(decide(readCall(ws.on, file)), null);
|
|
84
89
|
});
|
|
85
90
|
|
|
86
91
|
test("a binary file by a NUL byte passes even with an unlisted extension", (t) => {
|
|
@@ -88,106 +93,38 @@ test("a binary file by a NUL byte passes even with an unlisted extension", (t) =
|
|
|
88
93
|
const file = path.join(ws.on, "data.bin");
|
|
89
94
|
const body = Array.from({ length: 20 }, (_, i) => `line ${i}\n`).join("");
|
|
90
95
|
writeFileSync(file, Buffer.concat([Buffer.from(body, "utf8"), Buffer.from([0])]));
|
|
91
|
-
assert.equal(decide(readCall(ws.on, file)
|
|
96
|
+
assert.equal(decide(readCall(ws.on, file)), null);
|
|
92
97
|
});
|
|
93
98
|
|
|
94
99
|
test("a checkout with no swarm.explore section sees no change", (t) => {
|
|
95
100
|
const ws = workspace(t);
|
|
96
101
|
const file = longFile(ws.plain);
|
|
97
|
-
assert.equal(decide(readCall(ws.plain, file)
|
|
102
|
+
assert.equal(decide(readCall(ws.plain, file)), null);
|
|
98
103
|
});
|
|
99
104
|
|
|
100
105
|
test("a bad key reports the error as context instead of refusing", (t) => {
|
|
101
106
|
const ws = workspace(t, { maxReadLines: "many" });
|
|
102
107
|
const file = longFile(ws.on);
|
|
103
|
-
const result = decide(readCall(ws.on, file)
|
|
108
|
+
const result = decide(readCall(ws.on, file));
|
|
104
109
|
assert.match(result.hookSpecificOutput.additionalContext, /swarm\.explore\.maxReadLines/);
|
|
105
110
|
});
|
|
106
111
|
|
|
107
|
-
test("a refusal leaves one marker file named by the sha256 of the absolute path, nothing else", (t) => {
|
|
108
|
-
const ws = workspace(t);
|
|
109
|
-
const file = longFile(ws.on);
|
|
110
|
-
assert.notEqual(decide(readCall(ws.on, file), ws.tmp), null);
|
|
111
|
-
const dir = slotDirOf("session-aaaa", ws.tmp);
|
|
112
|
-
assert.deepEqual(readdirSync(dir), [markerNameOf(file)]);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test("two refusals of different files each leave their own marker; consuming one leaves the other", (t) => {
|
|
116
|
-
const ws = workspace(t);
|
|
117
|
-
const fileA = longFile(ws.on, "a.txt");
|
|
118
|
-
const fileB = longFile(ws.on, "b.txt");
|
|
119
|
-
assert.notEqual(decide(readCall(ws.on, fileA), ws.tmp), null);
|
|
120
|
-
assert.notEqual(decide(readCall(ws.on, fileB), ws.tmp), null);
|
|
121
|
-
const dir = slotDirOf("session-aaaa", ws.tmp);
|
|
122
|
-
assert.deepEqual(new Set(readdirSync(dir)), new Set([markerNameOf(fileA), markerNameOf(fileB)]));
|
|
123
|
-
|
|
124
|
-
assert.equal(decide(readCall(ws.on, fileA), ws.tmp), null);
|
|
125
|
-
assert.deepEqual(readdirSync(dir), [markerNameOf(fileB)]);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
test("the session state folder holds only marker files, never a shared list file", (t) => {
|
|
129
|
-
const ws = workspace(t);
|
|
130
|
-
const fileA = longFile(ws.on, "a.txt");
|
|
131
|
-
const fileB = longFile(ws.on, "b.txt");
|
|
132
|
-
decide(readCall(ws.on, fileA), ws.tmp);
|
|
133
|
-
decide(readCall(ws.on, fileB), ws.tmp);
|
|
134
|
-
const dir = slotDirOf("session-aaaa", ws.tmp);
|
|
135
|
-
for (const name of readdirSync(dir)) assert.match(name, /^[0-9a-f]{64}\.refused$/);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
test("a third whole read after a consumed marker is refused again", (t) => {
|
|
139
|
-
const ws = workspace(t);
|
|
140
|
-
const file = longFile(ws.on);
|
|
141
|
-
assert.notEqual(decide(readCall(ws.on, file), ws.tmp), null); // first: refused, marker recorded
|
|
142
|
-
assert.equal(decide(readCall(ws.on, file), ws.tmp), null); // second: passes, marker consumed
|
|
143
|
-
const dir = slotDirOf("session-aaaa", ws.tmp);
|
|
144
|
-
assert.equal(existsSync(path.join(dir, markerNameOf(file))), false);
|
|
145
|
-
assert.notEqual(decide(readCall(ws.on, file), ws.tmp), null); // third: refused again
|
|
146
|
-
});
|
|
147
|
-
|
|
148
112
|
test("a file outside the checkout passes with no judgment", (t) => {
|
|
149
113
|
const ws = workspace(t);
|
|
150
114
|
const outside = mkdtempSync(path.join(os.tmpdir(), "qc-outside-"));
|
|
151
115
|
t.after(() => rmSync(outside, { recursive: true, force: true }));
|
|
152
116
|
const file = longFile(outside);
|
|
153
|
-
assert.equal(decide(readCall(ws.on, file)
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
test("a respelled path is refused on the first whole read, then a respelled retry consumes the marker", (t) => {
|
|
157
|
-
const ws = workspace(t);
|
|
158
|
-
const file = longFile(ws.on);
|
|
159
|
-
|
|
160
|
-
let respelled;
|
|
161
|
-
if (os.platform() === "win32") {
|
|
162
|
-
const drive = file[0];
|
|
163
|
-
respelled = (drive === drive.toUpperCase() ? drive.toLowerCase() : drive.toUpperCase()) + file.slice(1);
|
|
164
|
-
} else {
|
|
165
|
-
const link = path.join(path.dirname(ws.on), "link-to-on");
|
|
166
|
-
try {
|
|
167
|
-
symlinkSync(ws.on, link, "dir");
|
|
168
|
-
} catch (error) {
|
|
169
|
-
if (error.code === "EPERM") return t.skip("no permission to create a symlink");
|
|
170
|
-
throw error;
|
|
171
|
-
}
|
|
172
|
-
t.after(() => rmSync(link, { force: true }));
|
|
173
|
-
respelled = path.join(link, path.basename(file));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
assert.notEqual(decide(readCall(ws.on, file), ws.tmp), null); // first, as spelled: refused, marker recorded under the real path
|
|
177
|
-
assert.equal(decide(readCall(ws.on, respelled), ws.tmp), null); // retry, respelled: passes, marker consumed
|
|
178
|
-
|
|
179
|
-
const dir = slotDirOf("session-aaaa", ws.tmp);
|
|
180
|
-
assert.deepEqual(readdirSync(dir), []); // nothing left behind, real or respelled
|
|
117
|
+
assert.equal(decide(readCall(ws.on, file)), null);
|
|
181
118
|
});
|
|
182
119
|
|
|
183
120
|
test("a file in a different checkout passes even though the session's own checkout turns the guard on", (t) => {
|
|
184
121
|
const ws = workspace(t);
|
|
185
122
|
const file = longFile(ws.plain);
|
|
186
|
-
assert.equal(decide(readCall(ws.on, file)
|
|
123
|
+
assert.equal(decide(readCall(ws.on, file)), null);
|
|
187
124
|
});
|
|
188
125
|
|
|
189
126
|
test("a non-Read call, or a Read with no file_path, sees no change", (t) => {
|
|
190
127
|
const ws = workspace(t);
|
|
191
|
-
assert.equal(decide({ hook_event_name: "PreToolUse", tool_name: "Edit", tool_input: {} }
|
|
192
|
-
assert.equal(decide({ hook_event_name: "PreToolUse", tool_name: "Read", tool_input: {}, cwd: ws.on }
|
|
128
|
+
assert.equal(decide({ hook_event_name: "PreToolUse", tool_name: "Edit", tool_input: {} }), null);
|
|
129
|
+
assert.equal(decide({ hook_event_name: "PreToolUse", tool_name: "Read", tool_input: {}, cwd: ws.on }), null);
|
|
193
130
|
});
|
package/src/cli/explore-hint.mjs
CHANGED
|
@@ -43,8 +43,8 @@ function lineCountOf(text) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const grepHint = (tools) =>
|
|
46
|
-
`Explore hint: this Grep answer runs long.
|
|
47
|
-
`${tools.map((tool) => tool.name).join(", ")}.`;
|
|
46
|
+
`Explore hint: this Grep answer runs long. Optional explore tools: ` +
|
|
47
|
+
`${tools.map((tool) => tool.name).join(", ")}. Grep and shell searches remain available.`;
|
|
48
48
|
|
|
49
49
|
const outputHint = (summarizer) => `Explore hint: this output runs long. Pipe it through the summarizer instead: ${summarizer}`;
|
|
50
50
|
|
|
@@ -114,7 +114,7 @@ export function decide(call, tmp = os.tmpdir()) {
|
|
|
114
114
|
|
|
115
115
|
const text = textOf(call.tool_response);
|
|
116
116
|
if (tool === "Grep") {
|
|
117
|
-
return lineCountOf(text) > settings.maxGrepLines ? context(grepHint(settings.tools)) : null;
|
|
117
|
+
return settings.tools.length > 0 && lineCountOf(text) > settings.maxGrepLines ? context(grepHint(settings.tools)) : null;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
if (text.length <= settings.maxOutputChars) return null;
|
|
@@ -9,7 +9,7 @@ import { decide, excerptOf, replacedOutput } from "./explore-hint.mjs";
|
|
|
9
9
|
|
|
10
10
|
const HOOKS = fileURLToPath(new URL("../../../../hooks/hooks.json", import.meta.url));
|
|
11
11
|
|
|
12
|
-
const TOOLS = [{ name: "
|
|
12
|
+
const TOOLS = [{ name: "CocoIndex", use: "find the files for a concept", how: 'ccc search "<question>"' }];
|
|
13
13
|
|
|
14
14
|
/** A checkout with `swarm.explore` on, and one with no config. */
|
|
15
15
|
function workspace(t, explore = { tools: TOOLS, maxGrepLines: 5, maxOutputChars: 20, longOutput: "hint", summarizer: "lfm-ask" }) {
|
|
@@ -40,7 +40,13 @@ const lines = (n) => Array.from({ length: n }, (_, i) => `match ${i}`).join("\n"
|
|
|
40
40
|
test("a Grep answer over maxGrepLines adds context naming the tools", (t) => {
|
|
41
41
|
const ws = workspace(t);
|
|
42
42
|
const text = contextOf(decide(call(ws.on, "Grep", lines(10))));
|
|
43
|
-
assert.match(text, /
|
|
43
|
+
assert.match(text, /CocoIndex/);
|
|
44
|
+
assert.match(text, /optional/i);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("a long Grep answer with no tools keeps its output and receives no tool hint", (t) => {
|
|
48
|
+
const ws = workspace(t, { maxGrepLines: 5 });
|
|
49
|
+
assert.equal(decide(call(ws.on, "Grep", lines(10))), null);
|
|
44
50
|
});
|
|
45
51
|
|
|
46
52
|
test("a Grep answer at or under maxGrepLines sees no change", (t) => {
|
|
@@ -91,20 +97,23 @@ test("a bad key reports the error as context instead of a hint", (t) => {
|
|
|
91
97
|
test("a Grep { content } shaped answer over maxGrepLines adds context naming the tools", (t) => {
|
|
92
98
|
const ws = workspace(t);
|
|
93
99
|
const text = contextOf(decide(call(ws.on, "Grep", { content: lines(10) })));
|
|
94
|
-
assert.match(text, /
|
|
100
|
+
assert.match(text, /CocoIndex/);
|
|
101
|
+
assert.match(text, /optional/i);
|
|
95
102
|
});
|
|
96
103
|
|
|
97
104
|
test("a Grep { matches } shaped answer over maxGrepLines adds context naming the tools", (t) => {
|
|
98
105
|
const ws = workspace(t);
|
|
99
106
|
const matches = Array.from({ length: 10 }, (_, i) => `match ${i}`);
|
|
100
107
|
const text = contextOf(decide(call(ws.on, "Grep", { matches })));
|
|
101
|
-
assert.match(text, /
|
|
108
|
+
assert.match(text, /CocoIndex/);
|
|
109
|
+
assert.match(text, /optional/i);
|
|
102
110
|
});
|
|
103
111
|
|
|
104
112
|
test("a Grep answer of unknown shape counts real newlines and escaped \\n sequences", (t) => {
|
|
105
113
|
const ws = workspace(t);
|
|
106
114
|
const text = contextOf(decide(call(ws.on, "Grep", { blob: lines(10) })));
|
|
107
|
-
assert.match(text, /
|
|
115
|
+
assert.match(text, /CocoIndex/);
|
|
116
|
+
assert.match(text, /optional/i);
|
|
108
117
|
});
|
|
109
118
|
|
|
110
119
|
test("a Bash { stdout, stderr } shaped answer over maxOutputChars adds context naming the summarizer", (t) => {
|
package/src/cli/init.mjs
CHANGED
|
@@ -68,6 +68,17 @@ async function addScripts(config) {
|
|
|
68
68
|
return [{ to: file, status: "written" }];
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/** Adds the worktree folder to `.gitignore` unless a line already names it, so no worktree shows in `git status`. QC-015. */
|
|
72
|
+
async function ignoreWorktrees(config) {
|
|
73
|
+
const file = path.join(config.root, ".gitignore");
|
|
74
|
+
const dir = config.worktree.dir.replace(/\/+$/, "");
|
|
75
|
+
const text = existsSync(file) ? await readFile(file, "utf8") : "";
|
|
76
|
+
const spellings = new Set([dir, `${dir}/`, `/${dir}`, `/${dir}/`]);
|
|
77
|
+
if (text.split(/\r?\n/).some((line) => spellings.has(line.trim()))) return { to: file, status: "kept" };
|
|
78
|
+
await writeFile(file, `${text}${text === "" || text.endsWith("\n") ? "" : "\n"}${dir}/\n`);
|
|
79
|
+
return { to: file, status: "written" };
|
|
80
|
+
}
|
|
81
|
+
|
|
71
82
|
export async function runInit(config, args = []) {
|
|
72
83
|
const force = args.includes("--force");
|
|
73
84
|
const results = [];
|
|
@@ -83,6 +94,7 @@ export async function runInit(config, args = []) {
|
|
|
83
94
|
// The docs and the CI workflow both tell a reader to run these, so `init` puts them
|
|
84
95
|
// where a reader will look rather than leaving the instruction dangling.
|
|
85
96
|
results.push(...(await addScripts(config)));
|
|
97
|
+
results.push(await ignoreWorktrees(config));
|
|
86
98
|
|
|
87
99
|
for (const { to, status } of results) {
|
|
88
100
|
console.log(`${status.padEnd(11)} ${path.relative(config.root, to)}`);
|
package/src/cli/merge-guard.mjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// The trigger that holds `gh pr merge` to its review. Before the call it needs `--match-head-commit` and an
|
|
3
3
|
// APPROVED review of `swarm.review.merge` for that sha. After the call it records the merge and its issues.
|
|
4
4
|
// A merge that names another repository than the checkout's `origin` is that repository's own, so it passes.
|
|
5
|
+
// With `swarm.dispatch` off, the record is still written for the worktree check of QC-015, and nothing is judged.
|
|
5
6
|
|
|
6
7
|
import { spawnSync } from "node:child_process";
|
|
7
8
|
import { fileURLToPath } from "node:url";
|
|
@@ -10,6 +11,7 @@ import { ghApiMerges, ghMerges } from "./gh-merge-command.mjs";
|
|
|
10
11
|
import { runGh } from "./gh-run.mjs";
|
|
11
12
|
import { appendRecord, latestVerdictFor, readLedger } from "./ledger.mjs";
|
|
12
13
|
import { workflowAt } from "./workflow-settings.mjs";
|
|
14
|
+
import { worktreeRuleAt } from "./worktree-settings.mjs";
|
|
13
15
|
|
|
14
16
|
const VIEW_FIELDS = "number,url,state,mergedAt,baseRefName,headRefName,body,closingIssuesReferences";
|
|
15
17
|
const MAYBE_GH = /\bgh(?:\.exe)?\b/i;
|
|
@@ -138,6 +140,8 @@ export function decide(call, { gh = runGh } = {}) {
|
|
|
138
140
|
} catch (error) {
|
|
139
141
|
return event === "PreToolUse" ? context(`Merge guard is off: ${error.message}`) : null;
|
|
140
142
|
}
|
|
143
|
+
// Only the record needs a ledger; the review check stays off with the workflow.
|
|
144
|
+
if (!workflow && event !== "PreToolUse") workflow = worktreeRuleAt(call.cwd ?? process.cwd());
|
|
141
145
|
if (!workflow) return null;
|
|
142
146
|
if (event === "PreToolUse") {
|
|
143
147
|
const [found] = ghApiMerges(command, parse, { cwd: call.cwd ?? process.cwd() });
|
|
@@ -58,13 +58,25 @@ const MERGED = JSON.stringify({
|
|
|
58
58
|
],
|
|
59
59
|
});
|
|
60
60
|
|
|
61
|
-
test("with swarm.dispatch off, a merge is
|
|
61
|
+
test("with swarm.dispatch off, a merge is not judged, and the merge is recorded for the worktree check", (t) => {
|
|
62
62
|
const ws = workspace(t);
|
|
63
63
|
const { gh, calls } = fakeGh(MERGED);
|
|
64
64
|
assert.equal(decide(shell(ws.off, "gh pr merge 60"), { gh }), null);
|
|
65
|
-
assert.equal(decide(shell(ws.off, "gh pr merge 60", "PostToolUse"), { gh }), null);
|
|
66
65
|
assert.equal(decide(shell(ws.off, "gh api -X PUT repos/o/r/pulls/60/merge"), { gh }), null);
|
|
66
|
+
assert.equal(calls.length, 0, "a PreToolUse call reads nothing");
|
|
67
|
+
assert.equal(decide(shell(ws.off, "gh pr merge 60", "PostToolUse"), { gh }), null);
|
|
68
|
+
const records = readLedger(ws.ledgerOf(ws.off));
|
|
69
|
+
assert.deepEqual(records.map(({ type, pr, repo, branch }) => ({ type, pr, repo, branch })), [{ type: "merge", pr: 60, repo: "o/r", branch: "feat/62-x" }]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("with swarm.dispatch off and worktree.enforce false, a merge is neither judged nor recorded", (t) => {
|
|
73
|
+
const ws = workspace(t);
|
|
74
|
+
writeFileSync(path.join(ws.off, "qc.config.json"), JSON.stringify({ swarm: {}, worktree: { enforce: false } }));
|
|
75
|
+
const { gh, calls } = fakeGh(MERGED);
|
|
76
|
+
assert.equal(decide(shell(ws.off, "gh pr merge 60"), { gh }), null);
|
|
77
|
+
assert.equal(decide(shell(ws.off, "gh pr merge 60", "PostToolUse"), { gh }), null);
|
|
67
78
|
assert.equal(calls.length, 0);
|
|
79
|
+
assert.equal(existsSync(ws.ledgerOf(ws.off)), false);
|
|
68
80
|
});
|
|
69
81
|
|
|
70
82
|
test("a merge without --match-head-commit is refused", (t) => {
|
|
@@ -326,3 +338,10 @@ test("a gh api call to a repos/ path whose body file quotes the mutation is allo
|
|
|
326
338
|
assert.equal(decide(shell(ws.on, "gh api repos/o/r/issues/64/comments -F body=@review.md")), null);
|
|
327
339
|
assert.match(denied(decide(shell(ws.on, "gh api graphql -F query=@review.md"))) ?? "", /gh pr merge <n> --match-head-commit <sha>/);
|
|
328
340
|
});
|
|
341
|
+
|
|
342
|
+
test("with swarm.dispatch off, a merge whose command failed after GitHub merged is still recorded", (t) => {
|
|
343
|
+
const ws = workspace(t);
|
|
344
|
+
// `gh pr merge --delete-branch` exits non-zero when a worktree holds the branch, after the merge itself.
|
|
345
|
+
assert.equal(decide(shell(ws.off, "gh pr merge 60 --squash --delete-branch", "PostToolUseFailure"), { gh: fakeGh(MERGED).gh }), null);
|
|
346
|
+
assert.deepEqual(readLedger(ws.ledgerOf(ws.off)).map(({ type, pr }) => ({ type, pr })), [{ type: "merge", pr: 60 }]);
|
|
347
|
+
});
|
|
@@ -41,10 +41,10 @@ const ALWAYS_PRESENT = ["Read", "Bash"];
|
|
|
41
41
|
const READ_TOOLS = ["Read", "Grep", "Glob", "Bash", "PowerShell"];
|
|
42
42
|
const EDIT_TOOLS = [...READ_TOOLS, "Skill", "Edit", "Write"];
|
|
43
43
|
const EXPECTED = {
|
|
44
|
-
"sdd-implementer": { model: "
|
|
45
|
-
"sdd-planner": { model: "
|
|
46
|
-
"sdd-reviewer": { model: "
|
|
47
|
-
"sdd-branch-reviewer": { model: "
|
|
44
|
+
"sdd-implementer": { model: "gpt-6.1-sol", effort: "high", tools: EDIT_TOOLS },
|
|
45
|
+
"sdd-planner": { model: "gpt-6.1-sol", effort: "high", tools: EDIT_TOOLS },
|
|
46
|
+
"sdd-reviewer": { model: "gpt-6.1-sol", effort: "high", tools: READ_TOOLS },
|
|
47
|
+
"sdd-branch-reviewer": { model: "gpt-6.1-sol", effort: "high", tools: READ_TOOLS },
|
|
48
48
|
};
|
|
49
49
|
// Fields Claude Code ignores on a plugin agent, and the one that drops a repository's CLAUDE.md and rules.
|
|
50
50
|
const FORBIDDEN_FIELDS = ["disallowedTools", "hooks", "mcpServers", "permissionMode", "omitClaudeMd"];
|
|
@@ -23,7 +23,7 @@ const LOCATION_NAMES = new Set(["cd", "chdir", "sl", "set-location", "pushd", "p
|
|
|
23
23
|
const PATH_PARAMETERS = new Set(["-path", "-literalpath"]);
|
|
24
24
|
|
|
25
25
|
/** @returns the directory a location change moves to, or null when the segment is none. */
|
|
26
|
-
function locationTarget(segment) {
|
|
26
|
+
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++) {
|
package/src/cli/qc.mjs
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
import process from "node:process";
|
|
5
5
|
import { load } from "../config.mjs";
|
|
6
6
|
import { runCheck } from "./check.mjs";
|
|
7
|
+
import { checkFiles } from "./check-input.mjs";
|
|
7
8
|
|
|
8
9
|
const USAGE = `qc — architecture gates
|
|
9
10
|
|
|
10
|
-
qc check [file...]
|
|
11
|
+
qc check [file...] [--files-from <path|->]
|
|
12
|
+
every structural gate; files given, or listed NUL-separated in a file or on stdin, take the fast path
|
|
11
13
|
qc init scaffold the docs, config, hooks and workflow into this repository
|
|
12
14
|
qc feature <name> scaffold a feature in the configured anatomy
|
|
13
15
|
qc run [name] drive one feature command headless; no name lists them
|
|
@@ -25,9 +27,9 @@ const USAGE = `qc — architecture gates
|
|
|
25
27
|
qc tokens [--session <id>] [--project <dir>] [--claude-dir <dir>] [--json]
|
|
26
28
|
one session's tokens and cache hit rate per agent type, from its transcripts
|
|
27
29
|
qc worktree add <name> <branch> [--from <ref>]
|
|
28
|
-
fetch, add
|
|
30
|
+
fetch, add .worktree/<name> in the main checkout, install, print the path
|
|
29
31
|
qc worktree remove <name>
|
|
30
|
-
refuse a dirty tree; delete it, then its branch once merged or
|
|
32
|
+
refuse a dirty tree; delete it, then its branch once merged, pushed, or a merged PR head
|
|
31
33
|
qc enforcement-map refresh the generated rule/gate table in docs/enforcement.md
|
|
32
34
|
qc doctor the hooks and the lockfile must run the same harness
|
|
33
35
|
qc config print the resolved configuration
|
|
@@ -45,7 +47,10 @@ async function main() {
|
|
|
45
47
|
|
|
46
48
|
switch (command) {
|
|
47
49
|
case "check": {
|
|
48
|
-
const {
|
|
50
|
+
const { files, listed } = await checkFiles(rest);
|
|
51
|
+
// An empty list means nothing staged to check, not the full check.
|
|
52
|
+
if (listed && files.length === 0) return;
|
|
53
|
+
const { problems, lines } = await runCheck(config, files);
|
|
49
54
|
if (problems.length > 0) {
|
|
50
55
|
for (const problem of problems) {
|
|
51
56
|
console.error(`FAIL ${problem.feature ?? problem.path} ${problem.rule}: ${problem.detail}`);
|
|
@@ -25,6 +25,7 @@ test("hooks.json runs each workflow check on its events", () => {
|
|
|
25
25
|
assert.deepEqual(matchersOf("SubagentStop", script), [undefined], script);
|
|
26
26
|
}
|
|
27
27
|
assert.deepEqual(matchersOf("Stop", "issue-gate.mjs"), [undefined]);
|
|
28
|
+
assert.deepEqual(matchersOf("Stop", "worktree-gate.mjs"), [undefined]);
|
|
28
29
|
});
|
|
29
30
|
|
|
30
31
|
test("every node script hooks.json names exists", () => {
|