Skip to content

Conversation

@hannesrudolph
Copy link
Collaborator

@hannesrudolph hannesrudolph commented Jan 14, 2026

Closes #10711

Summary

When switching modes, Gemini needs to see all tool definitions to understand conversation history, but should only be allowed to call tools permitted in the current mode. This PR implements the allowedFunctionNames feature for the Gemini provider to prevent model errors when switching modes.

Problem

When a user switches from one mode (e.g., "code") to another (e.g., "architect"), the conversation history may contain tool calls from the previous mode. Gemini requires the tool definitions to be present to understand the history, but the current mode may not have access to those tools. This causes errors in the Gemini API.

Solution

  • Add allowedFunctionNames support to the Gemini provider via toolConfig.functionCallingConfig.allowedFunctionNames
  • Pass ALL tool definitions to Gemini (for history compatibility) while restricting callable tools
  • Only apply this behavior to Gemini (other providers continue using filtered tools as before)

Changes

  • src/api/index.ts: Added allowedFunctionNames?: string[] to ApiHandlerCreateMessageMetadata interface
  • src/api/providers/gemini.ts: Implemented toolConfig.functionCallingConfig.allowedFunctionNames when provided
  • src/core/task/build-tools.ts: Added buildNativeToolsArrayWithRestrictions function with includeAllToolsWithRestrictions option
  • src/core/task/Task.ts: Added provider check (apiProvider === "gemini") to only use the new behavior for Gemini
  • src/api/providers/__tests__/gemini-handler.spec.ts: Added 5 comprehensive tests for the new functionality

Provider Support Research

  • Gemini: ✅ Supports allowedFunctionNames via toolConfig.functionCallingConfig
  • OpenAI: Has allowed_tools via tool_choice.type = "allowed_tools" (not yet implemented)
  • Anthropic: ❌ Does not support this feature - only has auto, any, tool (single), none

Test Results

All 5231 tests pass across 376 test files.


Important

Adds allowedFunctionNames to Gemini provider to restrict tool usage based on mode while maintaining historical context.

  • Behavior:
    • Implements allowedFunctionNames in src/api/providers/gemini.ts to restrict tool usage based on mode.
    • Passes all tool definitions to Gemini for historical context but restricts callable tools.
    • Only affects Gemini; other providers remain unchanged.
  • API Changes:
    • Adds allowedFunctionNames?: string[] to ApiHandlerCreateMessageMetadata in src/api/index.ts.
  • Tool Building:
    • Adds buildNativeToolsArrayWithRestrictions in src/core/task/build-tools.ts to support tool restrictions.
  • Testing:
    • Adds tests in src/api/providers/__tests__/gemini-handler.spec.ts for allowedFunctionNames functionality.

This description was created by Ellipsis for f53e096. You can customize this summary. It will automatically update as commits are pushed.

… errors

When switching modes, Gemini needs to see all tool definitions to understand
conversation history, but should only be allowed to call tools permitted in
the current mode. This implements the allowedFunctionNames feature:

- Add allowedFunctionNames to ApiHandlerCreateMessageMetadata interface
- Implement toolConfig.functionCallingConfig.allowedFunctionNames in Gemini provider
- Add buildNativeToolsArrayWithRestrictions to return all tools with restrictions
- Only apply to Gemini provider (other providers continue using filtered tools)
- Add comprehensive tests for the new functionality
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. Enhancement New feature or request labels Jan 14, 2026
@roomote
Copy link
Contributor

roomote bot commented Jan 14, 2026

Oroocle Clock   See task on Roo Cloud

Re-review complete. No new issues found since the alias/canonical consistency fix.

  • Ensure allowedFunctionNames and declared tool names stay consistent when tool aliases/renames are involved (Gemini needs allowed names to exist in functionDeclarations).
Previous reviews

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@roomote
Copy link
Contributor

roomote bot commented Jan 14, 2026

Fixaroo Clock   See task on Roo Cloud

Fixed the reported issue by resolving alias names to canonical names in allowedFunctionNames to ensure consistency with functionDeclarations. All local checks passed.

View commit | Revert commit

…nNames

When tools are renamed via aliasRenames (e.g., write_to_file -> write_file),
the allowedFunctionNames array would contain alias names that do not exist
in functionDeclarations (which uses canonical names). This caused Gemini to
error because names in allowedFunctionNames must exist in functionDeclarations.

Fixed by applying resolveToolAlias when extracting allowedFunctionNames to
ensure alias names are resolved back to their canonical names, maintaining
consistency with functionDeclarations.

Added tests for resolveToolAlias function to verify alias resolution behavior.
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 14, 2026
@mrubens mrubens merged commit 9b1c850 into main Jan 14, 2026
12 checks passed
@mrubens mrubens deleted the gemini-tool-fix branch January 14, 2026 04:49
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jan 14, 2026
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement New feature or request lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Gemini API errors when switching modes due to missing tool definitions

4 participants