architecture-harness 0.14.0 → 0.16.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/dispatch-guard.mjs +5 -3
- package/src/cli/dispatch-guard.test.mjs +83 -0
- package/src/cli/dispatch-reminder.mjs +45 -9
- package/src/cli/dispatch-reminder.test.mjs +66 -2
- package/src/cli/dispatch-slot.mjs +151 -36
- package/src/cli/dispatch-slot.test.mjs +160 -11
- package/src/cli/dispatch.mjs +100 -8
- package/src/cli/dispatch.test.mjs +105 -1
- package/src/cli/explore-guard.mjs +211 -0
- package/src/cli/explore-guard.test.mjs +193 -0
- package/src/cli/explore-hint.mjs +85 -0
- package/src/cli/explore-hint.test.mjs +134 -0
- package/src/cli/plugin-agents.test.mjs +11 -5
- package/src/config.mjs +33 -2
|
@@ -1,18 +1,29 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
|
-
import { existsSync, mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { test } from "node:test";
|
|
6
6
|
import { dispatchDefaults } from "../config.mjs";
|
|
7
|
-
import { claimSlot, reclaimSlot, releaseSlot, slotDirOf
|
|
7
|
+
import { claimSlot, reclaimSlot, releaseSlot, slotDirOf } from "./dispatch-slot.mjs";
|
|
8
8
|
|
|
9
|
-
function sessionDir(t) {
|
|
9
|
+
function sessionDir(t, session = "s") {
|
|
10
10
|
const tmp = mkdtempSync(path.join(os.tmpdir(), "qc-dispatch-slot-"));
|
|
11
11
|
t.after(() => rmSync(tmp, { recursive: true, force: true }));
|
|
12
|
-
return slotDirOf(
|
|
12
|
+
return slotDirOf(session, tmp);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
const settings = { ...dispatchDefaults, slotMinutes: 1 };
|
|
16
|
+
const withSlots = (implementerSlots) => ({ ...settings, implementerSlots });
|
|
17
|
+
const IMPLEMENTER = "sdd-implementer";
|
|
18
|
+
|
|
19
|
+
/** The claim files of one session folder, sorted, with no path. */
|
|
20
|
+
const claimFiles = (dir) => (existsSync(dir) ? readdirSync(dir).filter((name) => name.endsWith(".claim")).sort() : []);
|
|
21
|
+
/** What each claim file holds, sorted. */
|
|
22
|
+
const heldIn = (dir) => claimFiles(dir).map((name) => readFileSync(path.join(dir, name), "utf8")).sort();
|
|
23
|
+
const ageClaim = (dir, name, ms) => {
|
|
24
|
+
const old = new Date(Date.now() - ms);
|
|
25
|
+
utimesSync(path.join(dir, name), old, old);
|
|
26
|
+
};
|
|
16
27
|
|
|
17
28
|
test("the slot folder is keyed on the session, with a file-safe name", () => {
|
|
18
29
|
assert.equal(slotDirOf("sess/1:a", "/tmp"), path.join("/tmp", "architecture-kit", "dispatch", "sess_1_a"));
|
|
@@ -22,7 +33,8 @@ test("the first claim holds the slot, and a second claim with no stop between ge
|
|
|
22
33
|
const dir = sessionDir(t);
|
|
23
34
|
assert.equal(claimSlot(dir, settings, Date.now()), null);
|
|
24
35
|
const holder = claimSlot(dir, settings, Date.now());
|
|
25
|
-
assert.equal(holder.slotFile,
|
|
36
|
+
assert.equal(holder.slotFile, path.join(dir, claimFiles(dir)[0]));
|
|
37
|
+
assert.equal(holder.limit, 1);
|
|
26
38
|
assert.equal(holder.expiresAt.getTime() - holder.claimedAt.getTime(), 60_000);
|
|
27
39
|
});
|
|
28
40
|
|
|
@@ -30,7 +42,7 @@ test("a claim older than slotMinutes is taken over", (t) => {
|
|
|
30
42
|
const dir = sessionDir(t);
|
|
31
43
|
claimSlot(dir, settings, Date.now());
|
|
32
44
|
const old = new Date(Date.now() - 120_000);
|
|
33
|
-
utimesSync(
|
|
45
|
+
utimesSync(path.join(dir, claimFiles(dir)[0]), old, old);
|
|
34
46
|
assert.equal(claimSlot(dir, settings, Date.now()), null);
|
|
35
47
|
});
|
|
36
48
|
|
|
@@ -39,9 +51,9 @@ test("only an implementer's stop frees the slot, and an empty agent type frees n
|
|
|
39
51
|
claimSlot(dir, settings, Date.now());
|
|
40
52
|
assert.equal(releaseSlot(dir, ""), false);
|
|
41
53
|
assert.equal(releaseSlot(dir, "architecture:sdd-reviewer"), false);
|
|
42
|
-
assert.equal(
|
|
54
|
+
assert.equal(claimFiles(dir).length, 1);
|
|
43
55
|
assert.equal(releaseSlot(dir, "architecture:sdd-implementer"), true);
|
|
44
|
-
assert.
|
|
56
|
+
assert.deepEqual(claimFiles(dir), []);
|
|
45
57
|
assert.equal(claimSlot(dir, settings, Date.now()), null);
|
|
46
58
|
});
|
|
47
59
|
|
|
@@ -57,7 +69,7 @@ test("a resumed implementer claims a free slot again, and a reviewer does not",
|
|
|
57
69
|
claimSlot(dir, settings, Date.now());
|
|
58
70
|
releaseSlot(dir, "sdd-implementer");
|
|
59
71
|
reclaimSlot(dir, "architecture:sdd-reviewer", Date.now());
|
|
60
|
-
assert.
|
|
72
|
+
assert.deepEqual(claimFiles(dir), []);
|
|
61
73
|
reclaimSlot(dir, "sdd-implementer", Date.now());
|
|
62
74
|
assert.notEqual(claimSlot(dir, settings, Date.now()), null);
|
|
63
75
|
});
|
|
@@ -69,12 +81,149 @@ test("a session folder with no settings, or settings that do not parse, changes
|
|
|
69
81
|
writeFileSync(path.join(dir, "settings.json"), "not json");
|
|
70
82
|
assert.equal(releaseSlot(dir, "sdd-implementer"), false);
|
|
71
83
|
reclaimSlot(dir, "sdd-implementer", Date.now());
|
|
72
|
-
assert.
|
|
84
|
+
assert.deepEqual(claimFiles(dir), []);
|
|
73
85
|
});
|
|
74
86
|
|
|
75
87
|
test("a claim whose settings cannot be written is released, and the error reaches the caller", (t) => {
|
|
76
88
|
const dir = sessionDir(t);
|
|
77
89
|
mkdirSync(path.join(dir, "settings.json"), { recursive: true });
|
|
78
90
|
assert.throws(() => claimSlot(dir, settings, Date.now()));
|
|
79
|
-
assert.
|
|
91
|
+
assert.deepEqual(claimFiles(dir), []);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("the default limit is 1, and a second holder is refused with the limit and the first holder", (t) => {
|
|
95
|
+
const dir = sessionDir(t);
|
|
96
|
+
assert.equal(dispatchDefaults.implementerSlots, 1);
|
|
97
|
+
assert.equal(claimSlot(dir, settings, Date.now(), "use-1"), null);
|
|
98
|
+
const refused = claimSlot(dir, settings, Date.now(), "use-2");
|
|
99
|
+
assert.equal(refused.limit, 1);
|
|
100
|
+
assert.deepEqual(refused.holders.map((holder) => holder.id), ["use-1"]);
|
|
101
|
+
assert.deepEqual(claimFiles(dir).length, 1);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("a limit of 3 holds three claims and refuses the fourth, naming the three holders", (t) => {
|
|
105
|
+
const dir = sessionDir(t);
|
|
106
|
+
for (const id of ["use-1", "use-2", "use-3"]) assert.equal(claimSlot(dir, withSlots(3), Date.now(), id), null);
|
|
107
|
+
const refused = claimSlot(dir, withSlots(3), Date.now(), "use-4");
|
|
108
|
+
assert.equal(refused.limit, 3);
|
|
109
|
+
assert.deepEqual(refused.holders.map((holder) => holder.id).sort(), ["use-1", "use-2", "use-3"]);
|
|
110
|
+
assert.equal(claimFiles(dir).length, 3);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("a stop frees its own holder's claim only, so one slot opens and the others stay", (t) => {
|
|
114
|
+
const dir = sessionDir(t);
|
|
115
|
+
const three = withSlots(3);
|
|
116
|
+
for (const id of ["use-1", "use-2", "use-3"]) claimSlot(dir, three, Date.now(), id);
|
|
117
|
+
for (const id of ["agent-a", "agent-b", "agent-c"]) reclaimSlot(dir, IMPLEMENTER, Date.now(), id);
|
|
118
|
+
assert.equal(claimFiles(dir).length, 3);
|
|
119
|
+
assert.equal(releaseSlot(dir, IMPLEMENTER, "agent-b"), true);
|
|
120
|
+
assert.equal(claimFiles(dir).length, 2);
|
|
121
|
+
assert.deepEqual(heldIn(dir), ["agent:agent-a", "agent:agent-c"]);
|
|
122
|
+
assert.equal(claimSlot(dir, three, Date.now(), "use-4"), null);
|
|
123
|
+
assert.notEqual(claimSlot(dir, three, Date.now(), "use-5"), null);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("a start after a dispatch binds the dispatch's claim and counts once", (t) => {
|
|
127
|
+
const dir = sessionDir(t);
|
|
128
|
+
const three = withSlots(3);
|
|
129
|
+
claimSlot(dir, three, Date.now(), "use-a");
|
|
130
|
+
reclaimSlot(dir, IMPLEMENTER, Date.now(), "agent-a");
|
|
131
|
+
assert.equal(claimFiles(dir).length, 1);
|
|
132
|
+
assert.equal(claimSlot(dir, three, Date.now(), "use-b"), null);
|
|
133
|
+
assert.equal(claimSlot(dir, three, Date.now(), "use-c"), null);
|
|
134
|
+
assert.notEqual(claimSlot(dir, three, Date.now(), "use-d"), null);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("a stop with no start before it frees one waiting claim", (t) => {
|
|
138
|
+
const dir = sessionDir(t);
|
|
139
|
+
claimSlot(dir, withSlots(2), Date.now(), "use-1");
|
|
140
|
+
claimSlot(dir, withSlots(2), Date.now(), "use-2");
|
|
141
|
+
assert.equal(releaseSlot(dir, IMPLEMENTER, "agent-x"), true);
|
|
142
|
+
assert.equal(claimFiles(dir).length, 1);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("an expired claim frees its slot and the live claims stay", (t) => {
|
|
146
|
+
const dir = sessionDir(t);
|
|
147
|
+
const three = withSlots(3);
|
|
148
|
+
for (const id of ["use-1", "use-2", "use-3"]) claimSlot(dir, three, Date.now(), id);
|
|
149
|
+
assert.notEqual(claimSlot(dir, three, Date.now(), "use-4"), null);
|
|
150
|
+
ageClaim(dir, claimFiles(dir)[0], 120_000);
|
|
151
|
+
assert.equal(claimSlot(dir, three, Date.now(), "use-4"), null);
|
|
152
|
+
assert.equal(claimFiles(dir).length, 3);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("the refusal names the holder that expires first", (t) => {
|
|
156
|
+
const dir = sessionDir(t);
|
|
157
|
+
const two = withSlots(2);
|
|
158
|
+
claimSlot(dir, two, Date.now(), "use-1");
|
|
159
|
+
claimSlot(dir, two, Date.now(), "use-2");
|
|
160
|
+
ageClaim(dir, claimFiles(dir).find((name) => readFileSync(path.join(dir, name), "utf8") === "pending:use-2"), 30_000);
|
|
161
|
+
const refused = claimSlot(dir, two, Date.now(), "use-3");
|
|
162
|
+
assert.ok(readFileSync(refused.slotFile, "utf8"), "pending:use-2");
|
|
163
|
+
assert.equal(refused.expiresAt.getTime() - refused.claimedAt.getTime(), 60_000);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("a resumed implementer takes its own slot again while one is free, and never past the limit", (t) => {
|
|
167
|
+
const dir = sessionDir(t);
|
|
168
|
+
const two = withSlots(2);
|
|
169
|
+
claimSlot(dir, two, Date.now(), "use-1");
|
|
170
|
+
reclaimSlot(dir, IMPLEMENTER, Date.now(), "agent-1");
|
|
171
|
+
releaseSlot(dir, IMPLEMENTER, "agent-1");
|
|
172
|
+
assert.deepEqual(claimFiles(dir), []);
|
|
173
|
+
reclaimSlot(dir, IMPLEMENTER, Date.now(), "agent-1");
|
|
174
|
+
assert.equal(claimFiles(dir).length, 1);
|
|
175
|
+
reclaimSlot(dir, IMPLEMENTER, Date.now(), "agent-1");
|
|
176
|
+
assert.equal(claimFiles(dir).length, 1);
|
|
177
|
+
reclaimSlot(dir, IMPLEMENTER, Date.now(), "agent-2");
|
|
178
|
+
reclaimSlot(dir, IMPLEMENTER, Date.now(), "agent-3");
|
|
179
|
+
assert.equal(claimFiles(dir).length, 2);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("a claim lands in the first slot it creates: a pre-created slot-0 sends it to slot-1 at 2, and refuses it at 1", (t) => {
|
|
183
|
+
const dir = sessionDir(t);
|
|
184
|
+
mkdirSync(dir, { recursive: true });
|
|
185
|
+
writeFileSync(path.join(dir, "slot-0.claim"), "pending:other");
|
|
186
|
+
assert.equal(claimSlot(dir, withSlots(2), Date.now(), "use-1"), null);
|
|
187
|
+
assert.deepEqual(claimFiles(dir), ["slot-0.claim", "slot-1.claim"]);
|
|
188
|
+
assert.equal(readFileSync(path.join(dir, "slot-0.claim"), "utf8"), "pending:other");
|
|
189
|
+
assert.equal(readFileSync(path.join(dir, "slot-1.claim"), "utf8"), "pending:use-1");
|
|
190
|
+
|
|
191
|
+
const one = sessionDir(t, "one");
|
|
192
|
+
mkdirSync(one, { recursive: true });
|
|
193
|
+
writeFileSync(path.join(one, "slot-0.claim"), "pending:other");
|
|
194
|
+
const refused = claimSlot(one, withSlots(1), Date.now(), "use-1");
|
|
195
|
+
assert.deepEqual(refused.holders.map((holder) => holder.id), ["other"]);
|
|
196
|
+
assert.deepEqual(claimFiles(one), ["slot-0.claim"]);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
test("every slot pre-created means the claim is refused and no file changes", (t) => {
|
|
200
|
+
const dir = sessionDir(t);
|
|
201
|
+
mkdirSync(dir, { recursive: true });
|
|
202
|
+
for (const index of [0, 1, 2]) writeFileSync(path.join(dir, `slot-${index}.claim`), `agent:a${index}`);
|
|
203
|
+
const refused = claimSlot(dir, withSlots(3), Date.now(), "use-1");
|
|
204
|
+
assert.equal(refused.limit, 3);
|
|
205
|
+
assert.deepEqual(heldIn(dir), ["agent:a0", "agent:a1", "agent:a2"]);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("a repeat of one holder's claim leaves its slot alone and takes no second slot", (t) => {
|
|
209
|
+
const dir = sessionDir(t);
|
|
210
|
+
const three = withSlots(3);
|
|
211
|
+
assert.equal(claimSlot(dir, three, Date.now(), "use-1"), null);
|
|
212
|
+
ageClaim(dir, "slot-0.claim", 10_000);
|
|
213
|
+
const aged = statSync(path.join(dir, "slot-0.claim")).mtimeMs;
|
|
214
|
+
assert.equal(claimSlot(dir, three, Date.now(), "use-1"), null);
|
|
215
|
+
assert.equal(statSync(path.join(dir, "slot-0.claim")).mtimeMs, aged);
|
|
216
|
+
assert.deepEqual(claimFiles(dir), ["slot-0.claim"]);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test("the stop and the start read the limit recorded at the claim, and a record with no limit means 1", (t) => {
|
|
220
|
+
const dir = sessionDir(t);
|
|
221
|
+
claimSlot(dir, withSlots(2), Date.now(), "use-1");
|
|
222
|
+
reclaimSlot(dir, IMPLEMENTER, Date.now(), "agent-1");
|
|
223
|
+
reclaimSlot(dir, IMPLEMENTER, Date.now(), "agent-2");
|
|
224
|
+
assert.equal(claimFiles(dir).length, 2);
|
|
225
|
+
const { implementerTypes, slotMinutes } = settings;
|
|
226
|
+
writeFileSync(path.join(dir, "settings.json"), JSON.stringify({ implementerTypes, slotMinutes }));
|
|
227
|
+
reclaimSlot(dir, IMPLEMENTER, Date.now(), "agent-3");
|
|
228
|
+
assert.equal(claimFiles(dir).length, 2);
|
|
80
229
|
});
|
package/src/cli/dispatch.mjs
CHANGED
|
@@ -3,17 +3,36 @@
|
|
|
3
3
|
|
|
4
4
|
import { existsSync } from "node:fs";
|
|
5
5
|
import path from "node:path";
|
|
6
|
-
import { CONFIG_FILE, dispatchDefaults, load, merge } from "../config.mjs";
|
|
6
|
+
import { CONFIG_FILE, MODEL_FAMILIES, dispatchDefaults, exploreDefaults, load, merge } from "../config.mjs";
|
|
7
7
|
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
8
8
|
|
|
9
9
|
const KEY = "swarm.dispatch";
|
|
10
|
+
const EXPLORE_KEY = "swarm.explore";
|
|
10
11
|
const DEFAULT_TYPE = "general-purpose";
|
|
11
12
|
const FORK = "fork";
|
|
13
|
+
const WILDCARD_TYPE = "*";
|
|
12
14
|
|
|
13
15
|
const isNameList = (value) => Array.isArray(value) && value.every((name) => typeof name === "string" && name !== "");
|
|
16
|
+
const isFamilyList = (value) => Array.isArray(value) && value.length > 0 && value.every((name) => MODEL_FAMILIES.includes(name));
|
|
14
17
|
const wrong = (key, value, want) =>
|
|
15
18
|
new Error(`${KEY}.${key} in qc.config.json must be ${want}, got ${JSON.stringify(value)}`);
|
|
16
19
|
|
|
20
|
+
function checkModels(models) {
|
|
21
|
+
const wantFamilies = `a non-empty list of known families (${MODEL_FAMILIES.join(", ")})`;
|
|
22
|
+
if (typeof models !== "object" || models === null || Array.isArray(models)) {
|
|
23
|
+
throw wrong("models", models, "an object mapping an agent type to its model families");
|
|
24
|
+
}
|
|
25
|
+
for (const [type, families] of Object.entries(models)) {
|
|
26
|
+
if (!isFamilyList(families)) throw wrong(`models.${type}`, families, wantFamilies);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The first family name a model string contains, lower-cased. Null when it names none the guard knows. */
|
|
31
|
+
export function familyOf(model) {
|
|
32
|
+
const lower = model.toLowerCase();
|
|
33
|
+
return MODEL_FAMILIES.find((family) => lower.includes(family)) ?? null;
|
|
34
|
+
}
|
|
35
|
+
|
|
17
36
|
/** @returns the dispatch settings, or null when the section is absent; throws naming the key a repository got wrong. */
|
|
18
37
|
export function dispatchSettings(swarm = {}) {
|
|
19
38
|
const raw = swarm.dispatch;
|
|
@@ -21,14 +40,21 @@ export function dispatchSettings(swarm = {}) {
|
|
|
21
40
|
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
22
41
|
throw new Error(`${KEY} in qc.config.json must be an object, got ${JSON.stringify(raw)}`);
|
|
23
42
|
}
|
|
24
|
-
const { allowedTypes, implementerTypes, maxPromptChars, slotMinutes } = merge(
|
|
43
|
+
const { allowedTypes, implementerTypes, maxPromptChars, slotMinutes, implementerSlots, models } = merge(
|
|
44
|
+
dispatchDefaults,
|
|
45
|
+
raw,
|
|
46
|
+
);
|
|
25
47
|
if (!isNameList(allowedTypes)) throw wrong("allowedTypes", allowedTypes, "a list of agent types");
|
|
26
48
|
if (!isNameList(implementerTypes)) throw wrong("implementerTypes", implementerTypes, "a list of agent types");
|
|
27
49
|
if (!Number.isInteger(maxPromptChars) || maxPromptChars < 1) {
|
|
28
50
|
throw wrong("maxPromptChars", maxPromptChars, "a positive whole number");
|
|
29
51
|
}
|
|
30
52
|
if (typeof slotMinutes !== "number" || !(slotMinutes > 0)) throw wrong("slotMinutes", slotMinutes, "a positive number");
|
|
31
|
-
|
|
53
|
+
if (!Number.isInteger(implementerSlots) || implementerSlots < 1) {
|
|
54
|
+
throw wrong("implementerSlots", implementerSlots, "a positive whole number");
|
|
55
|
+
}
|
|
56
|
+
checkModels(models);
|
|
57
|
+
return { allowedTypes, implementerTypes, maxPromptChars, slotMinutes, implementerSlots, models };
|
|
32
58
|
}
|
|
33
59
|
|
|
34
60
|
/** @returns the settings of the checkout that holds `cwd`, or null when it has no config or no section. */
|
|
@@ -38,6 +64,44 @@ export function dispatchSettingsAt(cwd) {
|
|
|
38
64
|
return dispatchSettings(load(root).swarm);
|
|
39
65
|
}
|
|
40
66
|
|
|
67
|
+
const isToolEntry = (entry) =>
|
|
68
|
+
isPlainRecord(entry) && ["name", "use", "how"].every((key) => typeof entry[key] === "string" && entry[key] !== "");
|
|
69
|
+
const isPlainRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
70
|
+
const wrongExplore = (key, value, want) =>
|
|
71
|
+
new Error(`${EXPLORE_KEY}.${key} in qc.config.json must be ${want}, got ${JSON.stringify(value)}`);
|
|
72
|
+
|
|
73
|
+
/** @returns the explore settings, or null when the section is absent; throws naming the key a repository got wrong. */
|
|
74
|
+
export function exploreSettings(swarm = {}) {
|
|
75
|
+
const raw = swarm.explore;
|
|
76
|
+
if (raw === undefined || raw === null || raw === false) return null;
|
|
77
|
+
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
78
|
+
throw new Error(`${EXPLORE_KEY} in qc.config.json must be an object, got ${JSON.stringify(raw)}`);
|
|
79
|
+
}
|
|
80
|
+
const { tools, summarizer, maxReadLines, maxGrepLines, maxOutputChars, exempt } = merge(exploreDefaults, raw);
|
|
81
|
+
if (!Array.isArray(tools) || !tools.every(isToolEntry)) {
|
|
82
|
+
throw wrongExplore("tools", tools, 'a list of { "name", "use", "how" } strings');
|
|
83
|
+
}
|
|
84
|
+
if (summarizer !== null && (typeof summarizer !== "string" || summarizer === "")) {
|
|
85
|
+
throw wrongExplore("summarizer", summarizer, "a non-empty string or null");
|
|
86
|
+
}
|
|
87
|
+
for (const [key, value] of [
|
|
88
|
+
["maxReadLines", maxReadLines],
|
|
89
|
+
["maxGrepLines", maxGrepLines],
|
|
90
|
+
["maxOutputChars", maxOutputChars],
|
|
91
|
+
]) {
|
|
92
|
+
if (!Number.isInteger(value) || value < 1) throw wrongExplore(key, value, "a positive whole number");
|
|
93
|
+
}
|
|
94
|
+
if (!isNameList(exempt)) throw wrongExplore("exempt", exempt, "a list of globs");
|
|
95
|
+
return { tools, summarizer, maxReadLines, maxGrepLines, maxOutputChars, exempt };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** @returns the explore settings of the checkout that holds `cwd`, or null when it has no config or no section. */
|
|
99
|
+
export function exploreSettingsAt(cwd) {
|
|
100
|
+
const root = checkoutRootOf(cwd);
|
|
101
|
+
if (!root || !existsSync(path.join(root, CONFIG_FILE))) return null;
|
|
102
|
+
return exploreSettings(load(root).swarm);
|
|
103
|
+
}
|
|
104
|
+
|
|
41
105
|
/** The type a dispatch names, trimmed. The Agent tool runs general-purpose when it names none. */
|
|
42
106
|
export const typeOf = (input) =>
|
|
43
107
|
typeof input.subagent_type === "string" && input.subagent_type.trim() !== "" ? input.subagent_type.trim() : DEFAULT_TYPE;
|
|
@@ -63,22 +127,50 @@ const promptRefusal = (length, max) =>
|
|
|
63
127
|
`Dispatch guard: the prompt has ${length} characters, over ${KEY}.maxPromptChars (${max}) in qc.config.json. ` +
|
|
64
128
|
"Write the brief to a file, and send a short prompt that names the brief and the report file.";
|
|
65
129
|
|
|
130
|
+
function tierRefusal(type, model, families) {
|
|
131
|
+
return (
|
|
132
|
+
`Dispatch guard: ${type} may run only on ${families.join(", ")}, so it refuses model "${model}". ` +
|
|
133
|
+
`Name a model of one of those families, or dispatch a type in ${KEY}.models with one that fits.`
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** @returns the reason a named model breaks its type's tier, or null when it fits. */
|
|
138
|
+
function tierRefusalFor(type, model, settings) {
|
|
139
|
+
const families = settings.models[type] ?? settings.models[WILDCARD_TYPE];
|
|
140
|
+
const family = familyOf(model);
|
|
141
|
+
if (family && families.includes(family)) return null;
|
|
142
|
+
return tierRefusal(type, model, families);
|
|
143
|
+
}
|
|
144
|
+
|
|
66
145
|
/** @returns the reason the dispatch breaks a rule that needs no state, or null to let it through. */
|
|
67
146
|
export function dispatchRefusal(input, settings) {
|
|
68
147
|
const type = typeOf(input);
|
|
69
148
|
const model = typeof input.model === "string" ? input.model.trim() : "";
|
|
70
149
|
const allowed = settings.allowedTypes.includes(type);
|
|
71
150
|
if (!allowed && (type === FORK || model === "")) return modelRefusal(type, settings);
|
|
151
|
+
if (model !== "" && type !== FORK) {
|
|
152
|
+
const refusal = tierRefusalFor(type, model, settings);
|
|
153
|
+
if (refusal) return refusal;
|
|
154
|
+
}
|
|
72
155
|
const length = typeof input.prompt === "string" ? input.prompt.length : 0;
|
|
73
156
|
if (length > settings.maxPromptChars) return promptRefusal(length, settings.maxPromptChars);
|
|
74
157
|
return null;
|
|
75
158
|
}
|
|
76
159
|
|
|
77
|
-
/** The reason
|
|
78
|
-
export function slotRefusal({ type, claimedAt, expiresAt, slotFile }) {
|
|
160
|
+
/** The reason an implementer over the limit waits, with the two ways a slot frees. */
|
|
161
|
+
export function slotRefusal({ type, claimedAt, expiresAt, slotFile, limit = 1, holders = [] }) {
|
|
162
|
+
const frees = `If no implementer runs, because its dispatch was denied after this hook, delete ${slotFile}.`;
|
|
163
|
+
if (limit === 1) {
|
|
164
|
+
return (
|
|
165
|
+
`Dispatch guard: an implementer holds the one implementer slot of this session since ${claimedAt.toISOString()}. ` +
|
|
166
|
+
`Wait for it to stop, then dispatch ${type}. The slot frees when it stops, or at ${expiresAt.toISOString()}. ` +
|
|
167
|
+
frees
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
const named = holders.map((holder) => `${holder.id} (since ${holder.claimedAt.toISOString()})`).join(", ");
|
|
79
171
|
return (
|
|
80
|
-
`Dispatch guard:
|
|
81
|
-
`Wait for
|
|
82
|
-
|
|
172
|
+
`Dispatch guard: all ${limit} implementer slots of this session are held, by ${named}. ` +
|
|
173
|
+
`Wait for one to stop, then dispatch ${type}. A slot frees when its holder stops, and by ${expiresAt.toISOString()} at the latest. ` +
|
|
174
|
+
frees
|
|
83
175
|
);
|
|
84
176
|
}
|
|
@@ -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, isImplementer, slotRefusal, typeOf } from "./dispatch.mjs";
|
|
4
|
+
import { dispatchRefusal, dispatchSettings, 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);
|
|
@@ -28,11 +28,34 @@ test("a wrong value names its key", () => {
|
|
|
28
28
|
[{ implementerTypes: [""] }, /swarm\.dispatch\.implementerTypes/],
|
|
29
29
|
[{ maxPromptChars: 0 }, /swarm\.dispatch\.maxPromptChars/],
|
|
30
30
|
[{ slotMinutes: -1 }, /swarm\.dispatch\.slotMinutes/],
|
|
31
|
+
[{ implementerSlots: 0 }, /swarm\.dispatch\.implementerSlots/],
|
|
32
|
+
[{ implementerSlots: -1 }, /swarm\.dispatch\.implementerSlots/],
|
|
33
|
+
[{ implementerSlots: 1.5 }, /swarm\.dispatch\.implementerSlots/],
|
|
34
|
+
[{ implementerSlots: "2" }, /swarm\.dispatch\.implementerSlots/],
|
|
35
|
+
[{ implementerSlots: null }, /swarm\.dispatch\.implementerSlots/],
|
|
36
|
+
[{ models: "opus" }, /swarm\.dispatch\.models\b/],
|
|
37
|
+
[{ models: ["opus"] }, /swarm\.dispatch\.models\b/],
|
|
38
|
+
[{ models: { "sdd-planner": [] } }, /swarm\.dispatch\.models\.sdd-planner/],
|
|
39
|
+
[{ models: { "sdd-planner": ["gpt"] } }, /swarm\.dispatch\.models\.sdd-planner/],
|
|
31
40
|
]) {
|
|
32
41
|
assert.throws(() => dispatchSettings({ dispatch }), key, JSON.stringify(dispatch));
|
|
33
42
|
}
|
|
34
43
|
});
|
|
35
44
|
|
|
45
|
+
test("a repository's models merges key by key with the defaults", () => {
|
|
46
|
+
const settings = dispatchSettings({ dispatch: { models: { "architecture:sdd-planner": ["sonnet"] } } });
|
|
47
|
+
assert.deepEqual(settings.models["architecture:sdd-planner"], ["sonnet"]);
|
|
48
|
+
assert.deepEqual(settings.models["architecture:sdd-implementer"], dispatchDefaults.models["architecture:sdd-implementer"]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("familyOf finds the first known family a model string contains, lower-cased", () => {
|
|
52
|
+
assert.equal(familyOf("sonnet"), "sonnet");
|
|
53
|
+
assert.equal(familyOf("claude-sonnet-5"), "sonnet");
|
|
54
|
+
assert.equal(familyOf("opus[1m]"), "opus");
|
|
55
|
+
assert.equal(familyOf("OPUS"), "opus");
|
|
56
|
+
assert.equal(familyOf("gpt-4"), null);
|
|
57
|
+
});
|
|
58
|
+
|
|
36
59
|
const on = dispatchSettings({ dispatch: { maxPromptChars: 20 } });
|
|
37
60
|
|
|
38
61
|
test("a dispatch with neither a model nor an allowed type is refused, and the reason names both fixes", () => {
|
|
@@ -68,6 +91,52 @@ test("the model rule comes before the length rule", () => {
|
|
|
68
91
|
assert.match(dispatchRefusal({ prompt: "x".repeat(21) }, on) ?? "", /names no model/);
|
|
69
92
|
});
|
|
70
93
|
|
|
94
|
+
test("a planner on sonnet is refused, and an implementer on opus is refused", () => {
|
|
95
|
+
const planner = dispatchRefusal({ subagent_type: "architecture:sdd-planner", model: "sonnet" }, on) ?? "";
|
|
96
|
+
assert.match(planner, /architecture:sdd-planner/);
|
|
97
|
+
assert.match(planner, /"sonnet"/);
|
|
98
|
+
assert.match(planner, /opus/);
|
|
99
|
+
const implementer = dispatchRefusal({ subagent_type: "architecture:sdd-implementer", model: "opus" }, on) ?? "";
|
|
100
|
+
assert.match(implementer, /architecture:sdd-implementer/);
|
|
101
|
+
assert.match(implementer, /"opus"/);
|
|
102
|
+
assert.match(implementer, /sonnet/);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("a general-purpose dispatch on opus is refused, naming the wildcard's families", () => {
|
|
106
|
+
const reason = dispatchRefusal({ model: "opus" }, on) ?? "";
|
|
107
|
+
assert.match(reason, /general-purpose/);
|
|
108
|
+
assert.match(reason, /sonnet, haiku/);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("a model string with no known family is refused", () => {
|
|
112
|
+
assert.match(dispatchRefusal({ model: "gpt-4" }, on) ?? "", /general-purpose/);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("each allowed pair passes, including a full model id that resolves to its family", () => {
|
|
116
|
+
assert.equal(dispatchRefusal({ subagent_type: "architecture:sdd-planner", model: "opus" }, on), null);
|
|
117
|
+
assert.equal(dispatchRefusal({ subagent_type: "architecture:sdd-branch-reviewer", model: "opus" }, on), null);
|
|
118
|
+
assert.equal(dispatchRefusal({ subagent_type: "architecture:sdd-implementer", model: "sonnet" }, on), null);
|
|
119
|
+
assert.equal(dispatchRefusal({ subagent_type: "architecture:sdd-reviewer", model: "sonnet" }, on), null);
|
|
120
|
+
assert.equal(dispatchRefusal({ model: "haiku" }, on), null);
|
|
121
|
+
assert.equal(dispatchRefusal({ subagent_type: "architecture:sdd-implementer", model: "claude-sonnet-5" }, on), null);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("a fork keeps its current rule and never reaches the tier check", () => {
|
|
125
|
+
assert.match(
|
|
126
|
+
dispatchRefusal({ subagent_type: "fork", model: "gpt-4" }, on) ?? "",
|
|
127
|
+
/`fork` is in swarm\.dispatch\.allowedTypes/,
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("a repository override replaces one type's families and leaves the rest at default", () => {
|
|
132
|
+
const withOverride = dispatchSettings({ dispatch: { models: { "architecture:sdd-planner": ["sonnet"] } } });
|
|
133
|
+
assert.equal(dispatchRefusal({ subagent_type: "architecture:sdd-planner", model: "sonnet" }, withOverride), null);
|
|
134
|
+
assert.match(
|
|
135
|
+
dispatchRefusal({ subagent_type: "architecture:sdd-implementer", model: "opus" }, withOverride) ?? "",
|
|
136
|
+
/architecture:sdd-implementer/,
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
|
|
71
140
|
test("an omitted type is general-purpose, and only a listed type is an implementer", () => {
|
|
72
141
|
assert.equal(typeOf({}), "general-purpose");
|
|
73
142
|
assert.equal(typeOf({ subagent_type: "" }), "general-purpose");
|
|
@@ -88,3 +157,38 @@ test("the slot refusal names the expiry and the file that frees the slot", () =>
|
|
|
88
157
|
assert.match(reason, /1970-01-01T01:00:00\.000Z/);
|
|
89
158
|
assert.match(reason, /delete \/tmp\/slot\.claim/);
|
|
90
159
|
});
|
|
160
|
+
|
|
161
|
+
test("implementerSlots defaults to 1, and a repository sets a whole number", () => {
|
|
162
|
+
assert.equal(dispatchSettings({ dispatch: {} }).implementerSlots, 1);
|
|
163
|
+
assert.equal(dispatchSettings({ dispatch: { implementerSlots: 3 } }).implementerSlots, 3);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("a refusal over a limit above 1 names the limit and each holder, and one at 1 keeps its text", () => {
|
|
167
|
+
const holders = [
|
|
168
|
+
{ id: "use-1", file: "/tmp/pending-use-1.claim", claimedAt: new Date(0) },
|
|
169
|
+
{ id: "agent-2", file: "/tmp/agent-agent-2.claim", claimedAt: new Date(1000) },
|
|
170
|
+
];
|
|
171
|
+
const reason = slotRefusal({
|
|
172
|
+
type: "sdd-implementer",
|
|
173
|
+
claimedAt: new Date(0),
|
|
174
|
+
expiresAt: new Date(3_600_000),
|
|
175
|
+
slotFile: "/tmp/pending-use-1.claim",
|
|
176
|
+
limit: 2,
|
|
177
|
+
holders,
|
|
178
|
+
});
|
|
179
|
+
assert.match(reason, /all 2 implementer slots/);
|
|
180
|
+
assert.match(reason, /use-1/);
|
|
181
|
+
assert.match(reason, /agent-2/);
|
|
182
|
+
assert.match(reason, /1970-01-01T01:00:00\.000Z/);
|
|
183
|
+
assert.match(reason, /delete \/tmp\/pending-use-1\.claim/);
|
|
184
|
+
const one = slotRefusal({
|
|
185
|
+
type: "sdd-implementer",
|
|
186
|
+
claimedAt: new Date(0),
|
|
187
|
+
expiresAt: new Date(3_600_000),
|
|
188
|
+
slotFile: "/tmp/slot.claim",
|
|
189
|
+
limit: 1,
|
|
190
|
+
holders: holders.slice(0, 1),
|
|
191
|
+
});
|
|
192
|
+
assert.match(one, /holds the one implementer slot/);
|
|
193
|
+
assert.doesNotMatch(one, /use-1/);
|
|
194
|
+
});
|