Skip to content

Comments

Update prompt and grep tool definition to limit context size#18780

Merged
gundermanc merged 16 commits intomainfrom
gundermanc/grep-limits-prompt
Feb 11, 2026
Merged

Update prompt and grep tool definition to limit context size#18780
gundermanc merged 16 commits intomainfrom
gundermanc/grep-limits-prompt

Conversation

@gundermanc
Copy link
Member

@gundermanc gundermanc commented Feb 11, 2026

Summary

Steer the agent to use bounded searches and/or advanced searches.

Adds some parameters commonly used in grep based pipelines to narrow searches.

Related Issues

#17112

How to Validate

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
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @gundermanc, 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 grep tool by introducing several new parameters that allow for more precise control over search operations and their output. These additions aim to improve efficiency by enabling users to limit the number of matches, exclude specific patterns, and retrieve only file paths, thereby optimizing context window usage and reducing noise in search results. The changes are integrated into both the git grep and JavaScript fallback implementations, ensuring consistent behavior and robust validation.

Highlights

  • Enhanced Grep Tool Functionality: Introduced new parameters to the grep tool, including exclude_pattern, names_only, max_matches_per_file, and total_max_matches, providing more granular control over search results.
  • Improved Context Efficiency Guidance: Added a new 'Context Efficiency' section to the core mandates prompt, advising users to utilize the new grep limiting parameters to minimize wasted context windows.
  • Comprehensive Testing: Added extensive unit tests to validate the functionality of the new grep parameters across both the JavaScript fallback and ripGrep implementations, ensuring correct behavior for result truncation, file path-only output, and exclusion patterns.
  • Schema and Snapshot Updates: Updated the tool definitions and their corresponding snapshot tests to reflect the newly added grep parameters and their descriptions, ensuring consistency in the tool's schema.
Changelog
  • packages/core/src/prompts/snippets.ts
    • Added a new 'Context Efficiency' section to the core mandates, advising on the use of grep search limits.
  • packages/core/src/tools/definitions/snapshots/coreToolsModelSnapshots.test.ts.snap
    • Updated snapshots to include max_matches_per_file and total_max_matches parameters for the grep tool.
  • packages/core/src/tools/definitions/coreTools.ts
    • Added exclude_pattern, names_only, max_matches_per_file, and total_max_matches to the GREP_DEFINITION.
  • packages/core/src/tools/grep.test.ts
    • Added tests for total_max_matches to ensure result truncation.
    • Added tests for max_matches_per_file to verify per-file match limits.
    • Added tests for names_only to confirm file path-only output.
    • Added tests for exclude_pattern to validate exclusion functionality.
  • packages/core/src/tools/grep.ts
    • Added exclude_pattern, names_only, max_matches_per_file, and total_max_matches to the GrepToolParams interface.
    • Implemented logic to respect total_max_matches for overall result limiting.
    • Integrated exclude_pattern and max_matches_per_file into the _grep function for both git grep and JavaScript fallback.
    • Added output formatting logic for names_only parameter.
    • Added validation for exclude_pattern, max_matches_per_file, and total_max_matches parameters.
  • packages/core/src/tools/ripGrep.test.ts
    • Added tests to verify max_matches_per_file is passed to rg.
    • Added tests for total_max_matches to ensure result truncation in ripGrep.
    • Added tests for names_only to confirm file path-only output in ripGrep.
    • Added tests for exclude_pattern to validate exclusion functionality in ripGrep.
  • packages/core/src/tools/ripGrep.ts
    • Added exclude_pattern, names_only, max_matches_per_file, and total_max_matches to the RipGrepToolParams interface.
    • Implemented logic to respect total_max_matches for overall result limiting in ripGrep.
    • Integrated exclude_pattern and max_matches_per_file into the _ripGrep function and rg command arguments.
    • Added output formatting logic for names_only parameter.
    • Updated the tool description to remove the fixed 'Max 100 matches' limit.
    • Updated tool schema to include new parameters and their descriptions, and added minimum constraints for relevant integer parameters.
    • Added validation for exclude_pattern, max_matches_per_file, and total_max_matches parameters.
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.

@github-actions
Copy link

github-actions bot commented Feb 11, 2026

Size Change: +3.87 kB (+0.02%)

Total Size: 24.3 MB

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

