-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathprompt-template-bridge.test.ts
More file actions
366 lines (323 loc) · 11.6 KB
/
prompt-template-bridge.test.ts
File metadata and controls
366 lines (323 loc) · 11.6 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
PROMPT_TEMPLATE_SUBAGENT_CANCEL_EVENT,
PROMPT_TEMPLATE_SUBAGENT_REQUEST_EVENT,
PROMPT_TEMPLATE_SUBAGENT_RESPONSE_EVENT,
PROMPT_TEMPLATE_SUBAGENT_STARTED_EVENT,
PROMPT_TEMPLATE_SUBAGENT_UPDATE_EVENT,
registerPromptTemplateDelegationBridge,
type PromptTemplateBridgeEvents,
} from "./prompt-template-bridge.ts";
class FakeEvents implements PromptTemplateBridgeEvents {
private handlers = new Map<string, Array<(data: unknown) => void>>();
on(event: string, handler: (data: unknown) => void): () => void {
const list = this.handlers.get(event) ?? [];
list.push(handler);
this.handlers.set(event, list);
return () => {
const current = this.handlers.get(event) ?? [];
this.handlers.set(event, current.filter((h) => h !== handler));
};
}
emit(event: string, data: unknown): void {
const list = this.handlers.get(event) ?? [];
for (const handler of [...list]) handler(data);
}
}
function once(events: FakeEvents, event: string): Promise<unknown> {
return new Promise((resolve) => {
const unsubscribe = events.on(event, (payload) => {
unsubscribe();
resolve(payload);
});
});
}
describe("prompt-template delegation bridge", () => {
it("emits started/update/response on successful request", async () => {
const events = new FakeEvents();
let executeCalls = 0;
const bridge = registerPromptTemplateDelegationBridge({
events,
getContext: () => ({ cwd: "/repo" }),
execute: async (_requestId, _request, _signal, _ctx, onUpdate) => {
executeCalls++;
onUpdate({
details: {
results: [{ agent: "worker", model: "openai/gpt-5-mini" }],
progress: [{
index: 0,
agent: "worker",
currentTool: "read",
currentToolArgs: "index.ts",
recentOutput: ["line 1"],
recentTools: [{ tool: "read", args: '{"path":"index.ts"}' }],
toolCount: 1,
durationMs: 10,
tokens: 42,
}],
},
});
return {
details: {
results: [{ messages: [{ role: "assistant", content: [{ type: "text", text: "ok" }] }] }],
},
};
},
});
const startedPromise = once(events, PROMPT_TEMPLATE_SUBAGENT_STARTED_EVENT);
const updatePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_UPDATE_EVENT);
const responsePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_RESPONSE_EVENT);
events.emit(PROMPT_TEMPLATE_SUBAGENT_REQUEST_EVENT, {
requestId: "r1",
agent: "worker",
task: "do work",
context: "fresh",
model: "openai/gpt-5",
cwd: "/repo",
});
const started = await startedPromise as { requestId: string };
assert.equal(started.requestId, "r1");
const update = await updatePromise as {
requestId: string;
currentTool?: string;
toolCount?: number;
recentOutputLines?: string[];
recentTools?: Array<{ tool: string; args: string }>;
model?: string;
taskProgress?: Array<{ model?: string }>;
};
assert.equal(update.requestId, "r1");
assert.equal(update.currentTool, "read");
assert.equal(update.toolCount, 1);
assert.deepEqual(update.recentOutputLines, ["line 1"]);
assert.deepEqual(update.recentTools, [{ tool: "read", args: '{"path":"index.ts"}' }]);
assert.equal(update.model, "openai/gpt-5-mini");
assert.equal(update.taskProgress?.[0]?.model, "openai/gpt-5-mini");
const response = await responsePromise as { requestId: string; isError: boolean; messages: unknown[] };
assert.equal(response.requestId, "r1");
assert.equal(response.isError, false);
assert.equal(Array.isArray(response.messages), true);
assert.equal(executeCalls, 1);
bridge.dispose();
});
it("filters malformed recent output entries in updates", async () => {
const events = new FakeEvents();
const bridge = registerPromptTemplateDelegationBridge({
events,
getContext: () => ({ cwd: "/repo" }),
execute: async (_requestId, _request, _signal, _ctx, onUpdate) => {
onUpdate({
details: {
results: [{ agent: "worker", model: "openai/gpt-5-mini" }],
progress: [{
index: 0,
agent: "worker",
recentOutput: ["line 1", 123 as unknown as string],
}],
},
});
return { details: { results: [{ messages: [] }] } };
},
});
const updatePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_UPDATE_EVENT);
const responsePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_RESPONSE_EVENT);
events.emit(PROMPT_TEMPLATE_SUBAGENT_REQUEST_EVENT, {
requestId: "r-malformed-output",
agent: "worker",
task: "do work",
context: "fresh",
model: "openai/gpt-5",
cwd: "/repo",
});
const update = await updatePromise as {
recentOutput?: string;
recentOutputLines?: string[];
taskProgress?: Array<{ recentOutput?: string; recentOutputLines?: string[] }>;
};
assert.equal(update.recentOutput, undefined);
assert.deepEqual(update.recentOutputLines, ["line 1"]);
assert.equal(update.taskProgress?.[0]?.recentOutput, undefined);
assert.deepEqual(update.taskProgress?.[0]?.recentOutputLines, ["line 1"]);
await responsePromise;
bridge.dispose();
});
it("returns structured error when no active context", async () => {
const events = new FakeEvents();
const bridge = registerPromptTemplateDelegationBridge({
events,
getContext: () => null,
execute: async () => ({ details: { results: [{ messages: [] }] } }),
});
const responsePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_RESPONSE_EVENT);
events.emit(PROMPT_TEMPLATE_SUBAGENT_REQUEST_EVENT, {
requestId: "r2",
agent: "worker",
task: "do work",
context: "fresh",
model: "openai/gpt-5",
cwd: "/repo",
});
const response = await responsePromise as { isError: boolean; errorText?: string };
assert.equal(response.isError, true);
assert.match(response.errorText ?? "", /No active extension context/);
bridge.dispose();
});
it("accepts requests when delegated cwd differs from active context", async () => {
const events = new FakeEvents();
let executeCwd: string | undefined;
const bridge = registerPromptTemplateDelegationBridge({
events,
getContext: () => ({ cwd: "/actual" }),
execute: async (_requestId, request) => {
executeCwd = request.cwd;
return { details: { results: [{ messages: [] }] } };
},
});
const responsePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_RESPONSE_EVENT);
events.emit(PROMPT_TEMPLATE_SUBAGENT_REQUEST_EVENT, {
requestId: "r3",
agent: "worker",
task: "do work",
context: "fresh",
model: "openai/gpt-5",
cwd: "/repo",
});
const response = await responsePromise as { isError: boolean; errorText?: string };
assert.equal(response.isError, false);
assert.equal(executeCwd, "/repo");
bridge.dispose();
});
it("applies pending cancel when cancel arrives before request", async () => {
const events = new FakeEvents();
let executeCalls = 0;
const bridge = registerPromptTemplateDelegationBridge({
events,
getContext: () => ({ cwd: "/repo" }),
execute: async () => {
executeCalls++;
return { details: { results: [{ messages: [] }] } };
},
});
events.emit(PROMPT_TEMPLATE_SUBAGENT_CANCEL_EVENT, { requestId: "r4" });
const responsePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_RESPONSE_EVENT);
events.emit(PROMPT_TEMPLATE_SUBAGENT_REQUEST_EVENT, {
requestId: "r4",
agent: "worker",
task: "do work",
context: "fresh",
model: "openai/gpt-5",
cwd: "/repo",
});
const response = await responsePromise as { isError: boolean; errorText?: string };
assert.equal(response.isError, true);
assert.equal(response.errorText, "Delegated prompt cancelled.");
assert.equal(executeCalls, 0);
bridge.dispose();
});
it("cancels in-flight delegated execution", async () => {
const events = new FakeEvents();
const bridge = registerPromptTemplateDelegationBridge({
events,
getContext: () => ({ cwd: "/repo" }),
execute: async (_requestId, _request, signal) =>
await new Promise((_resolve, reject) => {
signal.addEventListener("abort", () => reject(new Error("aborted")), { once: true });
}),
});
const startedPromise = once(events, PROMPT_TEMPLATE_SUBAGENT_STARTED_EVENT);
const responsePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_RESPONSE_EVENT);
events.emit(PROMPT_TEMPLATE_SUBAGENT_REQUEST_EVENT, {
requestId: "r5",
agent: "worker",
task: "do work",
context: "fresh",
model: "openai/gpt-5",
cwd: "/repo",
});
await startedPromise;
events.emit(PROMPT_TEMPLATE_SUBAGENT_CANCEL_EVENT, { requestId: "r5" });
const response = await responsePromise as { isError: boolean; errorText?: string };
assert.equal(response.isError, true);
assert.match(response.errorText ?? "", /aborted/i);
bridge.dispose();
});
it("accepts tasks payloads and emits parallelResults", async () => {
const events = new FakeEvents();
let executeTasks: Array<{ agent: string; task: string; model?: string }> | undefined;
const bridge = registerPromptTemplateDelegationBridge({
events,
getContext: () => ({ cwd: "/repo" }),
execute: async (_requestId, request) => {
executeTasks = request.tasks;
return {
details: {
results: [
{ agent: "worker-a", messages: [{ role: "assistant", content: [{ type: "text", text: "a" }] }], exitCode: 0 },
{ agent: "worker-b", messages: [], exitCode: 1, error: "failed" },
],
},
};
},
});
const responsePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_RESPONSE_EVENT);
events.emit(PROMPT_TEMPLATE_SUBAGENT_REQUEST_EVENT, {
requestId: "r6",
tasks: [
{ agent: "worker-a", task: "A", model: "openai/gpt-5" },
{ agent: "worker-b", task: "B", model: "anthropic/claude-sonnet-4-20250514" },
],
context: "fresh",
model: "openai/gpt-5",
cwd: "/repo",
});
const response = await responsePromise as {
isError: boolean;
parallelResults?: Array<{ agent: string; isError: boolean; errorText?: string }>;
};
assert.equal(Array.isArray(executeTasks), true);
assert.equal(executeTasks?.length, 2);
assert.equal(executeTasks?.[0]?.model, "openai/gpt-5");
assert.equal(executeTasks?.[1]?.model, "anthropic/claude-sonnet-4-20250514");
assert.equal(response.isError, false);
assert.equal(response.parallelResults?.[0]?.agent, "worker-a");
assert.equal(response.parallelResults?.[0]?.isError, false);
assert.equal(response.parallelResults?.[1]?.agent, "worker-b");
assert.equal(response.parallelResults?.[1]?.isError, true);
assert.equal(response.parallelResults?.[1]?.errorText, "failed");
bridge.dispose();
});
it("marks missing parallel task results as errors", async () => {
const events = new FakeEvents();
const bridge = registerPromptTemplateDelegationBridge({
events,
getContext: () => ({ cwd: "/repo" }),
execute: async () => ({
details: {
results: [{ agent: "worker-a", messages: [{ role: "assistant", content: [{ type: "text", text: "ok" }] }], exitCode: 0 }],
},
}),
});
const responsePromise = once(events, PROMPT_TEMPLATE_SUBAGENT_RESPONSE_EVENT);
events.emit(PROMPT_TEMPLATE_SUBAGENT_REQUEST_EVENT, {
requestId: "r7",
tasks: [
{ agent: "worker-a", task: "A" },
{ agent: "worker-b", task: "B" },
],
context: "fresh",
model: "openai/gpt-5",
cwd: "/repo",
});
const response = await responsePromise as {
isError: boolean;
parallelResults?: Array<{ agent: string; isError: boolean; errorText?: string }>;
};
assert.equal(response.isError, false);
assert.equal(response.parallelResults?.[0]?.isError, false);
assert.equal(response.parallelResults?.[1]?.agent, "worker-b");
assert.equal(response.parallelResults?.[1]?.isError, true);
assert.match(response.parallelResults?.[1]?.errorText ?? "", /missing result/i);
bridge.dispose();
});
});