@firstpick/pi-package-webui 0.7.0 → 0.7.2

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.
@@ -0,0 +1,300 @@
1
+ import assert from "node:assert/strict";
2
+ import { registerSubagentGate } from "../lib/subagent-gate.mjs";
3
+
4
+ class EventBus {
5
+ constructor() { this.handlers = new Map(); }
6
+ on(name, handler) {
7
+ const handlers = this.handlers.get(name) || new Set();
8
+ handlers.add(handler);
9
+ this.handlers.set(name, handlers);
10
+ return () => handlers.delete(handler);
11
+ }
12
+ emit(name, value) {
13
+ for (const handler of [...(this.handlers.get(name) || [])]) handler(value);
14
+ }
15
+ }
16
+
17
+ function createHarness(script) {
18
+ const events = new EventBus();
19
+ let tool;
20
+ let runSerial = 0;
21
+ const requests = [];
22
+ const pi = {
23
+ events,
24
+ registerTool(value) { tool = value; },
25
+ };
26
+ const registration = registerSubagentGate(pi);
27
+ events.on("subagents:rpc:v1:request", (request) => {
28
+ requests.push(request);
29
+ if (request.method === "stop") {
30
+ events.emit(`subagents:rpc:v1:reply:${request.requestId}`, { version: 1, requestId: request.requestId, success: true, data: { state: "stopping" } });
31
+ return;
32
+ }
33
+ assert.equal(request.method, "spawn");
34
+ const next = script.shift();
35
+ assert.ok(next, `unexpected spawn request for ${request.params?.agent}`);
36
+ if (next.spawnError) {
37
+ events.emit(`subagents:rpc:v1:reply:${request.requestId}`, {
38
+ version: 1,
39
+ requestId: request.requestId,
40
+ success: false,
41
+ error: { code: next.code || "execution_failed", message: next.spawnError },
42
+ });
43
+ return;
44
+ }
45
+ const runId = next.runId || `gate-run-${++runSerial}`;
46
+ const respond = () => {
47
+ if (next.completeBeforeReply && !next.noCompletion) events.emit("subagent:async-complete", { runId, ...next.completion });
48
+ events.emit(`subagents:rpc:v1:reply:${request.requestId}`, {
49
+ version: 1,
50
+ requestId: request.requestId,
51
+ success: true,
52
+ data: { details: next.noRunId ? {} : { asyncId: runId } },
53
+ });
54
+ if (!next.completeBeforeReply && !next.noCompletion) queueMicrotask(() => events.emit("subagent:async-complete", { runId, ...next.completion }));
55
+ };
56
+ if (next.replyDelayMs) setTimeout(respond, next.replyDelayMs);
57
+ else respond();
58
+ });
59
+ return { tool, requests, dispose: () => registration.dispose() };
60
+ }
61
+
62
+ function completion({ agent = "reviewer", model = "anthropic/claude-opus-4-8", output = "Useful result", error, success = true, state = success ? "complete" : "failed", ...extra } = {}) {
63
+ return {
64
+ success,
65
+ state,
66
+ summary: error || output,
67
+ results: [{ agent, model, output, error, success, ...extra }],
68
+ };
69
+ }
70
+
71
+ async function execute(tool, params, signal = new AbortController().signal) {
72
+ return tool.execute("test-call", params, signal, undefined);
73
+ }
74
+
75
+ async function executeFailure(tool, params, signal = new AbortController().signal) {
76
+ try {
77
+ await execute(tool, params, signal);
78
+ assert.fail("expected subagent gate failure");
79
+ } catch (error) {
80
+ assert.equal(error.name, "SubagentGateError");
81
+ return error.details;
82
+ }
83
+ }
84
+
85
+ {
86
+ const harness = createHarness([{ completeBeforeReply: true, completion: completion() }]);
87
+ const result = await execute(harness.tool, { tasks: [{ agent: "reviewer", task: "Review" }] });
88
+ assert.equal(result.isError, undefined);
89
+ assert.equal(result.details.gate.status, "satisfied");
90
+ assert.equal(result.details.gate.qualifyingSuccesses, 1);
91
+ assert.equal(result.details.gate.attempts.length, 1, "completion emitted before the spawn reply should be recovered from the cache");
92
+ harness.dispose();
93
+ }
94
+
95
+ {
96
+ const harness = createHarness([
97
+ { completion: completion({ success: false, error: "provider overloaded (503)", output: "" }) },
98
+ { completion: completion({ model: "openrouter/moonshotai/kimi-k3", output: "Recovered result" }) },
99
+ ]);
100
+ const result = await execute(harness.tool, {
101
+ tasks: [{
102
+ agent: "reviewer",
103
+ task: "Review",
104
+ model: "anthropic/claude-opus-4-8",
105
+ fallbackModels: ["openrouter/moonshotai/kimi-k3"],
106
+ retrySafety: "read-only",
107
+ }],
108
+ });
109
+ assert.equal(result.details.gate.status, "satisfied");
110
+ assert.deepEqual(result.details.gate.attempts.map((attempt) => [attempt.status, attempt.failureKind, attempt.provider]), [
111
+ ["failed", "transient-provider", "anthropic"],
112
+ ["succeeded", undefined, "openrouter"],
113
+ ]);
114
+ assert.equal(harness.requests.filter((request) => request.method === "spawn").length, 2);
115
+ harness.dispose();
116
+ }
117
+
118
+ {
119
+ const harness = createHarness([{ completion: completion({ success: false, error: "provider overloaded (503)", output: "" }) }]);
120
+ const details = await executeFailure(harness.tool, {
121
+ tasks: [{ agent: "worker", task: "Edit files", retrySafety: "may-write" }],
122
+ maxAttemptsPerTask: 3,
123
+ });
124
+ assert.equal(details.gate.status, "failed");
125
+ assert.equal(details.gate.attempts.length, 1, "a started may-write task must not be automatically retried");
126
+ harness.dispose();
127
+ }
128
+
129
+ {
130
+ const harness = createHarness([
131
+ { spawnError: "temporary async runner startup failure" },
132
+ { completion: completion({ agent: "worker", model: "openai-codex/gpt-5.6-terra", output: "Writer started only after retry" }) },
133
+ ]);
134
+ const result = await execute(harness.tool, {
135
+ tasks: [{ agent: "worker", task: "Edit files", retrySafety: "may-write" }],
136
+ });
137
+ assert.equal(result.details.gate.status, "satisfied");
138
+ assert.equal(result.details.gate.attempts.length, 2, "a pre-launch failure may be retried even for may-write tasks");
139
+ assert.equal(result.details.gate.attempts[0].failureKind, "pre-launch");
140
+ harness.dispose();
141
+ }
142
+
143
+ {
144
+ const harness = createHarness([{ completion: completion({ success: false, state: "stopped", error: "Stopped by user", output: "", stopped: true }) }]);
145
+ const details = await executeFailure(harness.tool, {
146
+ tasks: [{ agent: "reviewer", task: "Review", retrySafety: "read-only" }],
147
+ maxAttemptsPerTask: 3,
148
+ });
149
+ assert.equal(details.gate.attempts.length, 1);
150
+ assert.equal(details.gate.attempts[0].failureKind, "stopped");
151
+ harness.dispose();
152
+ }
153
+
154
+ {
155
+ const harness = createHarness([
156
+ { completion: completion({ agent: "reviewer-a", model: "anthropic/claude-opus-4-8", output: "First provider result" }) },
157
+ { completion: completion({ agent: "reviewer-b", model: "anthropic/claude-fable-5", output: "Duplicate provider result" }) },
158
+ { completion: completion({ agent: "reviewer-b", model: "openrouter/moonshotai/kimi-k3", output: "Distinct provider result" }) },
159
+ ]);
160
+ const result = await execute(harness.tool, {
161
+ tasks: [
162
+ { agent: "reviewer-a", task: "Review A", model: "anthropic/claude-opus-4-8", retrySafety: "read-only" },
163
+ {
164
+ agent: "reviewer-b",
165
+ task: "Review B",
166
+ model: "anthropic/claude-fable-5",
167
+ fallbackModels: ["openrouter/moonshotai/kimi-k3"],
168
+ retrySafety: "read-only",
169
+ },
170
+ ],
171
+ requiredSuccesses: 2,
172
+ requireDistinctProviders: true,
173
+ concurrency: 1,
174
+ });
175
+ assert.equal(result.details.gate.status, "satisfied");
176
+ assert.equal(result.details.gate.qualifyingSuccesses, 2);
177
+ assert.deepEqual(result.details.gate.attempts.map((attempt) => [attempt.status, attempt.failureKind, attempt.provider]), [
178
+ ["succeeded", undefined, "anthropic"],
179
+ ["not-qualifying", "provider-diversity", "anthropic"],
180
+ ["succeeded", undefined, "openrouter"],
181
+ ]);
182
+ harness.dispose();
183
+ }
184
+
185
+ {
186
+ const harness = createHarness([{ noCompletion: true }]);
187
+ const controller = new AbortController();
188
+ queueMicrotask(() => controller.abort());
189
+ const details = await executeFailure(harness.tool, {
190
+ tasks: [{ agent: "reviewer", task: "Review", retrySafety: "read-only" }],
191
+ }, controller.signal);
192
+ assert.equal(details.gate.status, "cancelled");
193
+ assert.equal(details.gate.attempts[0].status, "cancelled", "a cancelled gate must not retain a misleading running attempt");
194
+ assert.ok(details.gate.attempts[0].endedAt);
195
+ assert.equal(harness.requests.filter((request) => request.method === "stop").length, 1, "cancelling the gate should stop its live async child");
196
+ harness.dispose();
197
+ }
198
+
199
+ {
200
+ const harness = createHarness([{ replyDelayMs: 10, noCompletion: true }]);
201
+ const controller = new AbortController();
202
+ queueMicrotask(() => controller.abort());
203
+ const details = await executeFailure(harness.tool, {
204
+ tasks: [{ agent: "worker", task: "Edit files", retrySafety: "may-write", phase: "implementation" }],
205
+ maxAttemptsPerTask: 3,
206
+ }, controller.signal);
207
+ assert.equal(details.gate.attempts.length, 1, "abort during spawn must not be classified as a retryable pre-launch failure");
208
+ assert.equal(details.gate.attempts[0].status, "cancelled");
209
+ assert.equal(details.gate.attempts[0].phase, "implementation");
210
+ assert.equal(harness.requests.filter((request) => request.method === "stop").length, 1, "a late spawn reply must still be reconciled and stopped");
211
+ harness.dispose();
212
+ }
213
+
214
+ {
215
+ const harness = createHarness([{ noRunId: true, noCompletion: true }]);
216
+ const details = await executeFailure(harness.tool, {
217
+ tasks: [{ agent: "worker", task: "Edit files", retrySafety: "may-write" }],
218
+ maxAttemptsPerTask: 3,
219
+ });
220
+ assert.equal(details.gate.attempts.length, 1, "a success reply without a run id is ambiguous and must not relaunch a writer");
221
+ assert.equal(details.gate.attempts[0].failureKind, "protocol-ambiguous");
222
+ harness.dispose();
223
+ }
224
+
225
+ {
226
+ const harness = createHarness([{ completion: completion({ success: false, state: "interrupted", error: "Paused for input", output: "", interrupted: true }) }]);
227
+ const details = await executeFailure(harness.tool, {
228
+ tasks: [{ agent: "reviewer", task: "Review", retrySafety: "read-only" }],
229
+ maxAttemptsPerTask: 3,
230
+ });
231
+ assert.equal(details.gate.attempts.length, 1);
232
+ assert.equal(details.gate.attempts[0].failureKind, "interrupted");
233
+ harness.dispose();
234
+ }
235
+
236
+ {
237
+ const harness = createHarness([
238
+ { completion: completion({ success: false, error: "provider overloaded (503)", output: "" }) },
239
+ { completion: completion({ success: false, error: "provider overloaded again (503)", output: "" }) },
240
+ ]);
241
+ const details = await executeFailure(harness.tool, {
242
+ tasks: [{ agent: "reviewer", task: "Review", retrySafety: "read-only" }],
243
+ maxAttemptsPerTask: 2,
244
+ });
245
+ assert.equal(details.gate.attempts.length, 2, "transient retries must stop at the explicit attempt budget");
246
+ assert.deepEqual(details.exhaustedSlots, [0]);
247
+ harness.dispose();
248
+ }
249
+
250
+ {
251
+ const harness = createHarness([]);
252
+ const details = await executeFailure(harness.tool, {
253
+ tasks: [{ agent: "reviewer", task: "Review", model: "anthropic/claude-opus-4-8", retrySafety: "read-only" }],
254
+ excludedProviders: ["anthropic"],
255
+ });
256
+ assert.equal(details.gate.attempts[0].failureKind, "provider-exhausted");
257
+ assert.equal(harness.requests.filter((request) => request.method === "spawn").length, 0, "known-excluded model candidates must not spend a child launch");
258
+ harness.dispose();
259
+ }
260
+
261
+ {
262
+ const harness = createHarness([{ completion: completion() }]);
263
+ const result = await execute(harness.tool, {
264
+ tasks: [
265
+ { agent: "reviewer", task: "Review A", retrySafety: "read-only" },
266
+ { agent: "reviewer", task: "Review B", retrySafety: "read-only" },
267
+ ],
268
+ requiredSuccesses: 1,
269
+ concurrency: 1,
270
+ });
271
+ assert.equal(harness.requests.filter((request) => request.method === "spawn").length, 1, "queued slots must not launch after quorum is already satisfied");
272
+ assert.deepEqual(result.details.skippedSlots, [1]);
273
+ harness.dispose();
274
+ }
275
+
276
+ {
277
+ const harness = createHarness([{ noCompletion: true }]);
278
+ const details = await executeFailure(harness.tool, {
279
+ tasks: [{ agent: "reviewer", task: "Review", retrySafety: "read-only" }],
280
+ gateTimeoutMs: 5,
281
+ });
282
+ assert.equal(details.gate.status, "failed");
283
+ assert.equal(details.gate.attempts[0].status, "cancelled");
284
+ assert.equal(details.gate.attempts[0].failureKind, "timeout");
285
+ assert.equal(harness.requests.filter((request) => request.method === "stop").length, 1);
286
+ harness.dispose();
287
+ }
288
+
289
+ {
290
+ const harness = createHarness([{ spawnError: "Unknown agent: missing", code: "invalid_params" }]);
291
+ const details = await executeFailure(harness.tool, {
292
+ tasks: [{ agent: "missing", task: "Do work", retrySafety: "read-only" }],
293
+ maxAttemptsPerTask: 3,
294
+ });
295
+ assert.equal(details.gate.attempts.length, 1, "deterministic invalid configuration must not be repeated blindly");
296
+ assert.equal(details.gate.attempts[0].failureKind, "configuration");
297
+ harness.dispose();
298
+ }
299
+
300
+ console.log("subagent-gate.test.mjs passed");
@@ -75,8 +75,8 @@ await writeFile(asyncStatusFile, JSON.stringify({
75
75
  startedAt: Date.now() - 1000,
76
76
  lastUpdate: Date.now(),
77
77
  steps: [
78
- { agent: "reviewer", status: "running", sessionFile: asyncSessionFile, recentOutput: [], currentTool: "bash", currentToolArgs: "sleep 5" },
79
- { agent: "scout", status: "running" },
78
+ { agent: "reviewer", status: "running", sessionFile: asyncSessionFile, recentOutput: [], currentTool: "bash", currentToolArgs: "sleep 5", model: "anthropic/claude-opus-4-8:high", thinking: "high" },
79
+ { agent: "scout", status: "running", model: "openai-codex/gpt-5.6-sol", thinking: "high" },
80
80
  ],
81
81
  }));
