@@ -307,18 +307,18 @@ public EventKind onMessageSend(MessageSendParams params, ServerCallContext conte
307307
308308 // Step 4: Fetch the final task state from TaskStore (all events have been processed)
309309 Task updatedTask = taskStore .get (taskId );
310- if (updatedTask != null ) {
311- if (failed && !updatedTask .getStatus ().state ().isFinal ()) {
312- // If we had exceptions above, update the Task to reflect that
313- Message msg = new Message .Builder ()
314- .role (Message .Role .AGENT )
310+ Message errorMessage = failed ?
311+ new Message .Builder ()
312+ .role (Message .Role .AGENT ) // System seems more accurate for this
315313 .parts (Collections .singletonList (new TextPart (error )))
316314 .contextId (mss .requestContext .getContextId ())
317315 .taskId (taskId )
318- .build ();
319-
316+ .build () : null ;
317+ if (updatedTask != null ) {
318+ if (failed && !updatedTask .getStatus ().state ().isFinal ()) {
319+ // If we had exceptions above, update the Task to reflect that
320320 updatedTask = new Task .Builder (updatedTask )
321- .status (new TaskStatus (TaskState .FAILED , msg , null ))
321+ .status (new TaskStatus (TaskState .FAILED , errorMessage , null ))
322322 .build ();
323323 taskStore .save (updatedTask );
324324 }
@@ -328,6 +328,11 @@ public EventKind onMessageSend(MessageSendParams params, ServerCallContext conte
328328 taskId , updatedTask .getStatus ().state (),
329329 updatedTask .getArtifacts ().size ());
330330 }
331+
332+ } else if (failed ) {
333+ // No Task but return an error to the user
334+ LOGGER .warn ("Task {} not found in store, but a failure occurred: {}" , taskId , error );
335+ kind = errorMessage ; // Return the error message as the EventKind
331336 }
332337 }
333338 if (kind instanceof Task taskResult && !taskId .equals (taskResult .getId ())) {
0 commit comments