Skip to content

fix(cli): report error status and exit code in stream-json tool_result for failed shell commands - #30521

Open
zsicheng0214 wants to merge 1 commit into
google-gemini:mainfrom
zsicheng0214:fix/stream-json-tool-result-nonzero-exit
Open

zsicheng0214 wants to merge 1 commit into
google-gemini:mainfrom
zsicheng0214:fix/stream-json-tool-result-nonzero-exit

Conversation

@zsicheng0214

@zsicheng0214 zsicheng0214 commented Nov 29, 2026 •

Copy link
Copy Markdown

Summary

When run_shell_command exits with a non-zero exit code in headless --output-format stream-json mode, gemini-cli currently emits tool_result with "status": "success" and omits both the error object and the non-zero exit code.

This PR updates both non-interactive CLI entry points (nonInteractiveCli.ts and nonInteractiveCliAgentSession.ts) so that when toolResponse.data.isError === true (as set by ShellToolInvocation on non-zero exit codes), the emitted stream-json tool_result event accurately reports "status": "error" and includes an error payload with the exit code (e.g., Command exited with code 42).

Details

Root Cause

  • In packages/core/src/tools/shell.ts (ShellToolInvocation.execute), when a shell command terminates with a non-zero exit code, the tool returns structured metadata data: { exitCode, isError: true } while keeping resultDisplay as the command's stdout/stderr output and leaving toolResponse.error unset (so completedToolCall.status stays 'success').
  • In packages/cli/src/nonInteractiveCli.ts and packages/cli/src/nonInteractiveCliAgentSession.ts, streamFormatter.emitEvent previously checked only completedToolCall.status === 'error' / event.isError and toolResponse.error.
  • As a result, downstream headless consumers (CI runners, IDE integrations, trajectory evaluators) parsing --output-format stream-json saw:
    {"type":"tool_result","tool_id":"run_shell_command-...","status":"success","output":"..."}
    even when the command failed with exit code 42 (or 1, 127, etc.) and had no stderr.

Fix

  • Check data?.['isError'] === true in addition to completedToolCall.status === 'error' / event.isError when computing status for JsonStreamEventType.TOOL_RESULT.
  • When data.isError === true and toolResponse.error is absent, populate error: { type: errorType || 'TOOL_EXECUTION_ERROR', message: exitCode !== undefined ? 'Command exited with code <N>' : 'Tool execution failed' }.
  • Preserves existing behavior for both zero-exit shell calls (isError: false) and hard tool execution errors where toolResponse.error is already set.
  • Adds unit test coverage in packages/cli/src/nonInteractiveCli.test.ts verifying non-zero shell exit codes emit status: 'error' and error.message: 'Command exited with code 42'.

Related Issues

Fixes #30553

Related: #30542 (timeout) and #30543 (signal termination) cover other shell failure modes and are not fixed here.

How to Validate

  1. Run the non-interactive CLI unit test suite:
    npx vitest run packages/cli/src/nonInteractiveCli.test.ts
    Verified: 70/70 tests passed (including the new regression test should emit error status and exit code in stream-json tool_result when toolResponse.data.isError is true).
  2. Run formatting and lint checks on modified files:
    npx prettier --check packages/cli/src/nonInteractiveCli.ts packages/cli/src/nonInteractiveCliAgentSession.ts packages/cli/src/nonInteractiveCli.test.ts
    npx eslint packages/cli/src/nonInteractiveCli.ts packages/cli/src/nonInteractiveCliAgentSession.ts packages/cli/src/nonInteractiveCli.test.ts
    Verified: 0 formatting issues, 0 lint errors/warnings.

Pre-Merge Checklist

  • Added/updated tests (if needed)
  • Validated on required platforms/methods:
    • Linux
      • npm run
      • npx

@zsicheng0214
zsicheng0214 requested a review from a team as a code owner November 29, 2026 02:15
@google-cla

google-cla Bot commented Nov 29, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where shell command failures were incorrectly reported as successful in the headless stream-json output format. By checking for the 'isError' flag and exit code metadata, the CLI now accurately reflects the failure status and provides descriptive error messages for downstream consumers.

Highlights

  • Improved Error Reporting: Updated the CLI to correctly identify and report shell command failures as 'error' status in stream-json output when a non-zero exit code is returned.
  • Enhanced Tool Result Metadata: Included specific error messages containing the exit code (e.g., 'Command exited with code 42') in the tool_result event when a shell command fails.
  • Regression Testing: Added a new unit test in nonInteractiveCli.test.ts to verify that non-zero shell exit codes are properly captured and emitted in the stream-json output.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added the size/m A medium sized PR label Nov 29, 2026
@github-actions

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 114
  • Additions: +108
  • Deletions: -6
  • Files changed: 3

@gemini-cli gemini-cli Bot added the status/need-issue Pull requests that need to have an associated issue. label Nov 29, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the non-interactive CLI and agent session handlers to correctly detect and report errors when a shell tool execution fails with a non-zero exit code (indicated by isError and exitCode in the tool's response data). It ensures that the JSON stream formatter emits an error status and includes a descriptive error message containing the exit code. A corresponding unit test has been added to verify this behavior. I have no feedback to provide.

@zsicheng0214

Copy link
Copy Markdown
Author

@googlebot I signed it!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: non-zero exit from run_shell_command is reported as status "success" in stream-json tool_result

1 participant