rosemarYuan commented on code in PR #887:
URL: https://github.com/apache/flink-agents/pull/887#discussion_r3734609244


##########
api/src/main/java/org/apache/flink/agents/api/event/MemoryEvent.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.api.event;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.core.json.JsonWriteFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.json.JsonMapper;
+import org.apache.flink.agents.api.Event;
+
+import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.function.BiFunction;
+
+/**
+ * Base class of the memory observation events. One event per (memory scope x 
operation) is emitted
+ * at the action finish boundary; each concrete subclass pins one of the seven 
operation kinds as
+ * its {@code type}.
+ *
+ * <p>Attributes: {@code key} — the textual form of the Flink key the 
operation belongs to; {@code
+ * value} — the operation's folded JSON value map. On the wire the event 
serializes as {@code {id,
+ * type, attributes: {key, value}}}.
+ */
+public abstract class MemoryEvent extends Event {
+
+    private static final ObjectMapper MAPPER =
+            
JsonMapper.builder().disable(JsonWriteFeature.WRITE_NAN_AS_STRINGS).build();
+
+    /**
+     * The seven memory event types, each mapped to its deserialization 
constructor ({@code (id,
+     * attributes)}). Single source of truth for both {@link #isMemoryType} 
and {@link #fromEvent}.
+     */
+    private static final Map<String, BiFunction<UUID, Map<String, Object>, 
MemoryEvent>> FACTORIES =
+            Map.<String, BiFunction<UUID, Map<String, Object>, MemoryEvent>>of(
+                    ShortTermWriteEvent.EVENT_TYPE, ShortTermWriteEvent::new,
+                    ShortTermReadEvent.EVENT_TYPE, ShortTermReadEvent::new,
+                    SensoryWriteEvent.EVENT_TYPE, SensoryWriteEvent::new,
+                    SensoryReadEvent.EVENT_TYPE, SensoryReadEvent::new,
+                    LongTermUpdateEvent.EVENT_TYPE, LongTermUpdateEvent::new,
+                    LongTermGetEvent.EVENT_TYPE, LongTermGetEvent::new,
+                    LongTermSearchEvent.EVENT_TYPE, LongTermSearchEvent::new);
+
+    /** Returns true iff {@code type} is one of the seven memory operation 
event types. */
+    public static boolean isMemoryType(String type) {
+        return type != null && FACTORIES.containsKey(type);
+    }
+
+    protected MemoryEvent(String type, String key, Map<String, Object> value) {
+        super(type, normalizeAttributes(key, value));
+    }
+
+    protected MemoryEvent(String type, Map<String, Object> attributes) {
+        super(type, normalizeAttributes(attributes));
+    }
+
+    /**
+     * Deserialization constructor path. The subclass {@code @JsonCreator}s 
are REQUIRED: instances
+     * land in ActionState.outputEvents and deserialize polymorphically via 
the {@code @class} mixin
+     * in ActionStateSerde — without an explicit creator Jackson cannot 
instantiate the subtype.
+     */
+    protected MemoryEvent(UUID id, String type, Map<String, Object> 
attributes) {
+        super(id, type, normalizeAttributes(attributes));
+    }
+
+    static Map<String, Object> normalizeAttributes(Map<String, Object> 
attributes) {
+        if (attributes == null) {
+            throw new IllegalArgumentException("Memory observation attributes 
must not be null.");
+        }
+        Object key = attributes.get("key");
+        if (!(key instanceof String)) {
+            throw new IllegalArgumentException(
+                    "Memory observation attribute 'key' must be a string.");
+        }
+        Object value = attributes.get("value");
+        if (!(value instanceof Map)) {
+            throw new IllegalArgumentException(
+                    "Memory observation attribute 'value' must be a map.");
+        }
+        Map<String, Object> normalized = new LinkedHashMap<>(attributes);
+        try {
+            normalized.put(
+                    "value", MAPPER.readValue(MAPPER.writeValueAsBytes(value), 
Object.class));
+        } catch (IOException | RuntimeException e) {
+            throw new IllegalArgumentException(
+                    "Memory observation value must be JSON serializable.", e);
+        }
+        return normalized;
+    }
+
+    static Map<String, Object> normalizeAttributes(String key, Map<String, 
Object> value) {
+        Map<String, Object> attributes = new LinkedHashMap<>();
+        attributes.put("key", key);
+        attributes.put("value", value);
+        return normalizeAttributes(attributes);
+    }
+
+    /** Converts a generic {@link Event} carrying a memory type into its typed 
subclass view. */
+    public static MemoryEvent fromEvent(Event event) {
+        BiFunction<UUID, Map<String, Object>, MemoryEvent> factory = 
FACTORIES.get(event.getType());
+        if (factory == null) {
+            throw new IllegalArgumentException("Not a memory event type: " + 
event.getType());
+        }
+        MemoryEvent result = factory.apply(event.getId(), 
event.getAttributes());

Review Comment:
   Thanks @wenjin272 for catching this! I updated both 
`MemoryEvent.fromEvent(...)` and `AgentRunBeginEvent.fromEvent(...)` to use the 
shared `reconstructFrom(...)` helper. This preserves the event ID, source 
timestamp, `upstreamEventId`, and `upstreamActionName` during typed 
reconstruction. I also added regression assertions for the upstream fields in 
both reconstruction paths. The changes have been pushed.



-- 
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]

Reply via email to