architecture-harness 0.19.0 → 0.20.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 +1 -1
- package/src/cli/controller-guard.mjs +16 -9
- package/src/cli/controller-guard.test.mjs +44 -0
- package/src/cli/direct-lane.mjs +209 -0
- package/src/cli/direct-lane.test.mjs +196 -0
- package/src/cli/dispatch-guard.test.mjs +6 -23
- package/src/cli/dispatch-reminder.mjs +19 -4
- package/src/cli/dispatch-reminder.test.mjs +36 -15
- 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/gh-run.mjs +2 -2
- package/src/cli/git-read.mjs +3 -3
- package/src/cli/merge-guard.mjs +70 -21
- package/src/cli/merge-guard.test.mjs +57 -0
- package/src/cli/plugin-agents.test.mjs +4 -4
- package/src/cli/workflow-settings.mjs +18 -1
- package/src/config.mjs +26 -12
- package/src/glob.mjs +7 -0
- package/templates/docs/enforcement.md +2 -2
|
@@ -33,7 +33,7 @@ test("a checkout with no config, or no dispatch section, gets no note", (t) => {
|
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
test("with the section on, every start adds the note, a compaction included", (t) => {
|
|
36
|
-
const [on] = checkouts(t, [{ swarm: { dispatch: {} } }]);
|
|
36
|
+
const [on] = checkouts(t, [{ swarm: { direct: false, dispatch: {} } }]);
|
|
37
37
|
for (const source of ["startup", "resume", "clear", "compact", "fork"]) {
|
|
38
38
|
const output = decide(start(on, source));
|
|
39
39
|
assert.equal(output.hookSpecificOutput.hookEventName, "SessionStart", source);
|
|
@@ -49,6 +49,8 @@ test("the note names the workflow, the four types, the model tiers, and the one-
|
|
|
49
49
|
"architecture:sdd-reviewer",
|
|
50
50
|
"architecture:sdd-branch-reviewer",
|
|
51
51
|
"one implementer at a time",
|
|
52
|
+
"gpt-6.1-sol",
|
|
53
|
+
"high reasoning effort",
|
|
52
54
|
"Worktree: <path>",
|
|
53
55
|
"VERDICT: <APPROVED|CHANGES_REQUIRED> <sha>",
|
|
54
56
|
"--match-head-commit <sha>",
|
|
@@ -60,35 +62,38 @@ test("the note names the workflow, the four types, the model tiers, and the one-
|
|
|
60
62
|
|
|
61
63
|
test("with swarm.explore also on, the note names its tools", (t) => {
|
|
62
64
|
const [on] = checkouts(t, [
|
|
63
|
-
{ swarm: { dispatch: {}, explore: { tools: [{ name: "
|
|
65
|
+
{ swarm: { dispatch: {}, explore: { tools: [{ name: "CocoIndex", use: "u", how: "h" }, { name: "GitNexus", use: "u", how: "h" }] } } },
|
|
64
66
|
]);
|
|
65
67
|
const text = decide(start(on, "startup")).hookSpecificOutput.additionalContext;
|
|
66
68
|
assert.ok(text.startsWith(REMINDER));
|
|
67
|
-
assert.match(text, /
|
|
69
|
+
assert.match(text, /CocoIndex, GitNexus/);
|
|
70
|
+
assert.match(text, /optional/i);
|
|
68
71
|
});
|
|
69
72
|
|
|
70
73
|
test("with swarm.explore off, or naming no tools, the note is exactly REMINDER", (t) => {
|
|
71
74
|
const [off, empty] = checkouts(t, [
|
|
72
|
-
{ swarm: { dispatch: {} } },
|
|
73
|
-
{ swarm: { dispatch: {}, explore: { tools: [] } } },
|
|
75
|
+
{ swarm: { direct: false, dispatch: {} } },
|
|
76
|
+
{ swarm: { direct: false, dispatch: {}, explore: { tools: [] } } },
|
|
74
77
|
]);
|
|
75
78
|
assert.equal(decide(start(off, "startup")).hookSpecificOutput.additionalContext, REMINDER);
|
|
76
79
|
assert.equal(decide(start(empty, "startup")).hookSpecificOutput.additionalContext, REMINDER);
|
|
77
80
|
});
|
|
78
81
|
|
|
79
82
|
test("with swarm.explore on and swarm.dispatch off, the SessionStart note still names the tools", (t) => {
|
|
80
|
-
const [on] = checkouts(t, [{ swarm: { explore: { tools: [{ name: "
|
|
83
|
+
const [on] = checkouts(t, [{ swarm: { explore: { tools: [{ name: "CocoIndex", use: "u", how: "h" }] } } }]);
|
|
81
84
|
const output = decide(start(on, "startup"));
|
|
82
85
|
assert.equal(output.hookSpecificOutput.hookEventName, "SessionStart");
|
|
83
|
-
assert.match(output.hookSpecificOutput.additionalContext, /
|
|
86
|
+
assert.match(output.hookSpecificOutput.additionalContext, /CocoIndex/);
|
|
87
|
+
assert.match(output.hookSpecificOutput.additionalContext, /optional/i);
|
|
84
88
|
assert.ok(!output.hookSpecificOutput.additionalContext.startsWith(" "));
|
|
85
89
|
});
|
|
86
90
|
|
|
87
91
|
test("SubagentStart names the tools when swarm.explore is on, independent of swarm.dispatch", (t) => {
|
|
88
|
-
const [on] = checkouts(t, [{ swarm: { explore: { tools: [{ name: "
|
|
92
|
+
const [on] = checkouts(t, [{ swarm: { explore: { tools: [{ name: "CocoIndex", use: "u", how: "h" }] } } }]);
|
|
89
93
|
const output = decide(subagentStart(on));
|
|
90
94
|
assert.equal(output.hookSpecificOutput.hookEventName, "SubagentStart");
|
|
91
|
-
assert.match(output.hookSpecificOutput.additionalContext, /
|
|
95
|
+
assert.match(output.hookSpecificOutput.additionalContext, /CocoIndex/);
|
|
96
|
+
assert.match(output.hookSpecificOutput.additionalContext, /optional/i);
|
|
92
97
|
});
|
|
93
98
|
|
|
94
99
|
test("SubagentStart with no swarm.explore section, or none naming tools, is a no-op", (t) => {
|
|
@@ -104,7 +109,7 @@ test("a config error reaches the session as context", (t) => {
|
|
|
104
109
|
});
|
|
105
110
|
|
|
106
111
|
test("the hook runs as its own process and prints the note as JSON, or nothing with no config", (t) => {
|
|
107
|
-
const [on, plain] = checkouts(t, [{ swarm: { dispatch: {} } }, null]);
|
|
112
|
+
const [on, plain] = checkouts(t, [{ swarm: { direct: false, dispatch: {} } }, null]);
|
|
108
113
|
const run = (cwd) => spawnSync(process.execPath, [NOTE], { input: JSON.stringify(start(cwd, "startup")), encoding: "utf8" });
|
|
109
114
|
const noted = run(on);
|
|
110
115
|
assert.equal(noted.status, 0, noted.stderr);
|
|
@@ -136,7 +141,7 @@ test("hooks.json also runs the note on SubagentStart, all matchers, beside the e
|
|
|
136
141
|
test("a limit above 1 adds one line stating it, and a limit of 1 leaves the note as REMINDER", (t) => {
|
|
137
142
|
const [three, one] = checkouts(t, [
|
|
138
143
|
{ swarm: { dispatch: { implementerSlots: 3 } } },
|
|
139
|
-
{ swarm: { dispatch: { implementerSlots: 1 } } },
|
|
144
|
+
{ swarm: { direct: false, dispatch: { implementerSlots: 1 } } },
|
|
140
145
|
]);
|
|
141
146
|
const text = decide(start(three, "startup")).hookSpecificOutput.additionalContext;
|
|
142
147
|
assert.ok(text.startsWith(REMINDER));
|
|
@@ -150,7 +155,7 @@ test("SubagentStart gives each tool's use and command, in config order, since a
|
|
|
150
155
|
swarm: {
|
|
151
156
|
explore: {
|
|
152
157
|
tools: [
|
|
153
|
-
{ name: "
|
|
158
|
+
{ name: "CocoIndex", use: "find the files for a concept", how: 'ccc search "<question>"' },
|
|
154
159
|
{ name: "GitNexus", use: "callers and callees", how: "gitnexus context <symbol> -r repo" },
|
|
155
160
|
],
|
|
156
161
|
},
|
|
@@ -160,15 +165,31 @@ test("SubagentStart gives each tool's use and command, in config order, since a
|
|
|
160
165
|
assert.equal(
|
|
161
166
|
decide(subagentStart(on)).hookSpecificOutput.additionalContext,
|
|
162
167
|
[
|
|
163
|
-
"
|
|
164
|
-
'-
|
|
168
|
+
"Optional explore tools, and how to run each. Read, Grep, and shell searches remain available:",
|
|
169
|
+
'- CocoIndex: find the files for a concept (ccc search "<question>")',
|
|
165
170
|
"- GitNexus: callers and callees (gitnexus context <symbol> -r repo)",
|
|
166
171
|
].join("\n"),
|
|
167
172
|
);
|
|
168
173
|
});
|
|
169
174
|
|
|
170
175
|
test("a swarm.explore section that does not parse leaves SubagentStart silent and the session note as REMINDER", (t) => {
|
|
171
|
-
const [bad] = checkouts(t, [{ swarm: { dispatch: {}, explore: { tools: "
|
|
176
|
+
const [bad] = checkouts(t, [{ swarm: { direct: false, dispatch: {}, explore: { tools: "CocoIndex" } } }]);
|
|
172
177
|
assert.equal(decide(subagentStart(bad)), null);
|
|
173
178
|
assert.equal(decide(start(bad, "startup")).hookSpecificOutput.additionalContext, REMINDER);
|
|
174
179
|
});
|
|
180
|
+
|
|
181
|
+
test("with the direct lane on by default, the note names its limits after REMINDER", (t) => {
|
|
182
|
+
const [lane, custom] = checkouts(t, [{ swarm: { dispatch: {} } }, { swarm: { dispatch: {}, direct: { maxLines: 5, maxFiles: 1 } } }]);
|
|
183
|
+
const text = decide(start(lane, "startup")).hookSpecificOutput.additionalContext;
|
|
184
|
+
assert.ok(text.startsWith(REMINDER));
|
|
185
|
+
assert.match(text, /at most 20 changed lines in 2 files needs no subagent/);
|
|
186
|
+
assert.match(text, /--match-head-commit <sha>/);
|
|
187
|
+
assert.match(decide(start(custom, "startup")).hookSpecificOutput.additionalContext, /at most 5 changed lines in 1 file needs/);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test("a swarm.direct section that does not parse names the key and leaves the rest of the note", (t) => {
|
|
191
|
+
const [bad] = checkouts(t, [{ swarm: { dispatch: {}, direct: { maxLines: 0 } } }]);
|
|
192
|
+
const text = decide(start(bad, "startup")).hookSpecificOutput.additionalContext;
|
|
193
|
+
assert.ok(text.startsWith(REMINDER));
|
|
194
|
+
assert.match(text, /Direct lane is off: swarm\.direct\.maxLines/);
|
|
195
|
+
});
|
package/src/cli/dispatch.mjs
CHANGED
|
@@ -27,10 +27,11 @@ function checkModels(models) {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
/**
|
|
30
|
+
/** An exact GPT model id or the first Claude family a model string contains, lower-cased. */
|
|
31
31
|
export function familyOf(model) {
|
|
32
32
|
const lower = model.toLowerCase();
|
|
33
|
-
|
|
33
|
+
if (lower.startsWith("gpt-")) return MODEL_FAMILIES.includes(lower) ? lower : null;
|
|
34
|
+
return MODEL_FAMILIES.find((family) => !family.startsWith("gpt-") && lower.includes(family)) ?? null;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
/** @returns the dispatch settings, or null when the section is absent; throws naming the key a repository got wrong. */
|
|
@@ -126,7 +127,7 @@ function modelRefusal(type, settings) {
|
|
|
126
127
|
}
|
|
127
128
|
return (
|
|
128
129
|
`Dispatch guard: the dispatch names no model, so the ${type} agent runs on the session's model. ` +
|
|
129
|
-
`Pass \`model\` with the
|
|
130
|
+
`Pass \`model\` with the configured model, or dispatch one of: ${allowed}. ` +
|
|
130
131
|
`To allow another type with no model, add it to ${KEY}.allowedTypes in qc.config.json.`
|
|
131
132
|
);
|
|
132
133
|
}
|
|
@@ -54,6 +54,16 @@ test("familyOf finds the first known family a model string contains, lower-cased
|
|
|
54
54
|
assert.equal(familyOf("opus[1m]"), "opus");
|
|
55
55
|
assert.equal(familyOf("OPUS"), "opus");
|
|
56
56
|
assert.equal(familyOf("gpt-4"), null);
|
|
57
|
+
assert.equal(familyOf("GPT-6.1-SOL"), "gpt-6.1-sol");
|
|
58
|
+
assert.equal(familyOf("gpt-6.1-sol-other"), null);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("every SDD role and the wildcard accept GPT 6.1 Sol by default and refuse another GPT model", () => {
|
|
62
|
+
const settings = dispatchSettings({ dispatch: {} });
|
|
63
|
+
for (const type of [...settings.allowedTypes, "general-purpose", "Explore"]) {
|
|
64
|
+
assert.equal(dispatchRefusal({ subagent_type: type, model: "gpt-6.1-sol" }, settings), null, type);
|
|
65
|
+
assert.match(dispatchRefusal({ subagent_type: type, model: "gpt-6-sol" }, settings) ?? "", /gpt-6\.1-sol/, type);
|
|
66
|
+
}
|
|
57
67
|
});
|
|
58
68
|
|
|
59
69
|
const on = dispatchSettings({ dispatch: { maxPromptChars: 20 } });
|
|
@@ -68,8 +78,8 @@ test("a dispatch with neither a model nor an allowed type is refused, and the re
|
|
|
68
78
|
});
|
|
69
79
|
|
|
70
80
|
test("a named model or an allowed type passes", () => {
|
|
71
|
-
assert.equal(dispatchRefusal({ subagent_type: "general-purpose", model: "
|
|
72
|
-
assert.equal(dispatchRefusal({ model: "
|
|
81
|
+
assert.equal(dispatchRefusal({ subagent_type: "general-purpose", model: "gpt-6.1-sol", prompt: "p" }, on), null);
|
|
82
|
+
assert.equal(dispatchRefusal({ model: "gpt-6.1-sol" }, on), null);
|
|
73
83
|
assert.equal(dispatchRefusal({ subagent_type: "architecture:sdd-reviewer" }, on), null);
|
|
74
84
|
});
|
|
75
85
|
|
|
@@ -80,11 +90,11 @@ test("a fork passes only as an allowed type, since it ignores the model", () =>
|
|
|
80
90
|
});
|
|
81
91
|
|
|
82
92
|
test("a prompt over the limit is refused, and a prompt at the limit passes", () => {
|
|
83
|
-
const reason = dispatchRefusal({ model: "
|
|
93
|
+
const reason = dispatchRefusal({ model: "gpt-6.1-sol", prompt: "x".repeat(21) }, on) ?? "";
|
|
84
94
|
assert.match(reason, /21 characters/);
|
|
85
95
|
assert.match(reason, /swarm\.dispatch\.maxPromptChars \(20\)/);
|
|
86
96
|
assert.match(reason, /Write the brief to a file/);
|
|
87
|
-
assert.equal(dispatchRefusal({ model: "
|
|
97
|
+
assert.equal(dispatchRefusal({ model: "gpt-6.1-sol", prompt: "x".repeat(20) }, on), null);
|
|
88
98
|
});
|
|
89
99
|
|
|
90
100
|
test("the model rule comes before the length rule", () => {
|
|
@@ -95,30 +105,28 @@ test("a planner on sonnet is refused, and an implementer on opus is refused", ()
|
|
|
95
105
|
const planner = dispatchRefusal({ subagent_type: "architecture:sdd-planner", model: "sonnet" }, on) ?? "";
|
|
96
106
|
assert.match(planner, /architecture:sdd-planner/);
|
|
97
107
|
assert.match(planner, /"sonnet"/);
|
|
98
|
-
assert.match(planner, /
|
|
108
|
+
assert.match(planner, /gpt-6\.1-sol/);
|
|
99
109
|
const implementer = dispatchRefusal({ subagent_type: "architecture:sdd-implementer", model: "opus" }, on) ?? "";
|
|
100
110
|
assert.match(implementer, /architecture:sdd-implementer/);
|
|
101
111
|
assert.match(implementer, /"opus"/);
|
|
102
|
-
assert.match(implementer, /
|
|
112
|
+
assert.match(implementer, /gpt-6\.1-sol/);
|
|
103
113
|
});
|
|
104
114
|
|
|
105
115
|
test("a general-purpose dispatch on opus is refused, naming the wildcard's families", () => {
|
|
106
116
|
const reason = dispatchRefusal({ model: "opus" }, on) ?? "";
|
|
107
117
|
assert.match(reason, /general-purpose/);
|
|
108
|
-
assert.match(reason, /
|
|
118
|
+
assert.match(reason, /gpt-6\.1-sol/);
|
|
109
119
|
});
|
|
110
120
|
|
|
111
121
|
test("a model string with no known family is refused", () => {
|
|
112
122
|
assert.match(dispatchRefusal({ model: "gpt-4" }, on) ?? "", /general-purpose/);
|
|
113
123
|
});
|
|
114
124
|
|
|
115
|
-
test("each
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
assert.equal(dispatchRefusal({
|
|
120
|
-
assert.equal(dispatchRefusal({ model: "haiku" }, on), null);
|
|
121
|
-
assert.equal(dispatchRefusal({ subagent_type: "architecture:sdd-implementer", model: "claude-sonnet-5" }, on), null);
|
|
125
|
+
test("each shipped role accepts its configured model, bare and scoped", () => {
|
|
126
|
+
for (const type of on.allowedTypes) {
|
|
127
|
+
assert.equal(dispatchRefusal({ subagent_type: type, model: "gpt-6.1-sol" }, on), null, type);
|
|
128
|
+
}
|
|
129
|
+
assert.equal(dispatchRefusal({ model: "gpt-6.1-sol" }, on), null);
|
|
122
130
|
});
|
|
123
131
|
|
|
124
132
|
test("a fork keeps its current rule and never reaches the tier check", () => {
|
|
@@ -196,11 +204,11 @@ test("a refusal over a limit above 1 names the limit and each holder, and one at
|
|
|
196
204
|
test("exploreToolLines gives one `- name: use (how)` line per tool, in config order", () => {
|
|
197
205
|
assert.deepEqual(
|
|
198
206
|
exploreToolLines([
|
|
199
|
-
{ name: "
|
|
207
|
+
{ name: "CocoIndex", use: "find the files for a concept", how: 'ccc search "<question>"' },
|
|
200
208
|
{ name: "GitNexus", use: "callers and callees", how: "gitnexus context <symbol> -r repo" },
|
|
201
209
|
]),
|
|
202
210
|
[
|
|
203
|
-
'-
|
|
211
|
+
'- CocoIndex: find the files for a concept (ccc search "<question>")',
|
|
204
212
|
"- GitNexus: callers and callees (gitnexus context <symbol> -r repo)",
|
|
205
213
|
],
|
|
206
214
|
);
|
|
@@ -211,7 +219,7 @@ test("a shipped type that names a full model id gets a cache note, never a refus
|
|
|
211
219
|
const settings = { ...dispatchDefaults };
|
|
212
220
|
const note = dispatchNote({ subagent_type: "architecture:sdd-implementer", model: "claude-sonnet-5", prompt: "x" }, settings);
|
|
213
221
|
assert.match(note, /share no prompt cache/);
|
|
214
|
-
assert.equal(dispatchNote({ subagent_type: "architecture:sdd-implementer", model: "
|
|
222
|
+
assert.equal(dispatchNote({ subagent_type: "architecture:sdd-implementer", model: "gpt-6.1-sol", prompt: "x" }, settings), null);
|
|
215
223
|
assert.equal(dispatchNote({ subagent_type: "architecture:sdd-implementer", prompt: "x" }, settings), null);
|
|
216
224
|
assert.equal(dispatchNote({ subagent_type: "general-purpose", model: "claude-sonnet-5", prompt: "x" }, settings), null);
|
|
217
225
|
});
|
|
@@ -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;
|