fix(core): limit recursive reasoning turns per single user request#28164
fix(core): limit recursive reasoning turns per single user request#28164amelidev wants to merge 2 commits into
Conversation
|
📊 PR Size: size/M
|
Summary of ChangesHello, 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 introduces a safety mechanism to the core agent reasoning engine to mitigate potential infinite loop exploits. By enforcing a maximum turn limit per request, the system is better protected against malicious inputs that could otherwise exhaust local CPU resources or API quotas. The implementation includes flexible configuration options and robust unit testing to ensure reliable behavior across different usage scenarios. Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to limit recursive turns per single prompt_id in GeminiClient. It tracks the turn count using a new promptTurnCount property, resetting it when the prompt_id changes. If the turn count exceeds the configured maximum (defaulting to 15), it yields a MaxSessionTurns event and terminates the turn. Corresponding unit tests have been added to verify both the default and custom limits, as well as the reset behavior. There are no review comments, so no additional feedback is provided.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to limit recursive turns per single prompt_id in GeminiClient to prevent infinite loops, defaulting to 15 turns or respecting a custom configured limit. It also adds a comprehensive test suite to verify this behavior. Feedback points out a leftover debug statement in the test file that writes to ./debug-test.log and should be removed.
| const fs = await import('node:fs'); | ||
| fs.writeFileSync('./debug-test.log', 'START TEST\n'); |
Summary
This PR implements a strict recursive reasoning turn limit of 15 turns per single user request (customizable if
maxSessionTurnsis configured to a positive value) in the core agent reasoning engine. This protects user local CPU resources and model API quotas/credits from infinite loop exploits caused by malicious workspace files with indirect prompt injection.Details
promptTurnCounttoGeminiClientwhich tracks recursive turn depth scoped precisely to the uniqueprompt_idof the user's single query.0whenever a new prompt is initiated (transitioning to a newprompt_id).maxSessionTurnsis set by the user to a positive integer, that limit is honored; otherwise, it defaults to a safe and robust limit of 15 turns.MaxSessionTurnsevent and aborts recursive processing, allowing the CLI layers to gracefully terminate execution.packages/core/src/core/client.test.ts.Related Issues
Resolves the infinite loop issue described in
issue.md.How to Validate
npm test -w @google/gemini-cli-core -- src/core/client.test.tsPre-Merge Checklist