Skip to content

Commit bbf03e4

Browse files
committed
fix: Event consumer should stop on input_required
1 parent 1277f24 commit bbf03e4

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎server-common/src/main/java/io/a2a/server/events/EventConsumer.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.a2a.spec.Event;
77
import io.a2a.spec.Message;
88
import io.a2a.spec.Task;
9+
import io.a2a.spec.TaskState;
910
import io.a2a.spec.TaskStatusUpdateEvent;
1011
import mutiny.zero.BackpressureStrategy;
1112
import mutiny.zero.TubeConfiguration;
@@ -77,7 +78,7 @@ public Flow.Publisher<EventQueueItem> consumeAll() {
7778
} else if (event instanceof Message) {
7879
isFinalEvent = true;
7980
} else if (event instanceof Task task) {
80-
isFinalEvent = task.status().state().isFinal();
81+
isFinalEvent = isFinalOrInterruptedTask(task);
8182
} else if (event instanceof QueueClosedEvent) {
8283
// Poison pill event - signals queue closure from remote node
8384
// Do NOT send to subscribers - just close the queue
@@ -120,6 +121,10 @@ public Flow.Publisher<EventQueueItem> consumeAll() {
120121
});
121122
}
122123

124+
private boolean isFinalOrInterruptedTask(Task task) {
125+
return task.status().state().isFinal() || task.status().state() == TaskState.INPUT_REQUIRED;
126+
}
127+
123128
public EnhancedRunnable.DoneCallback createAgentRunnableDoneCallback() {
124129
return agentRunnable -> {
125130
if (agentRunnable.getError() != null) {

‎server-common/src/test/java/io/a2a/server/events/EventConsumerTest.java‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ public void onSubscribe(Flow.Subscription subscription) {
188188
public void onNext(EventQueueItem item) {
189189
receivedEvents.add(item.getEvent());
190190
subscription.request(1);
191-
192191
}
193192

194193
@Override
@@ -237,7 +236,6 @@ public void onSubscribe(Flow.Subscription subscription) {
237236
public void onNext(EventQueueItem item) {
238237
receivedEvents.add(item.getEvent());
239238
subscription.request(1);
240-
241239
}
242240

243241
@Override
@@ -257,6 +255,69 @@ public void onComplete() {
257255
assertSame(message, receivedEvents.get(0));
258256
}
259257

258+
@Test
259+
public void testConsumeTaskInputRequired() throws Exception {
260+
Task task = Task.builder()
261+
.id("task-id")
262+
.contextId("task-context")
263+
.status(new TaskStatus(TaskState.INPUT_REQUIRED))
264+
.build();
265+
List<Event> events = List.of(
266+
task,
267+
TaskArtifactUpdateEvent.builder()
268+
.taskId("task-123")
269+
.contextId("session-xyz")
270+
.artifact(Artifact.builder()
271+
.artifactId("11")
272+
.parts(new TextPart("text"))
273+
.build())
274+
.build(),
275+
TaskStatusUpdateEvent.builder()
276+
.taskId("task-123")
277+
.contextId("session-xyz")
278+
.status(new TaskStatus(TaskState.WORKING))
279+
.isFinal(true)
280+
.build());
281+
for (Event event : events) {
282+
eventQueue.enqueueEvent(event);
283+
}
284+
285+
Flow.Publisher<EventQueueItem> publisher = eventConsumer.consumeAll();
286+
final List<Event> receivedEvents = new ArrayList<>();
287+
final AtomicReference<Throwable> error = new AtomicReference<>();
288+
289+
publisher.subscribe(new Flow.Subscriber<>() {
290+
private Flow.Subscription subscription;
291+
292+
@Override
293+
public void onSubscribe(Flow.Subscription subscription) {
294+
this.subscription = subscription;
295+
subscription.request(1);
296+
}
297+
298+
@Override
299+
public void onNext(EventQueueItem item) {
300+
receivedEvents.add(item.getEvent());
301+
subscription.request(1);
302+
}
303+
304+
@Override
305+
public void onError(Throwable throwable) {
306+
error.set(throwable);
307+
}
308+
309+
@Override
310+
public void onComplete() {
311+
subscription.cancel();
312+
}
313+
});
314+
315+
assertNull(error.get());
316+
// The stream is closed after the input_required task
317+
assertEquals(1, receivedEvents.size());
318+
assertSame(task, receivedEvents.get(0));
319+
}
320+
260321
@Test
261322
public void testCreateAgentRunnableDoneCallbackSetsError() {
262323
EnhancedRunnable mockRunnable = new EnhancedRunnable() {

0 commit comments

Comments
 (0)