architecture-harness 0.16.0 → 0.18.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 +8 -3
- package/src/cli/bash-command-guard.mjs +25 -10
- package/src/cli/bash-command-guard.test.mjs +30 -0
- package/src/cli/bash-edit-guard.mjs +28 -7
- package/src/cli/bash-edit-guard.test.mjs +24 -0
- package/src/cli/budget-counter.mjs +25 -4
- package/src/cli/budget-guard.mjs +73 -4
- package/src/cli/budget-guard.test.mjs +111 -0
- package/src/cli/budget.mjs +3 -2
- package/src/cli/check-registries.test.mjs +181 -0
- package/src/cli/check.mjs +130 -9
- package/src/cli/cli.test.mjs +215 -2
- package/src/cli/context-signal.mjs +92 -0
- package/src/cli/context-signal.test.mjs +99 -0
- package/src/cli/contract-history.mjs +46 -0
- package/src/cli/contract-history.test.mjs +118 -0
- package/src/cli/controller-guard.mjs +73 -0
- package/src/cli/controller-guard.test.mjs +119 -0
- package/src/cli/dispatch-guard.mjs +14 -4
- package/src/cli/dispatch-guard.test.mjs +5 -6
- package/src/cli/dispatch-reminder.mjs +19 -11
- package/src/cli/dispatch-reminder.test.mjs +32 -0
- package/src/cli/dispatch.mjs +23 -2
- package/src/cli/dispatch.test.mjs +39 -1
- package/src/cli/doctor.mjs +2 -1
- package/src/cli/explore-guard.mjs +2 -2
- package/src/cli/explore-hint.mjs +67 -7
- package/src/cli/explore-hint.test.mjs +104 -4
- package/src/cli/gh-merge-command.mjs +228 -0
- package/src/cli/gh-merge-command.test.mjs +273 -0
- package/src/cli/gh-run.mjs +30 -0
- package/src/cli/gh-run.test.mjs +22 -0
- package/src/cli/git-hooks.test.mjs +28 -0
- package/src/cli/git-read.mjs +42 -0
- package/src/cli/git-read.test.mjs +94 -0
- package/src/cli/handoff-note.mjs +69 -0
- package/src/cli/handoff-note.test.mjs +71 -0
- package/src/cli/init.mjs +7 -3
- package/src/cli/issue-gate.mjs +169 -0
- package/src/cli/issue-gate.test.mjs +234 -0
- package/src/cli/ledger.mjs +138 -0
- package/src/cli/ledger.test.mjs +127 -0
- package/src/cli/merge-guard.mjs +193 -0
- package/src/cli/merge-guard.test.mjs +328 -0
- package/src/cli/plan-check.mjs +151 -0
- package/src/cli/plan-check.test.mjs +143 -0
- package/src/cli/plan-stop.mjs +101 -0
- package/src/cli/plan-stop.test.mjs +136 -0
- package/src/cli/plugin-agents.test.mjs +105 -8
- package/src/cli/pr-check.mjs +17 -5
- package/src/cli/pr-check.test.mjs +85 -4
- package/src/cli/prose.mjs +192 -0
- package/src/cli/prose.test.mjs +248 -0
- package/src/cli/qc.mjs +22 -0
- package/src/cli/registry-history.mjs +1 -1
- package/src/cli/report-stash.mjs +69 -0
- package/src/cli/report-stash.test.mjs +71 -0
- package/src/cli/report-stop.mjs +266 -0
- package/src/cli/report-stop.test.mjs +471 -0
- package/src/cli/shell-command.mjs +81 -6
- package/src/cli/shell-command.test.mjs +101 -1
- package/src/cli/task-gate.mjs +179 -0
- package/src/cli/task-gate.test.mjs +507 -0
- package/src/cli/tokens.mjs +239 -0
- package/src/cli/tokens.test.mjs +286 -0
- package/src/cli/transcript-usage.mjs +135 -0
- package/src/cli/transcript-usage.test.mjs +129 -0
- package/src/cli/workflow-hooks.test.mjs +72 -0
- package/src/cli/workflow-place.mjs +143 -0
- package/src/cli/workflow-place.test.mjs +146 -0
- package/src/cli/workflow-settings.mjs +77 -0
- package/src/cli/workflow-settings.test.mjs +79 -0
- package/src/cli/worktree.mjs +1 -1
- package/src/cli/worktree.test.mjs +19 -2
- package/src/config.mjs +99 -4
- package/src/config.test.mjs +38 -3
- package/src/descriptions.mjs +5 -1
- package/src/eslint/index.mjs +2 -0
- package/src/eslint/jsx.mjs +57 -0
- package/src/eslint/preset.mjs +32 -6
- package/src/eslint/preset.test.mjs +60 -0
- package/src/eslint/rules/durable-idempotency-key.mjs +26 -0
- package/src/eslint/rules/ime-safe-key.mjs +1 -50
- package/src/eslint/rules/mutation-control-pending.mjs +128 -0
- package/src/eslint/rules/no-supersession-trail.mjs +8 -4
- package/src/eslint/rules/registry-literal.mjs +36 -2
- package/src/eslint/rules.test.mjs +153 -0
- package/src/gates/call-args.mjs +90 -0
- package/src/gates/call-args.test.mjs +21 -0
- package/src/gates/config-floor.mjs +41 -20
- package/src/gates/config-floor.test.mjs +71 -1
- package/src/gates/contract-idempotency.mjs +103 -0
- package/src/gates/contract-idempotency.test.mjs +276 -0
- package/src/gates/contract-operations.mjs +120 -0
- package/src/gates/contract-operations.test.mjs +275 -0
- package/src/gates/contract-routes.mjs +82 -0
- package/src/gates/contract-routes.test.mjs +210 -0
- package/src/gates/registry-literal.mjs +79 -15
- package/src/gates/registry-literal.test.mjs +89 -0
- package/src/gates/saga-key.mjs +253 -0
- package/src/gates/saga-key.test.mjs +322 -0
- package/src/gates/scrub.mjs +108 -0
- package/src/gates/scrub.test.mjs +29 -0
- package/src/gates/tenant-isolation-test.mjs +80 -0
- package/src/gates/tenant-isolation-test.test.mjs +129 -0
- package/src/gates/tenant-predicate.mjs +644 -91
- package/src/gates/tenant-predicate.test.mjs +662 -1
- package/src/prose-lines.mjs +49 -0
- package/src/prose-lines.test.mjs +63 -0
- package/src/registries.mjs +89 -0
- package/src/registries.test.mjs +44 -0
- package/src/vale-style.test.mjs +158 -0
- package/templates/docs/enforcement.md +26 -2
- package/templates/github/workflows/ci.yml +18 -1
- package/templates/quality-thresholds.json +1 -1
- package/templates/vale/.vale.ini +7 -0
- package/templates/vale/styles/Qc/AndOr.yml +7 -0
- package/templates/vale/styles/Qc/InOrderTo.yml +6 -0
- package/templates/vale/styles/Qc/Latin.yml +9 -0
- package/templates/vale/styles/Qc/PassiveVoice.yml +21 -0
- package/templates/vale/styles/Qc/Please.yml +6 -0
- package/templates/vale/styles/Qc/Progress.yml +9 -0
- package/templates/vale/styles/Qc/SentenceLength.yml +6 -0
- package/templates/vale/styles/Qc/Utilize.yml +10 -0
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// and fork, because a rule held only in the conversation is lost to a compaction.
|
|
4
4
|
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
|
-
import { dispatchSettingsAt, exploreSettingsAt } from "./dispatch.mjs";
|
|
6
|
+
import { dispatchSettingsAt, exploreSettingsAt, exploreToolLines } from "./dispatch.mjs";
|
|
7
7
|
|
|
8
8
|
export const REMINDER =
|
|
9
9
|
"Subagent workflow (swarm.dispatch in qc.config.json): run a plan with superpowers:subagent-driven-development. " +
|
|
@@ -11,7 +11,8 @@ export const REMINDER =
|
|
|
11
11
|
"architecture:sdd-reviewer to review it; before merge, dispatch architecture:sdd-branch-reviewer to review the " +
|
|
12
12
|
"whole branch. The planner and the branch reviewer run on opus, the implementer and the task reviewer on sonnet. " +
|
|
13
13
|
"Run one implementer at a time: a hook refuses a second while one runs. Name a model on any other dispatch, and " +
|
|
14
|
-
"pass each brief as a file path."
|
|
14
|
+
"pass each brief as a file path. Name the worktree on a prompt line `Worktree: <path>`. A reviewer starts with " +
|
|
15
|
+
"`VERDICT: <APPROVED|CHANGES_REQUIRED> <sha>`, and a merge passes `--match-head-commit <sha>`.";
|
|
15
16
|
|
|
16
17
|
const context = (eventName, text) => ({ hookSpecificOutput: { hookEventName: eventName, additionalContext: text } });
|
|
17
18
|
|
|
@@ -21,23 +22,30 @@ const slotsLine = ({ implementerSlots }) =>
|
|
|
21
22
|
? ` This repository allows up to ${implementerSlots} implementers at once, each in its own worktree, so the one-at-a-time rule above does not apply. A hook refuses one over the limit.`
|
|
22
23
|
: "";
|
|
23
24
|
|
|
24
|
-
/**
|
|
25
|
-
function
|
|
25
|
+
/** The explore tools, or null when the section is off, names none, or does not parse. */
|
|
26
|
+
function exploreToolsAt(cwd) {
|
|
26
27
|
let settings;
|
|
27
28
|
try {
|
|
28
29
|
settings = exploreSettingsAt(cwd);
|
|
29
30
|
} catch {
|
|
30
|
-
// The explore guard and hint each report their own config error; the
|
|
31
|
-
return
|
|
31
|
+
// The explore guard and hint each report their own config error; the notes stay unaffected.
|
|
32
|
+
return null;
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
return settings && settings.tools.length > 0 ? settings.tools : null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** One added line naming `swarm.explore`'s tools, or "" when there are none. */
|
|
38
|
+
function exploreLine(cwd) {
|
|
39
|
+
const tools = exploreToolsAt(cwd);
|
|
40
|
+
return tools ? ` The repository's own explore tools: ${tools.map((tool) => tool.name).join(", ")}.` : "";
|
|
35
41
|
}
|
|
36
42
|
|
|
37
|
-
/**
|
|
43
|
+
/** A subagent may carry no MCP tool, so its note gives each tool's command, not only its name. */
|
|
38
44
|
function decideSubagentStart(cwd) {
|
|
39
|
-
const
|
|
40
|
-
|
|
45
|
+
const tools = exploreToolsAt(cwd);
|
|
46
|
+
if (!tools) return null;
|
|
47
|
+
const lines = ["The repository's own explore tools, and how to run each:", ...exploreToolLines(tools)];
|
|
48
|
+
return context("SubagentStart", lines.join("\n"));
|
|
41
49
|
}
|
|
42
50
|
|
|
43
51
|
/** @returns the hook output, or null when the checkout turns on neither `swarm.dispatch` nor `swarm.explore`. */
|
|
@@ -49,6 +49,9 @@ 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
|
+
"Worktree: <path>",
|
|
53
|
+
"VERDICT: <APPROVED|CHANGES_REQUIRED> <sha>",
|
|
54
|
+
"--match-head-commit <sha>",
|
|
52
55
|
]) {
|
|
53
56
|
assert.ok(REMINDER.includes(word), word);
|
|
54
57
|
}
|
|
@@ -140,3 +143,32 @@ test("a limit above 1 adds one line stating it, and a limit of 1 leaves the note
|
|
|
140
143
|
assert.match(text, /up to 3 implementers at once/);
|
|
141
144
|
assert.equal(decide(start(one, "startup")).hookSpecificOutput.additionalContext, REMINDER);
|
|
142
145
|
});
|
|
146
|
+
|
|
147
|
+
test("SubagentStart gives each tool's use and command, in config order, since a plugin agent carries no MCP tool", (t) => {
|
|
148
|
+
const [on] = checkouts(t, [
|
|
149
|
+
{
|
|
150
|
+
swarm: {
|
|
151
|
+
explore: {
|
|
152
|
+
tools: [
|
|
153
|
+
{ name: "slm-rerank", use: "find the files for a concept", how: 'slm-rerank -q "<question>" --stub -k 5' },
|
|
154
|
+
{ name: "GitNexus", use: "callers and callees", how: "gitnexus context <symbol> -r repo" },
|
|
155
|
+
],
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
]);
|
|
160
|
+
assert.equal(
|
|
161
|
+
decide(subagentStart(on)).hookSpecificOutput.additionalContext,
|
|
162
|
+
[
|
|
163
|
+
"The repository's own explore tools, and how to run each:",
|
|
164
|
+
'- slm-rerank: find the files for a concept (slm-rerank -q "<question>" --stub -k 5)',
|
|
165
|
+
"- GitNexus: callers and callees (gitnexus context <symbol> -r repo)",
|
|
166
|
+
].join("\n"),
|
|
167
|
+
);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
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: "slm-rerank" } } }]);
|
|
172
|
+
assert.equal(decide(subagentStart(bad)), null);
|
|
173
|
+
assert.equal(decide(start(bad, "startup")).hookSpecificOutput.additionalContext, REMINDER);
|
|
174
|
+
});
|
package/src/cli/dispatch.mjs
CHANGED
|
@@ -77,7 +77,9 @@ export function exploreSettings(swarm = {}) {
|
|
|
77
77
|
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
78
78
|
throw new Error(`${EXPLORE_KEY} in qc.config.json must be an object, got ${JSON.stringify(raw)}`);
|
|
79
79
|
}
|
|
80
|
-
const { tools, summarizer, maxReadLines, maxGrepLines, maxOutputChars, exempt } = merge(exploreDefaults, raw);
|
|
80
|
+
const { tools, summarizer, maxReadLines, maxGrepLines, maxOutputChars, exempt, longOutput, excerptChars: set } = merge(exploreDefaults, raw);
|
|
81
|
+
// A repository that sets only maxOutputChars keeps its explore hooks: the excerpt default follows it.
|
|
82
|
+
const excerptChars = raw.excerptChars === undefined && Number.isInteger(maxOutputChars) ? Math.min(exploreDefaults.excerptChars, Math.floor(maxOutputChars / 2)) : set;
|
|
81
83
|
if (!Array.isArray(tools) || !tools.every(isToolEntry)) {
|
|
82
84
|
throw wrongExplore("tools", tools, 'a list of { "name", "use", "how" } strings');
|
|
83
85
|
}
|
|
@@ -88,11 +90,14 @@ export function exploreSettings(swarm = {}) {
|
|
|
88
90
|
["maxReadLines", maxReadLines],
|
|
89
91
|
["maxGrepLines", maxGrepLines],
|
|
90
92
|
["maxOutputChars", maxOutputChars],
|
|
93
|
+
["excerptChars", excerptChars],
|
|
91
94
|
]) {
|
|
92
95
|
if (!Number.isInteger(value) || value < 1) throw wrongExplore(key, value, "a positive whole number");
|
|
93
96
|
}
|
|
97
|
+
if (excerptChars >= maxOutputChars) throw wrongExplore("excerptChars", excerptChars, `smaller than maxOutputChars (${maxOutputChars})`);
|
|
94
98
|
if (!isNameList(exempt)) throw wrongExplore("exempt", exempt, "a list of globs");
|
|
95
|
-
|
|
99
|
+
if (longOutput !== "excerpt" && longOutput !== "hint") throw wrongExplore("longOutput", longOutput, '"excerpt" or "hint"');
|
|
100
|
+
return { tools, summarizer, maxReadLines, maxGrepLines, maxOutputChars, exempt, longOutput, excerptChars };
|
|
96
101
|
}
|
|
97
102
|
|
|
98
103
|
/** @returns the explore settings of the checkout that holds `cwd`, or null when it has no config or no section. */
|
|
@@ -102,6 +107,9 @@ export function exploreSettingsAt(cwd) {
|
|
|
102
107
|
return exploreSettings(load(root).swarm);
|
|
103
108
|
}
|
|
104
109
|
|
|
110
|
+
/** One `- name: use (how)` line per explore tool: the form every explore note and refusal shares. */
|
|
111
|
+
export const exploreToolLines = (tools) => tools.map((tool) => `- ${tool.name}: ${tool.use} (${tool.how})`);
|
|
112
|
+
|
|
105
113
|
/** The type a dispatch names, trimmed. The Agent tool runs general-purpose when it names none. */
|
|
106
114
|
export const typeOf = (input) =>
|
|
107
115
|
typeof input.subagent_type === "string" && input.subagent_type.trim() !== "" ? input.subagent_type.trim() : DEFAULT_TYPE;
|
|
@@ -157,6 +165,19 @@ export function dispatchRefusal(input, settings) {
|
|
|
157
165
|
return null;
|
|
158
166
|
}
|
|
159
167
|
|
|
168
|
+
/** A note for a dispatch that starts a second prompt cache for its type, or null. It never refuses. */
|
|
169
|
+
export function dispatchNote(input, settings) {
|
|
170
|
+
const type = typeOf(input);
|
|
171
|
+
const model = typeof input.model === "string" ? input.model.trim() : "";
|
|
172
|
+
if (model === "" || type === FORK || !settings.allowedTypes.includes(type)) return null;
|
|
173
|
+
if (MODEL_FAMILIES.includes(model.toLowerCase())) return null;
|
|
174
|
+
return (
|
|
175
|
+
`Dispatch note: ${type} sets its own model, and "${model}" is a full model id. Requests to different models share no prompt cache, ` +
|
|
176
|
+
`so this dispatch pays the type's shared first-call prefix again when the id differs from the definition's model. ` +
|
|
177
|
+
"A dispatch of this type with no `model` shares one cache with the others."
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
160
181
|
/** The reason an implementer over the limit waits, with the two ways a slot frees. */
|
|
161
182
|
export function slotRefusal({ type, claimedAt, expiresAt, slotFile, limit = 1, holders = [] }) {
|
|
162
183
|
const frees = `If no implementer runs, because its dispatch was denied after this hook, delete ${slotFile}.`;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
2
|
import { test } from "node:test";
|
|
3
3
|
import { defaults, dispatchDefaults, merge } from "../config.mjs";
|
|
4
|
-
import { dispatchRefusal, dispatchSettings, familyOf, isImplementer, slotRefusal, typeOf } from "./dispatch.mjs";
|
|
4
|
+
import { dispatchNote, dispatchRefusal, dispatchSettings, exploreSettings, exploreToolLines, familyOf, isImplementer, slotRefusal, typeOf } from "./dispatch.mjs";
|
|
5
5
|
|
|
6
6
|
test("a swarm section with no dispatch key keeps the guard off", () => {
|
|
7
7
|
assert.equal(dispatchSettings({}), null);
|
|
@@ -192,3 +192,41 @@ test("a refusal over a limit above 1 names the limit and each holder, and one at
|
|
|
192
192
|
assert.match(one, /holds the one implementer slot/);
|
|
193
193
|
assert.doesNotMatch(one, /use-1/);
|
|
194
194
|
});
|
|
195
|
+
|
|
196
|
+
test("exploreToolLines gives one `- name: use (how)` line per tool, in config order", () => {
|
|
197
|
+
assert.deepEqual(
|
|
198
|
+
exploreToolLines([
|
|
199
|
+
{ name: "slm-rerank", use: "find the files for a concept", how: 'slm-rerank -q "<question>" --stub -k 5' },
|
|
200
|
+
{ name: "GitNexus", use: "callers and callees", how: "gitnexus context <symbol> -r repo" },
|
|
201
|
+
]),
|
|
202
|
+
[
|
|
203
|
+
'- slm-rerank: find the files for a concept (slm-rerank -q "<question>" --stub -k 5)',
|
|
204
|
+
"- GitNexus: callers and callees (gitnexus context <symbol> -r repo)",
|
|
205
|
+
],
|
|
206
|
+
);
|
|
207
|
+
assert.deepEqual(exploreToolLines([]), []);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("a shipped type that names a full model id gets a cache note, never a refusal", () => {
|
|
211
|
+
const settings = { ...dispatchDefaults };
|
|
212
|
+
const note = dispatchNote({ subagent_type: "architecture:sdd-implementer", model: "claude-sonnet-5", prompt: "x" }, settings);
|
|
213
|
+
assert.match(note, /share no prompt cache/);
|
|
214
|
+
assert.equal(dispatchNote({ subagent_type: "architecture:sdd-implementer", model: "sonnet", prompt: "x" }, settings), null);
|
|
215
|
+
assert.equal(dispatchNote({ subagent_type: "architecture:sdd-implementer", prompt: "x" }, settings), null);
|
|
216
|
+
assert.equal(dispatchNote({ subagent_type: "general-purpose", model: "claude-sonnet-5", prompt: "x" }, settings), null);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("an excerptChars the repository leaves out is half of maxOutputChars, at most 4000, and never an error", () => {
|
|
220
|
+
assert.equal(exploreSettings({ explore: { maxOutputChars: 20 } }).excerptChars, 10);
|
|
221
|
+
assert.equal(exploreSettings({ explore: { maxOutputChars: 7 } }).excerptChars, 3);
|
|
222
|
+
assert.equal(exploreSettings({ explore: { maxOutputChars: 7999 } }).excerptChars, 3999);
|
|
223
|
+
assert.equal(exploreSettings({ explore: { maxOutputChars: 20000 } }).excerptChars, 4000);
|
|
224
|
+
assert.equal(exploreSettings({ explore: {} }).excerptChars, 4000);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("an excerptChars the repository sets must be smaller than maxOutputChars", () => {
|
|
228
|
+
assert.equal(exploreSettings({ explore: { maxOutputChars: 20, excerptChars: 19 } }).excerptChars, 19);
|
|
229
|
+
for (const excerptChars of [20, 21]) {
|
|
230
|
+
assert.throws(() => exploreSettings({ explore: { maxOutputChars: 20, excerptChars } }), /swarm\.explore\.excerptChars .*smaller than maxOutputChars \(20\)/);
|
|
231
|
+
}
|
|
232
|
+
});
|
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 { requiredHooks } from "../config.mjs";
|
|
12
13
|
import { hookDrift } from "./git-hooks.mjs";
|
|
13
14
|
|
|
14
15
|
const execFileAsync = promisify(execFile);
|
|
@@ -125,7 +126,7 @@ export async function runDoctor(config) {
|
|
|
125
126
|
|
|
126
127
|
/** @returns true when an installed git hook lacks a call `hooks.required` names. */
|
|
127
128
|
function reportHooks(config) {
|
|
128
|
-
const { problems, notes } = hookDrift(config.root, config
|
|
129
|
+
const { problems, notes } = hookDrift(config.root, requiredHooks(config));
|
|
129
130
|
for (const problem of problems) console.error(`FAIL hooks ${problem.path}: ${problem.detail}`);
|
|
130
131
|
for (const note of notes) console.log(`NOTE hooks ${note}`);
|
|
131
132
|
if (problems.length === 0) console.log("OK hooks each git hook makes the calls hooks.required names");
|
|
@@ -21,7 +21,7 @@ import path from "node:path";
|
|
|
21
21
|
import { fileURLToPath } from "node:url";
|
|
22
22
|
import { globMatcher } from "../glob.mjs";
|
|
23
23
|
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
24
|
-
import { exploreSettingsAt } from "./dispatch.mjs";
|
|
24
|
+
import { exploreSettingsAt, exploreToolLines } from "./dispatch.mjs";
|
|
25
25
|
import { slotDirOf } from "./dispatch-slot.mjs";
|
|
26
26
|
|
|
27
27
|
const BINARY_EXTENSIONS = new Set(["png", "jpg", "jpeg", "gif", "webp", "svg", "pdf", "ipynb"]);
|
|
@@ -149,7 +149,7 @@ function exceedsLineBudget(file, maxLines) {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
function refusalMessage(tools) {
|
|
152
|
-
const lines = tools
|
|
152
|
+
const lines = exploreToolLines(tools);
|
|
153
153
|
lines.push("Read a range with offset and limit, or read the same file again to read it whole.");
|
|
154
154
|
return lines.join("\n");
|
|
155
155
|
}
|
package/src/cli/explore-hint.mjs
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The trigger for `swarm.explore`'s search and output hints:
|
|
3
|
-
//
|
|
4
|
-
// context naming the summarizer when the output runs long. Neither refuses anything; both are a
|
|
5
|
-
// nudge the model can take or leave.
|
|
2
|
+
// The trigger for `swarm.explore`'s search and output hints: a long Grep answer names the repository's tools,
|
|
3
|
+
// and a long successful command output is saved to a file and shown as an excerpt, or named to the summarizer.
|
|
6
4
|
|
|
5
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
7
8
|
import { fileURLToPath } from "node:url";
|
|
8
9
|
import { exploreSettingsAt } from "./dispatch.mjs";
|
|
9
10
|
|
|
11
|
+
const FAILURE_LINE = /\b(?:fail(?:s|ed|ure|ing)?|errors?|not ok|panic|exception)\b|✖/i;
|
|
12
|
+
const MAX_SIGNAL_LINES = 20;
|
|
13
|
+
const MAX_SIGNAL_CHARS = 200;
|
|
14
|
+
const fileSafe = (id) => String(id).replace(/[^A-Za-z0-9_.-]/g, "_");
|
|
15
|
+
|
|
10
16
|
const context = (text) => ({ hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: text } });
|
|
11
17
|
|
|
12
18
|
/** The tool's own answer, as plain text, from whatever shape `tool_response` carries. */
|
|
@@ -42,8 +48,57 @@ const grepHint = (tools) =>
|
|
|
42
48
|
|
|
43
49
|
const outputHint = (summarizer) => `Explore hint: this output runs long. Pipe it through the summarizer instead: ${summarizer}`;
|
|
44
50
|
|
|
51
|
+
/** The head, the failure-shaped lines between, and the tail of a long output, in about `size` characters. */
|
|
52
|
+
export function excerptOf(text, size) {
|
|
53
|
+
const headSize = Math.floor(size / 4);
|
|
54
|
+
const tailStart = text.length - (size - headSize);
|
|
55
|
+
const middle = text.slice(headSize, tailStart);
|
|
56
|
+
const signal = middle
|
|
57
|
+
.split(/\r?\n/)
|
|
58
|
+
.filter((line) => FAILURE_LINE.test(line))
|
|
59
|
+
.slice(0, MAX_SIGNAL_LINES)
|
|
60
|
+
.map((line) => line.slice(0, MAX_SIGNAL_CHARS));
|
|
61
|
+
return { head: text.slice(0, headSize), signal, tail: text.slice(tailStart), omitted: middle.length };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const askLine = (summarizer, file) =>
|
|
65
|
+
summarizer === null ? "" : ` Ask the summarizer: ${summarizer.includes("<command>") ? summarizer.replace("<command>", `cat "${file}"`) : summarizer}.`;
|
|
66
|
+
|
|
67
|
+
/** What Claude sees in place of a long output: the excerpt, with a line that names the saved file. */
|
|
68
|
+
export function excerptText(text, size, file, summarizer) {
|
|
69
|
+
const { head, signal, tail, omitted } = excerptOf(text, size);
|
|
70
|
+
const found = signal.length > 0 ? ` Its lines that name a failure or an error:\n${signal.join("\n")}` : "";
|
|
71
|
+
return `${head}\n[Explore excerpt: ${omitted} characters of this output are left out here, and the whole output is in ${file}. Read a range of it or search it.${askLine(summarizer, file)}${found}]\n${tail}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The tool output with its text replaced, in the same shape, or null for an image or a shape the hook does not know. */
|
|
75
|
+
export function replacedOutput(response, replacement) {
|
|
76
|
+
if (typeof response === "string") return replacement;
|
|
77
|
+
if (response === null || typeof response !== "object" || response.isImage === true) return null;
|
|
78
|
+
if (typeof response.stdout === "string" || typeof response.stderr === "string") return { ...response, stdout: replacement, stderr: "" };
|
|
79
|
+
if (typeof response.output === "string") return { ...response, output: replacement };
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Saves the whole output and shows its excerpt. @returns the hook output, or null when either step cannot run */
|
|
84
|
+
function excerptOutput(call, text, settings, tmp) {
|
|
85
|
+
if (replacedOutput(call.tool_response, "") === null) return null;
|
|
86
|
+
const scratch = typeof call.scratchpad_dir === "string" && call.scratchpad_dir !== "" ? call.scratchpad_dir : null;
|
|
87
|
+
const dir = scratch ? path.join(scratch, "outputs") : path.join(tmp, "architecture-kit", "outputs", fileSafe(call.session_id ?? "session"));
|
|
88
|
+
const file = path.join(dir, `${fileSafe(call.tool_use_id ?? Date.now())}.txt`);
|
|
89
|
+
try {
|
|
90
|
+
mkdirSync(dir, { recursive: true });
|
|
91
|
+
writeFileSync(file, text);
|
|
92
|
+
} catch {
|
|
93
|
+
// A folder the hook cannot write leaves the output whole, with the hint.
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
const replaced = replacedOutput(call.tool_response, excerptText(text, settings.excerptChars, file, settings.summarizer));
|
|
97
|
+
return { hookSpecificOutput: { hookEventName: "PostToolUse", updatedToolOutput: replaced } };
|
|
98
|
+
}
|
|
99
|
+
|
|
45
100
|
/** The verdict on one finished Grep, Bash, or PowerShell call. @returns the hook output, or null for no change. */
|
|
46
|
-
export function decide(call) {
|
|
101
|
+
export function decide(call, tmp = os.tmpdir()) {
|
|
47
102
|
if (call.hook_event_name !== "PostToolUse") return null;
|
|
48
103
|
const tool = call.tool_name;
|
|
49
104
|
if (tool !== "Grep" && tool !== "Bash" && tool !== "PowerShell") return null;
|
|
@@ -62,8 +117,13 @@ export function decide(call) {
|
|
|
62
117
|
return lineCountOf(text) > settings.maxGrepLines ? context(grepHint(settings.tools)) : null;
|
|
63
118
|
}
|
|
64
119
|
|
|
65
|
-
if (
|
|
66
|
-
|
|
120
|
+
if (text.length <= settings.maxOutputChars) return null;
|
|
121
|
+
// Only an output at least twice the excerpt shrinks enough to be worth a file.
|
|
122
|
+
if (settings.longOutput === "excerpt" && text.length > 2 * settings.excerptChars) {
|
|
123
|
+
const excerpt = excerptOutput(call, text, settings, tmp);
|
|
124
|
+
if (excerpt) return excerpt;
|
|
125
|
+
}
|
|
126
|
+
return settings.summarizer === null ? null : context(outputHint(settings.summarizer));
|
|
67
127
|
}
|
|
68
128
|
|
|
69
129
|
async function readStdin() {
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
2
|
import { execFileSync } from "node:child_process";
|
|
3
|
-
import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
4
4
|
import os from "node:os";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { test } from "node:test";
|
|
7
7
|
import { fileURLToPath } from "node:url";
|
|
8
|
-
import { decide } from "./explore-hint.mjs";
|
|
8
|
+
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
12
|
const TOOLS = [{ name: "slm-rerank", use: "find the files for a concept", how: 'slm-rerank -q "<question>" --stub -k 5' }];
|
|
13
13
|
|
|
14
14
|
/** A checkout with `swarm.explore` on, and one with no config. */
|
|
15
|
-
function workspace(t, explore = { tools: TOOLS, maxGrepLines: 5, maxOutputChars: 20, summarizer: "lfm-ask" }) {
|
|
15
|
+
function workspace(t, explore = { tools: TOOLS, maxGrepLines: 5, maxOutputChars: 20, longOutput: "hint", summarizer: "lfm-ask" }) {
|
|
16
16
|
const base = realpathSync.native(mkdtempSync(path.join(os.tmpdir(), "qc-explore-hint-")));
|
|
17
17
|
t.after(() => rmSync(base, { recursive: true, force: true }));
|
|
18
18
|
const on = path.join(base, "on");
|
|
@@ -66,7 +66,7 @@ test("a command output at or under maxOutputChars sees no change", (t) => {
|
|
|
66
66
|
});
|
|
67
67
|
|
|
68
68
|
test("no summarizer configured means no output hint even over the limit", (t) => {
|
|
69
|
-
const ws = workspace(t, { tools: TOOLS, maxGrepLines: 5, maxOutputChars: 20, summarizer: null });
|
|
69
|
+
const ws = workspace(t, { tools: TOOLS, maxGrepLines: 5, maxOutputChars: 20, longOutput: "hint", summarizer: null });
|
|
70
70
|
assert.equal(decide(call(ws.on, "Bash", "x".repeat(30))), null);
|
|
71
71
|
});
|
|
72
72
|
|
|
@@ -132,3 +132,103 @@ test("hooks.json runs the hint on PostToolUse Grep and Bash|PowerShell", () => {
|
|
|
132
132
|
.map((entry) => entry.matcher);
|
|
133
133
|
assert.deepEqual(matchers.sort(), ["Bash|PowerShell", "Grep"]);
|
|
134
134
|
});
|
|
135
|
+
|
|
136
|
+
const bashResponse = (stdout) => ({ stdout, stderr: "", interrupted: false, isImage: false });
|
|
137
|
+
const longOutput = `${"h".repeat(50)}\n${"m".repeat(1000)}\nnot ok 3 - breaks\n${"m".repeat(2000)}\n${"t".repeat(80)}`;
|
|
138
|
+
const excerptWorkspace = (t, extra = {}) =>
|
|
139
|
+
workspace(t, { tools: TOOLS, maxOutputChars: 120, summarizer: '<command> | lfm-ask "<question>"', excerptChars: 100, ...extra });
|
|
140
|
+
|
|
141
|
+
test("a long successful output is saved whole and reaches Claude as its head, its failure lines, and its tail", (t) => {
|
|
142
|
+
const ws = excerptWorkspace(t);
|
|
143
|
+
const scratch = path.join(ws.on, "scratch");
|
|
144
|
+
const output = decide({ ...call(ws.on, "Bash", bashResponse(longOutput)), tool_use_id: "toolu_9", scratchpad_dir: scratch });
|
|
145
|
+
const replaced = output.hookSpecificOutput.updatedToolOutput;
|
|
146
|
+
const file = path.join(scratch, "outputs", "toolu_9.txt");
|
|
147
|
+
assert.equal(readFileSync(file, "utf8"), longOutput);
|
|
148
|
+
assert.equal(replaced.stderr, "");
|
|
149
|
+
assert.equal(replaced.isImage, false);
|
|
150
|
+
assert.ok(replaced.stdout.startsWith("h".repeat(25)));
|
|
151
|
+
assert.ok(replaced.stdout.endsWith("t".repeat(75)));
|
|
152
|
+
assert.match(replaced.stdout, /not ok 3 - breaks/);
|
|
153
|
+
assert.ok(replaced.stdout.includes(file));
|
|
154
|
+
assert.ok(replaced.stdout.includes(`cat "${file}" | lfm-ask`));
|
|
155
|
+
assert.ok(replaced.stdout.length < longOutput.length);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("with no scratchpad folder, the whole output goes under the temp folder of the session", (t) => {
|
|
159
|
+
const ws = excerptWorkspace(t);
|
|
160
|
+
const tmp = path.join(ws.on, "tmp");
|
|
161
|
+
const output = decide({ ...call(ws.on, "Bash", bashResponse(longOutput)), session_id: "s/1", tool_use_id: "toolu_1" }, tmp);
|
|
162
|
+
assert.ok(output.hookSpecificOutput.updatedToolOutput.stdout.includes(path.join(tmp, "architecture-kit", "outputs", "s_1", "toolu_1.txt")));
|
|
163
|
+
assert.ok(existsSync(path.join(tmp, "architecture-kit", "outputs", "s_1", "toolu_1.txt")));
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("one long line with no failure is still cut, and a string output stays a string", (t) => {
|
|
167
|
+
const ws = excerptWorkspace(t);
|
|
168
|
+
const scratch = path.join(ws.on, "scratch");
|
|
169
|
+
const oneLine = "x".repeat(5000);
|
|
170
|
+
const output = decide({ ...call(ws.on, "PowerShell", oneLine), tool_use_id: "toolu_2", scratchpad_dir: scratch });
|
|
171
|
+
const replaced = output.hookSpecificOutput.updatedToolOutput;
|
|
172
|
+
assert.equal(typeof replaced, "string");
|
|
173
|
+
assert.ok(replaced.length < 1000);
|
|
174
|
+
assert.doesNotMatch(replaced, /name a failure/);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("longOutput hint, an image, an unknown shape, or an output under twice excerptChars keeps the hint", (t) => {
|
|
178
|
+
const hint = excerptWorkspace(t, { longOutput: "hint" });
|
|
179
|
+
assert.match(contextOf(decide(call(hint.on, "Bash", bashResponse(longOutput)))) ?? "", /lfm-ask/);
|
|
180
|
+
const ws = excerptWorkspace(t);
|
|
181
|
+
assert.match(contextOf(decide(call(ws.on, "Bash", { ...bashResponse(longOutput), isImage: true }))) ?? "", /lfm-ask/);
|
|
182
|
+
assert.match(contextOf(decide(call(ws.on, "Bash", { lines: longOutput.split("\n") }))) ?? "", /lfm-ask/);
|
|
183
|
+
assert.match(contextOf(decide(call(ws.on, "Bash", bashResponse("y".repeat(150))))) ?? "", /lfm-ask/);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test("a wrong longOutput or excerptChars is a named config error, reported as context", (t) => {
|
|
187
|
+
for (const extra of [{ longOutput: "cut" }, { excerptChars: 0 }]) {
|
|
188
|
+
const ws = excerptWorkspace(t, extra);
|
|
189
|
+
assert.match(contextOf(decide(call(ws.on, "Bash", bashResponse(longOutput)))) ?? "", /Explore guard is off: swarm\.explore\./);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("an excerptChars that is not smaller than maxOutputChars is a named config error, reported as context", (t) => {
|
|
194
|
+
for (const excerptChars of [120, 5000]) {
|
|
195
|
+
const ws = excerptWorkspace(t, { excerptChars });
|
|
196
|
+
assert.match(contextOf(decide(call(ws.on, "Bash", bashResponse(longOutput)))) ?? "", /Explore guard is off: swarm\.explore\.excerptChars .*smaller than maxOutputChars \(120\)/);
|
|
197
|
+
}
|
|
198
|
+
const fine = excerptWorkspace(t, { excerptChars: 119 });
|
|
199
|
+
assert.ok(decide(call(fine.on, "Bash", bashResponse(longOutput))).hookSpecificOutput.updatedToolOutput);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("excerptOf keeps a quarter for the head, the rest for the tail, and at most 20 failure lines", () => {
|
|
203
|
+
const text = `${"a".repeat(100)}\n${Array.from({ length: 30 }, (_, index) => `error ${index}`).join("\n")}\n${"z".repeat(400)}`;
|
|
204
|
+
const { head, signal, tail, omitted } = excerptOf(text, 200);
|
|
205
|
+
assert.equal(head.length, 50);
|
|
206
|
+
assert.equal(tail.length, 150);
|
|
207
|
+
assert.equal(signal.length, 20);
|
|
208
|
+
assert.equal(omitted, text.length - 200);
|
|
209
|
+
assert.equal(replacedOutput({ output: "x" }, "y").output, "y");
|
|
210
|
+
assert.equal(replacedOutput(null, "y"), null);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test("replacedOutput blanks a non-empty stderr and keeps the other fields", () => {
|
|
214
|
+
const replaced = replacedOutput({ stdout: "out", stderr: "boom", interrupted: false, isImage: false }, "y");
|
|
215
|
+
assert.deepEqual(replaced, { stdout: "y", stderr: "", interrupted: false, isImage: false });
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("an excerpt with no summarizer names the file and offers no summarizer", (t) => {
|
|
219
|
+
const ws = excerptWorkspace(t, { summarizer: null });
|
|
220
|
+
const scratch = path.join(ws.on, "scratch");
|
|
221
|
+
const output = decide({ ...call(ws.on, "Bash", bashResponse(longOutput)), tool_use_id: "toolu_3", scratchpad_dir: scratch });
|
|
222
|
+
const { stdout } = output.hookSpecificOutput.updatedToolOutput;
|
|
223
|
+
assert.ok(stdout.includes(path.join(scratch, "outputs", "toolu_3.txt")));
|
|
224
|
+
assert.doesNotMatch(stdout, /Ask the summarizer/);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("a save that fails leaves the output whole and shows the hint", (t) => {
|
|
228
|
+
const ws = excerptWorkspace(t);
|
|
229
|
+
const blocker = path.join(ws.on, "blocker");
|
|
230
|
+
writeFileSync(blocker, "a regular file");
|
|
231
|
+
const output = decide({ ...call(ws.on, "Bash", bashResponse(longOutput)), tool_use_id: "toolu_4" }, path.join(blocker, "tmp"));
|
|
232
|
+
assert.equal(output.hookSpecificOutput.updatedToolOutput, undefined);
|
|
233
|
+
assert.match(contextOf(output) ?? "", /lfm-ask/);
|
|
234
|
+
});
|