Skip to content

feat(core): implement tool repair and length continuation auto-recovery#24978

Open
achernez wants to merge 2 commits intogoogle-gemini:mainfrom
achernez:feat/core-auto-repair-continuation
Open

feat(core): implement tool repair and length continuation auto-recovery#24978
achernez wants to merge 2 commits intogoogle-gemini:mainfrom
achernez:feat/core-auto-repair-continuation

Conversation

@achernez
Copy link
Copy Markdown

@achernez achernez commented Apr 8, 2026

Summary

This PR implements two reliability features into @google/gemini-cli-core to improve agent robustness:

1. Tool Hallucination Auto-Repair (Fuzzy Matching)

  • Kebab-to-Snake Normalization: Automatically converts tool names from kebab-case (read-file) to snake_case (read_file).
  • Levenshtein Distance: Attempts to repair hallucinated tool names using a strict threshold (Max 2 edits).
  • Ambiguity Check: Aborts repair if multiple valid tools are equally close to the hallucination.
  • Implementation: Logic isolated in packages/core/src/utils/fuzzy-matcher.ts and integrated into the Scheduler.

2. Length Continuation

  • Auto-Recovery: Detects when a model response is truncated due to length (finishReason: 'MAX_TOKENS').
  • System Injection: Automatically prompts the model to continue where it left off.
  • Safety Cap: Limits continuations to 3 iterations per turn to prevent token drain.
  • Implementation: Handled in the LegacyAgentSession run loop.

Verification

  • Added unit tests for fuzzy-matcher.ts.
  • Added integrated tests in scheduler.test.ts for tool repair logic and ambiguity handling.
  • Added integrated tests in legacy-agent-session.test.ts for length continuation and iteration limits.
  • Verified with npm run lint and npm run build.
  • Fixed a bug in scripts/build.js that caused build failures in some Linux environments.

Fixes #24977

@achernez achernez requested a review from a team as a code owner April 8, 2026 22:06
@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 introduces two key robustness features to the agent core: automatic tool name repair for handling minor model hallucinations and seamless response continuation when output is truncated due to token limits. These changes improve the overall reliability of agent interactions and include comprehensive unit and integration testing. Additionally, a build script modification ensures better compatibility across different Linux environments.

Highlights

  • Tool Hallucination Auto-Repair: Implemented fuzzy matching and kebab-to-snake case normalization to automatically repair hallucinated tool names, improving agent reliability.
  • Length Continuation: Added automatic response continuation when the model hits token limits, capped at 3 iterations per turn to prevent infinite loops.
  • Build Script Fix: Updated the build script to resolve issues in certain Linux environments by manually parsing workspaces instead of relying on 'npm query'.
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.

@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 8, 2026

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.

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 introduces length continuation for agent sessions when a response is truncated due to MAX_TOKENS, adds tool name repair capabilities (normalization and fuzzy matching) to the scheduler, and updates the build script to dynamically detect workspaces. I have provided feedback regarding the naming consistency of the new usage property and suggested refactoring the duplicated tool repair logic into a helper method.

outputTokens?: number;
cachedTokens?: number;
/** Whether this usage report is for a continued response. */
is_continuation?: boolean;
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

The property is_continuation uses snake_case, which is inconsistent with the camelCase naming convention used for other properties in the Usage interface (e.g., inputTokens, outputTokens, cachedTokens). It also conflicts with the isContinuation property added to AgentEventCommon on line 83 of this same file. Please rename it to isContinuation to maintain consistency and adhere to TypeScript naming standards.

Suggested change
is_continuation?: boolean;
isContinuation?: boolean;

Comment on lines +325 to +359
if (!tool) {
// Attempt normalization: kebab-case to snake_case
const normalizedName = normalizeToolName(request.name);
if (normalizedName !== request.name) {
tool = toolRegistry.getTool(normalizedName);
if (tool) {
debugLogger.log(
`Repaired tool name: ${request.name} -> ${normalizedName} (via normalization)`,
);
enrichedRequest.name = normalizedName;
enrichedRequest.originalRequestName =
enrichedRequest.originalRequestName || request.name;
}
}
}

if (!tool) {
// Attempt fuzzy matching: Levenshtein distance <= 2
const fuzzyResult = getClosestMatch(
request.name,
toolRegistry.getAllToolNames(),
2,
);
if (fuzzyResult.repairedName && !fuzzyResult.isAmbiguous) {
tool = toolRegistry.getTool(fuzzyResult.repairedName);
if (tool) {
debugLogger.log(
`Repaired tool name: ${request.name} -> ${fuzzyResult.repairedName} (via fuzzy matching, distance: ${fuzzyResult.distance})`,
);
enrichedRequest.name = fuzzyResult.repairedName;
enrichedRequest.originalRequestName =
enrichedRequest.originalRequestName || request.name;
}
}
}
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

The tool name repair logic (normalization and fuzzy matching) is duplicated here and in the _execute method (lines 811-841). This duplication increases the risk of inconsistent behavior if the repair logic needs to be updated in the future. Please refactor this logic into a private helper method (e.g., _tryRepairToolName) that can be called from both locations.

@gemini-cli gemini-cli bot added the area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality label Apr 8, 2026
@achernez
Copy link
Copy Markdown
Author

achernez commented Apr 8, 2026

@googlebot I signed it!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Core Reliability: Tool Name Fuzzy Matching and Response Length Continuation

1 participant