Skip to content

fix: disabled web search tools still being injected into LLM requests#6526

Closed
he-yufeng wants to merge 1 commit intoAstrBotDevs:masterfrom
he-yufeng:fix/respect-tool-active-in-web-search
Closed

fix: disabled web search tools still being injected into LLM requests#6526
he-yufeng wants to merge 1 commit intoAstrBotDevs:masterfrom
he-yufeng:fix/respect-tool-active-in-web-search

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

@he-yufeng he-yufeng commented Mar 17, 2026

Problem

When a user deactivates web_search_tavily (or any other web search tool) through the admin behavior config panel, the tool still gets added to LLM requests. The model can then call the "disabled" tool as if nothing happened.

Root cause: edit_web_search_tools filter fetches tools via func_tool_mgr.get_func() which returns the tool object regardless of its active status, then calls tool_set.add_tool() unconditionally. This bypasses the admin deactivation that sets tool.active = False.

Fix

Add an active check before adding any web search tool. Extracted a _try_add helper that wraps the get-and-check pattern to keep the provider branches clean:

def _try_add(tool_name: str) -> None:
    t = func_tool_mgr.get_func(tool_name)
    if t and t.active:
        tool_set.add_tool(t)

This is consistent with how astr_main_agent.py already filters inactive tools during persona setup (line 362: if not tool.active: persona_toolset.remove_tool(tool.name)).

All four provider branches (default, tavily, baidu_ai_search, bocha) are covered.

How to test

  1. Set web search provider to tavily and enable web search
  2. Deactivate web_search_tavily in admin behavior config
  3. Send a message that would trigger web search
  4. Model should NOT attempt to call web_search_tavily

Fixes #6506

Summary by Sourcery

Ensure only active web search tools are injected into LLM toolsets based on the selected provider.

Bug Fixes:

  • Prevent deactivated web search tools from being added to LLM requests when the provider is changed in the admin behavior config.

Enhancements:

  • Introduce a helper to conditionally add web search tools and apply consistent active-status checks across all web search providers.

edit_web_search_tools filter was fetching tools via get_func() and
adding them to the tool set without checking their active status.
Tools deactivated by the admin would get re-added on every request.

Extract a _try_add helper that checks tool.active before adding,
consistent with how persona setup already filters inactive tools.

Fixes AstrBotDevs#6506
@auto-assign auto-assign bot requested review from Fridemn and anka-afk March 17, 2026 14:09
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Mar 17, 2026
@gemini-code-assist
Copy link
Copy Markdown
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 critical issue where web search tools, despite being deactivated by an administrator, were still being made available to LLM requests. The changes ensure that the system correctly respects the active status of tools, preventing unintended functionality and improving administrative control over tool availability. The refactoring introduces a helper function to streamline the conditional addition of tools.

Highlights

  • Problem Identified: Disabled web search tools were still being injected into LLM requests, allowing the model to call tools that an administrator had deactivated.
  • Root Cause: The edit_web_search_tools filter fetched tool objects via func_tool_mgr.get_func() which does not check the tool's active status, and then unconditionally added them to the toolset.
  • Solution Implemented: An active check was added before any web search tool is added to the LLM requests. This logic was encapsulated in a new _try_add helper function for cleaner code and consistent application across all web search providers.
  • Consistency: The fix aligns with existing logic in astr_main_agent.py where inactive tools are already filtered during persona setup.
Changelog
  • astrbot/builtin_stars/web_searcher/main.py
    • Introduced a _try_add helper function to encapsulate the logic for adding tools only if they are active.
    • Refactored tool addition for 'default' and 'tavily' providers to utilize the new _try_add helper.
    • Added an explicit active check for the 'AIsearch' tool before adding it to the toolset.
    • Updated the 'bocha' provider's tool addition to use the _try_add helper.
Activity
  • No human activity (comments, reviews, etc.) has been recorded on this pull request yet.
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
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:

  • Consider lifting the _try_add helper out of edit_web_search_tools into a reusable utility or method-level helper so it can be shared across similar call sites and is easier to test or extend in the future.
  • The Baidu AIsearch branch now has a manual aisearch_tool.active check instead of using _try_add; aligning this path with the helper (or at least the same get-and-check pattern) would keep provider handling consistent and reduce subtle behavioral differences.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider lifting the `_try_add` helper out of `edit_web_search_tools` into a reusable utility or method-level helper so it can be shared across similar call sites and is easier to test or extend in the future.
- The Baidu `AIsearch` branch now has a manual `aisearch_tool.active` check instead of using `_try_add`; aligning this path with the helper (or at least the same get-and-check pattern) would keep provider handling consistent and reduce subtle behavioral differences.

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 correctly fixes a bug where disabled web search tools were still being added to LLM requests. The introduction of the _try_add helper function to check a tool's active status before adding it is a good refactoring that improves code clarity and prevents duplication. My review includes one suggestion to further improve consistency by applying this new helper function to the baidu_ai_search provider branch as well.

Comment on lines 591 to +595
aisearch_tool = func_tool_mgr.get_func("AIsearch")
if not aisearch_tool:
raise ValueError("Cannot get Baidu AI Search MCP tool.")
tool_set.add_tool(aisearch_tool)
if aisearch_tool.active:
tool_set.add_tool(aisearch_tool)
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

While this implementation is correct, it's inconsistent with the other provider branches which use the new _try_add helper function. To improve consistency and make the code easier to maintain, you could use the helper here as well.

The special requirement for the baidu_ai_search provider to raise an error if the tool is missing can be preserved by checking for the tool's existence before calling the helper. Although this results in a second call to get_func, it makes the intent clearer and the overall structure more uniform.

Suggested change
aisearch_tool = func_tool_mgr.get_func("AIsearch")
if not aisearch_tool:
raise ValueError("Cannot get Baidu AI Search MCP tool.")
tool_set.add_tool(aisearch_tool)
if aisearch_tool.active:
tool_set.add_tool(aisearch_tool)
if not func_tool_mgr.get_func("AIsearch"):
raise ValueError("Cannot get Baidu AI Search MCP tool.")
_try_add("AIsearch")

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Apr 3, 2026
@Soulter Soulter changed the title Fix disabled web search tools still being injected into LLM requests fix: disabled web search tools still being injected into LLM requests Apr 3, 2026
@Soulter
Copy link
Copy Markdown
Member

Soulter commented Apr 3, 2026

fixed in #6584

@Soulter Soulter closed this Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer 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] 在管理行为配置里禁用系统函数但模型仍能调用

2 participants