Copilot commented on code in PR #950:
URL: https://github.com/apache/flink-agents/pull/950#discussion_r3698599248
##########
api/src/main/java/org/apache/flink/agents/api/Event.java:
##########
@@ -46,25 +48,34 @@ public class Event {
/** Unified event with user-defined type and attributes. */
public Event(String type, Map<String, Object> attributes) {
- this(UUID.randomUUID(), type, attributes);
+ this(UUID.randomUUID(), type, attributes, new HashMap<>());
}
/** Unified event with user-defined type and empty attributes. */
public Event(String type) {
this(type, new HashMap<>());
}
- @JsonCreator
public Event(
@JsonProperty("id") UUID id,
@JsonProperty("type") String type,
@JsonProperty("attributes") Map<String, Object> attributes) {
+ this(id, type, attributes, new HashMap<>());
+ }
+
+ @JsonCreator
+ public Event(
+ @JsonProperty("id") UUID id,
+ @JsonProperty("type") String type,
+ @JsonProperty("attributes") Map<String, Object> attributes,
+ @JsonProperty("attachments") Map<String, Object> attachments) {
if (type == null || type.isEmpty()) {
throw new IllegalArgumentException("Event 'type' must not be null
or empty.");
}
this.id = id;
this.type = type;
this.attributes = attributes != null ? attributes : new HashMap<>();
+ this.attachments = attachments != null ? attachments : new HashMap<>();
}
Review Comment:
Event stores the provided `attachments` map instance directly. If callers
pass an immutable map (e.g., `Map.of(...)`) or a shared map, runtime code that
wraps attachments into `MemoryRef` (via
`EventAttachmentUtils.storeEventAttachments`) will fail with
`UnsupportedOperationException` or mutate caller-owned state. Copy
`attributes`/`attachments` into new mutable maps in the constructor.
##########
api/src/main/java/org/apache/flink/agents/api/Event.java:
##########
@@ -125,7 +149,16 @@ public static Event fromEvent(Event event) {
* @throws IOException if JSON parsing fails or the 'type' field is
missing or empty
*/
public static Event fromJson(String json) throws IOException {
- return MAPPER.readValue(json, Event.class);
+ Event event = MAPPER.readValue(json, Event.class);
+ for (Map.Entry<String, Object> entry :
event.getAttachments().entrySet()) {
+ Object attachment = entry.getValue();
+ if (attachment instanceof Map
+ && ((Map<?, ?>)
attachment).containsKey(MemoryRef.MEMORY_TYPE_FIELD)
+ && ((Map<?, ?>)
attachment).containsKey(MemoryRef.PATH_FIELD)) {
+ entry.setValue(MAPPER.convertValue(attachment,
MemoryRef.class));
+ }
Review Comment:
`fromJson` converts any attachment map containing `memory_type` and `path`
into a `MemoryRef`, even if the map has additional keys. That can unexpectedly
reinterpret user-provided attachment objects. Make the conversion strict (only
when the map has exactly those two keys), matching the Python side behavior.
##########
python/flink_agents/runtime/memory/event_attachment_utils.py:
##########
@@ -0,0 +1,90 @@
+################################################################################
+# 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.
+#################################################################################
+from __future__ import annotations
+
+import hashlib
+from typing import TYPE_CHECKING, Any
+
+from flink_agents.api.events.event import OutputEvent
+from flink_agents.api.memory_object import validate_memory_value
+from flink_agents.api.memory_reference import MemoryRef
+
+if TYPE_CHECKING:
+ from uuid import UUID
+
+ from flink_agents.api.events.event import Event
+ from flink_agents.api.runner_context import RunnerContext
+
+_ATTACHMENT_ROOT = "__event_attachments__"
+
+
+class EventAttachmentError(RuntimeError):
+ """Raised when an Event attachment cannot be stored or loaded."""
+
+
+def _hash_attachment_key(key: str) -> str:
+ return hashlib.sha256(key.encode("UTF-8")).hexdigest()
+
+
+def build_attachment_path(event_id: UUID, key: str) -> str:
+ """Build the canonical SensoryMemory path for one attachment."""
+ return f"{_ATTACHMENT_ROOT}.{event_id}.{_hash_attachment_key(key)}"
+
+
+def _attachment_context(event: Event, key: str, path: str | None = None) ->
str:
+ suffix = f", path={path}" if path is not None else ""
+ return f"event_id={event.id}, event_type={event.type}, key={key}{suffix}"
+
+
+def store_event_attachments(event: Event, ctx: RunnerContext) -> None:
+ """Store concrete attachment values in SensoryMemory and replace them with
refs."""
+ if not event.attachments:
+ return
+
+ if event.type == OutputEvent.EVENT_TYPE:
+ keys = ", ".join(sorted(event.attachments))
+ msg = f"Output events cannot carry attachments:
{_attachment_context(event, keys)}"
+ raise EventAttachmentError(msg)
+
Review Comment:
Python `store_event_attachments` currently rejects attachments on
`OutputEvent`, but this PR also adds attachments to cross-language OutputEvent
snapshots and copies attachments in `OutputEvent.from_event(...)`. This
mismatch will make `ctx.send_event(OutputEvent(..., attachments=...))` fail on
Python while the Java runtime accepts it. Either allow OutputEvent attachments
here or enforce the same restriction on both runtimes/APIs.
--
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]