avichaym commented on code in PR #677: URL: https://github.com/apache/flink-agents/pull/677#discussion_r3247597774
########## e2e-test/flink-agents-end-to-end-tests-integration/src/test/java/org/apache/flink/agents/integration/test/EventKryoSerializationTest.java: ########## @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.flink.agents.integration.test; + +import org.apache.flink.agents.api.Event; +import org.apache.flink.agents.api.chat.messages.ChatMessage; +import org.apache.flink.agents.api.chat.messages.MessageRole; +import org.apache.flink.agents.api.event.ChatRequestEvent; +import org.apache.flink.agents.api.event.ContextRetrievalResponseEvent; +import org.apache.flink.agents.api.event.ToolRequestEvent; +import org.apache.flink.agents.api.event.ToolResponseEvent; +import org.apache.flink.agents.api.tools.ToolResponse; +import org.apache.flink.agents.api.vectorstores.Document; +import org.apache.flink.api.common.serialization.SerializerConfigImpl; +import org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer; +import org.apache.flink.core.memory.DataInputDeserializer; +import org.apache.flink.core.memory.DataOutputSerializer; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * Verifies that the Event classes which carry collection-typed payloads remain Kryo-friendly when + * constructed with immutable collections (List.of(...), Map.of(...)). + * + * <p>On JDK 17+, Flink's Kryo serializer cannot reflectively access internal fields of {@code + * java.util.ImmutableCollections} subclasses without {@code --add-opens + * java.base/java.util=ALL-UNNAMED}. Surefire in this build adds that flag, which papers over the + * production failure mode in tests, so a pure round-trip assertion would not actually catch the + * regression. To guard the contract directly we therefore assert that the stored collections are + * mutable (i.e. defensive-copied to ArrayList / HashMap) — adding to the returned collection must + * not throw {@link UnsupportedOperationException}, which is what would happen if the immutable + * input was stored by reference. The Kryo round-trip is kept as additional coverage for payload + * integrity. + * + * <p>This module is wired into CI against Flink 1.20 / 2.0 / 2.1 / 2.2 on JDK 17+, so a regression + * here would fail across every supported Flink version. + */ +public class EventKryoSerializationTest { + + @Test + void chatRequestEventDefensiveCopiesImmutableList() throws Exception { + ChatRequestEvent event = + new ChatRequestEvent( + "myModel", List.of(new ChatMessage(MessageRole.USER, "hello"))); + + // Stored list must be mutable; the immutable List.of(...) input must have been copied. + assertDoesNotThrow( + () -> event.getMessages().add(new ChatMessage(MessageRole.USER, "world")), + "ChatRequestEvent must defensive-copy its messages list"); + + ChatRequestEvent roundTripped = roundTrip(event, ChatRequestEvent.class); + assertEquals("myModel", roundTripped.getModel()); + assertNotNull(roundTripped.getMessages()); + assertEquals(2, roundTripped.getMessages().size()); + } + + @Test + void contextRetrievalResponseEventDefensiveCopiesImmutableList() throws Exception { + UUID requestId = UUID.randomUUID(); + ContextRetrievalResponseEvent event = + new ContextRetrievalResponseEvent( + requestId, + "what is flink agents?", + List.of( + new Document( + "Apache Flink Agents is a streaming agent framework.", + Map.of(), + "doc-1"))); + + assertDoesNotThrow( + () -> event.getDocuments().add(new Document("extra", Map.of(), "doc-2")), + "ContextRetrievalResponseEvent must defensive-copy its documents list"); + + ContextRetrievalResponseEvent roundTripped = + roundTrip(event, ContextRetrievalResponseEvent.class); + assertEquals(requestId, roundTripped.getRequestId()); + assertEquals("what is flink agents?", roundTripped.getQuery()); + assertEquals(2, roundTripped.getDocuments().size()); + } + + @Test + void toolRequestEventDefensiveCopiesImmutableList() throws Exception { + ToolRequestEvent event = + new ToolRequestEvent( + "myModel", + List.of( + Map.of( + "id", "call_1", + "name", "myTool", + "arguments", Map.of("x", 1)))); Review Comment: Agreed, option 2 makes more sense. Updated to revert the Tool event changes and keeping defensive copies only on ChatRequestEvent and ContextRetrievalResponseEvent. Test trimmed to those two cases. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
