diff --git a/api/server/controllers/agents/callbacks.js b/api/server/controllers/agents/callbacks.js index 0f90ed85b8..1651661d8f 100644 --- a/api/server/controllers/agents/callbacks.js +++ b/api/server/controllers/agents/callbacks.js @@ -34,6 +34,7 @@ const { captureSubagentIdentity, getAttachmentOwnership, collectToolCallIds, + stampCommandExecutor, } = require('@librechat/api'); const { processFileCitations } = require('~/server/services/Files/Citations'); const { processCodeOutput, runPreviewFinalize } = require('~/server/services/Files/Code/process'); @@ -659,6 +660,7 @@ function getDefaultHandlers({ toolCall.inputValidationError = true; } } + stampCommandExecutor(toolExecuteOptions?.attachedCommandStepIds, data?.result, toolCall); if (data?.result != null) { await emitForJob({ event, data }); } else if (checkIfLastAgent(metadata?.last_agent_id, metadata?.langgraph_node)) { diff --git a/api/server/services/Endpoints/agents/initialize.js b/api/server/services/Endpoints/agents/initialize.js index 40d2002b56..e62e955b9a 100644 --- a/api/server/services/Endpoints/agents/initialize.js +++ b/api/server/services/Endpoints/agents/initialize.js @@ -444,6 +444,7 @@ const initializeClientWithProvider = async ({ // is composed with this authoritative job signal by the handler. runSignal: signal, foregroundRunId, + attachedCommandStepIds: new Set(), ordinaryToolCancellation: ordinaryToolCancellationEnabled, backgroundCompletionResultMaxChars, loadTools: async ( diff --git a/client/src/components/Chat/Messages/Content/Part.tsx b/client/src/components/Chat/Messages/Content/Part.tsx index f83f1fa556..85ac951ec2 100644 --- a/client/src/components/Chat/Messages/Content/Part.tsx +++ b/client/src/components/Chat/Messages/Content/Part.tsx @@ -413,6 +413,7 @@ const Part = memo(function Part({ runStepDurationMs={toolCall.runStepDurationMs} backgrounded={toolCall.backgrounded} backgroundCancelled={toolCall.backgroundTask?.cancelled === true} + executor={toolCall.executor} attachments={attachments} hideAttachments={hideAttachments} onExpand={onToolExpand} diff --git a/client/src/components/Chat/Messages/Content/Parts/BashCall.tsx b/client/src/components/Chat/Messages/Content/Parts/BashCall.tsx index 81d05dd18e..2cae8f90fb 100644 --- a/client/src/components/Chat/Messages/Content/Parts/BashCall.tsx +++ b/client/src/components/Chat/Messages/Content/Parts/BashCall.tsx @@ -15,6 +15,7 @@ import useFollowScroll from './useFollowScroll'; import { OutputRenderer } from '../ToolOutput'; import { ERROR_PATTERNS } from './ExecuteCode'; import { AttachmentGroup } from './Attachment'; +import { parseCommandOutput } from './command'; import { useToolCallIntent } from './intent'; import { TOOL_ROW_CLASSES } from '../rows'; import PtcToolTrace from './PtcToolTrace'; @@ -30,6 +31,7 @@ export default function BashCall({ runStepDurationMs, backgrounded, backgroundCancelled = false, + executor, initialProgress = 0.1, args, output = '', @@ -45,6 +47,7 @@ export default function BashCall({ runStepDurationMs?: PartMetadata['runStepDurationMs']; backgrounded?: PartMetadata['backgrounded']; backgroundCancelled?: boolean; + executor?: PartMetadata['executor']; args?: string | Record; output?: string; attachments?: TAttachment[]; @@ -58,8 +61,41 @@ export default function BashCall({ const isWritingCommand = !command || !areToolCallArgsComplete(args); const sandboxStarting = useAtomValue(sandboxStartingByToolCallId(toolCallId ?? '')); + /** Only a call the server stamped as attached-workspace carries an exit + * status trailer; sandbox output keeps the text heuristic even when it + * prints something that looks like one. */ + const result = useMemo( + () => (executor === 'attached_workspace' ? parseCommandOutput(output) : null), + [executor, output], + ); const outputHasError = useMemo(() => ERROR_PATTERNS.test(output), [output]); const outputIsEmpty = output.trim() === SANDBOX_EMPTY_OUTPUT; + const verdict = (() => { + if (result?.timedOut === true) { + return localize('com_ui_command_timed_out'); + } + if (result?.signal != null) { + return localize('com_ui_command_terminated', { 0: result.signal }); + } + if (result?.failed === true && result.exitCode != null) { + return localize('com_ui_command_exit_code', { 0: String(result.exitCode) }); + } + return undefined; + })(); + const outputSegments = useMemo( + () => + result == null + ? undefined + : [ + { text: result.head }, + { + text: result.stderr, + className: result.failed ? 'text-status-error' : 'text-text-secondary', + }, + { text: result.trailer, className: 'text-text-tertiary' }, + ], + [result], + ); /** A backgrounded call's persisted output stays the dispatch handle until * the detached run settles and patches it; render a background state * instead of the handle JSON. Completion arrives live as the status marker @@ -91,7 +127,7 @@ export default function BashCall({ hasInput: !!command, onExpand, runStepStatus, - extraError: backgroundFailed, + extraError: backgroundFailed || result?.failed === true, extraCancelled: cancelledInBackground, }); @@ -163,6 +199,7 @@ export default function BashCall({ } hasInput={!!command || hasOutput} isExpanded={showCode} + verdict={verdict} />
@@ -220,7 +257,8 @@ export default function BashCall({ )} diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/BashCall.test.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/BashCall.test.tsx index a70d0c9b09..3e0276e4b5 100644 --- a/client/src/components/Chat/Messages/Content/Parts/__tests__/BashCall.test.tsx +++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/BashCall.test.tsx @@ -8,8 +8,11 @@ import store from '~/store'; jest.mock('~/hooks', () => ({ useLocalize: () => - (key: string): string => { + (key: string, values?: Record): string => { const translations: Record = { + com_ui_command_exit_code: `exit code ${values?.[0]}`, + com_ui_command_terminated: `terminated by ${values?.[0]}`, + com_ui_command_timed_out: 'timed out', com_ui_writing_command: 'Writing command', com_ui_running_command: 'Running command', com_ui_command_finished: 'Finished running', @@ -44,14 +47,17 @@ jest.mock('~/components/Chat/Messages/Content/ProgressText', () => ({ phase, inProgressText, finishedText, + verdict, }: { phase: 'running' | 'completed' | 'cancelled' | 'failed'; inProgressText: string; finishedText: string; + verdict?: string; }) => (
{phase === 'running' ? inProgressText : finishedText} {phase === 'failed' ? ' — tool failed' : ''} + {phase === 'failed' && verdict ? ` · ${verdict}` : ''}
), })); @@ -498,3 +504,67 @@ describe('BashCall output pane', () => { expect(code).toHaveClass('!text-xs', '!whitespace-pre-wrap', '!break-words'); }); }); + +describe('BashCall exit status', () => { + const renderSettled = (output: string, attached = true) => + render( + + + , + ); + + it('does not read a trailer the sandbox command printed itself', () => { + const { container } = renderSettled('stdout:\n[exit code: 1]', false); + expect(screen.getByTestId('progress-text')).toHaveTextContent('Finished running'); + expect(screen.getByTestId('progress-text')).not.toHaveTextContent('tool failed'); + expect(container.querySelectorAll('pre')[1]).toHaveTextContent('[exit code: 1]'); + }); + + it('fails the same output when the server marked it attached-workspace', () => { + renderSettled('stdout:\n[exit code: 1]'); + expect(screen.getByTestId('progress-text')).toHaveTextContent(/tool failed · exit code 1$/); + }); + + it('fails a non-zero exit even when the output matches no error pattern', () => { + renderSettled('stdout:\n1 test failed\n\n[exit code: 2]'); + expect(screen.getByTestId('progress-text')).toHaveTextContent(/tool failed · exit code 2$/); + }); + + it('keeps a zero exit successful even when stderr reads like an error', () => { + const { container } = renderSettled( + 'stdout:\nok\n\nstderr:\nError: deprecated flag\n\n[exit code: 0]', + ); + expect(screen.getByTestId('progress-text')).toHaveTextContent('Finished running'); + expect(screen.getByTestId('progress-text')).not.toHaveTextContent('tool failed'); + const stderr = screen.getByText(/deprecated flag/); + expect(stderr).toHaveClass('text-text-secondary'); + expect(container.querySelectorAll('pre')[1]).toHaveClass('text-text-primary'); + expect(container.querySelectorAll('pre')[1]).toHaveTextContent('stdout: ok'); + }); + + it('marks stderr as an error on a failed run', () => { + renderSettled('stderr:\nboom\n\n[exit code: 1]'); + expect(screen.getByText(/boom/)).toHaveClass('text-status-error'); + }); + + it.each([ + ['stdout:\nsleeping\n\n[terminated by SIGKILL][timed out]', 'timed out'], + ['Command completed with no output.\n[terminated by SIGTERM]', 'terminated by SIGTERM'], + ])('names the reason for a stopped command: %s', (output, reason) => { + renderSettled(output); + expect(screen.getByTestId('progress-text')).toHaveTextContent(`tool failed · ${reason}`); + }); + + it('keeps the text heuristic for sandbox output without an exit trailer', () => { + const { container } = renderSettled('stdout:\nTraceback (most recent call last)\n', false); + expect(screen.getByTestId('progress-text')).not.toHaveTextContent('tool failed'); + expect(container.querySelectorAll('pre')[1]).toHaveClass('text-status-error'); + }); +}); diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/command.test.ts b/client/src/components/Chat/Messages/Content/Parts/__tests__/command.test.ts new file mode 100644 index 0000000000..73507ae504 --- /dev/null +++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/command.test.ts @@ -0,0 +1,47 @@ +import { parseCommandOutput } from '../command'; + +describe('parseCommandOutput', () => { + it('reads a successful run', () => { + const output = 'stdout:\nready\n\n[exit code: 0]'; + expect(parseCommandOutput(output)).toEqual({ + exitCode: 0, + signal: null, + timedOut: false, + truncated: false, + failed: false, + head: 'stdout:\nready\n\n', + stderr: '', + trailer: '[exit code: 0]', + }); + }); + + it('splits stderr and fails a non-zero exit', () => { + const output = 'stdout:\nok\n\nstderr:\nboom\n\n[exit code: 2][output truncated]'; + const result = parseCommandOutput(output); + expect(result).toMatchObject({ exitCode: 2, failed: true, truncated: true }); + expect(result?.head).toBe('stdout:\nok\n\n'); + expect(result?.stderr).toBe('stderr:\nboom\n\n'); + expect(`${result?.head}${result?.stderr}${result?.trailer}`).toBe(output); + }); + + it('reads stderr-only, signal and timeout runs', () => { + expect( + parseCommandOutput('stderr:\nkilled\n\n[terminated by SIGKILL][timed out]'), + ).toMatchObject({ exitCode: null, signal: 'SIGKILL', timedOut: true, failed: true, head: '' }); + expect(parseCommandOutput('Command completed with no output.\n[exit code: 1]')).toMatchObject({ + exitCode: 1, + failed: true, + stderr: '', + }); + }); + + it('returns null for output without the attached-workspace trailer', () => { + expect(parseCommandOutput('stdout:\nhello\n')).toBeNull(); + expect(parseCommandOutput('')).toBeNull(); + expect(parseCommandOutput('Error: rate limited\n[exit code: 1]')).toBeNull(); + }); + + it('ignores a marker the command printed itself', () => { + expect(parseCommandOutput('stdout:\n[exit code: 1]\n')).toBeNull(); + }); +}); diff --git a/client/src/components/Chat/Messages/Content/Parts/command.ts b/client/src/components/Chat/Messages/Content/Parts/command.ts new file mode 100644 index 0000000000..5448de74d7 --- /dev/null +++ b/client/src/components/Chat/Messages/Content/Parts/command.ts @@ -0,0 +1,74 @@ +/** + * Reads the verdict the attached-workspace `bash_tool` writes into its output + * (`formatCommandResult` in `packages/api/src/code/command.ts`): + * + * stdout:\n\n stderr:\n\n [exit code: N][terminated by SIG][timed out][output truncated] + * + * The text alone does not prove where it came from: the sandbox `bash_tool` + * shares the name and trims its output, so a sandbox command that prints + * `[exit code: 1]` last yields the same shape. Callers must only parse output + * of a call whose server-stamped `executor` is `attached_workspace`, and keep + * the text heuristics otherwise. + */ +export interface CommandOutput { + exitCode: number | null; + signal: string | null; + timedOut: boolean; + truncated: boolean; + /** Non-zero exit, a terminating signal or a timeout. */ + failed: boolean; + /** `head + stderr + trailer === output`, so a renderer can style each part + * without dropping any text. `head` is the stdout section (or the + * no-output notice), `stderr` the stderr section with its label. */ + head: string; + stderr: string; + trailer: string; +} + +const TRAILER = + /\n((?:\[(?:exit code: -?\d+|terminated by [\w+-]+|timed out|output truncated)\])+)$/; +const MARKER = /\[(exit code: (-?\d+)|terminated by ([\w+-]+)|timed out|output truncated)\]/g; +const STDOUT = 'stdout:\n'; +const STDERR = 'stderr:\n'; +const EMPTY = 'Command completed with no output.\n'; + +export function parseCommandOutput(output: string): CommandOutput | null { + const match = TRAILER.exec(output); + if (match == null) { + return null; + } + const body = output.slice(0, match.index + 1); + if (!body.startsWith(STDOUT) && !body.startsWith(STDERR) && body !== EMPTY) { + return null; + } + let exitCode: number | null = null; + let signal: string | null = null; + let timedOut = false; + let truncated = false; + for (const [, marker, code, name] of match[1].matchAll(MARKER)) { + if (code != null) { + exitCode = Number(code); + } else if (name != null) { + signal = name; + } else if (marker === 'timed out') { + timedOut = true; + } else { + truncated = true; + } + } + /** The last label wins: output that itself prints `stderr:` is ambiguous, + * and styling too little as stderr is the safer mistake. Either way no + * text is dropped. */ + const labelAt = body.startsWith(STDERR) ? 0 : body.lastIndexOf(`\n${STDERR}`) + 1; + const split = labelAt > 0 || body.startsWith(STDERR) ? labelAt : body.length; + return { + exitCode, + signal, + timedOut, + truncated, + failed: (exitCode != null && exitCode !== 0) || signal != null || timedOut, + head: body.slice(0, split), + stderr: body.slice(split), + trailer: match[1], + }; +} diff --git a/client/src/components/Chat/Messages/Content/ProgressText.tsx b/client/src/components/Chat/Messages/Content/ProgressText.tsx index 25a9249e95..0f7dcf9a02 100644 --- a/client/src/components/Chat/Messages/Content/ProgressText.tsx +++ b/client/src/components/Chat/Messages/Content/ProgressText.tsx @@ -72,6 +72,7 @@ export default function ProgressText({ hasInput = true, popover = false, isExpanded = false, + verdict, }: { /** * The card's settled state, resolved once by the caller via @@ -94,6 +95,9 @@ export default function ProgressText({ hasInput?: boolean; popover?: boolean; isExpanded?: boolean; + /** Why a failed card failed ("exit code 2"), shown after the failure + * suffix. Plain text inside the button, so it is part of its name. */ + verdict?: string; }) { const localize = useLocalize(); /** For locale-aware decimal formatting of the sub-10s duration value. */ @@ -194,6 +198,9 @@ export default function ProgressText({ {errorSuffix && ( · {errorSuffix} )} + {errorSuffix && verdict && ( + · {verdict} + )} {duration && ( <> {/* The compact form is the readable one on screen but a poor diff --git a/client/src/components/Chat/Messages/Content/ToolOutput/OutputRenderer.tsx b/client/src/components/Chat/Messages/Content/ToolOutput/OutputRenderer.tsx index 4a3c652dc8..6d9a006796 100644 --- a/client/src/components/Chat/Messages/Content/ToolOutput/OutputRenderer.tsx +++ b/client/src/components/Chat/Messages/Content/ToolOutput/OutputRenderer.tsx @@ -103,6 +103,37 @@ function extractText(raw: string, verbatim = false): ExtractedText { const TRUNCATE_LINES = 20; const VISIBLE_LINES = 15; +export interface OutputSegment { + text: string; + className?: string; +} + +/** The last `count` lines of `segments`, keeping each line's own styling. */ +function tailSegments(segments: OutputSegment[], count: number): OutputSegment[] { + const tail: OutputSegment[] = []; + /** Matches the line count: a final newline ends the last line rather than starting one. */ + const last = segments.findLast((segment) => segment.text !== ''); + let newlines = last?.text.endsWith('\n') === true ? -1 : 0; + for (let i = segments.length - 1; i >= 0; i--) { + const { text, className } = segments[i]; + let cut = text.length; + while (cut > 0) { + const at = text.lastIndexOf('\n', cut - 1); + if (at < 0) { + break; + } + newlines++; + if (newlines >= count) { + tail.unshift({ text: text.slice(at + 1), className }); + return tail; + } + cut = at; + } + tail.unshift({ text, className }); + } + return tail; +} + interface OutputRendererProps { text: string; copyText?: string; @@ -111,6 +142,9 @@ interface OutputRendererProps { /** `terminal` renders command output: monospace, verbatim (no JSON reformatting), and the * collapsed view keeps the LAST lines, where failures and stack traces land. */ variant?: 'default' | 'terminal'; + /** Terminal output split into styled runs whose texts join to `text` (for example stdout, + * stderr and an exit trailer). Rendered in place of the plain text. */ + segments?: OutputSegment[]; } export default function OutputRenderer({ @@ -118,6 +152,7 @@ export default function OutputRenderer({ copyText, error: forceError = false, variant = 'default', + segments, }: OutputRendererProps) { const localize = useLocalize(); const terminal = variant === 'terminal'; @@ -145,6 +180,9 @@ export default function OutputRenderer({ const collapsedLines = terminal ? lines.slice(-VISIBLE_LINES) : lines.slice(0, VISIBLE_LINES); const visibleText = needsTruncation && !isExpanded ? collapsedLines.join('\n') : displayText; const structured = !isJson && (terminal || isStructuredText(displayText)); + const styled = terminal && segments != null && !error; + const visibleSegments = + styled && needsTruncation && !isExpanded ? tailSegments(segments, VISIBLE_LINES) : segments; return (
@@ -165,7 +203,15 @@ export default function OutputRenderer({ !error && !structured && 'text-text-primary font-sans text-sm', )} > - {visibleText} + {styled + ? visibleSegments?.map((segment, i) => + segment.text === '' ? null : ( + + {segment.text} + + ), + ) + : visibleText} )}
diff --git a/client/src/components/Chat/Messages/Content/ToolOutput/__tests__/OutputRenderer.test.tsx b/client/src/components/Chat/Messages/Content/ToolOutput/__tests__/OutputRenderer.test.tsx index 619433e1e6..2cb2d9f50b 100644 --- a/client/src/components/Chat/Messages/Content/ToolOutput/__tests__/OutputRenderer.test.tsx +++ b/client/src/components/Chat/Messages/Content/ToolOutput/__tests__/OutputRenderer.test.tsx @@ -62,6 +62,37 @@ describe('OutputRenderer', () => { expect(shown[0]).toBe('line 16'); }); + it('keeps segment styling when collapsing terminal output to its tail', () => { + const out = Array.from({ length: 25 }, (_, i) => `out ${i + 1}`).join('\n') + '\n'; + const err = 'stderr:\nboom\n'; + const trailer = '[exit code: 1]'; + render( + , + ); + const pre = screen.getByText(/out 25/).closest('pre') as HTMLElement; + const shown = (pre.textContent ?? '').split('\n'); + expect(shown).toHaveLength(15); + expect(shown[0]).toBe('out 14'); + expect(screen.getByText(/boom/)).toHaveClass('text-status-error'); + expect(screen.getByText('[exit code: 1]')).toHaveClass('text-text-tertiary'); + }); + + it('shows as many styled lines as plain lines when output ends in a newline', () => { + const out = Array.from({ length: 30 }, (_, i) => `row ${i + 1}`).join('\n') + '\n'; + render(); + const shown = (screen.getByText(/row 30/).closest('pre')?.textContent ?? '').split('\n'); + expect(shown.filter(Boolean)).toHaveLength(15); + expect(shown[0]).toBe('row 16'); + }); + it('does not treat text between bracketed prefixes as a tool-call error', () => { expect(isError('Error: [agent] unexpected [search] tool call failed: unavailable')).toBe(false); }); diff --git a/client/src/components/Chat/Messages/Content/__tests__/ProgressText.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/ProgressText.test.tsx index cf6f1c0569..816a7f157b 100644 --- a/client/src/components/Chat/Messages/Content/__tests__/ProgressText.test.tsx +++ b/client/src/components/Chat/Messages/Content/__tests__/ProgressText.test.tsx @@ -237,3 +237,15 @@ describe('ProgressText fixed siblings', () => { expect(container.querySelector('svg')).toHaveClass('shrink-0'); }); }); + +describe('ProgressText verdict', () => { + it('names the failure reason inside the button on a failed card', () => { + renderProgressText({ phase: 'failed', verdict: 'exit code 2' }); + expect(screen.getByRole('button')).toHaveTextContent('· com_ui_tool_failed· exit code 2'); + }); + + it('shows no verdict on a successful card', () => { + renderProgressText({ verdict: 'exit code 2' }); + expect(screen.queryByText(/exit code 2/)).not.toBeInTheDocument(); + }); +}); diff --git a/client/src/components/Chat/Messages/Content/__tests__/outcome.test.ts b/client/src/components/Chat/Messages/Content/__tests__/outcome.test.ts new file mode 100644 index 0000000000..27beb63dad --- /dev/null +++ b/client/src/components/Chat/Messages/Content/__tests__/outcome.test.ts @@ -0,0 +1,47 @@ +import { Tools, ContentTypes } from 'librechat-data-provider'; +import type { TMessageContentParts } from 'librechat-data-provider'; +import { summarizeSpan } from '../outcome'; + +const bashPart = ( + id: string, + output: string, + name: string = Tools.bash_tool, + executor: string | null = 'attached_workspace', +) => + ({ + type: ContentTypes.TOOL_CALL, + [ContentTypes.TOOL_CALL]: { + id, + name, + args: { command: 'make test' }, + output, + progress: 1, + runStepStatus: 'completed', + ...(executor != null && { executor }), + }, + }) as unknown as TMessageContentParts; + +describe('summarizeSpan command exit status', () => { + it('counts a bash call that exited non-zero as failed, like its card', () => { + const summary = summarizeSpan([ + bashPart('a', 'stdout:\nok\n\n[exit code: 0]'), + bashPart('b', 'stdout:\n1 failing\n\n[exit code: 1]'), + bashPart('c', 'stdout:\nslow\n\n[timed out]'), + ]); + expect(summary.failed).toBe(2); + }); + + it('ignores a trailer on sandbox output the server did not mark', () => { + const sandbox = summarizeSpan([ + bashPart('a', 'stdout:\n[exit code: 1]', Tools.bash_tool, null), + ]); + expect(sandbox.failed).toBe(0); + const attached = summarizeSpan([bashPart('a', 'stdout:\n[exit code: 1]')]); + expect(attached.failed).toBe(1); + }); + + it('does not read exit trailers on tools other than bash', () => { + const summary = summarizeSpan([bashPart('a', 'stdout:\nx\n\n[exit code: 1]', 'fetch_image')]); + expect(summary.failed).toBe(0); + }); +}); diff --git a/client/src/components/Chat/Messages/Content/outcome.ts b/client/src/components/Chat/Messages/Content/outcome.ts index 7b039c0c3f..8e310b85a0 100644 --- a/client/src/components/Chat/Messages/Content/outcome.ts +++ b/client/src/components/Chat/Messages/Content/outcome.ts @@ -12,6 +12,7 @@ import { resolveToolCallPhase } from '~/utils/toolCallPhase'; import { isMemoryFailureOutput } from './Parts/MemoryCall'; import { filterAttachmentsForPart } from '~/utils/map'; import { isBashProgrammaticToolCall } from './routing'; +import { parseCommandOutput } from './Parts/command'; import { isError } from './ToolOutput'; /** @@ -122,7 +123,12 @@ export function getToolMeta( name === 'set_memory' || name === 'delete_memory' ? isMemoryFailureOutput(name, tc.output ?? '') || (ownAttachments ?? []).some((attachment) => attachment[Tools.memory]?.type === 'error') - : hasFailedOutput(tc.output); + : hasFailedOutput(tc.output) || + /** `BashCall` fails an attached-workspace command that reports a + * non-zero exit; only the server marker says the trailer is real. */ + (name === Tools.bash_tool && + toolCall.executor === 'attached_workspace' && + parseCommandOutput(tc.output ?? '')?.failed === true); /** A backgrounded bash/code task reports its verdict through a * `background_task_status` attachment, not its output: the dispatch step * keeps a benign handle and usually closes as `completed`. The child card diff --git a/client/src/hooks/SSE/steps/content.ts b/client/src/hooks/SSE/steps/content.ts index fc2b7cd311..cdd458f0c8 100644 --- a/client/src/hooks/SSE/steps/content.ts +++ b/client/src/hooks/SSE/steps/content.ts @@ -333,6 +333,9 @@ export function updateContent( ) { Object.assign(newToolCall, { inputValidationError: true }); } + if (contentPart.tool_call.executor != null) { + newToolCall.executor = contentPart.tool_call.executor; + } } updatedContent[index] = { diff --git a/client/src/locales/en/translation.json b/client/src/locales/en/translation.json index f881fed8d8..582efa088c 100644 --- a/client/src/locales/en/translation.json +++ b/client/src/locales/en/translation.json @@ -1293,8 +1293,11 @@ "com_ui_collapse_chat": "Collapse Chat", "com_ui_collapse_summary": "Collapse Summary", "com_ui_collapse_thoughts": "Collapse Thoughts", + "com_ui_command_exit_code": "exit code {{0}}", "com_ui_command_finished": "Finished running", "com_ui_command_placeholder": "Optional: Enter a command for the prompt or name will be used", + "com_ui_command_terminated": "terminated by {{0}}", + "com_ui_command_timed_out": "timed out", "com_ui_command_usage_placeholder": "Select a Prompt by command or name", "com_ui_complete": "Complete!", "com_ui_complete_setup": "Complete Setup", diff --git a/packages/api/src/agents/handlers.spec.ts b/packages/api/src/agents/handlers.spec.ts index 9fdc0028e9..d42b1cf0ec 100644 --- a/packages/api/src/agents/handlers.spec.ts +++ b/packages/api/src/agents/handlers.spec.ts @@ -25,7 +25,7 @@ import { import { markSandboxReady } from './prewarm'; import { ContentFilterError } from '../middleware/contentFilter'; import { WorkspaceToolHttpError } from '../code/workspace'; -import { createAttachedWorkspaceBashTool } from '../code/command'; +import { createAttachedWorkspaceBashTool, stampCommandExecutor } from '../code/command'; import { createCodeApiUploadRegistry } from '~/utils'; function createMockTool( @@ -8992,3 +8992,77 @@ describe('per-call onResult reporting', () => { expect(results[0].status).toBe('success'); }); }); + +type ExecutorPart = { output?: string; executor?: string }; + +describe('attached-workspace command provenance', () => { + const echoTrailer = 'stdout:\n[exit code: 1]'; + + function attachedTool() { + const fetchImpl = jest.fn( + async () => + new Response( + JSON.stringify({ + protocolVersion: 1, + operation: 'execute_command', + workspaceId: 'project-a', + exitCode: 1, + stdout: 'boom\n', + stderr: '', + truncated: false, + timedOut: false, + }), + { status: 200, headers: { 'Content-Type': 'application/json' } }, + ), + ); + return createAttachedWorkspaceBashTool({ + baseUrl: 'https://code.example.com/v1/', + authHeaders: () => ({}), + workspaceId: 'project-a', + fetchImpl, + }); + } + + async function runStep(bashTool: unknown, stepId: string): Promise> { + const attachedCommandStepIds = new Set(); + const handler = createToolExecuteHandler({ + loadTools: async () => ({ loadedTools: [bashTool as never] }), + attachedCommandStepIds, + }); + await new Promise((resolve, reject) => { + handler.handle('on_tool_execute', { + toolCalls: [{ id: `call-${stepId}`, name: 'bash_tool', args: { command: 'x' }, stepId }], + resolve, + reject, + } as ToolExecuteBatchRequest); + }); + return attachedCommandStepIds; + } + + it('stamps the executor only for the attached-workspace bash tool', async () => { + const attachedSteps = await runStep(attachedTool(), 'step-attached'); + expect([...attachedSteps]).toEqual(['step-attached']); + const attachedEvent = { id: 'step-attached', tool_call: { output: 'x' } as ExecutorPart }; + const attachedPart: ExecutorPart = {}; + stampCommandExecutor(attachedSteps, attachedEvent, attachedPart); + expect(attachedEvent.tool_call.executor).toBe('attached_workspace'); + expect(attachedPart.executor).toBe('attached_workspace'); + expect(attachedSteps.size).toBe(0); + + const sandbox = createMockTool('bash_tool', []); + sandbox.invoke.mockResolvedValue({ + content: echoTrailer, + artifact: { session_id: 'sandbox-session', files: [] }, + }); + const sandboxSteps = await runStep(sandbox, 'step-sandbox'); + expect(sandboxSteps.size).toBe(0); + const sandboxEvent = { + id: 'step-sandbox', + tool_call: { output: echoTrailer } as ExecutorPart, + }; + const sandboxPart: ExecutorPart = {}; + stampCommandExecutor(sandboxSteps, sandboxEvent, sandboxPart); + expect(sandboxEvent.tool_call.executor).toBeUndefined(); + expect(sandboxPart.executor).toBeUndefined(); + }); +}); diff --git a/packages/api/src/agents/handlers.ts b/packages/api/src/agents/handlers.ts index 17a6450fe0..22fb174306 100644 --- a/packages/api/src/agents/handlers.ts +++ b/packages/api/src/agents/handlers.ts @@ -109,6 +109,11 @@ import { runOutsideTracing, getSafeErrorMetadata, } from '~/utils'; +import { + isAttachedWorkspaceBashTool, + resolveAttachedWorkspaceQueueWaitMs, + resolveAttachedWorkspaceRequestTimeoutMs, +} from '~/code/command'; import { ContentFilterError, contentFilterModelBoundBlockResponse, @@ -119,10 +124,6 @@ import { WORKSPACE_EDIT_MAX_COUNT, WORKSPACE_WRITE_MAX_BYTES, } from '~/code/workspace'; -import { - resolveAttachedWorkspaceQueueWaitMs, - resolveAttachedWorkspaceRequestTimeoutMs, -} from '~/code/command'; import { hasIntentArg, stripIntentArg, @@ -298,6 +299,9 @@ export interface ToolExecuteOptions { backgroundCompletionResultMaxChars?: number; /** Callback to process tool artifacts (code output files, file citations, etc.) */ toolEndCallback?: ToolEndCallback; + /** Run steps whose call resolved to the attached-workspace `bash_tool` + * instance; the step-completed handler consumes them to stamp `executor`. */ + attachedCommandStepIds?: Set; /** Durable internal-completion adapter, present only for an Event Actor invocation. */ eventActorDetachedAction?: EventActorDetachedActionLifecycle; /** Called once per batch before tool execution to lazily provision files to tool @@ -5575,6 +5579,7 @@ export function createToolExecuteHandler(options: ToolExecuteOptions): EventHand foregroundRunId, loadTools, toolEndCallback, + attachedCommandStepIds, eventActorDetachedAction, persistBackgroundCodeResult, backgroundToolCompletion, @@ -5799,6 +5804,12 @@ export function createToolExecuteHandler(options: ToolExecuteOptions): EventHand void markSandboxReady(sandboxConversationId); } }; + /** Provenance is the resolved tool instance, never the output. */ + const recordAttachedCommand = (tc: ToolCallRequest, tool: unknown): void => { + if (tc.stepId && isAttachedWorkspaceBashTool(tool)) { + attachedCommandStepIds?.add(tc.stepId); + } + }; const authoringQueues = new Map>(); const sandboxAuthoringContexts = new Map(); @@ -5862,6 +5873,7 @@ export function createToolExecuteHandler(options: ToolExecuteOptions): EventHand }) ?? missingToolResult ); } + recordAttachedCommand(tc, tool); const isCodeCall = isCodeSessionAwareToolCall(tc.name, mergedConfigurable); const harvestEnabled = isCodeCall && persistBackgroundCodeResult != null; const liveArtifactPollRequired = @@ -7378,6 +7390,7 @@ export function createToolExecuteHandler(options: ToolExecuteOptions): EventHand ); return missingToolResult; } + recordAttachedCommand(tc, tool); let normalizedArgs: unknown = tc.args; try { diff --git a/packages/api/src/code/command.ts b/packages/api/src/code/command.ts index 2ec67f9885..f17a068101 100644 --- a/packages/api/src/code/command.ts +++ b/packages/api/src/code/command.ts @@ -26,6 +26,43 @@ import { BACKGROUND_TOOL_INVOCATION_CONFIG_KEY } from '~/agents/invocation'; const DEFAULT_OUTPUT_BYTES = 256 * 1024; +export const ATTACHED_WORKSPACE_EXECUTOR = 'attached_workspace'; + +/** Tools built by `createAttachedWorkspaceBashTool`. Membership is object + * identity, so neither a tool name nor anything a command prints can claim it. */ +const attachedWorkspaceBashTools = new WeakSet(); + +export function isAttachedWorkspaceBashTool(value: unknown): boolean { + return typeof value === 'object' && value != null && attachedWorkspaceBashTools.has(value); +} + +interface ExecutorTarget { + executor?: string; +} + +/** + * Consumes the run step the execute handler recorded when it resolved the + * attached-workspace tool, and stamps the executor on the completion event's + * tool call (streamed) and on the aggregated part (persisted). A step that was + * not recorded is left untouched, whatever its output says. + */ +export function stampCommandExecutor( + attachedStepIds: Set | null | undefined, + result: { id?: unknown; tool_call?: ExecutorTarget } | null | undefined, + part?: ExecutorTarget | null, +): void { + const stepId = result?.id; + if (typeof stepId !== 'string' || attachedStepIds?.delete(stepId) !== true) { + return; + } + if (result?.tool_call != null) { + result.tool_call.executor = ATTACHED_WORKSPACE_EXECUTOR; + } + if (part != null) { + part.executor = ATTACHED_WORKSPACE_EXECUTOR; + } +} + export const ATTACHED_WORKSPACE_BASH_DESCRIPTION = `Runs bash commands inside the selected attached environment and returns stdout/stderr. Its workspace may be an existing project, Git repository, or empty directory. Session behavior: @@ -324,7 +361,7 @@ export function createAttachedWorkspaceBashTool({ buildAttachedWorkspaceBashSchema(effectiveMaxTimeoutMs, environment), ); const actions = environment?.actions ?? []; - return tool( + const bashTool = tool( async ( rawInput: { command?: string; @@ -417,4 +454,6 @@ export function createAttachedWorkspaceBashTool({ responseFormat: 'content_and_artifact', }, ) as unknown as DynamicStructuredTool; + attachedWorkspaceBashTools.add(bashTool); + return bashTool; } diff --git a/packages/api/src/stream/implementations/RedisJobStore.ts b/packages/api/src/stream/implementations/RedisJobStore.ts index 36848f3522..7cb82dc836 100644 --- a/packages/api/src/stream/implementations/RedisJobStore.ts +++ b/packages/api/src/stream/implementations/RedisJobStore.ts @@ -4275,6 +4275,18 @@ export class RedisJobStore implements IJobStoreV2 { // Pass event string directly - GraphEvents values are lowercase strings // eslint-disable-next-line @typescript-eslint/no-explicit-any aggregateContent({ event: event.event as any, data: event.data as any }); + + // The SDK aggregator rebuilds tool calls from known fields only, so the + // server-stamped executor is copied back from the stored completion. + if (event.event === 'on_run_step_completed') { + const completed = (event.data as { result?: Agents.ToolEndEvent }).result; + const executor = completed?.tool_call?.executor; + const index = completed?.id != null ? replayedStepIndices.get(completed.id) : undefined; + const part = index != null ? contentParts[index] : undefined; + if (executor != null && part?.type === ContentTypes.TOOL_CALL && part.tool_call) { + part.tool_call.executor = executor; + } + } } const reasoningIndices = new Set([ diff --git a/packages/data-provider/src/types/agents.ts b/packages/data-provider/src/types/agents.ts index 7e13f3799f..cff15cee4a 100644 --- a/packages/data-provider/src/types/agents.ts +++ b/packages/data-provider/src/types/agents.ts @@ -118,6 +118,8 @@ export namespace Agents { }; /** The tool call was rejected before execution because its input failed schema validation. */ inputValidationError?: true; + /** Server-stamped provenance; see `PartMetadata.executor`. */ + executor?: 'attached_workspace'; /** Auth URL */ auth?: string; /** Expiration time */ diff --git a/packages/data-provider/src/types/content.ts b/packages/data-provider/src/types/content.ts index bdeeac8c7e..47dea9bc56 100644 --- a/packages/data-provider/src/types/content.ts +++ b/packages/data-provider/src/types/content.ts @@ -169,6 +169,13 @@ export type PartMetadata = { * as dispatch time rather than the task's runtime. */ backgrounded?: boolean; + /** + * Stamped by the server when the run step completes, and only when the call + * resolved to the attached-workspace `bash_tool` instance. The sandbox tool + * shares that name, so this is the only trustworthy sign that the output + * ends in an exit-status trailer; it is never derived from output text. + */ + executor?: 'attached_workspace'; /** * Content index this part occupied while its run streamed. The aggregator * writes parts at provider-source indexes, so the streamed array is sparse;