Open
feat(runtime): 增加可信输入轮与延迟结算 #277
feat(runtime): 增加可信输入轮与延迟结算 #277
Open
lynn45Add on 2 days ago
lynn45
lynn45
2 days ago

Paired: GitHub #297 ↔ GitCode !277

/kind feature

改动

基于现有 Runtime 准入、A2A 排队和任务发射器,提供可选的进程内输入轮 SPI。请求经准备后才打开服务端输入边界。同一任务中的不同用户输入独立结算,内部续跑复用本轮边界,后台回调和被拒绝的请求不创建用户输入轮。

A2A 同步和流式执行以任务发射器确认的结果或运行时已接纳的队列推进为结算依据。等待输入以后关流失败、迟到回调或取消竞争都不会覆盖已确认的结果。HTTP 客户端断流与服务端执行取消分别处理。新增 SPI 不改变 HTTP、SSE 或 A2A 的报文结构。

验证

基于 develop 4526258a,使用上游 Core Java 0.1.18 制品运行 Maven 定向回归和安装。Spec 6 项、App 284 项全部通过。覆盖实际 HTTP 入口、同一任务连续两轮等待后完成、同步和流式的仲裁、已接纳取消、关闭事件队列失败、客户端断流和原响应回调。git diff --check 通过。

Self-checklist

likedislike
Merge blocked
atomgit-bot
atomgit-bot
2 days ago Comments:

变更摘要

本 PR 在现有 Runtime 准入、A2A 排队与任务发射器之上新增了一套可选的进程内“用户输入轮”SPI:请求只有在准入准备完成后才打开服务端输入边界,并通过 ServeRootState 上报运行时已接纳的最终结果。新增的 RootInputInvocation 负责把宿主提供的边界绑定到请求生命周期,统一处理同步返回、流式观察、异常、清理失败与取消判定;A2AAgentExecutor 与两个编排器改为以“发射器确认的结果 / 队列推进后的接纳状态”作为结算依据,避免迟到回调、关流失败或取消竞争覆盖已确认结果。SPI 不改变 HTTP、SSE 或 A2A 的报文结构,承载字段在 ServeRequest 中为不参与序列化的进程内字段。

主要改动

  • 新增输入轮 SPI 契约:新增 ServeInputBoundary(提供 context() 与 finish(ServeRootState))、ServeInputBoundaryFactory(open(ServeRequest))以及枚举 ServeRootState(COMPLETED/WAITING_INPUT/FAILED/CANCELLED),并给 ServeRequest 增加被 @JsonIgnore、transient、ToString.Exclude、EqualsAndHashCode.Exclude 修饰的 inputTurnContext 字段,确保该字段永不来自报文且不进入调试输出。

  • 输入轮绑定与结算协调器:新增 RootInputInvocation,提供 begin/none/defer/returned/failed/cleanupFailed/observe/finish,其中 DeferredSettlement 通过共享的 accepted 状态实现“准入与权威结果任意先后到达”的一次性结算;ObservedStream 在保留下游观察者回调的同时记录 TYPE_CANCEL、TYPE_ERROR、TYPE_INTERRUPT 信号,并以 isCancelled() 区分客户端断流与服务端执行取消。

  • 编排器接入准入后开边界:DefaultServeOrchestrator 与 A2AEnabledServeOrchestrator 在 prepareTask 成功后调用 RootInputInvocation.begin,在 finally 中先 completeTask 再 input.finish(...),清理抛出异常时标记 cleanupFailed();流式路径用 input.observe(observer) 包装下游观察者,A2A 续跑请求通过 resume.setInputTurnContext(original.getInputTurnContext()) 复用本轮边界。

  • A2A 执行器改为按输入轮仲裁结算:A2AAgentExecutor 用 activeRounds(按 taskId)替换 activeCancellations,InputRound 增加 isUserInput/isCancelled/accepted/settlement 与 begin/emit/cancel 方法,requiresInput、cancel、fail、complete 均需先被 emit 接受才生效并只结算一次;取消在已进入非等待态时抛 TaskNotCancelableError,closeEventQueue 失败不再回退调用 emitter.complete(),后续轮次(advanceWaitingInput)在 synchronized 内推进 lease 并以 WAITING_INPUT 结算。

  • 工厂注入与装配:QueryMvcController、QueryWebFluxController 通过新增的 ObjectProvider<ServeInputBoundaryFactory> 构造器在准入前把工厂写入 serveRequest,A2AAutoConfiguration 与 HostedRuntimeAssembler 通过 setInputBoundaryFactory 把可选工厂交给 A2AAgentExecutor,保持无该 Bean 时行为不变。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
