Skip to content

bug(antigravity/claude): parse <thinking> XML tags into thought blocks#1867

Open
lang-911 wants to merge 10 commits intorouter-for-me:mainfrom
lang-911:fix/1777-antigravity-thinking-tag-parser
Open

bug(antigravity/claude): parse <thinking> XML tags into thought blocks#1867
lang-911 wants to merge 10 commits intorouter-for-me:mainfrom
lang-911:fix/1777-antigravity-thinking-tag-parser

Conversation

@lang-911
Copy link

@lang-911 lang-911 commented Mar 5, 2026

Closes #1777

Summary

  • add Antigravity Claude thinking-tag parser for <thinking>...</thinking> text blocks
  • apply parser in streaming and stream-to-nonstream execution paths
  • add comprehensive parser tests (including split-tag stream chunks)

Validation

  • go test ./internal/runtime/executor -run ThinkingTagParser -count=1
  • go test ./...

@gemini-code-assist
Copy link
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 addresses a bug where <thinking> XML tags from Claude models were not being properly parsed and handled. It introduces a new parser specifically designed to extract content within these tags and mark it as 'thought' blocks, improving the accuracy and utility of responses from Claude models in both real-time streaming and batch processing scenarios.

Highlights

  • New Thinking Tag Parser: Introduced a dedicated ThinkingTagParser to correctly identify and process <thinking>...</thinking> XML tags from Antigravity Claude model responses, marking the enclosed content as 'thought' blocks.
  • Integration into Execution Paths: The new parser has been integrated into both the streaming and stream-to-nonstream conversion logic within the AntigravityExecutor to ensure consistent handling of thinking tags across different response types.
  • Comprehensive Testing: Added extensive unit tests for the ThinkingTagParser, covering various edge cases such as tags split across stream chunks, partial tags, unicode-escaped tags, and multiple/consecutive thinking blocks.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • internal/runtime/executor/antigravity_executor.go
    • Instantiated NewThinkingTagParser for streaming responses, passing the base model name.
    • Applied thinkingParser.Process to the payload in the streaming loop.
    • Retrieved streamModelName to initialize ThinkingTagParser for stream-to-nonstream conversion.
    • Applied thinkingParser.Process to each line in the stream-to-nonstream conversion.
  • internal/runtime/executor/thinking_tag_parser.go
    • Added a new file defining the ThinkingTagParser struct and its methods.
    • Implemented NewThinkingTagParser to activate parsing only for Claude models.
    • Developed Process method to parse response.candidates.0.content.parts and identify thinking blocks.
    • Created splitThinkingText to handle tag detection, including partial tags and buffering across chunks.
    • Included splitTrailingPartialTag helper function for robust partial tag handling.
  • internal/runtime/executor/thinking_tag_parser_test.go
    • Added a new test file for ThinkingTagParser.
    • Included test cases for non-Claude models (passthrough).
    • Covered scenarios with no thinking tags, simple thinking blocks, and tags split across chunks.
    • Tested open/close tag splits, thinking followed by regular text, and function calls.
    • Validated empty thinking blocks, partial tag flushing, unicode-escaped tags, and multiple/consecutive thinking blocks.
    • Ensured correct behavior with realistic log replays and buffer management.
Activity
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 introduces a new parser for <thinking> tags in Antigravity Claude responses, which is a great feature. The implementation is solid, with comprehensive tests covering various scenarios, including streaming and split tags. I've identified a critical issue related to double-processing of payloads which should be addressed. Additionally, I have a couple of suggestions for the new thinking_tag_parser.go file to improve maintainability and robustness. Overall, good work on this feature.

@lang-911
Copy link
Author

lang-911 commented Mar 5, 2026

/gemini review

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 introduces a robust ThinkingTagParser to correctly parse <thinking> XML tags within streaming responses from Claude models. The implementation effectively handles partial tags across stream chunks, which is crucial for streaming data processing. The new thinking_tag_parser.go file is well-structured, and its integration into antigravity_executor.go is clean and appropriate. The accompanying test suite (thinking_tag_parser_test.go) is comprehensive, covering a wide range of scenarios, including edge cases like split tags, empty blocks, and unicode-escaped tags, which significantly enhances the reliability of this new feature. Overall, this is a well-engineered and thoroughly tested addition.

