gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

refactor(stdio): always patch stdout and use createWorkingStdio for clean output

Open allenhutchison opened this issue 3 weeks ago • 5 comments

Summary

  • Refactor stdout handling to ensure consistent logging capture
  • Rename createInkStdiocreateWorkingStdio for clarity
  • Always call patchStdio() and use createWorkingStdio() for clean output

Problem

When running gemini --experimental-acp as a subprocess, the CLI hangs because logging interferes with JSON-RPC communication.

Solution

Design pattern: Always patch stdout/stderr to capture logs, but provide createWorkingStdio() as a bypass mechanism for components that need clean output (JSON-RPC, UI rendering).

This is more robust than conditionally skipping patching because:

  1. Logging always works consistently
  2. Each component explicitly opts into clean output when needed
  3. No risk of logs corrupting JSON output in any mode

Changes

  • packages/core: Rename createInkStdiocreateWorkingStdio
  • packages/cli/gemini.tsx: Always call patchStdio(); use createWorkingStdio() for Ink
  • packages/cli/zedIntegration.ts: Use createWorkingStdio() for JSON output
  • packages/cli/nonInteractiveCli.ts: Use createWorkingStdio() for JSON output

Test plan

  • [x] All existing tests pass
  • [x] Updated ACP mode test to expect patchStdio to be called
  • [x] Fixed IDE detection test pollution from environment variables

Fixes #13913

allenhutchison avatar Nov 30 '25 03:11 allenhutchison

Summary of Changes

Hello @allenhutchison, I'm Gemini Code Assist[^1]! 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 a critical bug where the CLI, when operating in Agent Communication Protocol (ACP) mode as a subprocess, would fail to produce output and hang. The core of the fix involves preventing the CLI's standard I/O patching mechanism from interfering with the raw stdout required for JSON-RPC communication in ACP mode. By introducing an early check for the ACP flag, the system can now conditionally bypass this patching, ensuring seamless operation for integrations that rely on ACP.

Highlights

  • Fix for ACP Mode Hanging: Resolved an issue where the CLI would hang indefinitely when run in Agent Communication Protocol (ACP) mode as a subprocess, particularly when integrated with tools like Zed.
  • Early ACP Mode Detection: Implemented an early detection mechanism for the --experimental-acp flag using process.argv.includes('--experimental-acp') before standard argument parsing occurs.
  • Conditional Stdio Patching: Modified the main function to skip patchStdio() when ACP mode is detected, allowing raw stdout for JSON-RPC communication, which was previously being redirected by internal event handlers.
  • Comprehensive Unit Tests: Added new unit tests to verify the isAcpModeFromArgs() function's behavior and to ensure patchStdio is correctly skipped in ACP mode and called in normal operation.
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 :thumbsup: and :thumbsdown: 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.

