Skip to content

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

fix(memory): persist memory valid_at/forget_at as server-local time instead of UTC (#18538)

euvreef6b336690f6
diff --git a/internal/service/memory.go b/internal/service/memory.go
index 5b3b621bca..d247261a6e 100644
--- a/internal/service/memory.go
+++ b/internal/service/memory.go
@@ -847,7 +847,9 @@ func (s *MemoryService) ForgetMessage(ctx context.Context, userID string, memory
return errors.New("message store is not initialized")
}
- now := time.Now().UTC()
+ // forget_at is stamped as server-local wall clock, not UTC. forget_at_flt
+ // below is a Unix millisecond value and therefore zone-independent.
+ now := memoryNow()
forgetTime := now.Format("2006-01-02 15:04:05")
messageDocID := fmt.Sprintf("%s_%d", memoryID, messageID)
updates := map[string]interface{}{
diff --git a/internal/service/memory_extractor.go b/internal/service/memory_extractor.go
index 9a4250e39a..73ac4fa3cc 100644
--- a/internal/service/memory_extractor.go
+++ b/internal/service/memory_extractor.go
@@ -51,10 +51,15 @@ import (
"gorm.io/gorm"
)
-// memoryTimeLayout is the storage format for valid_at / invalid_at,
-// matching timestamp_to_date on the Python side.
+// memoryTimeLayout is the storage format for valid_at / invalid_at: a
+// server-local wall-clock string ("YYYY-MM-DD HH:MM:SS"), never UTC-shifted.
const memoryTimeLayout = "2006-01-02 15:04:05"
+// memoryNow is the wall clock behind every memory timestamp. Tests pin it
+// to a fixed instant in a fixed location so the server-local assertions are
+// deterministic on any host, including UTC CI runners.
+var memoryNow = time.Now
+
// ErrMemoryTaskTerminal marks a memory-task failure that already has a durable
// terminal outcome (task row absent, or progress already persisted as failed),
// so the caller must Ack rather than Nack/redeliver. Transient failures (DB
@@ -147,7 +152,8 @@ func (s *MemoryMessageService) saveExtractedToMemory(ctx context.Context, memory
}
s.updateTaskProgress(ctx, taskID, 0.5, fmt.Sprintf("Extracted %d messages from raw dialogue.", len(extracted)))
- now := time.Now().UTC()
+ // conversation_time is stamped as server-local wall clock, not UTC.
+ now := memoryNow()
messages := make([]map[string]any, 0, len(extracted))
for _, item := range extracted {
messages = append(messages, buildExtractedMessage(generateRawMessageID(ctx), sourceID, memoryID, msg, item, now))
@@ -172,7 +178,7 @@ func (s *MemoryMessageService) extractByLLM(ctx context.Context, mem *CreateMemo
}
conversation := fmt.Sprintf("User Input: %s\nAgent Response: %s", msg.UserInput, msg.AgentResponse)
- now := time.Now().UTC().Format(memoryTimeLayout)
+ now := memoryNow().Format(memoryTimeLayout)
messages := []models.Message{{Role: "system", Content: systemPrompt}}
if mem.UserPrompt != nil && strings.TrimSpace(*mem.UserPrompt) != "" {
messages = append(messages,
@@ -277,18 +283,20 @@ func parseMemoryExtraction(answer string, extractTypes []string) []extractedMemo
}
// formatMemoryTime normalizes an LLM-supplied timestamp (ISO 8601 or
-// already-formatted) into memoryTimeLayout. Unparseable or empty input
-// falls back to the supplied time.
+// already-formatted) into memoryTimeLayout. The parsed timestamp keeps its
+// own wall clock (no zone conversion). Unparseable or empty input falls
+// back to the supplied time formatted in its own location (server-local
+// when callers pass time.Now()).
func formatMemoryTime(value string, fallback time.Time) string {
v := strings.TrimSpace(value)
if v != "" {
for _, layout := range []string{time.RFC3339, "2006-01-02T15:04:05", "2006-01-02 15:04:05", "2006-01-02"} {
if t, err := time.Parse(layout, v); err == nil {
- return t.UTC().Format(memoryTimeLayout)
+ return t.Format(memoryTimeLayout)
}
}
}
- return fallback.UTC().Format(memoryTimeLayout)
+ return fallback.Format(memoryTimeLayout)
}
// updateTaskProgress stamps and persists task progress, mirroring
diff --git a/internal/service/memory_extractor_test.go b/internal/service/memory_extractor_test.go
new file mode 100644
index 0000000000..697ce3f8f0
--- /dev/null
+++ b/internal/service/memory_extractor_test.go
@@ -0,0 +1,95 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed 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.
+//
+
+// memory_extractor_test.go — timezone-semantics tests for the memory
+// extractor's timestamp handling (valid_at / invalid_at persistence).
+
+package service
+
+import (
+ "testing"
+ "time"
+)
+
+// pinMemoryNow fixes the memory wall clock at the given instant for the
+// duration of a test, restoring it afterwards.
+func pinMemoryNow(t *testing.T, instant time.Time) {
+ t.Helper()
+ orig := memoryNow
+ memoryNow = func() time.Time { return instant }
+ t.Cleanup(func() { memoryNow = orig })
+}
+
+// TestFormatMemoryTimeKeepsParsedWallClock: an offset-bearing or
+// Z-suffixed ISO timestamp must keep its own wall clock — never be
+// converted to UTC the way time.Time.UTC would.
+func TestFormatMemoryTimeKeepsParsedWallClock(t *testing.T) {
+ fallback := time.Date(2026, 8, 20, 0, 0, 0, 0, time.UTC)
+ for _, test := range []struct {
+ name string
+ value string
+ want string
+ }{
+ {name: "offset iso", value: "2026-08-20T10:05:00+08:00", want: "2026-08-20 10:05:00"},
+ {name: "offset iso with fractional seconds", value: "2026-08-20T10:05:00.123+08:00", want: "2026-08-20 10:05:00"},
+ {name: "zulu iso", value: "2026-08-20T02:05:00Z", want: "2026-08-20 02:05:00"},
+ {name: "naive iso", value: "2026-08-20T10:05:00", want: "2026-08-20 10:05:00"},
+ {name: "storage layout", value: "2026-08-20 10:05:00", want: "2026-08-20 10:05:00"},
+ {name: "date only", value: "2026-08-20", want: "2026-08-20 00:00:00"},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ if got := formatMemoryTime(test.value, fallback); got != test.want {
+ t.Fatalf("formatMemoryTime(%q) = %q, want %q", test.value, got, test.want)
+ }
+ })
+ }
+}
+
+// TestFormatMemoryTimeFallbackKeepsFallbackLocation: empty or unparseable
+// input falls back to the supplied time formatted in its own location, so
+// callers passing time.Now() persist server-local wall clock.
+func TestFormatMemoryTimeFallbackKeepsFallbackLocation(t *testing.T) {
+ fallback := time.Date(2026, 8, 20, 10, 5, 0, 0, time.FixedZone("UTC+8", 8*3600))
+ for _, value := range []string{"", " ", "not a timestamp"} {
+ if got := formatMemoryTime(value, fallback); got != "2026-08-20 10:05:00" {
+ t.Fatalf("formatMemoryTime(%q) = %q, want fallback wall clock %q", value, got, "2026-08-20 10:05:00")
+ }
+ }
+}
+
+// TestBuildExtractedMessageValidAtSemantics: extracted messages persist the
+// LLM-supplied wall clock as-is and fall back to the (server-local) now.
+func TestBuildExtractedMessageValidAtSemantics(t *testing.T) {
+ msg := MemoryMessage{UserID: "u1", AgentID: "a1", SessionID: "s1"}
+ now := time.Date(2026, 8, 20, 10, 5, 0, 0, time.Local)
+
+ withLLMTime := buildExtractedMessage(8, 42, "mem-1", msg, extractedMemory{
+ MessageType: "fact",
+ Content: "likes coffee",
+ ValidAt: "2026-08-20T10:05:00+08:00",
+ }, now)
+ if got := withLLMTime["valid_at"]; got != "2026-08-20 10:05:00" {
+ t.Fatalf("valid_at = %v, want LLM wall clock %q", got, "2026-08-20 10:05:00")
+ }
+
+ fallbackOnly := buildExtractedMessage(9, 42, "mem-1", msg, extractedMemory{
+ MessageType: "fact",
+ Content: "likes coffee",
+ }, now)
+ if got := fallbackOnly["valid_at"]; got != now.Format(memoryTimeLayout) {
+ t.Fatalf("valid_at = %v, want fallback %q", got, now.Format(memoryTimeLayout))
+ }
+}
diff --git a/internal/service/memory_message_service.go b/internal/service/memory_message_service.go
index 6863725c3b..adb1718d82 100644
--- a/internal/service/memory_message_service.go
+++ b/internal/service/memory_message_service.go
@@ -214,10 +214,11 @@ func buildRawMessage(
"agent_id": msg.AgentID,
"session_id": msg.SessionID,
"content": content,
- "valid_at": time.Now().UTC().Format("2006-01-02 15:04:05"),
- "invalid_at": nil,
- "forget_at": nil,
- "status": true,
+ // valid_at is stamped as server-local wall clock, not UTC.
+ "valid_at": memoryNow().Format(memoryTimeLayout),
+ "invalid_at": nil,
+ "forget_at": nil,
+ "status": true,
}
}
diff --git a/internal/service/memory_message_service_test.go b/internal/service/memory_message_service_test.go
index 011ba0c377..646d35d8f1 100644
--- a/internal/service/memory_message_service_test.go
+++ b/internal/service/memory_message_service_test.go
@@ -25,6 +25,7 @@ import (
"context"
"strings"
"testing"
+ "time"
)
// TestQueueSaveToMemoryTask_NilService: a nil receiver surfaces
@@ -67,6 +68,31 @@ func TestQueueSaveToMemoryTask_MissingAgentID(t *testing.T) {
}
}
+// TestBuildRawMessage_ValidAtServerLocal: valid_at is stamped as a
+// server-local wall-clock string, not UTC — otherwise memories asked at
+// 10:05 local show up as 02:05. The clock is pinned to a fixed instant in a
+// fixed non-UTC location so the assertion holds on any host, including UTC
+// CI runners.
+func TestBuildRawMessage_ValidAtServerLocal(t *testing.T) {
+ pinMemoryNow(t, time.Date(2026, 8, 20, 10, 5, 0, 0, time.FixedZone("UTC+8", 8*3600)))
+
+ raw := buildRawMessage(42, "mem-1", MemoryMessage{
+ UserID: "u1",
+ AgentID: "a1",
+ SessionID: "s1",
+ UserInput: "hi",
+ AgentResponse: "hello",
+ })
+
+ got, ok := raw["valid_at"].(string)
+ if !ok {
+ t.Fatalf("valid_at = %#v, want string", raw["valid_at"])
+ }
+ if want := "2026-08-20 10:05:00"; got != want {
+ t.Fatalf("valid_at = %q, want server-local wall clock %q (UTC-shifted would be %q)", got, want, "2026-08-20 02:05:00")
+ }
+}
+
// TestBuildRawMessage_EnvelopeShape: the row envelope carries
// every logical field the Python extractor reads. Storage-level
// fields (message_type_kwd, content_ltks, tokenized_content_ltks,
diff --git a/internal/service/memory_message_test.go b/internal/service/memory_message_test.go
index 8c7265240e..6a35f01c63 100644
--- a/internal/service/memory_message_test.go
+++ b/internal/service/memory_message_test.go
@@ -7,6 +7,7 @@ import (
"reflect"
"strings"
"testing"
+ "time"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
@@ -132,6 +133,12 @@ func setupMemoryMessageTestDB(t *testing.T) {
func TestForgetMessageKeepsCompanionFieldForNonOceanBaseEngines(t *testing.T) {
setupMemoryMessageTestDB(t)
+ // Pin the clock to a fixed instant in a fixed non-UTC location so the
+ // server-local assertions below hold on any host, including UTC CI
+ // runners.
+ pinned := time.Date(2026, 8, 20, 10, 5, 0, 0, time.FixedZone("UTC+8", 8*3600))
+ pinMemoryNow(t, pinned)
+
if err := dao.DB.Create(&entity.Memory{
ID: "memory-1",
Name: "Test memory",
@@ -171,11 +178,18 @@ func TestForgetMessageKeepsCompanionFieldForNonOceanBaseEngines(t *testing.T) {
}
if forgetAt, ok := docEngine.updateValue["forget_at"].(string); !ok || forgetAt == "" {
t.Fatalf("forget_at = %#v, want non-empty string", docEngine.updateValue["forget_at"])
+ } else if want := "2026-08-20 10:05:00"; forgetAt != want {
+ // forget_at must be the server-local wall clock, not a
+ // UTC-shifted one (which would be "2026-08-20 02:05:00").
+ t.Fatalf("forget_at = %q, want server-local wall clock %q", forgetAt, want)
}
- _, hasCompanion := docEngine.updateValue["forget_at_flt"]
+ companion, hasCompanion := docEngine.updateValue["forget_at_flt"]
if hasCompanion != test.wantForgetAtCompanion {
t.Fatalf("forget_at_flt present = %t, want %t", hasCompanion, test.wantForgetAtCompanion)
}
+ if hasCompanion && companion != pinned.UnixMilli() {
+ t.Fatalf("forget_at_flt = %v, want Unix milliseconds of the stamped instant %d", companion, pinned.UnixMilli())
+ }
})
}
}