Copy link

@JasperDale420 JasperDale420 left a comment

Choose a reason for hiding this comment

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

Not merging this yet. The existing review on the PR already flags a critical double-processing risk in the new thinking-tag path, and I do not want to ship the executor changes until that issue is resolved and the branch is re-reviewed.

Copy link
Collaborator

@luispater luispater left a comment

Choose a reason for hiding this comment

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

Thanks — the latest commit fixes the earlier double-processing concern, and the parser coverage is strong. I still see one blocking issue in the split path: when a single text part is split into multiple segments, the parser rebuilds fresh part objects with only text/thought and drops original metadata. That is risky for Claude because the downstream Antigravity->Claude response translator relies on thoughtSignature to cache/replay signed thinking blocks in later turns. If a tagged text part also carries thoughtSignature, the current rewrite loses it before translation. Please preserve relevant metadata on the resulting thought segment(s) — at minimum thoughtSignature — and add a regression test for a tagged text part that also includes a signature.

lang-911 and others added 3 commits March 7, 2026 09:21
Closes router-for-me#1777

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Remove double thinkingParser processing from convertStreamToNonStream
- Generate partial tag lists programmatically via generatePartials()
- Handle sjson.Set errors with logging instead of silent discard

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lang-911 lang-911 force-pushed the fix/1777-antigravity-thinking-tag-parser branch from 54ad2e8 to d373d79 Compare March 8, 2026 06:20
@lang-911
Copy link
Author

lang-911 commented Mar 8, 2026

Updated in d373d79.

Addressed the latest blocking review by preserving thought signatures when tag parsing rewrites a text part:

  • split/single-segment tagged thought text now moves the signature onto a dedicated empty thought part immediately after the final derived thought segment
  • visible text segments drop thought/signature metadata
  • close-tag-only chunks still preserve the signature so the prior thinking block can be closed/cached correctly

Added regressions for the split tagged-part case, the single-segment case, the close-tag-only case, and a Claude translator contract test for signature caching.

Validation:

  • go test ./internal/runtime/executor -run 'ThinkingTagParser' -count=1
  • go test ./internal/runtime/executor -count=1
  • go test ./internal/translator/antigravity/claude -count=1

@lang-911
Copy link
Author

lang-911 commented Mar 8, 2026

Follow-up in 0a83ee0: the failed translator-path-guard check was caused by my earlier regression test addition under internal/translator, not by the parser fix itself. I moved that contract coverage into internal/runtime/executor/thinking_tag_parser_test.go and restored internal/translator/antigravity/claude/antigravity_claude_response_test.go to its original content so the PR no longer changes restricted translator paths.

Revalidated:

  • go test ./internal/runtime/executor -run 'ThinkingTagParser' -count=1
  • go test ./internal/runtime/executor -count=1

@lang-911
Copy link
Author

lang-911 commented Mar 8, 2026

/gemini review

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 introduces a robust parser for <thinking> XML tags in Antigravity Claude responses, correctly handling streaming and chunked data. The new ThinkingTagParser is stateful and manages partial tags across stream chunks effectively. The integration into the AntigravityExecutor for both streaming and non-streaming paths is well-implemented. The addition of a comprehensive test suite provides strong validation for the new functionality. My review includes a few minor suggestions to improve code clarity and conciseness by removing redundant code and simplifying some return statements.

@lang-911
Copy link
Author

lang-911 commented Mar 8, 2026

Addressed the latest gemini-code-assist cleanup findings in thinking_tag_parser.go:

  • simplified the final sjson.Delete(...) return in rewriteThinkingSegmentPart
  • simplified the final sjson.Set(...) return in buildThoughtSignaturePart
  • removed the dead maxTagBufferLen guard and constant

Validation:

  • go test ./internal/runtime/executor -run 'ThinkingTagParser' -count=1
  • go test ./internal/runtime/executor -count=1

