Skip to content

fix(a2a): close the client of cancelled calls and stop using a lifecycle hook - #3663

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/issue-2612-a2a-client-lifecycle
Oct 20, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/issue-2612-a2a-client-lifecycle

Conversation

@guslegend0510

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (main)

Description

A2A client 去掉弃用 API 的第一步:A2aAgent 内部不再用 Hook 管理 A2A client,顺带修掉取消调用时 client 不关闭的问题。不改公开 API(只把一个本来就不起作用的 Builder 方法标为弃用)。

问题

  • A2aAgent 用一个内部 Hook 管理 A2A client:PreCall 时创建,PostCall 和 Error 时关闭;client、事件上下文和请求 id 都放在字段上。
  • 调用被取消(dispose、超时)时,PostCall 和 Error 都不会触发,这次调用的 client 一直不关闭。
  • PreCallEvent / PostCallEvent / ErrorEvent / HookEvent 都是 @Deprecated(forRemoval = true)。

改动

  • client 和事件上下文属于每次调用:调用开始时创建,用 Mono.using 保证成功、失败、取消都会关闭 client。字段上只保留「当前调用」的引用,给 interrupt() 用。
  • 去掉内部 Hook,getHooks() 只返回用户给的 Hook。用户的 Hook(包括 Pre/Chunk/PostReasoning)和弃用的 stream() 行为不变。
  • 改用不带 checkRunning 的 AgentBase 构造器(这个参数在 AgentBase 里本来就被忽略),Builder.checkRunning(...) 标为 @Deprecated。
  • 测试原来靠一个 Hook 在 PreCall 之后用反射替换 client,改为包内可见的 clientReplacer:仍按配置真实构建 client,再换成 mock,原来覆盖的构建逻辑(自定义 transport、transport 不存在时报错)照常覆盖。

不在本 PR

streamEvents()、middleware、Memory 改用 AgentState、按 RuntimeContext 中断,后续分别提小 PR。

测试

  • 新增:取消调用后 client 被关闭;getHooks() 只有用户给的 Hook。两条在 main 上都会失败。
  • 补充:成功调用后 client 被关闭(防回归,main 上通过)。
  • A2A client 模块 121 个测试通过;Starter 的 A2aHttpEndToEndTest(真实 HTTP,包括用 Hook 收流式分块的用例)14 个里 13 个通过、1 个跳过(等 fix(a2a): 取消任务后结束服务端执行,并释放服务端线程池 #3578 的中断用例);Nacos A2A 模块全部通过。
  • 本地(JDK 17,macOS):mvn -B -T1 clean verify 97 个模块全部通过(10,938 个测试,0 失败,200 跳过)。补丁覆盖率:行 36/36,分支 1/2(未覆盖的是测试不替换 client 的分支,由 Starter 端到端测试覆盖)。

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn -B -T1 clean verify)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)(用法没有变化,不需要改文档)
  • Code is ready for review

…cle hook

A2aAgent built its A2A client in a PreCall hook and closed it in the
PostCall and Error hooks, keeping the client and the event context of
the call in fields. A cancelled call fires neither hook, so its client
was never closed. The hook API is also deprecated for removal.

The client and the event context now belong to the call: they are
created when the call starts, and the client is closed however the call
ends, cancellation included. Only a reference to the call in progress is
kept, for interrupt(). The internal hook no longer shows up in
getHooks(); hooks given to the builder work as before.

A2aAgent now uses the AgentBase constructor without checkRunning, which
had no effect, and Builder.checkRunning is deprecated.
@jujn
jujn merged commit 73cb2da into agentscope-ai:main Oct 20, 2026
5 checks passed
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
…cle hook (#3663)

Backport the verified fix to the 2.0.x implementation and retain its existing public contracts where the upstream kernel differs.

(cherry picked from commit 73cb2da)
Backport-review-patch: eb66dd61f9d5177f6804aef339bc7c08d995cc4f
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants