Skip to content

fix: Tool call was added to history without being executed when hitting SubAgent max turn#14703

Closed
Amanda111 wants to merge 2 commits intogoogle-gemini:mainfrom
Amanda111:feat/subagent-max-turn-optimization
Closed

fix: Tool call was added to history without being executed when hitting SubAgent max turn#14703
Amanda111 wants to merge 2 commits intogoogle-gemini:mainfrom
Amanda111:feat/subagent-max-turn-optimization

Conversation

@Amanda111
Copy link

Summary

The issue occurred because the termination check happened before executing the last turn, causing tool calls to be added to history without being executed. Now the warning message is appended after the last turn's tools execute, giving the model a final chance to complete with full context.

Details

Changes:

  • Move max_turns check to happen after turn execution, not before
  • Append warning message to tool results instead of replacing them
  • Add checkIsLastTurn() helper
  • Ensure all tool calls have corresponding tool results

Related Issues

#14088

How to Validate

Hitting SubAgent max turn

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

…ausing tool calls to be added to history without being executed
@Amanda111 Amanda111 requested a review from a team as a code owner December 8, 2025 12:27
@google-cla
Copy link

google-cla bot commented Dec 8, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Amanda111, 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 bug where tool calls were incorrectly added to the agent's history without being executed when the SubAgent reached its maximum turn limit. The core fix involves reordering the termination check to happen after a turn's execution and introducing a mechanism to append a warning message and allow a final turn for the agent to complete its task with full context, thereby preventing incomplete state issues and improving the agent's ability to resolve tasks under turn constraints.

Highlights

  • Termination Logic Refinement: The agent's termination check for max_turns has been moved to occur after a turn's execution, rather than before, to ensure all actions are processed.
  • Enhanced Last Turn Handling: When the max_turns limit is reached, a warning message is now appended to the tool results, and the agent is given one final turn to process this context.
  • Improved Agent Resilience: This change prevents tool calls from being recorded in the history without actual execution, ensuring the agent has a complete context to potentially resolve its task even when hitting turn limits.
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 fixes an important issue where tool calls on the final turn of a sub-agent were not executed before the max_turns termination. The new approach of checking isLastTurn and allowing one final turn with a warning is a good solution. However, I've identified a critical issue where the new logic for handling max_turns conflicts with the existing generic recovery mechanism, leading to a redundant recovery attempt. This should be addressed before merging. Additionally, the related unit tests need to be updated to reflect these significant logic changes, as the current tests do not catch this new issue.

Comment on lines +432 to +463
if (isLastTurn && turnResult.status === 'continue') {
// Append warning message to the tool results
const warningText = this.getFinalWarningMessage(
AgentTerminateMode.MAX_TURNS,
);
const nextMessageParts = turnResult.nextMessage.parts || [];
nextMessageParts.push({ text: warningText });
currentMessage = {
role: 'user',
parts: nextMessageParts,
};

// Execute one more turn with the warning included
const finalTurnResult = await this.executeTurn(
chat,
currentMessage,
turnCounter++,
combinedSignal,
timeoutController.signal,
);

if (
finalTurnResult.status === 'stop' &&
finalTurnResult.terminateReason === AgentTerminateMode.GOAL
) {
terminateReason = AgentTerminateMode.GOAL;
finalResult = finalTurnResult.finalResult;
} else {
terminateReason = AgentTerminateMode.MAX_TURNS;
}
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This new logic block is a great improvement for handling the max_turns limit, as it ensures the final turn's tool calls are executed. However, it introduces a side effect by conflicting with the existing generic recovery mechanism.

When this block sets terminateReason = AgentTerminateMode.MAX_TURNS and breaks the loop, the code proceeds to the unified recovery block at line 472. This causes executeFinalWarningTurn to run again, resulting in a redundant recovery attempt and an extra, unexpected turn for the agent.

To fix this, the unified recovery block should be modified to exclude the MAX_TURNS case, since it's now handled here. Please update the condition at line 472 to prevent this redundant execution:

// In file packages/core/src/agents/executor.ts at line 472

      if (
        terminateReason !== AgentTerminateMode.ERROR &&
        terminateReason !== AgentTerminateMode.ABORTED &&
        terminateReason !== AgentTerminateMode.GOAL &&
        terminateReason !== AgentTerminateMode.MAX_TURNS
      ) {

@Amanda111 Amanda111 changed the title fix: Tool call was added to history without being executed when hitting SubAgent max turns fix: Tool call was added to history without being executed when hitting SubAgent max turn Dec 8, 2025
@Amanda111
Copy link
Author

@silviojr

@gemini-cli gemini-cli bot added status/need-issue Pull requests that need to have an associated issue. priority/p1 Important and should be addressed in the near term. area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality and removed status/need-issue Pull requests that need to have an associated issue. labels Jan 7, 2026
@bdmorgan
Copy link
Collaborator

Hi @Amanda111, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this.

We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines.

Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed.

Thank you for your understanding and for being a part of our community!

@gemini-cli
Copy link
Contributor

gemini-cli bot commented Jan 24, 2026

Hi there! Thank you for your contribution to Gemini CLI.

To improve our contribution process and better track changes, we now require all pull requests to be associated with an existing issue, as announced in our recent discussion and as detailed in our CONTRIBUTING.md.

This pull request is being closed because it is not currently linked to an issue. You can easily reopen this PR once you have linked it to an issue.

How to link an issue:
Add a keyword followed by the issue number (e.g., Fixes #123) in the description of your pull request. For more details, see the GitHub Documentation.

Thank you for your understanding and for being a part of our community!

@gemini-cli gemini-cli bot closed this Jan 24, 2026
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 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