Skip to content

fix: prevent hangs in non-interactive mode and improve agent guidance#20893

Merged
cocosheng-g merged 5 commits intogoogle-gemini:mainfrom
cocosheng-g:analysis/issue-5724-16567
Mar 10, 2026
Merged

fix: prevent hangs in non-interactive mode and improve agent guidance#20893
cocosheng-g merged 5 commits intogoogle-gemini:mainfrom
cocosheng-g:analysis/issue-5724-16567

Conversation

@cocosheng-g
Copy link
Contributor

Summary

This PR improves stability and agent behavior in non-interactive mode by preventing tool hangs and providing clearer guidance to the model.

Details

  1. Fast Failure for Subprocesses: Updated childProcessFallback in packages/core/src/services/shellExecutionService.ts to inject a minimal set of environment variables (GIT_TERMINAL_PROMPT, GIT_ASKPASS, GH_PROMPT_DISABLED, DISPLAY, etc.). This forces tools like Git and GitHub CLI to fail instantly rather than hanging for 5 minutes when they encounter an interactive credential requirement while running in a detached process group without a TTY.
  2. Interaction-Aware Agent: Updated non-interactive system instructions in packages/core/src/prompts/snippets.ts. The agent is now explicitly informed it is in a headless environment and should not ask the user questions, but instead explain failures (like missing credentials) and suggest alternatives.
  3. Unit Tests: Added a new test case in shellExecutionService.test.ts to verify the correct injection of these variables and updated prompt snapshots.

Related Issues

Fixes #16567
Addresses #5724

How to Validate

  1. Run the following command in a terminal (it should fail instantly with a credential error instead of hanging):
    DEBUG=gemini* npm run start -- --prompt "git clone https://fakeuser@github.com/google-gemini/private-repo.git" --yolo --allowed-tools=run_shell_command
  2. Run unit tests for affected packages:
    npm test -w @google/gemini-cli-core -- src/services/shellExecutionService.test.ts
    npm test -w @google/gemini-cli-core -- src/core/prompts.test.ts

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
    • Windows
    • Linux
      • npm run

@cocosheng-g cocosheng-g requested a review from a team as a code owner March 2, 2026 22:22
@gemini-code-assist
Copy link
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 significantly enhances the stability and behavior of the agent when operating in non-interactive or headless environments. It addresses potential issues where tools might hang awaiting user input by configuring subprocesses to fail quickly under such conditions. Additionally, it refines the agent's instructions to ensure it provides clear explanations for failures and suggests alternative solutions, rather than attempting to interact with the user in contexts where interaction is not possible.

Highlights

  • Prevented Tool Hangs: Tool hangs in non-interactive mode were prevented by injecting specific environment variables (e.g., GIT_TERMINAL_PROMPT, GH_PROMPT_DISABLED) for Git and GitHub CLI, forcing them to fail instantly when interactive credential requirements are encountered.
  • Improved Agent Guidance: Agent guidance for non-interactive environments was improved, explicitly informing the agent it is in a headless environment and should explain failures and suggest alternatives instead of asking for user input.
  • Added Unit Tests: A new unit test was added to verify the correct injection of headless environment variables in the shell execution service.
Changelog
  • packages/core/src/core/snapshots/prompts.test.ts.snap
    • Updated the snapshot for non-interactive prompt instructions.
  • packages/core/src/prompts/snippets.ts
    • Modified the mandateContinueWork function to include detailed guidance for agents operating in headless/CI environments, instructing them not to ask questions and to explain limitations.
  • packages/core/src/services/shellExecutionService.test.ts
    • Added a new test case to confirm that specific environment variables (like GIT_TERMINAL_PROMPT, GH_PROMPT_DISABLED) are correctly injected when executing shell commands in non-interactive mode.
  • packages/core/src/services/shellExecutionService.ts
    • Injected a set of environment variables into child processes when in non-interactive mode to disable interactive prompts for tools like Git and GitHub CLI, preventing them from hanging.
