diff --git a/internal/gateway/memory_mcp.go b/internal/gateway/memory_mcp.go index ce9995349..cf87656f5 100644 --- a/internal/gateway/memory_mcp.go +++ b/internal/gateway/memory_mcp.go @@ -469,4 +469,9 @@ func memoryForget(a memoryArgs) (string, error) { // put a memory on the day before or after the one the user lived // (Norethcy on X). Memories saved before this are stored in UTC ("…Z") // and are shown the same way. -func memoryStamp(t time.Time) string { return t.In(time.Local).Format("2006-01-02T15:04Z07:00") } +func memoryStamp(t time.Time) string { return t.In(memoryZone()).Format("2006-01-02T15:04Z07:00") } + +// memoryZone is the zone memoryStamp writes in: time.Local, a variable for +// tests, which must never swap time.Local itself (every goroutine still +// running reads it, so the swap is a race). +var memoryZone = func() *time.Location { return time.Local } diff --git a/internal/gateway/memory_mcp_test.go b/internal/gateway/memory_mcp_test.go index 3ee281331..08b6ffc37 100644 --- a/internal/gateway/memory_mcp_test.go +++ b/internal/gateway/memory_mcp_test.go @@ -6,7 +6,6 @@ import ( "net/http/httptest" "os" "path/filepath" - "regexp" "strings" "testing" "time" @@ -256,11 +255,13 @@ func TestRootDirs(t *testing.T) { // A memory is dated in the reader's own zone, with the time and its // offset: at 04:30 in Shanghai on the 11th, UTC is still the 10th // (Norethcy on X). One saved before, stored as "…Z", reads the same way. +// That a new one is stored in the local zone is memory's +// TestSaveKeepsTheLocalZone. func TestMemorySearchDatesInLocalTime(t *testing.T) { fresh(t) - oldLocal := time.Local - time.Local = time.FixedZone("CST", 8*3600) - t.Cleanup(func() { time.Local = oldLocal }) + oldZone := memoryZone + memoryZone = func() *time.Location { return time.FixedZone("CST", 8*3600) } + t.Cleanup(func() { memoryZone = oldZone }) old := `{"memories":[{"id":"m1","text":"Heavy tests run on mote","kind":"fact","scope":"global","origin":"agent","status":"active","created":"2026-11-10T20:30:00Z","updated":"2026-11-10T20:30:00Z"}]}` if err := os.MkdirAll(filepath.Dir(memory.Path()), 0o700); err != nil { @@ -274,16 +275,4 @@ func TestMemorySearchDatesInLocalTime(t *testing.T) { if bad || !strings.Contains(said, ", 2026-11-11T04:30+08:00: Heavy tests run on mote") { t.Fatalf("an old UTC memory: %s", said) } - - // a new one is kept with the zone it was saved in - if _, bad := memPost(t, h, memorySaveTool, `{"text":"Answer in Chinese","kind":"preference"}`); bad { - t.Fatal("save failed") - } - b, _ := os.ReadFile(memory.Path()) - if !strings.Contains(string(b), `"updated": "`) && !strings.Contains(string(b), `"updated":"`) { - t.Fatalf("no updated in %s", b) - } - if !regexp.MustCompile(`"updated":\s*"[^"]*\+08:00"`).Match(b) { - t.Fatalf("saved in UTC, not the local zone: %s", b) - } } diff --git a/internal/memory/memory_test.go b/internal/memory/memory_test.go index bf8f8ffdc..16d8bba7d 100644 --- a/internal/memory/memory_test.go +++ b/internal/memory/memory_test.go @@ -33,6 +33,21 @@ func save(t *testing.T, text, kind, scope string, tags ...string) *Memory { return m } +// A memory is stored with the zone it was saved in, not in UTC: read back +// as a day, UTC puts 04:30 in Shanghai on the day before (Norethcy on X). +func TestSaveKeepsTheLocalZone(t *testing.T) { + at := fresh(t) + *at = time.Date(2026, 11, 11, 4, 30, 0, 0, time.FixedZone("CST", 8*3600)) + save(t, "Answer in Chinese", Preference, Global) + b, err := os.ReadFile(Path()) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(b), `"updated": "2026-11-11T04:30:00+08:00"`) && !strings.Contains(string(b), `"updated":"2026-11-11T04:30:00+08:00"`) { + t.Fatalf("not saved in the local zone: %s", b) + } +} + func TestSaveKeepsOneOfTheSameText(t *testing.T) { fresh(t) a := save(t, "UI uses no gradients.", Preference, Global, "ui")