Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 152 additions & 4 deletions packages/web-shell/client/components/messages/ToolGroup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,71 @@ afterEach(() => {
}
});

function makeShellTool(output: string): ACPToolCall {
function makeShellTool(
output: string,
status: ACPToolCall['status'] = 'completed',
): ACPToolCall {
return {
callId: 'call-shell-1',
toolName: 'Shell',
status: 'completed',
status,
rawOutput: { output },
};
}

function renderShellTool(output: string): HTMLElement {
function renderTool(tool: ACPToolCall): HTMLElement {
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
act(() => {
root.render(
<I18nProvider language="en">
<ToolGroup tools={[makeShellTool(output)]} />
<ToolGroup tools={[tool]} />
</I18nProvider>,
);
});
mounted.push({ root, container });
return container;
}

function renderShellTool(output: string): HTMLElement {
const container = renderTool(makeShellTool(output));
// Completed tools collapse to a one-line summary by default; open the row so
// the assertions below can inspect the bash-output view.
const chevron = [...container.querySelectorAll('span')].find(
(s) => s.textContent === '▸',
);
if (chevron?.parentElement) click(chevron.parentElement);
return container;
}

function makeShellCommandTool(command: string): ACPToolCall {
return {
callId: 'call-shell-cmd',
toolName: 'run_shell_command',
status: 'completed',
args: { command },
rawOutput: { output: 'done' },
};
}

function makeAgentTool(status: ACPToolCall['status']): ACPToolCall {
return {
callId: 'agent-1',
toolName: 'task',
status,
args: { description: 'agentDescMarker' },
subTools: [
{
callId: 'agent-1-sub-1',
toolName: 'Read',
status: 'completed',
args: { file_path: '/ws/SubToolMarker.ts' },
},
],
};
}

function getExpandButton(container: HTMLElement): HTMLButtonElement {
const button = container.querySelector('button');
expect(button).not.toBeNull();
Expand Down Expand Up @@ -124,3 +165,110 @@ describe('shell tool output expand toggle', () => {
expect(button.getAttribute('aria-expanded')).toBe('false');
});
});

describe('tool description expand toggle', () => {
it('keeps a finished command collapsed but reveals it in full on expand', () => {
const command = `npm run build && npm run test && npm run lint -- ${'x'.repeat(
80,
)}`;
const container = renderTool(makeShellCommandTool(command));

Comment thread
wenshao marked this conversation as resolved.
Resolved
// Completed → collapsed: chevron points right. The full command (no longer
// hard-capped at one line's worth of characters) is present in the header.
expect(container.textContent).toContain('▸');
expect(container.textContent).toContain(command);

const chevron = [...container.querySelectorAll('span')].find(
(s) => s.textContent === '▸',
);
click(chevron!.parentElement!);

// Expanded → chevron flips and the full command is reflowed below.
expect(container.textContent).toContain('▾');
expect(container.textContent).toContain(command);
});
});

describe('auto-collapse on finish', () => {
it('collapses a completed tool to its summary by default', () => {
const container = renderTool(makeShellTool('a\nb\nc\nd'));
// The expanded bash <pre> is not rendered until the user opens the row.
expect(container.querySelector('pre')).toBeNull();
expect(container.textContent).toContain('▸');
});

it('keeps a running tool expanded so streaming output stays visible', () => {
const container = renderTool(
makeShellTool('streaming output', 'in_progress'),
);
expect(container.querySelector('pre')?.textContent).toContain('streaming');
});

it('keeps a failed tool expanded so the error stays visible', () => {
const container = renderTool(
makeShellTool('error: boom\n at step 1', 'failed'),
);
expect(container.querySelector('pre')?.textContent).toContain('boom');
});

it('auto-collapses a running tool when it transitions to completed', () => {
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
mounted.push({ root, container });

const output = 'line1\nline2\nline3\nline4';
act(() => {
root.render(
<I18nProvider language="en">
<ToolGroup tools={[makeShellTool(output, 'in_progress')]} />
</I18nProvider>,
);
});
// Running → expanded: the bash output is visible.
expect(container.querySelector('pre')).not.toBeNull();

// Same callId, now finished → the row collapses on its own.
act(() => {
root.render(
<I18nProvider language="en">
<ToolGroup tools={[makeShellTool(output, 'completed')]} />
</I18nProvider>,
);
});
expect(container.querySelector('pre')).toBeNull();
});

it('does not collapse an agent the user expanded when it completes', () => {
const container = document.createElement('div');
document.body.appendChild(container);
const root = createRoot(container);
mounted.push({ root, container });

const render = (status: ACPToolCall['status']) =>
act(() => {
root.render(
<I18nProvider language="en">
<ToolGroup tools={[makeAgentTool(status)]} />
</I18nProvider>,
);
});

render('in_progress');
// Agents start collapsed: the sub-tool panel is hidden.
expect(container.textContent).not.toContain('SubToolMarker');

// Expand by clicking the agent's summary row.
const summaryLabel = [...container.querySelectorAll('span')].find(
(s) => s.textContent === 'task:',
);
expect(summaryLabel).toBeTruthy();
click(summaryLabel!.parentElement!);
expect(container.textContent).toContain('SubToolMarker');

// Completion must NOT yank the panel shut: the collapse-on-finish effect
// is scoped to non-agent tools.
render('completed');
expect(container.textContent).toContain('SubToolMarker');
});
});
51 changes: 49 additions & 2 deletions packages/web-shell/client/components/messages/ToolGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ function buildUnifiedDiff(oldText: string, newText: string): string {
const MAX_BASH_LINE_CHARS = 150;
const MAX_READ_LINES = 25;

// A description longer than this is likely ellipsised on a normal-width row, so
// the row becomes expandable to re-flow the full text into a wrapped block.
const DESCRIPTION_EXPAND_THRESHOLD = 60;

export function resolveShellOutputMaxLines(
settings: readonly DaemonSettingDescriptor[],
): number {
Expand Down Expand Up @@ -411,6 +415,10 @@ function getAgentDisplayInfo(
}

function shouldAutoExpand(tool: ACPToolCall): boolean {
// Once a tool completes successfully, collapse it to its one-line summary so
Comment thread
wenshao marked this conversation as resolved.
Resolved
// the transcript stays scannable; the user can click to reopen it. Failures
// stay expanded so the error output remains visible without an extra click.
if (tool.status === 'completed') return false;
const name = tool.toolName.toLowerCase();
if (isAskUserQuestionToolName(tool.toolName)) return true;
if (name === 'write_file' || name === 'writefile') return true;
Expand Down Expand Up @@ -465,6 +473,27 @@ function ToolHeaderExtra({ info }: { info: ToolHeaderExtraRenderInfo }) {
);
}

function isDescriptionExpandable(description: string): boolean {
return (
description.length > DESCRIPTION_EXPAND_THRESHOLD ||
description.includes('\n')

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] isDescriptionExpandable has two branches — description.length > DESCRIPTION_EXPAND_THRESHOLD and description.includes('\n') — but only the length branch is tested. A short (under 60 chars) description containing a newline should make the row expandable, but no test verifies this. A regression in the newline check (e.g., someone simplifies to length-only) would silently remove expandability for multi-line short descriptions.

Consider adding a test with a description under 60 chars containing \n (e.g., command: "echo a\necho b") and asserting the chevron appears.

— qwen3.7-max via Qwen Code /review

);
}

function ToggleChevron({
expandable,
expanded,
}: {
expandable: boolean;
expanded: boolean;
}) {
return (
<span className={styles.lineToggle} aria-hidden="true">
{expandable ? (expanded ? '▾' : '▸') : ''}
</span>
);
}

function getActiveTool(tools: ACPToolCall[]): ACPToolCall {
return (
tools.find((t) => t.status === 'in_progress') ?? tools[tools.length - 1]
Expand Down Expand Up @@ -611,6 +640,14 @@ export const ToolLine = memo(function ToolLine({
return () => clearInterval(id);
}, [isRunningAgent]);

// Collapse a regular tool to its one-line summary once it completes
// successfully. Agents are excluded — they keep whatever expand state the
// user chose, driven from their own panel — and failures stay open so the
// error output remains visible.
useEffect(() => {
if (!isAgent && tool.status === 'completed') setExpanded(false);
Comment thread
wenshao marked this conversation as resolved.
Resolved
}, [isAgent, tool.status]);

if (isAgent) {
if (hasApproval && onConfirm) {
return (
Expand Down Expand Up @@ -712,10 +749,16 @@ export const ToolLine = memo(function ToolLine({
isShellToolName(tool.toolName) || isWebFetchToolName(tool.toolName)
? ''
: formatElapsed(tool.startTime, tool.endTime);
const expandable = hasExpandableContent(tool);

const name = tool.toolName.toLowerCase();
const isTodo = name === 'todowrite';
// A row expands when it has detail output (bash/diff/read content) or when
// its description is long enough to be ellipsised. When a long description is
// expanded we move it out of the header into a wrapped block below, so the
// header drops its single-line copy.
const descExpandable = !isTodo && isDescriptionExpandable(description);
Comment thread
wenshao marked this conversation as resolved.
Resolved
const expandable = !isTodo && (hasExpandableContent(tool) || descExpandable);
const relocateDescription = expanded && descExpandable;

if (hasApproval && onConfirm) {
return (
Expand All @@ -731,20 +774,24 @@ export const ToolLine = memo(function ToolLine({
className={`${styles.lineMain} ${expandable ? styles.lineExpandable : ''}`}
onClick={expandable ? () => setExpanded(!expanded) : undefined}
>
<ToggleChevron expandable={expandable} expanded={expanded} />
<StatusIcon status={tool.status} />
<span className={styles.lineName}>{displayName}</span>
<ToolHeaderExtra
info={{
kind: getToolHeaderKind(tool),
tool,
displayName,
description,
description: relocateDescription ? '' : description,
elapsed,
workspaceCwd,
}}
/>
</div>
{isTodo && <TodoWriteContent tool={tool} />}
{relocateDescription && (
<div className={styles.lineFullArg}>{description}</div>
)}
{!isTodo && !expanded && result && (
<div className={styles.lineOutput}>{result}</div>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,24 @@ describe('toolFormatting', () => {
),
).toBe('3 line(s)');
});

it('keeps long shell commands in full instead of capping at one line', () => {
const command = `echo ${'a'.repeat(200)}`;
expect(
getToolDescription(
tool({ toolName: 'run_shell_command', args: { command } }),
),
).toBe(command);
});

it('still bounds a pathologically long description', () => {
const result = getToolDescription(
tool({
toolName: 'run_shell_command',
args: { command: 'x'.repeat(5000) },
}),
);
expect(result.length).toBeLessThan(5000);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The assertion expect(result.length).toBeLessThan(5000) is too loose to verify the new MAX_DESCRIPTION_LENGTH = 2000 ceiling. This would also pass with the old limit of 120 or any other value below 5000, providing no regression safety for the actual boundary.

Consider tightening to expect(result.length).toBeLessThanOrEqual(2003) (2000 chars + "...") to pin the exact limit.

— qwen3.7-max via Qwen Code /review

expect(result.endsWith('...')).toBe(true);
});
});
13 changes: 10 additions & 3 deletions packages/web-shell/client/components/messages/toolFormatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,21 @@ export function truncateText(text: string, max: number): string {
return text.slice(0, max) + '...';
}

// The tool-header description is shown single-line (CSS-ellipsised) when the
// row is collapsed and fully wrapped when it is expanded, so we keep the whole
// string rather than hard-capping it at a line's worth of characters. A
// generous ceiling still guards against a pathological multi-megabyte command
// bloating the DOM.
const MAX_DESCRIPTION_LENGTH = 2000;

export function getToolDescription(
tool: ACPToolCall,
workspaceCwd?: string,
): string {
const fromTitle = getDescriptionFromTitle(tool, workspaceCwd);
if (fromTitle) return truncateText(fromTitle, 120);
if (fromTitle) return truncateText(fromTitle, MAX_DESCRIPTION_LENGTH);
const fromArgs = getDescriptionFromArgs(tool, workspaceCwd);
if (fromArgs) return truncateText(fromArgs, 120);
if (fromArgs) return truncateText(fromArgs, MAX_DESCRIPTION_LENGTH);
return '';
}

Expand Down Expand Up @@ -177,7 +184,7 @@ function getDescriptionFromArgs(
if (args.description) {
description += ` (${String(args.description).replace(/\n/g, ' ')})`;
}
return truncateText(description, 120);
return truncateText(description, MAX_DESCRIPTION_LENGTH);
}
if (name === 'grep_search' || name === 'grep' || name === 'search') {
const pattern = args.pattern ?? args.query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
min-width: 0;
}

/* Leading disclosure column. Rendered (blank) for every tool row so names stay
aligned; shows ▸/▾ only when the row can expand. */
.lineToggle {
width: 12px;
flex-shrink: 0;
font-size: 10px;
line-height: 1;
color: var(--text-dimmed);
text-align: center;
user-select: none;
}

.icon {
font-size: 13px;
flex-shrink: 0;
Expand Down Expand Up @@ -110,6 +122,21 @@
line-height: 1.4;
}

/* Full, wrapped tool argument shown below the header when the row is expanded
(the header drops its single-line ellipsised copy). Aligns with the expanded
output block below it. */
.lineFullArg {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] .lineFullArg renders up to 2000 chars with pre-wrap and no max-height or overflow constraint. At maximum length this produces ~25-30 lines of wrapped monospace text that pushes all subsequent tool rows down with no scrolling. By contrast, .expandedOutput caps at max-height: 400px with overflow-y: auto.

Suggested change
.lineFullArg {
.lineFullArg {
margin-left: 16px;
margin-top: 2px;
font-size: 12px;
line-height: 1.4;
color: var(--text-secondary);
font-family: var(--font-mono);
white-space: pre-wrap;
word-break: break-word;
overflow-wrap: anywhere;
max-height: 200px;
overflow-y: auto;
}

— qwen3.7-max via Qwen Code /review

margin-left: 16px;
margin-top: 2px;
font-size: 12px;
line-height: 1.4;
color: var(--text-secondary);
font-family: var(--font-mono);
white-space: pre-wrap;
word-break: break-word;
overflow-wrap: anywhere;
}

.lineDetail {
}

Expand Down
Loading