2 days ago Comments:

代码审查

审查结论

已报告问题

# 级别 位置 问题
1 P2 A2AAgentExecutor.java:1105-1107 closeEventQueue 失败后删除 emitter.complete() 兜底且无替代收尾,确认 INPUT_REQUIRED/CANCELLED 后若关队列失败,SSE 流不再终止、队列与消费者线程不释放(有条件触发,属可靠性/资源泄漏回归;若为有意取舍请忽略)
2 P3 DefaultServeOrchestrator.java:102-107 流式路径 completeTask 失败时不调用新增的 input.cleanupFailed(),与本次在同步 query() 路径(66-73 行)新增的处理不对称,导致该输入轮可能永远不结算
3 P3 A2AAgentExecutor.java:905-921 InputRound.cancel 在 isCancelled 已为真时抛 TaskNotCancelableError,重复/重试取消不再幂等(旧实现幂等),且抛错会跳过 orchestrator.cancelActive 与 deferredInputs.cancelTask

逐文件审查记录

文件 结果
documents/zh/.../com.openjiuwen.service/spec.README.md 无问题(新增“可信输入轮扩展”章节,描述与 @JsonIgnore/transient、ServeRootState 四态一致,无危险指令/不安全默认配置)
.../app/autoconfigure/A2AAutoConfiguration.java 无问题(ObjectProvider<ServeInputBoundaryFactory> 参数为可选注入;javadoc 未同步新增 @param,属文档细节,未列报)
.../app/controller/a2a/A2AAgentExecutor.java 见问题 1、3(另核查:activeRounds 键 taskId 与 ctx.getTaskId()/A2AMessageContext.from 一致;isUserInput 在 execute/executeDeferred/continueTask 三处取值正确;advanceWaitingInput 的 accepted 与 release(lease, isTerminal) 语义与旧 isWaiting 等价;被删除的 completeAndDrain(2 参)/finishInputRound/activeCancellations 无残留引用)
.../app/controller/query/QueryMvcController.java 无问题(serveRequest() 为每请求新建实例,无跨请求共享;未就绪/未解析到编排器时提前返回,不创建轮次)
.../app/controller/query/QueryWebFluxController.java 无问题(同上;仅同步/流式分派前注入工厂)
.../app/hosting/HostedRuntimeAssembler.java 无问题(assembly.component(...) + beanFactory().getBeanProvider(...) 兜底可用,与同文件 autowireBean/initializeBean 用法一致)
.../app/input/RootInputInvocation.java 无问题(begin 以“类型替换”防重复开轮;DeferredSettlement 双向竞态有 accepted/synchronized 守护;NONE 单例所有入口均有 boundary == null 短路;stateOf 的 _cancel/_interrupt/result_type 判定与 A2AAgentExecutor.CANCEL/INTERRUPT 及 JiuwenCoreAgentHandler.isErrorResult 取值一致)
.../app/orchestrator/A2AEnabledServeOrchestrator.java 无问题(query 四个返回点均经 input.returned,异常经 input.failed();streamQuery 用 observed 重建 TerminalQueryStreamObserver,finish 在 releaseTerminal 之后执行;buildBatchResumeRequest 正确透传 inputTurnContext,避免内部续跑二次开轮)
.../app/orchestrator/DefaultServeOrchestrator.java 见问题 2
.../test/.../A2AAgentExecutorTest.java 无问题(新增 5 个用例与实现一致;answerVoid、requestAdapter(ctx-1)、requestContext 等辅助已存在,scope 匹配成立,无编译/断言错误)
.../test/.../QueryMvcControllerTest.java 无问题(DefaultListableBeanFactory/MediaType/post 等导入均已在文件中存在,无编译风险)
.../test/.../RootInputInvocationTest.java 无问题(四个用例分别覆盖未准入不开轮、取消信封结算、断流不结算、取消保留响应,均与实现路径吻合)
.../spec/dto/ServeRequest.java 无问题(@JsonIgnore + transient + ToString/EqualsAndHashCode.Exclude,@Data 不再生成序列化入口,无线上注入任意对象的面)
.../spec/spi/ServeInputBoundary.java 无问题(纯 SPI 契约)
.../spec/spi/ServeInputBoundaryFactory.java 无问题(@FunctionalInterface 契约;DeferredSettlement 实现同接口,begin 通过 instanceof 区分)
.../spec/spi/ServeRootState.java 无问题(枚举四态与文档/实现一致)
.../test/.../ServeRequestTest.java 无问题(反序列化忽略 + 序列化/toString 不泄露内部身份,断言有效)

