agentEmitter.addArtifact(message) and agentEmitter.complete(message) trigger 2 events each
i run this scenario
Turn one: client agent sends the first message. Server Agent does
agentEmitter.Submit(message1);
agentEmitter.startWork(message2);
agentEmitter.requiresInput(message3);
the client agent receive
Once taskStatusUpdateEvent for TaskState.TASK_STATE_SUBMITTED with message1
Once taskStatusUpdateEvent for TaskState.TASK_STATE_WORKING with message2
Once taskStatusUpdateEvent for TaskState.TASK_STATE_INPUT_REQUIRED with message3
all correct so far
Turn two: client agent sends the second message with the contextId and TaskId from the first turn. Server Agent does
agentEmitter.addArtifact(Parts);
agentEmitter.complete(message4);
the client agent received
twice taskArtifactUpdateEvent with the artifact
twice taskStatusUpdateEvent for TaskState.TASK_STATE_COMPLETED with message4
the issue is that the events taskArtifactUpdateEvent and taskStatusUpdateEvent are triggered twice instead of once.
Hmm, that's strange - I'll try to take a look
@mescaja Can you please provide a reproducer? Or describe in a bit more detail what the clients do?
i.e. if there are two clients subscribing, they should both receive the events. But it is hard to say without knowing exactly what you are doing :-)
@kabir . It seems the issue only occurs when the task is set to INPUT_REQUIRED. The client agent received the TaskArtifactUpdateEvent twice
- first when the Server agent adds the artifact
- second when the Server agent completes the task
When the task goes through statuses , SUBMITTED >> WORKING >> COMPLETED, the client agent only receive the TaskArtifactUpdateEvent once
will try setup a reproducer
i solve the issue by adding this
if (messageResponse.isDone()) {
return;
}
to the consumer list. After adding the fix to the https://github.com/a2aproject/a2a-java/blob/main/examples/helloworld/client/src/main/java/org/a2aproject/sdk/examples/helloworld/client/HelloWorldClient.java, the code should looks like
// Create consumers list for handling client events
List<BiConsumer<ClientEvent, AgentCard>> consumers = new ArrayList<>();
consumers.add((event, agentCard) -> {
if (messageResponse.isDone()) {
return;
}
if (event instanceof MessageEvent messageEvent) {
Message responseMessage = messageEvent.getMessage();
StringBuilder textBuilder = new StringBuilder();
if (responseMessage.parts() != null) {
for (Part<?> part : responseMessage.parts()) {
if (part instanceof TextPart textPart) {
textBuilder.append(textPart.text());
}
}
}
messageResponse.complete(textBuilder.toString());
} else {
System.out.println("Received client event: " + event.getClass().getSimpleName());
}
});
Of course the HelloWorldClient.java does not support multi turn conversation, but if it did, the fixes above would solve the issue in this sample A2A client
I think the root cause is because a brand new Client is built on every turn, the duplicate events mean the previous Client instances from turn one are never being closed.When a new client is built for turn two, it opens a fresh JSON-RPC streaming connection over HTTP (as seen by your thread logs HttpClient-14 and HttpClient-16). If the old client isn't explicitly shut down, its connection stays alive in the background. Both the old client for turn one and the new client for turn two receive the exact same broadcasted events from the remote server for that taskId.
Despite the fact I ensure that this is run at the end of each turn, still the turn one consumer is live and streaming tasks events.
messageResponse.complete(A2aMessage);
@kabir @ehsavoie, I am not sure something must be done in the SDK itself so to not having to add my fixes in the code. I leave to it. Apart from that i'm happy to close this ticket
If I understand correctly, you have several clients open and listening to streaming requests (either from subscribe or message send streaming). In this case it is expected that each open client will receive the events. We actually test for this
If I understand correctly, you have several clients open and listening to streaming requests (either from subscribe or message send streaming). In this case it is expected that each open client will receive the events. We actually test for this
there's only one client agent (the only subscriber of task events) and only one server agent. Each turn creates a new instance of the A2A client and consumers.
List<BiConsumer<ClientEvent, AgentCard>> consumers = getConsumers(messageResponse);
...
ClientBuilder clientBuilder = Client
.addConsumers(consumers)
...
Message.Builder messageBuilder = Message.builder()
...
client.sendMessage(a2aMessage);
...
messageResponse.complete(a2aCompleteMessage)
Turn one: client agent sends first A2A message with task id=null, contextid=null (A2A server agent emit/submit and new task and start working on it, set status on INPUT_REQUIRED) Turn two: client agent receives taskid and contextid, sends the second A2A message (A2A server agent completes the task)
Quarkus is stateless, each time the A2A client runs, a new instance of the consumers, client and a2aMessage are created.
the question is why the instances of consumers, client created as part of turn one call, still life during turn two.
i need to understand how i can close/cancel/kill all instances of client, consumers and messageResponse at the end of each turn
running messageResponse.complete(a2aCompleteMessage) still leave the consumers running
what i can see right now is that for as long as the task is life (status either INPUT_REQUIRED, AUTH_REQUIRED, SUBMITTED or WORKING) each turn leave consumers running at the end of it hence messages will be consumed as many times as turns have run.
On the scenarios i am running, multi-turn conversation consist of 2 turns hence same event consumed twice. I am pretty sure if i change my scenario and leave the task let's say WORKING at the end of turn two and run turn three, the artifact event as part of it will be consumed 3 times.
i just need to either programmatically kill the consumers as part of my code or the java SDK does it for me automatically at the end of each turn.
i can see that consumers are only closed/killed automatically when the task reach COMPLETION, FAILED, CANCELLED or REJECTED state.
I think you need to manage this yourself, see https://a2a-protocol.org/latest/specification/#312-send-streaming-message and https://a2a-protocol.org/latest/specification/#316-subscribe-to-task which say the stream should stay open until a terminal state is reached.
But I did add something which might help a while ago here https://github.com/a2aproject/a2a-java/pull/1008 (see the discussion on the linked issue).
Thanks @kabir i have implemented your suggestion closing the channel using ch.shutdownNow() as per your example at the end of each turn to stop the stream. It does work.