compressed-size-action

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 several useful parameters to the grep and ripgrep tools, such as total_max_matches, max_matches_per_file, exclude_pattern, and names_only, to control output and improve efficiency. A high-severity argument injection vulnerability was identified in the grep_search tool implementation (grep.ts), where the search pattern could be misinterpreted as a command-line option. This has been addressed by using the -e flag in external command calls, with ripgrep remaining unaffected due to its existing use of the --regexp flag. Additionally, a critical issue was found where the system prompt suggests an incorrect parameter name to the LLM, leading to tool call failures.

- **Source Control:** Do not stage or commit changes unless specifically requested by the user.

## Context Efficiency:
- Minimize wasted context window by scoping and limiting all of your ${GREP_TOOL_NAME} searches. e.g.: pass max_total_matches, include, and max_matches_per_file.
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

There's a mismatch between the parameter name suggested in this prompt (max_total_matches) and the actual parameter name defined in the tool's schema (total_max_matches). The LLM will follow the prompt's instruction, leading to failed tool calls because the parameter name will be incorrect. Please update the prompt to use the correct parameter name total_max_matches.

Suggested change
- Minimize wasted context window by scoping and limiting all of your ${GREP_TOOL_NAME} searches. e.g.: pass max_total_matches, include, and max_matches_per_file.
- Minimize wasted context window by scoping and limiting all of your ${GREP_TOOL_NAME} searches. e.g.: pass total_max_matches, include, and max_matches_per_file.

'-n',
'-E',
'--ignore-case',
pattern,
Copy link
Contributor

Choose a reason for hiding this comment

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

security-high high

The pattern parameter is passed as a positional argument to git grep. If the pattern starts with a hyphen (-), it can be interpreted as a command-line option rather than a search pattern. This allows for argument injection, which can be used to manipulate the command's behavior or potentially leak information (e.g., using -f to read patterns from sensitive files).

To fix this, use the -e flag to explicitly specify that the following argument is the pattern.

Suggested change
pattern,
'-e', pattern,

@gemini-cli gemini-cli bot added the priority/p1 Important and should be addressed in the near term. label Feb 11, 2026
@gundermanc gundermanc force-pushed the gundermanc/grep-limits-prompt branch from f92afd0 to 12340dc Compare February 11, 2026 02:07
@gundermanc gundermanc force-pushed the gundermanc/grep-limits-prompt branch from 12340dc to 4b0c9ce Compare February 11, 2026 02:15
@gundermanc gundermanc changed the title Gundermanc/grep limits prompt Update prompt and grep tool definition to limit context size Feb 11, 2026
@gemini-cli gemini-cli bot added area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. labels Feb 11, 2026
@gundermanc gundermanc force-pushed the gundermanc/grep-limits-prompt branch from 379c823 to 838ea7a Compare February 11, 2026 04:09
@gundermanc gundermanc marked this pull request as ready for review February 11, 2026 17:21
@gundermanc gundermanc requested a review from a team as a code owner February 11, 2026 17:21
@joshualitt joshualitt self-requested a review February 11, 2026 18:12
Copy link
Contributor

@alisa-alisa alisa-alisa left a comment

Choose a reason for hiding this comment

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

Some minor comments

/**
* Optional: If true, only the file paths of the matches will be returned.
*/
names_only?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe file_paths_only then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Won't fixing this one. The model seems to understand this one.

/**
* Optional: If true, only the file paths of the matches will be returned.
*/
names_only?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: same as in the other file.

RipGrepTool.Name,
'SearchText',
'Searches for a regular expression pattern within file contents. Max 100 matches.',
'Searches for a regular expression pattern within file contents.',
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we remove the max?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. I'm okay removing this, but should we update GREP_DEFINITION in coreTools.ts for consistency?

import { describe, expect } from 'vitest';
import { evalTest } from './test-helper.js';

