1919import io .github .agentic .ai .agent .AgentExecutionContext ;
2020import io .github .agentic .ai .agent .AgentOutcome ;
2121import io .github .agentic .ai .agent .AgentRequest ;
22+ import io .github .agentic .ai .agent .AgentResult ;
2223import io .github .agentic .ai .agent .result .StructuredOutputFinalizer ;
2324import io .github .agentic .ai .api .chat .ChatClient ;
2425import io .github .agentic .ai .api .chat .ChatClientCapabilities ;
@@ -94,22 +95,36 @@ void repairedRunDeliversUsageInclusiveOfRepairCall(boolean streamModel) throws E
9495 }
9596
9697 /**
97- * 验证修复客户端在响应前失败时,只保留主调用已报告的用量,不猜测修复消耗。
98+ * 验证修复客户端在响应前失败时,交付总量保持未知:没有响应不能证明修复消耗为零,
99+ * 事件视图与异常携带的结果都不得退回修复前的 (1,2,3)。
98100 */
99101 @ ParameterizedTest (name = "streamModel={0}" )
100102 @ ValueSource (booleans = { false , true })
101- void failedRepairKeepsOnlyReportedUsage (boolean streamModel ) throws Exception {
103+ void failedRepairKeepsDeliveryTotalUnknown (boolean streamModel ) throws Exception {
102104 final RepairClient repair = RepairClient .failing (new IllegalStateException ("repair backend down" ));
103- final UsageView view = new UsageView ();
104- final ReActAgent agent = agent (new InvalidJsonClient (streamModel ), repair , streamModel );
105- try (AgentExecutionContext context = context ("failed-repair" )) {
106- agent .stream (AgentRequest .messages (List .of (ChatMessage .user ("give me the status" ))), context )
107- .subscribe (view );
108- assertThrows (ExecutionException .class , () -> view .completed .get (5 , TimeUnit .SECONDS ));
109- }
105+ final UsageView view = runExpectingFailure (streamModel , repair );
110106
111107 assertAll (() -> assertNull (view .outcome , "A failed repair must not publish a successful outcome" ),
112- () -> assertEquals (MAIN , view .visibleUsage , "Only the main call's reported usage stays visible" ));
108+ () -> assertEquals (Usage .unknown (), view .visibleUsage ,
109+ "No response proves the repair consumption, so the visible total stays unknown" ));
110+ }
111+
112+ /**
113+ * 验证修复响应带用量但输出仍不合法时,合并用量先于失败传播:事件消费者的最后一个
114+ * UsageUpdate 与异常携带的 result.usage 一致(11/22/33),不停在修复前的 (1,2,3)。
115+ */
116+ @ ParameterizedTest (name = "streamModel={0}" )
117+ @ ValueSource (booleans = { false , true })
118+ void repairedButInvalidRunPublishesMergedUsageBeforeFailing (boolean streamModel ) throws Exception {
119+ final RepairClient repair = RepairClient .replying ("still not json" , REPAIR );
120+ final UsageView view = runExpectingFailure (streamModel , repair );
121+
122+ final AgentExecutionException failure = (AgentExecutionException ) view .error ;
123+ assertAll (() -> assertEquals (TOTAL , view .visibleUsage ,
124+ "The merged usage must be published before the failure propagates" ),
125+ () -> assertEquals (TOTAL , failure .result ().usage (), "The thrown result carries the merged total" ),
126+ () -> assertEquals (REPAIR , failure .result ().repairUsage ()),
127+ () -> assertEquals (AgentResult .Status .VALIDATION_FAILED , failure .result ().status ()));
113128 }
114129
115130 /** 运行单轮无效输出 + 一次修复的完整调用,直到事件流完成。 */
@@ -124,6 +139,18 @@ private UsageView run(boolean streamModel, RepairClient repair) throws Exception
124139 return view ;
125140 }
126141
142+ /** 运行到以失败终结的事件流,返回已记录失败原因的视图。 */
143+ private UsageView runExpectingFailure (boolean streamModel , RepairClient repair ) throws Exception {
144+ final UsageView view = new UsageView ();
145+ final ReActAgent agent = agent (new InvalidJsonClient (streamModel ), repair , streamModel );
146+ try (AgentExecutionContext context = context ("repair-usage-test" )) {
147+ agent .stream (AgentRequest .messages (List .of (ChatMessage .user ("give me the status" ))), context )
148+ .subscribe (view );
149+ assertThrows (ExecutionException .class , () -> view .completed .get (5 , TimeUnit .SECONDS ));
150+ }
151+ return view ;
152+ }
153+
127154 private ReActAgent agent (ChatClient mainModel , RepairClient repair , boolean streamModel ) {
128155 return ReActAgent .builder (mainModel )
129156 .streamModel (streamModel )
@@ -251,7 +278,7 @@ public ChatClientCapabilities capabilities() {
251278
252279 }
253280
254- /** 按事件契约维护可见用量与最终 outcome 的消费者视图。 */
281+ /** 按事件契约维护可见用量与最终 outcome 的消费者视图,失败时记录原因 。 */
255282 private static final class UsageView implements Flow .Subscriber <AgentEvent > {
256283
257284 private final CompletableFuture <Void > completed = new CompletableFuture <>();
@@ -262,6 +289,8 @@ private static final class UsageView implements Flow.Subscriber<AgentEvent> {
262289
263290 private String deliveredText ;
264291
292+ private Throwable error ;
293+
265294 @ Override
266295 public void onSubscribe (Flow .Subscription subscription ) {
267296 subscription .request (Long .MAX_VALUE );
@@ -282,8 +311,9 @@ else if (event instanceof AgentEvent.Finished finished) {
282311 }
283312
284313 @ Override
285- public void onError (Throwable error ) {
286- completed .completeExceptionally (error );
314+ public void onError (Throwable failure ) {
315+ this .error = failure ;
316+ completed .completeExceptionally (failure );
287317 }
288318
289319 @ Override
0 commit comments