Skip to content

fix(cli): harden prompt trimming and improve version detection#23828

Open
chrisjcthomas wants to merge 4 commits intogoogle-gemini:mainfrom
chrisjcthomas:codex/pr21679-code
Open

fix(cli): harden prompt trimming and improve version detection#23828
chrisjcthomas wants to merge 4 commits intogoogle-gemini:mainfrom
chrisjcthomas:codex/pr21679-code

Conversation

@chrisjcthomas
Copy link
Copy Markdown
Contributor

Summary

  • Split the non-docs changes out of docs: add vi shortcuts and clarify MCP sandbox setup #21679 for separate review
  • Harden prompt submission by safely coercing submittedValue before trimming in the CLI input flow
  • Stringify array settings values in the settings dialog and prefer the repository package version when running from source

Testing

  • npx vitest run src/utils/version.test.ts
  • npx vitest run src/utils/settingsUtils.test.ts

@chrisjcthomas chrisjcthomas requested a review from a team as a code owner March 25, 2026 21:13
@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 enhances the CLI's robustness and improves its display and versioning mechanisms. It hardens user input handling by ensuring prompt values are always strings before processing, refines the display of array-based settings by serializing them to JSON, and improves version detection by preferring the main repository's package version when running from a development environment.

Highlights

  • CLI Input Hardening: Implemented safer prompt submission by coercing submittedValue to a string before trimming, preventing potential errors with non-string inputs.
  • Settings Display Improvement: Ensured array settings values are correctly stringified as JSON when displayed in the settings dialog.
  • Version Detection Enhancement: Updated the version detection logic to prioritize the repository's main package version when the CLI is run from source.
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.

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.

@chrisjcthomas
Copy link
Copy Markdown
Contributor Author

@jkcinouye I split the non-docs fixes out of #21679 into this separate PR so they can be reviewed independently. If you have a chance, I’d appreciate a look.

@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 Mar 25, 2026
Copy link
Copy Markdown
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 refactors the version lookup logic to traverse up the directory tree, prioritizing the main CLI package version, and adds robust handling for null/undefined input values in UI components. It also enhances the settings display utility to correctly format arrays as JSON strings, with a corresponding new test case. A review comment points out that the new version lookup implementation in packages/core/src/utils/version.ts is inefficient due to redundant directory scanning and suggests a more performant and maintainable approach using asynchronous file system operations.

Note: Security Review did not run due to the size of the PR.

Comment on lines +25 to +47
let currentDir = __dirname;
let bestVersion = 'unknown';

while (true) {
const pkgJson = await getPackageJson(currentDir);
if (pkgJson?.version) {
bestVersion = pkgJson.version;
if (
pkgJson.name === '@google/gemini-cli' ||
pkgJson.name === 'gemini-cli'
) {
break;
}
}

const parentDir = path.dirname(currentDir);
if (parentDir === currentDir) {
break;
}
currentDir = parentDir;
}

return bestVersion;
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.

high

This implementation is inefficient because it combines a while loop that traverses up the directory tree with a call to getPackageJson, which also searches up the tree using read-package-up. This results in redundant directory scanning in each iteration.

While the result is cached after the first run, this implementation is difficult to maintain and reason about. A more efficient approach would be to perform a single, explicit upward traversal. For example, using Node's fs.promises module to asynchronously read package.json from currentDir directly within the loop would make the logic clearer and more performant, and avoid blocking the event loop.

References
  1. Use asynchronous file system operations (e.g., fs.promises.readFile) instead of synchronous ones (e.g., fs.readFileSync) to avoid blocking the event loop.

@chrisjcthomas
Copy link
Copy Markdown
Contributor Author

@g-samroberts adding you here as well since you had context on #21679. This is the split code-only follow-up from that PR; if you have a chance, I’d appreciate a look.

@gemini-cli gemini-cli Bot added the area/documentation Gemini CLI documentation tasks and issues label Mar 25, 2026
@chrisjcthomas chrisjcthomas requested a review from a team as a code owner March 27, 2026 07:34
@chrisjcthomas
Copy link
Copy Markdown
Contributor Author

Addressed the version lookup review note in the latest push. The implementation now performs a single explicit upward traversal with async package.json reads instead of combining an outer traversal with getPackageJson()'s own upward search. Also updated the focused unit test coverage for the source-version preference and cache behavior.

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 area/documentation Gemini CLI documentation tasks and issues 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.

1 participant