总体风险判断

本次改动的核心(准备后开轮、A2A 由发射器/运行时队列推进结算、迟到回调与断流不覆盖已确认结果)实现自洽:结算入口均经 emit/accept 的“首次接受”门槛,release 语义与旧 isWaiting 等价,新旧 SPI 不改变 HTTP/SSE/A2A 报文结构。剩余风险集中在错误收尾与取消语义的边角:关队列失败后的流终止兜底被移除(P2,需 Maintainer 确认是否为目标取舍)、流式清理失败不结算(P3)、重复取消不再幂等(P3)。整体可接受,建议修正 P2 后再合入。

类型 数量
🔴 阻塞 0
🟡 建议 2

💬 仅评论

likedislike
Incorrect?
atomgit-bot
atomgit-bot
2 days ago Comments:

AtomGit AI 助手使用指南

在 PR 评论中使用命令即可触发。支持在普通评论和代码行评论中使用。

功能 命令 说明
代码审查 /ai review 检查代码质量、潜在问题、安全风险
PR 摘要 /ai summary 生成 PR 变更内容的结构化摘要
代码解释 /ai explain 解释代码变更的逻辑和意图
自由提问 @atomgit-bot <问题> 询问关于本 PR 的任何问题
帮助 /ai help 显示此帮助信息

免责声明

AI 助手可能存在误判,请结合自身判断。可以对评论点 👍 或 👎 帮助我们改进。

likedislike
Incorrect?
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: sig/sig-agent-runtime-java
openJiuwen-bot
openJiuwen-botCollaborators
2 days ago Comments:

欢迎来到 openJiuwen 社区

Hey @lynn45 , 感谢你对社区的贡献.

机器人使用手册

有关指令的使用,可以点击 此处 查看详情。开发人员可以在每个PR或Issue下方评论特定指令来触发机器人任务。

联系指引

有疑问可以联系 SIG: sig-agent-runtime-java ,
维护者是: @LucioIT, @fujilie ,
审核者是: @fujilie, @whylazy, @xiaoming-2026 .

likedislike
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: openJiuwen-cla/yes
openjiuwen-syncopenjiuwen-syncCollaborators
2 days ago Modify description of pull request
openJiuwen-bot
openJiuwen-botCollaborators
2 days ago Comments:
TASK STATUS DETAILS
CodeCheck ❌FAILED Click here
Blacklist Scan ✅SUCCESS Click here
AntiPoison ✅SUCCESS Click here
Software Composition Analysis ✅SUCCESS Click here
Maven Build ✅SUCCESS See CHECK tab
likedislike
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: ci-failed
atomgit-bot
atomgit-bot2 days agoStart code review1
service/agent-service-app/src/main/java/com/openjiuwen/service/app/controller/a2a/A2AAgentExecutor.java
@@ -1010,2 +1105,3 @@
1010- emitter.complete();
1105+ } catch (ReflectiveOperationException | RuntimeException failure) {
1106+ log.warn("A2A queue close failed after accepting a task outcome taskId={}", taskId, failure);
10111107 }
atomgit-bot
atomgit-bot2 days agoComments:

🟡 Medium Priority

