Skip to content

Commit 548a9e5

Browse files
fix(agent): keep repair-failed totals unknown and publish merged usage before failure (#207)
1 parent cac7ae3 commit 548a9e5

4 files changed

Lines changed: 60 additions & 29 deletions

File tree

‎argi-agent/argi-agent-core/src/main/java/io/github/agentic/ai/agent/react/ReActAgent.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -694,17 +694,22 @@ private void finish(List<ChatMessage> messages, Map<String, Object> state, Usage
694694
ChatMessage terminal, String terminalMessageId) {
695695
Map<String, Object> outputs = new LinkedHashMap<>(state);
696696
Usage deliveredUsage = usage;
697-
Usage repairUsage = null;
698697
if (outputSchema != null && outputFinalizer != null) {
699698
AgentResult result = outputFinalizer.finalizeOutput(terminal, outputSchema, usage, context.control());
699+
if (result.repairUsage() != null) {
700+
// A repair re-ask happened after the last per-turn update: publish
701+
// the delivery-inclusive cumulative usage before anything consumes
702+
// or propagates the result, so UsageUpdate consumers and the result
703+
// — delivered or thrown — always agree on the same totals.
704+
emit(new AgentEvent.UsageUpdate(result.usage()));
705+
}
700706
if (result.status() != AgentResult.Status.COMPLETED) {
701707
throw new AgentExecutionException(result);
702708
}
703709
// The finalizer's usage is the delivery total: a repair re-ask spent
704710
// tokens after the last model turn, and the finished snapshot must carry
705711
// the same total the result reports instead of the pre-repair one.
706712
deliveredUsage = result.usage();
707-
repairUsage = result.repairUsage();
708713
// A successful repair replaces the delivered terminal: the message that
709714
// actually passed validation becomes the final message while the failed
710715
// original stays in history, and stream consumers see it through the
@@ -721,12 +726,6 @@ private void finish(List<ChatMessage> messages, Map<String, Object> state, Usage
721726
outputs.put("structuredOutput", result.data());
722727
}
723728
}
724-
if (repairUsage != null) {
725-
// A repair re-ask happened after the last per-turn update: publish the
726-
// delivery-inclusive cumulative usage so a UsageUpdate consumer sees the
727-
// repair spend before the finished snapshot agrees with it.
728-
emit(new AgentEvent.UsageUpdate(deliveredUsage));
729-
}
730729
emit(new AgentEvent.Finished(AgentOutcome.completed(messages, outputs, deliveredUsage)));
731730
}
732731

‎argi-agent/argi-agent-core/src/main/java/io/github/agentic/ai/agent/result/StructuredOutputFinalizer.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,10 @@ public AgentResult finalizeOutput(ChatMessage terminal, String outputSchema, Usa
184184
if (repair == null) {
185185
// The repair re-ask could not be performed at all: keep the original failure
186186
// and mark the distinction from a repair that ran but stayed invalid. The
187-
// call was issued yet nothing about its consumption is known, so the segment
188-
// is present but unknown and the main usage stays as reported.
189-
return AgentResult.validationFailed(first.message(), usage, Usage.unknown(),
187+
// call was issued yet no response proves what it consumed, so neither the
188+
// segment nor the delivery total may claim it spent nothing: both stay
189+
// unknown instead of the pre-repair usage standing in as an exact total.
190+
return AgentResult.validationFailed(first.message(), Usage.unknown(), Usage.unknown(),
190191
repairCallFailedIssues(first.issues()));
191192
}
192193
// The repair re-ask is part of the delivery: its reported usage folds into the

‎argi-agent/argi-agent-core/src/test/java/io/github/agentic/ai/agent/react/ReActAgentRepairUsageTest.java‎

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.github.agentic.ai.agent.AgentExecutionContext;
2020
import io.github.agentic.ai.agent.AgentOutcome;
2121
import io.github.agentic.ai.agent.AgentRequest;
22+
import io.github.agentic.ai.agent.AgentResult;
2223
import io.github.agentic.ai.agent.result.StructuredOutputFinalizer;
2324
import io.github.agentic.ai.api.chat.ChatClient;
2425
import 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

‎argi-agent/argi-agent-core/src/test/java/io/github/agentic/ai/agent/result/StructuredOutputFinalizerTest.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,18 @@ void repairResponseWithoutAssistantMessageCountsUsage() {
216216
}
217217

218218
@Test
219-
@DisplayName("a repair client that fails before any response keeps only the known usage")
220-
void repairClientFailureBeforeResponseKeepsKnownUsageOnly() {
219+
@DisplayName("a repair client that fails before any response keeps the delivery total unknown")
220+
void repairClientFailureBeforeResponseKeepsTotalUnknown() {
221221
ScriptedRepairClient client = ScriptedRepairClient.failing(new IllegalStateException("repair backend down"));
222222
StructuredOutputFinalizer repairing = new StructuredOutputFinalizer(client);
223223

224224
AgentResult result = repairing.finalizeOutput(ChatMessage.assistant("not json"), SCHEMA, new Usage(1L, 2L, 3L));
225225

226226
assertThat(result.status()).isEqualTo(AgentResult.Status.VALIDATION_FAILED);
227-
assertThat(result.usage()).isEqualTo(new Usage(1L, 2L, 3L));
228-
// The re-ask was issued but nothing about its consumption is known: the segment
229-
// is present and unknown rather than fabricated as zero or absent.
227+
// No response proves what the issued repair consumed, so neither the segment
228+
// nor the total may claim it spent zero: both stay unknown instead of
229+
// fabricating the pre-repair (1,2,3) as an exact delivery total.
230+
assertThat(result.usage()).isEqualTo(Usage.unknown());
230231
assertThat(result.repairUsage()).isEqualTo(Usage.unknown());
231232
}
232233

0 commit comments

Comments
 (0)