agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: SubAgentTool never closes the agents it obtains from SubAgentProvider (leaks into GracefulShutdownManager.stateSavers)

Open littlebird69 opened this issue 1 month ago • 3 comments

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) and doOnSuccess(saveAgentState). There is no close() 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

  1. Register a sub-agent: toolkit.registration().subAgent(() -> ReActAgent.builder()...build()).
  2. Have the parent agent invoke the sub-agent tool N times.
  3. Inspect GracefulShutdownManager.stateSavers reflectively. 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 SubAgentConfig already supplies both (related: #1627).
  • Close the per-call agent when the call terminates. Do it in doFinally, after the existing CANCEL → interrupt and after saveAgentState, 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

littlebird69 avatar Oct 17 '26 11:10 littlebird69

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.

ZhaoYJ9 avatar Oct 19 '26 08:10 ZhaoYJ9

感谢认领!方案方向没问题。有几个细节如果没处理好会静默出错,提一下:

  1. close 必须严格排在状态保存之后。 saveAgentState 是在 doOnSuccess 里读 ra.getAgentState() 的,而 close() 会调用 clearStateCache()。如果 close 先于它或与它并列执行,续接会话就会存进一份空状态,且不报任何错。把 close 挂在最外层的 doFinally 上就能保证顺序。
  2. 取消时先 interrupt 再 close,放在同一个 doFinally 回调里,这样先后顺序是确定的。
  3. close() 只存在于 ReActAgent(实现了 AutoCloseable),Agent 接口上没有,所以需要用 instanceof AutoCloseable 判断后再关。另外 SubAgentProvider 的约定只是"应当"每次返回新实例,建议在 javadoc 里写明:工具会关闭 provider 返回的实例。

回归测试方面,除了校验 stateSavers 不残留,建议再加一条"修复后续接会话仍能看到上一轮记忆"的断言,可以兜住第 1 点。PR 提了我可以帮忙 review。

littlebird69 avatar Oct 19 '26 08:10 littlebird69

感谢详细的指导!

我会确保在状态持久化之后进行清理,并在取消时于关闭之前处理中断。

我还会在 Javadoc 中阐明提供者生命周期契约,并添加覆盖资源清理和会话状态连续性的回归测试。

PR 准备好后我会在这里附上链接。感谢你愿意帮忙审阅!

ZhaoYJ9 avatar Oct 19 '26 09:10 ZhaoYJ9