Skip to content

Commit 305c127

Browse files
kabirclaude
andauthored
fix: apply historyLength to SendStreamingMessage responses (a2aproject#983)
The streaming path (onMessageSendStream) was not applying limitTaskHistory to Task events, causing historyLength to be silently ignored for streaming calls. Added the same limitTaskHistory call used in the non-streaming path to the streaming subscriber's onNext method. This fixes a2aproject#982 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1017125 commit 305c127

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎server-common/src/main/java/org/a2aproject/sdk/server/requesthandlers/DefaultRequestHandler.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public Task onGetTask(TaskQueryParams params, ServerCallContext context) throws
312312
* Limits the history of a task to the most recent N messages.
313313
*
314314
* @param task the task to limit
315-
* @param historyLength the maximum number of recent messages to keep (0 or negative = unlimited)
315+
* @param historyLength the maximum number of recent messages to keep ({@code null} = no limit, 0 = empty history)
316316
* @return the task with limited history, or the original task if no limiting needed
317317
*/
318318
private static Task limitTaskHistory(Task task, @Nullable Integer historyLength) {
@@ -736,6 +736,10 @@ public void cancel() {
736736
@Override
737737
public void onNext(StreamingEventKind item) {
738738
LOGGER.debug("onNext: {} for task {}", item.getClass().getSimpleName(), taskId.get());
739+
if (item instanceof Task task) {
740+
Integer historyLength = params.configuration() != null ? params.configuration().historyLength() : null;
741+
item = limitTaskHistory(task, historyLength);
742+
}
739743
subscriber.onNext(item);
740744
}
741745

‎tests/server-common/src/test/java/org/a2aproject/sdk/server/apps/common/AbstractA2AServerTest.java‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3193,4 +3193,79 @@ public void testSendMessageWithHistoryLengthZero() throws Exception {
31933193
}
31943194
}
31953195

3196+
@Test
3197+
@Timeout(value = 30, unit = TimeUnit.SECONDS)
3198+
public void testSendStreamingMessageWithHistoryLengthZero() throws Exception {
3199+
AtomicReference<String> taskIdRef = new AtomicReference<>();
3200+
3201+
try {
3202+
Message initialMessage = Message.builder(MESSAGE)
3203+
.parts(new TextPart("input-required:Trigger INPUT_REQUIRED"))
3204+
.build();
3205+
3206+
CountDownLatch initialLatch = new CountDownLatch(1);
3207+
getClient().sendMessage(initialMessage, List.of((event, agentCard) -> {
3208+
if (event instanceof TaskEvent te) {
3209+
taskIdRef.set(te.getTask().id());
3210+
initialLatch.countDown();
3211+
} else if (event instanceof TaskUpdateEvent tue) {
3212+
taskIdRef.set(tue.getTask().id());
3213+
if (tue.getTask().status().state() == TaskState.TASK_STATE_INPUT_REQUIRED) {
3214+
initialLatch.countDown();
3215+
}
3216+
}
3217+
}), error -> {
3218+
if (!isStreamClosedError(error)) {
3219+
initialLatch.countDown();
3220+
}
3221+
});
3222+
3223+
assertTrue(initialLatch.await(15, TimeUnit.SECONDS), "Initial streaming sendMessage should complete");
3224+
String taskId = taskIdRef.get();
3225+
assertNotNull(taskId, "Should have captured task ID");
3226+
3227+
Message followUp = Message.builder(MESSAGE)
3228+
.taskId(taskId)
3229+
.parts(new TextPart("input-required:User input"))
3230+
.build();
3231+
3232+
MessageSendParams params = MessageSendParams.builder()
3233+
.message(followUp)
3234+
.configuration(MessageSendConfiguration.builder()
3235+
.historyLength(0)
3236+
.build())
3237+
.build();
3238+
3239+
CountDownLatch followUpLatch = new CountDownLatch(1);
3240+
AtomicReference<Task> resultTaskRef = new AtomicReference<>();
3241+
getClient().sendMessage(params, List.of((BiConsumer<ClientEvent, AgentCard>) (event, agentCard) -> {
3242+
if (event instanceof TaskEvent te) {
3243+
resultTaskRef.set(te.getTask());
3244+
followUpLatch.countDown();
3245+
} else if (event instanceof TaskUpdateEvent tue) {
3246+
resultTaskRef.set(tue.getTask());
3247+
if (tue.getTask().status().state() == TaskState.TASK_STATE_COMPLETED) {
3248+
followUpLatch.countDown();
3249+
}
3250+
}
3251+
}), error -> {
3252+
if (!isStreamClosedError(error)) {
3253+
followUpLatch.countDown();
3254+
}
3255+
}, null);
3256+
3257+
assertTrue(followUpLatch.await(15, TimeUnit.SECONDS), "Follow-up streaming sendMessage should complete");
3258+
Task resultTask = resultTaskRef.get();
3259+
assertNotNull(resultTask, "Should have received a Task response");
3260+
assertTrue(resultTask.history() == null || resultTask.history().isEmpty(),
3261+
"historyLength=0 should return no history, but got " +
3262+
(resultTask.history() != null ? resultTask.history().size() : 0) + " messages");
3263+
} finally {
3264+
String taskId = taskIdRef.get();
3265+
if (taskId != null) {
3266+
deleteTaskInTaskStore(taskId);
3267+
}
3268+
}
3269+
}
3270+
31963271
}

0 commit comments

Comments
 (0)