Description
通过分组激活工具后,大模型还是无法识别到新激活的工具,这里就不写代码了;
和这个一样:#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
oss-maintainer commentedon Sep 16, 2026
Issue Evaluation
Category:
bug| Status: Confirmed | Severity: mediumVerified 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—updateToolGroupsreally does setgroup.setActive(true)and updateactiveGroups.ReActAgent.java:697(insideactivateSlotForContext, reached frombeforeAgentExecutionat the start of every call) —toolkit.setActiveGroups(loaded.getToolContext().getActivatedGroups())wipes any between-call mutation.ReActAgent.java:2365-2367— reasoning builds tools viatoolkit.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 seedsactivatedGroupsfrominitialActiveToolGroups, 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 afterbuild(), so debugging confirms the change "worked" (Toolkit.java:732-738), anddocs/v1/en/docs/task/agent-config.md:736-747shows exactly this between-call pattern. The v2 docs only cover the LLM-sidereset_equipped_toolsmeta tool, andToolGroupsE2ETestonly mutates beforebuild()— the broken path has zero test coverage.Workaround (this works today)
Mutate the per-session state directly, then persist it:
This is the same get→mutate→save pattern
HarnessAgentalready uses internally for plan mode (HarnessAgent.java:408-416) andsetPermissionMode(:450-453) — there is just no equivalent wrapper for tool groups.Possible fixes (needs maintainer call before a PR)
ReActAgent.setActiveToolGroups(String userId, String sessionId, List<String> groups)(+ aHarnessAgentdelegate) following the existing plan-mode pattern, and document it indocs/v2/en/docs/building-blocks/tool.md.updateToolGroups/setActiveGroupsis called on the agent's own toolkit afterbuild().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
aiyili commentedon Oct 21, 2026
Hi, I'd like to work on this. The diagnosis is clear:
activateSlotForContextoverwrites toolkit activeGroups from the persisted session state on every call entry, so out-of-bandupdateToolGroupsevaporates. Plan: makeupdateToolGroupswrite 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 subsequentcall()without a fresh state seed. PR coming shortly.aiyili commentedon Oct 28, 2026
Checking in on this after #3206 (per-session activation API, merged): the original repro was an existing toolkit instance not seeing
updateToolGroupschanges becauseactivateSlotForContextre-stamps from persisted state on every call entry. Does the per-session API fully cover the out-of-bandupdateToolGroupsuse case now, or does the toolkit-instance staleness remain (i.e.updateToolGroups+ nextcall()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.