From f3daad6da1c02a6b62939d5978c10c98e7c0c728 Mon Sep 17 00:00:00 2001 From: YS Liu Date: Wed, 25 Feb 2026 11:12:11 +0800 Subject: [PATCH] agent: ensure today's daily note file exists before LLM read_file - Add EnsureTodayFile() to create memory/YYYYMM/YYYYMMDD.md with date header if missing - Call it from GetMemoryContext() so path advertised in system prompt always exists - Avoids read_file tool failure when agent (or cron) tries to read today's note Co-authored-by: Cursor --- pkg/agent/memory.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/agent/memory.go b/pkg/agent/memory.go index dd5f4441c..36d60928f 100644 --- a/pkg/agent/memory.go +++ b/pkg/agent/memory.go @@ -98,6 +98,21 @@ func (ms *MemoryStore) AppendToday(content string) error { return os.WriteFile(todayFile, []byte(newContent), 0o644) } +// EnsureTodayFile creates today's daily note file (and month directory) if missing, +// so paths advertised in the system prompt (memory/YYYYMM/YYYYMMDD.md) exist when the LLM reads them. +func (ms *MemoryStore) EnsureTodayFile() { + todayFile := ms.getTodayFile() + monthDir := filepath.Dir(todayFile) + if err := os.MkdirAll(monthDir, 0o755); err != nil { + return + } + if _, err := os.Stat(todayFile); err == nil { + return // already exists + } + header := fmt.Sprintf("# %s\n\n", time.Now().Format("2006-01-02")) + _ = os.WriteFile(todayFile, []byte(header), 0o644) +} + // GetRecentDailyNotes returns daily notes from the last N days. // Contents are joined with "---" separator. func (ms *MemoryStore) GetRecentDailyNotes(days int) string { @@ -125,6 +140,7 @@ func (ms *MemoryStore) GetRecentDailyNotes(days int) string { // GetMemoryContext returns formatted memory context for the agent prompt. // Includes long-term memory and recent daily notes. func (ms *MemoryStore) GetMemoryContext() string { + ms.EnsureTodayFile() // so memory/YYYYMM/YYYYMMDD.md exists when LLM uses read_file longTerm := ms.ReadLongTerm() recentNotes := ms.GetRecentDailyNotes(3)