/gemini review

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 introduces a ThinkingTagParser to handle <thinking> tags in responses from Antigravity Claude models. The parser is stateful to correctly process streaming responses where tags might be split across multiple chunks. The implementation is robust, with comprehensive unit tests covering various edge cases.

My review focuses on the new parser implementation. I've identified opportunities to improve performance and readability in two helper functions by reducing redundant JSON processing. The core parsing logic appears sound and well-tested.

@lang-911
Copy link
Author

lang-911 commented Mar 8, 2026

Addressed the remaining gemini-code-assist findings in thinking_tag_parser.go:

  • refactored rewriteThinkingSegmentPart to use a single encoding/json decode/edit/encode pass with map[string]json.RawMessage
  • refactored buildThoughtSignaturePart to build the signature-only part with one marshal step
  • added a regression that verifies split tagged parts preserve nested raw metadata and large integer metadata without generic normalization

Validation:

  • go test ./internal/runtime/executor -run 'ThinkingTagParser' -count=1
  • go test ./internal/runtime/executor -count=1

/gemini review

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 introduces a robust parser for <thinking> tags in Antigravity Claude responses, correctly handling streaming and non-streaming scenarios. The implementation is well-structured and includes a comprehensive test suite that covers numerous edge cases. The integration into the existing executor is clean. I have one suggestion to simplify a helper function for better readability and minor performance improvement.

@lang-911
Copy link
Author

lang-911 commented Mar 8, 2026

Addressed the latest gemini-code-assist helper cleanup in thinking_tag_parser.go:

  • simplified buildThoughtSignaturePart to start from a static base JSON string and apply a single sjson.Set(...)
  • removed the temporary struct and json.Marshal path for that helper

Validation:

  • go test ./internal/runtime/executor -run 'ThinkingTagParser' -count=1
  • go test ./internal/runtime/executor -count=1

/gemini review

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 introduces a robust parser for <thinking> tags in Antigravity Claude model responses, handling streaming and partial tags effectively. The implementation is well-structured and accompanied by a comprehensive set of unit tests that cover numerous edge cases, which is great to see. I have one suggestion to improve the performance and consistency of JSON manipulation in the new parser.

@lang-911
Copy link
Author

lang-911 commented Mar 8, 2026

Addressed the final gemini-code-assist finding in thinking_tag_parser.go:

  • switched rewriteThinkingSegmentPart back to sjson.Set / sjson.Delete for the segment rewrite path
  • removed the now-unused encoding/json import from that file
  • kept the existing signed-thinking and raw-metadata regressions unchanged as guardrails

Validation:

  • go test ./internal/runtime/executor -run 'ThinkingTagParser' -count=1
  • go test ./internal/runtime/executor -count=1

All Gemini review threads on this PR are now resolved.

/gemini review

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 introduces a parser for <thinking> tags in Antigravity Claude responses, which is a great enhancement for handling model thoughts. The implementation includes a new stateful ThinkingTagParser that correctly processes streaming data, including tags split across multiple chunks. The logic is applied to both streaming and stream-to-non-stream execution paths in the Antigravity executor. The addition of a comprehensive test suite covering numerous edge cases is excellent and provides high confidence in the correctness of the new parser. The code is well-structured. I have one minor suggestion to improve code idiom.

@lang-911
Copy link
Author

lang-911 commented Mar 8, 2026

Addressed the final gemini-code-assist readability suggestion in thinking_tag_parser.go:

  • replaced the manual append + copy + index assignment insertion with the idiomatic nested append(...) form
  • left the insertAt calculation and signature-placement behavior unchanged
  • kept the existing signed-thinking and raw-metadata regressions unchanged as guardrails

Validation:

  • go test ./internal/runtime/executor -run 'ThinkingTagParser' -count=1
  • go test ./internal/runtime/executor -count=1

@gemini-code-assist
Copy link
Contributor

Thank you for the diligent updates, lang-911! I appreciate you addressing the readability suggestion in thinking_tag_parser.go by replacing the manual append + copy + index assignment with the idiomatic nested append(...) form. The continued validation with the provided tests is also excellent. This pull request looks good to go now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(antigravity/claude): Thinking blocks returned as raw <thinking> XML tags instead of proper thinking content blocks

3 participants