diff --git a/frontend/src/components/NormalizedConversation/TurnActivity.test.tsx b/frontend/src/components/NormalizedConversation/TurnActivity.test.tsx new file mode 100644 index 000000000..5b329699f --- /dev/null +++ b/frontend/src/components/NormalizedConversation/TurnActivity.test.tsx @@ -0,0 +1,138 @@ +import { fireEvent, render, screen } from '@testing-library/react'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { describe, expect, it, vi } from 'vitest'; +import type { ContentBlock } from 'shared/types'; +import i18n from '@/i18n'; +import { TurnActivity } from './TurnActivity'; +import { planTurnBlocks } from './messageTurnBlocks'; + +vi.mock('@/contexts/PanelActionsContext', () => ({ + useOptionalPanelActionsContext: () => null, +})); + +vi.mock('@/components/ConfigProvider', () => ({ + useUserSystem: () => ({ config: { theme: 'light' } }), + useOptionalUserSystem: () => ({ config: { theme: 'light' } }), +})); + +vi.mock('@/components/common/RawLogText', () => ({ + default: ({ content }: { content: string }) =>
{content}
, +})); + +vi.mock('./AstryxMarkdown', () => ({ + AstryxMarkdown: ({ value }: { value: string }) =>
{value}
, +})); + +function renderCommand({ + id, + status = 'failed', + code, + backgrounded = false, +}: { + id: string; + status?: 'failed' | 'completed' | 'in_progress'; + code?: number; + backgrounded?: boolean; +}) { + const blocks: ContentBlock[] = [ + { + type: 'tool_use', + tool_use_id: id, + tool_name: 'exec_command', + kind: 'execute', + status, + input_preview: JSON.stringify({ cmd: 'powershell.exe -Command test' }), + meta: backgrounded ? { asyncTasks: { backgrounded: true } } : null, + }, + ]; + if (status !== 'in_progress') { + blocks.push({ + type: 'tool_result', + tool_use_id: id, + is_error: status === 'failed', + output_preview: JSON.stringify({ + stdout: 'original stdout', + stderr: 'original stderr', + ...(code !== undefined ? { exit_code: code } : {}), + }), + }); + } + const view = render( + + ({ item, index }))} + attempt={{ id: 'attempt-1', container_ref: null } as never} + task={null} + /> + + ); + if (!screen.queryByTestId('turn-tool-chip-row')) { + fireEvent.click( + screen.getByRole('button', { name: /运行 1 个命令|Ran 1 command/ }) + ); + } + return view; +} + +describe('terminal failure summaries in the timeline', () => { + it('shows a reported nonzero exit code while collapsed and preserves output on expansion', () => { + renderCommand({ id: 'exit-23', code: 23 }); + expect(screen.getByText('命令退出码 23')).toBeVisible(); + expect(screen.queryByText('工具调用失败')).not.toBeInTheDocument(); + const row = screen.getByTestId('turn-tool-chip-row'); + expect(row).toHaveAttribute('aria-expanded', 'false'); + expect(screen.getByTestId('turn-tool-chip-item')).toHaveAttribute( + 'data-tool-status', + 'error' + ); + fireEvent.click(row); + expect(screen.getByText(/original stdout/)).toBeVisible(); + expect(screen.getByText(/original stderr/)).toBeVisible(); + expect(screen.getByText('exit 23')).toBeVisible(); + }); + + it('uses the command result when the transport reports completion', () => { + renderCommand({ id: 'completed-exit-1', status: 'completed', code: 1 }); + expect(screen.getByText('命令退出码 1')).toBeVisible(); + expect(screen.getByTestId('turn-tool-chip-item')).toHaveAttribute( + 'data-tool-status', + 'error' + ); + }); + + it.each([ + ['successful', 'completed', 0, false, 'complete'], + ['running', 'in_progress', undefined, false, 'running'], + ['background', 'failed', 23, true, 'complete'], + ] as const)( + 'does not invent a failure for %s commands', + (id, status, code, backgrounded, expected) => { + renderCommand({ id, status, code, backgrounded }); + expect(screen.getByTestId('turn-tool-chip-item')).toHaveAttribute( + 'data-tool-status', + expected + ); + expect( + screen.queryByText(/命令退出码|工具调用失败/) + ).not.toBeInTheDocument(); + } + ); + + it('keeps an unknown failure generic instead of inventing an exit code', () => { + renderCommand({ id: 'unknown-failure' }); + expect(screen.getByText('工具调用失败')).toBeVisible(); + expect(screen.queryByText(/命令退出码/)).not.toBeInTheDocument(); + }); + + it('localizes the reported exit code in English', async () => { + await i18n.changeLanguage('en'); + renderCommand({ id: 'english-exit', code: 7 }); + expect(screen.getByText('Command exited with code 7')).toBeVisible(); + }); +}); diff --git a/frontend/src/components/NormalizedConversation/TurnActivity.tsx b/frontend/src/components/NormalizedConversation/TurnActivity.tsx index 77d962d08..6b8ad1101 100644 --- a/frontend/src/components/NormalizedConversation/TurnActivity.tsx +++ b/frontend/src/components/NormalizedConversation/TurnActivity.tsx @@ -28,6 +28,7 @@ import { } from './tools/subagentCardModel'; import { getToolCallDescription, + getToolExitStatus, getToolSummary, } from './conversation-entry-utils'; import type { IndexedTurnItem } from './messageTurnAggregate'; @@ -632,6 +633,21 @@ export function TurnActivity({ const expansionKey = `${turnId}-${index}`; const action = toolEntry.action_type; const backgrounded = isToolCallBackgrounded(use.meta); + const status = backgrounded + ? 'complete' + : getToolExitStatus(toolEntry) === 'error' + ? 'error' + : getToolChatStatus(toolEntry.status); + const exitStatus = + action.action === 'command_run' ? action.result?.exit_status : null; + const errorMessage = + status === 'error' + ? exitStatus?.type === 'exit_code' && exitStatus.code !== 0 + ? t('messageTurnView.toolSummary.commandExitCode', { + code: exitStatus.code, + }) + : t('messageTurnView.toolSummary.failed') + : undefined; callCategories.set( toolUseId || expansionKey, summaryCategory(action.action) @@ -710,13 +726,8 @@ export function TurnActivity({ monoChip: !description && isMonoToolChip(action), additions: editStats.additions, deletions: editStats.deletions, - status: backgrounded - ? 'complete' - : getToolChatStatus(toolEntry.status), - errorMessage: - toolEntry.status.status === 'failed' - ? t('messageTurnView.toolSummary.failed') - : undefined, + status, + errorMessage, rawEntry: entry, resultDetail: ( diff --git a/frontend/src/components/NormalizedConversation/TurnToolChips.tsx b/frontend/src/components/NormalizedConversation/TurnToolChips.tsx index e4b6e2c92..23acbf7a6 100644 --- a/frontend/src/components/NormalizedConversation/TurnToolChips.tsx +++ b/frontend/src/components/NormalizedConversation/TurnToolChips.tsx @@ -271,7 +271,9 @@ export function TurnToolChipRow({ ) : null} {isError && errorMessage && !expanded ? ( - {errorMessage} + + {errorMessage} + ) : null} diff --git a/frontend/src/i18n/locales/en/conversation.json b/frontend/src/i18n/locales/en/conversation.json index f4213b76f..418f4c420 100644 --- a/frontend/src/i18n/locales/en/conversation.json +++ b/frontend/src/i18n/locales/en/conversation.json @@ -270,7 +270,8 @@ "otherActivities_one": "{{count}} other activity", "otherActivities_other": "{{count}} other activities", "separator": ", ", - "failed": "Tool call failed" + "failed": "Tool call failed", + "commandExitCode": "Command exited with code {{code}}" }, "fileReadRange": "Lines {{start}}–{{end}}", "fileReadFrom": "From line {{start}}", diff --git a/frontend/src/i18n/locales/zh-CN/conversation.json b/frontend/src/i18n/locales/zh-CN/conversation.json index 328cbc7df..cb0f64c40 100644 --- a/frontend/src/i18n/locales/zh-CN/conversation.json +++ b/frontend/src/i18n/locales/zh-CN/conversation.json @@ -270,7 +270,8 @@ "otherActivities_one": "{{count}} 项其他活动", "otherActivities_other": "{{count}} 项其他活动", "separator": "、", - "failed": "工具调用失败" + "failed": "工具调用失败", + "commandExitCode": "命令退出码 {{code}}" }, "fileReadRange": "第 {{start}}–{{end}} 行", "fileReadFrom": "从第 {{start}} 行开始",