Skip to content

fix(cli): prevent ghost text wrapping infinite loop at narrow widths - #29239

Open
Anurag-M1 wants to merge 1 commit into
google-gemini:mainfrom
Anurag-M1:fix/input-prompt-ghost-text-infinite-loop
Open

fix(cli): prevent ghost text wrapping infinite loop at narrow widths#29239
Anurag-M1 wants to merge 1 commit into
google-gemini:mainfrom
Anurag-M1:fix/input-prompt-ghost-text-infinite-loop

Conversation

@Anurag-M1

Copy link
Copy Markdown

Fixes #19985

Description

In packages/cli/src/ui/components/InputPrompt.tsx, getGhostTextLines attempts to wrap words using a while loop:

while (stringWidth(wordToProcess) > inputWidth) {
  let part = '';
  const wordCP = toCodePoints(wordToProcess);
  let partWidth = 0;
  let splitIndex = 0;
  for (let i = 0; i < wordCP.length; i++) {
    const char = wordCP[i];
    const charWidth = stringWidth(char);
    if (partWidth + charWidth > inputWidth) {
      break;
    }
    part += char;
    partWidth += charWidth;
    splitIndex = i + 1;
  }
  additionalLines.push(part);
  wordToProcess = cpSlice(wordToProcess, splitIndex);
}

When inputWidth is narrower than a single character's rendered width (such as wide CJK or emoji characters with width 2 in narrow terminals where inputWidth <= 1), splitIndex stays 0. Because wordToProcess = cpSlice(wordToProcess, splitIndex) fails to shrink, the loop spins indefinitely at 100% CPU and freezes the terminal prompt.

Solution

Added a safeguard after the character width loop:

if (splitIndex === 0 && wordCP.length > 0) {
  part = wordCP[0];
  splitIndex = 1;
}

This guarantees that wordToProcess shrinks by at least one code point per iteration, ensuring the hard-split loop always terminates.

Testing

  • Added regression test in packages/cli/src/ui/components/InputPrompt.test.tsx under describe('ghost text wrapping') verifying that rendering with narrow inputWidth = 1 and wide character ghost text completes without hanging.
  • All 200 unit tests in InputPrompt.test.tsx pass cleanly.
  • npm run typecheck --workspace @google/gemini-cli passed with 0 errors.
  • eslint passed with 0 warnings.

Fixes google-gemini#19985

When inputWidth is narrower than a single character's rendered width
(e.g. wide CJK or emoji characters in narrow terminal widths), splitIndex
remains 0 in getGhostTextLines and wordToProcess never shrinks, causing
an infinite while loop that pegs CPU at 100% and freezes the prompt.

Force-advance by at least one code point when splitIndex is 0 so the
hard-split loop is guaranteed to terminate. Adds a regression unit test.
@Anurag-M1
Anurag-M1 requested a review from a team as a code owner September 7, 2026 17:20
@github-actions github-actions Bot added the size/s A small PR label Sep 7, 2026
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

📊 PR Size: size/S

  • Lines changed: 42
  • Additions: +42
  • Deletions: -0
  • Files changed: 2

@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 a critical issue where the CLI input prompt would enter an infinite loop and freeze the terminal when rendering ghost text in extremely narrow widths. By forcing the string processing loop to advance even when a character's width exceeds the available space, the fix ensures stability in constrained terminal environments.

Highlights

  • Infinite Loop Prevention: Added a safety check in the ghost text wrapping logic to ensure that the processing loop always advances by at least one code point, preventing CPU hangs when the input width is narrower than a single character.
  • Regression Testing: Introduced a new test case in InputPrompt.test.tsx that simulates narrow terminal widths with wide characters to verify the fix and prevent future regressions.
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.

@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 fixes an infinite loop issue in InputPrompt.tsx that occurs when the inputWidth is narrower than a single codepoint (e.g., when dealing with wide characters). It introduces a guard to force-advance by one codepoint when splitIndex remains 0. Additionally, a regression test has been added in InputPrompt.test.tsx to verify this behavior and prevent future freezes. There are no review comments, so I have no feedback to provide.

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality 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. size/s A small PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI hangs/freezes when using @filename:line or @filename:range syntax

1 participant