@firstpick/pi-package-webui 0.6.8 → 0.7.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.
@@ -0,0 +1,174 @@
1
+ import assert from "node:assert/strict";
2
+ import { spawn, spawnSync } from "node:child_process";
3
+ import { chmod, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import path from "node:path";
6
+ import { dirname, join } from "node:path";
7
+ import { fileURLToPath, pathToFileURL } from "node:url";
8
+
9
+ const root = join(dirname(fileURLToPath(import.meta.url)), "..");
10
+ const workspaceRoot = join(root, "..");
11
+ const serverScript = join(root, "bin", "pi-webui.mjs");
12
+ const fakePi = join(root, "tests", "fixtures", "fake-pi.mjs");
13
+ const reviewModuleUrl = pathToFileURL(join(workspaceRoot, "pi-extension-aur-review", "src", "git.ts")).href;
14
+ const port = 41000 + Math.floor(Math.random() * 10000);
15
+
16
+ function git(cwd, ...args) {
17
+ const result = spawnSync("git", args, { cwd, encoding: "utf8" });
18
+ assert.equal(result.status, 0, `git ${args.join(" ")} failed:\n${result.stderr || result.stdout}`);
19
+ return result.stdout;
20
+ }
21
+
22
+ async function request(pathname, { method = "GET", body } = {}) {
23
+ const response = await fetch(`http://127.0.0.1:${port}${pathname}`, {
24
+ method,
25
+ headers: body === undefined ? undefined : { "content-type": "application/json" },
26
+ body: body === undefined ? undefined : JSON.stringify(body),
27
+ signal: AbortSignal.timeout(8_000),
28
+ });
29
+ return { status: response.status, body: await response.json().catch(() => ({})) };
30
+ }
31
+
32
+ function extensionSnapshot(repo) {
33
+ const script = `
34
+ import { captureGitSnapshot } from ${JSON.stringify(reviewModuleUrl)};
35
+ try {
36
+ const snapshot = await captureGitSnapshot(process.env.HASH_CONTRACT_REPO, [], "staged");
37
+ process.stdout.write(JSON.stringify({ ok: true, stagedContentHash: snapshot.stagedContentHash }));
38
+ } catch (error) {
39
+ process.stdout.write(JSON.stringify({ ok: false, error: error instanceof Error ? error.message : String(error) }));
40
+ }
41
+ `;
42
+ const result = spawnSync(process.execPath, ["--experimental-strip-types", "--input-type=module", "--eval", script], {
43
+ encoding: "utf8",
44
+ env: { ...process.env, HASH_CONTRACT_REPO: repo },
45
+ });
46
+ assert.equal(result.status, 0, `extension hash helper failed: ${result.stderr}`);
47
+ return JSON.parse(result.stdout);
48
+ }
49
+
50
+ const repo = await mkdtemp(path.join(tmpdir(), "pi-webui-staged-hash-contract-"));
51
+ const agentDir = await mkdtemp(path.join(tmpdir(), "pi-webui-staged-hash-agent-"));
52
+ await chmod(fakePi, 0o755);
53
+ const child = spawn(process.execPath, [serverScript, "--cwd", repo, "--host", "127.0.0.1", "--port", String(port), "--pi", fakePi], {
54
+ stdio: ["ignore", "pipe", "pipe"],
55
+ env: {
56
+ ...process.env,
57
+ PI_CODING_AGENT_DIR: agentDir,
58
+ PI_WEBUI_SETTINGS_FILE: path.join(agentDir, "settings.json"),
59
+ },
60
+ });
61
+ let serverOutput = "";
62
+ child.stdout.on("data", (chunk) => { serverOutput += String(chunk); });
63
+ child.stderr.on("data", (chunk) => { serverOutput += String(chunk); });
64
+
65
+ try {
66
+ let health;
67
+ for (let attempt = 0; attempt < 60; attempt++) {
68
+ try {
69
+ health = await request("/api/health");
70
+ if (health.status === 200) break;
71
+ } catch {
72
+ // Server is still starting.
73
+ }
74
+ await new Promise((resolve) => setTimeout(resolve, 100));
75
+ }
76
+ assert.equal(health?.status, 200, `server did not start:\n${serverOutput}`);
77
+ const tabs = await request("/api/tabs");
78
+ const tabId = tabs.body?.data?.tabs?.[0]?.id;
79
+ assert.ok(tabId, "contract server should expose its startup tab");
80
+ const staged = async () => {
81
+ const result = await request(`/api/git-workflow/staged-content?tab=${encodeURIComponent(tabId)}`);
82
+ assert.equal(result.status, 200, `staged-content endpoint should be read-only and available: ${result.body?.error || ""}`);
83
+ assert.equal(result.body?.ok, true, result.body?.error || "staged-content endpoint failed");
84
+ return result.body.data;
85
+ };
86
+ const assertMatchesExtension = async (label) => {
87
+ const server = await staged();
88
+ const extension = extensionSnapshot(repo);
89
+ assert.equal(server.hasStagedChanges, true, `${label}: server should find staged content`);
90
+ assert.equal(extension.ok, true, `${label}: extension should capture staged content (${extension.error || ""})`);
91
+ assert.match(server.stagedContentHash || "", /^[a-f0-9]{64}$/i, `${label}: server should return a SHA-256 staged-content hash`);
92
+ assert.equal(server.stagedContentHash, extension.stagedContentHash, `${label}: extension and server staged-content hashes must match`);
93
+ return server.stagedContentHash;
94
+ };
95
+
96
+ git(repo, "init", "-q");
97
+ git(repo, "config", "user.name", "Hash Contract");
98
+ git(repo, "config", "user.email", "hash-contract@example.invalid");
99
+
100
+ // Empty/unborn state has no substantive index content and exposes no token.
101
+ assert.deepEqual(await staged(), { root: repo, hasStagedChanges: false, stagedContentHash: null });
102
+ const emptyExtension = extensionSnapshot(repo);
103
+ assert.equal(emptyExtension.ok, false, "extension should reject an empty staged review");
104
+ assert.match(emptyExtension.error, /No substantive staged changes/i);
105
+
106
+ // Unborn HEAD must use the index-vs-empty-tree diff without a HEAD lookup.
107
+ await writeFile(path.join(repo, "normal.txt"), "unborn normal\n");
108
+ git(repo, "add", "normal.txt");
109
+ await assertMatchesExtension("unborn normal");
110
+ git(repo, "commit", "-qm", "base");
111
+ await writeFile(path.join(repo, "deleted.txt"), "delete\n");
112
+ await writeFile(path.join(repo, "rename-from.txt"), "rename\n");
113
+ git(repo, "add", ".");
114
+ git(repo, "commit", "-qm", "fixtures");
115
+
116
+ const reset = () => git(repo, "reset", "--hard", "HEAD");
117
+ await writeFile(path.join(repo, "normal.txt"), "ordinary staged edit\n");
118
+ git(repo, "add", "normal.txt");
119
+ await assertMatchesExtension("normal text");
120
+ reset();
121
+
122
+ await writeFile(path.join(repo, "binary.bin"), Buffer.from([0, 1, 2, 0xff, 0x0a]));
123
+ git(repo, "add", "binary.bin");
124
+ await assertMatchesExtension("binary");
125
+ reset();
126
+
127
+ await writeFile(path.join(repo, "normal.txt"), "#!/bin/sh\necho mode\n");
128
+ await chmod(path.join(repo, "normal.txt"), 0o755);
129
+ git(repo, "add", "normal.txt");
130
+ await assertMatchesExtension("mode");
131
+ reset();
132
+
133
+ await symlink("normal.txt", path.join(repo, "normal-link"));
134
+ git(repo, "add", "normal-link");
135
+ await assertMatchesExtension("symlink");
136
+ reset();
137
+
138
+ await writeFile(path.join(repo, "added.txt"), "added\n");
139
+ git(repo, "rm", "-q", "deleted.txt");
140
+ git(repo, "mv", "rename-from.txt", "rename-to.txt");
141
+ git(repo, "add", "added.txt");
142
+ await assertMatchesExtension("add delete rename");
143
+ reset();
144
+
145
+ // A server mutation rechecks an untrusted expected token at its own action
146
+ // boundary, rejecting drift before either generation or commit can consume it.
147
+ await writeFile(path.join(repo, "normal.txt"), "approved v1\n");
148
+ git(repo, "add", "normal.txt");
149
+ const approvedHash = await assertMatchesExtension("post-approval baseline");
150
+ await writeFile(path.join(repo, "normal.txt"), "drifted v2\n");
151
+ git(repo, "add", "normal.txt");
152
+ const driftedGeneration = await request("/api/git-workflow/generate", { method: "POST", body: { tab: tabId, kind: "commit", expectedStagedContentHash: approvedHash } });
153
+ assert.equal(driftedGeneration.status, 409, "staged drift must be rejected before commit-message generation");
154
+ assert.match(driftedGeneration.body?.error || "", /Staged content changed after manual approval/i);
155
+ const driftedCommit = await request("/api/git-workflow/commit", { method: "POST", body: { tab: tabId, variant: "input", message: "must not commit", expectedStagedContentHash: approvedHash } });
156
+ assert.equal(driftedCommit.status, 200);
157
+ assert.equal(driftedCommit.body?.ok, false, "staged drift must be rejected before commit");
158
+ assert.match(driftedCommit.body?.error || "", /Staged content changed after manual approval/i);
159
+ reset();
160
+
161
+ assert.deepEqual(await staged(), { root: repo, hasStagedChanges: false, stagedContentHash: null }, "reset index should return the no-staged state");
162
+ const resetExtension = extensionSnapshot(repo);
163
+ assert.equal(resetExtension.ok, false, "extension should reject reset/no-staged content");
164
+
165
+ console.log("staged content hash contract tests passed");
166
+ } finally {
167
+ child.kill("SIGTERM");
168
+ await Promise.race([
169
+ new Promise((resolve) => child.once("close", resolve)),
170
+ new Promise((resolve) => setTimeout(resolve, 2_000)),
171
+ ]);
172
+ if (child.exitCode === null) child.kill("SIGKILL");
173
+ await Promise.all([rm(repo, { recursive: true, force: true }), rm(agentDir, { recursive: true, force: true })]);
174
+ }
@@ -63,11 +63,12 @@ const ctx = {
63
63
  const asyncRunDir = await mkdtemp(path.join(tmpdir(), "pi-webui-subagent-output-test-"));
64
64
  const asyncSessionFile = path.join(asyncRunDir, "reviewer-session.jsonl");
65
65
  await writeFile(asyncSessionFile, [
66
- JSON.stringify({ type: "message", message: { role: "assistant", content: [{ type: "text", text: "REVIEWER STREAM 1 OF 18" }, { type: "toolCall", name: "bash", arguments: { command: "sleep 5" } }] } }),
67
- JSON.stringify({ type: "message", message: { role: "toolResult", content: [{ type: "text", text: "(no output)\nreviewer tool output line" }] } }),
66
+ JSON.stringify({ type: "message", timestamp: "2026-07-19T12:00:00.000Z", message: { role: "assistant", content: [{ type: "thinking", thinking: "Checking structured transcript extraction" }, { type: "text", text: "REVIEWER STREAM 1 OF 18" }, { type: "toolCall", id: "review-call", name: "bash", arguments: { command: "sleep 5" } }, { type: "text", text: "Review complete." }] } }),
67
+ JSON.stringify({ type: "message", timestamp: "2026-07-19T12:00:01.000Z", message: { role: "toolResult", toolCallId: "review-call", toolName: "bash", isError: false, content: [{ type: "text", text: "(no output)\nreviewer tool output line" }] } }),
68
68
  "",
69
69
  ].join("\n"));
70
- await writeFile(path.join(asyncRunDir, "status.json"), JSON.stringify({
70
+ const asyncStatusFile = path.join(asyncRunDir, "status.json");
71
+ await writeFile(asyncStatusFile, JSON.stringify({
71
72
  runId: "run-a",
72
73
  mode: "parallel",
73
74
  state: "running",
@@ -136,8 +137,85 @@ assert.equal(asyncOutputResponse.ok, true);
136
137
  assert.deepEqual(asyncOutputResponse.data.agent.recentOutput, [
137
138
  "REVIEWER STREAM 1 OF 18",
138
139
  "▶ bash {\"command\":\"sleep 5\"}",
140
+ "Review complete.",
139
141
  "reviewer tool output line",
140
- ], "async output should be recovered from the child session transcript when status recentOutput is empty");
142
+ ], "async output should remain available as a bounded text fallback when status recentOutput is empty");
143
+ assert.deepEqual(asyncOutputResponse.data.agent.transcript, [
144
+ {
145
+ role: "assistant",
146
+ timestamp: "2026-07-19T12:00:00.000Z",
147
+ content: [
148
+ { type: "thinking", thinking: "Checking structured transcript extraction" },
149
+ { type: "text", text: "REVIEWER STREAM 1 OF 18" },
150
+ { type: "toolCall", id: "review-call", name: "bash", arguments: "{\"command\":\"sleep 5\"}" },
151
+ { type: "text", text: "Review complete." },
152
+ ],
153
+ },
154
+ {
155
+ role: "toolResult",
156
+ timestamp: "2026-07-19T12:00:01.000Z",
157
+ toolCallId: "review-call",
158
+ toolName: "bash",
159
+ content: [{ type: "text", text: "reviewer tool output line" }],
160
+ },
161
+ ], "async output should preserve assistant thinking, tool calls, final text, and tool results for the main transcript renderer");
162
+
163
+ const boundedSessionFile = path.join(asyncRunDir, "bounded-reviewer-session.jsonl");
164
+ await writeFile(boundedSessionFile, Array.from({ length: 122 }, (_unused, index) => JSON.stringify({
165
+ type: "message",
166
+ timestamp: `2026-07-19T12:01:${String(index).padStart(2, "0")}.000Z`,
167
+ message: { role: "assistant", content: [{ type: "text", text: `bounded transcript line ${index}` }] },
168
+ })).join("\n"));
169
+ await writeFile(asyncStatusFile, JSON.stringify({
170
+ runId: "run-a",
171
+ mode: "parallel",
172
+ state: "running",
173
+ startedAt: Date.now() - 1000,
174
+ lastUpdate: Date.now(),
175
+ steps: [{ agent: "reviewer", status: "running", sessionFile: boundedSessionFile }],
176
+ }));
177
+ await helperCommand.handler(JSON.stringify({
178
+ requestId: "bounded-subagent-output-test",
179
+ action: "subagent-output",
180
+ payload: { runId: "run-a", agentId: "run-a:step:0:reviewer" },
181
+ }), ctx);
182
+ const boundedOutputNotice = notifications.find((entry) => entry.message.startsWith("__PI_WEBUI_HELPER_RESPONSE__:")
183
+ && JSON.parse(entry.message.slice("__PI_WEBUI_HELPER_RESPONSE__:".length)).requestId === "bounded-subagent-output-test");
184
+ assert.ok(boundedOutputNotice, "bounded async output helper action should return a response notification");
185
+ const boundedOutputResponse = JSON.parse(boundedOutputNotice.message.slice("__PI_WEBUI_HELPER_RESPONSE__:".length));
186
+ assert.equal(boundedOutputResponse.data.agent.recentOutput.length, 120, "structured child extraction should retain the existing 120-line output bound");
187
+ assert.equal(boundedOutputResponse.data.agent.recentOutput[0], "bounded transcript line 2");
188
+ assert.equal(boundedOutputResponse.data.agent.recentOutput.at(-1), "bounded transcript line 121");
189
+ assert.equal(boundedOutputResponse.data.agent.transcript.length, 120, "structured child extraction should apply the same tail bound to rendered entries");
190
+
191
+ const incompleteToolSessionFile = path.join(asyncRunDir, "incomplete-tool-session.jsonl");
192
+ await writeFile(incompleteToolSessionFile, JSON.stringify({
193
+ type: "message",
194
+ timestamp: "2026-07-19T12:02:00.000Z",
195
+ message: { role: "assistant", content: [{ type: "toolCall", id: "tail-call", name: "read", arguments: { path: "README.md" } }] },
196
+ }));
197
+ await writeFile(asyncStatusFile, JSON.stringify({
198
+ runId: "run-a",
199
+ mode: "parallel",
200
+ state: "running",
201
+ startedAt: Date.now() - 1000,
202
+ lastUpdate: Date.now(),
203
+ steps: [{ agent: "reviewer", status: "running", sessionFile: incompleteToolSessionFile }],
204
+ }));
205
+ await helperCommand.handler(JSON.stringify({
206
+ requestId: "incomplete-tool-subagent-output-test",
207
+ action: "subagent-output",
208
+ payload: { runId: "run-a", agentId: "run-a:step:0:reviewer" },
209
+ }), ctx);
210
+ const incompleteToolNotice = notifications.find((entry) => entry.message.startsWith("__PI_WEBUI_HELPER_RESPONSE__:")
211
+ && JSON.parse(entry.message.slice("__PI_WEBUI_HELPER_RESPONSE__:".length)).requestId === "incomplete-tool-subagent-output-test");
212
+ assert.ok(incompleteToolNotice, "incomplete tool output helper action should return a response notification");
213
+ const incompleteToolResponse = JSON.parse(incompleteToolNotice.message.slice("__PI_WEBUI_HELPER_RESPONSE__:".length));
214
+ assert.deepEqual(incompleteToolResponse.data.agent.transcript, [{
215
+ role: "assistant",
216
+ timestamp: "2026-07-19T12:02:00.000Z",
217
+ content: [{ type: "toolCall", id: "tail-call", name: "read", arguments: "{\"path\":\"README.md\"}" }],
218
+ }], "a live or truncated session tail should retain an unpaired tool call for a pending tool card");
141
219
 
142
220
  for (const handler of extensionHandlers.get("tool_execution_start") || []) {
143
221
  handler({
@@ -195,6 +273,7 @@ assert.equal(outputResponse.ok, true);
195
273
  assert.equal(outputResponse.data.agent.currentTool, "bash");
196
274
  assert.equal(outputResponse.data.agent.currentToolArgs, "npm test");
197
275
  assert.deepEqual(outputResponse.data.agent.recentOutput, ["Running focused tests", "12 assertions passed"]);
276
+ assert.deepEqual(outputResponse.data.agent.transcript, [], "foreground snapshots without a child session transcript should retain the recentOutput-only fallback");
198
277
  assert.deepEqual(outputResponse.data.agent.recentTools, [{ tool: "read", args: "package.json", endMs: 1000 }]);
199
278
 
200
279
  for (const handler of extensionHandlers.get("tool_execution_end") || []) {
@@ -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,