[^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.

gemini-code-assist[bot] avatar Nov 30 '25 03:11 gemini-code-assist[bot]

Size Change: +160 B (0%)

Total Size: 21.5 MB

ℹ️ View Unchanged
Filename Size Change
./bundle/gemini.js 21.4 MB +160 B (0%)
./bundle/sandbox-macos-permissive-closed.sb 1.03 kB 0 B
./bundle/sandbox-macos-permissive-open.sb 890 B 0 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB 0 B
./bundle/sandbox-macos-restrictive-closed.sb 3.29 kB 0 B
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB 0 B
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB 0 B

compressed-size-action

github-actions[bot] avatar Nov 30 '25 03:11 github-actions[bot]

Thanks so much. I ran into this problem with neovim/avante and started to solve it before noticing your work. I have one glitch I'm investigating which might be something my side. Otherwise, looks good for things like permissions flow etc.

codefromthecrypt avatar Nov 30 '25 11:11 codefromthecrypt

sorry to be clear I meant this fix works.. I just meant that gemini acp in general, the features I am using are around permissions flow, etc. :shipit:

codefromthecrypt avatar Nov 30 '25 11:11 codefromthecrypt

I have an ACP fix waiting for this to be merged first https://github.com/google-gemini/gemini-cli/pull/14190

codefromthecrypt avatar Dec 01 '25 03:12 codefromthecrypt

@allenhutchison Heads up - this fix wasn't included in v0.19.1.

The merge went to main but the v0.19.1 release was cut from release/v0.19.0 branch without cherry-picking this commit.

Tested v0.19.1 and ACP subprocess mode still hangs. See my comment on #13913 for details.

brandomagnani avatar Dec 03 '25 05:12 brandomagnani

still stuck in loading... guess another weekend with no fix :(

gillmourdev avatar Dec 04 '25 08:12 gillmourdev

PLEASE, this fix is entirely blocking Zed users for weeks!

42piratas avatar Dec 05 '25 21:12 42piratas

/patch preview

allenhutchison avatar Dec 08 '25 19:12 allenhutchison

Patch workflow(s) dispatched successfully!

📋 Details:

  • Channels: preview
  • Commit: 828afe113ea8f33d698e7597f240e268588aec12
  • Workflows Created: 1

🔗 Track Progress:

github-actions[bot] avatar Dec 08 '25 19:12 github-actions[bot]

🚀 Patch PR Created!

📋 Patch Details:

📝 Next Steps:

  1. Review and approve the hotfix PR: #14733
  2. Once merged, the patch release will automatically trigger
  3. You'll receive updates here when the release completes

🔗 Track Progress:

github-actions[bot] avatar Dec 08 '25 19:12 github-actions[bot]

🚀 Patch Release Started!

📋 Release Details:

  • Environment: prod
  • Channel: preview → publishing to npm tag preview
  • Version: v0.20.0-preview.1
  • Hotfix PR: Merged ✅
  • Release Branch: release/v0.20.0-preview.1-pr-14159

⏳ Status: The patch release is now running. You'll receive another update when it completes.

🔗 Track Progress:

github-actions[bot] avatar Dec 08 '25 20:12 github-actions[bot]

Patch Release Failed!

📋 Details:

  • Version: 0.20.0-preview.2
  • Channel: preview
  • Error: The patch release workflow encountered an error

🔍 Next Steps:

  1. Check the workflow logs for detailed error information
  2. The maintainers have been notified via automatic issue creation
  3. You may need to retry the patch once the issue is resolved

🔗 Troubleshooting:

github-actions[bot] avatar Dec 08 '25 20:12 github-actions[bot]

Patch Release Failed!

📋 Details:

  • Version: 0.20.0-preview.3
  • Channel: preview
  • Error: The patch release workflow encountered an error

🔍 Next Steps:

  1. Check the workflow logs for detailed error information
  2. The maintainers have been notified via automatic issue creation
  3. You may need to retry the patch once the issue is resolved

🔗 Troubleshooting:

github-actions[bot] avatar Dec 08 '25 21:12 github-actions[bot]

Patch Release Failed!

📋 Details:

  • Version: 0.20.0-preview.3
  • Channel: preview
  • Error: The patch release workflow encountered an error

🔍 Next Steps:

  1. Check the workflow logs for detailed error information
  2. The maintainers have been notified via automatic issue creation
  3. You may need to retry the patch once the issue is resolved

🔗 Troubleshooting:

github-actions[bot] avatar Dec 08 '25 22:12 github-actions[bot]

Patch Release Failed!

📋 Details:

  • Version: 0.20.0-preview.4
  • Channel: preview
  • Error: The patch release workflow encountered an error

🔍 Next Steps:

  1. Check the workflow logs for detailed error information
  2. The maintainers have been notified via automatic issue creation
  3. You may need to retry the patch once the issue is resolved

🔗 Troubleshooting:

github-actions[bot] avatar Dec 08 '25 22:12 github-actions[bot]

has this fix been released in 0.20? it seems that the patch gh action failed

gsabran avatar Dec 10 '25 01:12 gsabran

the fix seems to be released in 0.20

gillmourdev avatar Dec 10 '25 06:12 gillmourdev

it has, thanks.

gsabran avatar Dec 10 '25 18:12 gsabran