1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
import { describe, expect, it, vi } from "vitest";
import {
type AvailableAgent,
createSummonTool,
type SummonCallbacks,
} from "../../src/tools/summon.js";
const noopCallbacks: SummonCallbacks = {
spawn: async () => "agent-id-stub",
getResult: async () => ({ status: "done", result: "" }),
};
describe("createSummonTool — description content", () => {
it("lists the agent directories so the LLM knows where to look", () => {
const tool = createSummonTool(
"/tmp/work",
noopCallbacks,
[],
[],
["/home/u/.config/dispatch/agents", "/tmp/work/.dispatch/agents"],
);
expect(tool.description).toContain("/home/u/.config/dispatch/agents");
expect(tool.description).toContain("/tmp/work/.dispatch/agents");
expect(tool.description).toContain("read_file");
});
it("includes available agent slugs+names in the description", () => {
const agents: AvailableAgent[] = [
{
slug: "programmer",
name: "Programmer",
description: "Implements code from a plan.",
path: "/home/u/.config/dispatch/agents/programmer.toml",
},
{
slug: "researcher",
name: "Researcher",
description: "Investigates topics.",
path: "/home/u/.config/dispatch/agents/researcher.toml",
},
];
const tool = createSummonTool(
"/tmp/work",
noopCallbacks,
agents,
[],
["/home/u/.config/dispatch/agents"],
);
expect(tool.description).toContain("programmer");
expect(tool.description).toContain("Programmer");
expect(tool.description).toContain("Implements code from a plan");
expect(tool.description).toContain("researcher");
expect(tool.description).toContain("Investigates topics");
});
it("emits a 'no agents defined' notice when the catalog is empty", () => {
const tool = createSummonTool(
"/tmp/work",
noopCallbacks,
[],
[],
["/home/u/.config/dispatch/agents"],
);
expect(tool.description).toContain("No agent definitions are currently defined");
});
it("shows two groups when userAgentEnabled is true", () => {
const subagents: AvailableAgent[] = [
{
slug: "programmer",
name: "Programmer",
description: "Codes things",
path: "/agents/programmer.toml",
},
];
const userAgents: AvailableAgent[] = [
{
slug: "default",
name: "Default",
description: "Default agent",
path: "/agents/default.toml",
},
];
const tool = createSummonTool(
"/tmp/work",
noopCallbacks,
subagents,
userAgents,
["/agents"],
true,
);
expect(tool.description).toContain("Subagents (spawned as child tabs):");
expect(tool.description).toContain(
"User agents (spawned as independent top-level tabs, requires top_level=true):",
);
expect(tool.description).toContain("programmer");
expect(tool.description).toContain("default");
});
it("hides user agents group when userAgentEnabled is false", () => {
const subagents: AvailableAgent[] = [
{
slug: "programmer",
name: "Programmer",
description: "Codes things",
path: "/agents/programmer.toml",
},
];
const userAgents: AvailableAgent[] = [
{
slug: "default",
name: "Default",
description: "Default agent",
path: "/agents/default.toml",
},
];
const tool = createSummonTool(
"/tmp/work",
noopCallbacks,
subagents,
userAgents,
["/agents"],
false,
);
expect(tool.description).toContain("Available agents:");
expect(tool.description).not.toContain("User agents");
// "default" appears in generic description text, so check for the slug listing format
expect(tool.description).not.toContain("- default: Default");
});
});
describe("createSummonTool — execute() argument forwarding", () => {
it("forwards agent slug through to callbacks.spawn", async () => {
const spawn = vi.fn(async () => "tab-xyz");
const tool = createSummonTool(
"/tmp/work",
{ spawn, getResult: async () => ({ status: "done", result: "ok" }) },
[],
[],
);
await tool.execute({
task: "do thing",
agent: "programmer",
background: true,
});
expect(spawn).toHaveBeenCalledTimes(1);
const callArg = spawn.mock.calls[0]?.[0];
expect(callArg).toMatchObject({
task: "do thing",
agentSlug: "programmer",
});
});
it("returns spawned agent_id when background=true (no blocking on result)", async () => {
const getResult = vi.fn(async () => ({ status: "done" as const, result: "should-not-see" }));
const tool = createSummonTool("/tmp/work", { spawn: async () => "id-42", getResult }, [], []);
const out = await tool.execute({ task: "x", agent: "test-agent", background: true });
expect(out).toContain("id-42");
// Background mode must not block on getResult
expect(getResult).not.toHaveBeenCalled();
});
it("blocks on result and returns it when background=false (default)", async () => {
const tool = createSummonTool(
"/tmp/work",
{
spawn: async () => "id-1",
getResult: async () => ({ status: "done", result: "child-output" }),
},
[],
[],
);
const out = await tool.execute({ task: "x", agent: "test-agent" });
// Foreground summons prefix the blocked result with `agent_id: <id>` so
// the frontend's ToolCallDisplay regex can surface the "Open Tab" button
// (see summon.ts). Assert both the prefix and the child output survive.
expect(out).toContain("agent_id: id-1");
expect(out).toBe("agent_id: id-1\n\nchild-output");
});
it("surfaces child errors when blocking", async () => {
const tool = createSummonTool(
"/tmp/work",
{
spawn: async () => "id-1",
getResult: async () => ({ status: "error", error: "boom" }),
},
[],
[],
);
const out = await tool.execute({ task: "x", agent: "test-agent" });
expect(out).toContain("boom");
});
it("returns fire-and-forget message when top_level=true", async () => {
const spawn = vi.fn(async () => "ua-tab-1");
const getResult = vi.fn(async () => ({ status: "done" as const, result: "nope" }));
const tool = createSummonTool(
"/tmp/work",
{ spawn, getResult },
[],
[],
[],
true, // userAgentEnabled
);
const out = await tool.execute({
task: "do stuff",
agent: "default",
top_level: true,
});
expect(out).toContain("User agent spawned successfully");
expect(out).toContain("ua-tab-1");
expect(out).toContain("fire-and-forget");
expect(getResult).not.toHaveBeenCalled();
// Verify topLevel was forwarded to spawn
const callArg = spawn.mock.calls[0]?.[0];
expect(callArg).toMatchObject({ topLevel: true });
});
it("ignores top_level when userAgentEnabled is false", async () => {
const spawn = vi.fn(async () => "tab-1");
const getResult = vi.fn(async () => ({ status: "done" as const, result: "result" }));
const tool = createSummonTool(
"/tmp/work",
{ spawn, getResult },
[],
[],
[],
false, // userAgentEnabled
);
const out = await tool.execute({
task: "do stuff",
agent: "default",
top_level: true, // should be ignored
});
// Should behave as a normal foreground summon, not fire-and-forget
expect(out).not.toContain("fire-and-forget");
expect(getResult).toHaveBeenCalled();
});
});
describe("createSummonTool — user-agent-only mode (perm_user_agent without perm_summon)", () => {
// userAgentEnabled=true, subagentEnabled=false → the tool spawns ONLY
// top-level user agents. `top_level` is implied (and forced), the
// subagent/parallel-work prose is dropped, and only the user-agent
// catalog group is shown.
const subagents: AvailableAgent[] = [
{
slug: "programmer",
name: "Programmer",
description: "Codes things",
path: "/agents/programmer.toml",
},
];
const userAgents: AvailableAgent[] = [
{
slug: "default",
name: "Default",
description: "Default agent",
path: "/agents/default.toml",
},
];
function userAgentOnlyTool(
spawn = vi.fn(async () => "ua-1"),
getResult = vi.fn(async () => ({ status: "done" as const, result: "nope" })),
) {
return {
spawn,
getResult,
tool: createSummonTool(
"/tmp/work",
{ spawn, getResult },
subagents,
userAgents,
["/agents"],
true, // userAgentEnabled
false, // subagentEnabled
),
};
}
it("describes spawning user agents and omits subagent/parallel-work prose", () => {
const { tool } = userAgentOnlyTool();
expect(tool.description).toContain("Spawn an independent top-level user agent");
expect(tool.description).toContain("fire-and-forget");
expect(tool.description).not.toContain("Pattern for parallel work");
expect(tool.description).not.toContain("Set background=true");
});
it("lists only the user-agent catalog group, not subagents", () => {
const { tool } = userAgentOnlyTool();
expect(tool.description).toContain("User agents (spawned as independent top-level tabs):");
expect(tool.description).toContain("default");
// Subagents must not be advertised in user-agent-only mode.
expect(tool.description).not.toContain("Subagents (spawned as child tabs):");
expect(tool.description).not.toContain("- programmer: Programmer");
});
it("only lists user-agent slugs in the 'agent' parameter description", () => {
const { tool } = userAgentOnlyTool();
const agentParam = (tool.parameters as unknown as { shape: { agent: { description: string } } })
.shape.agent;
expect(agentParam.description).toContain("default");
expect(agentParam.description).not.toContain("programmer");
});
it("omits the top_level parameter (it is implied)", () => {
const { tool } = userAgentOnlyTool();
const shape = (tool.parameters as unknown as { shape: Record<string, unknown> }).shape;
expect("top_level" in shape).toBe(false);
});
it("omits the background parameter (user agents are fire-and-forget)", () => {
const { tool } = userAgentOnlyTool();
const shape = (tool.parameters as unknown as { shape: Record<string, unknown> }).shape;
expect("background" in shape).toBe(false);
});
it("forces topLevel=true on spawn even when top_level is not passed", async () => {
const spawn = vi.fn(async () => "ua-99");
const getResult = vi.fn(async () => ({ status: "done" as const, result: "nope" }));
const { tool } = userAgentOnlyTool(spawn, getResult);
const out = await tool.execute({ task: "do stuff", agent: "default" });
expect(out).toContain("User agent spawned successfully");
expect(out).toContain("ua-99");
expect(out).toContain("fire-and-forget");
// Never blocks on a result for fire-and-forget user agents.
expect(getResult).not.toHaveBeenCalled();
const callArg = spawn.mock.calls[0]?.[0];
expect(callArg).toMatchObject({ topLevel: true, agentSlug: "default" });
});
});
describe("createSummonTool — subagentEnabled defaults preserve legacy behavior", () => {
it("defaults subagentEnabled=true so omitting it keeps subagent spawning", async () => {
const spawn = vi.fn(async () => "tab-1");
const getResult = vi.fn(async () => ({ status: "done" as const, result: "child" }));
// No userAgentEnabled/subagentEnabled args → legacy subagent-only mode.
const tool = createSummonTool("/tmp/work", { spawn, getResult }, [], []);
const out = await tool.execute({ task: "x", agent: "programmer" });
// Foreground subagent summon blocks and returns the child result.
expect(out).toBe("agent_id: tab-1\n\nchild");
expect(getResult).toHaveBeenCalled();
const callArg = spawn.mock.calls[0]?.[0];
expect(callArg).not.toHaveProperty("topLevel");
});
});
|