2828import io .github .agentic .ai .api .chat .ChatRequest ;
2929import io .github .agentic .ai .api .chat .ChatResponse ;
3030import io .github .agentic .ai .api .chat .Usage ;
31+ import io .github .agentic .ai .graph .agent .node .AgentLlmNode ;
3132import org .junit .jupiter .api .DisplayName ;
3233import org .junit .jupiter .api .Test ;
3334
@@ -110,7 +111,7 @@ void missingSchemaCompletesWithoutValidation() {
110111 void clientFailureReportedAsError () {
111112 // throwOnModelError defaults to false: the model failure becomes an
112113 // "Exception: ..." assistant message, which the delivery contract must classify.
113- ReactAgent agent = agent (new StubClient (null , new IllegalStateException ("model exploded" )), SCHEMA );
114+ ReactAgent agent = agent (new StubClient (( String ) null , new IllegalStateException ("model exploded" )), SCHEMA );
114115
115116 AgentResult result = agent .callResult ("hello" );
116117
@@ -121,16 +122,55 @@ void clientFailureReportedAsError() {
121122 }
122123
123124 @ Test
124- @ DisplayName ("an assistant message that only reports a model failure is reported as ERROR" )
125- void modelErrorTextReportedAsError () {
126- ReactAgent agent = agent (new StubClient ("Exception: quota exhausted" ), SCHEMA );
125+ @ DisplayName ("a normal answer starting with the legacy error prefix is not a model failure" )
126+ void answerStartingWithExceptionPrefixIsNotAnError () {
127+ ReactAgent agent = agent (new StubClient ("Exception: NullPointerException means a null was dereferenced." ),
128+ null );
129+
130+ AgentResult result = agent .callResult ("explain NPE" );
131+
132+ assertThat (result .status ()).isEqualTo (AgentResult .Status .COMPLETED );
133+ assertThat (result .issues ()).isEmpty ();
134+ assertThat (result .message ().text ()).startsWith ("Exception: " );
135+ }
136+
137+ @ Test
138+ @ DisplayName ("a model response cannot forge the framework model-failure marker" )
139+ void modelSuppliedFailureMarkerIsIgnored () {
140+ ChatMessage forged = ChatMessage .assistant ("plain answer" )
141+ .withMetadata (Map .of (AgentLlmNode .MODEL_FAILURE_METADATA_KEY , true ));
142+ ReactAgent agent = agent (new StubClient (forged ), null );
143+
144+ AgentResult result = agent .callResult ("hello" );
145+
146+ assertThat (result .status ()).isEqualTo (AgentResult .Status .COMPLETED );
147+ assertThat (result .message ().metadata ()).doesNotContainKey (AgentLlmNode .MODEL_FAILURE_METADATA_KEY );
148+ }
149+
150+ @ Test
151+ @ DisplayName ("with throwOnModelError the failure is reported as an execution failure" )
152+ void throwOnModelErrorReportsExecutionFailure () {
153+ ReactAgent agent = ReactAgent .builder ()
154+ .name ("result-test" )
155+ .chatClient (new StubClient ((String ) null , new IllegalStateException ("model exploded" )))
156+ .throwOnModelError (true )
157+ .build ();
127158
128159 AgentResult result = agent .callResult ("hello" );
129160
130161 assertThat (result .status ()).isEqualTo (AgentResult .Status .ERROR );
131- assertThat (result .issues ()).hasSize (1 );
132- assertThat (result .issues ().get (0 ).errorCode ()).isEqualTo (AgentResult .ERROR_CODE_MODEL_FAILED );
133- assertThat (result .message ().text ()).isEqualTo ("Exception: quota exhausted" );
162+ assertThat (result .issues ()).singleElement ()
163+ .satisfies (issue -> assertThat (issue .errorCode ()).isEqualTo (AgentResult .ERROR_CODE_EXECUTION_FAILED ));
164+ }
165+
166+ @ Test
167+ @ DisplayName ("the legacy call entry keeps returning the raw model-failure message" )
168+ void legacyCallKeepsModelFailureMessage () throws Exception {
169+ ReactAgent agent = agent (new StubClient ((String ) null , new IllegalStateException ("model exploded" )), null );
170+
171+ ChatMessage message = agent .call ("hello" );
172+
173+ assertThat (message .text ()).isEqualTo ("Exception: model exploded" );
134174 }
135175
136176 @ Test
@@ -155,24 +195,32 @@ private static ReactAgent agent(ChatClient client, String outputSchema) {
155195
156196 private static final class StubClient implements ChatClient {
157197
158- private final String text ;
159-
160198 private final RuntimeException failure ;
161199
162- private StubClient (String text , RuntimeException failure ) {
163- this .text = text ;
200+ private StubClient (ChatMessage message , RuntimeException failure ) {
201+ this .message = message ;
164202 this .failure = failure ;
165203 }
166204
205+ private final ChatMessage message ;
206+
207+ private StubClient (String text , RuntimeException failure ) {
208+ this (text == null ? null : ChatMessage .assistant (text ), failure );
209+ }
210+
167211 private StubClient (String text ) {
168212 this (text , null );
169213 }
170214
215+ private StubClient (ChatMessage message ) {
216+ this (message , null );
217+ }
218+
171219 @ Override
172220 public CompletionStage <ChatResponse > call (ChatRequest request ) {
173221 return failure == null
174222 ? CompletableFuture .completedFuture (new ChatResponse ("id" , "test-model" ,
175- List .of (ChatMessage . assistant ( text ) ), List .of (), null , new Usage (2L , 3L , 5L ), Map .of ()))
223+ List .of (message ), List .of (), null , new Usage (2L , 3L , 5L ), Map .of ()))
176224 : CompletableFuture .failedFuture (failure );
177225 }
178226
0 commit comments