变更点(1105-1107 行):原 catch (ReflectiveOperationException | SecurityException e) 分支在关闭事件队列失败时会 emitter.complete() 作为兜底;本次改成 catch (ReflectiveOperationException | RuntimeException failure) 后只记 warn,不再做任何收尾。

影响链路:acceptWaiting(671-681)、finishQuery(760-766)、cancelTask(1005-1009)都依赖 closeEventQueue 在确认 INPUT_REQUIRED / CANCELLED 之后关闭 emitter 的事件队列,closeEventQueue 的 javadoc 也明确写着「Closes the emitter's underlying event queue so the SSE stream terminates」。emitterEventQueue 是通过反射读取 AgentEmitter.eventQueue,closeWhenDrained 还会调用 queue.close(...),两者都可能抛异常(反射失败、SDK 版本变化、队列已关闭等),此时异常被新 catch 吞掉。

失败模式:一旦关队列失败,任务状态虽保持 INPUT_REQUIRED/CANCELLED(这是本次想修的目标),但客户端 SSE 流不再被终止,队列与其消费者线程也不再释放;若失败是系统性的(例如 SDK 内部字段变动导致反射恒失败),每次等待输入/取消都会留下一个不终止的流与队列,属于连接/资源泄漏。旧实现虽有覆盖状态的问题,但至少会终止流。

建议:关队列失败时增加不写任务状态的兜底(直接 queue.close(false,false) 或等价传输终止),避免留空操作导致 SSE 流与事件队列永不释放。

