[Bug]: Tool groups activated at build time are lost on first call for new sessions
Describe the bug
When creating a ReActAgent with a Toolkit that has tool groups activated at build time (via toolkit.createToolGroup(name, desc, true)), the activated groups are silently lost on the first call for any new session. This results in an empty tools array being sent to the LLM — the model cannot call any tools.
To Reproduce
Steps to reproduce the behavior:
- Create a
Toolkitand register a tool group withactive=true:
Toolkit toolkit = new Toolkit();
toolkit.createToolGroup("myTools", "My tool group", true);
toolkit.registration().tool(myToolObject).group("myTools").apply();
// At this point, toolkit.getActiveGroups() returns ["myTools"] ✓
- Build a
ReActAgentwith this toolkit and anAgentStateStore:
ReActAgent agent = ReActAgent.builder()
.name("testAgent")
.sysPrompt("You are a helpful assistant.")
.model(someModel)
.toolkit(toolkit)
.maxIters(10)
.stateStore(agentStateStore) // ← key: stateStore is configured
.build();
- Call the agent with a new session (no prior state in the store):
RuntimeContext ctx = RuntimeContext.builder()
.sessionId("new-session-001")
.userId("user-001")
.build();
agent.call("Please use myTool to do something", ctx);
- Observe that the LLM request body has an empty
toolsarray — the model cannot call any tools.
What happens internally:
Inside ReActAgent.activateSlotForContext() (line 479-481):
if (toolkit != null) {
toolkit.setActiveGroups(loaded.getToolContext().getActivatedGroups());
}
For a new session, loadOrCreateAgentStateForSlot() returns a freshState() where ToolContextState.activatedGroups defaults to an empty list. This empty list then overwrites the toolkit's build-time active groups via setActiveGroups([]).
Then in reasoning() (line 1947-1949):
List<ToolSchema> tools = toolkit.getToolSchemas(
state.getToolContext().getActivatedGroups() // ← empty list
);
// → all grouped tools are filtered out → tools = []
The LLM receives a request with no tools field, so it cannot invoke any tool.
Expected behavior
Tool groups activated at build time (active=true) should remain active for new sessions. The initial activation state should be treated as a template, similar to how initialPermissionContext is handled.
Error messages
No exception is thrown. The bug manifests as:
- The LLM request body has an empty (or missing)
toolsarray - The model responds without calling any tools, often saying it doesn't have access to tools
Environment (please complete the following information):
- AgentScope-Java Version: 2.0.0-SNAPSHOT (master branch)
- Java Version: 21
- OS: Windows
Additional context
Root cause:
The constructor saves initialPermissionContext as a template for new sessions, but does not save the toolkit's initial active groups:
// ReActAgent constructor (line 315):
this.initialPermissionContext = builder.permissionContext; // ✓ saved
// ⚠️ No equivalent for toolkit active groups
When freshState() creates a new AgentState, it builds a default ToolContextState with an empty activatedGroups list. Then activateSlotForContext() uses this empty list to call toolkit.setActiveGroups([]), clearing all build-time activations.
Suggested fix:
- Save the toolkit's initial active groups in the constructor:
// In constructor:
this.initialActiveGroups = agentToolkit != null
? List.copyOf(agentToolkit.getActiveGroups())
: List.of();
- Use them when creating fresh state:
// In freshState():
ToolContextState toolCtx = ToolContextState.builder().build();
if (!initialActiveGroups.isEmpty()) {
toolCtx.setActivatedGroups(new ArrayList<>(initialActiveGroups));
}
asb.toolContext(toolCtx);
This ensures that new sessions inherit the build-time tool group activation, and activateSlotForContext() will correctly restore the toolkit's active groups instead of clearing them.
Describe the bug
创建 ReActAgent 时,如果 Toolkit 在构建阶段通过 toolkit.createToolGroup(name, desc, true) 激活了工具组,那么在新会话的首次调用中,这些激活的工具组会被静默清空。这导致发送给 LLM 的请求体中 tools 数组为空——模型无法调用任何工具。
To Reproduce
复现步骤:
- 创建
Toolkit并注册一个active=true的工具组:
Toolkit toolkit = new Toolkit();
toolkit.createToolGroup("myTools", "My tool group", true);
toolkit.registration().tool(myToolObject).group("myTools").apply();
// 此时 toolkit.getActiveGroups() 返回 ["myTools"] ✓
- 使用该 toolkit 和
AgentStateStore构建ReActAgent:
ReActAgent agent = ReActAgent.builder()
.name("testAgent")
.sysPrompt("You are a helpful assistant.")
.model(someModel)
.toolkit(toolkit)
.maxIters(10)
.stateStore(agentStateStore) // ← 关键:配置了 stateStore
.build();
- 使用一个新会话(store 中没有历史状态)调用 Agent:
RuntimeContext ctx = RuntimeContext.builder()
.sessionId("new-session-001")
.userId("user-001")
.build();
agent.call("Please use myTool to do something", ctx);
- 观察到 LLM 请求体中
tools数组为空——模型无法调用任何工具。
What happens internally:
在 ReActAgent.activateSlotForContext()(第 479-481 行)中:
if (toolkit != null) {
toolkit.setActiveGroups(loaded.getToolContext().getActivatedGroups());
}
对于新会话,loadOrCreateAgentStateForSlot() 返回 freshState() 创建的全新 AgentState,其中 ToolContextState.activatedGroups 默认为空列表。这个空列表随后通过 setActiveGroups([]) 覆盖了 toolkit 在构建阶段设置的激活状态。
然后在 reasoning()(第 1947-1949 行)中:
List<ToolSchema> tools = toolkit.getToolSchemas(
state.getToolContext().getActivatedGroups() // ← 空列表
);
// → 所有分组工具被过滤掉 → tools = []
LLM 收到的请求没有 tools 字段,因此无法调用任何工具。
Expected behavior
构建阶段激活的工具组(active=true)应在新会话中保持激活状态。初始激活状态应作为模板使用,与 initialPermissionContext 的处理方式一致。
Error messages
不会抛出任何异常。该 bug 的表现为:
- LLM 请求体中
tools数组为空(或缺失) - 模型回复时不调用任何工具,通常表示自己没有可用的工具
Environment (please complete the following information):
- AgentScope-Java Version: 2.0.0-SNAPSHOT (master branch)
- Java Version: 21
- OS: Windows
Additional context
根因分析:
构造函数保存了 initialPermissionContext 作为新会话的模板,但没有保存 toolkit 的初始激活工具组:
// ReActAgent 构造函数(第 315 行):
this.initialPermissionContext = builder.permissionContext; // ✓ 已保存
// ⚠️ 工具组激活状态没有对应的保存逻辑
当 freshState() 创建新的 AgentState 时,会构建一个默认的 ToolContextState,其 activatedGroups 为空列表。随后 activateSlotForContext() 用这个空列表调用 toolkit.setActiveGroups([]),清除了构建阶段的所有激活状态。
建议修复方案:
- 在构造函数中保存 toolkit 的初始激活工具组:
// 构造函数中:
this.initialActiveGroups = agentToolkit != null
? List.copyOf(agentToolkit.getActiveGroups())
: List.of();
- 在创建全新状态时使用它:
// freshState() 中:
ToolContextState toolCtx = ToolContextState.builder().build();
if (!initialActiveGroups.isEmpty()) {
toolCtx.setActivatedGroups(new ArrayList<>(initialActiveGroups));
}
asb.toolContext(toolCtx);
这样可以确保新会话继承构建阶段的工具组激活状态,activateSlotForContext() 将正确恢复 toolkit 的激活组而不是清空它们。
补充说明 / Workaround
经过进一步排查,我们发现在当前业务场景下工具组(Tool Group)并非必需功能,可以通过一个简单的 workaround 绕过此问题:
不使用 createToolGroup,直接注册工具即可:
// ❌ 会触发 bug
toolkit.createToolGroup("myTools", "My tool group", true);
toolkit.registration().tool(myToolObject).group("myTools").apply();
// ✅ workaround:不分组,直接注册
toolkit.registration().tool(myToolObject).apply();
原理: 在 ToolSchemaProvider.buildSchemas() 中,工具的过滤逻辑为:
if (groupManager.isGroupedTool(toolName) && !activeTools.contains(toolName)) {
continue; // 跳过未激活的分组工具
}
未分组的工具(ungrouped tools)不会进入此分支,因此始终被包含在 schemas 中,不受 activatedGroups 空列表的影响。
注意: 此 workaround 仅适用于不需要动态切换工具组激活状态的场景。如果你的 Agent 需要在运行时通过 meta tool 动态激活/停用工具组,仍需等待框架层面的修复。
不建议直接贴AI分析的长篇大论提ISSUE,PR需要表达清楚,否则很难会被关注。
I'd like to work on this. Plan: preserve toolkit build-time active groups as an initial template (similar to initialPermissionContext) and apply them in freshState() so new sessions don't wipe activated tool groups.
Update: this appears already fixed on main via #2319 (initialActiveToolGroups is captured in the constructor and passed into freshState()). I'll step back unless maintainers want a dedicated regression test PR.
This no longer reproduces on 2.0.3. ReActAgent now captures the toolkit's active groups at construction (initialActiveToolGroups = List.copyOf(toolkit.getActiveGroups()), ReActAgent.java:332), and freshState(...) seeds new sessions with them.
Verified with a HarnessAgent backed by a Redis AgentStateStore: a group created with active=true at build time is present in the tool list of the first call of a brand-new session. It may be worth closing this issue, or adding a regression test that pins it.
Issue Evaluation
Category: bug | Status: Already fixed on main
Re-verified against current main (matches the 2.0.3 repro attempt in this thread):
ReActAgentsnapshots the toolkit's build-time active groups in the constructor —initialActiveToolGroups = List.copyOf(toolkit.getActiveGroups())(ReActAgent.java:253, 332).- The snapshot is threaded into new-session state creation (
ReActAgent.java:427-428, 670-678, used byloadOrCreateAgentStateForSloteven when astateStoreis configured) and applied to a freshToolContextStateviainitialActiveToolGroups.forEach(toolContext::addActivatedGroup)(ReActAgent.java:462-465). - Behavior is pinned by tests (
ReActAgentPerSessionStateTest.java:116-145: fresh slots inherit default/active groups, persisted empty slots stay empty).
So the first call of a brand-new session no longer sends an empty tools array; persisted sessions keep taking precedence when state exists. This was resolved by the PR referenced by commenters (#2319).
@Coco-king could you confirm on a current build and close this issue if it no longer reproduces? (Leaving issue state to the reporter/maintainers.)
Automated evaluation by github-manager-bot