Skip to content

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

Merged
allenhutchison merged 13 commits intomainfrom
fix/acp-stdio-patching
Dec 2, 2025
Merged

refactor(stdio): always patch stdout and use createWorkingStdio for clean output#14159
allenhutchison merged 13 commits intomainfrom
fix/acp-stdio-patching

Conversation

@allenhutchison
Copy link
Collaborator

@allenhutchison allenhutchison commented Nov 30, 2025

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

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

Fixes #13913

…ation

When running `gemini --experimental-acp` as a subprocess (as Zed does),
the CLI would produce no output and hang indefinitely. This was caused
by `patchStdio()` running at the start of `main()` and redirecting
stdout/stderr to internal event handlers, breaking ACP mode which needs
raw stdout for JSON-RPC communication.

This fix detects the `--experimental-acp` flag early (before argument
parsing) using `process.argv` and skips stdio patching when ACP mode
is enabled.

Fixes #13913
@allenhutchison allenhutchison requested a review from a team as a code owner November 30, 2025 03:18
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @allenhutchison, 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 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 👍 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.

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 introduces a fix to prevent the CLI from hanging in ACP mode by skipping stdio patching. The approach of detecting the --experimental-acp flag early is sound. However, the detection mechanism is brittle and will fail for common argument patterns like --experimental-acp=true, which would reintroduce the bug. I've suggested a more robust implementation for the flag detection and an accompanying test case to ensure correctness. Addressing this will make the fix more reliable.

@github-actions
Copy link

github-actions bot commented Nov 30, 2025

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

Address code review feedback:
- Change from `includes('--experimental-acp')` to
  `some(arg => arg.startsWith('--experimental-acp'))` to handle
  cases like `--experimental-acp=true`
- Add test case for `--experimental-acp=true`
Handle all yargs boolean flag formats:
- `--experimental-acp` (true)
- `--experimental-acp=true` (true)
- `--experimental-acp=false` (false)
- `--experimental-acp=0` (false)

Added tests for false value cases.
@codefromthecrypt
Copy link
Contributor

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
Copy link
Contributor

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 added a commit to codefromthecrypt/gemini-cli that referenced this pull request Dec 1, 2025
@codefromthecrypt
Copy link
Contributor

I have an ACP fix waiting for this to be merged first #14190

@allenhutchison allenhutchison changed the title fix(acp): skip stdio patching in ACP mode to enable JSON-RPC communication refactor(stdio): always patch stdout and use createWorkingStdio for clean output Dec 1, 2025
…lean output

This changes the approach from "skip patching in ACP mode" to "always patch,
but use createWorkingStdio() for components needing clean output."

Changes:
- Rename createInkStdio → createWorkingStdio
- Always call patchStdio() in gemini.tsx (remove ACP conditional)
- Use createWorkingStdio().stdout for:
  - Ink UI rendering
  - ACP/Zed JSON-RPC output
  - Non-interactive CLI JSON output
- Fix test environment pollution from ANTIGRAVITY_CLI_ALIAS env var

Fixes #13913
Resolved conflicts in:
- packages/cli/src/gemini.tsx: Always patch stdio, use startupProfiler
- packages/cli/src/zed-integration/zedIntegration.ts: Import both createWorkingStdio and startupProfiler
The isAcpModeFromArgs() function was used to conditionally skip patchStdio()
in ACP mode. With the new design (always patch, use createWorkingStdio for
clean output), this function is no longer needed.
Revert changes to detect-ide.test.ts, clearcut-logger.test.ts, and
EditorSettingsDialog snapshot that were fixing environment pollution
issues unrelated to the stdio refactoring.

Also remove the ACP mode stdio patching tests since patchStdio() is now
always called and those tests no longer add value.
Copy link
Contributor

@jacob314 jacob314 left a comment

Choose a reason for hiding this comment

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

lgtm

@allenhutchison allenhutchison added this pull request to the merge queue Dec 2, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 2, 2025
@allenhutchison allenhutchison added this pull request to the merge queue Dec 2, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 2, 2025
@allenhutchison allenhutchison added this pull request to the merge queue Dec 2, 2025
Merged via the queue into main with commit 828afe1 Dec 2, 2025
22 checks passed
@allenhutchison allenhutchison deleted the fix/acp-stdio-patching branch December 2, 2025 23:15
@brandomagnani
Copy link

@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.

@gillmourdev
Copy link

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

@42piratas
Copy link

42piratas commented Dec 5, 2025

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

@allenhutchison
Copy link
Collaborator Author

/patch preview

@github-actions
Copy link

github-actions bot commented Dec 8, 2025

Patch workflow(s) dispatched successfully!

📋 Details:

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

🔗 Track Progress:

github-actions bot pushed a commit that referenced this pull request Dec 8, 2025
@github-actions
Copy link

github-actions bot commented Dec 8, 2025

🚀 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
Copy link

github-actions bot commented Dec 8, 2025

🚀 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
Copy link

github-actions bot commented Dec 8, 2025

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
Copy link

github-actions bot commented Dec 8, 2025

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:

1 similar comment
@github-actions
Copy link

github-actions bot commented Dec 8, 2025

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
Copy link

github-actions bot commented Dec 8, 2025

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:

@gsabran gsabran mentioned this pull request Dec 8, 2025
18 tasks
@gsabran
Copy link

gsabran commented Dec 10, 2025

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

@gillmourdev
Copy link

the fix seems to be released in 0.20

@gsabran
Copy link

gsabran commented Dec 10, 2025

it has, thanks.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ACP subprocess mode broken in v0.18.0+ - no JSON-RPC response when run as subprocess

7 participants