Skip to content

fix(agent): preserve tool metadata after skills-like requery#9220

Open
Last-emo-boy wants to merge 2 commits into
AstrBotDevs:masterfrom
Last-emo-boy:fix/9217-preserve-requery-tool-metadata
Open

fix(agent): preserve tool metadata after skills-like requery#9220
Last-emo-boy wants to merge 2 commits into
AstrBotDevs:masterfrom
Last-emo-boy:fix/9217-preserve-requery-tool-metadata

Conversation

@Last-emo-boy

@Last-emo-boy Last-emo-boy commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #9217.

Gemini returns a thought signature with the function call produced by a
skills_like parameter re-query. The runner replaced the selected tool call's
name, arguments, and ID with the re-query result, but kept the original
provider metadata map. When the call ID changed, the re-query signature was
therefore absent from the assistant history, and Gemini rejected the next turn.

This change follows Google's
thought-signature guidance,
which requires model-generated function-call signatures to be returned
unchanged in subsequent turns.

Modifications / 改动点

  • Keep tools_call_extra_content synchronized with the name, arguments, and ID
    returned by a skills_like re-query.

  • Preserve the real provider metadata associated with the final function call
    instead of retaining metadata from the initial tool-selection response.

  • Add regression coverage using distinct initial and re-query call IDs and
    signatures, verifying that the recorded assistant history contains the
    re-query signature.

  • Leave non-skills_like execution, Gemini serialization, shared message
    models, configuration, and dependencies unchanged.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

python -m pytest -q \
  tests/test_tool_loop_agent_runner.py \
  tests/test_gemini_source.py

34 passed
ruff format --check .
480 files already formatted
ruff check .
All checks passed!
git diff --check
Passed

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Ensure skills-like tool re-queries correctly preserve provider metadata and token usage accounting for Gemini tool calls.

Bug Fixes:

  • Preserve tools_call_extra_content when replacing a skills-like tool selection with a re-query response so the final function-call signature is recorded in assistant history.

Enhancements:

  • Track and aggregate token usage from skills-like requery and repair provider calls into runner stats and per-conversation usage without double-counting non-requery tool selections.

Tests:

  • Add regression tests covering skills-like requery metadata preservation, usage accumulation across extra provider requests, and avoiding double-counting when no schema match occurs.

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request ensures that tools_call_extra_content is preserved during skills-like re-queries in tool_loop_agent_runner.py and updates the corresponding tests to verify this behavior. The review feedback points out that the token usage from the re-query response (requery_resp) is currently not accumulated, which leads to inaccurate token usage reporting, and suggests adding code to track it.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +874 to +876
llm_resp.tools_call_extra_content = (
requery_resp.tools_call_extra_content
)

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.

medium

The token usage of the skills-like re-query response (requery_resp) is currently not tracked or accumulated into the agent's total token usage statistics (self.stats.token_usage) or the conversation's token usage (self.req.conversation.token_usage). This leads to inaccurate token usage reporting when the skills-like mode is active.

We should accumulate the token usage from requery_resp when it is available.

                    llm_resp.tools_call_extra_content = (
                        requery_resp.tools_call_extra_content
                    )
                    if requery_resp.usage:
                        self.stats.token_usage += requery_resp.usage
                        if self.req.conversation:
                            self.req.conversation.token_usage += requery_resp.usage.total

@Last-emo-boy Last-emo-boy marked this pull request as ready for review July 12, 2026 10:51
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 12, 2026

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've left some high level feedback:

  • The usage accounting logic for requery_resp and repair_resp in _resolve_tool_exec is duplicated; consider extracting a small helper (e.g., _add_usage(usage)) to centralize the self.stats.token_usage and conversation.token_usage updates and avoid divergence if the accounting rules change.
  • You currently update llm_resp.tools_call_extra_content in step() when a skills_like re-query happens and also reassign llm_resp from requery_resp/repair_resp inside _resolve_tool_exec; it might be clearer to keep all response-normalization (including tool call and extra content synchronization) in one place to reduce the chance of future inconsistencies between the two paths.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The usage accounting logic for `requery_resp` and `repair_resp` in `_resolve_tool_exec` is duplicated; consider extracting a small helper (e.g., `_add_usage(usage)`) to centralize the `self.stats.token_usage` and `conversation.token_usage` updates and avoid divergence if the accounting rules change.
- You currently update `llm_resp.tools_call_extra_content` in `step()` when a `skills_like` re-query happens and also reassign `llm_resp` from `requery_resp`/`repair_resp` inside `_resolve_tool_exec`; it might be clearer to keep all response-normalization (including tool call and extra content synchronization) in one place to reduce the chance of future inconsistencies between the two paths.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Gemini API 在工具调用时遇到丢失思考签名错误

1 participant