@firstpick/pi-package-webui 0.6.9 → 0.7.1

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,157 @@
1
+ import assert from "node:assert/strict";
2
+ import { spawn } from "node:child_process";
3
+ import { chmod, mkdtemp, rm } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import path from "node:path";
6
+ import { setTimeout as delay } from "node:timers/promises";
7
+ import { dirname, join } from "node:path";
8
+ import { fileURLToPath } from "node:url";
9
+
10
+ const root = join(dirname(fileURLToPath(import.meta.url)), "..");
11
+ const serverScript = join(root, "bin", "pi-webui.mjs");
12
+ const fakePi = join(root, "tests", "fixtures", "fake-pi.mjs");
13
+ const PI_RPC_JSONL_LINE_MAX_BYTES = 32 * 1024 * 1024;
14
+
15
+ function nextPort() {
16
+ return 32000 + Math.floor(Math.random() * 20000);
17
+ }
18
+
19
+ async function request(port, pathname, { method = "GET", body, timeoutMs = 10_000 } = {}) {
20
+ const response = await fetch(`http://127.0.0.1:${port}${pathname}`, {
21
+ method,
22
+ headers: body === undefined ? undefined : { "content-type": "application/json" },
23
+ body: body === undefined ? undefined : JSON.stringify(body),
24
+ signal: AbortSignal.timeout(timeoutMs),
25
+ });
26
+ let payload;
27
+ try {
28
+ payload = await response.json();
29
+ } catch {
30
+ payload = undefined;
31
+ }
32
+ return { status: response.status, body: payload };
33
+ }
34
+
35
+ async function waitForHealth(port, child, output) {
36
+ for (let attempt = 0; attempt < 100; attempt += 1) {
37
+ if (child.exitCode !== null) break;
38
+ try {
39
+ const health = await request(port, "/api/health", { timeoutMs: 1_000 });
40
+ if (health.status === 200) return health;
41
+ } catch {
42
+ // The server is still starting.
43
+ }
44
+ await delay(50);
45
+ }
46
+ assert.fail(`server did not become healthy:\n${output()}`);
47
+ }
48
+
49
+ async function waitForSseEvent(port, tabId, predicate, trigger) {
50
+ const controller = new AbortController();
51
+ const timeout = setTimeout(() => controller.abort(new Error("timed out waiting for SSE event")), 30_000);
52
+ try {
53
+ const response = await fetch(`http://127.0.0.1:${port}/api/events?tab=${encodeURIComponent(tabId)}`, { signal: controller.signal });
54
+ assert.equal(response.status, 200, "SSE connection should open");
55
+ const triggerPromise = Promise.resolve().then(trigger);
56
+ const reader = response.body.getReader();
57
+ const decoder = new TextDecoder();
58
+ let buffer = "";
59
+ while (true) {
60
+ const { done, value } = await reader.read();
61
+ if (done) break;
62
+ buffer += decoder.decode(value, { stream: true });
63
+ let boundary;
64
+ while ((boundary = buffer.indexOf("\n\n")) >= 0) {
65
+ const block = buffer.slice(0, boundary);
66
+ buffer = buffer.slice(boundary + 2);
67
+ const data = block.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trim()).join("\n");
68
+ if (!data) continue;
69
+ const event = JSON.parse(data);
70
+ if (predicate(event)) return { event, triggerResult: await triggerPromise };
71
+ }
72
+ }
73
+ throw new Error("SSE stream ended before the expected event");
74
+ } finally {
75
+ clearTimeout(timeout);
76
+ controller.abort();
77
+ }
78
+ }
79
+
80
+ async function stopServerProcess(child) {
81
+ if (!child || child.exitCode !== null || child.signalCode !== null) return;
82
+ const exited = new Promise((resolve) => child.once("exit", resolve));
83
+ child.kill("SIGKILL");
84
+ await exited;
85
+ }
86
+
87
+ async function startServer({ oversizedLineBytes = 0 } = {}) {
88
+ const cwd = await mkdtemp(path.join(tmpdir(), "pi-webui-transport-hardening-"));
89
+ const port = nextPort();
90
+ let child;
91
+ try {
92
+ await chmod(fakePi, 0o755);
93
+ let output = "";
94
+ child = spawn(process.execPath, [serverScript, "--cwd", cwd, "--host", "127.0.0.1", "--port", String(port), "--pi", fakePi], {
95
+ stdio: ["ignore", "pipe", "pipe"],
96
+ env: {
97
+ ...process.env,
98
+ PI_CODING_AGENT_DIR: path.join(cwd, "agent"),
99
+ ...(oversizedLineBytes ? { FAKE_PI_OVERSIZED_JSONL_BYTES: String(oversizedLineBytes) } : {}),
100
+ },
101
+ });
102
+ child.stdout.on("data", (chunk) => { output += String(chunk); });
103
+ child.stderr.on("data", (chunk) => { output += String(chunk); });
104
+ await waitForHealth(port, child, () => output);
105
+ const tabs = await request(port, "/api/tabs");
106
+ const tabId = tabs.body?.data?.tabs?.[0]?.id;
107
+ assert.ok(tabId, "server should create an initial RPC tab");
108
+ return { child, cwd, output: () => output, port, tabId };
109
+ } catch (error) {
110
+ await stopServerProcess(child);
111
+ await rm(cwd, { recursive: true, force: true });
112
+ throw error;
113
+ }
114
+ }
115
+
116
+ async function stopServer(server) {
117
+ await stopServerProcess(server.child);
118
+ await rm(server.cwd, { recursive: true, force: true });
119
+ }
120
+
121
+ let server;
122
+ try {
123
+ server = await startServer();
124
+ // Closing the parent-side reader reproduces a server stderr sink that can no
125
+ // longer accept Pi diagnostics. The server must keep serving its browser API.
126
+ server.child.stderr.destroy();
127
+ await delay(25);
128
+ const stderrResult = await waitForSseEvent(
129
+ server.port,
130
+ server.tabId,
131
+ (event) => event.type === "pi_stderr_sink_error",
132
+ () => request(server.port, "/api/prompt", { method: "POST", body: { tab: server.tabId, message: "fixture stderr diagnostic" } }),
133
+ );
134
+ assert.equal(stderrResult.triggerResult.status, 200, "Pi stderr diagnostics should not fail the prompt request when server stderr is closed");
135
+ assert.match(stderrResult.event.error || "", /stderr could not be mirrored/i, "browser clients should receive the stderr sink diagnostic");
136
+ await delay(50);
137
+ assert.equal(server.child.exitCode, null, "closed server stderr must not terminate the WebUI process");
138
+ assert.equal((await request(server.port, "/api/health")).status, 200, "WebUI should remain healthy after the closed-stderr diagnostic");
139
+ await stopServer(server);
140
+ server = null;
141
+
142
+ server = await startServer({ oversizedLineBytes: PI_RPC_JSONL_LINE_MAX_BYTES + 1024 });
143
+ const oversizedResult = await waitForSseEvent(
144
+ server.port,
145
+ server.tabId,
146
+ (event) => event.type === "pi_stdout_line_too_large",
147
+ () => request(server.port, "/api/prompt", { method: "POST", timeoutMs: 30_000, body: { tab: server.tabId, message: "fixture oversized jsonl" } }),
148
+ );
149
+ assert.equal(oversizedResult.event.maxBytes, PI_RPC_JSONL_LINE_MAX_BYTES, "the discard diagnostic should expose the explicit physical-line limit");
150
+ assert.equal(oversizedResult.triggerResult.status, 200, "the valid RPC response after an oversized unterminated line should still be parsed");
151
+ assert.equal(oversizedResult.triggerResult.body?.data?.output, "fake oversized JSONL line discarded");
152
+ assert.equal((await request(server.port, `/api/state?tab=${encodeURIComponent(server.tabId)}`)).status, 200, "the server should continue parsing later RPC traffic after discarding the oversized line");
153
+ } finally {
154
+ if (server) await stopServer(server);
155
+ }
156
+
157
+ console.log("transport hardening harness passed");
@@ -231,38 +231,120 @@ function subagentRecentTools(value) {
231
231
  })).filter((entry) => entry.tool);
