Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: second sort key for memory messages (#22004)

Lynndb74a2dc6308
diff --git a/internal/service/memory.go b/internal/service/memory.go
index 96cd971fe5..4d10fe7ee2 100644
--- a/internal/service/memory.go
+++ b/internal/service/memory.go
@@ -1980,6 +1980,9 @@ func (s *MemoryService) listMemoryMessages(ctx context.Context, memory *entity.M
sourceID := memoryMessageKey(message["source_id"])
groupedExtracts[sourceID] = append(groupedExtracts[sourceID], message)
}
+ for _, extracts := range groupedExtracts {
+ sortMemoryExtractMessages(extracts)
+ }
for _, message := range rawMessages {
messageID := memoryMessageKey(message["message_id"])
if extracts, ok := groupedExtracts[messageID]; ok {
@@ -2102,6 +2105,57 @@ func memoryMessageKey(value interface{}) string {
return strings.TrimSpace(fmt.Sprint(value))
}
+// sortMemoryExtractMessages orders the extracts grouped under one raw message
+// by the combined key (valid_at desc, message_id asc), porting the Python fix
+// in memory/services/messages.py (PR #21246). The engine query only sorts by
+// valid_at, so facts sharing a second-granularity valid_at — the common case,
+// since extraction stamps them with the conversation time — come back in
+// storage order, which reshuffles whenever a document is rewritten (e.g. an
+// enable/disable toggle). Two stable passes mirror the Python sort calls:
+// the tie-breaker ascending first, then the primary key descending.
+func sortMemoryExtractMessages(extracts []map[string]interface{}) {
+ sort.SliceStable(extracts, func(i, j int) bool {
+ return memoryMessageIDSortKey(extracts[i]["message_id"]) < memoryMessageIDSortKey(extracts[j]["message_id"])
+ })
+ sort.SliceStable(extracts, func(i, j int) bool {
+ return memoryValidAtSortKey(extracts[i]["valid_at"]) > memoryValidAtSortKey(extracts[j]["valid_at"])
+ })
+}
+
+// memoryMessageIDSortKey renders a message_id as a number for ordering.
+// Elasticsearch returns it as a JSON number and Infinity as a string, so a
+// plain string comparison would order "10" before "9". Missing or unparseable
+// values sort first, matching Python's _message_id_sort_key.
+func memoryMessageIDSortKey(value interface{}) int64 {
+ switch v := value.(type) {
+ case int64:
+ return v
+ case int:
+ return int64(v)
+ case float64:
+ return int64(v)
+ case string:
+ if id, err := strconv.ParseInt(strings.TrimSpace(v), 10, 64); err == nil {
+ return id
+ }
+ }
+ return -1
+}
+
+// memoryValidAtSortKey renders valid_at as a comparable string. Timestamps
+// are stored normalized as "YYYY-MM-DD HH:mm:ss", so lexicographic order is
+// chronological order. Missing values become "", sorting oldest, matching
+// Python's _valid_at_sort_key.
+func memoryValidAtSortKey(value interface{}) string {
+ if value == nil {
+ return ""
+ }
+ if s, ok := value.(string); ok {
+ return s
+ }
+ return fmt.Sprint(value)
+}
+
// isList checks if a value is a list or array type
// This is a utility function for type validation
//
diff --git a/internal/service/memory_message_test.go b/internal/service/memory_message_test.go
index 7f861da572..44e060ed0b 100644
--- a/internal/service/memory_message_test.go
+++ b/internal/service/memory_message_test.go
@@ -1002,3 +1002,69 @@ func TestMemorySearchRejectsInvalidSimilarityBeforeStorage(t *testing.T) {
}
}
}
+
+// The engine query behind the extract list only sorts by valid_at, so facts
+// sharing a second-granularity valid_at — the common case, since extraction
+// stamps them with the conversation time — come back in storage order, which
+// reshuffles whenever a document is rewritten (e.g. an enable/disable toggle).
+// This pins the ported Python fix (PR #21246): each message's extracts are
+// re-ordered in place by the combined key (valid_at desc, message_id asc).
+func TestSortMemoryExtractMessagesOrdersByValidAtThenMessageID(t *testing.T) {
+ t.Run("newest valid_at first, ties broken by message_id ascending", func(t *testing.T) {
+ extracts := []map[string]interface{}{
+ {"id": "extract-1", "message_id": 12, "valid_at": "2026-08-12 10:00:00"},
+ {"id": "extract-2", "message_id": 13, "valid_at": "2026-08-13 09:00:00"},
+ {"id": "extract-3", "message_id": 11, "valid_at": "2026-08-12 10:00:00"},
+ }
+ sortMemoryExtractMessages(extracts)
+ want := []string{"extract-2", "extract-3", "extract-1"}
+ for i, id := range want {
+ if extracts[i]["id"] != id {
+ t.Fatalf("position %d = %v, want %s (full order: %v)", i, extracts[i]["id"], id, ids(extracts))
+ }
+ }
+ })
+
+ t.Run("string ids compare numerically and missing valid_at sorts oldest", func(t *testing.T) {
+ // Infinity returns message_id as a string; a lexicographic tie-break
+ // would order "10" before "9". valid_at can be missing entirely; its
+ // empty key must lose to every real timestamp.
+ extracts := []map[string]interface{}{
+ {"id": "a", "message_id": "10", "valid_at": "2026-08-12 10:00:00"},
+ {"id": "b", "message_id": "9", "valid_at": "2026-08-12 10:00:00"},
+ {"id": "c", "message_id": "11", "valid_at": "2026-08-12 10:00:00"},
+ {"id": "d", "message_id": "12"},
+ }
+ sortMemoryExtractMessages(extracts)
+ want := []string{"b", "a", "c", "d"}
+ for i, id := range want {
+ if extracts[i]["id"] != id {
+ t.Fatalf("position %d = %v, want %s (full order: %v)", i, extracts[i]["id"], id, ids(extracts))
+ }
+ }
+ })
+
+ t.Run("groups sort independently", func(t *testing.T) {
+ first := []map[string]interface{}{
+ {"id": "first-old", "message_id": 2, "valid_at": "2026-08-01 00:00:00"},
+ {"id": "first-new", "message_id": 3, "valid_at": "2026-08-02 00:00:00"},
+ }
+ second := []map[string]interface{}{
+ {"id": "second-old", "message_id": 5, "valid_at": "2026-08-05 00:00:00"},
+ {"id": "second-new", "message_id": 6, "valid_at": "2026-08-06 00:00:00"},
+ }
+ sortMemoryExtractMessages(first)
+ sortMemoryExtractMessages(second)
+ if ids(first)[0] != "first-new" || ids(second)[0] != "second-new" {
+ t.Fatalf("groups interfered: first=%v second=%v", ids(first), ids(second))
+ }
+ })
+}
+
+func ids(extracts []map[string]interface{}) []string {
+ out := make([]string, 0, len(extracts))
+ for _, extract := range extracts {
+ out = append(out, fmt.Sprint(extract["id"]))
+ }
+ return out
+}