82
82
 
@@ -121,8 +121,28 @@ let payload = latestPayload();
121
121
  assert.equal(payload.available, true, "successful pi-subagents RPC should mark status available");
122
122
  assert.deepEqual(payload.runs.map((run) => run.id), ["run-a", "run-b"]);
123
123
  assert.deepEqual(payload.runs[0].agents.map((agent) => [agent.name, agent.currentTool]), [["reviewer", "read"], ["scout", "grep"]]);
124
+ assert.deepEqual(payload.runs[0].agents.map((agent) => [agent.model, agent.thinking]), [["anthropic/claude-opus-4-8:high", "high"], ["openai-codex/gpt-5.6-sol", "high"]], "async overview should publish effective lifecycle model and reasoning metadata");
124
125
  assert.deepEqual(payload.runs[1].agents.map((agent) => [agent.name, agent.nested]), [["worker", false], ["nested-oracle", true]]);
125
126
 
127
+ bus.emit("webui:subagent-gate:v1:update", {
128
+ version: 1,
129
+ id: "gate-a",
130
+ status: "running",
131
+ requiredSuccesses: 2,
132
+ qualifyingSuccesses: 1,
133
+ requireDistinctProviders: true,
134
+ startedAt: Date.now() - 2000,
135
+ updatedAt: Date.now(),
136
+ attempts: [
137
+ { id: "gate-a:0:1", taskIndex: 0, attempt: 1, maxAttempts: 2, agent: "reviewer", retrySafety: "read-only", runId: "review-1", model: "anthropic/claude-opus-4-8", provider: "anthropic", status: "succeeded" },
138
+ { id: "gate-a:1:1", taskIndex: 1, attempt: 1, maxAttempts: 2, agent: "reviewer", retrySafety: "read-only", runId: "review-2", model: "openrouter/moonshotai/kimi-k3", provider: "openrouter", status: "failed", failureKind: "transient-provider", error: "provider overloaded" },
139
+ ],
140
+ });
141
+ payload = latestPayload();
142
+ assert.equal(payload.gates.length, 1, "helper should publish retry gate lifecycle alongside running children");
143
+ assert.equal(payload.gates[0].qualifyingSuccesses, 1);
144
+ assert.deepEqual(payload.gates[0].attempts.map((attempt) => [attempt.status, attempt.failureKind]), [["succeeded", undefined], ["failed", "transient-provider"]]);
145
+
126
146
  const helperCommand = registeredCommands.get("webui-helper");
127
147
  assert.ok(helperCommand?.handler, "Web UI helper command should be registered");
128
148
  await helperCommand.handler(JSON.stringify({
@@ -134,6 +154,8 @@ const asyncOutputNotice = notifications.find((entry) => entry.message.includes("
134
154
  assert.ok(asyncOutputNotice, "async subagent output helper action should return a response notification");
135
155
  const asyncOutputResponse = JSON.parse(asyncOutputNotice.message.slice("__PI_WEBUI_HELPER_RESPONSE__:".length));
136
156
  assert.equal(asyncOutputResponse.ok, true);
157
+ assert.equal(asyncOutputResponse.data.agent.model, "anthropic/claude-opus-4-8:high");
158
+ assert.equal(asyncOutputResponse.data.agent.thinking, "high");
137
159
  assert.deepEqual(asyncOutputResponse.data.agent.recentOutput, [
138
160
  "REVIEWER STREAM 1 OF 18",
139
161
  "▶ bash {\"command\":\"sleep 5\"}",
@@ -222,12 +244,19 @@ for (const handler of extensionHandlers.get("tool_execution_start") || []) {
222
244
  type: "tool_execution_start",
223
245
  toolCallId: "foreground-call",
224
246
  toolName: "subagent",
225
- args: { tasks: [{ agent: "tester", task: "Run tests" }, { agent: "reviewer", task: "Review diff" }] },
247
+ args: {
248
+ model: "openai-codex/gpt-5.6-terra:xhigh",
249
+ tasks: [
250
+ { agent: "tester", task: "Run tests" },
251
+ { agent: "reviewer", task: "Review diff", model: "anthropic/claude-opus-4-8:high" },
252
+ ],
253
+ },
226
254
  }, ctx);
227
255
  }
228
256
  payload = latestPayload();
229
257
  let foreground = payload.runs.find((run) => run.source === "foreground");
230
258
  assert.deepEqual(foreground?.agents.map((agent) => agent.name), ["tester", "reviewer"], "foreground parallel children should appear while the tool runs");
259
+ assert.deepEqual(foreground?.agents.map((agent) => [agent.model, agent.thinking]), [["openai-codex/gpt-5.6-terra:xhigh", "xhigh"], ["anthropic/claude-opus-4-8:high", "high"]], "foreground overview should preserve run-level defaults and per-child model/reasoning overrides");
231
260
 
232
261
  for (const handler of extensionHandlers.get("tool_execution_update") || []) {
233
262
  handler({
@@ -272,6 +301,8 @@ const outputResponse = JSON.parse(outputNotice.message.slice("__PI_WEBUI_HELPER_
272
301
  assert.equal(outputResponse.ok, true);
273
302
  assert.equal(outputResponse.data.agent.currentTool, "bash");
274
303
  assert.equal(outputResponse.data.agent.currentToolArgs, "npm test");
304
+ assert.equal(outputResponse.data.agent.model, "openai-codex/gpt-5.6-terra:xhigh");
305
+ assert.equal(outputResponse.data.agent.thinking, "xhigh");
275
306
  assert.deepEqual(outputResponse.data.agent.recentOutput, ["Running focused tests", "12 assertions passed"]);
276
307
  assert.deepEqual(outputResponse.data.agent.transcript, [], "foreground snapshots without a child session transcript should retain the recentOutput-only fallback");
277
308
  assert.deepEqual(outputResponse.data.agent.recentTools, [{ tool: "read", args: "package.json", endMs: 1000 }]);