Skip to content

[Bug]:updateToolGroups() does not take effect on existing harnessAgent.getToolkit() instance #3167

Open

Description

@1065609785

通过分组激活工具后,大模型还是无法识别到新激活的工具,这里就不写代码了;
和这个一样:#1203

Environment (please complete the following information):

AgentScope-Java Version: [2.0.3]
Java Version: [21]
OS: [ windows10]

我用大模型分析的源码给出的原因:
结论
不是深拷贝的锅——深拷贝确实存在,但不是你这个现象的死因。真正的机制是:AgentScope 2.x 的工具可见性由「每个 (userId, sessionId) 会话槽位的 AgentState」决定,不是由 toolkit 上的 activeGroups 标志决定。你在 Controller 里改的 toolkit 标志,在下一次 call() 入口就被会话状态覆盖回去了。

证据链(agentscope-core 2.0.3 sources)
改标志确实生效:ToolGroupManager.updateToolGroups 把 group.setActive(true) 并加入 activeGroups —— 你的两条日志(active status set to: true、激活后 [DEMO_TYPE, TRIP_ABNORMAL_TYPE])都来自这里,都是真的。

但每次 call 入口被覆盖:ReActAgent.activateSlotForContext()(构建每次调用的会话槽位)末尾有一行:

java

toolkit.setActiveGroups(loaded.getToolContext().getActivatedGroups());
toolkit 的 activeGroups 被会话状态里持久化的列表(你第一次调用时种子化的 [DEMO_TYPE])重置。你改的东西在这里蒸发。

推理时根本不读那个标志:reasoning() 里传给模型的工具面是:

java

List tools = toolkit.getToolSchemas(state.getToolContext().getActivatedGroups());
权威数据源是 AgentState.toolContext.activatedGroups(每个 userId/sessionId 一份,为了多会话并发互不干扰)。新会话的种子值 = build 时刻快照的 initialActiveToolGroups。

关于深拷贝:build() 时 toolkit 确实被 Toolkit.copy() 深拷贝,但 getToolkit() 返回的就是 agent 自己那份拷贝——所以即使没有深拷贝,你的改法也会被第 2、3 条杀掉。HarnessAgent 里的 getToolkit() / getDelegate().getToolkit() 最终指向同一个实例,所以两种写法症状一致。

大模型给的应用层修改建议:
agent.getAgentState(userId, sessionId)
.getToolContext()
.setActivatedGroups(List.of("DEMO_TYPE", "TRIP_ABNORMAL_TYPE"));
agent.saveAgentState(userId, sessionId);

Activity

  1. added
    bug
    on Sep 16, 2026
  2. oss-maintainer commented on Sep 16, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed | Severity: medium

    Verified against current main (6c8ccc5c6, 2.0.3-SNAPSHOT). All points in your analysis hold — the deep copy is not the root cause; tool-group activation has a dual source of truth.

    Root cause

    The model-facing tool surface is resolved from the per-session AgentState.toolContext.activatedGroups, while the toolkit's own activation flags are only a per-call scratch copy that is overwritten at call entry:

    • ToolGroupManager.java:168-188 — updateToolGroups really does set group.setActive(true) and update activeGroups.
    • ReActAgent.java:697 (inside activateSlotForContext, reached from beforeAgentExecution at the start of every call) — toolkit.setActiveGroups(loaded.getToolContext().getActivatedGroups()) wipes any between-call mutation.
    • ReActAgent.java:2365-2367 — reasoning builds tools via toolkit.getToolSchemas(state.getToolContext().getActivatedGroups()), i.e. it reads the per-session state, not the toolkit flag.
    • ReActAgent.java:463-465 / :333 — a fresh session slot seeds activatedGroups from initialActiveToolGroups, captured once at build time.
    • ReActAgent.java:5390 / :4182-4184, HarnessAgent.java:938-940 — getToolkit() returns the agent's own copy, so mutating it does hit the agent's toolkit; it just gets reset on the next call.

    Aggravating factor: toolkit.getActiveGroups() keeps reporting the mutation after build(), so debugging confirms the change "worked" (Toolkit.java:732-738), and docs/v1/en/docs/task/agent-config.md:736-747 shows exactly this between-call pattern. The v2 docs only cover the LLM-side reset_equipped_tools meta tool, and ToolGroupsE2ETest only mutates before build() — the broken path has zero test coverage.

    Workaround (this works today)

    Mutate the per-session state directly, then persist it:

    AgentState s = harnessAgent.getDelegate().getAgentState(userId, sessionId);
    s.getToolContext().setActivatedGroups(List.of("DEMO_TYPE", "TRIP_ABNORMAL_TYPE"));
    harnessAgent.getDelegate().saveAgentState(userId, sessionId); // required when a stateStore is configured

    This is the same get→mutate→save pattern HarnessAgent already uses internally for plan mode (HarnessAgent.java:408-416) and setPermissionMode (:450-453) — there is just no equivalent wrapper for tool groups.

    Possible fixes (needs maintainer call before a PR)

    1. Add ReActAgent.setActiveToolGroups(String userId, String sessionId, List<String> groups) (+ a HarnessAgent delegate) following the existing plan-mode pattern, and document it in docs/v2/en/docs/building-blocks/tool.md.
    2. Additionally warn when updateToolGroups / setActiveGroups is called on the agent's own toolkit after build().
    3. Not recommended: making the shared toolkit flags authoritative again — that would break the per-session isolation that the stateless-engine design guarantees for concurrent calls.

    Could a maintainer confirm which direction is acceptable (new convenience API + docs, or docs-only) before we generate a fix proposal? Option 3 would regress concurrency, so we will not propose it.


    Automated evaluation by github-manager-bot

  3. added
    area/core/tool
    on Sep 16, 2026
  4. aiyili commented on Oct 21, 2026

    @aiyili
    Contributor

    Hi, I'd like to work on this. The diagnosis is clear: activateSlotForContext overwrites toolkit activeGroups from the persisted session state on every call entry, so out-of-band updateToolGroups evaporates. Plan: make updateToolGroups write through to the active slot's persisted tool context (when a slot is active), so in-session updates survive the next call entry; tests asserting the update survives a subsequent call() without a fresh state seed. PR coming shortly.

  5. aiyili commented on Oct 28, 2026

    @aiyili
    Contributor

    Checking in on this after #3206 (per-session activation API, merged): the original repro was an existing toolkit instance not seeing updateToolGroups changes because activateSlotForContext re-stamps from persisted state on every call entry. Does the per-session API fully cover the out-of-band updateToolGroups use case now, or does the toolkit-instance staleness remain (i.e. updateToolGroups + next call() still reverts)? If the latter, I'd like to take the write-through fix (update the active slot's persisted tool context so in-session updates survive the next call entry). Will verify against current main first and report.

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/toolbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions