Skip to content

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

fix(memory): keep unparseable invalid_at null so memories stay valid (#20773)

Charles9d92751c358e
diff --git a/internal/service/memory_extractor.go b/internal/service/memory_extractor.go
index d3ba7bdd04..3f01fd51e1 100644
--- a/internal/service/memory_extractor.go
+++ b/internal/service/memory_extractor.go
@@ -545,10 +545,11 @@ func materializeMemoryExtraction(ctx context.Context, extracted []extractedMemor
for i, item := range extracted {
item.MessageID = generateRawMessageID(ctx)
item.ValidAt = formatMemoryTime(item.ValidAt, now)
- item.InvalidAt = strings.TrimSpace(item.InvalidAt)
- if item.InvalidAt != "" {
- item.InvalidAt = formatMemoryTime(item.InvalidAt, now)
- }
+ // An invalid_at the model made up normalizes to "", which persists as
+ // nil and leaves the memory valid (Python stores nothing via
+ // fallback=""). Falling back to now here would instead expire the
+ // memory at extraction time. valid_at keeps its now fallback above.
+ item.InvalidAt, _ = normalizeMemoryTime(item.InvalidAt)
materialized[i] = item
}
return materialized
diff --git a/internal/service/memory_extractor_test.go b/internal/service/memory_extractor_test.go
index 06a56e6340..84ea6a5243 100644
--- a/internal/service/memory_extractor_test.go
+++ b/internal/service/memory_extractor_test.go
@@ -131,6 +131,37 @@ func TestBuildExtractedMessageValidAtSemantics(t *testing.T) {
}
}
+// TestMaterializeMemoryExtractionInvalidAtStaysValidWhenUnparseable: an
+// invalid_at the LLM made up must not become "expired at extraction time".
+// Python stores nothing for it (fallback=""), so the Go side must persist nil
+// and keep the memory valid; parseable values keep their own wall clock.
+func TestMaterializeMemoryExtractionInvalidAtStaysValidWhenUnparseable(t *testing.T) {
+ msg := MemoryMessage{UserID: "u1", AgentID: "a1", SessionID: "s1"}
+ now := time.Date(2026, 8, 20, 10, 5, 0, 0, time.Local)
+
+ for _, test := range []struct {
+ name string
+ invalidAt string
+ want any
+ }{
+ {name: "unparseable", invalidAt: "last summer", want: nil},
+ {name: "empty", invalidAt: "", want: nil},
+ {name: "parseable", invalidAt: "2026-12-31T23:59:59+08:00", want: "2026-12-31 23:59:59"},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ materialized := materializeMemoryExtraction(t.Context(), []extractedMemory{{
+ MessageType: "fact",
+ Content: "likes coffee",
+ InvalidAt: test.invalidAt,
+ }}, now)
+ got := buildExtractedMessage(42, "mem-1", msg, materialized[0])["invalid_at"]
+ if got != test.want {
+ t.Fatalf("invalid_at = %#v, want %#v", got, test.want)
+ }
+ })
+ }
+}
+
// TestBuildExtractedMessageUsesStandardDocumentID ensures extracted items
// use the standard memoryID_messageID identity expected by GetMessageContent.
func TestBuildExtractedMessageUsesStandardDocumentID(t *testing.T) {