232
232
  }
233
233
 
234
- function subagentTranscriptPartLines(part) {
235
- if (!part || typeof part !== "object") return [];
236
- if (part.type === "text" && typeof part.text === "string") return part.text.split(/\r?\n/);
237
- if (part.type === "toolCall") {
238
- const name = subagentText(part.name, 120) || "tool";
239
- let args = "";
240
- try {
241
- args = typeof part.arguments === "string" ? part.arguments : JSON.stringify(part.arguments || {});
242
- } catch {
243
- args = "";
234
+ function subagentTranscriptText(value) {
235
+ return String(value ?? "").replace(/\r/g, "").slice(0, SUBAGENT_OUTPUT_LINE_LENGTH);
236
+ }
237
+
238
+ function subagentTranscriptTextLines(value) {
239
+ return String(value ?? "").replace(/\r/g, "").split("\n").map((line) => line.slice(0, SUBAGENT_OUTPUT_LINE_LENGTH));
240
+ }
241
+
242
+ function subagentTranscriptToolArguments(part) {
243
+ const value = part?.arguments ?? part?.args ?? part?.input ?? part?.toolCall?.arguments ?? {};
244
+ try {
245
+ return subagentTranscriptText(typeof value === "string" ? value : JSON.stringify(value));
246
+ } catch {
247
+ return "{}";
248
+ }
249
+ }
250
+
251
+ function subagentTranscriptMessageCandidates(message, timestamp, sourceId) {
252
+ const candidates = [];
253
+ const addText = (type, value, partIndex, output = type === "text") => {
254
+ if (typeof value !== "string") return;
255
+ for (const line of subagentTranscriptTextLines(value)) {
256
+ if (line.trim().toLowerCase() === "(no output)") continue;
257
+ candidates.push({
258
+ sourceId,
259
+ role: message.role,
260
+ timestamp,
261
+ toolCallId: subagentText(message.toolCallId || message.tool_call_id, 240),
262
+ toolName: subagentText(message.toolName || message.name, 120),
263
+ isError: message.isError === true,
264
+ partIndex,
265
+ part: type === "thinking" ? { type, thinking: line } : { type, text: line },
266
+ output: output ? line : null,
267
+ });
268
+ }
269
+ };
270
+ const content = Array.isArray(message.content) ? message.content : [{ type: "text", text: message.content }];
271
+ for (let partIndex = 0; partIndex < content.length; partIndex += 1) {
272
+ const part = content[partIndex];
273
+ if (message.role === "assistant" && part?.type === "thinking") {
274
+ addText("thinking", part.thinking ?? part.text ?? part.content, partIndex, false);
275
+ } else if (message.role === "assistant" && part?.type === "toolCall") {
276
+ const name = subagentText(part.name || part.toolName || part.toolCall?.name, 120) || "tool";
277
+ const argumentsText = subagentTranscriptToolArguments(part);
278
+ candidates.push({
279
+ sourceId,
280
+ role: message.role,
281
+ timestamp,
282
+ partIndex,
283
+ part: {
284
+ type: "toolCall",
285
+ name,
286
+ arguments: argumentsText,
287
+ ...(subagentText(part.id || part.toolCallId || part.tool_call_id, 240) ? { id: subagentText(part.id || part.toolCallId || part.tool_call_id, 240) } : {}),
288
+ },
289
+ output: `▶ ${name}${argumentsText && argumentsText !== "{}" ? ` ${argumentsText}` : ""}`,
290
+ });
291
+ } else if (part?.type === "text" || typeof part === "string") {
292
+ addText("text", typeof part === "string" ? part : part.text ?? part.content, partIndex);
293
+ }
294
+ }
295
+ return candidates;
296
+ }
297
+
298
+ function subagentTranscriptMessages(candidates) {
299
+ const messages = [];
300
+ let current = null;
301
+ for (const candidate of candidates) {
302
+ if (!current || current.sourceId !== candidate.sourceId) {
303
+ current = {
304
+ sourceId: candidate.sourceId,
305
+ role: candidate.role,
306
+ ...(candidate.timestamp ? { timestamp: candidate.timestamp } : {}),
307
+ ...(candidate.role === "toolResult" ? {
308
+ ...(candidate.toolCallId ? { toolCallId: candidate.toolCallId } : {}),
309
+ ...(candidate.toolName ? { toolName: candidate.toolName } : {}),
310
+ ...(candidate.isError ? { isError: true } : {}),
311
+ } : {}),
312
+ content: [],
313
+ };
314
+ messages.push(current);
315
+ }
316
+ const previous = current.content.at(-1);
317
+ if (previous?._subagentPartIndex === candidate.partIndex && previous.type === candidate.part.type && (candidate.part.type === "text" || candidate.part.type === "thinking")) {
318
+ const property = candidate.part.type === "thinking" ? "thinking" : "text";
319
+ previous[property] += `\n${candidate.part[property]}`;
320
+ continue;
244
321
  }
245
- return [`▶ ${name}${args && args !== "{}" ? ` ${args}` : ""}`];
322
+ current.content.push({ ...candidate.part, _subagentPartIndex: candidate.partIndex });
246
323
  }
247
- return [];
324
+ return messages.map(({ sourceId: _sourceId, content, ...message }) => ({
325
+ ...message,
326
+ content: content.map(({ _subagentPartIndex, ...part }) => part),
327
+ }));
248
328
  }
249
329
 
250
330
  function subagentTranscriptOutput(sessionFile) {
331
+ const empty = { recentOutput: [], transcript: [] };
251
332
  const file = String(sessionFile || "");
252
- if (!file || !path.isAbsolute(file) || path.extname(file) !== ".jsonl") return [];
333
+ if (!file || !path.isAbsolute(file) || path.extname(file) !== ".jsonl") return empty;
253
334
  let fd;
254
335
  try {
255
336
  fd = openSync(file, "r");
256
337
  const size = fstatSync(fd).size;
257
- if (size <= 0) return [];
338
+ if (size <= 0) return empty;
258
339
  const length = Math.min(size, SUBAGENT_TRANSCRIPT_TAIL_BYTES);
259
340
  const start = size - length;
260
341
  const buffer = Buffer.alloc(length);
261
342
  const bytesRead = readSync(fd, buffer, 0, length, start);
262
343
  const rawLines = buffer.subarray(0, bytesRead).toString("utf8").split(/\r?\n/);
263
344
  if (start > 0) rawLines.shift();
264
- const output = [];
265
- for (const rawLine of rawLines) {
345
+ const candidates = [];
346
+ for (let entryIndex = 0; entryIndex < rawLines.length; entryIndex += 1) {
347
+ const rawLine = rawLines[entryIndex];
266
348
  if (!rawLine.trim()) continue;
267
349
  let entry;
268
350
  try {
@@ -272,16 +354,15 @@ function subagentTranscriptOutput(sessionFile) {
272
354
  }
273
355
  const message = entry?.type === "message" ? entry.message : entry?.message?.role ? entry.message : null;
274
356
  if (!message || !["assistant", "toolResult"].includes(message.role)) continue;
275
- for (const part of Array.isArray(message.content) ? message.content : []) {
276
- for (const line of subagentTranscriptPartLines(part)) {
277
- if (line.trim().toLowerCase() === "(no output)") continue;
278
- output.push(line);
279
- }
280
- }
357
+ candidates.push(...subagentTranscriptMessageCandidates(message, entry.timestamp, entryIndex));
281
358
  }
282
- return subagentOutputLines(output);
359
+ const boundedCandidates = candidates.slice(-SUBAGENT_OUTPUT_LINE_LIMIT);
360
+ return {
361
+ recentOutput: subagentOutputLines(boundedCandidates.flatMap((candidate) => candidate.output === null ? [] : [candidate.output])),
362
+ transcript: subagentTranscriptMessages(boundedCandidates),
363
+ };
283
364
  } catch {
284
- return [];
365
+ return empty;
285
366
  } finally {
286
367
  if (fd !== undefined) {
287
368
  try { closeSync(fd); } catch { /* Best-effort close for live session tails. */ }
@@ -535,6 +616,7 @@ export default function webuiRpcHelper(pi) {
535
616
  tokens: Number.isFinite(patch.tokens) ? patch.tokens : Number.isFinite(agent.tokens) ? agent.tokens : undefined,
536
617
  recentTools: subagentRecentTools(patch.recentTools || agent.recentTools),
537
618
  recentOutput: subagentOutputLines(patch.recentOutput || agent.recentOutput),
619
+ transcript: Array.isArray(patch.transcript) ? patch.transcript : [],
538
620
  error: subagentText(patch.error, 1000) || undefined,
539
621
  },
540
622
  };
@@ -567,7 +649,8 @@ export default function webuiRpcHelper(pi) {
567
649
  const transcriptOutput = subagentTranscriptOutput(step.sessionFile || (steps.length === 1 ? status.sessionFile : undefined));
568
650
  return subagentOutputSnapshotFromAgent(run, agent, {
569
651
  ...step,
570
- recentOutput: transcriptOutput.length ? transcriptOutput : step.recentOutput,
652
+ recentOutput: transcriptOutput.recentOutput.length ? transcriptOutput.recentOutput : step.recentOutput,
653
+ transcript: transcriptOutput.transcript,
571
654
  status: step.status || status.state,
572
655
  updatedAt: step.lastActivityAt || status.lastUpdate,
573
656
  tokens: Number.isFinite(step.tokens?.total) ? step.tokens.total : step.tokens,