Skip to content

Conversation

@roomote
Copy link

@roomote roomote bot commented Oct 29, 2025

Description

This PR addresses Issue #8914 where Gemini's grounding feature was incorrectly removing array indices like [0] from generated code, interpreting them as citation markers.

Problem

When Gemini's grounding feature is enabled, it misinterprets bracketed numbers in code (e.g., fruits[0]) as citation markers and removes them. This causes generated code to be syntactically incorrect.

Solution

Added intelligent detection of code generation contexts to temporarily disable grounding when:

  • User asks to create/write/generate code files
  • User mentions programming languages or code snippets
  • Array index patterns are detected in the conversation

Changes

  • Added isCodeGenerationContext() method to detect code-related prompts
  • Modified createMessage() and completePrompt() to conditionally disable grounding
  • Added comprehensive test coverage for the grounding bypass logic

Testing

  • ✅ Added 3 new test cases covering various scenarios
  • ✅ All existing tests pass
  • ✅ Verified pattern matching with multiple code-related keywords

Notes

This is a workaround for what appears to be a limitation in Google's Gemini API grounding feature. The grounding feature interprets bracketed numbers as citations, which is problematic for code generation. This fix ensures that grounding is only disabled when necessary, maintaining its benefits for non-code contexts.

Fixes #8914

cc @sergedc - This PR implements a targeted fix that detects code generation contexts and temporarily disables grounding to preserve array indices. The grounding feature will still work normally for non-code queries.


Important

Disable Gemini grounding for code generation contexts to preserve array indices, with detection logic and tests added.

  • Behavior:
    • Added isCodeGenerationContext() in gemini.ts to detect code generation contexts and disable grounding.
    • Modified createMessage() and completePrompt() in gemini.ts to conditionally disable grounding based on code context.
  • Testing:
    • Added tests in gemini.spec.ts to verify grounding is disabled in code contexts and enabled otherwise.
    • Tests cover various code patterns and non-code scenarios.

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

…dex removal

When Gemini grounding is enabled, it misinterprets array indices like [0] as citation markers and removes them from generated code. This fix detects code generation contexts and temporarily disables grounding to preserve array indices.

Fixes #8914
@roomote roomote bot requested review from cte, jr and mrubens as code owners October 29, 2025 20:35
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Oct 29, 2025
@roomote
Copy link
Author

roomote bot commented Oct 29, 2025

Review Summary

I've reviewed this PR and identified 2 issues that should be addressed:

  • Line 83: The pattern "\\[\\d+\\]" is overly broad and will incorrectly match citation-style references in regular text (e.g., "see reference [1]"), causing grounding to be disabled for non-code contexts
  • Line 86: The pattern "fruits\\[0\\]" is redundant since it's already covered by the more general "\\[\\d+\\]" pattern on line 83

View Job

"def\\s+\\w+\\s*\\(", // Python function definition
"function\\s+\\w+\\s*\\(", // JavaScript function
"class\\s+\\w+", // Class definition
"\\[\\d+\\]", // Array index patterns
Copy link
Author

Choose a reason for hiding this comment

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

This pattern is overly broad and will incorrectly match citation-style references in regular text (e.g., "see reference [1]" or "table [2] shows results"), causing grounding to be disabled for non-code contexts where users discuss numbered references or citations. Consider removing this pattern or making it more specific to code contexts by requiring surrounding code syntax.

"\\[\\d+\\]", // Array index patterns
"array\\[",
"list\\[",
"fruits\\[0\\]", // Specific to the reported issue
Copy link
Author

Choose a reason for hiding this comment

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

This pattern is redundant as it's already covered by the more general "\\[\\d+\\]" pattern on line 83. The broader pattern will match fruits[0] along with any other array index. Recommend removing this specific pattern to reduce code duplication.

Copy link
Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

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

Review complete. Found 2 issues that should be addressed before approval.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Oct 29, 2025
@daniel-lxs
Copy link
Member

#8914 (comment)

@daniel-lxs daniel-lxs closed this Oct 29, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Oct 29, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] Gemini not outputting [0]

4 participants