Activity
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

@cocosheng-g cocosheng-g self-assigned this Mar 2, 2026
@cocosheng-g
Copy link
Contributor Author

/gemini review

Copy link
Contributor

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

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 effectively addresses hangs in non-interactive mode by disabling interactive prompts for shell commands executed without a TTY. The changes in shellExecutionService.ts correctly inject environment variables to force tools like Git to fail fast instead of hanging. The accompanying improvements to the agent's system prompt provide clearer guidance for behavior in headless environments. The new unit tests properly validate the core logic change. The implementation is robust and aligns with the goal of improving stability in non-interactive sessions.

@cocosheng-g cocosheng-g requested a review from kschaab March 2, 2026 22:27
@cocosheng-g cocosheng-g enabled auto-merge March 2, 2026 22:27
Copy link
Contributor

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

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 makes excellent improvements to prevent hangs in non-interactive mode by disabling interactive prompts from tools like Git and providing clearer guidance to the agent. The changes to the system prompts and the addition of unit tests are well-executed. However, I've identified a critical issue in how the Git configuration environment variables are being set, which could unintentionally break users' existing configurations. My review includes a detailed explanation and a suggested fix for this issue.

@gemini-cli gemini-cli bot added priority/p2 Important but can be addressed in a future release. area/non-interactive Issues related to GitHub Actions, SDK, 3P Integrations, Shell Scripting, Command line automation help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Mar 2, 2026
@cocosheng-g
Copy link
Contributor Author

/gemini review

Copy link
Contributor

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

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 improves the agent's behavior in non-interactive mode by preventing hangs from tools requiring credentials and by providing better guidance to the model. However, a critical issue was identified in shellExecutionService.ts: environment variables intended for non-interactive mode are unconditionally applied in the child_process fallback, which could break interactive credential prompts if the PTY fails to load during an interactive session. No specific security vulnerabilities were identified in this review.

@cocosheng-g
Copy link
Contributor Author

/gemini review

Copy link
Contributor

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

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 effectively addresses process hangs in non-interactive mode by setting environment variables to prevent tools like Git from prompting for credentials. The accompanying changes to the agent's system prompt provide better guidance for headless environments, and the new unit tests are comprehensive. Based on repository rules, the suggested robustness check for GIT_CONFIG_COUNT is not necessary as we assume internally managed configurations are valid.

@cocosheng-g cocosheng-g added this pull request to the merge queue Mar 10, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 10, 2026
@cocosheng-g cocosheng-g enabled auto-merge March 10, 2026 19:20
cocosheng-g and others added 4 commits March 10, 2026 16:04
…idance

- Injects headless-safe environment variables into non-interactive subprocesses to prevent credential prompt deadlocks (fixes google-gemini#16567).
- Updates system instructions for non-interactive mode to discourage impossible user questions (addresses google-gemini#5724).
- Adds unit tests to verify environment injection.
Co-authored-by: Keith Schaab <keith.schaab@gmail.com>
@cocosheng-g cocosheng-g force-pushed the analysis/issue-5724-16567 branch from 1351a4c to 0f06a03 Compare March 10, 2026 20:07
@cocosheng-g cocosheng-g added this pull request to the merge queue Mar 10, 2026
Merged via the queue into google-gemini:main with commit 519c75f Mar 10, 2026
27 checks passed
@cocosheng-g cocosheng-g deleted the analysis/issue-5724-16567 branch March 10, 2026 20:45
JaisalJain pushed a commit to JaisalJain/gemini-cli that referenced this pull request Mar 11, 2026
liamhelmer pushed a commit to badal-io/gemini-cli that referenced this pull request Mar 12, 2026
yashodipmore pushed a commit to yashodipmore/geemi-cli that referenced this pull request Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/non-interactive Issues related to GitHub Actions, SDK, 3P Integrations, Shell Scripting, Command line automation help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gemini CLI consistently hangs when running a command in non-interactive mode

2 participants