Skip to content

fix(agent): count repeated tool calls by arguments#7786

Open
RhoninSeiei wants to merge 2 commits intoAstrBotDevs:masterfrom
RhoninSeiei:codex/7784-tool-call-streak-args-clean
Open

fix(agent): count repeated tool calls by arguments#7786
RhoninSeiei wants to merge 2 commits intoAstrBotDevs:masterfrom
RhoninSeiei:codex/7784-tool-call-streak-args-clean

Conversation

@RhoninSeiei
Copy link
Copy Markdown
Contributor

@RhoninSeiei RhoninSeiei commented Apr 24, 2026

背景

修复 #7784 中提到的问题:连续工具调用提示目前只按工具名称累计。在浏览器批量操作等场景中,连续调用同一个工具但参数持续变化时,系统仍会提示“重复调用过高”,容易干扰正常任务。

修改内容

本次修改将连续调用计数依据从单一工具名扩展为“工具名 + 参数指纹”:

  • 使用 json.dumps(..., sort_keys=True) 生成稳定的参数指纹。
  • 参数不可 JSON 序列化时回退到 repr(),避免统计逻辑影响工具执行。
  • 只有工具名和参数指纹都相同的连续调用才累计次数。
  • 现有提示阈值和提示文本保持不变。

验证

  • 已检查提交差异,仅修改 astrbot/core/agent/runners/tool_loop_agent_runner.py

Fixes #7784

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

Bug Fixes:

  • Prevent over-counting repeated tool calls by distinguishing consecutive invocations with different arguments in the streak calculation.

@auto-assign auto-assign Bot requested review from Fridemn and advent259141 April 24, 2026 22:00
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Apr 24, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

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 new tool_args: dict[str, T.Any] assumption in _tool_call_streak_key / _track_tool_call_streak may be too strict for func_tool_args; if callers can pass non-dict structures (e.g., lists or None), consider typing and handling a broader input type to avoid runtime errors.
  • The JSON-based fingerprint can become very large for tools with big argument payloads; consider truncating or hashing the serialized args (e.g., using a stable hash of the JSON string) to keep the streak key small and avoid storing or comparing huge strings repeatedly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `tool_args: dict[str, T.Any]` assumption in `_tool_call_streak_key` / `_track_tool_call_streak` may be too strict for `func_tool_args`; if callers can pass non-dict structures (e.g., lists or `None`), consider typing and handling a broader input type to avoid runtime errors.
- The JSON-based fingerprint can become very large for tools with big argument payloads; consider truncating or hashing the serialized args (e.g., using a stable hash of the JSON string) to keep the streak key small and avoid storing or comparing huge strings repeatedly.

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.

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 updates the tool call streak tracking logic in tool_loop_agent_runner.py. The tracking mechanism has been refined to consider both the tool name and its arguments by introducing a fingerprinting method, _tool_call_streak_key, which uses JSON serialization to create a unique key for each call. Consequently, the _track_tool_call_streak method and its call sites have been updated to handle these argument-aware keys. I have no feedback to provide.

Copy link
Copy Markdown
Contributor Author

补充更新:根据首轮 Unit Tests 失败结果,已补充测试提交 0e64bd3

测试调整内容:保留“同名且同参数连续调用会触发递进提示”的断言,并新增“同名但参数变化时重置连续计数”的断言。当前 head 的 Code Format Check、Unit Tests、CodeQL、Smoke Test、AstrBot Dashboard CI 均已通过。

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

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 连续工具调用提示只按工具名累计,参数变化时仍误判为重复调用

1 participant