|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.agentic.ai.agent.react; |
| 17 | + |
| 18 | +import io.github.agentic.ai.agent.AgentExecutionContext; |
| 19 | +import io.github.agentic.ai.agent.AgentEvent; |
| 20 | +import io.github.agentic.ai.agent.AgentOutcome; |
| 21 | +import io.github.agentic.ai.agent.AgentRequest; |
| 22 | +import io.github.agentic.ai.agent.AgentResult; |
| 23 | +import io.github.agentic.ai.api.chat.ChatClient; |
| 24 | +import io.github.agentic.ai.api.chat.ChatClientCapabilities; |
| 25 | +import io.github.agentic.ai.api.chat.ChatEvent; |
| 26 | +import io.github.agentic.ai.api.chat.ChatMessage; |
| 27 | +import io.github.agentic.ai.api.chat.ChatRequest; |
| 28 | +import io.github.agentic.ai.api.chat.ChatResponse; |
| 29 | +import io.github.agentic.ai.api.chat.FinishReason; |
| 30 | +import io.github.agentic.ai.api.chat.Usage; |
| 31 | +import io.github.agentic.ai.agent.result.StructuredOutputFinalizer; |
| 32 | +import io.github.agentic.ai.runtime.execution.DelegationScope; |
| 33 | +import io.github.agentic.ai.runtime.execution.ExecutionBudget; |
| 34 | +import io.github.agentic.ai.runtime.execution.PrincipalReference; |
| 35 | +import io.github.agentic.ai.runtime.execution.TenantReference; |
| 36 | +import io.github.agentic.ai.runtime.execution.TrustedExecutionContext; |
| 37 | + |
| 38 | +import java.time.Clock; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.concurrent.CompletableFuture; |
| 43 | +import java.util.concurrent.CompletionStage; |
| 44 | +import java.util.concurrent.ExecutionException; |
| 45 | +import java.util.concurrent.Flow; |
| 46 | +import java.util.concurrent.TimeUnit; |
| 47 | +import java.util.concurrent.atomic.AtomicInteger; |
| 48 | + |
| 49 | +import org.junit.jupiter.api.Test; |
| 50 | + |
| 51 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 53 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 54 | + |
| 55 | +/** |
| 56 | + * Delivery consistency of the structured output boundary in the independent ReAct |
| 57 | + * agent: whatever message actually passed validation is the message finally |
| 58 | + * delivered, for the outcome snapshot and for stream consumers alike. |
| 59 | + */ |
| 60 | +class ReActAgentStructuredDeliveryTest { |
| 61 | + |
| 62 | + private static final String SCHEMA = "{\"type\":\"object\",\"required\":[\"answer\"]," |
| 63 | + + "\"additionalProperties\":false,\"properties\":{\"answer\":{\"type\":\"string\"}}}"; |
| 64 | + |
| 65 | + private static final String REPAIRED = "{\"answer\":\"fixed\"}"; |
| 66 | + |
| 67 | + @Test |
| 68 | + void validOriginalOutputIsDeliveredUnchanged() throws Exception { |
| 69 | + AtomicInteger repairs = new AtomicInteger(); |
| 70 | + ReActAgent agent = ReActAgent.builder(new ScriptedClient("{\"answer\":\"direct\"}")) |
| 71 | + .outputSchema(SCHEMA) |
| 72 | + .outputFinalizer(new StructuredOutputFinalizer(failingRepairClient(repairs))) |
| 73 | + .build(); |
| 74 | + |
| 75 | + AgentOutcome outcome = agent.invoke(request(), context()).get(3, TimeUnit.SECONDS); |
| 76 | + |
| 77 | + assertEquals(AgentOutcome.Status.COMPLETED, outcome.status()); |
| 78 | + assertEquals("{\"answer\":\"direct\"}", outcome.messages().get(outcome.messages().size() - 1).text()); |
| 79 | + assertEquals("{\"answer\":\"direct\"}", outcome.outputs().get("structuredOutput").toString()); |
| 80 | + assertEquals(0, repairs.get()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void successfulRepairDeliversRepairedMessageAsFinal() throws Exception { |
| 85 | + ReActAgent agent = ReActAgent.builder(new ScriptedClient("not-json")) |
| 86 | + .outputSchema(SCHEMA) |
| 87 | + .outputFinalizer(new StructuredOutputFinalizer(scriptedRepairClient(REPAIRED))) |
| 88 | + .build(); |
| 89 | + |
| 90 | + AgentOutcome outcome = agent.invoke(request(), context()).get(3, TimeUnit.SECONDS); |
| 91 | + |
| 92 | + assertEquals(AgentOutcome.Status.COMPLETED, outcome.status()); |
| 93 | + // The repaired message is the final delivered message, agreeing with the data. |
| 94 | + assertEquals(REPAIRED, outcome.messages().get(outcome.messages().size() - 1).text()); |
| 95 | + assertEquals(REPAIRED, outcome.outputs().get("structuredOutput").toString()); |
| 96 | + // The failed original stays in history right before it, not silently dropped. |
| 97 | + assertEquals("not-json", outcome.messages().get(outcome.messages().size() - 2).text()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void repairStillInvalidKeepsFailureStateDistinct() throws Exception { |
| 102 | + ReActAgent agent = ReActAgent.builder(new ScriptedClient("not-json")) |
| 103 | + .outputSchema(SCHEMA) |
| 104 | + .outputFinalizer(new StructuredOutputFinalizer(scriptedRepairClient("{\"wrong\":\"shape\"}"))) |
| 105 | + .build(); |
| 106 | + |
| 107 | + ExecutionException failure = assertThrows(ExecutionException.class, |
| 108 | + () -> agent.invoke(request(), context()).get(3, TimeUnit.SECONDS)); |
| 109 | + |
| 110 | + assertTrue(failure.getCause() instanceof AgentExecutionException); |
| 111 | + AgentExecutionException delivery = (AgentExecutionException) failure.getCause(); |
| 112 | + assertEquals(AgentResult.Status.VALIDATION_FAILED, delivery.result().status()); |
| 113 | + assertTrue(delivery.result().issues().stream().anyMatch( |
| 114 | + issue -> AgentResult.ERROR_CODE_REPAIRED_STILL_INVALID.equals(issue.errorCode()))); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void repairClientFailureMarksRepairCallFailed() throws Exception { |
| 119 | + ReActAgent agent = ReActAgent.builder(new ScriptedClient("not-json")) |
| 120 | + .outputSchema(SCHEMA) |
| 121 | + .outputFinalizer(new StructuredOutputFinalizer(brokenRepairClient())) |
| 122 | + .build(); |
| 123 | + |
| 124 | + ExecutionException failure = assertThrows(ExecutionException.class, |
| 125 | + () -> agent.invoke(request(), context()).get(3, TimeUnit.SECONDS)); |
| 126 | + |
| 127 | + assertTrue(failure.getCause() instanceof AgentExecutionException); |
| 128 | + AgentExecutionException delivery = (AgentExecutionException) failure.getCause(); |
| 129 | + assertEquals(AgentResult.Status.VALIDATION_FAILED, delivery.result().status()); |
| 130 | + assertTrue(delivery.result().issues().stream().anyMatch( |
| 131 | + issue -> AgentResult.ERROR_CODE_REPAIR_CALL_FAILED.equals(issue.errorCode()))); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void streamingCompletionExposesRepairedFinalMessage() throws Exception { |
| 136 | + ReActAgent agent = ReActAgent.builder(new StreamingClient("not-json")) |
| 137 | + .streamModel(true) |
| 138 | + .outputSchema(SCHEMA) |
| 139 | + .outputFinalizer(new StructuredOutputFinalizer(scriptedRepairClient(REPAIRED))) |
| 140 | + .build(); |
| 141 | + List<AgentEvent> events = new ArrayList<>(); |
| 142 | + CompletableFuture<AgentOutcome> finished = new CompletableFuture<>(); |
| 143 | + try (AgentExecutionContext context = context()) { |
| 144 | + agent.stream(request(), context).subscribe(new Flow.Subscriber<>() { |
| 145 | + public void onSubscribe(Flow.Subscription subscription) { |
| 146 | + subscription.request(Long.MAX_VALUE); |
| 147 | + } |
| 148 | + |
| 149 | + public void onNext(AgentEvent event) { |
| 150 | + events.add(event); |
| 151 | + if (event instanceof AgentEvent.Finished done) { |
| 152 | + finished.complete(done.outcome()); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public void onError(Throwable error) { |
| 157 | + finished.completeExceptionally(error); |
| 158 | + } |
| 159 | + |
| 160 | + public void onComplete() { |
| 161 | + } |
| 162 | + }); |
| 163 | + AgentOutcome outcome = finished.get(3, TimeUnit.SECONDS); |
| 164 | + assertEquals(REPAIRED, outcome.messages().get(outcome.messages().size() - 1).text()); |
| 165 | + assertEquals(REPAIRED, outcome.outputs().get("structuredOutput").toString()); |
| 166 | + } |
| 167 | + // The repaired message reaches stream consumers before the finished snapshot, |
| 168 | + // through the existing complete-message event contract. |
| 169 | + int repairIndex = -1; |
| 170 | + int finishIndex = -1; |
| 171 | + for (int index = 0; index < events.size(); index++) { |
| 172 | + AgentEvent event = events.get(index); |
| 173 | + if (event instanceof AgentEvent.Message message && "model-repair".equals(message.messageId())) { |
| 174 | + repairIndex = index; |
| 175 | + assertEquals(REPAIRED, message.message().text()); |
| 176 | + } |
| 177 | + if (event instanceof AgentEvent.Finished) { |
| 178 | + finishIndex = index; |
| 179 | + } |
| 180 | + } |
| 181 | + assertTrue(repairIndex >= 0, "stream consumers must see the repaired final message"); |
| 182 | + assertTrue(finishIndex > repairIndex, "the repaired message must precede the finished event"); |
| 183 | + } |
| 184 | + |
| 185 | + private static AgentRequest request() { |
| 186 | + return AgentRequest.messages(List.of(ChatMessage.user("hello"))); |
| 187 | + } |
| 188 | + |
| 189 | + private static AgentExecutionContext context() { |
| 190 | + TrustedExecutionContext trusted = TrustedExecutionContext.builder() |
| 191 | + .tenant(new TenantReference("test", "tenant")) |
| 192 | + .principal(new PrincipalReference("test", "principal")) |
| 193 | + .runId("run").correlationId("correlation").budget(ExecutionBudget.unlimited()) |
| 194 | + .delegationScope(DelegationScope.none()).build(); |
| 195 | + return new AgentExecutionContext(trusted, Clock.systemUTC()); |
| 196 | + } |
| 197 | + |
| 198 | + private static ChatResponse response(ChatMessage message) { |
| 199 | + return new ChatResponse(null, "test", List.of(message), List.of(), FinishReason.STOP, new Usage(1L, 1L, 1L), |
| 200 | + Map.of()); |
| 201 | + } |
| 202 | + |
| 203 | + private static ChatClient scriptedRepairClient(String json) { |
| 204 | + return new ChatClient() { |
| 205 | + public CompletionStage<ChatResponse> call(ChatRequest request) { |
| 206 | + return CompletableFuture.completedFuture(response(ChatMessage.assistant(json))); |
| 207 | + } |
| 208 | + |
| 209 | + public Flow.Publisher<ChatEvent> stream(ChatRequest request) { |
| 210 | + throw new UnsupportedOperationException(); |
| 211 | + } |
| 212 | + |
| 213 | + public ChatClientCapabilities capabilities() { |
| 214 | + return ChatClientCapabilities.basic(); |
| 215 | + } |
| 216 | + }; |
| 217 | + } |
| 218 | + |
| 219 | + private static ChatClient brokenRepairClient() { |
| 220 | + return new ChatClient() { |
| 221 | + public CompletionStage<ChatResponse> call(ChatRequest request) { |
| 222 | + return CompletableFuture.failedFuture(new IllegalStateException("repair backend down")); |
| 223 | + } |
| 224 | + |
| 225 | + public Flow.Publisher<ChatEvent> stream(ChatRequest request) { |
| 226 | + throw new UnsupportedOperationException(); |
| 227 | + } |
| 228 | + |
| 229 | + public ChatClientCapabilities capabilities() { |
| 230 | + return ChatClientCapabilities.basic(); |
| 231 | + } |
| 232 | + }; |
| 233 | + } |
| 234 | + |
| 235 | + private static ChatClient failingRepairClient(AtomicInteger calls) { |
| 236 | + return new ChatClient() { |
| 237 | + public CompletionStage<ChatResponse> call(ChatRequest request) { |
| 238 | + calls.incrementAndGet(); |
| 239 | + throw new AssertionError("the repair client must not be called"); |
| 240 | + } |
| 241 | + |
| 242 | + public Flow.Publisher<ChatEvent> stream(ChatRequest request) { |
| 243 | + throw new UnsupportedOperationException(); |
| 244 | + } |
| 245 | + |
| 246 | + public ChatClientCapabilities capabilities() { |
| 247 | + return ChatClientCapabilities.basic(); |
| 248 | + } |
| 249 | + }; |
| 250 | + } |
| 251 | + |
| 252 | + private static class ScriptedClient implements ChatClient { |
| 253 | + |
| 254 | + private final ChatResponse reply; |
| 255 | + |
| 256 | + ScriptedClient(String terminalText) { |
| 257 | + this.reply = response(ChatMessage.assistant(terminalText)); |
| 258 | + } |
| 259 | + |
| 260 | + public CompletionStage<ChatResponse> call(ChatRequest request) { |
| 261 | + return CompletableFuture.completedFuture(reply); |
| 262 | + } |
| 263 | + |
| 264 | + public Flow.Publisher<ChatEvent> stream(ChatRequest request) { |
| 265 | + throw new UnsupportedOperationException(); |
| 266 | + } |
| 267 | + |
| 268 | + public ChatClientCapabilities capabilities() { |
| 269 | + return ChatClientCapabilities.basic(); |
| 270 | + } |
| 271 | + |
| 272 | + } |
| 273 | + |
| 274 | + private static final class StreamingClient extends ScriptedClient { |
| 275 | + |
| 276 | + StreamingClient(String terminalText) { |
| 277 | + super(terminalText); |
| 278 | + } |
| 279 | + |
| 280 | + public Flow.Publisher<ChatEvent> stream(ChatRequest request) { |
| 281 | + return subscriber -> subscriber.onSubscribe(new Flow.Subscription() { |
| 282 | + public void request(long demand) { |
| 283 | + subscriber.onNext(ChatEvent.textDelta("not-json")); |
| 284 | + subscriber.onNext(ChatEvent.response(response(ChatMessage.assistant("not-json")))); |
| 285 | + subscriber.onComplete(); |
| 286 | + } |
| 287 | + |
| 288 | + public void cancel() { |
| 289 | + } |
| 290 | + }); |
| 291 | + } |
| 292 | + |
| 293 | + } |
| 294 | + |
| 295 | +} |
0 commit comments