BitFun icon indicating copy to clipboard operation
BitFun copied to clipboard

Bug: resolve_tool_manifest_policy produces duplicate GetToolSpec causing Tool names must be unique API error

Open 1688mengdie opened this issue 4 months ago • 2 comments

Summary

When GetToolSpec is explicitly included in allowed_tools and collapsed tools are present, resolve_tool_manifest_policy pushes GetToolSpec into expanded_tool_names twice, causing duplicate entries in the final tool_definitions sent to the AI provider. The provider rejects the request with:

HTTP 400: Tool names must be unique.

Steps to Reproduce

  1. Configure the agent with GetToolSpec explicitly in the tool allowlist
  2. Also include any tool whose default_exposure is Collapsed (e.g. WebFetch, WebSearch, Git, GetFileDiff)
  3. Send a request → AI provider returns Tool names must be unique

This is commonly hit when enabling a large set of tools (e.g. all ~60 available) because the conditions are easily met.

Root Cause

src/crates/execution/tool-contracts/src/framework.rs, function resolve_tool_manifest_policy:

  • Lines 240–253: iterates allowed_tools, pushes GetToolSpec into expanded_tool_names once (it has Expanded default exposure).
  • Lines 262–267: when collapsed tools exist, unconditionally pushes GetToolSpec into expanded_tool_names again, without a dedup check.

Note that allowed_tool_names already has a dedup guard (line 256–258), but expanded_tool_names does not — an asymmetric omission.

Known Workaround

Manually exclude GetToolSpec from the tool list. The system auto-adds it when collapsed tools are present.

Related Test Coverage (confirms the bug)

Two tests currently assert the duplicate as expected behavior:

Test File Current Expected
product_manifest_preserves_explicit_get_tool_spec_runtime_contract catalog.rs:706 ["WebFetch", "GetToolSpec", "GetToolSpec"]
tool_manifest_policy_preserves_explicit_get_tool_spec_duplicate_runtime_contract tool_contracts.rs:1569 [GET_TOOL_SPEC, GET_TOOL_SPEC]

Proposed Fix

Add dedup check before pushing to expanded_tool_names, mirroring the existing guard on allowed_tool_names:

         if tool_snapshot
             .iter()
             .any(|tool| tool.name == get_tool_spec_tool_name)
+            && !expanded_tool_names
+                .iter()
+                .any(|name| name == get_tool_spec_tool_name)
         {
             expanded_tool_names.push(get_tool_spec_tool_name.to_string());
         }

Tests updated: the two tests above should assert single occurrence instead of duplicate.

Verification:

  • cargo check --workspace — pass
  • cargo test -p bitfun-agent-tools — 143 pass
  • cargo test -p bitfun-core -- catalog — 22 pass
  • cargo test -p bitfun-agent-runtime --test post_call_hook_execution_contracts — 3 pass
  • pnpm run i18n:contract:test — 37 pass

1688mengdie avatar Jul 16 '26 01:07 1688mengdie

感谢反馈,最近在做这块的重构,一并修复一下

wsp1911 avatar Jul 16 '26 01:07 wsp1911

补充确认:Claw 模式同样受影响

今天在实际使用中复现了 Claw 模式的同一问题。

触发路径

Claw 的 agent profile 配置(i.agent_profiles.Claw.added_tools)中包含了 GetToolSpec,同时 Claw 默认工具中有 WebSearch、WebFetch 两个 Collapsed 工具。

esolve_tool_manifest_policy 的处理流程:

  1. 从配置中读取 GetToolSpec 进入 llowed_tools → 以 Expanded 身份进入 expanded_tool_names(第一次)
  2. 检测到 Collapsed 工具存在 → 无条件向 expanded_tool_names 再追加一次 GetToolSpec(第二次,无去重检查)
  3. ool_definitions 中出现两个同名 GetToolSpec
  4. API 返回 HTTP 400: \Tool names must be unique\

修复方式(用户侧 workaround)

从 Claw 的 dded_tools 配置中移除 GetToolSpec 即可。系统会在有 Collapsed 工具时自动追加,不需要手动添加。

结论

该问题不是特定 Agent 模式的问题,而是 esolve_tool_manifest_policy 中 expanded_tool_names 缺少去重守卫的通用 Bug。 任何同时满足以下条件的模式都会触发:

  1. 工具列表中显式包含 GetToolSpec
  2. 存在任意 Collapsed 工具

1688mengdie avatar Jul 16 '26 02:07 1688mengdie