Skip to content

Commit 3f105e0

Browse files
authored
perf(clients): bound subagent preview processing to the visible prefix (#21277)
1 parent 2148331 commit 3f105e0

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { subagentDetailPreview } from "./subagentDisplay.js";
4+
5+
describe("subagentDetailPreview", () => {
6+
it("prefers progress for live work and results for settled work", () => {
7+
const details = { progress: "Reading files", result: "Found two\n problems" };
8+
expect(subagentDetailPreview({ ...details, status: "running" })).toBe("Reading files");
9+
expect(subagentDetailPreview({ ...details, status: "completed" })).toBe("Found two problems");
10+
expect(
11+
subagentDetailPreview({ status: "failed", progress: "Last progress", result: " " }),
12+
).toBe("Last progress");
13+
expect(subagentDetailPreview({ status: "pending" })).toBeNull();
14+
});
15+
});
16+
17+
it.each([279, 280, 281])("preserves the preview boundary at %i characters", (length) => {
18+
const result = "x".repeat(length);
19+
expect(subagentDetailPreview({ status: "completed", result })).toBe(
20+
length > 280 ? "x".repeat(280) + "…" : result,
21+
);
22+
});
23+
24+
it("normalizes Unicode whitespace and ignores a large hidden suffix", () => {
25+
const progress = " \u00a0Reading\tfiles\n" + "x".repeat(1_000_000);
26+
expect(subagentDetailPreview({ status: "running", progress })).toBe(
27+
"Reading files " + "x".repeat(266) + "…",
28+
);
29+
expect(
30+
subagentDetailPreview({ status: "completed", result: "x".repeat(280) + " \n\u2028" }),
31+
).toBe("x".repeat(280));
32+
expect(subagentDetailPreview({ status: "completed", result: "x".repeat(279) + " \n y" })).toBe(
33+
"x".repeat(279) + "…",
34+
);
35+
expect(
36+
subagentDetailPreview({ status: "running", progress: "\uFEFF\u2029", result: " Done " }),
37+
).toBe("Done");
38+
expect(subagentDetailPreview({ status: "failed", result: "x".repeat(279) + "😀" })).toBe(
39+
"x".repeat(279) + "\uD83D…",
40+
);
41+
});

‎packages/client-runtime/src/state/subagentDisplay.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,23 @@ export function subagentDetailPreview(input: {
116116
readonly result?: string | null | undefined;
117117
readonly progress?: string | null | undefined;
118118
}): string | null {
119-
const result = input.result?.trim();
120-
const progress = input.progress?.trim();
121-
const detail =
122-
(isTerminalSubagentStatus(input.status) ? result || progress : progress || result) || "";
123-
const compact = detail.replace(/\s+/gu, " ");
124-
return compact.length > 280 ? `${compact.slice(0, 280).trimEnd()}…` : compact || null;
119+
const terminal = isTerminalSubagentStatus(input.status);
120+
const preferred = terminal ? input.result : input.progress;
121+
const fallback = terminal ? input.progress : input.result;
122+
const detail = preferred?.trimStart() || fallback?.trimStart() || "";
123+
if (detail.length <= 280) return detail.trimEnd().replace(/\s+/gu, " ") || null;
124+
const whitespace = /\s+/uy;
125+
let compact = "";
126+
for (let index = 0; index < detail.length; index += 1) {
127+
whitespace.lastIndex = index;
128+
const gap = whitespace.exec(detail);
129+
if (gap) {
130+
index += gap[0].length;
131+
if (index === detail.length) break;
132+
if (compact.length > 0) compact += " ";
133+
}
134+
compact += detail[index];
135+
if (compact.length > 280) return `${compact.slice(0, 280).trimEnd()}…`;
136+
}
137+
return compact || null;
125138
}

0 commit comments

Comments
 (0)