weiqingy commented on code in PR #887:
URL: https://github.com/apache/flink-agents/pull/887#discussion_r3633698568
##########
runtime/src/main/java/org/apache/flink/agents/runtime/context/RunnerContextImpl.java:
##########
@@ -169,6 +218,56 @@ public List<Event> drainEvents(Long timestamp) {
return list;
}
+ private void flushMemoryObservation() {
+ if (memoryContext == null) {
+ return;
+ }
+ // ALWAYS drain LTM records for this key (mailbox thread) — discarded
below if
+ // suppressed/disabled; skipping the drain would leak them into the
next action.
+ List<Map<String, Object>> ltmRecords = Collections.emptyList();
+ if (ltm != null) {
+ try {
+ ltmRecords =
+ MemoryEventBuilder.parseDrainedRecords(
+
ltm.drainObservationRecordsJson(observationKeyHash));
+ } catch (Exception e) {
+ LOG.debug("LTM observation drain failed; skipping", e);
Review Comment:
Nit, optional: the record side is the mirror of this fix.
`mem0_long_term_memory.py:343` and `:378` still swallow buffering failures at
`logger.debug`, whereas the drain side here now warns. Smaller blast radius (a
dropped observation for that op, not cross-action misattribution), and since
these observations are best-effort/audit-only, DEBUG may well be the intended
level.
##########
runtime/src/main/java/org/apache/flink/agents/runtime/memory/MemoryEventBuilder.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.runtime.memory;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.flink.agents.api.context.MemoryUpdate;
+import org.apache.flink.agents.api.event.LongTermGetEvent;
+import org.apache.flink.agents.api.event.LongTermSearchEvent;
+import org.apache.flink.agents.api.event.LongTermUpdateEvent;
+import org.apache.flink.agents.api.event.MemoryEvent;
+import org.apache.flink.agents.runtime.memory.MemoryEventSettings.MemoryOp;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Converts per-action memory records into {@link MemoryEvent}s at the action
finish boundary.
+ *
+ * <p>Values are dot-key flat maps; multiple operations on the same field
within one action fold to
+ * the last value (net effect). LTM search values map each query to its (last)
hit list.
+ */
+public final class MemoryEventBuilder {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(MemoryEventBuilder.class);
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private MemoryEventBuilder() {}
+
+ public static List<MemoryEvent> buildWriteEvents(
+ String key,
+ List<MemoryUpdate> sensoryUpdates,
+ List<MemoryUpdate> shortTermUpdates,
+ MemoryEventSettings settings) {
+ List<MemoryEvent> events = new ArrayList<>(2);
+ addFolded(events, key, MemoryOp.SENSORY_WRITE, sensoryUpdates,
settings);
+ addFolded(events, key, MemoryOp.SHORT_TERM_WRITE, shortTermUpdates,
settings);
+ return events;
+ }
+
+ public static List<MemoryEvent> buildReadEvents(
+ String key,
+ List<MemoryUpdate> sensoryReads,
+ List<MemoryUpdate> shortTermReads,
+ MemoryEventSettings settings) {
+ List<MemoryEvent> events = new ArrayList<>(2);
+ addFolded(events, key, MemoryOp.SENSORY_READ, sensoryReads, settings);
+ addFolded(events, key, MemoryOp.SHORT_TERM_READ, shortTermReads,
settings);
+ return events;
+ }
+
+ /**
+ * Builds LTM events from drained Python-side records ({@code
{op,set,id,value,key}} maps). The
+ * record-level {@code key} is a hashed partition key used only for drain
filtering; the emitted
+ * events carry the finishing action's readable {@code key} passed in here.
+ */
+ public static List<MemoryEvent> buildLtmEvents(
+ String key, List<Map<String, Object>> records, MemoryEventSettings
settings) {
+ Map<String, Object> updates = new LinkedHashMap<>();
+ Map<String, Object> gets = new LinkedHashMap<>();
+ Map<String, Object> searches = new LinkedHashMap<>();
+ for (Map<String, Object> record : records) {
+ Object op = record.get("op");
+ String set = record.get("set") != null ?
record.get("set").toString() : null;
+ if (set == null) {
+ continue;
+ }
+ Object id = record.get("id");
+ Object value = record.get("value");
+ if ("ADD".equals(op)) {
+ updates.put(set + "." + id, value);
+ } else if ("DELETE".equals(op)) {
+ updates.put(set + "." + id, null);
+ } else if ("DELETE_SET".equals(op)) {
+ updates.put(set, null);
Review Comment:
Thanks for working through all of these, they all look resolved to me. Left
one optional nit on the drain-logging thread, otherwise nothing further from me.
--
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]