likedislike
Incorrect?
atomgit-bot
atomgit-bot2 days agoStart code review1
service/agent-service-app/src/main/java/com/openjiuwen/service/app/orchestrator/A2AEnabledServeOrchestrator.java
@@ -234,29 +248,30 @@
234248 TerminalQueryStreamObserver terminalObserver = new TerminalQueryStreamObserver(observer);
235249 ServeRequest current = request;
236250 Optional<Object> taskToken = Optional.empty();
251+ RootInputInvocation input = RootInputInvocation.none();
237252 try {
238253 taskToken = agentHandler.prepareTask(request);
254+ input = RootInputInvocation.begin(request);
255+ QueryStreamObserver observed = input.observe(observer);
256+ if (observed != observer) {
257+ terminalObserver = new TerminalQueryStreamObserver(observed);
258+ }
239259 while (!handle.isCancelled() && !terminalObserver.isCancelled()) {
240260 Optional<ServeRequest> opt = tryResumePending(current, terminalObserver);
241261 if (opt.isEmpty()) {
@@ -254,6 +274,11 @@ public class A2AEnabledServeOrchestrator implements ServeOrchestrator, A2aPushNo
254274 }
255275 current = interruptResult.get();
256276 }
277+ } catch (RuntimeException | Error failure) {
278+ if (!(failure instanceof CancellationException)) {
279+ input.failed();
280+ }
281+ throw failure;
257282 } finally {
258283 // Empty token means prepareTask acquired nothing (e.g. busy
259284 // rejection) — completeTask must not disturb the other task.
@@ -264,6 +289,7 @@ public class A2AEnabledServeOrchestrator implements ServeOrchestrator, A2aPushNo
264289 batchCoordinator.abortResume(current);
265290 } finally {
266291 streamRegistry.unregister(request.getConversationId(), handle);
292+ input.finish(handle.isCancelled());
atomgit-bot
atomgit-bot2 days agoComments:

🟡 Medium Priority

变更行(A2AEnabledServeOrchestrator.java:292)input.finish(handle.isCancelled()); 把「执行被取消」作为唯一兜底信号传入,但 RootInputInvocation.finish(RootInputInvocation.java:115-120)只在 outcome == null 时才采用 executionCancelled,即 accepted != null && executionCancelled 时取消信号被完全丢弃。

证据链:

  1. 服务端取消(cancelActive/conversationId → streamRegistry.cancel)使 handle.isCancelled() 为真;
  2. 同文件 finishAgentStream(404-422)在 handle.isCancelled() 为真时仍然执行 observer.onComplete()(line 415-420),这是取消场景下 orchestrator 自己产生的「空完成」回调;
  3. 该回调经 TerminalQueryStreamObserver 延迟到 finally 中的 completeTaskAndRelease → releaseTerminal 释放,进入 RootInputInvocation.ObservedStream.complete(RootInputInvocation.java:208-215):此时 signal 为 null(取消不是 chunk),outcome 被置为 COMPLETED;
  4. 随后第 292 行 finish(true) 因 accepted != null 直接发布 COMPLETED。

失败模式:一次真实被服务端取消的轮次被上报为 ServeRootState.COMPLETED,CANCELLED 在直连(非 deferred)流式路径几乎不可达(除 runtime 主动发出 TYPE_CANCEL chunk 外),与 A2A deferred 路径「取消优先于迟到结果」的语义(A2AAgentExecutorTest#execute_activeSyncCancellation_settlesOnlyAfterCancelIsAccepted)相反。宿主侧会据此把已取消的轮次当作正常完成(例如记账、状态机、统计口径出错),并可能丢弃真正的取消原因。

建议:不要只用 outcome 判定最终状态:在 RootInputInvocation.finish(boolean executionCancelled) 中,当 executionCancelled 为真且已记录的状态是 COMPLETED(或为空)时,以 CANCELLED 覆盖;或者在 ObservedStream 中把取消信号一路带下来(例如让 complete() 识别取消上下文),使「取消后 runtime 补发的空 onComplete」不再被当作已完成。若业务确认为「已确认结果优先于取消」,请在 finish 的 Javadoc 中明确该优先级并补充一条取消 + onComplete 的直连路径用例来固化契约。

likedislike
Incorrect?
lynn45lynn45
2 days ago Pushed 1 commits:03d70258-fix(runtime): preserve input settlement through cleanup and cancellation
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: stat/needs-squash
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Removed label: openJiuwen-cla/yes
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: openJiuwen-cla/yes
openJiuwen-bot
openJiuwen-botCollaborators
2 days ago Comments:

CLA 签署成功

lynn45, 感谢您的代码贡献。 所有的代码贡献者都已完成了 CLA 签署。 👍

likedislike
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Removed label: ci-failed
openJiuwen-bot
openJiuwen-botCollaborators
2 days ago Comments:
TASK STATUS DETAILS
CodeCheck ✅SUCCESS Click here
Blacklist Scan ✅SUCCESS Click here
AntiPoison ✅SUCCESS Click here
Software Composition Analysis ✅SUCCESS Click here
Maven Build ✅SUCCESS See CHECK tab
likedislike
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: ci-successful
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Removed label: ci-successful
openJiuwen-bot
openJiuwen-botCollaborators
2 days ago Comments:
TASK STATUS DETAILS
CodeCheck ✅SUCCESS Click here
Blacklist Scan ✅SUCCESS Click here
AntiPoison ✅SUCCESS Click here
Software Composition Analysis ✅SUCCESS Click here
Maven Build ✅SUCCESS See CHECK tab
likedislike
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: ci-successful
thesky58
thesky589 hours agoStart code review1
service/agent-service-app/src/main/java/com/openjiuwen/service/app/orchestrator/DefaultServeOrchestrator.java
@@ -89,2 +100,4 @@
100+ input.completeTask(agentHandler, taskToken);
89101 } finally {
90102 streamRegistry.unregister(conversationId, handle);
103+ input.finish(handle.isCancelled());
thesky58
thesky589 hours agoComments:

service/agent-service-app/src/main/java/com/openjiuwen/service/app/orchestrator/DefaultServeOrchestrator.java:103 这里用 input.finish(handle.isCancelled()) 结算,但上面的 catch (CancellationException) 分支调用的是原始 observer.onComplete()(第 88 行),没有经过 input.observe 包装,ObservedStream 不会记下任何 outcome;一旦 handle.isCancelled() 为 false,finish 里 state 为 null 就直接 return,ServeInputBoundary 永远收不到 finish,扩展侧会一直挂着。建议这个分支改走 observed.onComplete(),或显式 input.failed() / 按 CANCELLED 结算,保证每条路径都恰好落到一次 finish。

likedislike