[Bug]: SubAgentTool never closes the agents it obtains from SubAgentProvider (leaks into GracefulShutdownManager.stateSavers)
Describe the bug
SubAgentTool calls agentProvider.provide() twice and never closes either instance:
- once in the constructor (SubAgentTool.java#L85). That sample agent is only read for its name/description and then dropped;
- once per invocation (SubAgentTool.java#L151). The reactive chain only has
doFinally(CANCEL → interruptAgent)anddoOnSuccess(saveAgentState). There is noclose()anywhere in the file.
ReActAgent implements AutoCloseable, and its close() exists precisely so that per-call instances get released: it calls shutdownManager.unbindStateSaver(this) and clearStateCache(). Because SubAgentTool never calls it, every delegation leaves one ReActAgent and its state cache referenced from GracefulShutdownManager.stateSavers for the lifetime of the JVM.
To Reproduce
- Register a sub-agent:
toolkit.registration().subAgent(() -> ReActAgent.builder()...build()). - Have the parent agent invoke the sub-agent tool N times.
- Inspect
GracefulShutdownManager.stateSaversreflectively. It grows by N + 1 (N per-call instances plus the constructor sample) and never shrinks.
Expected behavior
Instances obtained from SubAgentProvider are closed once SubAgentTool is done with them.
Suggested fix
- Close the sample agent right after reading its name/description. Better still, skip creating it when
SubAgentConfigalready supplies both (related: #1627). - Close the per-call agent when the call terminates. Do it in
doFinally, after the existing CANCEL → interrupt and aftersaveAgentState, e.g.if (agent instanceof AutoCloseable c) c.close();. The order matters: interrupt first, then close.
We currently work around this by attaching an outermost middleware to the sub-agent that closes it after the call.
Environment
- AgentScope-Java Version: 2.0.4 (unchanged since 2.0.3; still present on main @035487e)
- Java Version: 25
- OS: Linux / Windows
Hi maintainers, I'd like to work on this issue. I'll investigate the SubAgentTool lifecycle and implement a focused fix to ensure agents created by SubAgentProvider are properly closed. My plan is to:
- Release the temporary agent created during tool initialization.
- Ensure per-invocation agents are closed after completion, errors, or cancellation, while preserving the existing interruption and state-saving behavior.
- Add regression tests to verify that repeated sub-agent invocations do not leave stale registrations in GracefulShutdownManager. I'll keep the changes scoped to resource lifecycle management and submit a PR once the fix is validated. Please let me know if there are any concerns with this approach.
感谢认领!方案方向没问题。有几个细节如果没处理好会静默出错,提一下:
- close 必须严格排在状态保存之后。
saveAgentState是在doOnSuccess里读ra.getAgentState()的,而close()会调用clearStateCache()。如果 close 先于它或与它并列执行,续接会话就会存进一份空状态,且不报任何错。把 close 挂在最外层的doFinally上就能保证顺序。 - 取消时先 interrupt 再 close,放在同一个
doFinally回调里,这样先后顺序是确定的。 close()只存在于ReActAgent(实现了AutoCloseable),Agent接口上没有,所以需要用instanceof AutoCloseable判断后再关。另外SubAgentProvider的约定只是"应当"每次返回新实例,建议在 javadoc 里写明:工具会关闭 provider 返回的实例。
回归测试方面,除了校验 stateSavers 不残留,建议再加一条"修复后续接会话仍能看到上一轮记忆"的断言,可以兜住第 1 点。PR 提了我可以帮忙 review。
感谢详细的指导!
我会确保在状态持久化之后进行清理,并在取消时于关闭之前处理中断。
我还会在 Javadoc 中阐明提供者生命周期契约,并添加覆盖资源清理和会话状态连续性的回归测试。
PR 准备好后我会在这里附上链接。感谢你愿意帮忙审阅!