describe('Frugal Search', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: for me it is not obvious what this evals do - we don't have frugal search tool or anything, so I imagine, it might be hard to remember in a year or too. I would add a comment or smth.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the feedback. In the interest of keeping momentum (particularly avoiding the need to re-run evals), I'm going to take these in an immediate followup.

- **Source Control:** Do not stage or commit changes unless specifically requested by the user.

## Context Efficiency:
- Always minimize wasted context window by scoping and limiting all of your ${GREP_TOOL_NAME} searches. e.g.: pass total_max_matches, include, and max_matches_per_file.
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider updating the mandate with more refined language, an explicit "why," and a bit more instruction:

Search Frugality: ALWAYS scope and limit your searches to avoid context window exhaustion and ensure high-signal results. Use include to target relevant files and strictly limit results using total_max_matches and max_matches_per_file. For broad discovery, prefer names_only to identify files without retrieving their content.

@gundermanc gundermanc added this pull request to the merge queue Feb 11, 2026
Merged via the queue into main with commit 2a08456 Feb 11, 2026
32 checks passed
@gundermanc gundermanc deleted the gundermanc/grep-limits-prompt branch February 11, 2026 19:40
krsjenmt added a commit to krsjenmt/gemini-cli that referenced this pull request Feb 12, 2026
…ini/gemini-cli (#37)

* fix(cli): resolve double rendering in shpool and address vscode lint warnings (google-gemini#18704)

* feat(plan): document and validate Plan Mode policy overrides (google-gemini#18825)

* Fix pressing any key to exit select mode. (google-gemini#18421)

* fix(cli): update F12 behavior to only open drawer if browser fails (google-gemini#18829)

* feat(plan): allow skills to be enabled in plan mode (google-gemini#18817)

Co-authored-by: Jerop Kipruto <jerop@google.com>

* docs(plan): add documentation for plan mode tools (google-gemini#18827)

* Remove experimental note in extension settings docs (google-gemini#18822)

* Update prompt and grep tool definition to limit context size (google-gemini#18780)

* docs(plan): add `ask_user` tool documentation (google-gemini#18830)

* Revert unintended credentials exposure (google-gemini#18840)

* feat(core): update internal utility models to Gemini 3 (google-gemini#18773)

* feat(a2a): add value-resolver for auth credential resolution (google-gemini#18653)

* Removed getPlainTextLength (google-gemini#18848)

* More grep prompt tweaks (google-gemini#18846)

* refactor(cli): Reactive useSettingsStore hook (google-gemini#14915)

* fix(mcp): Ensure that stdio MCP server execution has the `GEMINI_CLI=1` env variable populated. (google-gemini#18832)

* fix(core): improve headless mode detection for flags and query args (google-gemini#18855)

* refactor(cli): simplify UI and remove legacy inline tool confirmation logic (google-gemini#18566)

* feat(cli): deprecate --allowed-tools and excludeTools in favor of policy engine (google-gemini#18508)

* fix(workflows): improve maintainer detection for automated PR actions (google-gemini#18869)

* refactor(cli): consolidate useToolScheduler and delete legacy implementation (google-gemini#18567)

* Update changelog for v0.28.0 and v0.29.0-preview0 (google-gemini#18819)

* fix(core): ensure sub-agents are registered regardless of tools.allowed (google-gemini#18870)

---------

Co-authored-by: Brad Dux <959674+braddux@users.noreply.github.com>
Co-authored-by: Jerop Kipruto <jerop@google.com>
Co-authored-by: Jacob Richman <jacob314@gmail.com>
Co-authored-by: Sandy Tao <sandytao520@icloud.com>
Co-authored-by: Adib234 <30782825+Adib234@users.noreply.github.com>
Co-authored-by: christine betts <chrstn@uw.edu>
Co-authored-by: Christian Gunderman <gundermanc@gmail.com>
Co-authored-by: Adam Weidman <65992621+adamfweidman@users.noreply.github.com>
Co-authored-by: Dev Randalpura <devrandalpura@google.com>
Co-authored-by: Pyush Sinha <pyushsinha20@gmail.com>
Co-authored-by: Richie Foreman <richie.foreman@gmail.com>
Co-authored-by: Gal Zahavi <38544478+galz10@users.noreply.github.com>
Co-authored-by: Abhi <43648792+abhipatel12@users.noreply.github.com>
Co-authored-by: Abhijit Balaji <abhijitbalaji@google.com>
Co-authored-by: Bryan Morgan <bryanmorgan@google.com>
Co-authored-by: g-samroberts <158088236+g-samroberts@users.noreply.github.com>
Co-authored-by: matt korwel <matt.korwel@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. priority/p1 Important and should be addressed in the near term.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants