Skip to content

updateToolGroups() does not take effect on existing ReActAgent instance #1203

Open

Description

@catshcroZhang

环境

  • AgentScope Java: 1.0.11
  • Model: Ollama (qwen3.5:9b-q4_K_M)
  • JDK: 17

问题描述

调用 toolkit.updateToolGroups() 后,已创建的 ReActAgent 实例在后续调用中仍然使用旧的工具列表,新激活的工具组对 LLM 不可见。


复现步骤

Toolkit toolkit = new Toolkit();
toolkit.createToolGroup("basic", "Basic Tools", true);
toolkit.createToolGroup("admin", "Admin Tools", false);
toolkit.registration().tool(new BasicTools()).group("basic").apply();
toolkit.registration().tool(new AdminTools()).group("admin").apply();

ReActAgent agent = ReActAgent.builder()
.name("Assistant").model(model).toolkit(toolkit).build();

// 运行时动态开启 admin 工具组
toolkit.updateToolGroups(List.of("admin"), true);

// 此后调用 agent,admin 工具对 LLM 不可见
agent.call(Msg.builder().textContent("查看系统日志").build()).block();


调试结论

通过 PreReasoningEvent Hook 验证:

hook: PreReasoningEvent → toolkit.getToolSchemas().size()
// updateToolGroups 前:2(只有 basic 工具)
// updateToolGroups 后:4(basic + admin 工具)✅ toolkit 状态已更新

toolkit.getToolSchemas() 显示工具数量确实变化,说明 toolkit 对象状态正确更新。但 LLM 收到的实际工具列表未变化——即使换用更强的模型(9b)并向 Memory 注入 SYSTEM 消息提示工具变更,LLM 仍无法调用新工具。

推测原因:ReActAgent 在 build 时将工具 schema 缓存到内部状态,call() 时直接使用缓存值而非重新读取 toolkit。


当前 Workaround

工具组变更后重建 agent:

toolkit.updateToolGroups(List.of("admin"), true);
agent = ReActAgent.builder()
.name("Assistant").model(model).toolkit(toolkit).build();


期望行为

每次 agent.call() 时应从 toolkit 实时读取当前激活的工具 schema,使 updateToolGroups() 的变更对后续调用立即生效。

Activity

  1. jujn commented on Apr 12, 2026

    @jujn
    Collaborator

    ReActAgent 在构建(build)时对 Toolkit 进行了深拷贝(防止多个Agent 共享同一个 Toolkit 并发问题)。可以试试 通过 Agent 获取其内部 Toolkit 实例:

    agent.getToolkit().updateToolGroups(List.of("admin"), true);   // √ ,更新当前 Toolkit 实例
    toolkit.updateToolGroups(List.of("admin"), true);   // x ,更新原始 Toolkit 实例
    
  2. Alexxigang commented on Apr 12, 2026

    @Alexxigang
    Contributor

    I reproduced this by tracing ReActAgent.Builder.build(): the builder deep-copies the provided Toolkit, so later toolkit.updateToolGroups(...) only mutates the caller-owned toolkit while the existing agent keeps using its internal snapshot. That is why newly activated groups are still missing from the tool schemas sent to the model on subsequent calls.

    I'm working on a fix that keeps the per-agent toolkit isolation but synchronizes external tool-group activation state for new invocations, and I'll add regression tests for repeated calls and streaming calls on an already-built agent.

  3. Alexxigang commented on Apr 12, 2026

    @Alexxigang
    Contributor

    Opened PR #1206: #1206

  4. wiscloud commented on Apr 13, 2026

    @wiscloud

    这个问题是不是会导致在运行时,让智能体根据任务需求自主选择激活哪些工具组,这个功能失败,对应的文档地址:https://java.agentscope.io/zh/task/tool.html#id19

    我的一个测试用例输出日志(定义了一个search组,组内有一个web_search工具),最初这个组是未激活的,在运行时,智能体激活了search组,但检查工具是否激活时,发现web_search未激活,通过的不过,发现是激活的组用的Toolkit和判断工具是否激活使用的Toolkit实例不是同一个。
    19:05:06.200 [boundedElastic-1] INFO io.agentscope.core.tool.ToolGroupManager -- Tool group 'search' active status set to: true
    19:06:52.406 [boundedElastic-3] WARN io.agentscope.core.tool.ToolExecutor -- Unauthorized tool call: 'web_search' is not available

  5. added
    bug
    on Apr 13, 2026
  6. added theissue type on Apr 13, 2026
  7. added
    area/core/agent
    on May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/agentbug

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions